Skip to content

[SandboxIR] Implement SIToFPInst #101374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
// | +- IntToPtrInst
// | |
// | +- PtrToIntInst
// | |
// | +- SIToFPInst
// |
// +- CallBase -----------+- CallBrInst
// | |
Expand Down Expand Up @@ -1378,6 +1380,27 @@ class CastInst : public Instruction {
#endif
};

class SIToFPInst final : public CastInst {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it's possible to do some CRTP magic to stamp out all of these instantiations much more succinctly. Let me mess around locally.

Copy link
Contributor

Choose a reason for hiding this comment

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

see 1cc8adb

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that works. Do you want to create a PR for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could also try doing something similar for the tests, since there is a lot of repetition there too. The only downside is that if there is a failure the error reported won't be as easy to follow, but I doubt these tests will fail.

If we go this way, then the rest of the CastInst sub-classes can be a single PR which would be really nice.

Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

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::SIToFP;
return false;
}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final;
LLVM_DUMP_METHOD void dump() const final;
#endif // NDEBUG
};

class FPToUIInst final : public CastInst {
public:
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
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 @@ -1143,6 +1143,34 @@ void CastInst::dump() const {
}
#endif // NDEBUG

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

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

void SIToFPInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG

Value *FPToUIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
const Twine &Name) {
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 @@ -1528,6 +1528,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
EXPECT_EQ(IntToPtr->getDestTy(), Tptr);

auto *SIToFP = cast<sandboxir::CastInst>(&*It++);
EXPECT_TRUE(isa<sandboxir::SIToFPInst>(SIToFP));
EXPECT_EQ(SIToFP->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
EXPECT_EQ(SIToFP->getSrcTy(), Ti32);
EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
Expand Down Expand Up @@ -1619,6 +1620,75 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
}
}

TEST_F(SandboxIRTest, SIToFPInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {
%sitofp = sitofp i32 %arg to float
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 *Tfloat = Type::getFloatTy(C);

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

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

TEST_F(SandboxIRTest, FPToUIInst) {
parseIR(C, R"IR(
define void @foo(float %arg) {
Expand Down
Loading