Skip to content

Commit 6d3317e

Browse files
authored
[SandboxIR] Implement SIToFPInst (#101374)
This patch implements sandboxir::SIToFPInst which mirrors llvm::SIToFPInst.
1 parent 0a01e8f commit 6d3317e

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
// | +- IntToPtrInst
4040
// | |
4141
// | +- PtrToIntInst
42+
// | |
43+
// | +- SIToFPInst
4244
// |
4345
// +- CallBase -----------+- CallBrInst
4446
// | |
@@ -1378,6 +1380,27 @@ class CastInst : public Instruction {
13781380
#endif
13791381
};
13801382

1383+
class SIToFPInst final : public CastInst {
1384+
public:
1385+
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1386+
BasicBlock *WhereBB, Context &Ctx,
1387+
const Twine &Name = "");
1388+
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1389+
Context &Ctx, const Twine &Name = "");
1390+
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1391+
Context &Ctx, const Twine &Name = "");
1392+
1393+
static bool classof(const Value *From) {
1394+
if (auto *I = dyn_cast<Instruction>(From))
1395+
return I->getOpcode() == Opcode::SIToFP;
1396+
return false;
1397+
}
1398+
#ifndef NDEBUG
1399+
void dump(raw_ostream &OS) const final;
1400+
LLVM_DUMP_METHOD void dump() const final;
1401+
#endif // NDEBUG
1402+
};
1403+
13811404
class FPToUIInst final : public CastInst {
13821405
public:
13831406
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,34 @@ void CastInst::dump() const {
11431143
}
11441144
#endif // NDEBUG
11451145

1146+
Value *SIToFPInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
1147+
BasicBlock *WhereBB, Context &Ctx,
1148+
const Twine &Name) {
1149+
return CastInst::create(DestTy, Instruction::Opcode::SIToFP, Src, WhereIt,
1150+
WhereBB, Ctx, Name);
1151+
}
1152+
Value *SIToFPInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1153+
Context &Ctx, const Twine &Name) {
1154+
return create(Src, DestTy, InsertBefore->getIterator(),
1155+
InsertBefore->getParent(), Ctx, Name);
1156+
}
1157+
Value *SIToFPInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1158+
Context &Ctx, const Twine &Name) {
1159+
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
1160+
}
1161+
1162+
#ifndef NDEBUG
1163+
void SIToFPInst::dump(raw_ostream &OS) const {
1164+
dumpCommonPrefix(OS);
1165+
dumpCommonSuffix(OS);
1166+
}
1167+
1168+
void SIToFPInst::dump() const {
1169+
dump(dbgs());
1170+
dbgs() << "\n";
1171+
}
1172+
#endif // NDEBUG
1173+
11461174
Value *FPToUIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
11471175
BasicBlock *WhereBB, Context &Ctx,
11481176
const Twine &Name) {

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
15281528
EXPECT_EQ(IntToPtr->getDestTy(), Tptr);
15291529

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

1623+
TEST_F(SandboxIRTest, SIToFPInst) {
1624+
parseIR(C, R"IR(
1625+
define void @foo(i32 %arg) {
1626+
%sitofp = sitofp i32 %arg to float
1627+
ret void
1628+
}
1629+
)IR");
1630+
Function &LLVMF = *M->getFunction("foo");
1631+
sandboxir::Context Ctx(C);
1632+
sandboxir::Function *F = Ctx.createFunction(&LLVMF);
1633+
unsigned ArgIdx = 0;
1634+
auto *Arg = F->getArg(ArgIdx++);
1635+
auto *BB = &*F->begin();
1636+
auto It = BB->begin();
1637+
Type *Ti32 = Type::getInt32Ty(C);
1638+
Type *Tfloat = Type::getFloatTy(C);
1639+
1640+
auto *SIToFP = cast<sandboxir::SIToFPInst>(&*It++);
1641+
EXPECT_EQ(SIToFP->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
1642+
EXPECT_EQ(SIToFP->getSrcTy(), Ti32);
1643+
EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
1644+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
1645+
1646+
{
1647+
// Check create() WhereIt, WhereBB
1648+
auto *NewI = cast<sandboxir::SIToFPInst>(
1649+
sandboxir::SIToFPInst::create(Arg, Tfloat, /*WhereIt=*/BB->end(),
1650+
/*WhereBB=*/BB, Ctx, "SIToFP"));
1651+
// Check getOpcode().
1652+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
1653+
// Check getSrcTy().
1654+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1655+
// Check getDestTy().
1656+
EXPECT_EQ(NewI->getDestTy(), Tfloat);
1657+
// Check instr position.
1658+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1659+
EXPECT_EQ(NewI->getPrevNode(), Ret);
1660+
}
1661+
{
1662+
// Check create() InsertBefore.
1663+
auto *NewI = cast<sandboxir::SIToFPInst>(
1664+
sandboxir::SIToFPInst::create(Arg, Tfloat,
1665+
/*InsertBefore=*/Ret, Ctx, "SIToFP"));
1666+
// Check getOpcode().
1667+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
1668+
// Check getSrcTy().
1669+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1670+
// Check getDestTy().
1671+
EXPECT_EQ(NewI->getDestTy(), Tfloat);
1672+
// Check instr position.
1673+
EXPECT_EQ(NewI->getNextNode(), Ret);
1674+
}
1675+
{
1676+
// Check create() InsertAtEnd.
1677+
auto *NewI = cast<sandboxir::SIToFPInst>(
1678+
sandboxir::SIToFPInst::create(Arg, Tfloat,
1679+
/*InsertAtEnd=*/BB, Ctx, "SIToFP"));
1680+
// Check getOpcode().
1681+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
1682+
// Check getSrcTy().
1683+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1684+
// Check getDestTy().
1685+
EXPECT_EQ(NewI->getDestTy(), Tfloat);
1686+
// Check instr position.
1687+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1688+
EXPECT_EQ(NewI->getParent(), BB);
1689+
}
1690+
}
1691+
16221692
TEST_F(SandboxIRTest, FPToUIInst) {
16231693
parseIR(C, R"IR(
16241694
define void @foo(float %arg) {

0 commit comments

Comments
 (0)