Skip to content

[SandboxIR] Implement PossiblyDisjointInst #106148

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 26, 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 @@ -144,6 +144,7 @@ class CatchSwitchInst;
class SwitchInst;
class UnaryOperator;
class BinaryOperator;
class PossiblyDisjointInst;
class AtomicRMWInst;
class AtomicCmpXchgInst;

Expand Down Expand Up @@ -2374,6 +2375,7 @@ class UnaryOperator : public UnaryInstruction {
};

class BinaryOperator : public SingleLLVMInstructionImpl<llvm::BinaryOperator> {
protected:
static Opcode getBinOpOpcode(llvm::Instruction::BinaryOps BinOp) {
switch (BinOp) {
case llvm::Instruction::Add:
Expand Down Expand Up @@ -2453,6 +2455,22 @@ class BinaryOperator : public SingleLLVMInstructionImpl<llvm::BinaryOperator> {
void swapOperands() { swapOperandsInternal(0, 1); }
};

/// An or instruction, which can be marked as "disjoint", indicating that the
/// inputs don't have a 1 in the same bit position. Meaning this instruction
/// can also be treated as an add.
class PossiblyDisjointInst : public BinaryOperator {
public:
void setIsDisjoint(bool B);
bool isDisjoint() const {
return cast<llvm::PossiblyDisjointInst>(Val)->isDisjoint();
}
/// For isa/dyn_cast.
static bool classof(const Value *From) {
return isa<Instruction>(From) &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dyn_cast !

cast<Instruction>(From)->getOpcode() == Opcode::Or;
}
};

class AtomicRMWInst : public SingleLLVMInstructionImpl<llvm::AtomicRMWInst> {
AtomicRMWInst(llvm::AtomicRMWInst *Atomic, Context &Ctx)
: SingleLLVMInstructionImpl(ClassID::AtomicRMW,
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,14 @@ Value *BinaryOperator::createWithCopiedFlags(Instruction::Opcode Op, Value *LHS,
InsertAtEnd, Ctx, Name);
}

void PossiblyDisjointInst::setIsDisjoint(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&PossiblyDisjointInst::isDisjoint,
&PossiblyDisjointInst::setIsDisjoint>>(
this);
cast<llvm::PossiblyDisjointInst>(Val)->setIsDisjoint(B);
}

void AtomicRMWInst::setAlignment(Align Align) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::getAlign,
Expand Down
25 changes: 25 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,31 @@ define void @foo(i8 %arg0, i8 %arg1, float %farg0, float %farg1) {
}
}

TEST_F(SandboxIRTest, PossiblyDisjointInst) {
parseIR(C, R"IR(
define void @foo(i8 %arg0, i8 %arg1) {
%or = or i8 %arg0, %arg1
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 *PDI = cast<sandboxir::PossiblyDisjointInst>(&*It++);

// Check setIsDisjoint(), isDisjoint().
auto OrigIsDisjoint = PDI->isDisjoint();
auto NewIsDisjoint = true;
EXPECT_NE(NewIsDisjoint, OrigIsDisjoint);
PDI->setIsDisjoint(NewIsDisjoint);
EXPECT_EQ(PDI->isDisjoint(), NewIsDisjoint);
PDI->setIsDisjoint(OrigIsDisjoint);
EXPECT_EQ(PDI->isDisjoint(), OrigIsDisjoint);
}

TEST_F(SandboxIRTest, AtomicRMWInst) {
parseIR(C, R"IR(
define void @foo(ptr %ptr, i8 %arg) {
Expand Down
26 changes: 26 additions & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,32 @@ define void @foo(<2 x i8> %v1, <2 x i8> %v2) {
EXPECT_THAT(SVI->getShuffleMask(), testing::ElementsAreArray(OrigMask));
}

TEST_F(TrackerTest, PossiblyDisjointInstSetters) {
parseIR(C, R"IR(
define void @foo(i8 %arg0, i8 %arg1) {
%or = or i8 %arg0, %arg1
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 *PDI = cast<sandboxir::PossiblyDisjointInst>(&*It++);

// Check setIsDisjoint().
auto OrigIsDisjoint = PDI->isDisjoint();
auto NewIsDisjoint = true;
EXPECT_NE(NewIsDisjoint, OrigIsDisjoint);
Ctx.save();
PDI->setIsDisjoint(NewIsDisjoint);
EXPECT_EQ(PDI->isDisjoint(), NewIsDisjoint);
Ctx.revert();
EXPECT_EQ(PDI->isDisjoint(), OrigIsDisjoint);
}

TEST_F(TrackerTest, AtomicRMWSetters) {
parseIR(C, R"IR(
define void @foo(ptr %ptr, i8 %arg) {
Expand Down
Loading