Skip to content

Commit b5cc658

Browse files
committed
llvm: Use emulated TLS when appropriate for the target
Closes #24236.
1 parent c96c913 commit b5cc658

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

src/codegen/llvm.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ pub fn defaultSingleThreaded(target: *const std.Target) bool {
8585
return false;
8686
}
8787

88+
pub 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+
88101
pub 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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static const bool assertions_on = false;
8383
LLVMTargetMachineRef 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi
105105
ZIG_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

110110
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
111111

0 commit comments

Comments
 (0)