Skip to content

Commit 1d8d1e7

Browse files
committed
make src test compile, not generating llvm yet
1 parent c8c4c83 commit 1d8d1e7

6 files changed

Lines changed: 107 additions & 0 deletions

File tree

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_abi::Align;
66
use rustc_codegen_ssa::MemFlags;
77
use rustc_codegen_ssa::common::TypeKind;
88
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
9+
use rustc_codegen_ssa::mir::place::PlaceValue;
910
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, BuilderMethods};
1011
use rustc_middle::bug;
1112
use rustc_middle::ty::offload_meta::{MappingFlags, OffloadMetadata, OffloadSize};
@@ -151,6 +152,7 @@ impl<'ll> OffloadKernelDims<'ll> {
151152
let OperandValue::Ref(place) = workgroup_op.val else {
152153
bug!("expected array operand by reference");
153154
};
155+
let place: PlaceValue<&Value> = place;
154156
let workgroup_val = builder.load(arr_ty, place.llval, four);
155157

156158
let OperandValue::Ref(place) = thread_op.val else {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 66 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,60 @@ 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+
1888+
//let OperandValue::Ref(place) = args[0].val else {
1889+
// bug!("expected array operand by reference");
1890+
//};
1891+
//let arg_ty = place.layout.llvm_type(bx.cx);
1892+
////let workgroup_val = bx.load(arr_ty, place.llval, four);
1893+
1894+
//let a = OffloadMetadata::from_ty(tcx, arg_ty);
1895+
1896+
let arg = &args[0];
1897+
1898+
let arg_ty = arg.layout.ty;
1899+
1900+
let ty::Ref(_, pointee_ty, _) = *arg_ty.kind() else {
1901+
bug!("expected preload argument to be a reference, got {arg_ty:?}");
1902+
};
1903+
1904+
let meta = OffloadMetadata::from_ty(tcx, pointee_ty);
1905+
1906+
let offload_globals_ref = cx.offload_globals.borrow();
1907+
let offload_globals = match offload_globals_ref.as_ref() {
1908+
Some(globals) => globals,
1909+
None => {
1910+
// Offload is not initialized, cannot continue
1911+
return;
1912+
}
1913+
};
1914+
}
1915+
18501916
// Generates the LLVM code to offload a Rust function to a target device (e.g., GPU).
18511917
// For each kernel call, it generates the necessary globals (including metadata such as
18521918
// 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ 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;
238240

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

library/core/src/offload.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::marker::PhantomData;
2+
3+
#[lang = "preload_type"]
4+
#[unstable(feature = "offload", issue = "124509")]
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+
#[unstable(feature = "offload", issue = "124509")]
12+
pub struct PreloadMut<'a, T: ?Sized> {
13+
cpu_ptr: *mut T,
14+
_marker: PhantomData<&'a mut T>,
15+
}
16+
17+
#[lang = "preload"]
18+
#[unstable(feature = "offload", issue = "124509")]
19+
pub fn preload<'a, T: ?Sized>(x: &'a T) -> Preload<'a, T> {
20+
Preload { cpu_ptr: x as *const T, _marker: PhantomData }
21+
}
22+
23+
#[lang = "preload_mut"]
24+
#[unstable(feature = "offload", issue = "124509")]
25+
pub fn preload_mut<'a, T: ?Sized>(x: &'a mut T) -> PreloadMut<'a, T> {
26+
PreloadMut { cpu_ptr: x as *mut T, _marker: PhantomData }
27+
}

0 commit comments

Comments
 (0)