Skip to content

Utils: Preserve address space for global_ctors #112532

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
Oct 18, 2024

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Oct 16, 2024

No description provided.

Copy link
Contributor Author

arsenm commented Oct 16, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @arsenm and the rest of your teammates on Graphite Graphite

@arsenm arsenm marked this pull request as ready for review October 16, 2024 12:25
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/CtorUtils.cpp (+3-3)
  • (added) llvm/test/Transforms/GlobalOpt/ctor-list-preserve-addrspace.ll (+19)
diff --git a/llvm/lib/Transforms/Utils/CtorUtils.cpp b/llvm/lib/Transforms/Utils/CtorUtils.cpp
index 507729bc5ebc06..968446c4eee117 100644
--- a/llvm/lib/Transforms/Utils/CtorUtils.cpp
+++ b/llvm/lib/Transforms/Utils/CtorUtils.cpp
@@ -45,9 +45,9 @@ static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemov
   }
 
   // Create the new global and insert it next to the existing list.
-  GlobalVariable *NGV =
-      new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
-                         CA, "", GCL->getThreadLocalMode());
+  GlobalVariable *NGV = new GlobalVariable(
+      CA->getType(), GCL->isConstant(), GCL->getLinkage(), CA, "",
+      GCL->getThreadLocalMode(), GCL->getAddressSpace());
   GCL->getParent()->insertGlobalVariable(GCL->getIterator(), NGV);
   NGV->takeName(GCL);
 
diff --git a/llvm/test/Transforms/GlobalOpt/ctor-list-preserve-addrspace.ll b/llvm/test/Transforms/GlobalOpt/ctor-list-preserve-addrspace.ll
new file mode 100644
index 00000000000000..3f2f041b90e74d
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/ctor-list-preserve-addrspace.ll
@@ -0,0 +1,19 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
+; RUN: opt -S -passes=globalopt < %s | FileCheck %s
+
+; Make sure the address space of global_ctors is preserved
+
+%ini = type { i32, ptr, ptr }
+
+@llvm.global_ctors = appending addrspace(1) global [1 x %ini] [%ini { i32 65534, ptr @ctor1, ptr null }]
+
+;.
+; CHECK: @llvm.global_ctors = appending addrspace(1) global [0 x %ini] zeroinitializer
+;.
+define void @ctor1() {
+; CHECK-LABEL: define void @ctor1() local_unnamed_addr {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+

Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

The change looks good.

@llvm.global_ctors = appending addrspace(1) global [1 x %ini] [%ini { i32 65534, ptr @ctor1, ptr null }]

;.
; CHECK: @llvm.global_ctors = appending addrspace(1) global [0 x %ini] zeroinitializer
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does it become [0 x %ini] afterwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It decided to remove the one entry (I guess this is a pass optimization bug, it should probably just delete the whole thing in this case)

@arsenm arsenm merged commit f225b07 into main Oct 18, 2024
12 checks passed
@arsenm arsenm deleted the users/arsenm/ctor-utils-correct-address-space branch October 18, 2024 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants