[llvm] Improve diagnostic for malformed constant vectors (PR #216253)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 22 23:44:39 PDT 2026


https://github.com/im-lunex updated https://github.com/llvm/llvm-project/pull/216253

>From e3f47242c4558946eb004285a2e6cfe0ef86d727 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 14 Aug 2026 01:53:52 +0600
Subject: [PATCH 1/4] fix issues/29043 still left a bit

---
 llvm/lib/AsmParser/LLParser.cpp               |  6 ++++
 .../constant-vector-typed-elements.ll         | 28 +++++++++++++++++++
 .../invalid-constant-vector-repeated-type.ll  |  9 ++++++
 3 files changed, 43 insertions(+)
 create mode 100644 llvm/test/Assembler/constant-vector-typed-elements.ll
 create mode 100644 llvm/test/Assembler/invalid-constant-vector-repeated-type.ll

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index edff818b3b152..0c8d53b8dbd74 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4275,6 +4275,12 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     // ValID ::= '<' ConstVector '>'         --> Vector.
     // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
     Lex.Lex();
+
+    // guard constant vector elements must begin with a type.
+    if (Lex.getKind() == lltok::APSInt)
+      return error(Lex.getLoc(), "unexpected vector type; constant vector "
+                                 "elements should not repeat the type");
+
     bool isPackedStruct = EatIfPresent(lltok::lbrace);
 
     SmallVector<Constant*, 16> Elts;
diff --git a/llvm/test/Assembler/constant-vector-typed-elements.ll b/llvm/test/Assembler/constant-vector-typed-elements.ll
new file mode 100644
index 0000000000000..d8393be0e929c
--- /dev/null
+++ b/llvm/test/Assembler/constant-vector-typed-elements.ll
@@ -0,0 +1,28 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; make sure typed constant vectors still parse fine after the apsint guard
+
+ at my_global = external global i32
+
+; CHECK: @g1 = constant <2 x i16> <i16 123, i16 456>
+ at g1 = constant <2 x i16> <i16 123, i16 456>
+
+; CHECK: @g2 = constant <2 x float> <float 1.000000e+00, float 2.000000e+00>
+ at g2 = constant <2 x float> <float 1.0, float 2.0>
+
+; CHECK: @g3 = constant <{ i32, i32 }> <{ i32 1, i32 2 }>
+ at g3 = constant <{ i32, i32 }> <{ i32 1, i32 2 }>
+
+; CHECK: @g4 = constant <2 x ptr> <ptr @my_global, ptr @my_global>
+ at g4 = constant <2 x ptr> <ptr @my_global, ptr @my_global>
+
+; CHECK: @g5 = constant <vscale x 2 x i32> <i32 1, i32 2>
+ at g5 = constant <vscale x 2 x i32> <i32 1, i32 2>
+
+; CHECK: @g6 = constant <{ i32, ptr, float }> <{ i32 1, ptr @my_global, float 2.000000e+00 }>
+ at g6 = constant <{ i32, ptr, float }> <{ i32 1, ptr @my_global, float 2.0 }>
+
+; CHECK: @g7 = constant <4 x i32> splat (i32 7)
+ at g7 = constant <4 x i32> splat (i32 7)
+
+; CHECK: @g8 = constant <0 x i32> zeroinitializer
+ at g8 = constant <0 x i32> zeroinitializer
diff --git a/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll b/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll
new file mode 100644
index 0000000000000..af2e945773047
--- /dev/null
+++ b/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll
@@ -0,0 +1,9 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; CHECK: error: unexpected vector type; constant vector elements should not repeat the type
+
+define <2 x i16> @test(<2 x i16> %in) {
+entry:
+  %arst = add <2 x i16> %in,
+      <2 x i16> <i16 123, i16 456>
+  ret <2 x i16> %arst
+}

>From f89963d29e16133c9dbc4ae36d74f0aa457fc332 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 14 Aug 2026 10:20:01 +0600
Subject: [PATCH 2/4] format improve msg

---
 llvm/lib/AsmParser/LLParser.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 0c8d53b8dbd74..06b709e13e8ac 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4278,8 +4278,7 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
 
     // guard constant vector elements must begin with a type.
     if (Lex.getKind() == lltok::APSInt)
-      return error(Lex.getLoc(), "unexpected vector type; constant vector "
-                                 "elements should not repeat the type");
+      return error(Lex.getLoc(), "constant vector elements must begin with a type");
 
     bool isPackedStruct = EatIfPresent(lltok::lbrace);
 

>From 96bc368a368e907abb087de74913788d9939f84b Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Fri, 14 Aug 2026 11:40:23 +0600
Subject: [PATCH 3/4] improve diagnostic for constant vectors

---
 llvm/lib/AsmParser/LLParser.cpp                        |  3 ++-
 llvm/test/Assembler/constant-vector-typed-elements.ll  | 10 +---------
 .../Assembler/invalid-constant-vector-repeated-type.ll |  2 +-
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 06b709e13e8ac..44ae9b002167f 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4278,7 +4278,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
 
     // guard constant vector elements must begin with a type.
     if (Lex.getKind() == lltok::APSInt)
-      return error(Lex.getLoc(), "constant vector elements must begin with a type");
+      return error(Lex.getLoc(),
+                   "constant vector elements must begin with a type");
 
     bool isPackedStruct = EatIfPresent(lltok::lbrace);
 
diff --git a/llvm/test/Assembler/constant-vector-typed-elements.ll b/llvm/test/Assembler/constant-vector-typed-elements.ll
index d8393be0e929c..9ce26cacd1dcf 100644
--- a/llvm/test/Assembler/constant-vector-typed-elements.ll
+++ b/llvm/test/Assembler/constant-vector-typed-elements.ll
@@ -1,4 +1,5 @@
 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
 ; make sure typed constant vectors still parse fine after the apsint guard
 
 @my_global = external global i32
@@ -15,14 +16,5 @@
 ; CHECK: @g4 = constant <2 x ptr> <ptr @my_global, ptr @my_global>
 @g4 = constant <2 x ptr> <ptr @my_global, ptr @my_global>
 
-; CHECK: @g5 = constant <vscale x 2 x i32> <i32 1, i32 2>
- at g5 = constant <vscale x 2 x i32> <i32 1, i32 2>
-
-; CHECK: @g6 = constant <{ i32, ptr, float }> <{ i32 1, ptr @my_global, float 2.000000e+00 }>
- at g6 = constant <{ i32, ptr, float }> <{ i32 1, ptr @my_global, float 2.0 }>
-
 ; CHECK: @g7 = constant <4 x i32> splat (i32 7)
 @g7 = constant <4 x i32> splat (i32 7)
-
-; CHECK: @g8 = constant <0 x i32> zeroinitializer
- at g8 = constant <0 x i32> zeroinitializer
diff --git a/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll b/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll
index af2e945773047..7564606d7eb9c 100644
--- a/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll
+++ b/llvm/test/Assembler/invalid-constant-vector-repeated-type.ll
@@ -1,5 +1,5 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
-; CHECK: error: unexpected vector type; constant vector elements should not repeat the type
+; CHECK: error: constant vector elements must begin with a type
 
 define <2 x i16> @test(<2 x i16> %in) {
 entry:

>From e1e371ec9500af43538cd74cf40a02f06433e822 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Sun, 23 Aug 2026 12:44:10 +0600
Subject: [PATCH 4/4] fine tune conditional logic

---
 llvm/lib/AsmParser/LLParser.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 44ae9b002167f..11d8471a4d88a 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4277,7 +4277,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     Lex.Lex();
 
     // guard constant vector elements must begin with a type.
-    if (Lex.getKind() == lltok::APSInt)
+    if (Lex.getKind() == lltok::APSInt ||
+        Lex.getKind() == lltok::Type)
       return error(Lex.getLoc(),
                    "constant vector elements must begin with a type");
 



More information about the llvm-commits mailing list