Skip to content

[SandboxIR][NFC] Move intrinsic code to Utils::isMemIntrinsic() #111019

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
Oct 3, 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
28 changes: 16 additions & 12 deletions llvm/include/llvm/SandboxIR/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/IntrinsicInst.h"
#include <optional>

namespace llvm::sandboxir {
Expand Down Expand Up @@ -101,24 +102,27 @@ class Utils {
}

static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
auto *LLVMI = cast<llvm::Instruction>(I->Val);
return match(LLVMI,
PatternMatch::m_Intrinsic<llvm::Intrinsic::stackrestore>()) ||
match(LLVMI,
PatternMatch::m_Intrinsic<llvm::Intrinsic::stacksave>());
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
auto IID = II->getIntrinsicID();
return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
}
return false;
}

/// \Returns true if intrinsic \p I touches memory. This is used by the
/// dependency graph.
static bool isMemIntrinsic(IntrinsicInst *II) {
auto IID = II->getIntrinsicID();
return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
}

/// We consider \p I as a Memory Dependency Candidate instruction if it
/// reads/write memory or if it has side-effects. This is used by the
/// dependency graph.
static bool isMemDepCandidate(Instruction *I) {
auto *LLVMI = cast<llvm::Instruction>(I->Val);
return LLVMI->mayReadOrWriteMemory() &&
(!isa<llvm::IntrinsicInst>(LLVMI) ||
(cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
Intrinsic::sideeffect &&
cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
Intrinsic::pseudoprobe));
IntrinsicInst *II;
return I->mayReadOrWriteMemory() &&
(!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
}
};
} // namespace llvm::sandboxir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class DGNode {
virtual ~DGNode() = default;
/// \Returns true if this is before \p Other in program order.
bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }

/// \Returns true if \p I is a memory dependency candidate instruction.
static bool isMemDepCandidate(Instruction *I) {
AllocaInst *Alloca;
Expand Down
28 changes: 28 additions & 0 deletions llvm/unittests/SandboxIR/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,31 @@ define void @foo(i8 %v1, ptr %ptr) {
EXPECT_TRUE(Utils::isMemDepCandidate(CallBar));
EXPECT_FALSE(Utils::isMemDepCandidate(Ret));
}

TEST_F(UtilsTest, Instruction_isMemIntrinsic) {
parseIR(C, R"IR(
declare void @llvm.sideeffect()
declare void @llvm.pseudoprobe(i64)
declare void @llvm.assume(i1)

define void @foo(ptr %ptr, i1 %cond) {
call void @llvm.sideeffect()
call void @llvm.pseudoprobe(i64 42)
call void @llvm.assume(i1 %cond)
ret void
}
)IR");
llvm::Function *LLVMF = &*M->getFunction("foo");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(LLVMF);
auto *BB = &*F->begin();
auto It = BB->begin();
auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
using Utils = sandboxir::Utils;

EXPECT_FALSE(Utils::isMemIntrinsic(SideEffect));
EXPECT_FALSE(Utils::isMemIntrinsic(PseudoProbe));
EXPECT_TRUE(Utils::isMemIntrinsic(OtherIntrinsic));
}
Loading