-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[GlobalOpt] Preserve Address Space when recreating GV #171211
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
[GlobalOpt] Preserve Address Space when recreating GV #171211
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 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 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. |
|
@llvm/pr-subscribers-llvm-transforms Author: Evgenii Podkorytov (e-podkorytov) ChangesFix for GlobalOpt pass: preserve Address Space when recreating GV Before global-opt After global-opt With proposed fix Full diff: https://github.com/llvm/llvm-project/pull/171211.diff 2 Files Affected:
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:
|
|
@espindola @nikic @fhahn please review this fix |
|
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. |
|
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 |
nikic
left a 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.
Okay, then this LGTM.
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))] |
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.
I would introduce a separate test for this, instead of modifying this existing one
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 don't believe adding a new parallel test to check the same transformation will bring us any benefit. |
|
@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! |
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
After global-opt
With proposed fix
@llvm.compiler.usedwill be re-created with preserving originaladdrspace(1)as: