[llvm] [MIRParser] Reject a one-element fixed-length vector type (PR #213290)
Taimuraz Kaitmazov via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 31 08:25:07 PDT 2026
https://github.com/atassis created https://github.com/llvm/llvm-project/pull/213290
MIParser accepts `<1 x sN>` and then asserts while building the LLT:
```
llc: llvm/include/llvm/CodeGenTypes/LowLevelType.h:72:
static LLT llvm::LLT::vector(ElementCount, LLT):
Assertion `!EC.isScalar() && "invalid number of vector elements"' failed.
```
It aborts during parsing, before any pass runs, so the failure carries no context
and a FileCheck downstream of it only reports empty input. I hit this writing a
.mir test in an out of tree target.
LLT has no one-element fixed-length vector. `getLLTForType`, `getLLTForMVT` and
`LLT::changeElementCount` all route a one-element `ElementCount` through
`LLT::scalarOrVector`, which hands back the element type, so nothing in tree can
produce one. Nothing calls `LLT::fixed_vector(1, ...)`, and no in-tree .mir test
uses the syntax.
`verifyVectorElementCount` already rejects 0 and anything above 2^16, and both
have tests (`parse-low-level-type-invalid7.mir`,
`parse-low-level-type-invalid8.mir`). 1 was the remaining hole, and it now gets
the same diagnostic instead of aborting.
Scalable vectors are unaffected. `<vscale x 1 x sN>` is a valid type and stays
legal through the `HasVScale` check; `CodeGen/MIR/Generic/scalable-vector-type.mir`
covers it for s8, s16, s32, s64 and p0, and 57 in-tree .mir files use the syntax.
## Testing
Release with assertions, AArch64:
- `check-llvm-codegen-aarch64`: 4133 passed, 5 expected failures, no failures.
- `check-llvm-codegen-mir`: 53 passed, no failures. The rest need targets I did
not build.
- The new negative test sits with the existing element-count ones and reports the
same diagnostic.
- By hand: `<1 x s64>` and `<1 x p0>` now report `invalid number of vector
elements` instead of aborting. `<vscale x 1 x s8>`, `<vscale x 1 x s64>`,
`<vscale x 1 x p0>`, `<2 x s64>` and `<vscale x 2 x s64>` still parse. `<0 x s1>`
and `<65536 x s1>` are still rejected.
>From 7516da0a6d63afe922fe78ee5db6c7958c956197 Mon Sep 17 00:00:00 2001
From: Taimuraz Kaitmazov <atassikay38 at gmail.com>
Date: Fri, 31 Jul 2026 18:07:41 +0300
Subject: [PATCH] [MIRParser] Reject a one-element fixed-length vector type
MIParser accepts <1 x sN>, then LLT::vector asserts on it:
llc: LowLevelType.h:72: static LLT llvm::LLT::vector(ElementCount, LLT):
Assertion `!EC.isScalar() && "invalid number of vector elements"' failed.
LLT has no one-element fixed-length vector. getLLTForType, getLLTForMVT and
LLT::changeElementCount all map such a type to the element type itself via
LLT::scalarOrVector, so nothing can produce one, and no in-tree .mir test
uses the syntax. verifyVectorElementCount already rejects 0 and anything
above 2^16, so 1 was the one remaining hole; it now reports the same
diagnostic instead of aborting the parser.
Scalable vectors are unaffected: <vscale x 1 x sN> is a valid type, kept
legal by the HasVScale check and covered by
CodeGen/MIR/Generic/scalable-vector-type.mir.
---
llvm/lib/CodeGen/MIRParser/MIParser.cpp | 9 ++++++---
.../MIR/AArch64/parse-low-level-type-invalid11.mir | 10 ++++++++++
2 files changed, 16 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/CodeGen/MIR/AArch64/parse-low-level-type-invalid11.mir
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index f14e2cd8ce3dc..72aec66b7bfa5 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -2110,8 +2110,11 @@ static bool verifyScalarSize(uint64_t Size) {
return Size != 0 && isUInt<16>(Size);
}
-static bool verifyVectorElementCount(uint64_t NumElts) {
- return NumElts != 0 && isUInt<16>(NumElts);
+static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale) {
+ // A fixed-length vector needs at least two elements: LLT::vector rejects a
+ // one-element ElementCount, and producers map such a type to the element
+ // type itself through LLT::scalarOrVector.
+ return NumElts != 0 && (HasVScale || NumElts != 1) && isUInt<16>(NumElts);
}
static bool verifyAddrSpace(uint64_t AddrSpace) {
@@ -2198,7 +2201,7 @@ bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
if (Token.isNot(MIToken::IntegerLiteral))
return GetError();
uint64_t NumElements = Token.integerValue().getZExtValue();
- if (!verifyVectorElementCount(NumElements))
+ if (!verifyVectorElementCount(NumElements, HasVScale))
return error("invalid number of vector elements");
lex();
diff --git a/llvm/test/CodeGen/MIR/AArch64/parse-low-level-type-invalid11.mir b/llvm/test/CodeGen/MIR/AArch64/parse-low-level-type-invalid11.mir
new file mode 100644
index 0000000000000..7bce51dc5dafb
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/AArch64/parse-low-level-type-invalid11.mir
@@ -0,0 +1,10 @@
+# RUN: not llc -mtriple=aarch64-- -run-pass none -o /dev/null %s 2>&1 | FileCheck %s
+# When a fixed-length low-level type vector has 1 element
+---
+name: test_vector_1_element
+body: |
+ bb.0:
+ liveins: $x0
+ ; CHECK: [[@LINE+1]]:11: invalid number of vector elements
+ %0:_(<1 x s64>) = G_IMPLICIT_DEF
+...
More information about the llvm-commits
mailing list