Skip to content

Commit 2ba8703

Browse files
committed
new code
1 parent 2fd855f commit 2ba8703

File tree

3 files changed

+174
-51
lines changed

3 files changed

+174
-51
lines changed

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 123 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,34 @@ pub(crate) fn handle_gpu_code<'ll>(
1818
// The offload memory transfer type for each kernel
1919
let mut o_types = vec![];
2020
let mut kernels = vec![];
21+
let mut region_ids = vec![];
2122
let offload_entry_ty = add_tgt_offload_entry(&cx);
2223
for num in 0..9 {
2324
let kernel = cx.get_function(&format!("kernel_{num}"));
2425
if let Some(kernel) = kernel {
25-
o_types.push(gen_define_handling(&cx, kernel, offload_entry_ty, num));
26+
let (o, k) = gen_define_handling(&cx, kernel, offload_entry_ty, num);
27+
o_types.push(o);
28+
region_ids.push(k);
2629
kernels.push(kernel);
2730
}
2831
}
2932

30-
gen_call_handling(&cx, &kernels, &o_types);
33+
gen_call_handling(&cx, &kernels, &o_types, &region_ids);
34+
}
35+
36+
// ; Function Attrs: nounwind
37+
// declare i32 @__tgt_target_kernel(ptr, i64, i32, i32, ptr, ptr) #2
38+
fn generate_launcher<'ll>(cx: &'ll SimpleCx<'_>) -> (&'ll llvm::Value, &'ll llvm::Type) {
39+
let tptr = cx.type_ptr();
40+
let ti64 = cx.type_i64();
41+
let ti32 = cx.type_i32();
42+
let args = vec![tptr, ti64, ti32, ti32, tptr, tptr];
43+
let tgt_fn_ty = cx.type_func(&args, ti32);
44+
let name = "__tgt_target_kernel";
45+
let tgt_decl = declare_offload_fn(&cx, name, tgt_fn_ty);
46+
let nounwind = llvm::AttributeKind::NoUnwind.create_attr(cx.llcx);
47+
attributes::apply_to_llfn(tgt_decl, Function, &[nounwind]);
48+
(tgt_decl, tgt_fn_ty)
3149
}
3250

3351
// What is our @1 here? A magic global, used in our data_{begin/update/end}_mapper:
@@ -83,7 +101,7 @@ pub(crate) fn add_tgt_offload_entry<'ll>(cx: &'ll SimpleCx<'_>) -> &'ll llvm::Ty
83101
offload_entry_ty
84102
}
85103

86-
fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
104+
fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) -> (&'ll llvm::Type, Vec<&'ll llvm::Type>) {
87105
let kernel_arguments_ty = cx.type_named_struct("struct.__tgt_kernel_arguments");
88106
let tptr = cx.type_ptr();
89107
let ti64 = cx.type_i64();
@@ -118,9 +136,7 @@ fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
118136
vec![ti32, ti32, tptr, tptr, tptr, tptr, tptr, tptr, ti64, ti64, tarr, tarr, ti32];
119137

120138
cx.set_struct_body(kernel_arguments_ty, &kernel_elements, false);
121-
// For now we don't handle kernels, so for now we just add a global dummy
122-
// to make sure that the __tgt_offload_entry is defined and handled correctly.
123-
cx.declare_global("my_struct_global2", kernel_arguments_ty);
139+
(kernel_arguments_ty, kernel_elements)
124140
}
125141

