1
1
use std:: ffi:: CString ;
2
2
3
3
use llvm:: Linkage :: * ;
4
- use rustc_abi:: Align ;
4
+ use rustc_abi:: { AddressSpace , Align } ;
5
5
use rustc_codegen_ssa:: back:: write:: CodegenContext ;
6
6
use rustc_codegen_ssa:: traits:: BaseTypeCodegenMethods ;
7
7
8
8
use crate :: builder:: gpu_offload:: * ;
9
- use crate :: llvm:: { self , Visibility } ;
9
+ use crate :: llvm:: { self , Linkage , Type , Value , Visibility } ;
10
10
use crate :: { LlvmCodegenBackend , ModuleLlvm , SimpleCx } ;
11
11
12
12
pub ( crate ) fn create_struct_ty < ' ll > (
@@ -22,6 +22,23 @@ pub(crate) fn create_struct_ty<'ll>(
22
22
}
23
23
}
24
24
25
+ pub ( crate ) fn add_global_decl < ' ll > (
26
+ cx : & SimpleCx < ' ll > ,
27
+ ty : & ' ll Type ,
28
+ name : & str ,
29
+ l : Linkage ,
30
+ hidden : bool ,
31
+ ) -> & ' ll llvm:: Value {
32
+ let c_name = CString :: new ( name) . unwrap ( ) ;
33
+ let llglobal: & ' ll llvm:: Value = llvm:: add_global ( cx. llmod , ty, & c_name) ;
34
+ llvm:: set_global_constant ( llglobal, true ) ;
35
+ llvm:: set_linkage ( llglobal, l) ;
36
+ if hidden {
37
+ llvm:: set_visibility ( llglobal, Visibility :: Hidden ) ;
38
+ }
39
+ llglobal
40
+ }
41
+
25
42
// We don't copy types from other functions because we generate a new module and context.
26
43
// Bringing in types from other contexts would likely cause issues.
27
44
pub ( crate ) fn gen_image_wrapper_module ( cgcx : & CodegenContext < LlvmCodegenBackend > ) {
@@ -32,6 +49,7 @@ pub(crate) fn gen_image_wrapper_module(cgcx: &CodegenContext<LlvmCodegenBackend>
32
49
ModuleLlvm :: new_simple ( name, dl_cstr. into_raw ( ) , target_cstr. into_raw ( ) , & cgcx) . unwrap ( ) ;
33
50
let cx = SimpleCx :: new ( m. llmod ( ) , m. llcx , cgcx. pointer_size ) ;
34
51
let tptr = cx. type_ptr ( ) ;
52
+ let tptr1 = cx. type_ptr_ext ( AddressSpace ( 1 ) ) ;
35
53
let ti64 = cx. type_i64 ( ) ;
36
54
let ti32 = cx. type_i32 ( ) ;
37
55
let ti16 = cx. type_i16 ( ) ;
@@ -44,28 +62,22 @@ pub(crate) fn gen_image_wrapper_module(cgcx: &CodegenContext<LlvmCodegenBackend>
44
62
let offload_entry_ty = add_tgt_offload_entry ( & cx) ;
45
63
let offload_entry_arr = cx. type_array ( offload_entry_ty, 0 ) ;
46
64
47
- let c_name = CString :: new ( "__start_omp_offloading_entries" ) . unwrap ( ) ;
48
- let llglobal = llvm:: add_global ( cx. llmod , offload_entry_arr, & c_name) ;
49
- llvm:: set_global_constant ( llglobal, true ) ;
50
- llvm:: set_linkage ( llglobal, ExternalLinkage ) ;
51
- llvm:: set_visibility ( llglobal, Visibility :: Hidden ) ;
52
- let c_name = CString :: new ( "__stop_omp_offloading_entries" ) . unwrap ( ) ;
53
- let llglobal = llvm:: add_global ( cx. llmod , offload_entry_arr, & c_name) ;
54
- llvm:: set_global_constant ( llglobal, true ) ;
55
- llvm:: set_linkage ( llglobal, ExternalLinkage ) ;
56
- llvm:: set_visibility ( llglobal, Visibility :: Hidden ) ;
65
+ let name = "__start_omp_offloading_entries" ;
66
+ add_global_decl ( & cx, offload_entry_arr, name, ExternalLinkage , true ) ;
67
+
68
+ let name = "__stop_omp_offloading_entries" ;
69
+ add_global_decl ( & cx, offload_entry_arr, name, ExternalLinkage , true ) ;
70
+
71
+ let name = "__dummy.omp_offloading_entries" ;
72
+ let llglobal = add_global_decl ( & cx, offload_entry_arr, name, InternalLinkage , false ) ;
57
73
58
- let c_name = CString :: new ( "__dummy.omp_offloading_entries" ) . unwrap ( ) ;
59
- let llglobal = llvm:: add_global ( cx. llmod , offload_entry_arr, & c_name) ;
60
- llvm:: set_global_constant ( llglobal, true ) ;
61
- llvm:: set_linkage ( llglobal, InternalLinkage ) ;
62
74
let c_section_name = CString :: new ( "omp_offloading_entries" ) . unwrap ( ) ;
63
75
llvm:: set_section ( llglobal, & c_section_name) ;
64
76
let zeroinit = cx. const_null ( offload_entry_arr) ;
65
77
llvm:: set_initializer ( llglobal, zeroinit) ;
66
78
67
79
CString :: new ( "llvm.compiler.used" ) . unwrap ( ) ;
68
- let arr_val = cx. const_array ( tptr , & [ llglobal] ) ;
80
+ let arr_val = cx. const_array ( tptr1 , & [ llglobal] ) ;
69
81
let c_section_name = CString :: new ( "llvm.metadata" ) . unwrap ( ) ;
70
82
let llglobal = add_global ( & cx, "llvm.compiler.used" , arr_val, AppendingLinkage ) ;
71
83
llvm:: set_section ( llglobal, & c_section_name) ;
@@ -74,30 +86,9 @@ pub(crate) fn gen_image_wrapper_module(cgcx: &CodegenContext<LlvmCodegenBackend>
74
86
//@llvm.compiler.used = appending global [1 x ptr] [ptr @__dummy.omp_offloading_entries], section "llvm.metadata"
75
87
76
88
let mapper_fn_ty = cx. type_func ( & [ tptr] , cx. type_void ( ) ) ;
77
- crate :: declare:: declare_simple_fn (
78
- & cx,
79
- & "__tgt_unregister_lib" ,
80
- llvm:: CallConv :: CCallConv ,
81
- llvm:: UnnamedAddr :: No ,
82
- llvm:: Visibility :: Default ,
83
- mapper_fn_ty,
84
- ) ;
85
- crate :: declare:: declare_simple_fn (
86
- & cx,
87
- & "__tgt_register_lib" ,
88
- llvm:: CallConv :: CCallConv ,
89
- llvm:: UnnamedAddr :: No ,
90
- llvm:: Visibility :: Default ,
91
- mapper_fn_ty,
92
- ) ;
93
- crate :: declare:: declare_simple_fn (
94
- & cx,
95
- & "atexit" ,
96
- llvm:: CallConv :: CCallConv ,
97
- llvm:: UnnamedAddr :: No ,
98
- llvm:: Visibility :: Default ,
99
- cx. type_func ( & [ tptr] , ti32) ,
100
- ) ;
89
+ declare_offload_fn ( & cx, & "__tgt_register_lib" , mapper_fn_ty) ;
90
+ declare_offload_fn ( & cx, & "__tgt_unregister_lib" , mapper_fn_ty) ;
91
+ declare_offload_fn ( & cx, & "atexit" , cx. type_func ( & [ tptr] , ti32) ) ;
101
92
102
93
let unknown_txt = "11111111111111" ;
103
94
let c_entry_name = CString :: new ( unknown_txt) . unwrap ( ) ;
0 commit comments