Skip to content

Commit da102aa

Browse files
committed
wip
1 parent 4e1a17d commit da102aa

5 files changed

Lines changed: 79 additions & 0 deletions

File tree

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
235235
codegen_offload(self, tcx, instance, args);
236236
return Ok(());
237237
}
238+
sym::preload => {
239+
if tcx.sess.opts.unstable_opts.offload.is_empty() {
240+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutEnable);
241+
}
242+
243+
if tcx.sess.lto() != rustc_session::config::Lto::Fat {
244+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutFatLTO);
245+
}
246+
247+
codegen_offload_preload(self, tcx, instance, args);
248+
return Ok(());
249+
}
238250
sym::is_val_statically_known => {
239251
if let OperandValue::Immediate(imm) = args[0].val {
240252
self.call_intrinsic(
@@ -1847,6 +1859,43 @@ fn codegen_autodiff<'ll, 'tcx>(
18471859
);
18481860
}
18491861

1862+
// For each PreLoad *call*, we now use some of our previous declared globals to move data to the gpu.
1863+
// For now, we only handle the data transfer part of it. Consecutive calls become a no-op on the
1864+
// LLVM side.
1865+
//
1866+
// Current steps:
1867+
// 0. Alloca some variables for the following steps
1868+
// 1. set insert point before PreLoad call.
1869+
// 2. generate all the GEPS and stores, to be used in 3)
1870+
// 3. generate __tgt_target_data_begin calls to move data to the GPU
1871+
//
1872+
// unchanged: keep kernel call. Later move the kernel to the GPU
1873+
//
1874+
// 4. set insert point after kernel call.
1875+
// 5. generate all the GEPS and stores, to be used in 6)
1876+
// 6. generate __tgt_target_data_end calls to move data from the GPU
1877+
fn codegen_offload_preload<'ll, 'tcx>(
1878+
bx: &mut Builder<'_, 'll, 'tcx>,
1879+
tcx: TyCtxt<'tcx>,
1880+
instance: ty::Instance<'tcx>,
1881+
args: &[OperandRef<'tcx, &'ll Value>],
1882+
) {
1883+
let cx = bx.cx;
1884+
//let fn_args = instance.args;
1885+
1886+
register_offload(cx);
1887+
let a = OffloadMetadata::from_ty(tcx, args[0]);
1888+
1889+
let offload_globals_ref = cx.offload_globals.borrow();
1890+
let offload_globals = match offload_globals_ref.as_ref() {
1891+
Some(globals) => globals,
1892+
None => {
1893+
// Offload is not initialized, cannot continue
1894+
return;
1895+
}
1896+
};
1897+
}
1898+
18501899
// Generates the LLVM code to offload a Rust function to a target device (e.g., GPU).
18511900
// For each kernel call, it generates the necessary globals (including metadata such as
18521901
// size and pass mode), manages memory mapping to and from the device, handles all

compiler/rustc_hir/src/lang_items.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ language_item_table! {
325325
DropGlue, sym::drop_glue, drop_glue_fn, Target::Fn, GenericRequirement::Exact(1);
326326
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
327327

328+
// Compiler-generated mapper functions for gpu offloading
329+
PreloadStruct, sym::preload_type, preload_type, Target::Struct, GenericRequirement::None;
330+
PreloadMutStruct, sym::preload_mut_type, preload_mut_type, Target::Struct, GenericRequirement::None;
331+
PreloadFn, sym::preload, preload_fn, Target::Fn, GenericRequirement::None;
332+
PreloadMutFn, sym::preload_mut, preload_mut_fn, Target::Fn, GenericRequirement::None;
333+
328334
/// For all binary crates without `#![no_main]`, Rust will generate a "main" function.
329335
/// The exact name and signature are target-dependent. The "main" function will invoke
330336
/// this lang item, passing it the `argc` and `argv` (or null, if those don't exist

compiler/rustc_span/src/symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,10 @@ symbols! {
15571557
prefetch_write_instruction,
15581558
prefix_nops,
15591559
preg,
1560+
preload,
1561+
preload_mut,
1562+
preload_type,
1563+
preload_mut_type,
15601564
prelude,
15611565
prelude_import,
15621566
preserves_flags,

library/core/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ pub mod autodiff {
235235
#[unstable(feature = "autodiff", issue = "124509")]
236236
pub use crate::macros::builtin::{autodiff_forward, autodiff_reverse};
237237
}
238+
#[unstable(feature = "offload", issue = "124509")]
239+
pub mod offload;
240+
//pub mod offload {
241+
// #[unstable(feature = "offload", issue = "124509")]
242+
// pub use crate::offload::{Preload, PreloadMut};
243+
//}
238244

239245
#[unstable(feature = "contracts", issue = "128044")]
240246
pub mod contracts;

library/core/src/offload.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::marker::PhantomData;
2+
use crate::ptr;
3+
4+
#[lang = "preload_type"]
5+
pub struct Preload<'a, T: ?Sized> {
6+
cpu_ptr: *const T,
7+
_marker: PhantomData<&'a T>,
8+
}
9+
10+
#[lang = "preload_mut_type"]
11+
pub struct PreloadMut<'a, T: ?Sized> {
12+
cpu_ptr: *mut T,
13+
_marker: PhantomData<&'a mut T>,
14+
}

0 commit comments

Comments
 (0)