-
Notifications
You must be signed in to change notification settings - Fork 13.5k
gpu offload host code generation #142097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
gpu offload host code generation #142097
Changes from all commits
3debe14
22a499f
3dc0c3b
c1e5961
5d3ce24
4a85567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,6 +653,7 @@ pub(crate) fn run_pass_manager( | |
// We then run the llvm_optimize function a second time, to optimize the code which we generated | ||
// in the enzyme differentiation pass. | ||
let enable_ad = config.autodiff.contains(&config::AutoDiff::Enable); | ||
let enable_gpu = config.offload.contains(&config::Offload::Enable); | ||
let stage = if thin { | ||
write::AutodiffStage::PreAD | ||
} else { | ||
|
@@ -667,6 +668,12 @@ pub(crate) fn run_pass_manager( | |
write::llvm_optimize(cgcx, dcx, module, None, config, opt_level, opt_stage, stage)?; | ||
} | ||
|
||
if cfg!(llvm_enzyme) && enable_gpu && !thin { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no dependency of offload on Enzyme, but since I think I'm supposed to gate my features, for now I'll just re-use the ones from Enzyme. |
||
let cx = | ||
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size); | ||
crate::builder::gpu_offload::handle_gpu_code(cgcx, &cx); | ||
} | ||
|
||
if cfg!(llvm_enzyme) && enable_ad && !thin { | ||
let cx = | ||
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use std::ops::Deref; | |
use std::{iter, ptr}; | ||
|
||
pub(crate) mod autodiff; | ||
pub(crate) mod gpu_offload; | ||
|
||
use libc::{c_char, c_uint, size_t}; | ||
use rustc_abi as abi; | ||
|
@@ -117,6 +118,70 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> { | |
} | ||
bx | ||
} | ||
|
||
pub(crate) fn my_alloca2(&mut self, ty: &'ll Type, align: Align, name: &str) -> &'ll Value { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll find a better name for it. |
||
let val = unsafe { | ||
let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED); | ||
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint); | ||
// Cast to default addrspace if necessary | ||
llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx.type_ptr(), UNNAMED) | ||
}; | ||
if name != "" { | ||
let name = std::ffi::CString::new(name).unwrap(); | ||
llvm::set_value_name(val, &name.as_bytes()); | ||
} | ||
val | ||
} | ||
|
||
pub(crate) fn inbounds_gep( | ||
&mut self, | ||
ty: &'ll Type, | ||
ptr: &'ll Value, | ||
indices: &[&'ll Value], | ||
) -> &'ll Value { | ||
unsafe { | ||
llvm::LLVMBuildGEPWithNoWrapFlags( | ||
self.llbuilder, | ||
ty, | ||
ptr, | ||
indices.as_ptr(), | ||
indices.len() as c_uint, | ||
UNNAMED, | ||
GEPNoWrapFlags::InBounds, | ||
) | ||
} | ||
} | ||
|
||
pub(crate) fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value { | ||
debug!("Store {:?} -> {:?}", val, ptr); | ||
assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer); | ||
unsafe { | ||
let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr); | ||
llvm::LLVMSetAlignment(store, align.bytes() as c_uint); | ||
store | ||
} | ||
} | ||
|
||
pub(crate) fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value { | ||
unsafe { | ||
let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED); | ||
llvm::LLVMSetAlignment(load, align.bytes() as c_uint); | ||
load | ||
} | ||
} | ||
|
||
fn memset(&mut self, ptr: &'ll Value, fill_byte: &'ll Value, size: &'ll Value, align: Align) { | ||
unsafe { | ||
llvm::LLVMRustBuildMemSet( | ||
self.llbuilder, | ||
ptr, | ||
align.bytes() as c_uint, | ||
fill_byte, | ||
size, | ||
false, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Empty string, to be used where LLVM expects an instruction name, indicating | ||
|
Uh oh!
There was an error while loading. Please reload this page.