Skip to content

[NaryReassociate] Check to avoid introducing poison when reusing SCEVs #98156

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 9, 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: 16 additions & 6 deletions llvm/lib/Transforms/Scalar/NaryReassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,24 @@ NaryReassociatePass::findClosestMatchingDominator(const SCEV *CandidateExpr,
// optimization makes the algorithm O(n).
while (!Candidates.empty()) {
// Candidates stores WeakTrackingVHs, so a candidate can be nullptr if it's
// removed
// during rewriting.
if (Value *Candidate = Candidates.back()) {
// removed during rewriting.
if (Value *Candidate = Candidates.pop_back_val()) {
Copy link

Choose a reason for hiding this comment

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

This change seems problematic. Previously, we returned a candidate without pop_back the stack. With this change, the stack popped before returning the candidate. I have seen some problems.

Instruction *CandidateInstruction = cast<Instruction>(Candidate);
if (DT->dominates(CandidateInstruction, Dominatee))
return CandidateInstruction;
if (!DT->dominates(CandidateInstruction, Dominatee))
continue;

// Make sure that the instruction is safe to reuse without introducing
// poison.
SmallVector<Instruction *> DropPoisonGeneratingInsts;
if (!SE->canReuseInstruction(CandidateExpr, CandidateInstruction,
DropPoisonGeneratingInsts))
continue;

for (Instruction *I : DropPoisonGeneratingInsts)
I->dropPoisonGeneratingAnnotations();

return CandidateInstruction;
}
Candidates.pop_back();
}
return nullptr;
}
Expand Down
40 changes: 40 additions & 0 deletions llvm/test/Transforms/NaryReassociate/nary-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,44 @@ define void @no_sext_fat_pointer(ptr addrspace(2) %a, i32 %i, i32 %j) {
ret void
}

define void @or_disjoint(ptr addrspace(2) %a, i32 %i, i32 %j, i32 %k) {
; CHECK-LABEL: @or_disjoint(
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[I:%.*]], [[J:%.*]]
; CHECK-NEXT: [[V2:%.*]] = getelementptr float, ptr addrspace(2) [[A:%.*]], i32 [[OR]]
; CHECK-NEXT: call void @foo(ptr addrspace(2) [[V2]])
; CHECK-NEXT: [[ADD1:%.*]] = add nuw nsw i32 [[I]], [[J]]
; CHECK-NEXT: [[ADD2:%.*]] = add nuw nsw i32 [[ADD1]], [[K:%.*]]
; CHECK-NEXT: [[V3:%.*]] = getelementptr float, ptr addrspace(2) [[A]], i32 [[ADD2]]
; CHECK-NEXT: call void @foo(ptr addrspace(2) [[V3]])
; CHECK-NEXT: ret void
;
%or = or disjoint i32 %i, %j
%v2 = getelementptr float, ptr addrspace(2) %a, i32 %or
call void @foo(ptr addrspace(2) %v2)
%add1 = add nuw nsw i32 %i, %j
%add2 = add nuw nsw i32 %add1, %k
%v3 = getelementptr float, ptr addrspace(2) %a, i32 %add2
call void @foo(ptr addrspace(2) %v3)
ret void
}

define void @drop_nuw_nsw(ptr addrspace(2) %a, i32 %i, i32 %j, i32 %k) {
; CHECK-LABEL: @drop_nuw_nsw(
; CHECK-NEXT: [[ADD0:%.*]] = add i32 [[I:%.*]], [[J:%.*]]
; CHECK-NEXT: [[V2:%.*]] = getelementptr float, ptr addrspace(2) [[A:%.*]], i32 [[ADD0]]
; CHECK-NEXT: call void @foo(ptr addrspace(2) [[V2]])
; CHECK-NEXT: [[V3:%.*]] = getelementptr float, ptr addrspace(2) [[V2]], i32 [[K:%.*]]
; CHECK-NEXT: call void @foo(ptr addrspace(2) [[V3]])
; CHECK-NEXT: ret void
;
%add0 = add nuw nsw i32 %i, %j
%v2 = getelementptr float, ptr addrspace(2) %a, i32 %add0
call void @foo(ptr addrspace(2) %v2)
%add1 = add i32 %i, %j
%add2 = add nuw nsw i32 %add1, %k
%v3 = getelementptr float, ptr addrspace(2) %a, i32 %add2
call void @foo(ptr addrspace(2) %v3)
ret void
}

declare void @foo(ptr addrspace(2))
Loading