[llvm] f33d519 - [SandboxIR] Implement ConstantInt (#104639)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 16 14:00:02 PDT 2024
Author: vporpo
Date: 2024-08-16T13:59:58-07:00
New Revision: f33d519cd471b1eec41c8b26f892ddb66bf8144f
URL: https://github.com/llvm/llvm-project/commit/f33d519cd471b1eec41c8b26f892ddb66bf8144f
DIFF: https://github.com/llvm/llvm-project/commit/f33d519cd471b1eec41c8b26f892ddb66bf8144f.diff
LOG: [SandboxIR] Implement ConstantInt (#104639)
This patch implements a very basic version of sandboxir::ConstantInt. It
is missing most of the factory functions present in llvm::ConstantInt,
but these will be added later.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/include/llvm/SandboxIR/SandboxIRValues.def
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 75c9d5017054b4..3c568f4956857f 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -107,6 +107,7 @@ namespace llvm {
namespace sandboxir {
class BasicBlock;
+class ConstantInt;
class Context;
class Function;
class Instruction;
@@ -489,22 +490,22 @@ class Constant : public sandboxir::User {
: sandboxir::User(ClassID::Constant, C, SBCtx) {}
Constant(ClassID ID, llvm::Constant *C, sandboxir::Context &SBCtx)
: sandboxir::User(ID, C, SBCtx) {}
- friend class Function; // For constructor
- friend class Context; // For constructor.
- Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
+ friend class ConstantInt; // For constructor.
+ friend class Function; // For constructor
+ friend class Context; // For constructor.
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
return getOperandUseDefault(OpIdx, Verify);
}
public:
- static Constant *createInt(Type *Ty, uint64_t V, Context &Ctx,
- bool IsSigned = false);
/// For isa/dyn_cast.
static bool classof(const sandboxir::Value *From) {
return From->getSubclassID() == ClassID::Constant ||
+ From->getSubclassID() == ClassID::ConstantInt ||
From->getSubclassID() == ClassID::Function;
}
sandboxir::Context &getParent() const { return getContext(); }
- unsigned getUseOperandNo(const Use &Use) const final {
+ unsigned getUseOperandNo(const Use &Use) const override {
return getUseOperandNoDefault(Use);
}
#ifndef NDEBUG
@@ -515,6 +516,41 @@ class Constant : public sandboxir::User {
#endif
};
+class ConstantInt : public Constant {
+ ConstantInt(llvm::ConstantInt *C, sandboxir::Context &Ctx)
+ : Constant(ClassID::ConstantInt, C, Ctx) {}
+ friend class Context; // For constructor.
+
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
+ llvm_unreachable("ConstantInt has no operands!");
+ }
+
+public:
+ /// If Ty is a vector type, return a Constant with a splat of the given
+ /// value. Otherwise return a ConstantInt for the given value.
+ static ConstantInt *get(Type *Ty, uint64_t V, Context &Ctx,
+ bool IsSigned = false);
+
+ // TODO: Implement missing functions.
+
+ /// For isa/dyn_cast.
+ static bool classof(const sandboxir::Value *From) {
+ return From->getSubclassID() == ClassID::ConstantInt;
+ }
+ unsigned getUseOperandNo(const Use &Use) const override {
+ llvm_unreachable("ConstantInt has no operands!");
+ }
+#ifndef NDEBUG
+ void verify() const override {
+ assert(isa<llvm::ConstantInt>(Val) && "Expected a ConstantInst!");
+ }
+ void dumpOS(raw_ostream &OS) const override {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+ }
+#endif
+};
+
/// Iterator for `Instruction`s in a `BasicBlock.
/// \Returns an sandboxir::Instruction & when derereferenced.
class BBIterator {
@@ -2045,7 +2081,7 @@ class Context {
Constant *getOrCreateConstant(llvm::Constant *LLVMC) {
return cast<Constant>(getOrCreateValueInternal(LLVMC, 0));
}
- friend class Constant; // For getOrCreateConstant().
+ friend class ConstantInt; // For getOrCreateConstant().
/// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
/// also create all contents of the block.
BasicBlock *createBasicBlock(llvm::BasicBlock *BB);
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index 81a916be21e4a3..cba5b69ebf1217 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -19,6 +19,7 @@ DEF_VALUE(Argument, Argument)
DEF_USER(User, User)
DEF_VALUE(Block, BasicBlock)
DEF_USER(Constant, Constant)
+DEF_USER(ConstantInt, ConstantInt)
#ifndef DEF_INSTR
#define DEF_INSTR(ID, OPCODE, CLASS)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 559d9ae7c00e7a..6e8c83528bded9 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -1695,18 +1695,20 @@ Value *ExtractElementInst::create(Value *Vec, Value *Idx,
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
-Constant *Constant::createInt(Type *Ty, uint64_t V, Context &Ctx,
- bool IsSigned) {
- llvm::Constant *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned);
- return Ctx.getOrCreateConstant(LLVMC);
-}
-
#ifndef NDEBUG
void Constant::dumpOS(raw_ostream &OS) const {
dumpCommonPrefix(OS);
dumpCommonSuffix(OS);
}
+#endif // NDEBUG
+ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, Context &Ctx,
+ bool IsSigned) {
+ auto *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned);
+ return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
+}
+
+#ifndef NDEBUG
void Function::dumpNameAndArgs(raw_ostream &OS) const {
auto *F = cast<llvm::Function>(Val);
OS << *F->getReturnType() << " @" << F->getName() << "(";
@@ -1788,6 +1790,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
return It->second.get();
if (auto *C = dyn_cast<llvm::Constant>(LLVMV)) {
+ if (auto *CI = dyn_cast<llvm::ConstantInt>(C)) {
+ It->second = std::unique_ptr<ConstantInt>(new ConstantInt(CI, *this));
+ return It->second.get();
+ }
if (auto *F = dyn_cast<llvm::Function>(LLVMV))
It->second = std::unique_ptr<Function>(new Function(F, *this));
else
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index e4563ab8f07ba6..6c0f1b19243c28 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -103,6 +103,30 @@ define void @foo(i32 %v1) {
#endif
}
+TEST_F(SandboxIRTest, ConstantInt) {
+ parseIR(C, R"IR(
+define void @foo(i32 %v0) {
+ %add0 = add i32 %v0, 42
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto &BB = *F.begin();
+ auto It = BB.begin();
+ auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
+ auto *FortyTwo = cast<sandboxir::ConstantInt>(Add0->getOperand(1));
+
+ // Check that creating an identical constant gives us the same object.
+ auto *NewCI = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
+ EXPECT_EQ(NewCI, FortyTwo);
+ // Check new constant.
+ auto *FortyThree = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 43, Ctx);
+ EXPECT_NE(FortyThree, FortyTwo);
+}
+
TEST_F(SandboxIRTest, Use) {
parseIR(C, R"IR(
define i32 @foo(i32 %v0, i32 %v1) {
@@ -610,12 +634,11 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
}
{
// Check SelectInst::create() Folded.
- auto *False =
- sandboxir::Constant::createInt(llvm::Type::getInt1Ty(C), 0, Ctx,
- /*IsSigned=*/false);
+ auto *False = sandboxir::ConstantInt::get(llvm::Type::getInt1Ty(C), 0, Ctx,
+ /*IsSigned=*/false);
auto *FortyTwo =
- sandboxir::Constant::createInt(llvm::Type::getInt1Ty(C), 42, Ctx,
- /*IsSigned=*/false);
+ sandboxir::ConstantInt::get(llvm::Type::getInt1Ty(C), 42, Ctx,
+ /*IsSigned=*/false);
auto *NewSel =
sandboxir::SelectInst::create(False, FortyTwo, FortyTwo, Ret, Ctx);
EXPECT_TRUE(isa<sandboxir::Constant>(NewSel));
@@ -706,7 +729,7 @@ define void @foo(i8 %v0, i8 %v1, <2 x i8> %vec) {
auto *LLVMArg0 = LLVMF.getArg(0);
auto *LLVMArgVec = LLVMF.getArg(2);
- auto *Zero = sandboxir::Constant::createInt(Type::getInt8Ty(C), 0, Ctx);
+ auto *Zero = sandboxir::ConstantInt::get(Type::getInt8Ty(C), 0, Ctx);
auto *LLVMZero = llvm::ConstantInt::get(Type::getInt8Ty(C), 0);
EXPECT_EQ(
sandboxir::InsertElementInst::isValidOperands(ArgVec, Arg0, Zero),
@@ -1866,8 +1889,7 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
}
{
// Check create() when it gets folded.
- auto *FortyTwo =
- sandboxir::Constant::createInt(Type::getInt32Ty(C), 42, Ctx);
+ auto *FortyTwo = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
auto *NewV = sandboxir::BinaryOperator::create(
sandboxir::Instruction::Opcode::Add, FortyTwo, FortyTwo,
/*InsertBefore=*/Ret, Ctx, "Folded");
@@ -1923,8 +1945,7 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
}
{
// Check createWithCopiedFlags() when it gets folded.
- auto *FortyTwo =
- sandboxir::Constant::createInt(Type::getInt32Ty(C), 42, Ctx);
+ auto *FortyTwo = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
auto *NewV = sandboxir::BinaryOperator::createWithCopiedFlags(
sandboxir::Instruction::Opcode::Add, FortyTwo, FortyTwo, CopyFrom,
/*InsertBefore=*/Ret, Ctx, "Folded");
@@ -2378,7 +2399,7 @@ define void @foo() {
auto *Ty = Type::getInt32Ty(C);
unsigned AddrSpace = 42;
auto *PtrTy = PointerType::get(C, AddrSpace);
- auto *ArraySize = sandboxir::Constant::createInt(Ty, 43, Ctx);
+ auto *ArraySize = sandboxir::ConstantInt::get(Ty, 43, Ctx);
{
// Check create() WhereIt, WhereBB.
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(
More information about the llvm-commits
mailing list