[llvm] 74405b9 - [SandboxIR] Implement a few Instruction member functions (#109709)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 23 13:46:31 PDT 2024
Author: vporpo
Date: 2024-09-23T13:46:28-07:00
New Revision: 74405b9d74d974523c0f9172a9b0c1cfd7320356
URL: https://github.com/llvm/llvm-project/commit/74405b9d74d974523c0f9172a9b0c1cfd7320356
DIFF: https://github.com/llvm/llvm-project/commit/74405b9d74d974523c0f9172a9b0c1cfd7320356.diff
LOG: [SandboxIR] Implement a few Instruction member functions (#109709)
This patch implements some of the missing member functions of
sandboxir::Instruction.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 4e751a75196ce1..84c32aeb0451f5 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -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,
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index b1ed72be1030d6..124c1bf92ca7fc 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -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,
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index ae143fd5b74ea1..1fcc9cbea152cd 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -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++;
@@ -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) {
More information about the llvm-commits
mailing list