以下代码目前在 lli 中不起作用:
//main.cpp
extern thread_local int tls;
int main() {
tls = 42;
return 0;
}
//clang++ -S -emit-llvm main.cpp && lli main.ll
llvm-ir:
; ModuleID = 'main.cpp'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@tls = external thread_local global i32, align 4
; Function Attrs: norecurse uwtable
define i32 @main() #0 {
%1 = alloca i32, align 4
store i32 0, i32* %1, align 4
%2 = call i32* @_ZTW3tls()
store i32 42, i32* %2, align 4
ret i32 0
}
define weak_odr hidden i32* @_ZTW3tls() {
br i1 icmp ne (void ()* @_ZTH3tls, void ()* null), label %1, label %2
; <label>:1 ; preds = %0
call void @_ZTH3tls()
br label %2
; <label>:2 ; preds = %1, %0
ret i32* @tls
}
declare extern_weak void @_ZTH3tls()
它会导致以下错误:
LLVM ERROR: Cannot select: 0x55ec0e9c3a60: i64 = X86ISD::WrapperRIP
TargetGlobalTLSAddress:i64<i32* @tls> 0 [TF=10]
0x55ec0e9c3858: i64 = TargetGlobalTLSAddress<i32* @tls> 0 [TF=10]
In function: _ZTW3tls
有没有办法模拟 TLS 并转换 llvm-ir 以使其工作?
使用从 thread_id 到指针的全局映射并将每次出现的 thread-local 替换为 allocator/deallocator/getter/setter 是否可行?
-femulated-tls
和有什么-ftls-model
用?
相关问题:
如何在 IR 转换过程中执行 TargetLowering?
http://lists.llvm.org/pipermail/llvm-dev/2017-February/109947.html