-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[TargetLowering] Fix the problem of emulated-TLS implementation witho… #101490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-backend-aarch64 @llvm/pr-subscribers-llvm-selectiondag Author: None (cceerczw) ChangesFor a __thread variable x, when emulated TLS is enabled and there is an access to x, the compiler first looks up the symbol __emutls_v.x within the module. However, the issue arises with an alias y of x, the compiler still tries to look up __emutls_v.y instead of __emutls_v.x. As a result, the lookup returns a nullptr, causing the compiler to crash. The purpose of this MR (Merge Request) is to ensure that in emulated TLS, before checking __emutls_v.y, the compiler first identifies which global value y is an alias of. Full diff: https://github.com/llvm/llvm-project/pull/101490.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 6fd23b5ab9f5f..e61f05445030d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -10225,8 +10225,10 @@ SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
ArgListTy Args;
ArgListEntry Entry;
- std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str();
- Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent());
+ const GlobalValue *GV = GA->getGlobal();
+ GV = GV->getAliaseeObject() ? GV->getAliaseeObject() : GV;
+ std::string NameString = ("__emutls_v." + GV->getName()).str();
+ Module *VariableModule = const_cast<Module*>(GV->getParent());
StringRef EmuTlsVarName(NameString);
GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName);
assert(EmuTlsVar && "Cannot find EmuTlsVar ");
diff --git a/llvm/test/CodeGen/AArch64/emutls_alias.ll b/llvm/test/CodeGen/AArch64/emutls_alias.ll
new file mode 100644
index 0000000000000..78a08fe454dd9
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/emutls_alias.ll
@@ -0,0 +1,20 @@
+; RUN: llc < %s -emulated-tls -mtriple=aarch64-linux-ohos -O0 \
+; RUN: | FileCheck -check-prefix=EMUTLS_CHECK %s
+
+%struct.__res_state = type { [5 x i8] }
+
+@foo = dso_local thread_local global %struct.__res_state { [5 x i8] c"\01\02\03\04\05" }, align 1
+
+@bar = hidden thread_local(initialexec) alias %struct.__res_state, ptr @foo
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @main() #0 {
+ %1 = alloca i32, align 4
+ store i32 0, ptr %1, align 4
+ store i8 0, ptr @bar, align 1
+ ; EMUTLS_CHECK: adrp x0, __emutls_v.foo
+ ; EMUTLS_CHECK-NEXT: add x0, x0, :lo12:__emutls_v.foo
+ ret i32 0
+}
+
+attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+neon,+v8a" }
|
const GlobalValue *GV = GA->getGlobal(); | ||
GV = GV->getAliaseeObject() ? GV->getAliaseeObject() : GV; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use stripPointerCastsAndAliases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent()); | ||
const GlobalValue *GV = GA->getGlobal(); | ||
GV = GV->getAliaseeObject() ? GV->getAliaseeObject() : GV; | ||
std::string NameString = ("__emutls_v." + GV->getName()).str(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SmallString
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code used 'NameString'. And the lenth of rvalue at line 10230 is indeterminate. So I don't think it's a good idea to use 'SmallString'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works for any size of string, it's just optimized for the common case where the name is small
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const GlobalValue *GV = GA->getGlobal(); | ||
GV = GV->getAliaseeObject() ? GV->getAliaseeObject() : GV; | ||
std::string NameString = ("__emutls_v." + GV->getName()).str(); | ||
Module *VariableModule = const_cast<Module*>(GV->getParent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need const_cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the 'const_cast' was removed, there will be an error caused:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you should also make VariableModule const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ret i32 0 | ||
} | ||
|
||
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+neon,+v8a" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need all these attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
||
@bar = hidden thread_local(initialexec) alias %struct.__res_state, ptr @foo | ||
|
||
; Function Attrs: noinline nounwind optnone uwtable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ea72068
to
cd9d03a
Compare
GV = GV->stripPointerCastsAndAliases() | ||
? cast<GlobalValue>(GV->stripPointerCastsAndAliases()) : GV; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GV = GA->getGlobal()->stripPointerCastsAndAliases(), nothing is conditional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, fixed
7d3771e
to
8c55ead
Compare
hi, @arsenm, thanks for the review, has any other reviews? |
@@ -0,0 +1,17 @@ | |||
; RUN: llc < %s -emulated-tls -mtriple=aarch64-linux-ohos -O0 \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need -O0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str(); | ||
Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent()); | ||
const GlobalValue *GV = cast<GlobalValue>(GA->getGlobal()->stripPointerCastsAndAliases()); | ||
SmallString<32> NameString = StringRef("__emutls_v." + GV->getName().str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SmallString<32> NameString = StringRef("__emutls_v." + GV->getName().str()); | |
SmallString<32> NameString("__emutls_v."); | |
NameString += GV->getName(); | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
8c55ead
to
e60e723
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
…ut checking alias of current global value at first. For a __thread variable x, when emulated TLS is enabled and there is an access to x, the compiler first looks up the symbol __emutls_v.x within the module. However, the issue arises with an alias y of x, the compiler still tries to look up __emutls_v.y instead of __emutls_v.x. As a result, the lookup returns a nullptr, causing the compiler to crash. The purpose of this MR (Merge Request) is to ensure that in emulated TLS, before checking __emutls_v.y, the compiler first identifies which global value y is an alias of.
e60e723
to
75cb304
Compare
@cceerczw Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
For a __thread variable x, when emulated TLS is enabled and there is an access to x, the compiler first looks up the symbol __emutls_v.x within the module. However, the issue arises with an alias y of x, the compiler still tries to look up __emutls_v.y instead of __emutls_v.x. As a result, the lookup returns a nullptr, causing the compiler to crash. The purpose of this MR (Merge Request) is to ensure that in emulated TLS, before checking __emutls_v.y, the compiler first identifies which global value y is an alias of.