diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index 6137faf38a9f..f6faf1309d87 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -3016,7 +3016,11 @@ void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { Assert(isa(TargetTy), "GEP base pointer is not a vector or a vector of pointers", &GEP); Assert(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP); + SmallVector Idxs(GEP.idx_begin(), GEP.idx_end()); + Assert(all_of( + Idxs, [](Value* V) { return V->getType()->isIntOrIntVectorTy(); }), + "GEP indexes must be integers", &GEP); Type *ElTy = GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs); Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP); diff --git a/llvm/test/Verifier/non-integer-gep-index.ll b/llvm/test/Verifier/non-integer-gep-index.ll new file mode 100644 index 000000000000..ba2dbeab8973 --- /dev/null +++ b/llvm/test/Verifier/non-integer-gep-index.ll @@ -0,0 +1,8 @@ +; RUN: not opt -verify %s.bc -disable-output + +; Test that verifier checks that gep indexes has correct type +; Specifically we want to check for the following pattern: +; %A1 = alloca i64 +; %G = getelementptr i64, i64* %A1, %A1 +; IR parser checks for this pattern independently from the verifier, so it's +; impossible to load from .ll file. Hence in this test we use bytecode input. diff --git a/llvm/test/Verifier/non-integer-gep-index.ll.bc b/llvm/test/Verifier/non-integer-gep-index.ll.bc new file mode 100644 index 000000000000..6d31d7b5e4ca Binary files /dev/null and b/llvm/test/Verifier/non-integer-gep-index.ll.bc differ