Skip to content

Commit 9e8219a

Browse files
authored
IR: Fix verifier missing addrspace mismatch in vector GEPs (#114091)
1 parent facdae6 commit 9e8219a

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,8 +4121,9 @@ void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
41214121
GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
41224122
Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);
41234123

4124-
Check(GEP.getType()->isPtrOrPtrVectorTy() &&
4125-
GEP.getResultElementType() == ElTy,
4124+
PointerType *PtrTy = dyn_cast<PointerType>(GEP.getType()->getScalarType());
4125+
4126+
Check(PtrTy && GEP.getResultElementType() == ElTy,
41264127
"GEP is not of right type for indices!", &GEP, ElTy);
41274128

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

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

41524151
visitInstruction(GEP);
41534152
}

llvm/unittests/IR/VerifierTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,35 @@ TEST(VerifierTest, AtomicRMW) {
385385
<< Error;
386386
}
387387

388+
TEST(VerifierTest, GetElementPtrInst) {
389+
LLVMContext C;
390+
Module M("M", C);
391+
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
392+
Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M);
393+
BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
394+
ReturnInst *RI = ReturnInst::Create(C, Entry);
395+
396+
FixedVectorType *V2P1Ty = FixedVectorType::get(PointerType::get(C, 1), 2);
397+
FixedVectorType *V2P2Ty = FixedVectorType::get(PointerType::get(C, 2), 2);
398+
399+
Instruction *GEPVec = GetElementPtrInst::Create(
400+
Type::getInt8Ty(C), ConstantAggregateZero::get(V2P1Ty),
401+
{ConstantVector::getSplat(ElementCount::getFixed(2),
402+
ConstantInt::get(Type::getInt64Ty(C), 0))},
403+
Entry);
404+
405+
GEPVec->insertBefore(RI);
406+
407+
// Break the address space of the source value
408+
GEPVec->getOperandUse(0).set(ConstantAggregateZero::get(V2P2Ty));
409+
410+
std::string Error;
411+
raw_string_ostream ErrorOS(Error);
412+
EXPECT_TRUE(verifyFunction(*F, &ErrorOS));
413+
EXPECT_TRUE(
414+
StringRef(Error).starts_with("GEP address space doesn't match type"))
415+
<< Error;
416+
}
417+
388418
} // end anonymous namespace
389419
} // end namespace llvm

0 commit comments

Comments
 (0)