Skip to content

Commit 74405b9

Browse files
authored
[SandboxIR] Implement a few Instruction member functions (#109709)
This patch implements some of the missing member functions of sandboxir::Instruction.
1 parent 3138eb5 commit 74405b9

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,61 @@ class Instruction : public sandboxir::User {
20572057
/// LangRef.html for the meaning of these flags.
20582058
void copyFastMathFlags(FastMathFlags FMF);
20592059

2060+
bool isAssociative() const {
2061+
return cast<llvm::Instruction>(Val)->isAssociative();
2062+
}
2063+
2064+
bool isCommutative() const {
2065+
return cast<llvm::Instruction>(Val)->isCommutative();
2066+
}
2067+
2068+
bool isIdempotent() const {
2069+
return cast<llvm::Instruction>(Val)->isIdempotent();
2070+
}
2071+
2072+
bool isNilpotent() const {
2073+
return cast<llvm::Instruction>(Val)->isNilpotent();
2074+
}
2075+
2076+
bool mayWriteToMemory() const {
2077+
return cast<llvm::Instruction>(Val)->mayWriteToMemory();
2078+
}
2079+
2080+
bool mayReadFromMemory() const {
2081+
return cast<llvm::Instruction>(Val)->mayReadFromMemory();
2082+
}
2083+
bool mayReadOrWriteMemory() const {
2084+
return cast<llvm::Instruction>(Val)->mayReadOrWriteMemory();
2085+
}
2086+
2087+
bool isAtomic() const { return cast<llvm::Instruction>(Val)->isAtomic(); }
2088+
2089+
bool hasAtomicLoad() const {
2090+
return cast<llvm::Instruction>(Val)->hasAtomicLoad();
2091+
}
2092+
2093+
bool hasAtomicStore() const {
2094+
return cast<llvm::Instruction>(Val)->hasAtomicStore();
2095+
}
2096+
2097+
bool isVolatile() const { return cast<llvm::Instruction>(Val)->isVolatile(); }
2098+
2099+
Type *getAccessType() const;
2100+
2101+
bool mayThrow(bool IncludePhaseOneUnwind = false) const {
2102+
return cast<llvm::Instruction>(Val)->mayThrow(IncludePhaseOneUnwind);
2103+
}
2104+
2105+
bool isFenceLike() const {
2106+
return cast<llvm::Instruction>(Val)->isFenceLike();
2107+
}
2108+
2109+
bool mayHaveSideEffects() const {
2110+
return cast<llvm::Instruction>(Val)->mayHaveSideEffects();
2111+
}
2112+
2113+
// TODO: Missing functions.
2114+
20602115
bool isStackSaveOrRestoreIntrinsic() const {
20612116
auto *I = cast<llvm::Instruction>(Val);
20622117
return match(I,

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ void Instruction::copyFastMathFlags(FastMathFlags FMF) {
569569
cast<llvm::Instruction>(Val)->copyFastMathFlags(FMF);
570570
}
571571

572+
Type *Instruction::getAccessType() const {
573+
return Ctx.getType(cast<llvm::Instruction>(Val)->getAccessType());
574+
}
575+
572576
void Instruction::setHasApproxFunc(bool B) {
573577
Ctx.getTracker()
574578
.emplaceIfTracking<GenericSetter<&Instruction::hasApproxFunc,

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,17 +1755,31 @@ define void @foo(i32 %v1) {
17551755

17561756
TEST_F(SandboxIRTest, Instruction) {
17571757
parseIR(C, R"IR(
1758-
define void @foo(i8 %v1) {
1758+
define void @foo(i8 %v1, ptr %ptr) {
1759+
bb0:
17591760
%add0 = add i8 %v1, %v1
17601761
%sub1 = sub i8 %add0, %v1
17611762
ret void
1763+
1764+
bb1:
1765+
%add1 = add i8 %v1, %v1
1766+
%sub2 = sub i8 %add1, %v1
1767+
%ld0 = load i8, ptr %ptr
1768+
store i8 %ld0, ptr %ptr
1769+
store volatile i8 %ld0, ptr %ptr
1770+
%atomicrmw = atomicrmw add ptr %ptr, i8 %v1 acquire
1771+
%udiv = udiv i8 %ld0, %v1
1772+
call void @foo()
1773+
ret void
17621774
}
17631775
)IR");
17641776
llvm::Function *LLVMF = &*M->getFunction("foo");
1777+
llvm::BasicBlock *LLVMBB1 = getBasicBlockByName(*LLVMF, "bb1");
17651778
sandboxir::Context Ctx(C);
17661779
sandboxir::Function *F = Ctx.createFunction(LLVMF);
17671780
auto *Arg = F->getArg(0);
1768-
auto *BB = &*F->begin();
1781+
auto *BB = cast<sandboxir::BasicBlock>(
1782+
Ctx.getValue(getBasicBlockByName(*LLVMF, "bb0")));
17691783
auto It = BB->begin();
17701784
auto *I0 = &*It++;
17711785
auto *I1 = &*It++;
@@ -1844,6 +1858,42 @@ define void @foo(i8 %v1) {
18441858
I1->eraseFromParent();
18451859
EXPECT_EQ(I0->getNumUses(), 0u);
18461860
EXPECT_EQ(I0->getNextNode(), Ret);
1861+
1862+
for (auto &LLVMI : *LLVMBB1) {
1863+
auto &I = cast<sandboxir::Instruction>(*Ctx.getValue(&LLVMI));
1864+
// Check isAssociative().
1865+
EXPECT_EQ(LLVMI.isAssociative(), I.isAssociative());
1866+
// Check isCommutative().
1867+
EXPECT_EQ(LLVMI.isCommutative(), I.isCommutative());
1868+
// Check isIdempotent().
1869+
EXPECT_EQ(LLVMI.isIdempotent(), I.isIdempotent());
1870+
// Check isNilpotent().
1871+
EXPECT_EQ(LLVMI.isNilpotent(), I.isNilpotent());
1872+
// Check mayWriteToMemory().
1873+
EXPECT_EQ(LLVMI.mayWriteToMemory(), I.mayWriteToMemory());
1874+
// Check mayReadFromMemory().
1875+
EXPECT_EQ(LLVMI.mayReadFromMemory(), I.mayReadFromMemory());
1876+
// Check mayReadOrWriteMemory().
1877+
EXPECT_EQ(LLVMI.mayReadOrWriteMemory(), I.mayReadOrWriteMemory());
1878+
// Check isAtomic().
1879+
EXPECT_EQ(LLVMI.isAtomic(), I.isAtomic());
1880+
if (I.isAtomic()) {
1881+
// Check hasAtomicLoad().
1882+
EXPECT_EQ(LLVMI.hasAtomicLoad(), I.hasAtomicLoad());
1883+
// Check hasAtomicStore().
1884+
EXPECT_EQ(LLVMI.hasAtomicStore(), I.hasAtomicStore());
1885+
}
1886+
// Check isVolatile().
1887+
EXPECT_EQ(LLVMI.isVolatile(), I.isVolatile());
1888+
// Check getAccessType().
1889+
EXPECT_EQ(Ctx.getType(LLVMI.getAccessType()), I.getAccessType());
1890+
// Check mayThrow().
1891+
EXPECT_EQ(LLVMI.mayThrow(), I.mayThrow());
1892+
// Check isFenceLike().
1893+
EXPECT_EQ(LLVMI.isFenceLike(), I.isFenceLike());
1894+
// Check mayHaveSideEffects().
1895+
EXPECT_EQ(LLVMI.mayHaveSideEffects(), I.mayHaveSideEffects());
1896+
}
18471897
}
18481898

18491899
TEST_F(SandboxIRTest, Instruction_isStackSaveOrRestoreIntrinsic) {

0 commit comments

Comments
 (0)