Skip to content

Commit b153d0a

Browse files
committed
Set maptypes using offload metadata
1 parent 23722aa commit b153d0a

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
@@ -267,19 +267,17 @@ pub(crate) fn gen_define_handling<'ll, 'tcx>(
267267
let types = cx.func_params_types(cx.get_type_of_global(kernel));
268268
// It seems like non-pointer values are automatically mapped. So here, we focus on pointer (or
269269
// reference) types.
270-
let num_ptr_types = types
271-
.iter()
272-
.filter(|&x| matches!(cx.type_kind(x), rustc_codegen_ssa::common::TypeKind::Pointer))
273-
.count();
274-
275-
let ptr_sizes = types
270+
let ptr_meta = types
276271
.iter()
277272
.zip(metadata)
278273
.filter_map(|(&x, meta)| match cx.type_kind(x) {
279-
rustc_codegen_ssa::common::TypeKind::Pointer => Some(meta.payload_size),
274+
rustc_codegen_ssa::common::TypeKind::Pointer => Some(meta),
280275
_ => None,
281276
})
282-
.collect::<Vec<u64>>();
277+
.collect::<Vec<OffloadMetadata>>();
278+
279+
let ptr_sizes = ptr_meta.iter().map(|m| m.payload_size).collect::<Vec<_>>();
280+
let ptr_transfer = ptr_meta.iter().map(|m| m.mode as u64 | 0x20).collect::<Vec<_>>();
283281

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

300295
// Next: For each function, generate these three entries. A weak constant,
301296
// 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)