Skip to content

[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

Merged
merged 1 commit into from
Aug 7, 2024

Conversation

cceerczw
Copy link
Contributor

@cceerczw cceerczw commented Aug 1, 2024

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.

Copy link

github-actions bot commented Aug 1, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added backend:AArch64 llvm:SelectionDAG SelectionDAGISel as well labels Aug 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2024

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-llvm-selectiondag

Author: None (cceerczw)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/101490.diff

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp (+4-2)
  • (added) llvm/test/CodeGen/AArch64/emutls_alias.ll (+20)
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" }

Comment on lines 10228 to 10229
const GlobalValue *GV = GA->getGlobal();
GV = GV->getAliaseeObject() ? GV->getAliaseeObject() : GV;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use stripPointerCastsAndAliases

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmallString

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'.

Copy link
Contributor

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

Copy link
Contributor Author

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());
Copy link
Contributor

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

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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor

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

Copy link
Contributor Author

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" }
Copy link
Contributor

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

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 10229 to 10230
GV = GV->stripPointerCastsAndAliases()
? cast<GlobalValue>(GV->stripPointerCastsAndAliases()) : GV;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, fixed

@cceerczw cceerczw force-pushed the fix-elumated-TLS branch 2 times, most recently from 7d3771e to 8c55ead Compare August 5, 2024 08:09
@cceerczw
Copy link
Contributor Author

cceerczw commented Aug 7, 2024

hi, @arsenm, thanks for the review, has any other reviews?

@@ -0,0 +1,17 @@
; RUN: llc < %s -emulated-tls -mtriple=aarch64-linux-ohos -O0 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need -O0

Copy link
Contributor Author

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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SmallString<32> NameString = StringRef("__emutls_v." + GV->getName().str());
SmallString<32> NameString("__emutls_v.");
NameString += GV->getName();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Copy link

github-actions bot commented Aug 7, 2024

✅ 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.
@arsenm arsenm merged commit 6f8e8fa into llvm:main Aug 7, 2024
5 of 7 checks passed
Copy link

github-actions bot commented Aug 7, 2024

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants