[llvm] [SCCP] Handle llvm.vscale intrinsic calls (PR #114033)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 08:39:54 PDT 2024


https://github.com/hazzlim updated https://github.com/llvm/llvm-project/pull/114033

>From e99076d480d04ec46758812312c9310664771bd8 Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Tue, 29 Oct 2024 09:49:24 +0000
Subject: [PATCH 1/2] [SCCP] Handle llvm.vscale intrinsic calls

Teach SCCP to compute a constant range for calls to llvm.vscale
intrinsics.
---
 llvm/lib/Transforms/Utils/SCCPSolver.cpp      |  6 ++
 llvm/test/Transforms/SCCP/vscale-intrinsic.ll | 56 +++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 llvm/test/Transforms/SCCP/vscale-intrinsic.ll

diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index c65710ea7551ac..4225e7e80fda6f 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -1923,6 +1923,12 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
       return (void)mergeInValue(IV, &CB, CopyOfVal);
     }
 
+    if (II->getIntrinsicID() == Intrinsic::vscale) {
+      unsigned BitWidth = CB.getType()->getScalarSizeInBits();
+      const ConstantRange Result = getVScaleRange(II->getFunction(), BitWidth);
+      return (void)mergeInValue(II, ValueLatticeElement::getRange(Result));
+    }
+
     if (ConstantRange::isIntrinsicSupported(II->getIntrinsicID())) {
       // Compute result range for intrinsics supported by ConstantRange.
       // Do this even if we don't know a range for all operands, as we may
diff --git a/llvm/test/Transforms/SCCP/vscale-intrinsic.ll b/llvm/test/Transforms/SCCP/vscale-intrinsic.ll
new file mode 100644
index 00000000000000..ca08c305c3059d
--- /dev/null
+++ b/llvm/test/Transforms/SCCP/vscale-intrinsic.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=sccp -S | FileCheck %s
+
+define i1 @vscale_i32_noattr() {
+; CHECK-LABEL: define i1 @vscale_i32_noattr() {
+; CHECK-NEXT:    [[SCALE:%.*]] = call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp ule i32 [[SCALE]], 16
+; CHECK-NEXT:    [[RES:%.*]] = and i1 true, [[CMP2]]
+; CHECK-NEXT:    ret i1 [[RES]]
+;
+  %scale = call i32 @llvm.vscale.i32()
+  %cmp1 = icmp uge i32 %scale, 1
+  %cmp2 = icmp ule i32 %scale, 16
+  %res = and i1 %cmp1, %cmp2
+  ret i1 %res
+}
+
+define i1 @vscale_i32_attr() vscale_range(1, 16) {
+; CHECK-LABEL: define i1 @vscale_i32_attr(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[SCALE:%.*]] = call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    ret i1 true
+;
+  %scale = call i32 @llvm.vscale.i32()
+  %cmp1 = icmp uge i32 %scale, 1
+  %cmp2 = icmp ule i32 %scale, 16
+  %res = and i1 %cmp1, %cmp2
+  ret i1 %res
+}
+
+define i1 @vscale_i64_noattr() {
+; CHECK-LABEL: define i1 @vscale_i64_noattr() {
+; CHECK-NEXT:    [[SCALE:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp ule i64 [[SCALE]], 16
+; CHECK-NEXT:    [[RES:%.*]] = and i1 true, [[CMP2]]
+; CHECK-NEXT:    ret i1 [[RES]]
+;
+  %scale = call i64 @llvm.vscale.i64()
+  %cmp1 = icmp uge i64 %scale, 1
+  %cmp2 = icmp ule i64 %scale, 16
+  %res = and i1 %cmp1, %cmp2
+  ret i1 %res
+}
+
+define i1 @vscale_i64_attr() vscale_range(1, 16) {
+; CHECK-LABEL: define i1 @vscale_i64_attr(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT:    [[SCALE:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-NEXT:    ret i1 true
+;
+  %scale = call i64 @llvm.vscale.i64()
+  %cmp1 = icmp uge i64 %scale, 1
+  %cmp2 = icmp ule i64 %scale, 16
+  %res = and i1 %cmp1, %cmp2
+  ret i1 %res
+}

>From f809e1bb97d3e303c14df2cafc4a78a9807621bb Mon Sep 17 00:00:00 2001
From: Hari Limaye <hari.limaye at arm.com>
Date: Tue, 29 Oct 2024 15:29:36 +0000
Subject: [PATCH 2/2] Regression test: Add branch elimination test case

---
 llvm/test/Transforms/SCCP/vscale-intrinsic.ll | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/llvm/test/Transforms/SCCP/vscale-intrinsic.ll b/llvm/test/Transforms/SCCP/vscale-intrinsic.ll
index ca08c305c3059d..7dc7ea9b991912 100644
--- a/llvm/test/Transforms/SCCP/vscale-intrinsic.ll
+++ b/llvm/test/Transforms/SCCP/vscale-intrinsic.ll
@@ -54,3 +54,28 @@ define i1 @vscale_i64_attr() vscale_range(1, 16) {
   %res = and i1 %cmp1, %cmp2
   ret i1 %res
 }
+
+define i32 @vscale_branch_elim(i32 %x) vscale_range(1, 16) {
+; CHECK-LABEL: define i32 @vscale_branch_elim(
+; CHECK-SAME: i32 [[X:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[SCALE:%.*]] = call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    [[BOUND:%.*]] = shl nuw nsw i32 [[SCALE]], 3
+; CHECK-NEXT:    br label %[[IF_END:.*]]
+; CHECK:       [[IF_END]]:
+; CHECK-NEXT:    ret i32 [[X]]
+;
+entry:
+  %scale = call i32 @llvm.vscale.i32()
+  %bound = shl nsw nuw i32 %scale, 3
+  %cmp = icmp uge i32 1, %bound
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  %double = mul i32 %x, 2
+  br label %if.end
+
+if.end:
+  %res = phi i32 [ %x, %entry ], [ %double, %if.then]
+  ret i32 %res
+}



More information about the llvm-commits mailing list