Skip to content

Conversation

@e-podkorytov
Copy link
Contributor

Fix for GlobalOpt pass: preserve Address Space when recreating GV
This fix prevents dropping addrspace(1) in the following code snippet (see modified LIT-test) for @llvm.compiler.used:

Before global-opt

@_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
@_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8

@llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]

After global-opt

@_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
@_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8

@llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]

With proposed fix @llvm.compiler.used will be re-created with preserving original addrspace(1) as:

@llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]

@github-actions
Copy link

github-actions bot commented Dec 8, 2025

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

llvmbot commented Dec 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Evgenii Podkorytov (e-podkorytov)

Changes

Fix for GlobalOpt pass: preserve Address Space when recreating GV
This fix prevents dropping addrspace(1) in the following code snippet (see modified LIT-test) for @<!-- -->llvm.compiler.used:

Before global-opt

@<!-- -->_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
@<!-- -->_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8

@<!-- -->llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZL1C to ptr addrspace(4))]

After global-opt

@<!-- -->_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
@<!-- -->_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8

@<!-- -->llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZL1C to ptr addrspace(4))]

With proposed fix @<!-- -->llvm.compiler.used will be re-created with preserving original addrspace(1) as:

@<!-- -->llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @<!-- -->_ZL1C to ptr addrspace(4))]

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/GlobalOpt.cpp (+4-3)
  • (modified) llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll (+2-2)
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 939071725253f..23d5230d72b1e 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2144,9 +2144,10 @@ static void setUsedInitializer(GlobalVariable &V,
 
   Module *M = V.getParent();
   V.removeFromParent();
-  GlobalVariable *NV =
-      new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage,
-                         ConstantArray::get(ATy, UsedArray), "");
+  GlobalVariable *NV = new GlobalVariable(
+      *M, ATy, false, GlobalValue::AppendingLinkage,
+      ConstantArray::get(ATy, UsedArray), "", nullptr,
+      GlobalVariable::NotThreadLocal, V.getType()->getAddressSpace());
   NV->takeName(&V);
   NV->setSection("llvm.metadata");
   delete &V;
diff --git a/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll b/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
index 9df8a4bfbfae1..724402f5c663e 100644
--- a/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
+++ b/llvm/test/Transforms/GlobalOpt/global-opt-addrspace.ll
@@ -8,9 +8,9 @@
 @_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
 @_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
 
-@llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]
+@llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]
 
-; CHECK: @llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4))]
+; CHECK: @llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4))]
 
 define weak_odr dso_local void @foo() {
 entry:

@e-podkorytov
Copy link
Contributor Author

@espindola @nikic @fhahn please review this fix

@nikic nikic requested a review from arsenm December 9, 2025 07:33
@nikic
Copy link
Contributor

nikic commented Dec 9, 2025

See also #162660 and related discussions. I believe our position is that the address space of the elements is required to be 0 (even though we're slightly inconsistent about this right now). I'm not sure about the address space of the magic global itself, but it should probably also be 0? @arsenm should know.

@arsenm
Copy link
Contributor

arsenm commented Dec 9, 2025

I don't think requiring 0 ever made it to the LangRef or IR verifier, although I think that's the most consistent thing to do as soon as there are multiple address spaces involved. However, it is reasonable to just preserve the original address space in the current state

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Okay, then this LGTM.

@e-podkorytov
Copy link
Contributor Author

See also #162660 and related discussions. I believe our position is that the address space of the elements is required to be 0 (even though we're slightly inconsistent about this right now). I'm not sure about the address space of the magic global itself, but it should probably also be 0? @arsenm should know.

Yes, I saw that PR and studied all the details. It refers to the pointers stored in the llvm.compiler.used/llvm.used, but not to the address space of the llvm.compiler.used/llvm.used itself.

@arsenm
Copy link
Contributor

arsenm commented Dec 9, 2025

Yes, I saw that PR and studied all the details. It refers to the pointers stored in the llvm.compiler.used/llvm.used, but not to the address space of the llvm.compiler.used/llvm.used itself.

I think there are issues when trying to link llvm.used with different address spaces in different modules too. You have to pick one in the end, I'm not sure what happens

@_ZM2C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8
@_ZL1C = internal addrspace(1) global %struct.FakeDeviceGlobal zeroinitializer, align 8

@llvm.compiler.used = appending global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZM2C to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL1C to ptr addrspace(4))]
Copy link
Contributor

Choose a reason for hiding this comment

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

I would introduce a separate test for this, instead of modifying this existing one

@e-podkorytov
Copy link
Contributor Author

Yes, I saw that PR and studied all the details. It refers to the pointers stored in the llvm.compiler.used/llvm.used, but not to the address space of the llvm.compiler.used/llvm.used itself.

I think there are issues when trying to link llvm.used with different address spaces in different modules too. You have to pick one in the end, I'm not sure what happens

Right, this is the original issue I'm trying to resolve, i.e. linking different modules: at the beginning 2 modules have llvm.used in addrspace(1), but after compiling 1st module, global-opt pass dropped address space from llvm.used resulting in implicit addrspace(0), and it failed to link with another module later.

I would introduce a separate test for this, instead of modifying this existing one

I don't believe adding a new parallel test to check the same transformation will bring us any benefit.

@nikic nikic merged commit 617ba83 into llvm:main Dec 10, 2025
12 checks passed
@github-actions
Copy link

@e-podkorytov 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants