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

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 03:23:36 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/8] 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/8] 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/8] 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/8] 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");
 

>From bdad411950442244acb2495cdd910d8e73d9f1b5 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Sun, 23 Aug 2026 13:00:51 +0600
Subject: [PATCH 5/8] formation

---
 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 11d8471a4d88a..b43150b0c555d 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4277,8 +4277,7 @@ 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 ||
-        Lex.getKind() == lltok::Type)
+    if (Lex.getKind() == lltok::APSInt || Lex.getKind() == lltok::Type)
       return error(Lex.getLoc(),
                    "constant vector elements must begin with a type");
 

>From 7549e760d05fa29a59552d442bf8a36f31334858 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Sun, 23 Aug 2026 17:34:34 +0600
Subject: [PATCH 6/8] try new approch

---
 llvm/include/llvm/AsmParser/LLParser.h |  1 +
 llvm/lib/AsmParser/LLParser.cpp        | 21 +++++++++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/AsmParser/LLParser.h b/llvm/include/llvm/AsmParser/LLParser.h
index 788b56cb78f08..86f2e2b7e4c27 100644
--- a/llvm/include/llvm/AsmParser/LLParser.h
+++ b/llvm/include/llvm/AsmParser/LLParser.h
@@ -196,6 +196,7 @@ namespace llvm {
 
     bool SeenNewDbgInfoFormat = false;
     bool SeenOldDbgInfoFormat = false;
+    bool InConstantVector = false;
 
     std::string SourceFileName;
 
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index b43150b0c555d..2a14585946a64 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3178,6 +3178,9 @@ bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
   SMLoc TypeLoc = Lex.getLoc();
   switch (Lex.getKind()) {
   default:
+    if (InConstantVector && Lex.getKind() == lltok::APSInt)
+      return error(Lex.getLoc(),
+                   "constant vector elements must begin with a type");
     return tokError(Msg);
   case lltok::Type:
     // Type ::= 'float' | 'void' (etc)
@@ -3230,6 +3233,10 @@ bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
         return true;
     } else if (parseArrayVectorType(Result, true))
       return true;
+    if (InConstantVector && Result && Result->isVectorTy())
+      return error(TypeLoc,
+                   "unexpected vector type; constant vector elements should not "
+                   "repeat the type");
     break;
   case lltok::LocalVar: {
     // Type ::= %foo
@@ -4203,6 +4210,9 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     ID.Kind = ValID::t_LocalName;
     break;
   case lltok::APSInt:
+    if (InConstantVector && !ExpectedTy)
+      return error(ID.Loc,
+                   "constant vector elements must begin with a type");
     ID.APSIntVal = Lex.getAPSIntVal();
     ID.Kind = ValID::t_APSInt;
     break;
@@ -4276,16 +4286,15 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
     Lex.Lex();
 
-    // guard constant vector elements must begin with a type.
-    if (Lex.getKind() == lltok::APSInt || Lex.getKind() == lltok::Type)
-      return error(Lex.getLoc(),
-                   "constant vector elements must begin with a type");
-
     bool isPackedStruct = EatIfPresent(lltok::lbrace);
 
     SmallVector<Constant*, 16> Elts;
     LocTy FirstEltLoc = Lex.getLoc();
-    if (parseGlobalValueVector(Elts) ||
+    bool SavedInConstantVector = InConstantVector;
+    InConstantVector = true;
+    bool ParseRes = parseGlobalValueVector(Elts);
+    InConstantVector = SavedInConstantVector;
+    if (ParseRes ||
         (isPackedStruct &&
          parseToken(lltok::rbrace, "expected end of packed struct")) ||
         parseToken(lltok::greater, "expected end of constant"))

>From da8eac146c5f99490befc407271ece209ff82654 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Sun, 23 Aug 2026 17:34:51 +0600
Subject: [PATCH 7/8] try new approch

---
 llvm/lib/AsmParser/LLParser.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 2a14585946a64..e740ac29e640d 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3234,9 +3234,10 @@ bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
     } else if (parseArrayVectorType(Result, true))
       return true;
     if (InConstantVector && Result && Result->isVectorTy())
-      return error(TypeLoc,
-                   "unexpected vector type; constant vector elements should not "
-                   "repeat the type");
+      return error(
+          TypeLoc,
+          "unexpected vector type; constant vector elements should not "
+          "repeat the type");
     break;
   case lltok::LocalVar: {
     // Type ::= %foo
@@ -4211,8 +4212,7 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     break;
   case lltok::APSInt:
     if (InConstantVector && !ExpectedTy)
-      return error(ID.Loc,
-                   "constant vector elements must begin with a type");
+      return error(ID.Loc, "constant vector elements must begin with a type");
     ID.APSIntVal = Lex.getAPSIntVal();
     ID.Kind = ValID::t_APSInt;
     break;

>From 1c9addd5c459935952c6c2b227641807348f5da3 Mon Sep 17 00:00:00 2001
From: im-lunex <thisissamir04 at gmail.com>
Date: Tue, 25 Aug 2026 16:23:17 +0600
Subject: [PATCH 8/8] improve malformed const vec diagnostic

---
 llvm/lib/AsmParser/LLParser.cpp | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index e740ac29e640d..acd0b4d14d3ec 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4945,13 +4945,24 @@ bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
     return false;
 
   do {
-    // Let the caller deal with inrange.
     if (Lex.getKind() == lltok::kw_inrange)
       return false;
 
     Constant *C;
-    if (parseGlobalTypeAndValue(C))
-      return true;
+    if (InConstantVector) {
+      Type *Ty = nullptr;
+      bool Saved = InConstantVector;
+      if (parseType(Ty))
+        return true;
+      InConstantVector = false;
+      bool Res = parseGlobalValue(Ty, C);
+      InConstantVector = Saved;
+      if (Res)
+        return true;
+    } else {
+      if (parseGlobalTypeAndValue(C))
+        return true;
+    }
     Elts.push_back(C);
   } while (EatIfPresent(lltok::comma));
 



More information about the llvm-commits mailing list