Skip to content

Commit 3b0a1ec

Browse files
authored
[SandboxIR] Implement PossiblyDisjointInst (#106148)
This patch implements sandboxir::PossiblyDisjointInst mirroring llvm::PossiblyDisjointInst.
1 parent 06edc1c commit 3b0a1ec

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class CatchSwitchInst;
144144
class SwitchInst;
145145
class UnaryOperator;
146146
class BinaryOperator;
147+
class PossiblyDisjointInst;
147148
class AtomicRMWInst;
148149
class AtomicCmpXchgInst;
149150

@@ -2374,6 +2375,7 @@ class UnaryOperator : public UnaryInstruction {
23742375
};
23752376

23762377
class BinaryOperator : public SingleLLVMInstructionImpl<llvm::BinaryOperator> {
2378+
protected:
23772379
static Opcode getBinOpOpcode(llvm::Instruction::BinaryOps BinOp) {
23782380
switch (BinOp) {
23792381
case llvm::Instruction::Add:
@@ -2453,6 +2455,22 @@ class BinaryOperator : public SingleLLVMInstructionImpl<llvm::BinaryOperator> {
24532455
void swapOperands() { swapOperandsInternal(0, 1); }
24542456
};
24552457

2458+
/// An or instruction, which can be marked as "disjoint", indicating that the
2459+
/// inputs don't have a 1 in the same bit position. Meaning this instruction
2460+
/// can also be treated as an add.
2461+
class PossiblyDisjointInst : public BinaryOperator {
2462+
public:
2463+
void setIsDisjoint(bool B);
2464+
bool isDisjoint() const {
2465+
return cast<llvm::PossiblyDisjointInst>(Val)->isDisjoint();
2466+
}
2467+
/// For isa/dyn_cast.
2468+
static bool classof(const Value *From) {
2469+
return isa<Instruction>(From) &&
2470+
cast<Instruction>(From)->getOpcode() == Opcode::Or;
2471+
}
2472+
};
2473+
24562474
class AtomicRMWInst : public SingleLLVMInstructionImpl<llvm::AtomicRMWInst> {
24572475
AtomicRMWInst(llvm::AtomicRMWInst *Atomic, Context &Ctx)
24582476
: SingleLLVMInstructionImpl(ClassID::AtomicRMW,

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,14 @@ Value *BinaryOperator::createWithCopiedFlags(Instruction::Opcode Op, Value *LHS,
17301730
InsertAtEnd, Ctx, Name);
17311731
}
17321732

1733+
void PossiblyDisjointInst::setIsDisjoint(bool B) {
1734+
Ctx.getTracker()
1735+
.emplaceIfTracking<GenericSetter<&PossiblyDisjointInst::isDisjoint,
1736+
&PossiblyDisjointInst::setIsDisjoint>>(
1737+
this);
1738+
cast<llvm::PossiblyDisjointInst>(Val)->setIsDisjoint(B);
1739+
}
1740+
17331741
void AtomicRMWInst::setAlignment(Align Align) {
17341742
Ctx.getTracker()
17351743
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::getAlign,

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,31 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
29612961
}
29622962
}
29632963

2964+
TEST_F(SandboxIRTest, PossiblyDisjointInst) {
2965+
parseIR(C, R"IR(
2966+
define void @foo(i8 %arg0, i8 %arg1) {
2967+
%or = or i8 %arg0, %arg1
2968+
ret void
2969+
}
2970+
)IR");
2971+
Function &LLVMF = *M->getFunction("foo");
2972+
sandboxir::Context Ctx(C);
2973+
2974+
auto &F = *Ctx.createFunction(&LLVMF);
2975+
auto *BB = &*F.begin();
2976+
auto It = BB->begin();
2977+
auto *PDI = cast<sandboxir::PossiblyDisjointInst>(&*It++);
2978+
2979+
// Check setIsDisjoint(), isDisjoint().
2980+
auto OrigIsDisjoint = PDI->isDisjoint();
2981+
auto NewIsDisjoint = true;
2982+
EXPECT_NE(NewIsDisjoint, OrigIsDisjoint);
2983+
PDI->setIsDisjoint(NewIsDisjoint);
2984+
EXPECT_EQ(PDI->isDisjoint(), NewIsDisjoint);
2985+
PDI->setIsDisjoint(OrigIsDisjoint);
2986+
EXPECT_EQ(PDI->isDisjoint(), OrigIsDisjoint);
2987+
}
2988+
29642989
TEST_F(SandboxIRTest, AtomicRMWInst) {
29652990
parseIR(C, R"IR(
29662991
define void @foo(ptr %ptr, i8 %arg) {

llvm/unittests/SandboxIR/TrackerTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,32 @@ define void @foo(<2 x i8> %v1, <2 x i8> %v2) {
988988
EXPECT_THAT(SVI->getShuffleMask(), testing::ElementsAreArray(OrigMask));
989989
}
990990

991+
TEST_F(TrackerTest, PossiblyDisjointInstSetters) {
992+
parseIR(C, R"IR(
993+
define void @foo(i8 %arg0, i8 %arg1) {
994+
%or = or i8 %arg0, %arg1
995+
ret void
996+
}
997+
)IR");
998+
Function &LLVMF = *M->getFunction("foo");
999+
sandboxir::Context Ctx(C);
1000+
1001+
auto &F = *Ctx.createFunction(&LLVMF);
1002+
auto *BB = &*F.begin();
1003+
auto It = BB->begin();
1004+
auto *PDI = cast<sandboxir::PossiblyDisjointInst>(&*It++);
1005+
1006+
// Check setIsDisjoint().
1007+
auto OrigIsDisjoint = PDI->isDisjoint();
1008+
auto NewIsDisjoint = true;
1009+
EXPECT_NE(NewIsDisjoint, OrigIsDisjoint);
1010+
Ctx.save();
1011+
PDI->setIsDisjoint(NewIsDisjoint);
1012+
EXPECT_EQ(PDI->isDisjoint(), NewIsDisjoint);
1013+
Ctx.revert();
1014+
EXPECT_EQ(PDI->isDisjoint(), OrigIsDisjoint);
1015+
}
1016+
9911017
TEST_F(TrackerTest, AtomicRMWSetters) {
9921018
parseIR(C, R"IR(
9931019
define void @foo(ptr %ptr, i8 %arg) {

0 commit comments

Comments
 (0)