[PATCH] D120000: [1/2] TLS loads opimization (hoist)
Xiang Zhang via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 16 19:19:16 PST 2022
xiangzhangllvm created this revision.
xiangzhangllvm added reviewers: LuoYuanke, pengfei, andrew.w.kaylor, clin1.
Herald added subscribers: dexonsmith, hiraditya, mgorny.
xiangzhangllvm requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
When we access a TLS variable in PIC mode, it usually get the TLS address by calling a lib function, some like
callq __tls_get_addr at PLT
This call was not show in IR or MIR, usually tag by a target-special flag (like pic) and generated in Assembly Printing.
So it is usually call it every time when TLS variable is accessed. Many of them are duplicated, especially in loops.
This patch is try to optimize it. It identifies/eliminate Redundant TLS Loads by hoist the TLS access when the related option is set.
The Clang option will add in patch [2/2]
For example:
static __thread int x;
int g();
int f(int c) {
int *px = &x;
while (c--)
*px += g();
return *px;
}
will generated Redundant TLS Loads by compiling it with
Clang++ -fPIC -ftls-model=global-dynamic -O2 -S
.LBB0_2: # %while.body
# =>This Inner Loop Header: Depth=1
callq _Z1gv at PLT
movl %eax, %ebp
leaq _ZL1x at TLSLD(%rip), %rdi
callq __tls_get_addr at PLT
addl _ZL1x at DTPOFF(%rax), %ebp
movl %ebp, _ZL1x at DTPOFF(%rax)
addl $-1, %ebx
jne .LBB0_2
jmp .LBB0_3
.LBB0_4: # %entry.while.end_crit_edge
leaq _ZL1x at TLSLD(%rip), %rdi
callq __tls_get_addr at PLT
movl _ZL1x at DTPOFF(%rax), %ebp
The Redundant TLS Loads will hurt the performance, especially in loops.
So we try to eliminate/move them if required by customers, let it be:
# %bb.0: # %entry
...
movl %edi, %ebx
leaq _ZL1x at TLSLD(%rip), %rdi
callq __tls_get_addr at PLT
leaq _ZL1x at DTPOFF(%rax), %r14
testl %ebx, %ebx
je .LBB0_1
.LBB0_2: # %while.body
# =>This Inner Loop Header: Depth=1
callq _Z1gv at PLT
addl (%r14), %eax
movl %eax, (%r14)
addl $-1, %ebx
jne .LBB0_2
jmp .LBB0_3
https://reviews.llvm.org/D120000
Files:
llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
llvm/include/llvm/CodeGen/MachinePassRegistry.def
llvm/include/llvm/IR/Module.h
llvm/include/llvm/InitializePasses.h
llvm/include/llvm/LinkAllPasses.h
llvm/include/llvm/Transforms/Scalar.h
llvm/include/llvm/Transforms/Scalar/TLSVariableControl.h
llvm/lib/CodeGen/TargetPassConfig.cpp
llvm/lib/IR/Module.cpp
llvm/lib/Passes/PassBuilder.cpp
llvm/lib/Passes/PassRegistry.def
llvm/lib/Transforms/Scalar/CMakeLists.txt
llvm/lib/Transforms/Scalar/Scalar.cpp
llvm/lib/Transforms/Scalar/TLSVariableControl.cpp
llvm/test/CodeGen/X86/tls-loads-control.ll
llvm/test/CodeGen/X86/tls-loads-control2.ll
llvm/test/CodeGen/X86/tls-loads-control3.ll
llvm/tools/llc/llc.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120000.409470.patch
Type: text/x-patch
Size: 49178 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220217/5f47be44/attachment-0001.bin>
More information about the llvm-commits
mailing list