Skip to content

[ValueTracking] Add experimental_get_vector_length to isKnownNonZero. #79950

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
Jan 30, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 30, 2024

If the input is non-zero, this intrinsic should also return a non-zero value.

If the input is non-zero, this intrinsic should also return a
non-zero value.
@topperc topperc requested a review from preames January 30, 2024 06:59
@topperc topperc requested a review from nikic as a code owner January 30, 2024 06:59
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jan 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 30, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Craig Topper (topperc)

Changes

If the input is non-zero, this intrinsic should also return a non-zero value.


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+2)
  • (modified) llvm/test/Analysis/ValueTracking/known-non-zero.ll (+24)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index f1f73f565c51f..362d02a39ef26 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2739,6 +2739,8 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
         break;
       case Intrinsic::vscale:
         return true;
+      case Intrinsic::experimental_get_vector_length:
+        return isKnownNonZero(I->getOperand(0), Depth, Q);
       default:
         break;
       }
diff --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index 2ce9df886d863..d804fe9664903 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -1269,3 +1269,27 @@ while.end.i:
   %bool = icmp eq i32 %orval, 0
   ret i1 %bool
 }
+
+define i1 @check_get_vector_length(i32 %x, i32 %y) {
+; CHECK-LABEL: @check_get_vector_length(
+; CHECK-NEXT:    [[NE:%.*]] = icmp ne i32 [[X:%.*]], 0
+; CHECK-NEXT:    br i1 [[NE]], label [[TRUE:%.*]], label [[FALSE:%.*]]
+; CHECK:       true:
+; CHECK-NEXT:    [[Z:%.*]] = call i32 @llvm.experimental.get.vector.length.i32(i32 [[X]], i32 1, i1 true)
+; CHECK-NEXT:    [[CMP0:%.*]] = icmp ugt i32 [[Z]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP0]]
+; CHECK:       false:
+; CHECK-NEXT:    ret i1 [[NE]]
+;
+  %ne = icmp ne i32 %x, 0
+  br i1 %ne, label %true, label %false
+true:
+  %z = call i32 @llvm.experimental.get.vector.length.i32(i32 %x, i32 1, i1 true)
+  %cmp0 = icmp ugt i32 %z, %y
+  %cmp1 = icmp eq i32 %y, 0
+  %r = or i1 %cmp0, %cmp1
+  ret i1 %r
+false:
+  ret i1 %ne
+}
+declare i32 @llvm.experimental.get.vector.length.i32(i32, i32, i1)

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

@topperc topperc merged commit d8e1b45 into llvm:main Jan 30, 2024
@topperc topperc deleted the pr/get_vector_length-nonzero branch January 30, 2024 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants