Skip to content

Commit f33d519

Browse files
authored
[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.
1 parent 4a0bbbc commit f33d519

File tree

4 files changed

+88
-24
lines changed

4 files changed

+88
-24
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ namespace llvm {
107107
namespace sandboxir {
108108

109109
class BasicBlock;
110+
class ConstantInt;
110111
class Context;
111112
class Function;
112113
class Instruction;
@@ -489,22 +490,22 @@ class Constant : public sandboxir::User {
489490
: sandboxir::User(ClassID::Constant, C, SBCtx) {}
490491
Constant(ClassID ID, llvm::Constant *C, sandboxir::Context &SBCtx)
491492
: sandboxir::User(ID, C, SBCtx) {}
492-
friend class Function; // For constructor
493-
friend class Context; // For constructor.
494-
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
493+
friend class ConstantInt; // For constructor.
494+
friend class Function; // For constructor
495+
friend class Context; // For constructor.
496+
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
495497
return getOperandUseDefault(OpIdx, Verify);
496498
}
497499

498500
public:
499-
static Constant *createInt(Type *Ty, uint64_t V, Context &Ctx,
500-
bool IsSigned = false);
501501
/// For isa/dyn_cast.
502502
static bool classof(const sandboxir::Value *From) {
503503
return From->getSubclassID() == ClassID::Constant ||
504+
From->getSubclassID() == ClassID::ConstantInt ||
504505
From->getSubclassID() == ClassID::Function;
505506
}
506507
sandboxir::Context &getParent() const { return getContext(); }
507-
unsigned getUseOperandNo(const Use &Use) const final {
508+
unsigned getUseOperandNo(const Use &Use) const override {
508509
return getUseOperandNoDefault(Use);
509510
}
510511
#ifndef NDEBUG
@@ -515,6 +516,41 @@ class Constant : public sandboxir::User {
515516
#endif
516517
};
517518

519+
class ConstantInt : public Constant {
520+
ConstantInt(llvm::ConstantInt *C, sandboxir::Context &Ctx)
521+
: Constant(ClassID::ConstantInt, C, Ctx) {}
522+
friend class Context; // For constructor.
523+
524+
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
525+
llvm_unreachable("ConstantInt has no operands!");
526+
}
527+
528+
public:
529+
/// If Ty is a vector type, return a Constant with a splat of the given
530+
/// value. Otherwise return a ConstantInt for the given value.
531+
static ConstantInt *get(Type *Ty, uint64_t V, Context &Ctx,
532+
bool IsSigned = false);
533+
534+
// TODO: Implement missing functions.
535+
536+
/// For isa/dyn_cast.
537+
static bool classof(const sandboxir::Value *From) {
538+
return From->getSubclassID() == ClassID::ConstantInt;
539+
}
540+
unsigned getUseOperandNo(const Use &Use) const override {
541+
llvm_unreachable("ConstantInt has no operands!");
542+
}
543+
#ifndef NDEBUG
544+
void verify() const override {
545+
assert(isa<llvm::ConstantInt>(Val) && "Expected a ConstantInst!");
546+
}
547+
void dumpOS(raw_ostream &OS) const override {
548+
dumpCommonPrefix(OS);
549+
dumpCommonSuffix(OS);
550+
}
551+
#endif
552+
};
553+
518554
/// Iterator for `Instruction`s in a `BasicBlock.
519555
/// \Returns an sandboxir::Instruction & when derereferenced.
520556
class BBIterator {
@@ -2045,7 +2081,7 @@ class Context {
20452081
Constant *getOrCreateConstant(llvm::Constant *LLVMC) {
20462082
return cast<Constant>(getOrCreateValueInternal(LLVMC, 0));
20472083
}
2048-
friend class Constant; // For getOrCreateConstant().
2084+
friend class ConstantInt; // For getOrCreateConstant().
20492085
/// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
20502086
/// also create all contents of the block.
20512087
BasicBlock *createBasicBlock(llvm::BasicBlock *BB);

llvm/include/llvm/SandboxIR/SandboxIRValues.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ DEF_VALUE(Argument, Argument)
1919
DEF_USER(User, User)
2020
DEF_VALUE(Block, BasicBlock)
2121
DEF_USER(Constant, Constant)
22+
DEF_USER(ConstantInt, ConstantInt)
2223

2324
#ifndef DEF_INSTR
2425
#define DEF_INSTR(ID, OPCODE, CLASS)

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,18 +1695,20 @@ Value *ExtractElementInst::create(Value *Vec, Value *Idx,
16951695
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
16961696
}
16971697

1698-
Constant *Constant::createInt(Type *Ty, uint64_t V, Context &Ctx,
1699-
bool IsSigned) {
1700-
llvm::Constant *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned);
1701-
return Ctx.getOrCreateConstant(LLVMC);
1702-
}
1703-
17041698
#ifndef NDEBUG
17051699
void Constant::dumpOS(raw_ostream &OS) const {
17061700
dumpCommonPrefix(OS);
17071701
dumpCommonSuffix(OS);
17081702
}
1703+
#endif // NDEBUG
17091704

1705+
ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, Context &Ctx,
1706+
bool IsSigned) {
1707+
auto *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned);
1708+
return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
1709+
}
1710+
1711+
#ifndef NDEBUG
17101712
void Function::dumpNameAndArgs(raw_ostream &OS) const {
17111713
auto *F = cast<llvm::Function>(Val);
17121714
OS << *F->getReturnType() << " @" << F->getName() << "(";
@@ -1788,6 +1790,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
17881790
return It->second.get();
17891791

17901792
if (auto *C = dyn_cast<llvm::Constant>(LLVMV)) {
1793+
if (auto *CI = dyn_cast<llvm::ConstantInt>(C)) {
1794+
It->second = std::unique_ptr<ConstantInt>(new ConstantInt(CI, *this));
1795+
return It->second.get();
1796+
}
17911797
if (auto *F = dyn_cast<llvm::Function>(LLVMV))
17921798
It->second = std::unique_ptr<Function>(new Function(F, *this));
17931799
else

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ define void @foo(i32 %v1) {
103103
#endif
104104
}
105105

106+
TEST_F(SandboxIRTest, ConstantInt) {
107+
parseIR(C, R"IR(
108+
define void @foo(i32 %v0) {
109+
%add0 = add i32 %v0, 42
110+
ret void
111+
}
112+
)IR");
113+
Function &LLVMF = *M->getFunction("foo");
114+
sandboxir::Context Ctx(C);
115+
116+
auto &F = *Ctx.createFunction(&LLVMF);
117+
auto &BB = *F.begin();
118+
auto It = BB.begin();
119+
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
120+
auto *FortyTwo = cast<sandboxir::ConstantInt>(Add0->getOperand(1));
121+
122+
// Check that creating an identical constant gives us the same object.
123+
auto *NewCI = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
124+
EXPECT_EQ(NewCI, FortyTwo);
125+
// Check new constant.
126+
auto *FortyThree = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 43, Ctx);
127+
EXPECT_NE(FortyThree, FortyTwo);
128+
}
129+
106130
TEST_F(SandboxIRTest, Use) {
107131
parseIR(C, R"IR(
108132
define i32 @foo(i32 %v0, i32 %v1) {
@@ -610,12 +634,11 @@ define void @foo(i1 %c0, i8 %v0, i8 %v1, i1 %c1) {
610634
}
611635
{
612636
// Check SelectInst::create() Folded.
613-
auto *False =
614-
sandboxir::Constant::createInt(llvm::Type::getInt1Ty(C), 0, Ctx,
615-
/*IsSigned=*/false);
637+
auto *False = sandboxir::ConstantInt::get(llvm::Type::getInt1Ty(C), 0, Ctx,
638+
/*IsSigned=*/false);
616639
auto *FortyTwo =
617-
sandboxir::Constant::createInt(llvm::Type::getInt1Ty(C), 42, Ctx,
618-
/*IsSigned=*/false);
640+
sandboxir::ConstantInt::get(llvm::Type::getInt1Ty(C), 42, Ctx,
641+
/*IsSigned=*/false);
619642
auto *NewSel =
620643
sandboxir::SelectInst::create(False, FortyTwo, FortyTwo, Ret, Ctx);
621644
EXPECT_TRUE(isa<sandboxir::Constant>(NewSel));
@@ -706,7 +729,7 @@ define void @foo(i8 %v0, i8 %v1, <2 x i8> %vec) {
706729

707730
auto *LLVMArg0 = LLVMF.getArg(0);
708731
auto *LLVMArgVec = LLVMF.getArg(2);
709-
auto *Zero = sandboxir::Constant::createInt(Type::getInt8Ty(C), 0, Ctx);
732+
auto *Zero = sandboxir::ConstantInt::get(Type::getInt8Ty(C), 0, Ctx);
710733
auto *LLVMZero = llvm::ConstantInt::get(Type::getInt8Ty(C), 0);
711734
EXPECT_EQ(
712735
sandboxir::InsertElementInst::isValidOperands(ArgVec, Arg0, Zero),
@@ -1866,8 +1889,7 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
18661889
}
18671890
{
18681891
// Check create() when it gets folded.
1869-
auto *FortyTwo =
1870-
sandboxir::Constant::createInt(Type::getInt32Ty(C), 42, Ctx);
1892+
auto *FortyTwo = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
18711893
auto *NewV = sandboxir::BinaryOperator::create(
18721894
sandboxir::Instruction::Opcode::Add, FortyTwo, FortyTwo,
18731895
/*InsertBefore=*/Ret, Ctx, "Folded");
@@ -1923,8 +1945,7 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
19231945
}
19241946
{
19251947
// Check createWithCopiedFlags() when it gets folded.
1926-
auto *FortyTwo =
1927-
sandboxir::Constant::createInt(Type::getInt32Ty(C), 42, Ctx);
1948+
auto *FortyTwo = sandboxir::ConstantInt::get(Type::getInt32Ty(C), 42, Ctx);
19281949
auto *NewV = sandboxir::BinaryOperator::createWithCopiedFlags(
19291950
sandboxir::Instruction::Opcode::Add, FortyTwo, FortyTwo, CopyFrom,
19301951
/*InsertBefore=*/Ret, Ctx, "Folded");
@@ -2378,7 +2399,7 @@ define void @foo() {
23782399
auto *Ty = Type::getInt32Ty(C);
23792400
unsigned AddrSpace = 42;
23802401
auto *PtrTy = PointerType::get(C, AddrSpace);
2381-
auto *ArraySize = sandboxir::Constant::createInt(Ty, 43, Ctx);
2402+
auto *ArraySize = sandboxir::ConstantInt::get(Ty, 43, Ctx);
23822403
{
23832404
// Check create() WhereIt, WhereBB.
23842405
auto *NewI = cast<sandboxir::AllocaInst>(sandboxir::AllocaInst::create(

0 commit comments

Comments
 (0)