Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4121,8 +4121,9 @@ void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);

Check(GEP.getType()->isPtrOrPtrVectorTy() &&
GEP.getResultElementType() == ElTy,
PointerType *PtrTy = dyn_cast<PointerType>(GEP.getType()->getScalarType());

Check(PtrTy && GEP.getResultElementType() == ElTy,
"GEP is not of right type for indices!", &GEP, ElTy);

if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
Expand All @@ -4144,10 +4145,8 @@ void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
}
}

if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
Check(GEP.getAddressSpace() == PTy->getAddressSpace(),
"GEP address space doesn't match type", &GEP);
}
Check(GEP.getAddressSpace() == PtrTy->getAddressSpace(),
"GEP address space doesn't match type", &GEP);

visitInstruction(GEP);
}
Expand Down
30 changes: 30 additions & 0 deletions llvm/unittests/IR/VerifierTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,35 @@ TEST(VerifierTest, AtomicRMW) {
<< Error;
}

TEST(VerifierTest, GetElementPtrInst) {
LLVMContext C;
Module M("M", C);
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
ReturnInst *RI = ReturnInst::Create(C, Entry);

FixedVectorType *V2P1Ty = FixedVectorType::get(PointerType::get(C, 1), 2);
FixedVectorType *V2P2Ty = FixedVectorType::get(PointerType::get(C, 2), 2);

Instruction *GEPVec = GetElementPtrInst::Create(
Type::getInt8Ty(C), ConstantAggregateZero::get(V2P1Ty),
{ConstantVector::getSplat(ElementCount::getFixed(2),
ConstantInt::get(Type::getInt64Ty(C), 0))},
Entry);

GEPVec->insertBefore(RI);

// Break the address space of the source value
GEPVec->getOperandUse(0).set(ConstantAggregateZero::get(V2P2Ty));

std::string Error;
raw_string_ostream ErrorOS(Error);
EXPECT_TRUE(verifyFunction(*F, &ErrorOS));
EXPECT_TRUE(
StringRef(Error).starts_with("GEP address space doesn't match type"))
<< Error;
}

} // end anonymous namespace
} // end namespace llvm
Loading