[llvm] [SandboxIR] Implement GetElementPtrInst (PR #101078)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 29 13:16:23 PDT 2024
https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/101078
This patch implements sandboxir::GetElementPtrInst which mirrors llvm::GetElementPtrInst.
>From ff765ad3e03a97855cf827d9dc43f9aa6dc65a9a Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Wed, 24 Jul 2024 11:30:20 -0700
Subject: [PATCH] [SandboxIR] Implement GetElementPtrInst
This patch implements sandboxir::GetElementPtrInst which mirrors
llvm::GetElementPtrInst.
---
llvm/include/llvm/SandboxIR/SandboxIR.h | 149 +++++++++++++++---
.../llvm/SandboxIR/SandboxIRValues.def | 1 +
llvm/lib/SandboxIR/SandboxIR.cpp | 67 ++++++++
llvm/unittests/SandboxIR/SandboxIRTest.cpp | 132 ++++++++++++++++
4 files changed, 329 insertions(+), 20 deletions(-)
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index d0d4b7e7d0b87..bca6037bd994b 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -93,6 +93,7 @@ class CallBase;
class CallInst;
class InvokeInst;
class CallBrInst;
+class GetElementPtrInst;
/// Iterator for the `Use` edges of a User's operands.
/// \Returns the operand `Use` when dereferenced.
@@ -196,18 +197,19 @@ class Value {
/// order.
llvm::Value *Val = nullptr;
- friend class Context; // For getting `Val`.
- friend class User; // For getting `Val`.
- friend class Use; // For getting `Val`.
- friend class SelectInst; // For getting `Val`.
- friend class BranchInst; // For getting `Val`.
- friend class LoadInst; // For getting `Val`.
- friend class StoreInst; // For getting `Val`.
- friend class ReturnInst; // For getting `Val`.
- friend class CallBase; // For getting `Val`.
- friend class CallInst; // For getting `Val`.
- friend class InvokeInst; // For getting `Val`.
- friend class CallBrInst; // For getting `Val`.
+ friend class Context; // For getting `Val`.
+ friend class User; // For getting `Val`.
+ friend class Use; // For getting `Val`.
+ friend class SelectInst; // For getting `Val`.
+ friend class BranchInst; // For getting `Val`.
+ friend class LoadInst; // For getting `Val`.
+ friend class StoreInst; // For getting `Val`.
+ friend class ReturnInst; // For getting `Val`.
+ friend class CallBase; // For getting `Val`.
+ friend class CallInst; // For getting `Val`.
+ friend class InvokeInst; // For getting `Val`.
+ friend class CallBrInst; // For getting `Val`.
+ friend class GetElementPtrInst; // For getting `Val`.
/// All values point to the context.
Context &Ctx;
@@ -540,14 +542,15 @@ class Instruction : public sandboxir::User {
/// A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
/// returns its topmost LLVM IR instruction.
llvm::Instruction *getTopmostLLVMInstruction() const;
- friend class SelectInst; // For getTopmostLLVMInstruction().
- friend class BranchInst; // For getTopmostLLVMInstruction().
- friend class LoadInst; // For getTopmostLLVMInstruction().
- friend class StoreInst; // For getTopmostLLVMInstruction().
- friend class ReturnInst; // For getTopmostLLVMInstruction().
- friend class CallInst; // For getTopmostLLVMInstruction().
- friend class InvokeInst; // For getTopmostLLVMInstruction().
- friend class CallBrInst; // For getTopmostLLVMInstruction().
+ friend class SelectInst; // For getTopmostLLVMInstruction().
+ friend class BranchInst; // For getTopmostLLVMInstruction().
+ friend class LoadInst; // For getTopmostLLVMInstruction().
+ friend class StoreInst; // For getTopmostLLVMInstruction().
+ friend class ReturnInst; // For getTopmostLLVMInstruction().
+ friend class CallInst; // For getTopmostLLVMInstruction().
+ friend class InvokeInst; // For getTopmostLLVMInstruction().
+ friend class CallBrInst; // For getTopmostLLVMInstruction().
+ friend class GetElementPtrInst; // For getTopmostLLVMInstruction().
/// \Returns the LLVM IR Instructions that this SandboxIR maps to in program
/// order.
@@ -1175,6 +1178,110 @@ class CallBrInst final : public CallBase {
#endif
};
+class GetElementPtrInst final : public Instruction {
+ /// Use Context::createGetElementPtrInst(). Don't call
+ /// the constructor directly.
+ GetElementPtrInst(llvm::Instruction *I, Context &Ctx)
+ : Instruction(ClassID::GetElementPtr, Opcode::GetElementPtr, I, Ctx) {}
+ GetElementPtrInst(ClassID SubclassID, llvm::Instruction *I, Context &Ctx)
+ : Instruction(SubclassID, Opcode::GetElementPtr, I, Ctx) {}
+ friend class Context; // For accessing the constructor in
+ // create*()
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
+ return getOperandUseDefault(OpIdx, Verify);
+ }
+ SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
+ return {cast<llvm::Instruction>(Val)};
+ }
+
+public:
+ static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
+ BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
+ const Twine &NameStr = "");
+ static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
+ Instruction *InsertBefore, Context &Ctx,
+ const Twine &NameStr = "");
+ static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
+ BasicBlock *InsertAtEnd, Context &Ctx,
+ const Twine &NameStr = "");
+
+ static bool classof(const Value *From) {
+ return From->getSubclassID() == ClassID::GetElementPtr;
+ }
+ unsigned getUseOperandNo(const Use &Use) const final {
+ return getUseOperandNoDefault(Use);
+ }
+ unsigned getNumOfIRInstrs() const final { return 1u; }
+
+ Type *getSourceElementType() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getSourceElementType();
+ }
+ Type *getResultElementType() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getResultElementType();
+ }
+ unsigned getAddressSpace() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace();
+ }
+
+ inline op_iterator idx_begin() { return op_begin() + 1; }
+ inline const_op_iterator idx_begin() const {
+ return const_cast<GetElementPtrInst *>(this)->idx_begin();
+ }
+ inline op_iterator idx_end() { return op_end(); }
+ inline const_op_iterator idx_end() const {
+ return const_cast<GetElementPtrInst *>(this)->idx_end();
+ }
+ inline iterator_range<op_iterator> indices() {
+ return make_range(idx_begin(), idx_end());
+ }
+ inline iterator_range<const_op_iterator> indices() const {
+ return const_cast<GetElementPtrInst *>(this)->indices();
+ }
+
+ Value *getPointerOperand() const;
+ static unsigned getPointerOperandIndex() {
+ return llvm::GetElementPtrInst::getPointerOperandIndex();
+ }
+ Type *getPointerOperandType() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType();
+ }
+ unsigned getPointerAddressSpace() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace();
+ }
+ unsigned getNumIndices() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getNumIndices();
+ }
+ bool hasIndices() const {
+ return cast<llvm::GetElementPtrInst>(Val)->hasIndices();
+ }
+ bool hasAllConstantIndices() const {
+ return cast<llvm::GetElementPtrInst>(Val)->hasAllConstantIndices();
+ }
+ GEPNoWrapFlags getNoWrapFlags() const {
+ return cast<llvm::GetElementPtrInst>(Val)->getNoWrapFlags();
+ }
+ bool isInBounds() const {
+ return cast<llvm::GetElementPtrInst>(Val)->isInBounds();
+ }
+ bool hasNoUnsignedSignedWrap() const {
+ return cast<llvm::GetElementPtrInst>(Val)->hasNoUnsignedSignedWrap();
+ }
+ bool hasNoUnsignedWrap() const {
+ return cast<llvm::GetElementPtrInst>(Val)->hasNoUnsignedWrap();
+ }
+ bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
+ return cast<llvm::GetElementPtrInst>(Val)->accumulateConstantOffset(DL,
+ Offset);
+ }
+ // TODO: Add missing member functions.
+
+#ifndef NDEBUG
+ void verify() const final {}
+ void dump(raw_ostream &OS) const override;
+ LLVM_DUMP_METHOD void dump() const override;
+#endif
+};
+
/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
/// an OpaqueInstr.
class OpaqueInst : public sandboxir::Instruction {
@@ -1329,6 +1436,8 @@ class Context {
friend InvokeInst; // For createInvokeInst()
CallBrInst *createCallBrInst(llvm::CallBrInst *I);
friend CallBrInst; // For createCallBrInst()
+ GetElementPtrInst *createGetElementPtrInst(llvm::GetElementPtrInst *I);
+ friend GetElementPtrInst; // For createGetElementPtrInst()
public:
Context(LLVMContext &LLVMCtx)
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index 5f6fc84fc2e07..29fb3aa396fdb 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -33,6 +33,7 @@ DEF_INSTR(Ret, OP(Ret), ReturnInst)
DEF_INSTR(Call, OP(Call), CallInst)
DEF_INSTR(Invoke, OP(Invoke), InvokeInst)
DEF_INSTR(CallBr, OP(CallBr), CallBrInst)
+DEF_INSTR(GetElementPtr, OP(GetElementPtr), GetElementPtrInst)
#ifdef DEF_VALUE
#undef DEF_VALUE
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 7c66ecbac4872..7ed3e24e247be 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -996,6 +996,60 @@ void CallBrInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}
+#endif // NDEBUG
+
+Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
+ ArrayRef<Value *> IdxList,
+ BasicBlock::iterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &NameStr) {
+ auto &Builder = Ctx.getLLVMIRBuilder();
+ if (WhereIt != WhereBB->end())
+ Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
+ else
+ Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
+ SmallVector<llvm::Value *> LLVMIdxList;
+ LLVMIdxList.reserve(IdxList.size());
+ for (Value *Idx : IdxList)
+ LLVMIdxList.push_back(Idx->Val);
+ llvm::Value *NewV = Builder.CreateGEP(Ty, Ptr->Val, LLVMIdxList, NameStr);
+ if (auto *NewGEP = dyn_cast<llvm::GetElementPtrInst>(NewV))
+ return Ctx.createGetElementPtrInst(NewGEP);
+ assert(isa<llvm::Constant>(NewV) && "Expected constant");
+ return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
+}
+
+Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
+ ArrayRef<Value *> IdxList,
+ Instruction *InsertBefore, Context &Ctx,
+ const Twine &NameStr) {
+ return GetElementPtrInst::create(Ty, Ptr, IdxList,
+ InsertBefore->getIterator(),
+ InsertBefore->getParent(), Ctx, NameStr);
+}
+
+Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
+ ArrayRef<Value *> IdxList,
+ BasicBlock *InsertAtEnd, Context &Ctx,
+ const Twine &NameStr) {
+ return GetElementPtrInst::create(Ty, Ptr, IdxList, InsertAtEnd->end(),
+ InsertAtEnd, Ctx, NameStr);
+}
+
+Value *GetElementPtrInst::getPointerOperand() const {
+ return Ctx.getValue(cast<llvm::GetElementPtrInst>(Val)->getPointerOperand());
+}
+
+#ifndef NDEBUG
+void GetElementPtrInst::dump(raw_ostream &OS) const {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+}
+
+void GetElementPtrInst::dump() const {
+ dump(dbgs());
+ dbgs() << "\n";
+}
void OpaqueInst::dump(raw_ostream &OS) const {
dumpCommonPrefix(OS);
@@ -1165,6 +1219,12 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<CallBrInst>(new CallBrInst(LLVMCallBr, *this));
return It->second.get();
}
+ case llvm::Instruction::GetElementPtr: {
+ auto *LLVMGEP = cast<llvm::GetElementPtrInst>(LLVMV);
+ It->second = std::unique_ptr<GetElementPtrInst>(
+ new GetElementPtrInst(LLVMGEP, *this));
+ return It->second.get();
+ }
default:
break;
}
@@ -1223,6 +1283,13 @@ CallBrInst *Context::createCallBrInst(llvm::CallBrInst *I) {
return cast<CallBrInst>(registerValue(std::move(NewPtr)));
}
+GetElementPtrInst *
+Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
+ auto NewPtr =
+ std::unique_ptr<GetElementPtrInst>(new GetElementPtrInst(I, *this));
+ return cast<GetElementPtrInst>(registerValue(std::move(NewPtr)));
+}
+
Value *Context::getValue(llvm::Value *V) const {
auto It = LLVMValueToValueMap.find(V);
if (It != LLVMValueToValueMap.end())
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 1ae37aa384f16..56e906744a9ca 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -9,6 +9,7 @@
#include "llvm/SandboxIR/SandboxIR.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Module.h"
@@ -1281,3 +1282,134 @@ define void @foo(i8 %arg) {
EXPECT_EQ(NewCallBr->getParent(), BB0);
}
}
+
+TEST_F(SandboxIRTest, GetElementPtrInstruction) {
+ parseIR(C, R"IR(
+define void @foo(ptr %ptr, <2 x ptr> %ptrs) {
+ %gep0 = getelementptr i8, ptr %ptr, i32 0
+ %gep1 = getelementptr nusw i8, ptr %ptr, i32 0
+ %gep2 = getelementptr nuw i8, ptr %ptr, i32 0
+ %gep3 = getelementptr inbounds {i32, {i32, i8}}, ptr %ptr, i32 1, i32 0
+ %gep4 = getelementptr inbounds {i8, i8, {i32, i16}}, <2 x ptr> %ptrs, i32 2, <2 x i32> <i32 0, i32 0>
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ BasicBlock *LLVMBB = &*LLVMF.begin();
+ auto LLVMIt = LLVMBB->begin();
+ SmallVector<llvm::GetElementPtrInst *, 4> LLVMGEPs;
+ while (isa<llvm::GetElementPtrInst>(&*LLVMIt))
+ LLVMGEPs.push_back(cast<llvm::GetElementPtrInst>(&*LLVMIt++));
+ auto *LLVMRet = cast<llvm::ReturnInst>(&*LLVMIt++);
+ sandboxir::Context Ctx(C);
+ [[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
+
+ for (llvm::GetElementPtrInst *LLVMGEP : LLVMGEPs) {
+ // Check classof().
+ auto *GEP = cast<sandboxir::GetElementPtrInst>(Ctx.getValue(LLVMGEP));
+ // Check getSourceElementType().
+ EXPECT_EQ(GEP->getSourceElementType(), LLVMGEP->getSourceElementType());
+ // Check getResultElementType().
+ EXPECT_EQ(GEP->getResultElementType(), LLVMGEP->getResultElementType());
+ // Check getAddressSpace().
+ EXPECT_EQ(GEP->getAddressSpace(), LLVMGEP->getAddressSpace());
+ // Check indices().
+ EXPECT_EQ(range_size(GEP->indices()), range_size(LLVMGEP->indices()));
+ auto IdxIt = GEP->idx_begin();
+ for (llvm::Value *LLVMIdxV : LLVMGEP->indices()) {
+ sandboxir::Value *IdxV = *IdxIt++;
+ EXPECT_EQ(IdxV, Ctx.getValue(LLVMIdxV));
+ }
+ // Check getPointerOperand().
+ EXPECT_EQ(GEP->getPointerOperand(),
+ Ctx.getValue(LLVMGEP->getPointerOperand()));
+ // Check getPointerOperandIndex().
+ EXPECT_EQ(GEP->getPointerOperandIndex(), LLVMGEP->getPointerOperandIndex());
+ // Check getPointerOperandType().
+ EXPECT_EQ(GEP->getPointerOperandType(), LLVMGEP->getPointerOperandType());
+ // Check getPointerAddressSpace().
+ EXPECT_EQ(GEP->getPointerAddressSpace(), LLVMGEP->getPointerAddressSpace());
+ // Check getNumIndices().
+ EXPECT_EQ(GEP->getNumIndices(), LLVMGEP->getNumIndices());
+ // Check hasIndices().
+ EXPECT_EQ(GEP->hasIndices(), LLVMGEP->hasIndices());
+ // Check hasAllConstantIndices().
+ EXPECT_EQ(GEP->hasAllConstantIndices(), LLVMGEP->hasAllConstantIndices());
+ // Check getNoWrapFlags().
+ EXPECT_EQ(GEP->getNoWrapFlags(), LLVMGEP->getNoWrapFlags());
+ // Check isInBounds().
+ EXPECT_EQ(GEP->isInBounds(), LLVMGEP->isInBounds());
+ // Check hasNoUnsignedWrap().
+ EXPECT_EQ(GEP->hasNoUnsignedWrap(), LLVMGEP->hasNoUnsignedWrap());
+ // Check accumulateConstantOffset().
+ DataLayout DL(M.get());
+ APInt Offset1 =
+ APInt::getZero(DL.getIndexSizeInBits(GEP->getPointerAddressSpace()));
+ APInt Offset2 =
+ APInt::getZero(DL.getIndexSizeInBits(GEP->getPointerAddressSpace()));
+ EXPECT_EQ(GEP->accumulateConstantOffset(DL, Offset1),
+ LLVMGEP->accumulateConstantOffset(DL, Offset2));
+ EXPECT_EQ(Offset1, Offset2);
+ }
+
+ auto *BB = &*F.begin();
+ auto *GEP0 = cast<sandboxir::GetElementPtrInst>(&*BB->begin());
+ auto *Ret = cast<sandboxir::ReturnInst>(Ctx.getValue(LLVMRet));
+ SmallVector<sandboxir::Value *> Indices(GEP0->indices());
+
+ // Check create() WhereIt, WhereBB.
+ auto *NewGEP0 =
+ cast<sandboxir::GetElementPtrInst>(sandboxir::GetElementPtrInst::create(
+ GEP0->getType(), GEP0->getPointerOperand(), Indices,
+ /*WhereIt=*/Ret->getIterator(), /*WhereBB=*/Ret->getParent(), Ctx,
+ "NewGEP0"));
+ EXPECT_EQ(NewGEP0->getName(), "NewGEP0");
+ EXPECT_EQ(NewGEP0->getType(), GEP0->getType());
+ EXPECT_EQ(NewGEP0->getPointerOperand(), GEP0->getPointerOperand());
+ EXPECT_EQ(range_size(NewGEP0->indices()), range_size(GEP0->indices()));
+ for (auto NewIt = NewGEP0->idx_begin(), NewItE = NewGEP0->idx_end(),
+ OldIt = GEP0->idx_begin();
+ NewIt != NewItE; ++NewIt) {
+ sandboxir::Value *NewIdxV = *NewIt;
+ sandboxir::Value *OldIdxV = *OldIt;
+ EXPECT_EQ(NewIdxV, OldIdxV);
+ }
+ EXPECT_EQ(NewGEP0->getNextNode(), Ret);
+
+ // Check create() InsertBefore.
+ auto *NewGEP1 =
+ cast<sandboxir::GetElementPtrInst>(sandboxir::GetElementPtrInst::create(
+ GEP0->getType(), GEP0->getPointerOperand(), Indices,
+ /*InsertBefore=*/Ret, Ctx, "NewGEP1"));
+ EXPECT_EQ(NewGEP1->getName(), "NewGEP1");
+ EXPECT_EQ(NewGEP1->getType(), GEP0->getType());
+ EXPECT_EQ(NewGEP1->getPointerOperand(), GEP0->getPointerOperand());
+ EXPECT_EQ(range_size(NewGEP1->indices()), range_size(GEP0->indices()));
+ for (auto NewIt = NewGEP0->idx_begin(), NewItE = NewGEP0->idx_end(),
+ OldIt = GEP0->idx_begin();
+ NewIt != NewItE; ++NewIt) {
+ sandboxir::Value *NewIdxV = *NewIt;
+ sandboxir::Value *OldIdxV = *OldIt;
+ EXPECT_EQ(NewIdxV, OldIdxV);
+ }
+ EXPECT_EQ(NewGEP1->getNextNode(), Ret);
+
+ // Check create() InsertAtEnd.
+ auto *NewGEP2 =
+ cast<sandboxir::GetElementPtrInst>(sandboxir::GetElementPtrInst::create(
+ GEP0->getType(), GEP0->getPointerOperand(), Indices,
+ /*InsertAtEnd=*/BB, Ctx, "NewGEP2"));
+ EXPECT_EQ(NewGEP2->getName(), "NewGEP2");
+ EXPECT_EQ(NewGEP2->getType(), GEP0->getType());
+ EXPECT_EQ(NewGEP2->getPointerOperand(), GEP0->getPointerOperand());
+ EXPECT_EQ(range_size(NewGEP2->indices()), range_size(GEP0->indices()));
+ for (auto NewIt = NewGEP0->idx_begin(), NewItE = NewGEP0->idx_end(),
+ OldIt = GEP0->idx_begin();
+ NewIt != NewItE; ++NewIt) {
+ sandboxir::Value *NewIdxV = *NewIt;
+ sandboxir::Value *OldIdxV = *OldIt;
+ EXPECT_EQ(NewIdxV, OldIdxV);
+ }
+ EXPECT_EQ(NewGEP2->getPrevNode(), Ret);
+ EXPECT_EQ(NewGEP2->getNextNode(), nullptr);
+}
More information about the llvm-commits
mailing list