Skip to content

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Oct 29, 2024

No description provided.

Copy link
Contributor Author

arsenm commented Oct 29, 2024

@arsenm arsenm added the llvm:ir label Oct 29, 2024 — with Graphite App
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2024

@llvm/pr-subscribers-llvm-ir

Author: Matt Arsenault (arsenm)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/114091.diff

2 Files Affected:

  • (modified) llvm/lib/IR/Verifier.cpp (+5-6)
  • (modified) llvm/unittests/IR/VerifierTest.cpp (+30)
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index ee807ca13787d5..ffcab98db9aa02 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -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())) {
@@ -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);
 }
diff --git a/llvm/unittests/IR/VerifierTest.cpp b/llvm/unittests/IR/VerifierTest.cpp
index 91cd35a10e9b92..462578a34da837 100644
--- a/llvm/unittests/IR/VerifierTest.cpp
+++ b/llvm/unittests/IR/VerifierTest.cpp
@@ -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

@arsenm arsenm marked this pull request as ready for review October 29, 2024 17:28
Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

arsenm commented Oct 30, 2024

Merge activity

  • Oct 29, 10:40 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Oct 29, 10:41 PM EDT: A user merged this pull request with Graphite.

@arsenm arsenm merged commit 9e8219a into main Oct 30, 2024
12 checks passed
@arsenm arsenm deleted the users/arsenm/ir-verifier-catch-broken-vector-of-pointer-gep branch October 30, 2024 02:41
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants