[llvm] [Matrix][IR] Don't crash when verifying strides with more than 64 bits (PR #163729)
Nathan Corbyn via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 16 03:09:31 PDT 2025
https://github.com/cofibrant updated https://github.com/llvm/llvm-project/pull/163729
>From 8436cb49fe2ccd371791615f6c2186b81be5da70 Mon Sep 17 00:00:00 2001
From: Nathan Corbyn <n_corbyn at apple.com>
Date: Thu, 16 Oct 2025 10:15:28 +0100
Subject: [PATCH] [Matrix][IR] Don't crash when verifying strides with more
than 64 bits
---
llvm/lib/IR/Verifier.cpp | 11 +++++--
.../Verifier/matrix-intrinsics-strides.ll | 29 +++++++++++++++++++
llvm/test/Verifier/matrix-intrinsics.ll | 5 ++--
3 files changed, 40 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/Verifier/matrix-intrinsics-strides.ll
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index c79a95087dbdd..218bedf1a7341 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -6479,9 +6479,16 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
NumRows->getZExtValue() * NumColumns->getZExtValue(),
"Result of a matrix operation does not fit in the returned vector!");
- if (Stride)
- Check(Stride->getZExtValue() >= NumRows->getZExtValue(),
+ if (Stride) {
+ // Stride can occupy an arbitrary bit-width, while rows and columns are
+ // always 32-bit, so zero extend to the largest common bit-width to
+ // compare.
+ unsigned BitWidth =
+ std::max(Stride->getBitWidth(), NumRows->getBitWidth());
+ Check(Stride->getValue().zext(BitWidth).uge(
+ NumRows->getValue().zext(BitWidth)),
"Stride must be greater or equal than the number of rows!", IF);
+ }
break;
}
diff --git a/llvm/test/Verifier/matrix-intrinsics-strides.ll b/llvm/test/Verifier/matrix-intrinsics-strides.ll
new file mode 100644
index 0000000000000..5ba324eebe090
--- /dev/null
+++ b/llvm/test/Verifier/matrix-intrinsics-strides.ll
@@ -0,0 +1,29 @@
+; RUN: opt %s -p verify -S -disable-output
+
+; This test ensures that verifier correctly handles very wide and very narrows
+; strides.
+
+define <4 x float> @column.major_load_stride_i8(ptr %m, i32 %arg) {
+ %result.1 = call <4 x float> @llvm.matrix.column.major.load.v4f32.i128(ptr %m, i8 16, i1 false, i32 2, i32 2)
+ ret <4 x float> %result.1
+}
+
+define <4 x float> @column.major_load_stride_i128(ptr %m, i32 %arg) {
+ %result.1 = call <4 x float> @llvm.matrix.column.major.load.v4f32.i128(ptr %m, i128 u0x10000000000000000, i1 false, i32 2, i32 2)
+ ret <4 x float> %result.1
+}
+
+define void @column.major_store_stride_i8(ptr %m, i64 %arg) {
+ call void @llvm.matrix.column.major.store.v4f32.i128(<4 x float> zeroinitializer, ptr %m, i8 16, i1 false, i32 2, i32 2)
+ ret void
+}
+
+define void @column.major_store_stride_i128(ptr %m, i64 %arg) {
+ call void @llvm.matrix.column.major.store.v4f32.i128(<4 x float> zeroinitializer, ptr %m, i128 u0x10000000000000000, i1 false, i32 2, i32 2)
+ ret void
+}
+
+declare <6 x float> @llvm.matrix.column.major.load.v6f32.i8(ptr, i8, i1, i32, i32)
+declare void @llvm.matrix.column.major.store.v4p0.i8(<4 x ptr>, ptr, i8, i1, i32, i32)
+declare <6 x float> @llvm.matrix.column.major.load.v6f32.i128(ptr, i64, i1, i32, i32)
+declare void @llvm.matrix.column.major.store.v4p0.i128(<4 x ptr>, ptr, i64, i1, i32, i32)
diff --git a/llvm/test/Verifier/matrix-intrinsics.ll b/llvm/test/Verifier/matrix-intrinsics.ll
index b6d5ad9a3cc49..e208d47c1d88e 100644
--- a/llvm/test/Verifier/matrix-intrinsics.ll
+++ b/llvm/test/Verifier/matrix-intrinsics.ll
@@ -1,8 +1,7 @@
-; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
+; RUN: not opt -S %s -p verify 2>&1 | FileCheck %s
define <4 x float> @transpose(<4 x float> %m, i32 %arg) {
-; CHECK: assembly parsed, but does not verify as correct!
-; CHECK-NEXT: Result of a matrix operation does not fit in the returned vector!
+; CHECK: Result of a matrix operation does not fit in the returned vector!
; CHECK-NEXT: Result of a matrix operation does not fit in the returned vector!
; CHECK-NEXT: Result of a matrix operation does not fit in the returned vector!
; CHECK-NEXT: immarg operand has non-immediate parameter
More information about the llvm-commits
mailing list