Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"controller",
"cuda-sys",
"hip-sys",
"erased_lifetime",
"hyper",
"hyperactor",
Expand Down
16 changes: 16 additions & 0 deletions hip-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "hip-sys"
version = "0.0.0"
authors = ["Facebook"]
edition = "2021"
license = "MIT"
links = "hip"
description = "Rust FFI bindings for HIP libraries"

[dependencies]
cxx = "1.0.119"
serde = { version = "1.0.185", features = ["derive", "rc"] }

[build-dependencies]
bindgen = "0.70.1"
build_utils = { path = "../build_utils" }
107 changes: 107 additions & 0 deletions hip-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

use std::env;
use std::path::PathBuf;

use build_utils;
#[cfg(target_os = "macos")]
fn main() {}

#[cfg(not(target_os = "macos"))]
fn main() {
// Validate CUDA installation and get CUDA home path
// let cuda_home = match build_utils::validate_cuda_installation() {
// Ok(home) => home,
// Err(_) => {
// build_utils::print_cuda_error_help();
// std::process::exit(1);
// }
// };
let hip_home = "/opt/rocm";

// Start building the bindgen configuration
let mut builder = bindgen::Builder::default()
// The input header we would like to generate bindings for
.header("src/wrapper.h")
.clang_arg("-x")
.clang_arg("c++")
.clang_arg("-std=gnu++20")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Allow the specified functions and types
.allowlist_function("hip.*")
.allowlist_function("HIP.*")
.allowlist_type("hip.*")
.allowlist_type("HIP.*")
// Use newtype enum style
.default_enum_style(bindgen::EnumVariation::NewType {
is_bitfield: false,
is_global: false,
});

// Add HIP include path (we already validated it exists)
let hip_include_path = format!("{}/include", hip_home);
builder = builder.clang_arg(format!("-I{}", hip_include_path));

// Include headers and libs from the active environment.
let python_config = match build_utils::python_env_dirs_with_interpreter("python3") {
Ok(config) => config,
Err(_) => {
eprintln!("Warning: Failed to get Python environment directories");
build_utils::PythonConfig {
include_dir: None,
lib_dir: None,
}
}
};

if let Some(include_dir) = &python_config.include_dir {
builder = builder.clang_arg(format!("-I{}", include_dir));
}
if let Some(lib_dir) = &python_config.lib_dir {
println!("cargo::rustc-link-search=native={}", lib_dir);
// Set cargo metadata to inform dependent binaries about how to set their
// RPATH (see controller/build.rs for an example).
println!("cargo::metadata=LIB_PATH={}", lib_dir);
}

// Get CUDA library directory and emit link directives
// let cuda_lib_dir = match build_utils::get_cuda_lib_dir() {
// Ok(dir) => dir,
// Err(_) => {
// build_utils::print_cuda_lib_error_help();
// std::process::exit(1);
// }
// };
let hip_lib_dir ="/opt/rocm/lib";

println!("cargo:rustc-link-search=native={}", hip_lib_dir);
// println!("cargo:rustc-link-lib=cuda");
// println!("cargo:rustc-link-lib=cudart");
println!("cargo:rustc-link-lib=hip");

// Write the bindings to the $OUT_DIR/bindings.rs file
match env::var("OUT_DIR") {
Ok(out_dir) => {
let out_path = PathBuf::from(out_dir);
match builder.generate() {
Ok(bindings) => match bindings.write_to_file(out_path.join("bindings.rs")) {
Ok(_) => {
println!("cargo::rustc-cfg=cargo");
println!("cargo::rustc-check-cfg=cfg(cargo)");
}
Err(e) => eprintln!("Warning: Couldn't write bindings: {}", e),
},
Err(e) => eprintln!("Warning: Unable to generate bindings: {}", e),
}
}
Err(_) => {
println!("Note: OUT_DIR not set, skipping bindings file generation");
}
}
}
53 changes: 53 additions & 0 deletions hip-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

use cxx::ExternType;
use cxx::type_id;

/// SAFETY: bindings
unsafe impl ExternType for hipStream_t {
type Id = type_id!("hipStream_t");
type Kind = cxx::kind::Opaque;
}

// When building with cargo, this is actually the lib.rs file for a crate.
// Include the generated bindings.rs and suppress lints.
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
mod inner {
#[cfg(cargo)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

pub use inner::*;

#[cfg(test)]
mod tests {
use std::mem::MaybeUninit;

use super::*;

#[test]
fn sanity() {
// SAFETY: testing bindings
unsafe {
let mut version = MaybeUninit::<i32>::uninit();
let result = hipDriverGetVersion(version.as_mut_ptr());
assert_eq!(result, hipError_t(0));
}
}
}
11 changes: 11 additions & 0 deletions hip-sys/src/wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <hip/hip_runtime.h>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hipStream_t should be included here @mreso

1 change: 0 additions & 1 deletion rdmaxcel-sys-hip/src/rdmaxcel.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "hip/hip_runtime.h"
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
Expand Down
Loading