Skip to content

Commit

Permalink
[SandboxIR] Implement PtrToIntInst (llvm#101211)
Browse files Browse the repository at this point in the history
This patch implements sandboxir::PtrToIntInst mirroring
llvm::PtrToIntInst.
  • Loading branch information
vporpo authored Jul 30, 2024
1 parent 8a4b095 commit 3cc288a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
23 changes: 22 additions & 1 deletion llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// |
// +- BranchInst
// |
// +- CastInst
// +- CastInst ------------- PtrToIntInst
// |
// +- CallBase -----------+- CallBrInst
// | |
Expand Down Expand Up @@ -95,6 +95,7 @@ class InvokeInst;
class CallBrInst;
class GetElementPtrInst;
class CastInst;
class PtrToIntInst;

/// Iterator for the `Use` edges of a User's operands.
/// \Returns the operand `Use` when dereferenced.
Expand Down Expand Up @@ -1331,6 +1332,7 @@ class CastInst : public Instruction {
CastInst(llvm::CastInst *CI, Context &Ctx)
: Instruction(ClassID::Cast, getCastOpcode(CI->getOpcode()), CI, Ctx) {}
friend Context; // for SBCastInstruction()
friend class PtrToInt; // For constructor.
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
return getOperandUseDefault(OpIdx, Verify);
}
Expand Down Expand Up @@ -1365,6 +1367,25 @@ class CastInst : public Instruction {
#endif
};

class PtrToIntInst final : 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) {
return isa<Instruction>(From) &&
cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt;
}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final;
LLVM_DUMP_METHOD void dump() const final;
#endif // NDEBUG
};

/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
/// an OpaqueInstr.
class OpaqueInst : public sandboxir::Instruction {
Expand Down
28 changes: 28 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,34 @@ void CastInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG

Value *PtrToIntInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
const Twine &Name) {
return CastInst::create(DestTy, Instruction::Opcode::PtrToInt, Src, WhereIt,
WhereBB, Ctx, Name);
}
Value *PtrToIntInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
Context &Ctx, const Twine &Name) {
return create(Src, DestTy, InsertBefore->getIterator(),
InsertBefore->getParent(), Ctx, Name);
}
Value *PtrToIntInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
Context &Ctx, const Twine &Name) {
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
}

#ifndef NDEBUG
void PtrToIntInst::dump(raw_ostream &OS) const {
dumpCommonPrefix(OS);
dumpCommonSuffix(OS);
}

void PtrToIntInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}

void OpaqueInst::dump(raw_ostream &OS) const {
dumpCommonPrefix(OS);
Expand Down
70 changes: 70 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
EXPECT_EQ(FPExt->getDestTy(), Tdouble);

auto *PtrToInt = cast<sandboxir::CastInst>(&*It++);
EXPECT_TRUE(isa<sandboxir::PtrToIntInst>(PtrToInt));
EXPECT_EQ(PtrToInt->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
EXPECT_EQ(PtrToInt->getSrcTy(), Tptr);
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
Expand Down Expand Up @@ -1612,3 +1613,72 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
#endif // NDEBUG
}
}

TEST_F(SandboxIRTest, PtrToIntInst) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
%ptrtoint = ptrtoint ptr %ptr to i32
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 *Ti32 = Type::getInt32Ty(C);
Type *Tptr = Ti32->getPointerTo();

auto *PtrToInt = cast<sandboxir::PtrToIntInst>(&*It++);
EXPECT_EQ(PtrToInt->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
EXPECT_EQ(PtrToInt->getSrcTy(), Tptr);
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

{
// Check create() WhereIt, WhereBB
auto *NewI = cast<sandboxir::PtrToIntInst>(
sandboxir::PtrToIntInst::create(Arg, Ti32, /*WhereIt=*/BB->end(),
/*WhereBB=*/BB, Ctx, "PtrToInt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
// Check getSrcTy().
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
// Check getDestTy().
EXPECT_EQ(NewI->getDestTy(), Ti32);
// Check instr position.
EXPECT_EQ(NewI->getNextNode(), nullptr);
EXPECT_EQ(NewI->getPrevNode(), Ret);
}
{
// Check create() InsertBefore.
auto *NewI = cast<sandboxir::PtrToIntInst>(
sandboxir::PtrToIntInst::create(Arg, Ti32,
/*InsertBefore=*/Ret, Ctx, "PtrToInt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
// Check getSrcTy().
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
// Check getDestTy().
EXPECT_EQ(NewI->getDestTy(), Ti32);
// Check instr position.
EXPECT_EQ(NewI->getNextNode(), Ret);
}
{
// Check create() InsertAtEnd.
auto *NewI = cast<sandboxir::PtrToIntInst>(
sandboxir::PtrToIntInst::create(Arg, Ti32,
/*InsertAtEnd=*/BB, Ctx, "PtrToInt"));
// Check getOpcode().
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
// Check getSrcTy().
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
// Check getDestTy().
EXPECT_EQ(NewI->getDestTy(), Ti32);
// Check instr position.
EXPECT_EQ(NewI->getNextNode(), nullptr);
EXPECT_EQ(NewI->getParent(), BB);
}
}

0 comments on commit 3cc288a

Please sign in to comment.