Skip to content

Commit 0915824

Browse files
committed
Set maptypes using offload metadata
1 parent 9f3ccf3 commit 0915824

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,17 @@ pub(crate) fn gen_define_handling<'ll, 'tcx>(
270270
let types = cx.func_params_types(cx.get_type_of_global(kernel));
271271
// It seems like non-pointer values are automatically mapped. So here, we focus on pointer (or
272272
// reference) types.
273-
let num_ptr_types = types
274-
.iter()
275-
.filter(|&x| matches!(cx.type_kind(x), rustc_codegen_ssa::common::TypeKind::Pointer))
276-
.count();
277-
278-
let ptr_sizes = types
273+
let ptr_meta = types
279274
.iter()
280275
.zip(metadata)
281276
.filter_map(|(&x, meta)| match cx.type_kind(x) {
282-
rustc_codegen_ssa::common::TypeKind::Pointer => Some(meta.payload_size),
277+
rustc_codegen_ssa::common::TypeKind::Pointer => Some(meta),
283278
_ => None,
284279
})
285-
.collect::<Vec<u64>>();
280+
.collect::<Vec<OffloadMetadata>>();
281+
282+
let ptr_sizes = ptr_meta.iter().map(|m| m.payload_size).collect::<Vec<_>>();
283+
let ptr_transfer = ptr_meta.iter().map(|m| m.mode as u64 | 0x20).collect::<Vec<_>>();
286284

287285
// We do not know their size anymore at this level, so hardcode a placeholder.
288286
// A follow-up pr will track these from the frontend, where we still have Rust types.
@@ -294,11 +292,8 @@ pub(crate) fn gen_define_handling<'ll, 'tcx>(
294292
// A non-mutable reference or pointer will be 1, an array that's not read, but fully overwritten
295293
// will be 2. For now, everything is 3, until we have our frontend set up.
296294
// 1+2+32: 1 (MapTo), 2 (MapFrom), 32 (Add one extra input ptr per function, to be used later).
297-
let memtransfer_types = add_priv_unnamed_arr(
298-
&cx,
299-
&format!(".offload_maptypes.{symbol}"),
300-
&vec![1 + 2 + 32; num_ptr_types],
301-
);
295+
let memtransfer_types =
296+
add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{symbol}"), &ptr_transfer);
302297

303298
// Next: For each function, generate these three entries. A weak constant,
304299
// the llvm.rodata entry name, and the llvm_offload_entries value

compiler/rustc_middle/src/ty/offload_meta.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ pub struct OffloadMetadata {
66
pub mode: TransferKind,
77
}
88

9+
// TODO(Sa4dUs): add `OMP_MAP_TARGET_PARAM = 0x20` flag only when needed
10+
#[repr(u64)]
11+
#[derive(Debug, Copy, Clone)]
912
pub enum TransferKind {
1013
FromGpu = 1,
1114
ToGpu = 2,
12-
Both = 3,
15+
Both = 1 + 2,
1316
}
1417

1518
impl OffloadMetadata {
@@ -18,7 +21,10 @@ impl OffloadMetadata {
1821
}
1922

2023
pub fn from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
21-
OffloadMetadata { payload_size: get_payload_size(tcx, ty), mode: TransferKind::Both }
24+
OffloadMetadata {
25+
payload_size: get_payload_size(tcx, ty),
26+
mode: TransferKind::from_ty(tcx, ty),
27+
}
2228
}
2329
}
2430

@@ -68,3 +74,49 @@ fn get_payload_size<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> u64 {
6874
.bytes(),
6975
}
7076
}
77+
78+
impl TransferKind {
79+
pub fn from_ty<'tcx>(_tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
80+
// TODO(Sa4dUs): this logic is probs not fully correct, but it works for now
81+
match ty.kind() {
82+
rustc_type_ir::TyKind::Bool
83+
| rustc_type_ir::TyKind::Char
84+
| rustc_type_ir::TyKind::Int(_)
85+
| rustc_type_ir::TyKind::Uint(_)
86+
| rustc_type_ir::TyKind::Float(_) => TransferKind::ToGpu,
87+
88+
rustc_type_ir::TyKind::Adt(_, _)
89+
| rustc_type_ir::TyKind::Tuple(_)
90+
| rustc_type_ir::TyKind::Array(_, _) => TransferKind::ToGpu,
91+
92+
rustc_type_ir::TyKind::RawPtr(_, rustc_ast::Mutability::Not)
93+
| rustc_type_ir::TyKind::Ref(_, _, rustc_ast::Mutability::Not) => TransferKind::ToGpu,
94+
95+
rustc_type_ir::TyKind::RawPtr(_, rustc_ast::Mutability::Mut)
96+
| rustc_type_ir::TyKind::Ref(_, _, rustc_ast::Mutability::Mut) => TransferKind::Both,
97+
98+
rustc_type_ir::TyKind::Slice(_)
99+
| rustc_type_ir::TyKind::Str
100+
| rustc_type_ir::TyKind::Dynamic(_, _) => TransferKind::Both,
101+
102+
rustc_type_ir::TyKind::FnDef(_, _)
103+
| rustc_type_ir::TyKind::FnPtr(_, _)
104+
| rustc_type_ir::TyKind::Closure(_, _)
105+
| rustc_type_ir::TyKind::CoroutineClosure(_, _)
106+
| rustc_type_ir::TyKind::Coroutine(_, _)
107+
| rustc_type_ir::TyKind::CoroutineWitness(_, _) => TransferKind::ToGpu,
108+
109+
rustc_type_ir::TyKind::Alias(_, _)
110+
| rustc_type_ir::TyKind::Param(_)
111+
| rustc_type_ir::TyKind::Bound(_, _)
112+
| rustc_type_ir::TyKind::Placeholder(_)
113+
| rustc_type_ir::TyKind::Infer(_)
114+
| rustc_type_ir::TyKind::Error(_) => TransferKind::ToGpu,
115+
116+
rustc_type_ir::TyKind::Never => TransferKind::ToGpu,
117+
rustc_type_ir::TyKind::Foreign(_) => TransferKind::Both,
118+
rustc_type_ir::TyKind::Pat(_, _) => TransferKind::Both,
119+
rustc_type_ir::TyKind::UnsafeBinder(_) => TransferKind::Both,
120+
}
121+
}
122+
}

tests/codegen-llvm/gpu_offload/offload_intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ compile-flags: -Zoffload=Enable -Zunstable-options -C opt-level=0 -Clto=fat
1+
//@ compile-flags: -Zoffload=Enable -Zunstable-options -C opt-level=3 -Clto=fat
22
//@ no-prefer-dynamic
33
//@ needs-enzyme
44

0 commit comments

Comments
 (0)