Skip to content

Commit 80cdd30

Browse files
committed
[LoopPeel] Use llvm.experimental.noalias.scope.decl for duplicating noalias metadata as needed.
The reduction of a sanitizer build failure when enabling the dominance check (D95335) showed that loop peeling also needs to take care of scope duplication, just like loop unrolling (D92887). Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D95544
1 parent 2939d2e commit 80cdd30

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

llvm/lib/Transforms/Utils/LoopPeel.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static void cloneLoopBlocks(
509509
SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *>> &ExitEdges,
510510
SmallVectorImpl<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
511511
ValueToValueMapTy &VMap, ValueToValueMapTy &LVMap, DominatorTree *DT,
512-
LoopInfo *LI) {
512+
LoopInfo *LI, ArrayRef<MDNode *> LoopLocalNoAliasDeclScopes) {
513513
BasicBlock *Header = L->getHeader();
514514
BasicBlock *Latch = L->getLoopLatch();
515515
BasicBlock *PreHeader = L->getLoopPreheader();
@@ -545,6 +545,15 @@ static void cloneLoopBlocks(
545545
}
546546
}
547547

548+
{
549+
// Identify what other metadata depends on the cloned version. After
550+
// cloning, replace the metadata with the corrected version for both
551+
// memory instructions and noalias intrinsics.
552+
std::string Ext = (Twine("Peel") + Twine(IterNumber)).str();
553+
cloneAndAdaptNoAliasScopes(LoopLocalNoAliasDeclScopes, NewBlocks,
554+
Header->getContext(), Ext);
555+
}
556+
548557
// Recursively create the new Loop objects for nested loops, if any,
549558
// to preserve LoopInfo.
550559
for (Loop *ChildLoop : *L) {
@@ -769,13 +778,19 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
769778
uint64_t ExitWeight = 0, FallThroughWeight = 0;
770779
initBranchWeights(Header, LatchBR, ExitWeight, FallThroughWeight);
771780

781+
// Identify what noalias metadata is inside the loop: if it is inside the
782+
// loop, the associated metadata must be cloned for each iteration.
783+
SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
784+
identifyNoAliasScopesToClone(L->getBlocks(), LoopLocalNoAliasDeclScopes);
785+
772786
// For each peeled-off iteration, make a copy of the loop.
773787
for (unsigned Iter = 0; Iter < PeelCount; ++Iter) {
774788
SmallVector<BasicBlock *, 8> NewBlocks;
775789
ValueToValueMapTy VMap;
776790

777791
cloneLoopBlocks(L, Iter, InsertTop, InsertBot, ExitEdges, NewBlocks,
778-
LoopBlocks, VMap, LVMap, DT, LI);
792+
LoopBlocks, VMap, LVMap, DT, LI,
793+
LoopLocalNoAliasDeclScopes);
779794

780795
// Remap to use values from the current iteration instead of the
781796
// previous one.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -S -loop-unroll -unroll-force-peel-count=1 | FileCheck %s
3+
; RUN: opt < %s -S -passes='loop-unroll<peeling;no-runtime>' -unroll-force-peel-count=1 | FileCheck %s
4+
5+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
; Loop peeling must result in valid scope declartions
9+
10+
define internal fastcc void @test01(i8* %p0, i8* %p1, i8* %p2) unnamed_addr align 2 {
11+
; CHECK-LABEL: @test01(
12+
; CHECK-NEXT: for.body47.lr.ph:
13+
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata !0)
14+
; CHECK-NEXT: br label [[FOR_BODY47_PEEL_BEGIN:%.*]]
15+
; CHECK: for.body47.peel.begin:
16+
; CHECK-NEXT: br label [[FOR_BODY47_PEEL:%.*]]
17+
; CHECK: for.body47.peel:
18+
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata !3)
19+
; CHECK-NEXT: store i8 42, i8* [[P0:%.*]], align 1, !alias.scope !3
20+
; CHECK-NEXT: store i8 43, i8* [[P1:%.*]], align 1, !alias.scope !0
21+
; CHECK-NEXT: store i8 44, i8* [[P2:%.*]], align 1, !alias.scope !5
22+
; CHECK-NEXT: store i8 42, i8* [[P0]], align 1, !noalias !3
23+
; CHECK-NEXT: store i8 43, i8* [[P1]], align 1, !noalias !0
24+
; CHECK-NEXT: store i8 44, i8* [[P2]], align 1, !noalias !5
25+
; CHECK-NEXT: [[CMP52_PEEL:%.*]] = icmp eq i32 0, 0
26+
; CHECK-NEXT: br i1 [[CMP52_PEEL]], label [[COND_TRUE_PEEL:%.*]], label [[COND_END_PEEL:%.*]]
27+
; CHECK: cond.true.peel:
28+
; CHECK-NEXT: store i8 52, i8* [[P0]], align 1, !alias.scope !3
29+
; CHECK-NEXT: store i8 53, i8* [[P1]], align 1, !alias.scope !0
30+
; CHECK-NEXT: store i8 54, i8* [[P2]], align 1, !alias.scope !5
31+
; CHECK-NEXT: store i8 52, i8* [[P0]], align 1, !noalias !3
32+
; CHECK-NEXT: store i8 53, i8* [[P1]], align 1, !noalias !0
33+
; CHECK-NEXT: store i8 54, i8* [[P2]], align 1, !noalias !5
34+
; CHECK-NEXT: br label [[COND_END_PEEL]]
35+
; CHECK: cond.end.peel:
36+
; CHECK-NEXT: store i8 62, i8* [[P0]], align 1, !alias.scope !3
37+
; CHECK-NEXT: store i8 63, i8* [[P1]], align 1, !alias.scope !0
38+
; CHECK-NEXT: store i8 64, i8* [[P2]], align 1, !alias.scope !5
39+
; CHECK-NEXT: store i8 62, i8* [[P0]], align 1, !noalias !3
40+
; CHECK-NEXT: store i8 63, i8* [[P1]], align 1, !noalias !0
41+
; CHECK-NEXT: store i8 64, i8* [[P2]], align 1, !noalias !5
42+
; CHECK-NEXT: [[INC_PEEL:%.*]] = add nuw i32 0, 1
43+
; CHECK-NEXT: [[EXITCOND_NOT_PEEL:%.*]] = icmp eq i32 [[INC_PEEL]], undef
44+
; CHECK-NEXT: br i1 [[EXITCOND_NOT_PEEL]], label [[FOR_COND_CLEANUP46:%.*]], label [[FOR_BODY47_PEEL_NEXT:%.*]]
45+
; CHECK: for.body47.peel.next:
46+
; CHECK-NEXT: br label [[FOR_BODY47_PEEL_NEXT1:%.*]]
47+
; CHECK: for.body47.peel.next1:
48+
; CHECK-NEXT: br label [[FOR_BODY47_LR_PH_PEEL_NEWPH:%.*]]
49+
; CHECK: for.body47.lr.ph.peel.newph:
50+
; CHECK-NEXT: br label [[FOR_BODY47:%.*]]
51+
; CHECK: for.cond.cleanup46.loopexit:
52+
; CHECK-NEXT: br label [[FOR_COND_CLEANUP46]]
53+
; CHECK: for.cond.cleanup46:
54+
; CHECK-NEXT: ret void
55+
; CHECK: for.body47:
56+
; CHECK-NEXT: [[J_02:%.*]] = phi i32 [ [[INC_PEEL]], [[FOR_BODY47_LR_PH_PEEL_NEWPH]] ], [ [[INC:%.*]], [[COND_END:%.*]] ]
57+
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata !6)
58+
; CHECK-NEXT: store i8 42, i8* [[P0]], align 1, !alias.scope !6
59+
; CHECK-NEXT: store i8 43, i8* [[P1]], align 1, !alias.scope !0
60+
; CHECK-NEXT: store i8 44, i8* [[P2]], align 1, !alias.scope !8
61+
; CHECK-NEXT: store i8 42, i8* [[P0]], align 1, !noalias !6
62+
; CHECK-NEXT: store i8 43, i8* [[P1]], align 1, !noalias !0
63+
; CHECK-NEXT: store i8 44, i8* [[P2]], align 1, !noalias !8
64+
; CHECK-NEXT: br i1 false, label [[COND_TRUE:%.*]], label [[COND_END]]
65+
; CHECK: cond.true:
66+
; CHECK-NEXT: store i8 52, i8* [[P0]], align 1, !alias.scope !6
67+
; CHECK-NEXT: store i8 53, i8* [[P1]], align 1, !alias.scope !0
68+
; CHECK-NEXT: store i8 54, i8* [[P2]], align 1, !alias.scope !8
69+
; CHECK-NEXT: store i8 52, i8* [[P0]], align 1, !noalias !6
70+
; CHECK-NEXT: store i8 53, i8* [[P1]], align 1, !noalias !0
71+
; CHECK-NEXT: store i8 54, i8* [[P2]], align 1, !noalias !8
72+
; CHECK-NEXT: br label [[COND_END]]
73+
; CHECK: cond.end:
74+
; CHECK-NEXT: store i8 62, i8* [[P0]], align 1, !alias.scope !6
75+
; CHECK-NEXT: store i8 63, i8* [[P1]], align 1, !alias.scope !0
76+
; CHECK-NEXT: store i8 64, i8* [[P2]], align 1, !alias.scope !8
77+
; CHECK-NEXT: store i8 62, i8* [[P0]], align 1, !noalias !6
78+
; CHECK-NEXT: store i8 63, i8* [[P1]], align 1, !noalias !0
79+
; CHECK-NEXT: store i8 64, i8* [[P2]], align 1, !noalias !8
80+
; CHECK-NEXT: [[INC]] = add nuw i32 [[J_02]], 1
81+
; CHECK-NEXT: br i1 undef, label [[FOR_COND_CLEANUP46_LOOPEXIT:%.*]], label [[FOR_BODY47]], [[LOOP9:!llvm.loop !.*]]
82+
;
83+
for.body47.lr.ph:
84+
call void @llvm.experimental.noalias.scope.decl(metadata !5)
85+
br label %for.body47
86+
87+
for.cond.cleanup46: ; preds = %cond.end
88+
ret void
89+
90+
for.body47: ; preds = %cond.end, %for.body47.lr.ph
91+
%j.02 = phi i32 [ 0, %for.body47.lr.ph ], [ %inc, %cond.end ]
92+
call void @llvm.experimental.noalias.scope.decl(metadata !0)
93+
store i8 42, i8* %p0, !alias.scope !0
94+
store i8 43, i8* %p1, !alias.scope !5
95+
store i8 44, i8* %p2, !alias.scope !7
96+
store i8 42, i8* %p0, !noalias !0
97+
store i8 43, i8* %p1, !noalias !5
98+
store i8 44, i8* %p2, !noalias !7
99+
%cmp52 = icmp eq i32 %j.02, 0
100+
br i1 %cmp52, label %cond.true, label %cond.end
101+
102+
cond.true: ; preds = %for.body47
103+
store i8 52, i8* %p0, !alias.scope !0
104+
store i8 53, i8* %p1, !alias.scope !5
105+
store i8 54, i8* %p2, !alias.scope !7
106+
store i8 52, i8* %p0, !noalias !0
107+
store i8 53, i8* %p1, !noalias !5
108+
store i8 54, i8* %p2, !noalias !7
109+
br label %cond.end
110+
111+
cond.end: ; preds = %cond.true, %for.body47
112+
store i8 62, i8* %p0, !alias.scope !0
113+
store i8 63, i8* %p1, !alias.scope !5
114+
store i8 64, i8* %p2, !alias.scope !7
115+
store i8 62, i8* %p0, !noalias !0
116+
store i8 63, i8* %p1, !noalias !5
117+
store i8 64, i8* %p2, !noalias !7
118+
%inc = add nuw i32 %j.02, 1
119+
%exitcond.not = icmp eq i32 %inc, undef
120+
br i1 %exitcond.not, label %for.cond.cleanup46, label %for.body47, !llvm.loop !3
121+
}
122+
123+
; Function Attrs: inaccessiblememonly nofree nosync nounwind willreturn
124+
declare void @llvm.experimental.noalias.scope.decl(metadata) #0
125+
126+
attributes #0 = { inaccessiblememonly nofree nosync nounwind willreturn }
127+
128+
!0 = !{!1}
129+
!1 = distinct !{!1, !2, !"foo: %inner.result"}
130+
!2 = distinct !{!2, !"foo"}
131+
!3 = distinct !{!3, !4}
132+
!4 = !{!"llvm.loop.mustprogress"}
133+
!5 = !{!6}
134+
!6 = distinct !{!6, !2, !"foo: %outer.result"}
135+
!7 = !{!1, !6}
136+
137+
; CHECK: !0 = !{!1}
138+
; CHECK: !1 = distinct !{!1, !2, !"foo: %outer.result"}
139+
; CHECK: !2 = distinct !{!2, !"foo"}
140+
; CHECK: !3 = !{!4}
141+
; CHECK: !4 = distinct !{!4, !2, !"foo: %inner.result:Peel0"}
142+
; CHECK: !5 = !{!4, !1}
143+
; CHECK: !6 = !{!7}
144+
; CHECK: !7 = distinct !{!7, !2, !"foo: %inner.result"}
145+
; CHECK: !8 = !{!7, !1}
146+
; CHECK: !9 = distinct !{!9, !10, !11, !12}
147+
; CHECK: !10 = !{!"llvm.loop.mustprogress"}
148+
; CHECK: !11 = !{!"llvm.loop.peeled.count", i32 1}
149+
; CHECK: !12 = !{!"llvm.loop.unroll.disable"}

0 commit comments

Comments
 (0)