126142
fn gen_tgt_data_mappers<'ll>(
@@ -187,7 +203,7 @@ fn gen_define_handling<'ll>(
187203
kernel: &'ll llvm::Value,
188204
offload_entry_ty: &'ll llvm::Type,
189205
num: i64,
190-
) -> &'ll llvm::Value {
206+
) -> (&'ll llvm::Value, &'ll llvm::Value) {
191207
let types = cx.func_params_types(cx.get_type_of_global(kernel));
192208
// It seems like non-pointer values are automatically mapped. So here, we focus on pointer (or
193209
// reference) types.
@@ -205,10 +221,11 @@ fn gen_define_handling<'ll>(
205221
// or both to and from the gpu (=3). Other values shouldn't affect us for now.
206222
// A non-mutable reference or pointer will be 1, an array that's not read, but fully overwritten
207223
// will be 2. For now, everything is 3, until we have our frontend set up.
224+
// 1+2+32: 1 (MapTo), 2 (MapFrom), 32 (Add extra input ptr once, idk, figure out later)
208225
let o_types =
209-
add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{num}"), &vec![3; num_ptr_types]);
226+
add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{num}"), &vec![1+2+32; num_ptr_types]);
210227
// Next: For each function, generate these three entries. A weak constant,
211-
// the llvm.rodata entry name, and the omp_offloading_entries value
228+
// the llvm.rodata entry name, and the llvm_offload_entries value
212229

213230
let name = format!(".kernel_{num}.region_id");
214231
let initializer = cx.get_const_i8(0);
@@ -242,13 +259,13 @@ fn gen_define_handling<'ll>(
242259
llvm::set_global_constant(llglobal, true);
243260
llvm::set_linkage(llglobal, WeakAnyLinkage);
244261
llvm::set_initializer(llglobal, initializer);
245-
llvm::set_alignment(llglobal, Align::ONE);
246-
let c_section_name = CString::new(".omp_offloading_entries").unwrap();
262+
llvm::set_alignment(llglobal, Align::EIGHT);
263+
let c_section_name = CString::new("llvm_offload_entries").unwrap();
247264
llvm::set_section(llglobal, &c_section_name);
248-
o_types
265+
(o_types, region_id)
249266
}
250267

