Skip to content

Commit a94ef31

Browse files
committed
make src test compile, not generating llvm yet
1 parent cced03b commit a94ef31

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
@@ -243,6 +243,18 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
243243
// offload *has* a return type, but somehow works without mentioning the place
244244
return IntrinsicResult::WroteIntoPlace;
245245
}
246+
sym::preload => {
247+
if tcx.sess.opts.unstable_opts.offload.is_empty() {
248+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutEnable);
249+
}
250+
251+
if tcx.sess.lto() != rustc_session::config::Lto::Fat {
252+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutFatLTO);
253+
}
254+
255+
codegen_offload_preload(self, tcx, instance, args);
256+
return IntrinsicResult::WroteIntoPlace;
257+
}
246258
sym::is_val_statically_known => {
247259
if let OperandValue::Immediate(imm) = args[0].val {
248260
self.call_intrinsic(
@@ -1905,6 +1917,60 @@ fn codegen_autodiff<'ll, 'tcx>(
19051917
);
19061918
}
19071919

1920+
// For each PreLoad *call*, we now use some of our previous declared globals to move data to the gpu.
1921+
// For now, we only handle the data transfer part of it. Consecutive calls become a no-op on the
1922+
// LLVM side.
1923+
//
1924+
// Current steps:
1925+
// 0. Alloca some variables for the following steps
1926+
// 1. set insert point before PreLoad call.
1927+
// 2. generate all the GEPS and stores, to be used in 3)
1928+
// 3. generate __tgt_target_data_begin calls to move data to the GPU
1929+
//
1930+
// unchanged: keep kernel call. Later move the kernel to the GPU
1931+
//
1932+
// 4. set insert point after kernel call.
1933+
// 5. generate all the GEPS and stores, to be used in 6)
1934+
// 6. generate __tgt_target_data_end calls to move data from the GPU
1935+
fn codegen_offload_preload<'ll, 'tcx>(
1936+
bx: &mut Builder<'_, 'll, 'tcx>,
1937+
tcx: TyCtxt<'tcx>,
1938+
instance: ty::Instance<'tcx>,
1939+
args: &[OperandRef<'tcx, &'ll Value>],
1940+
) {
1941+
let cx = bx.cx;
1942+
//let fn_args = instance.args;
1943+
1944+
register_offload(cx);
1945+
1946+
//let OperandValue::Ref(place) = args[0].val else {
1947+
// bug!("expected array operand by reference");
1948+
//};
1949+
//let arg_ty = place.layout.llvm_type(bx.cx);
1950+
////let workgroup_val = bx.load(arr_ty, place.llval, four);
1951+
1952+
//let a = OffloadMetadata::from_ty(tcx, arg_ty);
1953+
1954+
let arg = &args[0];
1955+
1956+
let arg_ty = arg.layout.ty;
1957+
1958+
let ty::Ref(_, pointee_ty, _) = *arg_ty.kind() else {
1959+
bug!("expected preload argument to be a reference, got {arg_ty:?}");
1960+
};
1961+
1962+
let meta = OffloadMetadata::from_ty(tcx, pointee_ty);
1963+
1964+
let offload_globals_ref = cx.offload_globals.borrow();
1965+
let offload_globals = match offload_globals_ref.as_ref() {
1966+
Some(globals) => globals,
1967+
None => {
1968+
// Offload is not initialized, cannot continue
1969+
return;
1970+
}
1971+
};
1972+
}
1973+
19081974
// Generates the LLVM code to offload a Rust function to a target device (e.g., GPU).
19091975
// For each kernel call, it generates the necessary globals (including metadata such as
19101976
// 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
@@ -1560,6 +1560,10 @@ symbols! {
15601560
prefetch_write_instruction,
15611561
prefix_nops,
15621562
preg,
1563+
preload,
1564+
preload_mut,
1565+
preload_type,
1566+
preload_mut_type,
15631567
prelude,
15641568
prelude_import,
15651569
preserves_flags,

library/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ pub mod autodiff {
233233
#[unstable(feature = "autodiff", issue = "124509")]
234234
pub use crate::macros::builtin::{autodiff_forward, autodiff_reverse};
235235
}
236+
#[unstable(feature = "offload", issue = "124509")]
237+
pub mod offload;
236238

237239
#[unstable(feature = "contracts", issue = "128044")]
238240
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)