[llvm] 2c1b71a - [VPlan] Compute URem via APInt in materializeVectorTripCount (#203604)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 12 12:04:29 PDT 2026
Author: Florian Hahn
Date: 2026-06-12T19:04:23Z
New Revision: 2c1b71af78df9d7c22e225069971d3276d91d546
URL: https://github.com/llvm/llvm-project/commit/2c1b71af78df9d7c22e225069971d3276d91d546
DIFF: https://github.com/llvm/llvm-project/commit/2c1b71af78df9d7c22e225069971d3276d91d546.diff
LOG: [VPlan] Compute URem via APInt in materializeVectorTripCount (#203604)
materializeVectorTripCount has a shortcut for scalable steps: if the
constant trip count is divisible by the maximum possible runtime step,
the vector trip count equals the trip count directly. This called
APInt::getZExtValue unconditionally, which asserts when the constant
value needs more than 64 bits.
Compute the URem in APInt to fix the crash.
Added:
llvm/test/Transforms/LoopVectorize/RISCV/i128-trip-count-evl.ll
Modified:
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 64d6c6fff256b..fe8edab40e846 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -5274,7 +5274,7 @@ void VPlanTransforms::materializeVectorTripCount(
// and the vector trip count equals TC directly.
const APInt *TCVal;
if (!RequiresScalarEpilogue && match(TC, m_APInt(TCVal)) && MaxRuntimeStep &&
- TCVal->getZExtValue() % *MaxRuntimeStep == 0) {
+ TCVal->urem(*MaxRuntimeStep) == 0) {
VectorTC.replaceAllUsesWith(TC);
return;
}
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/i128-trip-count-evl.ll b/llvm/test/Transforms/LoopVectorize/RISCV/i128-trip-count-evl.ll
new file mode 100644
index 0000000000000..4e7fefa61c806
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/i128-trip-count-evl.ll
@@ -0,0 +1,42 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -passes=loop-vectorize -mtriple=riscv64 -mattr=+v \
+; RUN: -force-tail-folding-style=data-with-evl \
+; RUN: --tail-folding-policy=must-fold-tail -S %s | FileCheck %s
+
+define void @i128_trip_count(ptr %p) {
+; CHECK-LABEL: define void @i128_trip_count(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i128 [ 0, %[[VECTOR_PH]] ], [ [[CURRENT_ITERATION_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[AVL:%.*]] = phi i128 [ 18446744073709551617, %[[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.experimental.get.vector.length.i128(i128 [[AVL]], i32 8, i1 true)
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr [[P]], i128 [[INDEX]]
+; CHECK-NEXT: call void @llvm.vp.store.nxv8i16.p0(<vscale x 8 x i16> splat (i16 1), ptr align 2 [[TMP1]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP0]])
+; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP0]] to i128
+; CHECK-NEXT: [[CURRENT_ITERATION_NEXT]] = add i128 [[TMP2]], [[INDEX]]
+; CHECK-NEXT: [[AVL_NEXT]] = sub nuw i128 [[AVL]], [[TMP2]]
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i128 [[AVL_NEXT]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i128 [ 0, %entry ], [ %iv.next, %loop ]
+ %p.iv = getelementptr inbounds i16, ptr %p, i128 %iv
+ store i16 1, ptr %p.iv, align 2
+ %iv.next = add i128 %iv, 1
+ %ec = icmp eq i128 %iv.next, 18446744073709551617
+ br i1 %ec, label %exit, label %loop
+
+exit:
+ ret void
+}
More information about the llvm-commits
mailing list