[llvm] r290261 - [LLParser] Parse vector GEP constant expression correctly

Michael Kuperstein via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 21 10:29:47 PST 2016


Author: mkuper
Date: Wed Dec 21 12:29:47 2016
New Revision: 290261

URL: http://llvm.org/viewvc/llvm-project?rev=290261&view=rev
Log:
[LLParser] Parse vector GEP constant expression correctly

The constantexpr parsing was too constrained and rejected legal vector GEPs.
This relaxes it to be similar to the ones for instruction parsing.

This fixes PR30816.

Differential Revision: https://reviews.llvm.org/D28013

Added:
    llvm/trunk/test/Assembler/getelementptr_vec_ce.ll
    llvm/trunk/test/Assembler/getelementptr_vec_ce2.ll
Modified:
    llvm/trunk/lib/AsmParser/LLParser.cpp

Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=290261&r1=290260&r2=290261&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Wed Dec 21 12:29:47 2016
@@ -3193,20 +3193,23 @@ bool LLParser::ParseValID(ValID &ID, Per
             ExplicitTypeLoc,
             "explicit pointee type doesn't match operand's pointee type");
 
+      unsigned GEPWidth =
+          BaseType->isVectorTy() ? BaseType->getVectorNumElements() : 0;
+
       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
       for (Constant *Val : Indices) {
         Type *ValTy = Val->getType();
         if (!ValTy->getScalarType()->isIntegerTy())
           return Error(ID.Loc, "getelementptr index must be an integer");
-        if (ValTy->isVectorTy() != BaseType->isVectorTy())
-          return Error(ID.Loc, "getelementptr index type missmatch");
         if (ValTy->isVectorTy()) {
           unsigned ValNumEl = ValTy->getVectorNumElements();
-          unsigned PtrNumEl = BaseType->getVectorNumElements();
-          if (ValNumEl != PtrNumEl)
+          if (GEPWidth && (ValNumEl != GEPWidth))
             return Error(
                 ID.Loc,
                 "getelementptr vector index has a wrong number of elements");
+          // GEPWidth may have been unknown because the base is a scalar,
+          // but it is known now.
+          GEPWidth = ValNumEl;
         }
       }
 

Added: llvm/trunk/test/Assembler/getelementptr_vec_ce.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/getelementptr_vec_ce.ll?rev=290261&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/getelementptr_vec_ce.ll (added)
+++ llvm/trunk/test/Assembler/getelementptr_vec_ce.ll Wed Dec 21 12:29:47 2016
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+ at G = global [4 x i32] zeroinitializer
+
+; CHECK-LABEL: @foo
+; CHECK: ret <4 x i32*> getelementptr ([4 x i32], [4 x i32]* @G, <4 x i32> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>)
+define <4 x i32*> @foo() {
+  ret <4 x i32*> getelementptr ([4 x i32], [4 x i32]* @G, i32 0, <4 x i32> <i32 0, i32 1, i32 2, i32 3>)
+}

Added: llvm/trunk/test/Assembler/getelementptr_vec_ce2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/getelementptr_vec_ce2.ll?rev=290261&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/getelementptr_vec_ce2.ll (added)
+++ llvm/trunk/test/Assembler/getelementptr_vec_ce2.ll Wed Dec 21 12:29:47 2016
@@ -0,0 +1,8 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+ at G = global [4 x [4 x i32]] zeroinitializer
+
+; CHECK: getelementptr vector index has a wrong number of elements
+define <4 x i32*> @foo() {
+  ret <4 x i32*> getelementptr ([4 x [4 x i32]], [4 x [4 x i32]]* @G, i32 0, <4 x i32> <i32 0, i32 1, i32 2, i32 3>, <8 x i32> zeroinitializer)
+}




More information about the llvm-commits mailing list