Skip to content

Commit

Permalink
fixup! [loop count assumptions] convert loop iteration metadata to as…
Browse files Browse the repository at this point in the history
…sumptions
  • Loading branch information
F-Stuckmann committed Nov 28, 2024
1 parent deff215 commit 9863dd9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/Utils/LoopIterCountAssumptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Value *recursivlyCloneInBB(Value *Op, BasicBlock &BB, ValueToValueMapTy &VMap) {
<< *Op << "\n");
return nullptr;
}
if (!I->isSafeToRemove()) {
LLVM_DEBUG(dbgs() << "Instruction is not Safe to clone: Aborting! " << *Op
<< "\n");
return nullptr;
}

auto NewI = I->clone();
NewI->insertBefore(&BB.front());
Expand Down
56 changes: 52 additions & 4 deletions llvm/test/Transforms/Util/loop-iter-count-assumptions.ll
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ for.body: ; preds = %for.cond
}


; int &n; for (int i =0 ; i < n + 1; i++)
; int &n
; for (int i =0 ; i < n + 1; i++)
define dso_local void @incrementOffsetOneSigned(ptr noalias %ptr, ptr nonnull align 32 dereferenceable(288) %n) #0 {
; CHECK-LABEL: @incrementOffsetOneSigned(
; CHECK-NEXT: entry:
Expand Down Expand Up @@ -1048,7 +1049,8 @@ for.body: ; preds = %for.cond
br label %for.cond, !llvm.loop !6
}

; unsigned &n; for (unsigned i =0 ; i < n + 1; i++)
; unsigned &n
; for (unsigned i =0 ; i < n + 1; i++)
define dso_local void @incrementOffsetOneUnsigned(ptr noalias %ptr, ptr nonnull align 32 dereferenceable(288) %n) #0 {
; CHECK-LABEL: @incrementOffsetOneUnsigned(
; CHECK-NEXT: entry:
Expand Down Expand Up @@ -1098,7 +1100,8 @@ for.body: ; preds = %for.cond
br label %for.cond, !llvm.loop !6
}

; int &n; for (int i =0 ; i < n - 1; i++)
; int &n
; for (int i =0 ; i < n - 1; i++)
define dso_local void @incrementOffsetNegOneSigned(ptr noalias %ptr, ptr nonnull align 32 dereferenceable(288) %n) #0 {
; CHECK-LABEL: @incrementOffsetNegOneSigned(
; CHECK-NEXT: entry:
Expand Down Expand Up @@ -1148,7 +1151,8 @@ for.body: ; preds = %for.cond
br label %for.cond, !llvm.loop !6
}

; unsigned &n; for (unsigned i =0 ; i < n - 1; i++)
; unsigned &n
; for (unsigned i =0 ; i < n - 1; i++)
define dso_local void @incrementOffsetNegOneUnsigned(ptr noalias %ptr, ptr nonnull align 32 dereferenceable(288) %n) #0 {
; CHECK-LABEL: @incrementOffsetNegOneUnsigned(
; CHECK-NEXT: entry:
Expand Down Expand Up @@ -1198,6 +1202,50 @@ for.body: ; preds = %for.cond
br label %for.cond, !llvm.loop !6
}

; do not insert Assumption if a call instruction should be cloned, since the
; result could be non-deterministic
; Function Attrs: mustprogress noinline nounwind optnone
define dso_local void @abortOnCallClone(ptr %ptr, i32 noundef %n, i32 noundef %m) #0 {
; CHECK-LABEL: @abortOnCallClone(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[FOR_COND:%.*]]
; CHECK: for.cond:
; CHECK-NEXT: [[I_0:%.*]] = phi i32 [ [[M:%.*]], [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY:%.*]] ]
; CHECK-NEXT: [[N_0:%.*]] = call i32 @calcN(i32 [[N:%.*]])
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[I_0]], [[N_0]]
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP:%.*]]
; CHECK: for.cond.cleanup:
; CHECK-NEXT: ret void
; CHECK: for.body:
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[PTR:%.*]], i32 [[I_0]]
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], 8
; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[INC]] = add nsw i32 [[I_0]], 1
; CHECK-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP0]]
;
entry:
br label %for.cond

for.cond: ; preds = %for.body, %entry
%i.0 = phi i32 [ %m, %entry ], [ %inc, %for.body ]
%n.0 = call i32 @calcN(i32 %n)
%cmp = icmp slt i32 %i.0, %n.0
br i1 %cmp, label %for.body, label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.cond
ret void

for.body: ; preds = %for.cond
%arrayidx = getelementptr inbounds i32, ptr %ptr, i32 %i.0
%0 = load i32, ptr %arrayidx, align 4
%add = add nsw i32 %0, 8
store i32 %add, ptr %arrayidx, align 4
%inc = add nsw i32 %i.0, 1
br label %for.cond, !llvm.loop !6
}

declare dso_local i32 @calcN(i32 noundef %n)

!3 = distinct !{!3, !11, !7, !9}
!6 = distinct !{!6, !7, !8, !9}
Expand Down

0 comments on commit 9863dd9

Please sign in to comment.