authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-07-06 22:19:08+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-07-07 07:23:24+02:00
logb5cc658ab4113b4db4f709dbec258910696990bd
tree7864e25eda40d6850582da4109e988b72df5bd2d
parentc96c913bab00bcae42991244381ddcc8b2377552

llvm: Use emulated TLS when appropriate for the target

Closes #24236.

6 files changed, 22 insertions(+), 3 deletions(-)

src/codegen/llvm.zig+1
......@@ -1047,6 +1047,7 @@ pub const Object = struct {
10471047 comp.data_sections,
10481048 float_abi,
10491049 if (target_util.llvmMachineAbi(&comp.root_mod.resolved_target.result)) |s| s.ptr else null,
1050 target_util.useEmulatedTls(&comp.root_mod.resolved_target.result),
10501051 );
10511052 errdefer target_machine.dispose();
10521053
src/codegen/llvm/bindings.zig+1
......@@ -79,6 +79,7 @@ pub const TargetMachine = opaque {
7979 data_sections: bool,
8080 float_abi: FloatABI,
8181 abi_name: ?[*:0]const u8,
82 emulated_tls: bool,
8283 ) *TargetMachine;
8384
8485 pub const dispose = LLVMDisposeTargetMachine;
src/main.zig+1-1
......@@ -6324,7 +6324,7 @@ fn cmdDumpLlvmInts(
63246324 if (llvm.Target.getFromTriple(triple, &target, &error_message) != .False) @panic("bad");
63256325 break :t target;
63266326 };
6327 const tm = llvm.TargetMachine.create(target, triple, null, null, .None, .Default, .Default, false, false, .Default, null);
6327 const tm = llvm.TargetMachine.create(target, triple, null, null, .None, .Default, .Default, false, false, .Default, null, false);
63286328 const dl = tm.createTargetDataLayout();
63296329 const context = llvm.Context.create();
63306330
src/target.zig+13
......@@ -85,6 +85,19 @@ pub fn defaultSingleThreaded(target: *const std.Target) bool {
8585 return false;
8686}
8787
88pub fn useEmulatedTls(target: *const std.Target) bool {
89 if (target.abi.isAndroid()) {
90 if (target.os.version_range.linux.android < 29) return true;
91 return false;
92 }
93 if (target.abi.isOpenHarmony()) return true;
94 return switch (target.os.tag) {
95 .openbsd => true,
96 .windows => target.abi == .cygnus,
97 else => false,
98 };
99}
100
88101pub fn hasValgrindSupport(target: *const std.Target, backend: std.builtin.CompilerBackend) bool {
89102 // We can't currently output the necessary Valgrind client request assembly when using the C
90103 // backend and compiling with an MSVC-like compiler.
src/zig_llvm.cpp+5-1
......@@ -83,7 +83,7 @@ static const bool assertions_on = false;
8383LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
8484 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
8585 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMFloatABI float_abi,
86 const char *abi_name)
86 const char *abi_name, bool emulated_tls)
8787{
8888 std::optional<Reloc::Model> RM;
8989 switch (Reloc){
......@@ -149,6 +149,10 @@ LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Tri
149149 opt.MCOptions.ABIName = abi_name;
150150 }
151151
152 if (emulated_tls) {
153 opt.EmulatedTLS = true;
154 }
155
152156 TargetMachine *TM = reinterpret_cast<Target*>(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
153157 OL, JIT);
154158 return reinterpret_cast<LLVMTargetMachineRef>(TM);
src/zig_llvm.h+1-1
......@@ -105,7 +105,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
105105ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
106106 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
107107 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMFloatABI float_abi,
108 const char *abi_name);
108 const char *abi_name, bool emulated_tls);
109109
110110ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
111111