Skip to content

[Analysis] Add new function isDereferenceableReadOnlyLoop #97292

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 2 commits into from
Jul 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
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/Loads.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
ScalarEvolution &SE, DominatorTree &DT,
AssumptionCache *AC = nullptr);

/// Return true if the loop \p L cannot fault on any iteration and only
/// contains read-only memory accesses.
bool isDereferenceableReadOnlyLoop(Loop *L, ScalarEvolution *SE,
DominatorTree *DT, AssumptionCache *AC);

/// Return true if we know that executing a load from this value cannot trap.
///
/// If DT and ScanFrom are specified this method performs context-sensitive
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Analysis/Loads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,18 @@ bool llvm::canReplacePointersIfEqual(const Value *From, const Value *To,

return isPointerAlwaysReplaceable(From, To, DL);
}

bool llvm::isDereferenceableReadOnlyLoop(Loop *L, ScalarEvolution *SE,
DominatorTree *DT,
AssumptionCache *AC) {
for (BasicBlock *BB : L->blocks()) {
for (Instruction &I : *BB) {
if (auto *LI = dyn_cast<LoadInst>(&I)) {
if (!isDereferenceableAndAlignedInLoop(LI, L, *SE, *DT, AC))
return false;
} else if (I.mayReadFromMemory() || I.mayWriteToMemory() || I.mayThrow())
return false;
}
}
return true;
}
87 changes: 87 additions & 0 deletions llvm/unittests/Analysis/LoadsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
Expand Down Expand Up @@ -114,3 +119,85 @@ define void @f(i32* %p1, i32* %p2, i64 %i) {
EXPECT_TRUE(canReplacePointersInUseIfEqual(PtrToIntUse, P2, DL));
EXPECT_TRUE(canReplacePointersInUseIfEqual(IcmpUse, P2, DL));
}

TEST(LoadsTest, IsDerefReadOnlyLoop) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C,
R"IR(
define i64 @f1() {
entry:
%p1 = alloca [1024 x i8]
%p2 = alloca [1024 x i8]
br label %loop

loop:
%index = phi i64 [ %index.next, %loop.inc ], [ 3, %entry ]
%arrayidx = getelementptr inbounds i8, ptr %p1, i64 %index
%ld1 = load i8, ptr %arrayidx, align 1
%arrayidx1 = getelementptr inbounds i8, ptr %p2, i64 %index
%ld2 = load i8, ptr %arrayidx1, align 1
%cmp3 = icmp eq i8 %ld1, %ld2
br i1 %cmp3, label %loop.inc, label %loop.end

loop.inc:
%index.next = add i64 %index, 1
%exitcond = icmp ne i64 %index.next, 67
br i1 %exitcond, label %loop, label %loop.end

loop.end:
%retval = phi i64 [ %index, %loop ], [ 67, %loop.inc ]
ret i64 %retval
}

define i64 @f2(ptr %p1) {
entry:
%p2 = alloca [1024 x i8]
br label %loop

loop:
%index = phi i64 [ %index.next, %loop.inc ], [ 3, %entry ]
%arrayidx = getelementptr inbounds i8, ptr %p1, i64 %index
%ld1 = load i8, ptr %arrayidx, align 1
%arrayidx1 = getelementptr inbounds i8, ptr %p2, i64 %index
%ld2 = load i8, ptr %arrayidx1, align 1
%cmp3 = icmp eq i8 %ld1, %ld2
br i1 %cmp3, label %loop.inc, label %loop.end

loop.inc:
%index.next = add i64 %index, 1
%exitcond = icmp ne i64 %index.next, 67
br i1 %exitcond, label %loop, label %loop.end

loop.end:
%retval = phi i64 [ %index, %loop ], [ 67, %loop.inc ]
ret i64 %retval
}
)IR");
auto *GV1 = M->getNamedValue("f1");
auto *GV2 = M->getNamedValue("f2");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be cleaner if you added a lambda function to perform the lookup and analysis, then return the result of isDereferenceableReadOnlyLoop and assert on the return value.

That way you wouldn't need the loop and the if/else based on I.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

ASSERT_TRUE(GV1 && GV2);
auto *F1 = dyn_cast<Function>(GV1);
auto *F2 = dyn_cast<Function>(GV2);
ASSERT_TRUE(F1 && F2);

TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI(TLII);

auto IsDerefReadOnlyLoop = [&TLI](Function *F) -> bool {
AssumptionCache AC(*F);
DominatorTree DT(*F);
LoopInfo LI(DT);
ScalarEvolution SE(*F, TLI, AC, DT, LI);

Function::iterator FI = F->begin();
// First basic block is entry - skip it.
BasicBlock *Header = &*(++FI);
assert(Header->getName() == "loop");
Loop *L = LI.getLoopFor(Header);

return isDereferenceableReadOnlyLoop(L, &SE, &DT, &AC);
};

ASSERT_TRUE(IsDerefReadOnlyLoop(F1));
ASSERT_FALSE(IsDerefReadOnlyLoop(F2));
}
Loading