Skip to content

[SandboxIR] Add Instruction::isStackSaveRestoreIntrinsic() and isMemDepCandidate() #109212

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
Sep 19, 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
22 changes: 22 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/SandboxIR/Tracker.h"
Expand Down Expand Up @@ -1943,6 +1945,26 @@ class Instruction : public sandboxir::User {
/// LangRef.html for the meaning of these flags.
void copyFastMathFlags(FastMathFlags FMF);

bool isStackSaveOrRestoreIntrinsic() const {
auto *I = cast<llvm::Instruction>(Val);
return match(I,
PatternMatch::m_Intrinsic<llvm::Intrinsic::stackrestore>()) ||
match(I, PatternMatch::m_Intrinsic<llvm::Intrinsic::stacksave>());
}

/// 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.
bool isMemDepCandidate() const {
auto *I = cast<llvm::Instruction>(Val);
return I->mayReadOrWriteMemory() &&
(!isa<llvm::IntrinsicInst>(I) ||
(cast<llvm::IntrinsicInst>(I)->getIntrinsicID() !=
Intrinsic::sideeffect &&
cast<llvm::IntrinsicInst>(I)->getIntrinsicID() !=
Intrinsic::pseudoprobe));
}

#ifndef NDEBUG
void dumpOS(raw_ostream &OS) const override;
#endif
Expand Down
71 changes: 71 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,77 @@ define void @foo(i8 %v1) {
EXPECT_EQ(I0->getNextNode(), Ret);
}

TEST_F(SandboxIRTest, Instruction_isStackSaveOrRestoreIntrinsic) {
parseIR(C, R"IR(
declare void @llvm.sideeffect()
define void @foo(i8 %v1, ptr %ptr) {
%add = add i8 %v1, %v1
%stacksave = call ptr @llvm.stacksave()
call void @llvm.stackrestore(ptr %stacksave)
call void @llvm.sideeffect()
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 *Add = cast<sandboxir::BinaryOperator>(&*It++);
auto *StackSave = cast<sandboxir::CallInst>(&*It++);
auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
auto *Other = cast<sandboxir::CallInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

EXPECT_FALSE(Add->isStackSaveOrRestoreIntrinsic());
EXPECT_TRUE(StackSave->isStackSaveOrRestoreIntrinsic());
EXPECT_TRUE(StackRestore->isStackSaveOrRestoreIntrinsic());
EXPECT_FALSE(Other->isStackSaveOrRestoreIntrinsic());
EXPECT_FALSE(Ret->isStackSaveOrRestoreIntrinsic());
}

TEST_F(SandboxIRTest, Instruction_isMemDepCandidate) {
parseIR(C, R"IR(
declare void @llvm.fake.use(...)
declare void @llvm.sideeffect()
declare void @llvm.pseudoprobe(i64, i64, i32, i64)
declare void @bar()
define void @foo(i8 %v1, ptr %ptr) {
%add0 = add i8 %v1, %v1
%ld0 = load i8, ptr %ptr
store i8 %v1, ptr %ptr
call void @llvm.sideeffect()
call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
call void @llvm.fake.use(ptr %ptr)
call void @bar()
ret void
}
)IR");
llvm::Function *LLVMF = &*M->getFunction("foo");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(LLVMF);
auto *Arg = F->getArg(0);
auto *BB = &*F->begin();
auto It = BB->begin();
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
auto *CallBar = cast<sandboxir::CallInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

EXPECT_FALSE(Add0->isMemDepCandidate());
EXPECT_TRUE(Ld0->isMemDepCandidate());
EXPECT_TRUE(St0->isMemDepCandidate());
EXPECT_FALSE(SideEffect0->isMemDepCandidate());
EXPECT_FALSE(PseudoProbe0->isMemDepCandidate());
EXPECT_TRUE(OtherIntrinsic0->isMemDepCandidate());
EXPECT_TRUE(CallBar->isMemDepCandidate());
EXPECT_FALSE(Ret->isMemDepCandidate());
}

TEST_F(SandboxIRTest, VAArgInst) {
parseIR(C, R"IR(
define void @foo(ptr %va) {
Expand Down
Loading