Skip to content

Commit b6b0a24

Browse files
authored
[SandboxIR] Implement the remaining CastInst sub-classes (#101537)
This patch implements: sandboxir::UIToFPInst sandboxir::FPExtInst sandboxir::FPTruncInst sandboxir::SExtInst sandboxir::ZExtInst sandboxir::TruncInst
1 parent 8557035 commit b6b0a24

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@
3232
// | |
3333
// | +- BitCastInst
3434
// | |
35+
// | +- FPExtInst
36+
// | |
3537
// | +- FPToSIInst
3638
// | |
3739
// | +- FPToUIInst
3840
// | |
41+
// | +- FPTruncInst
42+
// | |
3943
// | +- IntToPtrInst
4044
// | |
4145
// | +- PtrToIntInst
4246
// | |
47+
// | +- SExtInst
48+
// | |
4349
// | +- SIToFPInst
50+
// | |
51+
// | +- TruncInst
52+
// | |
53+
// | +- UIToFPInst
54+
// | |
55+
// | +- ZExtInst
4456
// |
4557
// +- CallBase -----------+- CallBrInst
4658
// | |
@@ -1458,6 +1470,12 @@ template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
14581470
}
14591471
};
14601472

1473+
class TruncInst final : public CastInstImpl<Instruction::Opcode::Trunc> {};
1474+
class ZExtInst final : public CastInstImpl<Instruction::Opcode::ZExt> {};
1475+
class SExtInst final : public CastInstImpl<Instruction::Opcode::SExt> {};
1476+
class FPTruncInst final : public CastInstImpl<Instruction::Opcode::FPTrunc> {};
1477+
class FPExtInst final : public CastInstImpl<Instruction::Opcode::FPExt> {};
1478+
class UIToFPInst final : public CastInstImpl<Instruction::Opcode::UIToFP> {};
14611479
class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
14621480
class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
14631481
class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,11 +1489,13 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
14891489

14901490
// Check classof(), getOpcode(), getSrcTy(), getDstTy()
14911491
auto *ZExt = cast<sandboxir::CastInst>(&*It++);
1492+
EXPECT_TRUE(isa<sandboxir::ZExtInst>(ZExt));
14921493
EXPECT_EQ(ZExt->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
14931494
EXPECT_EQ(ZExt->getSrcTy(), Ti32);
14941495
EXPECT_EQ(ZExt->getDestTy(), Ti64);
14951496

14961497
auto *SExt = cast<sandboxir::CastInst>(&*It++);
1498+
EXPECT_TRUE(isa<sandboxir::SExtInst>(SExt));
14971499
EXPECT_EQ(SExt->getOpcode(), sandboxir::Instruction::Opcode::SExt);
14981500
EXPECT_EQ(SExt->getSrcTy(), Ti32);
14991501
EXPECT_EQ(SExt->getDestTy(), Ti64);
@@ -1511,6 +1513,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
15111513
EXPECT_EQ(FPToSI->getDestTy(), Ti32);
15121514

15131515
auto *FPExt = cast<sandboxir::CastInst>(&*It++);
1516+
EXPECT_TRUE(isa<sandboxir::FPExtInst>(FPExt));
15141517
EXPECT_EQ(FPExt->getOpcode(), sandboxir::Instruction::Opcode::FPExt);
15151518
EXPECT_EQ(FPExt->getSrcTy(), Tfloat);
15161519
EXPECT_EQ(FPExt->getDestTy(), Tdouble);
@@ -1534,16 +1537,19 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
15341537
EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
15351538

15361539
auto *UIToFP = cast<sandboxir::CastInst>(&*It++);
1540+
EXPECT_TRUE(isa<sandboxir::UIToFPInst>(UIToFP));
15371541
EXPECT_EQ(UIToFP->getOpcode(), sandboxir::Instruction::Opcode::UIToFP);
15381542
EXPECT_EQ(UIToFP->getSrcTy(), Ti32);
15391543
EXPECT_EQ(UIToFP->getDestTy(), Tfloat);
15401544

15411545
auto *Trunc = cast<sandboxir::CastInst>(&*It++);
1546+
EXPECT_TRUE(isa<sandboxir::TruncInst>(Trunc));
15421547
EXPECT_EQ(Trunc->getOpcode(), sandboxir::Instruction::Opcode::Trunc);
15431548
EXPECT_EQ(Trunc->getSrcTy(), Ti32);
15441549
EXPECT_EQ(Trunc->getDestTy(), Ti16);
15451550

15461551
auto *FPTrunc = cast<sandboxir::CastInst>(&*It++);
1552+
EXPECT_TRUE(isa<sandboxir::FPTruncInst>(FPTrunc));
15471553
EXPECT_EQ(FPTrunc->getOpcode(), sandboxir::Instruction::Opcode::FPTrunc);
15481554
EXPECT_EQ(FPTrunc->getSrcTy(), Tdouble);
15491555
EXPECT_EQ(FPTrunc->getDestTy(), Tfloat);
@@ -1686,6 +1692,78 @@ void testCastInst(llvm::Module &M, Type *SrcTy, Type *DstTy) {
16861692
}
16871693
}
16881694

