Skip to content

Local: Handle noalias_addrspace in combineMetadata #103938

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 6 commits into from
Nov 26, 2024

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Aug 14, 2024

This should act like range.

Previously ConstantRangeList assumed a 64-bit range. Now query from the actual entries. This also means that the empty range has no bitwidth, so move asserts to avoid checking the bitwidth of empty ranges.

Copy link
Contributor Author

arsenm commented Aug 14, 2024

@llvmbot
Copy link
Member

llvmbot commented Aug 14, 2024

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Matt Arsenault (arsenm)

Changes

This should act like range.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+6-1)
  • (modified) llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll (+4-2)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index efb02fdec56d7..bbc8933d387e1 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -3319,6 +3319,10 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
         if (DoesKMove)
           K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J));
         break;
+      case LLVMContext::MD_noalias_addrspace:
+        if (DoesKMove)
+          K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
+        break;
     }
   }
   // Set !invariant.group from J if J has it. If both instructions have it
@@ -3360,7 +3364,8 @@ void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
                          LLVMContext::MD_prof,
                          LLVMContext::MD_nontemporal,
                          LLVMContext::MD_noundef,
-                         LLVMContext::MD_mmra};
+                         LLVMContext::MD_mmra,
+                         LLVMContext::MD_noalias_addrspace};
   combineMetadata(K, J, KnownIDs, KDominatesJ);
 }
 
diff --git a/llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll b/llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll
index 18aa5c9e044a9..f8985e78c0ca5 100644
--- a/llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll
+++ b/llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll
@@ -319,7 +319,7 @@ out:
 define void @hoist_noalias_addrspace_both(i1 %c, ptr %p, i64 %val) {
 ; CHECK-LABEL: @hoist_noalias_addrspace_both(
 ; CHECK-NEXT:  if:
-; CHECK-NEXT:    [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8
+; CHECK-NEXT:    [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META7:![0-9]+]]
 ; CHECK-NEXT:    ret void
 ;
 if:
@@ -361,7 +361,7 @@ out:
 define void @hoist_noalias_addrspace_switch(i64 %i, ptr %p, i64 %val) {
 ; CHECK-LABEL: @hoist_noalias_addrspace_switch(
 ; CHECK-NEXT:  out:
-; CHECK-NEXT:    [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8
+; CHECK-NEXT:    [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META8:![0-9]+]]
 ; CHECK-NEXT:    ret void
 ;
   switch i64 %i, label %bb0 [
@@ -398,4 +398,6 @@ out:
 ; CHECK: [[RNG4]] = !{i32 0, i32 10}
 ; CHECK: [[META5]] = !{i64 4}
 ; CHECK: [[META6]] = !{float 2.500000e+00}
+; CHECK: [[META7]] = !{i32 5, i32 6}
+; CHECK: [[META8]] = !{i32 4, i32 8}
 ;.

@arsenm arsenm force-pushed the users/arsenm/noalias-addrspace-metadata branch from f84e99b to 29b8b60 Compare September 24, 2024 04:35
@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from 0f1cd91 to 3ff8ae8 Compare September 24, 2024 04:36
@arsenm arsenm force-pushed the users/arsenm/noalias-addrspace-metadata branch 2 times, most recently from fb9d412 to d7b148d Compare September 30, 2024 07:09
@arsenm arsenm force-pushed the users/arsenm/noalias-addrspace-metadata branch from d7b148d to 4a6df18 Compare October 7, 2024 09:15
Base automatically changed from users/arsenm/noalias-addrspace-metadata to main October 7, 2024 19:21
@arsenm arsenm requested review from nikic and Pierre-vh October 7, 2024 19:25
@nikic
Copy link
Contributor

nikic commented Oct 7, 2024

It looks like this still includes the LangRef/IR changes.

@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from 3ff8ae8 to f7b9b84 Compare October 7, 2024 20:13
Copy link
Contributor

@frasercrmck frasercrmck left a comment

Choose a reason for hiding this comment

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

Should we also have a test for how it combines non-contiguous ranges?

@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from f7b9b84 to 4e5e920 Compare October 8, 2024 18:21
@llvmbot llvmbot added the llvm:ir label Oct 8, 2024
// Handle common cases.
if (empty() || Ranges.back().getUpper().slt(NewRange.getLower())) {
Ranges.push_back(NewRange);
return;
}

assert(getBitWidth() == NewRange.getBitWidth());
Copy link
Contributor

Choose a reason for hiding this comment

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

I notice we're moving a bunch of asserts around. I think that needs explaining in the commit message. However, it feels like something's wrong.

Does insert perhaps need to be updated so that it inserts 32-bit values depending on the range type? Would that fix us having mixed 64- and 32-bit range values? Is that even possible right now - what if we want to insert into an empty range? We can't know what bitwidth to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An empty range has no bitwidth. I don't think it's worth adding an explicit field for bit width to give a bit width to the empty range

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that an empty range has no bitwidth. I missed that ConstantRangeList only deals with the insertion of pre-existing ConstantRanges. I think it's okay on balance to require users to only insert/union/intersect ConstantRanges of matching bitwidths.

I think a good way of expressing this would be to keep the asserts where they are but have getBitWidth return a value that represents "none" or "empty" which always compares == true with bitwidths of other non-empty ranges. That's probably not worth the effort, though. What you have now is probably okay.

@@ -60,7 +60,7 @@ class [[nodiscard]] ConstantRangeList {
bool empty() const { return Ranges.empty(); }

/// Get the bit width of this ConstantRangeList.
uint32_t getBitWidth() const { return 64; }
uint32_t getBitWidth() const { return Ranges.front().getBitWidth(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

If the range list is empty, should this continue to return 64? Otherwise at best it'll assert and at worst it'll return garbage. An optional could also represent this scenario.

If there's a new precondition that you can only call getBitWidth on a non-empty range, that should be documented.

// Handle common cases.
if (empty() || Ranges.back().getUpper().slt(NewRange.getLower())) {
Ranges.push_back(NewRange);
return;
}

assert(getBitWidth() == NewRange.getBitWidth());
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that an empty range has no bitwidth. I missed that ConstantRangeList only deals with the insertion of pre-existing ConstantRanges. I think it's okay on balance to require users to only insert/union/intersect ConstantRanges of matching bitwidths.

I think a good way of expressing this would be to keep the asserts where they are but have getBitWidth return a value that represents "none" or "empty" which always compares == true with bitwidths of other non-empty ranges. That's probably not worth the effort, though. What you have now is probably okay.

@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from 8c22443 to 7424990 Compare October 26, 2024 01:27
@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from 7424990 to 05170f0 Compare November 5, 2024 20:47
@arsenm arsenm force-pushed the users/arsenm/combine-metadata-preserve-noalias-addrspace branch from 05170f0 to 3918491 Compare November 5, 2024 20:57
Copy link
Contributor Author

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

ping

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.

LGTM

@arsenm arsenm merged commit 4028bb1 into main Nov 26, 2024
6 of 8 checks passed
@arsenm arsenm deleted the users/arsenm/combine-metadata-preserve-noalias-addrspace branch November 26, 2024 14:13
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