Skip to content

Commit

Permalink
[SandboxIR] Implement a few Instruction member functions
Browse files Browse the repository at this point in the history
This patch implements some of the missing member functions
of sandboxir::Instruction.
  • Loading branch information
vporpo committed Sep 23, 2024
1 parent 2b892b0 commit 9fcc92c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
55 changes: 55 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,61 @@ class Instruction : public sandboxir::User {
/// LangRef.html for the meaning of these flags.
void copyFastMathFlags(FastMathFlags FMF);

bool isAssociative() const {
return cast<llvm::Instruction>(Val)->isAssociative();
}

bool isCommutative() const {
return cast<llvm::Instruction>(Val)->isCommutative();
}

bool isIdempotent() const {
return cast<llvm::Instruction>(Val)->isIdempotent();
}

bool isNilpotent() const {
return cast<llvm::Instruction>(Val)->isNilpotent();
}

bool mayWriteToMemory() const {
return cast<llvm::Instruction>(Val)->mayWriteToMemory();
}

bool mayReadFromMemory() const {
return cast<llvm::Instruction>(Val)->mayReadFromMemory();
}
bool mayReadOrWriteMemory() const {
return cast<llvm::Instruction>(Val)->mayReadOrWriteMemory();
}

bool isAtomic() const { return cast<llvm::Instruction>(Val)->isAtomic(); }

bool hasAtomicLoad() const {
return cast<llvm::Instruction>(Val)->hasAtomicLoad();
}

bool hasAtomicStore() const {
return cast<llvm::Instruction>(Val)->hasAtomicStore();
}

bool isVolatile() const { return cast<llvm::Instruction>(Val)->isVolatile(); }

Type *getAccessType() const;

bool mayThrow(bool IncludePhaseOneUnwind = false) const {
return cast<llvm::Instruction>(Val)->mayThrow(IncludePhaseOneUnwind);
}

bool isFenceLike() const {
return cast<llvm::Instruction>(Val)->isFenceLike();
}

bool mayHaveSideEffects() const {
return cast<llvm::Instruction>(Val)->mayHaveSideEffects();
}

// TODO: Missing functions.

bool isStackSaveOrRestoreIntrinsic() const {
auto *I = cast<llvm::Instruction>(Val);
return match(I,
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ void Instruction::copyFastMathFlags(FastMathFlags FMF) {
cast<llvm::Instruction>(Val)->copyFastMathFlags(FMF);
}

Type *Instruction::getAccessType() const {
return Ctx.getType(cast<llvm::Instruction>(Val)->getAccessType());
}

void Instruction::setHasApproxFunc(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasApproxFunc,
Expand Down
54 changes: 52 additions & 2 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1755,17 +1755,31 @@ define void @foo(i32 %v1) {

TEST_F(SandboxIRTest, Instruction) {
parseIR(C, R"IR(
define void @foo(i8 %v1) {
define void @foo(i8 %v1, ptr %ptr) {
bb0:
%add0 = add i8 %v1, %v1
%sub1 = sub i8 %add0, %v1
ret void
bb1:
%add1 = add i8 %v1, %v1
%sub2 = sub i8 %add1, %v1
%ld0 = load i8, ptr %ptr
store i8 %ld0, ptr %ptr
store volatile i8 %ld0, ptr %ptr
%atomicrmw = atomicrmw add ptr %ptr, i8 %v1 acquire
%udiv = udiv i8 %ld0, %v1
call void @foo()
ret void
}
)IR");
llvm::Function *LLVMF = &*M->getFunction("foo");
llvm::BasicBlock *LLVMBB1 = getBasicBlockByName(*LLVMF, "bb1");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(LLVMF);
auto *Arg = F->getArg(0);
auto *BB = &*F->begin();
auto *BB = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(*LLVMF, "bb0")));
auto It = BB->begin();
auto *I0 = &*It++;
auto *I1 = &*It++;
Expand Down Expand Up @@ -1844,6 +1858,42 @@ define void @foo(i8 %v1) {
I1->eraseFromParent();
EXPECT_EQ(I0->getNumUses(), 0u);
EXPECT_EQ(I0->getNextNode(), Ret);

for (auto &LLVMI : *LLVMBB1) {
auto &I = cast<sandboxir::Instruction>(*Ctx.getValue(&LLVMI));
// Check isAssociative().
EXPECT_EQ(LLVMI.isAssociative(), I.isAssociative());
// Check isCommutative().
EXPECT_EQ(LLVMI.isCommutative(), I.isCommutative());
// Check isIdempotent().
EXPECT_EQ(LLVMI.isIdempotent(), I.isIdempotent());
// Check isNilpotent().
EXPECT_EQ(LLVMI.isNilpotent(), I.isNilpotent());
// Check mayWriteToMemory().
EXPECT_EQ(LLVMI.mayWriteToMemory(), I.mayWriteToMemory());
// Check mayReadFromMemory().
EXPECT_EQ(LLVMI.mayReadFromMemory(), I.mayReadFromMemory());
// Check mayReadOrWriteMemory().
EXPECT_EQ(LLVMI.mayReadOrWriteMemory(), I.mayReadOrWriteMemory());
// Check isAtomic().
EXPECT_EQ(LLVMI.isAtomic(), I.isAtomic());
if (I.isAtomic()) {
// Check hasAtomicLoad().
EXPECT_EQ(LLVMI.hasAtomicLoad(), I.hasAtomicLoad());
// Check hasAtomicStore().
EXPECT_EQ(LLVMI.hasAtomicStore(), I.hasAtomicStore());
}
// Check isVolatile().
EXPECT_EQ(LLVMI.isVolatile(), I.isVolatile());
// Check getAccessType().
EXPECT_EQ(Ctx.getType(LLVMI.getAccessType()), I.getAccessType());
// Check mayThrow().
EXPECT_EQ(LLVMI.mayThrow(), I.mayThrow());
// Check isFenceLike().
EXPECT_EQ(LLVMI.isFenceLike(), I.isFenceLike());
// Check mayHaveSideEffects().
EXPECT_EQ(LLVMI.mayHaveSideEffects(), I.mayHaveSideEffects());
}
}

TEST_F(SandboxIRTest, Instruction_isStackSaveOrRestoreIntrinsic) {
Expand Down

0 comments on commit 9fcc92c

Please sign in to comment.