Skip to content
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

[Metadata] Try to merge the first and last ranges. #101860

Merged
merged 1 commit into from
Aug 4, 2024

Conversation

DianQK
Copy link
Member

@DianQK DianQK commented Aug 4, 2024

Fixes #101859.

If we have at least 2 ranges, we have to try to merge the last and first ones to handle the wrap range.

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2024

@llvm/pr-subscribers-llvm-ir

Author: DianQK (DianQK)

Changes

Fixes #101859.

If we have at least 2 ranges, we have to try to merge the last and first ones to handle the wrap range.


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

2 Files Affected:

  • (modified) llvm/lib/IR/Metadata.cpp (+3-2)
  • (added) llvm/test/Transforms/GVN/merge-range.ll (+20)
diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index ae5f5de142328..fd2f4d184162f 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -1318,10 +1318,11 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
     ++BI;
   }
 
-  // If we have more than 2 ranges (4 endpoints) we have to try to merge
+  // We haven't handled wrap in the previous merge,
+  // if we have at least 2 ranges (4 endpoints) we have to try to merge
   // the last and first ones.
   unsigned Size = EndPoints.size();
-  if (Size > 4) {
+  if (Size > 2) {
     ConstantInt *FB = EndPoints[0];
     ConstantInt *FE = EndPoints[1];
     if (tryMergeRange(EndPoints, FB, FE)) {
diff --git a/llvm/test/Transforms/GVN/merge-range.ll b/llvm/test/Transforms/GVN/merge-range.ll
new file mode 100644
index 0000000000000..ad1fa4cae5662
--- /dev/null
+++ b/llvm/test/Transforms/GVN/merge-range.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=gvn -S < %s | FileCheck %s
+
+define i8 @foo(ptr %arg, i8 %arg1) {
+; CHECK-LABEL: define i8 @foo(
+; CHECK-SAME: ptr [[ARG:%.*]], i8 [[ARG1:%.*]]) {
+; CHECK-NEXT:  [[BB:.*:]]
+; CHECK-NEXT:    [[I:%.*]] = load i8, ptr [[ARG]], align 1, !range [[RNG0:![0-9]+]]
+; CHECK-NEXT:    [[I3:%.*]] = add i8 [[I]], [[I]]
+; CHECK-NEXT:    ret i8 [[I3]]
+;
+bb:
+  %i = load i8, ptr %arg, align 1, !range !{i8 127, i8 -20}
+  %i2 = load i8, ptr %arg, align 1, !range !{i8 -27, i8 -24, i8 -20, i8 -17}
+  %i3 = add i8 %i, %i2
+  ret i8 %i3
+}
+;.
+; CHECK: [[RNG0]] = !{i8 127, i8 -17}
+;.

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2024

@llvm/pr-subscribers-llvm-transforms

Author: DianQK (DianQK)

Changes

Fixes #101859.

If we have at least 2 ranges, we have to try to merge the last and first ones to handle the wrap range.


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

2 Files Affected:

  • (modified) llvm/lib/IR/Metadata.cpp (+3-2)
  • (added) llvm/test/Transforms/GVN/merge-range.ll (+20)
diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index ae5f5de142328..fd2f4d184162f 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -1318,10 +1318,11 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
     ++BI;
   }
 
-  // If we have more than 2 ranges (4 endpoints) we have to try to merge
+  // We haven't handled wrap in the previous merge,
+  // if we have at least 2 ranges (4 endpoints) we have to try to merge
   // the last and first ones.
   unsigned Size = EndPoints.size();
-  if (Size > 4) {
+  if (Size > 2) {
     ConstantInt *FB = EndPoints[0];
     ConstantInt *FE = EndPoints[1];
     if (tryMergeRange(EndPoints, FB, FE)) {
diff --git a/llvm/test/Transforms/GVN/merge-range.ll b/llvm/test/Transforms/GVN/merge-range.ll
new file mode 100644
index 0000000000000..ad1fa4cae5662
--- /dev/null
+++ b/llvm/test/Transforms/GVN/merge-range.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=gvn -S < %s | FileCheck %s
+
+define i8 @foo(ptr %arg, i8 %arg1) {
+; CHECK-LABEL: define i8 @foo(
+; CHECK-SAME: ptr [[ARG:%.*]], i8 [[ARG1:%.*]]) {
+; CHECK-NEXT:  [[BB:.*:]]
+; CHECK-NEXT:    [[I:%.*]] = load i8, ptr [[ARG]], align 1, !range [[RNG0:![0-9]+]]
+; CHECK-NEXT:    [[I3:%.*]] = add i8 [[I]], [[I]]
+; CHECK-NEXT:    ret i8 [[I3]]
+;
+bb:
+  %i = load i8, ptr %arg, align 1, !range !{i8 127, i8 -20}
+  %i2 = load i8, ptr %arg, align 1, !range !{i8 -27, i8 -24, i8 -20, i8 -17}
+  %i3 = add i8 %i, %i2
+  ret i8 %i3
+}
+;.
+; CHECK: [[RNG0]] = !{i8 127, i8 -17}
+;.

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

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

LGTM. Nice catch!

@DianQK DianQK merged commit 4377656 into llvm:main Aug 4, 2024
10 checks passed
@DianQK DianQK deleted the merge-range branch August 4, 2024 08:45
@DianQK DianQK added this to the LLVM 19.X Release milestone Aug 4, 2024
@DianQK
Copy link
Member Author

DianQK commented Aug 4, 2024

/cherry-pick 4377656

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 4, 2024
Fixes llvm#101859.

If we have at least 2 ranges, we have to try to merge the last and first
ones to handle the wrap range.

(cherry picked from commit 4377656)
@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2024

/pull-request #101875

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 5, 2024
Fixes llvm#101859.

If we have at least 2 ranges, we have to try to merge the last and first
ones to handle the wrap range.

(cherry picked from commit 4377656)
banach-space pushed a commit to banach-space/llvm-project that referenced this pull request Aug 7, 2024
Fixes llvm#101859.

If we have at least 2 ranges, we have to try to merge the last and first
ones to handle the wrap range.
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this pull request Aug 15, 2024
Fixes llvm#101859.

If we have at least 2 ranges, we have to try to merge the last and first
ones to handle the wrap range.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Intervals are overlapping
4 participants