[llvm] d36c9f8 - [SandboxIR] Implement AddrSpaceCastInst (#101260)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 31 09:14:52 PDT 2024
Author: vporpo
Date: 2024-07-31T09:14:46-07:00
New Revision: d36c9f828d795d77127ea0fada6be4b7b5e19dbb
URL: https://github.com/llvm/llvm-project/commit/d36c9f828d795d77127ea0fada6be4b7b5e19dbb
DIFF: https://github.com/llvm/llvm-project/commit/d36c9f828d795d77127ea0fada6be4b7b5e19dbb.diff
LOG: [SandboxIR] Implement AddrSpaceCastInst (#101260)
This patch implements sandboxir::AddrSpaceCastInst which mirrors
llvm::AddrSpaceCastInst.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 5b2330c35ee23..b4a69885393fa 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -28,9 +28,11 @@
// |
// +- BranchInst
// |
-// +- CastInst -----------+- BitCastInst
-// | |
-// | +- PtrToIntInst
+// +- CastInst --------+- AddrSpaceCastInst
+// | |
+// | +- BitCastInst
+// | |
+// | +- PtrToIntInst
// |
// +- CallBase -----------+- CallBrInst
// | |
@@ -1411,6 +1413,43 @@ class BitCastInst : public CastInst {
#endif
};
+class AddrSpaceCastInst : public CastInst {
+public:
+ static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &Name = "");
+ static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
+ Context &Ctx, const Twine &Name = "");
+ static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
+ Context &Ctx, const Twine &Name = "");
+
+ static bool classof(const Value *From) {
+ if (auto *I = dyn_cast<Instruction>(From))
+ return I->getOpcode() == Opcode::AddrSpaceCast;
+ return false;
+ }
+ /// \Returns the pointer operand.
+ Value *getPointerOperand() { return getOperand(0); }
+ /// \Returns the pointer operand.
+ const Value *getPointerOperand() const {
+ return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
+ }
+ /// \Returns the operand index of the pointer operand.
+ static unsigned getPointerOperandIndex() { return 0u; }
+ /// \Returns the address space of the pointer operand.
+ unsigned getSrcAddressSpace() const {
+ return getPointerOperand()->getType()->getPointerAddressSpace();
+ }
+ /// \Returns the address space of the result.
+ unsigned getDestAddressSpace() const {
+ return getType()->getPointerAddressSpace();
+ }
+#ifndef NDEBUG
+ 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 {
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 010cc115a5cc3..56ffd22ee3bcd 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -1200,6 +1200,39 @@ void BitCastInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}
+#endif // NDEBUG
+
+Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &Name) {
+ return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
+ WhereIt, WhereBB, Ctx, Name);
+}
+
+Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy,
+ Instruction *InsertBefore, Context &Ctx,
+ const Twine &Name) {
+ return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
+ InsertBefore, Ctx, Name);
+}
+
+Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy,
+ BasicBlock *InsertAtEnd, Context &Ctx,
+ const Twine &Name) {
+ return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
+ InsertAtEnd, Ctx, Name);
+}
+
+#ifndef NDEBUG
+void AddrSpaceCastInst::dump(raw_ostream &OS) const {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+}
+
+void AddrSpaceCastInst::dump() const {
+ dump(dbgs());
+ dbgs() << "\n";
+}
void OpaqueInst::dump(raw_ostream &OS) const {
dumpCommonPrefix(OS);
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 28c3aebeb1862..72af73d9c2f10 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -1551,6 +1551,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
EXPECT_EQ(BitCast->getDestTy(), Tfloat);
auto *AddrSpaceCast = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::AddrSpaceCastInst>(AddrSpaceCast));
EXPECT_EQ(AddrSpaceCast->getOpcode(),
sandboxir::Instruction::Opcode::AddrSpaceCast);
EXPECT_EQ(AddrSpaceCast->getSrcTy(), Tptr);
@@ -1752,3 +1753,82 @@ define void @foo(i32 %arg) {
EXPECT_EQ(NewI->getParent(), BB);
}
}
+
+TEST_F(SandboxIRTest, AddrSpaceCastInst) {
+ parseIR(C, R"IR(
+define void @foo(ptr %ptr) {
+ %addrspacecast = addrspacecast ptr %ptr to ptr addrspace(1)
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(&LLVMF);
+ unsigned ArgIdx = 0;
+ auto *Arg = F->getArg(ArgIdx++);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ Type *Tptr = Type::getInt32Ty(C)->getPointerTo();
+ Type *Tptr1 = Tptr->getPointerTo(1);
+
+ auto *AddrSpaceCast = cast<sandboxir::AddrSpaceCastInst>(&*It++);
+ EXPECT_EQ(AddrSpaceCast->getOpcode(),
+ sandboxir::Instruction::Opcode::AddrSpaceCast);
+ EXPECT_EQ(AddrSpaceCast->getSrcTy(), Tptr);
+ EXPECT_EQ(AddrSpaceCast->getDestTy(), Tptr1);
+ EXPECT_EQ(AddrSpaceCast->getPointerOperand(), Arg);
+ EXPECT_EQ(sandboxir::AddrSpaceCastInst::getPointerOperandIndex(), 0u);
+ EXPECT_EQ(AddrSpaceCast->getSrcAddressSpace(),
+ cast<PointerType>(Tptr)->getPointerAddressSpace());
+ EXPECT_EQ(AddrSpaceCast->getDestAddressSpace(),
+ cast<PointerType>(Tptr1)->getPointerAddressSpace());
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ {
+ // Check create() WhereIt, WhereBB
+ auto *NewI =
+ cast<sandboxir::AddrSpaceCastInst>(sandboxir::AddrSpaceCastInst::create(
+ Arg, Tptr1, /*WhereIt=*/BB->end(),
+ /*WhereBB=*/BB, Ctx, "AddrSpaceCast"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AddrSpaceCast);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr1);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), nullptr);
+ EXPECT_EQ(NewI->getPrevNode(), Ret);
+ }
+ {
+ // Check create() InsertBefore.
+ auto *NewI =
+ cast<sandboxir::AddrSpaceCastInst>(sandboxir::AddrSpaceCastInst::create(
+ Arg, Tptr1,
+ /*InsertBefore=*/Ret, Ctx, "AddrSpaceCast"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AddrSpaceCast);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr1);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), Ret);
+ }
+ {
+ // Check create() InsertAtEnd.
+ auto *NewI =
+ cast<sandboxir::AddrSpaceCastInst>(sandboxir::AddrSpaceCastInst::create(
+ Arg, Tptr1,
+ /*InsertAtEnd=*/BB, Ctx, "AddrSpaceCast"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::AddrSpaceCast);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr1);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), nullptr);
+ EXPECT_EQ(NewI->getParent(), BB);
+ }
+}
More information about the llvm-commits
mailing list