Skip to content

[SandboxIR] Implement the remaining CastInst sub-classes #101537

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
Aug 1, 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
18 changes: 18 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@
// | |
// | +- BitCastInst
// | |
// | +- FPExtInst
// | |
// | +- FPToSIInst
// | |
// | +- FPToUIInst
// | |
// | +- FPTruncInst
// | |
// | +- IntToPtrInst
// | |
// | +- PtrToIntInst
// | |
// | +- SExtInst
// | |
// | +- SIToFPInst
// | |
// | +- TruncInst
// | |
// | +- UIToFPInst
// | |
// | +- ZExtInst
// |
// +- CallBase -----------+- CallBrInst
// | |
Expand Down Expand Up @@ -1458,6 +1470,12 @@ template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
}
};

class TruncInst final : public CastInstImpl<Instruction::Opcode::Trunc> {};
class ZExtInst final : public CastInstImpl<Instruction::Opcode::ZExt> {};
class SExtInst final : public CastInstImpl<Instruction::Opcode::SExt> {};
class FPTruncInst final : public CastInstImpl<Instruction::Opcode::FPTrunc> {};
class FPExtInst final : public CastInstImpl<Instruction::Opcode::FPExt> {};
class UIToFPInst final : public CastInstImpl<Instruction::Opcode::UIToFP> {};
class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};
Expand Down
78 changes: 78 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,11 +1489,13 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {

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

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

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

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

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

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

TEST_F(SandboxIRTest, TruncInst) {
parseIR(C, R"IR(
define void @foo(i64 %arg) {
%trunc = trunc i64 %arg to i32
ret void
}
)IR");
testCastInst<sandboxir::TruncInst, sandboxir::Instruction::Opcode::Trunc>(
*M,
/*SrcTy=*/Type::getInt64Ty(C), /*DstTy=*/Type::getInt32Ty(C));
}

TEST_F(SandboxIRTest, ZExtInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {
%zext = zext i32 %arg to i64
ret void
}
)IR");
testCastInst<sandboxir::ZExtInst, sandboxir::Instruction::Opcode::ZExt>(
*M,
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
}

TEST_F(SandboxIRTest, SExtInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {
%sext = sext i32 %arg to i64
ret void
}
)IR");
testCastInst<sandboxir::SExtInst, sandboxir::Instruction::Opcode::SExt>(
*M,
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
}

TEST_F(SandboxIRTest, FPTruncInst) {
parseIR(C, R"IR(
define void @foo(double %arg) {
%fptrunc = fptrunc double %arg to float
ret void
}
)IR");
testCastInst<sandboxir::FPTruncInst, sandboxir::Instruction::Opcode::FPTrunc>(
*M,
/*SrcTy=*/Type::getDoubleTy(C), /*DstTy=*/Type::getFloatTy(C));
}

TEST_F(SandboxIRTest, FPExtInst) {
parseIR(C, R"IR(
define void @foo(float %arg) {
%fpext = fpext float %arg to double
ret void
}
)IR");
testCastInst<sandboxir::FPExtInst, sandboxir::Instruction::Opcode::FPExt>(
*M,
/*SrcTy=*/Type::getFloatTy(C), /*DstTy=*/Type::getDoubleTy(C));
}

TEST_F(SandboxIRTest, UIToFPInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {
%uitofp = uitofp i32 %arg to float
ret void
}
)IR");
testCastInst<sandboxir::UIToFPInst, sandboxir::Instruction::Opcode::UIToFP>(
*M,
/*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getFloatTy(C));
}

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