251-
fn declare_offload_fn<'ll>(
268+
pub(crate) fn declare_offload_fn<'ll>(
252269
cx: &'ll SimpleCx<'_>,
253270
name: &str,
254271
ty: &'ll llvm::Type,
@@ -287,15 +304,17 @@ fn gen_call_handling<'ll>(
287304
cx: &'ll SimpleCx<'_>,
288305
_kernels: &[&'ll llvm::Value],
289306
o_types: &[&'ll llvm::Value],
307+
region_ids: &[&'ll llvm::Value],
290308
) {
309+
let (tgt_decl, tgt_target_kernel_ty) = generate_launcher(&cx);
291310
// %struct.__tgt_bin_desc = type { i32, ptr, ptr, ptr }
292311
let tptr = cx.type_ptr();
293312
let ti32 = cx.type_i32();
294313
let tgt_bin_desc_ty = vec![ti32, tptr, tptr, tptr];
295314
let tgt_bin_desc = cx.type_named_struct("struct.__tgt_bin_desc");
296315
cx.set_struct_body(tgt_bin_desc, &tgt_bin_desc_ty, false);
297316

298-
gen_tgt_kernel_global(&cx);
317+
let (tgt_kernel_decl, tgt_kernel_types) = gen_tgt_kernel_global(&cx);
299318
let (begin_mapper_decl, _, end_mapper_decl, fn_ty) = gen_tgt_data_mappers(&cx);
300319

301320
let main_fn = cx.get_function("main");
@@ -329,29 +348,33 @@ fn gen_call_handling<'ll>(
329348
// These represent the sizes in bytes, e.g. the entry for `&[f64; 16]` will be 8*16.
330349
let ty2 = cx.type_array(cx.type_i64(), num_args);
331350
let a4 = builder.direct_alloca(ty2, Align::EIGHT, ".offload_sizes");
351+
352+
//%kernel_args = alloca %struct.__tgt_kernel_arguments, align 8
353+
let a5 = builder.direct_alloca(tgt_kernel_decl, Align::EIGHT, "kernel_args");
354+
355+
// Step 1)
356+
unsafe { llvm::LLVMRustPositionBefore(builder.llbuilder, kernel_call) };
357+
builder.memset(tgt_bin_desc_alloca, cx.get_const_i8(0), cx.get_const_i64(32), Align::EIGHT);
358+
332359
// Now we allocate once per function param, a copy to be passed to one of our maps.
333360
let mut vals = vec![];
334361
let mut geps = vec![];
335362
let i32_0 = cx.get_const_i32(0);
336363
for (index, in_ty) in types.iter().enumerate() {
337364
// get function arg, store it into the alloca, and read it.
338-
let p = llvm::get_param(called, index as u32);
339-
let name = llvm::get_value_name(p);
340-
let name = str::from_utf8(&name).unwrap();
341-
let arg_name = format!("{name}.addr");
342-
let alloca = builder.direct_alloca(in_ty, Align::EIGHT, &arg_name);
343-
344-
builder.store(p, alloca, Align::EIGHT);
345-
let val = builder.load(in_ty, alloca, Align::EIGHT);
346-
let gep = builder.inbounds_gep(cx.type_f32(), val, &[i32_0]);
347-
vals.push(val);
365+
//let p = llvm::get_param(called, index as u32);
366+
//let name = llvm::get_value_name(p);
367+
//let name = str::from_utf8(&name).unwrap();
368+
//let arg_name = format!("{name}.addr");
369+
//let alloca = builder.direct_alloca(in_ty, Align::EIGHT, &arg_name);
370+
371+
let v = unsafe { llvm::LLVMGetOperand(kernel_call, index as u32).unwrap() };
372+
let gep = builder.inbounds_gep(cx.type_f32(), v, &[i32_0]);
373+
vals.push(v);
374+
//vals.push(val);
348375
geps.push(gep);
349376
}
350377

351-
// Step 1)
352-
unsafe { llvm::LLVMRustPositionBefore(builder.llbuilder, kernel_call) };
353-
builder.memset(tgt_bin_desc_alloca, cx.get_const_i8(0), cx.get_const_i64(32), Align::EIGHT);
354-
355378
let mapper_fn_ty = cx.type_func(&[cx.type_ptr()], cx.type_void());
356379
let register_lib_decl = declare_offload_fn(&cx, "__tgt_register_lib", mapper_fn_ty);
357380
let unregister_lib_decl = declare_offload_fn(&cx, "__tgt_unregister_lib", mapper_fn_ty);
@@ -421,16 +444,87 @@ fn gen_call_handling<'ll>(
421444

422445
// Step 3)
423446
// Here we will add code for the actual kernel launches in a follow-up PR.
447+
//%28 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 0
448+
//store i32 3, ptr %28, align 4
449+
//%29 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 1
450+
//store i32 3, ptr %29, align 4
451+
//%30 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 2
452+
//store ptr %26, ptr %30, align 8
453+
//%31 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 3
454+
//store ptr %27, ptr %31, align 8
455+
//%32 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 4
456+
//store ptr @.offload_sizes, ptr %32, align 8
457+
//%33 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 5
458+
//store ptr @.offload_maptypes, ptr %33, align 8
459+
//%34 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 6
460+
//store ptr null, ptr %34, align 8
461+
//%35 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 7
462+
//store ptr null, ptr %35, align 8
463+
//%36 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 8
464+
//store i64 0, ptr %36, align 8
465+
//%37 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 9
466+
//store i64 0, ptr %37, align 8
467+
//%38 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 10
468+
//store [3 x i32] [i32 2097152, i32 0, i32 0], ptr %38, align 4
469+
//%39 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 11
470+
//store [3 x i32] [i32 256, i32 0, i32 0], ptr %39, align 4
471+
//%40 = getelementptr inbounds nuw %struct.__tgt_kernel_arguments, ptr %kernel_args, i32 0, i32 12
472+
//store i32 0, ptr %40, align 4
424473
// FIXME(offload): launch kernels
474+
let mut values = vec![];
475+
values.push((4, cx.get_const_i32(3)));
476+
values.push((4, cx.get_const_i32(num_args)));
477+
values.push((8, geps.0));
478+
values.push((8, geps.1));
479+
values.push((8, geps.2));
480+
values.push((8, o_types[0]));
481+
values.push((8, cx.const_null(cx.type_ptr())));
482+
values.push((8, cx.const_null(cx.type_ptr())));
483+
values.push((8, cx.get_const_i64(0)));
484+
values.push((8, cx.get_const_i64(0)));
485+
let ti32 = cx.type_i32();
486+
let ci32_0 = cx.get_const_i32(0);
487+
values.push((8, cx.const_array(ti32, &vec![cx.get_const_i32(2097152), ci32_0, ci32_0])));
488+
values.push((8, cx.const_array(ti32, &vec![cx.get_const_i32(256), ci32_0, ci32_0])));
489+
values.push((4, cx.get_const_i32(0)));
490+
491+
for (i, value) in values.iter().enumerate() {
492+
let ptr = builder.inbounds_gep(tgt_kernel_decl, a5, &[i32_0, cx.get_const_i32(i as u64)]);
493+
builder.store(value.1, ptr, Align::from_bytes(value.0).unwrap());
494+
}
495+
496+
let args = vec![
497+
s_ident_t,
498+
// MAX == -1
499+
cx.get_const_i64(u64::MAX),
500+
cx.get_const_i32(2097152),
501+
cx.get_const_i32(256),
502+
region_ids[0],
503+
a5,
504+
];
505+
let offload_success = builder.call(tgt_target_kernel_ty, tgt_decl, &args, None);
506+
// %41 = call i32 @__tgt_target_kernel(ptr @1, i64 -1, i32 2097152, i32 256, ptr @.kernel_1.region_id, ptr %kernel_args)
507+
unsafe {
508+
let next = llvm::LLVMGetNextInstruction(offload_success).unwrap();
509+
dbg!(&next);
510+
llvm::LLVMRustPositionAfter(builder.llbuilder, next);
511+
let called_kernel = llvm::LLVMGetCalledValue(next).unwrap();
512+
llvm::LLVMInstructionEraseFromParent(next);
513+
dbg!(&called_kernel);
514+
}
425515

426516
// Step 4)
427-
unsafe { llvm::LLVMRustPositionAfter(builder.llbuilder, kernel_call) };
517+
//unsafe { llvm::LLVMRustPositionAfter(builder.llbuilder, kernel_call) };
428518

429519
let geps = get_geps(&mut builder, &cx, ty, ty2, a1, a2, a4);
430520
generate_mapper_call(&mut builder, &cx, geps, o, end_mapper_decl, fn_ty, num_args, s_ident_t);
431521

432522
builder.call(mapper_fn_ty, unregister_lib_decl, &[tgt_bin_desc_alloca], None);
433523

524+
drop(builder);
525+
unsafe { llvm::LLVMDeleteFunction(called) };
526+
dbg!("survived");
527+
434528
// With this we generated the following begin and end mappers. We could easily generate the
435529
// update mapper in an update.
436530
// call void @__tgt_target_data_begin_mapper(ptr @1, i64 -1, i32 3, ptr %27, ptr %28, ptr %29, ptr @.offload_maptypes, ptr null, ptr null)

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ unsafe extern "C" {
12091209

12101210
// Operations on functions
12111211
pub(crate) fn LLVMSetFunctionCallConv(Fn: &Value, CC: c_uint);
1212+
pub(crate) fn LLVMDeleteFunction(Fn: &Value);
12121213

12131214
// Operations about llvm intrinsics
12141215
pub(crate) fn LLVMLookupIntrinsicID(Name: *const c_char, NameLen: size_t) -> c_uint;
@@ -1238,6 +1239,8 @@ unsafe extern "C" {
12381239
pub(crate) fn LLVMIsAInstruction(Val: &Value) -> Option<&Value>;
12391240
pub(crate) fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
12401241
pub(crate) fn LLVMGetOperand(Val: &Value, Index: c_uint) -> Option<&Value>;
1242+
pub(crate) fn LLVMGetNextInstruction(Val: &Value) -> Option<&Value>;
1243+
pub(crate) fn LLVMInstructionEraseFromParent(Val: &Value);
12411244

12421245
// Operations on call sites
12431246
pub(crate) fn LLVMSetInstructionCallConv(Instr: &Value, CC: c_uint);

0 commit comments

Comments
 (0)