@@ -4,6 +4,7 @@ use llvm::Linkage::*;
4
4
use rustc_abi:: Align ;
5
5
use rustc_codegen_ssa:: back:: write:: CodegenContext ;
6
6
use rustc_codegen_ssa:: traits:: BaseTypeCodegenMethods ;
7
+ use tracing:: trace;
7
8
8
9
use crate :: builder:: SBuilder ;
9
10
use crate :: common:: AsCCharPtr ;
@@ -18,21 +19,41 @@ pub(crate) fn handle_gpu_code<'ll>(
18
19
// The offload memory transfer type for each kernel
19
20
let mut o_types = vec ! [ ] ;
20
21
let mut kernels = vec ! [ ] ;
22
+ let mut region_ids = vec ! [ ] ;
21
23
let offload_entry_ty = add_tgt_offload_entry ( & cx) ;
22
24
for num in 0 ..9 {
23
25
let kernel = cx. get_function ( & format ! ( "kernel_{num}" ) ) ;
24
26
if let Some ( kernel) = kernel {
25
- o_types. push ( gen_define_handling ( & cx, kernel, offload_entry_ty, num) ) ;
27
+ let ( o, k) = gen_define_handling ( & cx, kernel, offload_entry_ty, num) ;
28
+ o_types. push ( o) ;
29
+ region_ids. push ( k) ;
26
30
kernels. push ( kernel) ;
27
31
}
28
32
}
29
33
30
- gen_call_handling ( & cx, & kernels, & o_types) ;
34
+ gen_call_handling ( & cx, & kernels, & o_types, & region_ids) ;
35
+ }
36
+
37
+ // ; Function Attrs: nounwind
38
+ // declare i32 @__tgt_target_kernel(ptr, i64, i32, i32, ptr, ptr) #2
39
+ fn generate_launcher < ' ll > ( cx : & ' ll SimpleCx < ' _ > ) -> ( & ' ll llvm:: Value , & ' ll llvm:: Type ) {
40
+ let tptr = cx. type_ptr ( ) ;
41
+ let ti64 = cx. type_i64 ( ) ;
42
+ let ti32 = cx. type_i32 ( ) ;
43
+ let args = vec ! [ tptr, ti64, ti32, ti32, tptr, tptr] ;
44
+ let tgt_fn_ty = cx. type_func ( & args, ti32) ;
45
+ let name = "__tgt_target_kernel" ;
46
+ let tgt_decl = declare_offload_fn ( & cx, name, tgt_fn_ty) ;
47
+ let nounwind = llvm:: AttributeKind :: NoUnwind . create_attr ( cx. llcx ) ;
48
+ attributes:: apply_to_llfn ( tgt_decl, Function , & [ nounwind] ) ;
49
+ ( tgt_decl, tgt_fn_ty)
31
50
}
32
51
33
52
// What is our @1 here? A magic global, used in our data_{begin/update/end}_mapper:
34
53
// @0 = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1
35
54
// @1 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 0, i32 22, ptr @0 }, align 8
55
+ // FIXME(offload): @0 should include the file name (e.g. lib.rs) in which the function to be
56
+ // offloaded was defined.
36
57
fn generate_at_one < ' ll > ( cx : & ' ll SimpleCx < ' _ > ) -> & ' ll llvm:: Value {
37
58
// @0 = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1
38
59
let unknown_txt = ";unknown;unknown;0;0;;" ;
@@ -83,7 +104,7 @@ pub(crate) fn add_tgt_offload_entry<'ll>(cx: &'ll SimpleCx<'_>) -> &'ll llvm::Ty
83
104
offload_entry_ty
84
105
}
85
106
86
- fn gen_tgt_kernel_global < ' ll > ( cx : & ' ll SimpleCx < ' _ > ) {
107
+ fn gen_tgt_kernel_global < ' ll > ( cx : & ' ll SimpleCx < ' _ > ) -> & ' ll llvm :: Type {
87
108
let kernel_arguments_ty = cx. type_named_struct ( "struct.__tgt_kernel_arguments" ) ;
88
109
let tptr = cx. type_ptr ( ) ;
89
110
let ti64 = cx. type_i64 ( ) ;
@@ -107,7 +128,7 @@ fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
107
128
// uint64_t NoWait : 1; // Was this kernel spawned with a `nowait` clause.
108
129
// uint64_t IsCUDA : 1; // Was this kernel spawned via CUDA.
109
130
// uint64_t Unused : 62;
110
- // } Flags = {0, 0, 0};
131
+ // } Flags = {0, 0, 0}; // totals to 64 Bit, 8 Byte
111
132
// // The number of teams (for x,y,z dimension).
112
133
// uint32_t NumTeams[3] = {0, 0, 0};
113
134
// // The number of threads (for x,y,z dimension).
@@ -118,9 +139,7 @@ fn gen_tgt_kernel_global<'ll>(cx: &'ll SimpleCx<'_>) {
118
139
vec ! [ ti32, ti32, tptr, tptr, tptr, tptr, tptr, tptr, ti64, ti64, tarr, tarr, ti32] ;
119
140
120
141
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) ;
142
+ kernel_arguments_ty
124
143
}
125
144
126
145
fn gen_tgt_data_mappers < ' ll > (
@@ -187,7 +206,7 @@ fn gen_define_handling<'ll>(
187
206
kernel : & ' ll llvm:: Value ,
188
207
offload_entry_ty : & ' ll llvm:: Type ,
189
208
num : i64 ,
190
- ) -> & ' ll llvm:: Value {
209
+ ) -> ( & ' ll llvm:: Value , & ' ll llvm :: Value ) {
191
210
let types = cx. func_params_types ( cx. get_type_of_global ( kernel) ) ;
192
211
// It seems like non-pointer values are automatically mapped. So here, we focus on pointer (or
193
212
// reference) types.
@@ -205,10 +224,14 @@ fn gen_define_handling<'ll>(
205
224
// or both to and from the gpu (=3). Other values shouldn't affect us for now.
206
225
// A non-mutable reference or pointer will be 1, an array that's not read, but fully overwritten
207
226
// will be 2. For now, everything is 3, until we have our frontend set up.
208
- let o_types =
209
- add_priv_unnamed_arr ( & cx, & format ! ( ".offload_maptypes.{num}" ) , & vec ! [ 3 ; num_ptr_types] ) ;
227
+ // 1+2+32: 1 (MapTo), 2 (MapFrom), 32 (Add one extra input ptr per function, to be used later).
228
+ let o_types = add_priv_unnamed_arr (
229
+ & cx,
230
+ & format ! ( ".offload_maptypes.{num}" ) ,
231
+ & vec ! [ 1 + 2 + 32 ; num_ptr_types] ,
232
+ ) ;
210
233
// Next: For each function, generate these three entries. A weak constant,
211
- // the llvm.rodata entry name, and the omp_offloading_entries value
234
+ // the llvm.rodata entry name, and the llvm_offload_entries value
212
235
213
236
let name = format ! ( ".kernel_{num}.region_id" ) ;
214
237
let initializer = cx. get_const_i8 ( 0 ) ;
@@ -242,13 +265,13 @@ fn gen_define_handling<'ll>(
242
265
llvm:: set_global_constant ( llglobal, true ) ;
243
266
llvm:: set_linkage ( llglobal, WeakAnyLinkage ) ;
244
267
llvm:: set_initializer ( llglobal, initializer) ;
245
- llvm:: set_alignment ( llglobal, Align :: ONE ) ;
246
- let c_section_name = CString :: new ( ".omp_offloading_entries " ) . unwrap ( ) ;
268
+ llvm:: set_alignment ( llglobal, Align :: EIGHT ) ;
269
+ let c_section_name = CString :: new ( "llvm_offload_entries " ) . unwrap ( ) ;
247
270
llvm:: set_section ( llglobal, & c_section_name) ;
248
- o_types
271
+ ( o_types, region_id )
249
272
}
250
273
251
- fn declare_offload_fn < ' ll > (
274
+ pub ( crate ) fn declare_offload_fn < ' ll > (
252
275
cx : & ' ll SimpleCx < ' _ > ,
253
276
name : & str ,
254
277
ty : & ' ll llvm:: Type ,
@@ -287,15 +310,17 @@ fn gen_call_handling<'ll>(
287
310
cx : & ' ll SimpleCx < ' _ > ,
288
311
_kernels : & [ & ' ll llvm:: Value ] ,
289
312
o_types : & [ & ' ll llvm:: Value ] ,
313
+ region_ids : & [ & ' ll llvm:: Value ] ,
290
314
) {
315
+ let ( tgt_decl, tgt_target_kernel_ty) = generate_launcher ( & cx) ;
291
316
// %struct.__tgt_bin_desc = type { i32, ptr, ptr, ptr }
292
317
let tptr = cx. type_ptr ( ) ;
293
318
let ti32 = cx. type_i32 ( ) ;
294
319
let tgt_bin_desc_ty = vec ! [ ti32, tptr, tptr, tptr] ;
295
320
let tgt_bin_desc = cx. type_named_struct ( "struct.__tgt_bin_desc" ) ;
296
321
cx. set_struct_body ( tgt_bin_desc, & tgt_bin_desc_ty, false ) ;
297
322
298
- gen_tgt_kernel_global ( & cx) ;
323
+ let tgt_kernel_decl = gen_tgt_kernel_global ( & cx) ;
299
324
let ( begin_mapper_decl, _, end_mapper_decl, fn_ty) = gen_tgt_data_mappers ( & cx) ;
300
325
301
326
let main_fn = cx. get_function ( "main" ) ;
@@ -329,35 +354,32 @@ fn gen_call_handling<'ll>(
329
354
// These represent the sizes in bytes, e.g. the entry for `&[f64; 16]` will be 8*16.
330
355
let ty2 = cx. type_array ( cx. type_i64 ( ) , num_args) ;
331
356
let a4 = builder. direct_alloca ( ty2, Align :: EIGHT , ".offload_sizes" ) ;
357
+
358
+ //%kernel_args = alloca %struct.__tgt_kernel_arguments, align 8
359
+ let a5 = builder. direct_alloca ( tgt_kernel_decl, Align :: EIGHT , "kernel_args" ) ;
360
+
361
+ // Step 1)
362
+ unsafe { llvm:: LLVMRustPositionBefore ( builder. llbuilder , kernel_call) } ;
363
+ builder. memset ( tgt_bin_desc_alloca, cx. get_const_i8 ( 0 ) , cx. get_const_i64 ( 32 ) , Align :: EIGHT ) ;
364
+
332
365
// Now we allocate once per function param, a copy to be passed to one of our maps.
333
366
let mut vals = vec ! [ ] ;
334
367
let mut geps = vec ! [ ] ;
335
368
let i32_0 = cx. get_const_i32 ( 0 ) ;
336
- for ( index, in_ty) in types. iter ( ) . enumerate ( ) {
337
- // 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) ;
369
+ for index in 0 ..types. len ( ) {
370
+ let v = unsafe { llvm:: LLVMGetOperand ( kernel_call, index as u32 ) . unwrap ( ) } ;
371
+ let gep = builder. inbounds_gep ( cx. type_f32 ( ) , v, & [ i32_0] ) ;
372
+ vals. push ( v) ;
348
373
geps. push ( gep) ;
349
374
}
350
375
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
-
355
376
let mapper_fn_ty = cx. type_func ( & [ cx. type_ptr ( ) ] , cx. type_void ( ) ) ;
356
377
let register_lib_decl = declare_offload_fn ( & cx, "__tgt_register_lib" , mapper_fn_ty) ;
357
378
let unregister_lib_decl = declare_offload_fn ( & cx, "__tgt_unregister_lib" , mapper_fn_ty) ;
358
379
let init_ty = cx. type_func ( & [ ] , cx. type_void ( ) ) ;
359
380
let init_rtls_decl = declare_offload_fn ( cx, "__tgt_init_all_rtls" , init_ty) ;
360
381
382
+ // FIXME(offload): Later we want to add them to the wrapper code, rather than our main function.
361
383
// call void @__tgt_register_lib(ptr noundef %6)
362
384
builder. call ( mapper_fn_ty, register_lib_decl, & [ tgt_bin_desc_alloca] , None ) ;
363
385
// call void @__tgt_init_all_rtls()
@@ -420,17 +442,60 @@ fn gen_call_handling<'ll>(
420
442
generate_mapper_call ( & mut builder, & cx, geps, o, begin_mapper_decl, fn_ty, num_args, s_ident_t) ;
421
443
422
444
// Step 3)
423
- // Here we will add code for the actual kernel launches in a follow-up PR.
424
- // FIXME(offload): launch kernels
445
+ let mut values = vec ! [ ] ;
446
+ let offload_version = cx. get_const_i32 ( 3 ) ;
447
+ values. push ( ( 4 , offload_version) ) ;
448
+ values. push ( ( 4 , cx. get_const_i32 ( num_args) ) ) ;
449
+ values. push ( ( 8 , geps. 0 ) ) ;
450
+ values. push ( ( 8 , geps. 1 ) ) ;
451
+ values. push ( ( 8 , geps. 2 ) ) ;
452
+ values. push ( ( 8 , o_types[ 0 ] ) ) ;
453
+ // The next two are debug infos. FIXME(offload) set them
454
+ values. push ( ( 8 , cx. const_null ( cx. type_ptr ( ) ) ) ) ;
455
+ values. push ( ( 8 , cx. const_null ( cx. type_ptr ( ) ) ) ) ;
456
+ values. push ( ( 8 , cx. get_const_i64 ( 0 ) ) ) ;
457
+ values. push ( ( 8 , cx. get_const_i64 ( 0 ) ) ) ;
458
+ let ti32 = cx. type_i32 ( ) ;
459
+ let ci32_0 = cx. get_const_i32 ( 0 ) ;
460
+ values. push ( ( 4 , cx. const_array ( ti32, & vec ! [ cx. get_const_i32( 2097152 ) , ci32_0, ci32_0] ) ) ) ;
461
+ values. push ( ( 4 , cx. const_array ( ti32, & vec ! [ cx. get_const_i32( 256 ) , ci32_0, ci32_0] ) ) ) ;
462
+ values. push ( ( 4 , cx. get_const_i32 ( 0 ) ) ) ;
463
+
464
+ for ( i, value) in values. iter ( ) . enumerate ( ) {
465
+ let ptr = builder. inbounds_gep ( tgt_kernel_decl, a5, & [ i32_0, cx. get_const_i32 ( i as u64 ) ] ) ;
466
+ builder. store ( value. 1 , ptr, Align :: from_bytes ( value. 0 ) . unwrap ( ) ) ;
467
+ }
468
+
469
+ let args = vec ! [
470
+ s_ident_t,
471
+ // MAX == -1
472
+ cx. get_const_i64( u64 :: MAX ) ,
473
+ cx. get_const_i32( 2097152 ) ,
474
+ cx. get_const_i32( 256 ) ,
475
+ region_ids[ 0 ] ,
476
+ a5,
477
+ ] ;
478
+ let offload_success = builder. call ( tgt_target_kernel_ty, tgt_decl, & args, None ) ;
479
+ // %41 = call i32 @__tgt_target_kernel(ptr @1, i64 -1, i32 2097152, i32 256, ptr @.kernel_1.region_id, ptr %kernel_args)
480
+ unsafe {
481
+ let next = llvm:: LLVMGetNextInstruction ( offload_success) . unwrap ( ) ;
482
+ llvm:: LLVMRustPositionAfter ( builder. llbuilder , next) ;
483
+ let called_kernel = llvm:: LLVMGetCalledValue ( next) . unwrap ( ) ;
484
+ llvm:: LLVMInstructionEraseFromParent ( next) ;
485
+ trace ! ( & called_kernel) ;
486
+ }
425
487
426
488
// Step 4)
427
- unsafe { llvm:: LLVMRustPositionAfter ( builder. llbuilder , kernel_call) } ;
489
+ // unsafe { llvm::LLVMRustPositionAfter(builder.llbuilder, kernel_call) };
428
490
429
491
let geps = get_geps ( & mut builder, & cx, ty, ty2, a1, a2, a4) ;
430
492
generate_mapper_call ( & mut builder, & cx, geps, o, end_mapper_decl, fn_ty, num_args, s_ident_t) ;
431
493
432
494
builder. call ( mapper_fn_ty, unregister_lib_decl, & [ tgt_bin_desc_alloca] , None ) ;
433
495
496
+ drop ( builder) ;
497
+ unsafe { llvm:: LLVMDeleteFunction ( called) } ;
498
+
434
499
// With this we generated the following begin and end mappers. We could easily generate the
435
500
// update mapper in an update.
436
501
// 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)
0 commit comments