1695+
TEST_F(SandboxIRTest, TruncInst) {
1696+
parseIR(C, R"IR(
1697+
define void @foo(i64 %arg) {
1698+
%trunc = trunc i64 %arg to i32
1699+
ret void
1700+
}
1701+
)IR");
1702+
testCastInst<sandboxir::TruncInst, sandboxir::Instruction::Opcode::Trunc>(
1703+
*M,
1704+
/*SrcTy=*/Type::getInt64Ty(C), /*DstTy=*/Type::getInt32Ty(C));
1705+
}
1706+
1707+
TEST_F(SandboxIRTest, ZExtInst) {
1708+
parseIR(C, R"IR(
1709+
define void @foo(i32 %arg) {
1710+
%zext = zext i32 %arg to i64
1711+
ret void
1712+
}
1713+
)IR");
1714+
testCastInst<sandboxir::ZExtInst, sandboxir::Instruction::Opcode::ZExt>(
1715+
*M,
1716+
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
1717+
}
1718+
1719+
TEST_F(SandboxIRTest, SExtInst) {
1720+
parseIR(C, R"IR(
1721+
define void @foo(i32 %arg) {
1722+
%sext = sext i32 %arg to i64
1723+
ret void
1724+
}
1725+
)IR");
1726+
testCastInst<sandboxir::SExtInst, sandboxir::Instruction::Opcode::SExt>(
1727+
*M,
1728+
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
1729+
}
1730+
1731+
TEST_F(SandboxIRTest, FPTruncInst) {
1732+
parseIR(C, R"IR(
1733+
define void @foo(double %arg) {
1734+
%fptrunc = fptrunc double %arg to float
1735+
ret void
1736+
}
1737+
)IR");
1738+
testCastInst<sandboxir::FPTruncInst, sandboxir::Instruction::Opcode::FPTrunc>(
1739+
*M,
1740+
/*SrcTy=*/Type::getDoubleTy(C), /*DstTy=*/Type::getFloatTy(C));
1741+
}
1742+
1743+
TEST_F(SandboxIRTest, FPExtInst) {
1744+
parseIR(C, R"IR(
1745+
define void @foo(float %arg) {
1746+
%fpext = fpext float %arg to double
1747+
ret void
1748+
}
1749+
)IR");
1750+
testCastInst<sandboxir::FPExtInst, sandboxir::Instruction::Opcode::FPExt>(
1751+
*M,
1752+
/*SrcTy=*/Type::getFloatTy(C), /*DstTy=*/Type::getDoubleTy(C));
1753+
}
1754+
1755+
TEST_F(SandboxIRTest, UIToFPInst) {
1756+
parseIR(C, R"IR(
1757+
define void @foo(i32 %arg) {
1758+
%uitofp = uitofp i32 %arg to float
1759+
ret void
1760+
}
1761+
)IR");
1762+
testCastInst<sandboxir::UIToFPInst, sandboxir::Instruction::Opcode::UIToFP>(
1763+
*M,
1764+
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getFloatTy(C));
1765+
}
1766+
16891767
TEST_F(SandboxIRTest, SIToFPInst) {
16901768
parseIR(C, R"IR(
16911769
define void @foo(i32 %arg) {

0 commit comments

Comments
 (0)