@@ -57,80 +57,6 @@ impl<'ll> OffloadGlobals<'ll> {
5757 }
5858}
5959
60- // We need to register offload before using it. We also should unregister it once we are done, for
61- // good measures. Previously we have done so before and after each individual offload intrinsic
62- // call, but that comes at a performance cost. The repeated (un)register calls might also confuse
63- // the LLVM ompOpt pass, which tries to move operations to a better location. The easiest solution,
64- // which we copy from clang, is to just have those two calls once, in the global ctor/dtor section
65- // of the final binary.
66- pub ( crate ) fn register_offload < ' ll > ( cx : & CodegenCx < ' ll , ' _ > ) {
67- // First we check quickly whether we already have done our setup, in which case we return early.
68- // Shouldn't be needed for correctness.
69- let register_lib_name = "__tgt_register_lib" ;
70- if cx. get_function ( register_lib_name) . is_some ( ) {
71- return ;
72- }
73-
74- let reg_lib_decl = cx. type_func ( & [ cx. type_ptr ( ) ] , cx. type_void ( ) ) ;
75- let register_lib = declare_offload_fn ( & cx, register_lib_name, reg_lib_decl) ;
76- let unregister_lib = declare_offload_fn ( & cx, "__tgt_unregister_lib" , reg_lib_decl) ;
77-
78- let ptr_null = cx. const_null ( cx. type_ptr ( ) ) ;
79- let const_struct = cx. const_struct ( & [ cx. get_const_i32 ( 0 ) , ptr_null, ptr_null, ptr_null] , false ) ;
80- let omp_descriptor =
81- add_global ( cx, ".omp_offloading.descriptor" , const_struct, InternalLinkage ) ;
82- // @.omp_offloading.descriptor = internal constant %__tgt_bin_desc { i32 1, ptr @.omp_offloading.device_images, ptr @__start_llvm_offload_entries, ptr @__stop_llvm_offload_entries }
83- // @.omp_offloading.descriptor = internal constant %__tgt_bin_desc { i32 0, ptr null, ptr null, ptr null }
84-
85- let atexit = cx. type_func ( & [ cx. type_ptr ( ) ] , cx. type_i32 ( ) ) ;
86- let atexit_fn = declare_offload_fn ( cx, "atexit" , atexit) ;
87-
88- // FIXME(offload): Drop this, once we fully automated our offload compilation pipeline, since
89- // LLVM will initialize them for us if it sees gpu kernels being registered.
90- let init_ty = cx. type_func ( & [ ] , cx. type_void ( ) ) ;
91- let init_rtls = declare_offload_fn ( cx, "__tgt_init_all_rtls" , init_ty) ;
92-
93- let desc_ty = cx. type_func ( & [ ] , cx. type_void ( ) ) ;
94- let reg_name = ".omp_offloading.descriptor_reg" ;
95- let unreg_name = ".omp_offloading.descriptor_unreg" ;
96- let desc_reg_fn = declare_offload_fn ( cx, reg_name, desc_ty) ;
97- let desc_unreg_fn = declare_offload_fn ( cx, unreg_name, desc_ty) ;
98- llvm:: set_linkage ( desc_reg_fn, InternalLinkage ) ;
99- llvm:: set_linkage ( desc_unreg_fn, InternalLinkage ) ;
100- llvm:: set_section ( desc_reg_fn, c".text.startup" ) ;
101- llvm:: set_section ( desc_unreg_fn, c".text.startup" ) ;
102-
103- // define internal void @.omp_offloading.descriptor_reg() section ".text.startup" {
104- // entry:
105- // call void @__tgt_register_lib(ptr @.omp_offloading.descriptor)
106- // call void @__tgt_init_all_rtls()
107- // %0 = call i32 @atexit(ptr @.omp_offloading.descriptor_unreg)
108- // ret void
109- // }
110- let bb = Builder :: append_block ( cx, desc_reg_fn, "entry" ) ;
111- let mut a = Builder :: build ( cx, bb) ;
112- a. call ( reg_lib_decl, None , None , register_lib, & [ omp_descriptor] , None , None ) ;
113- a. call ( init_ty, None , None , init_rtls, & [ ] , None , None ) ;
114- a. call ( atexit, None , None , atexit_fn, & [ desc_unreg_fn] , None , None ) ;
115- a. ret_void ( ) ;
116-
117- // define internal void @.omp_offloading.descriptor_unreg() section ".text.startup" {
118- // entry:
119- // call void @__tgt_unregister_lib(ptr @.omp_offloading.descriptor)
120- // ret void
121- // }
122- let bb = Builder :: append_block ( cx, desc_unreg_fn, "entry" ) ;
123- let mut a = Builder :: build ( cx, bb) ;
124- a. call ( reg_lib_decl, None , None , unregister_lib, & [ omp_descriptor] , None , None ) ;
125- a. ret_void ( ) ;
126-
127- // @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 101, ptr @.omp_offloading.descriptor_reg, ptr null }]
128- let args = vec ! [ cx. get_const_i32( 101 ) , desc_reg_fn, ptr_null] ;
129- let const_struct = cx. const_struct ( & args, false ) ;
130- let arr = cx. const_array ( cx. val_ty ( const_struct) , & [ const_struct] ) ;
131- add_global ( cx, "llvm.global_ctors" , arr, AppendingLinkage ) ;
132- }
133-
13460pub ( crate ) struct OffloadKernelDims < ' ll > {
13561 num_workgroups : & ' ll Value ,
13662 threads_per_block : & ' ll Value ,
0 commit comments