如何在llvm-ir中模拟thread_local?

Gae*_*ano 25 c++ multithreading llvm llvm-ir lli

以下代码目前在lli中不起作用:

//main.cpp 
extern thread_local int tls;
int main() {
    tls = 42;
    return 0;
}

//clang++ -S -emit-llvm main.cpp && lli main.ll
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

它会导致以下错误:

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
Run Code Online (Sandbox Code Playgroud)

有没有办法模拟TLS并转换llvm-ir以使其工作?

使用从thread_id到指针的全局映射并用allocator/deallocator/getter/setter替换每次出现的本地线程是否可行?

-femulated-tls-ftls-model任何使用?

相关问题:

如何在IR-trasformation传递中执行TargetLowering?

http://lists.llvm.org/pipermail/llvm-dev/2017-February/109947.html

小智 1

既然你还没有说你看到的错误是什么,我假设它是某种形式的 LLVM ERROR: Program used external function '_ZTH3tls' which could not be resolved!

这是一个链接错误,实际上是指声明为具有外部链接的事实tls,但没有其他定义可以链接到(至少您已经发布了)。

代替

extern thread_local int tls;
Run Code Online (Sandbox Code Playgroud)

thread_local int tls;
Run Code Online (Sandbox Code Playgroud)

然后编译器将生成以下 IR

@tls = thread_local global i32 0, align 4
Run Code Online (Sandbox Code Playgroud)

如果您确实需要进行外部链接并使用lli,则需要llvm-link首先将 llvm 文件链接到 ,因为lli它无法自行链接。

例如

扩展.cpp

thread_local int tls = 0;
Run Code Online (Sandbox Code Playgroud)

主程序

extern thread_local int tls;
int main() {
  tls = 42;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译它会生成ext.llmain.ll。运行llvm-link -S ext.ll main.ll > output.ll以获取链接文件,然后lli output.ll也应该可以工作。

让我知道这是否解决了问题。