[llvm] [SandboxIR] Implement UnaryInstruction class (PR #101541)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 16:17:56 PDT 2024
https://github.com/vporpo updated https://github.com/llvm/llvm-project/pull/101541
>From b23e0a5408fcf91f8053e1ee670b4067555e88b7 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Tue, 30 Jul 2024 10:22:51 -0700
Subject: [PATCH] [SandboxIR] Implement UnaryInstruction class
This patch implements sandboxir::UnaryInstruction class and updates
sandboxir::LoadInst and sandboxir::CastInst to inherit from it instead of
sandboxir::Instruction.
---
llvm/include/llvm/SandboxIR/SandboxIR.h | 34 +++++++++++---
llvm/unittests/SandboxIR/SandboxIRTest.cpp | 53 ++++++++++++++++------
2 files changed, 67 insertions(+), 20 deletions(-)
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 0bd6cdac36f9a..313b541ddcaca 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -64,8 +64,6 @@
// |
// +- InsertElementInst
// |
-// +- LoadInst
-// |
// +- OpaqueInst
// |
// +- PHINode
@@ -78,6 +76,10 @@
// |
// +- StoreInst
// |
+// +- UnaryInstruction -+- LoadInst
+// | |
+// | +- CastInst
+// |
// +- UnaryOperator
//
// Use
@@ -108,6 +110,7 @@ class Function;
class Instruction;
class SelectInst;
class BranchInst;
+class UnaryInstruction;
class LoadInst;
class ReturnInst;
class StoreInst;
@@ -836,10 +839,26 @@ class BranchInst : public Instruction {
#endif
};
-class LoadInst final : public Instruction {
+/// An abstract class, parent of unary instructions.
+class UnaryInstruction : public Instruction {
+protected:
+ UnaryInstruction(ClassID ID, Opcode Opc, llvm::Instruction *LLVMI,
+ Context &Ctx)
+ : Instruction(ID, Opc, LLVMI, Ctx) {}
+
+public:
+ static bool classof(const Instruction *I) {
+ return isa<LoadInst>(I) || isa<CastInst>(I);
+ }
+ static bool classof(const Value *V) {
+ return isa<Instruction>(V) && classof(cast<Instruction>(V));
+ }
+};
+
+class LoadInst final : public UnaryInstruction {
/// Use LoadInst::create() instead of calling the constructor.
LoadInst(llvm::LoadInst *LI, Context &Ctx)
- : Instruction(ClassID::Load, Opcode::Load, LI, Ctx) {}
+ : UnaryInstruction(ClassID::Load, Opcode::Load, LI, Ctx) {}
friend Context; // for LoadInst()
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
return getOperandUseDefault(OpIdx, Verify);
@@ -1371,7 +1390,7 @@ class GetElementPtrInst final : public Instruction {
#endif
};
-class CastInst : public Instruction {
+class CastInst : public UnaryInstruction {
static Opcode getCastOpcode(llvm::Instruction::CastOps CastOp) {
switch (CastOp) {
case llvm::Instruction::ZExt:
@@ -1408,8 +1427,9 @@ class CastInst : public Instruction {
/// Use Context::createCastInst(). Don't call the
/// constructor directly.
CastInst(llvm::CastInst *CI, Context &Ctx)
- : Instruction(ClassID::Cast, getCastOpcode(CI->getOpcode()), CI, Ctx) {}
- friend Context; // for SBCastInstruction()
+ : UnaryInstruction(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);
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 5f858b47926ec..bc4e97c48bffe 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -751,6 +751,7 @@ define void @foo(ptr %arg0, ptr %arg1) {
auto *BB = &*F->begin();
auto It = BB->begin();
auto *Ld = cast<sandboxir::LoadInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(Ld));
auto *VLd = cast<sandboxir::LoadInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
@@ -1489,79 +1490,105 @@ 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));
+ auto *ZExtI = cast<sandboxir::ZExtInst>(ZExt);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(ZExtI));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(ZExtI));
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));
+ auto *SExtI = cast<sandboxir::SExtInst>(SExt);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(SExt));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(SExtI));
EXPECT_EQ(SExt->getOpcode(), sandboxir::Instruction::Opcode::SExt);
EXPECT_EQ(SExt->getSrcTy(), Ti32);
EXPECT_EQ(SExt->getDestTy(), Ti64);
auto *FPToUI = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::FPToUIInst>(FPToUI));
+ auto *FPToUII = cast<sandboxir::FPToUIInst>(FPToUI);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPToUI));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPToUII));
EXPECT_EQ(FPToUI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
EXPECT_EQ(FPToUI->getSrcTy(), Tfloat);
EXPECT_EQ(FPToUI->getDestTy(), Ti32);
auto *FPToSI = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::FPToSIInst>(FPToSI));
+ auto *FPToSII = cast<sandboxir::FPToSIInst>(FPToSI);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPToSI));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPToSII));
EXPECT_EQ(FPToSI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
EXPECT_EQ(FPToSI->getSrcTy(), Tfloat);
EXPECT_EQ(FPToSI->getDestTy(), Ti32);
auto *FPExt = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::FPExtInst>(FPExt));
+ auto *FPExtI = cast<sandboxir::FPExtInst>(FPExt);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPExt));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPExtI));
EXPECT_EQ(FPExt->getOpcode(), sandboxir::Instruction::Opcode::FPExt);
EXPECT_EQ(FPExt->getSrcTy(), Tfloat);
EXPECT_EQ(FPExt->getDestTy(), Tdouble);
auto *PtrToInt = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::PtrToIntInst>(PtrToInt));
+ auto *PtrToIntI = cast<sandboxir::PtrToIntInst>(PtrToInt);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(PtrToInt));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(PtrToIntI));
EXPECT_EQ(PtrToInt->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
EXPECT_EQ(PtrToInt->getSrcTy(), Tptr);
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
auto *IntToPtr = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::IntToPtrInst>(IntToPtr));
+ auto *IntToPtrI = cast<sandboxir::IntToPtrInst>(IntToPtr);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(IntToPtr));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(IntToPtrI));
EXPECT_EQ(IntToPtr->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
EXPECT_EQ(IntToPtr->getSrcTy(), Ti32);
EXPECT_EQ(IntToPtr->getDestTy(), Tptr);
auto *SIToFP = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::SIToFPInst>(SIToFP));
+ auto *SIToFPI = cast<sandboxir::SIToFPInst>(SIToFP);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(SIToFP));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(SIToFPI));
EXPECT_EQ(SIToFP->getOpcode(), sandboxir::Instruction::Opcode::SIToFP);
EXPECT_EQ(SIToFP->getSrcTy(), Ti32);
EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
auto *UIToFP = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::UIToFPInst>(UIToFP));
+ auto *UIToFPI = cast<sandboxir::UIToFPInst>(UIToFP);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(UIToFP));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(UIToFPI));
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));
+ auto *TruncI = cast<sandboxir::TruncInst>(Trunc);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(Trunc));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(TruncI));
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));
+ auto *FPTruncI = cast<sandboxir::FPTruncInst>(FPTrunc);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPTrunc));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(FPTruncI));
EXPECT_EQ(FPTrunc->getOpcode(), sandboxir::Instruction::Opcode::FPTrunc);
EXPECT_EQ(FPTrunc->getSrcTy(), Tdouble);
EXPECT_EQ(FPTrunc->getDestTy(), Tfloat);
auto *BitCast = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::BitCastInst>(BitCast));
+ auto *BitCastI = cast<sandboxir::BitCastInst>(BitCast);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(BitCast));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(BitCastI));
EXPECT_EQ(BitCast->getOpcode(), sandboxir::Instruction::Opcode::BitCast);
EXPECT_EQ(BitCast->getSrcTy(), Ti32);
EXPECT_EQ(BitCast->getDestTy(), Tfloat);
auto *AddrSpaceCast = cast<sandboxir::CastInst>(&*It++);
- EXPECT_TRUE(isa<sandboxir::AddrSpaceCastInst>(AddrSpaceCast));
+ auto *AddrSpaceCastI = cast<sandboxir::AddrSpaceCastInst>(AddrSpaceCast);
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(AddrSpaceCast));
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(AddrSpaceCastI));
EXPECT_EQ(AddrSpaceCast->getOpcode(),
sandboxir::Instruction::Opcode::AddrSpaceCast);
EXPECT_EQ(AddrSpaceCast->getSrcTy(), Tptr);
More information about the llvm-commits
mailing list