Skip to content

Commit 81a4643

Browse files
committed
LoopInfo: introduce Loop::getLocStr; unify debug output
Introduce a Loop::getLocStr stolen from LoopVectorize's static function getDebugLocString in order to have uniform debug output headers across LoopVectorize, LoopAccessAnalysis, and LoopDistribute. The motivation for this change is to have UpdateTestChecks recognize the headers and automatically generate CHECK lines for debug output, with minimal special-casing.
1 parent 55e5842 commit 81a4643

File tree

10 files changed

+90
-46
lines changed

10 files changed

+90
-46
lines changed

llvm/include/llvm/Analysis/LoopInfo.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ class LLVM_EXTERNAL_VISIBILITY Loop : public LoopBase<BasicBlock, Loop> {
385385
/// Return the source code span of the loop.
386386
LocRange getLocRange() const;
387387

388+
/// Return a string containing the location of the loop (file name + line
389+
/// number if present, otherwise module name). Meant to be used for debug
390+
/// printing within LLVM_DEBUG.
391+
std::string getLocStr() const;
392+
388393
StringRef getName() const {
389394
if (BasicBlock *Header = getHeader())
390395
if (Header->hasName())
@@ -690,7 +695,6 @@ llvm::MDNode *
690695
makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
691696
llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
692697
llvm::ArrayRef<llvm::MDNode *> AddAttrs);
693-
694698
} // namespace llvm
695699

696700
#endif

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,9 +2374,9 @@ void MemoryDepChecker::Dependence::print(
23742374

23752375
bool LoopAccessInfo::canAnalyzeLoop() {
23762376
// We need to have a loop header.
2377-
LLVM_DEBUG(dbgs() << "LAA: Found a loop in "
2378-
<< TheLoop->getHeader()->getParent()->getName() << ": "
2379-
<< TheLoop->getHeader()->getName() << '\n');
2377+
LLVM_DEBUG(dbgs() << "\nLAA: Checking a loop in '"
2378+
<< TheLoop->getHeader()->getParent()->getName() << "' from "
2379+
<< TheLoop->getLocStr() << "\n");
23802380

23812381
// We can only analyze innermost loops.
23822382
if (!TheLoop->isInnermost()) {
@@ -2403,6 +2403,8 @@ bool LoopAccessInfo::canAnalyzeLoop() {
24032403
return false;
24042404
}
24052405

2406+
LLVM_DEBUG(dbgs() << "LAA: Found a loop: " << TheLoop->getHeader()->getName()
2407+
<< "\n");
24062408
return true;
24072409
}
24082410

llvm/lib/Analysis/LoopInfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,17 @@ Loop::LocRange Loop::getLocRange() const {
663663
return LocRange();
664664
}
665665

666+
std::string Loop::getLocStr() const {
667+
std::string Result;
668+
raw_string_ostream OS(Result);
669+
if (const DebugLoc LoopDbgLoc = getStartLoc())
670+
LoopDbgLoc.print(OS);
671+
else
672+
// Just print the module name.
673+
OS << getHeader()->getParent()->getParent()->getModuleIdentifier();
674+
return Result;
675+
}
676+
666677
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
667678
LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
668679

llvm/lib/Transforms/Scalar/LoopDistribute.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,9 @@ class LoopDistributeForLoop {
659659
bool processLoop() {
660660
assert(L->isInnermost() && "Only process inner loops.");
661661

662-
LLVM_DEBUG(dbgs() << "\nLDist: In \""
663-
<< L->getHeader()->getParent()->getName()
664-
<< "\" checking " << *L << "\n");
662+
LLVM_DEBUG(dbgs() << "\nLDist: Checking a loop in '"
663+
<< L->getHeader()->getParent()->getName() << "' from "
664+
<< L->getLocStr() << "\n");
665665

666666
// Having a single exit block implies there's also one exiting block.
667667
if (!L->getExitBlock())
@@ -686,6 +686,9 @@ class LoopDistributeForLoop {
686686
if (!Dependences || Dependences->empty())
687687
return fail("NoUnsafeDeps", "no unsafe dependences to isolate");
688688

689+
LLVM_DEBUG(dbgs() << "LDist: Found a loop: " << L->getHeader()->getName()
690+
<< "\n");
691+
689692
InstPartitionContainer Partitions(L, LI, DT);
690693

691694
// First, go through each memory operation and assign them to consecutive

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,23 +1027,6 @@ static void reportVectorization(OptimizationRemarkEmitter *ORE, Loop *TheLoop,
10271027

10281028
} // end namespace llvm
10291029

1030-
#ifndef NDEBUG
1031-
/// \return string containing a file name and a line # for the given loop.
1032-
static std::string getDebugLocString(const Loop *L) {
1033-
std::string Result;
1034-
if (L) {
1035-
raw_string_ostream OS(Result);
1036-
if (const DebugLoc LoopDbgLoc = L->getStartLoc())
1037-
LoopDbgLoc.print(OS);
1038-
else
1039-
// Just print the module name.
1040-
OS << L->getHeader()->getParent()->getParent()->getModuleIdentifier();
1041-
OS.flush();
1042-
}
1043-
return Result;
1044-
}
1045-
#endif
1046-
10471030
namespace llvm {
10481031

10491032
// Loop vectorization cost-model hints how the scalar epilogue loop should be
@@ -9836,13 +9819,9 @@ bool LoopVectorizePass::processLoop(Loop *L) {
98369819
assert((EnableVPlanNativePath || L->isInnermost()) &&
98379820
"VPlan-native path is not enabled. Only process inner loops.");
98389821

9839-
#ifndef NDEBUG
9840-
const std::string DebugLocStr = getDebugLocString(L);
9841-
#endif /* NDEBUG */
9842-
98439822
LLVM_DEBUG(dbgs() << "\nLV: Checking a loop in '"
98449823
<< L->getHeader()->getParent()->getName() << "' from "
9845-
<< DebugLocStr << "\n");
9824+
<< L->getLocStr() << "\n");
98469825

98479826
LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE, TTI);
98489827

@@ -10112,15 +10091,15 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1011210091
});
1011310092
} else if (VectorizeLoop && !InterleaveLoop) {
1011410093
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10115-
<< ") in " << DebugLocStr << '\n');
10094+
<< ") in " << L->getLocStr() << '\n');
1011610095
ORE->emit([&]() {
1011710096
return OptimizationRemarkAnalysis(LV_NAME, IntDiagMsg.first,
1011810097
L->getStartLoc(), L->getHeader())
1011910098
<< IntDiagMsg.second;
1012010099
});
1012110100
} else if (VectorizeLoop && InterleaveLoop) {
1012210101
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10123-
<< ") in " << DebugLocStr << '\n');
10102+
<< ") in " << L->getLocStr() << '\n');
1012410103
LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
1012510104
}
1012610105

llvm/test/Analysis/LoopAccessAnalysis/print-order.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
; A[i+1] = A[i] + 1;
77
; }
88

9-
; CHECK: LAA: Found a loop in negative_step: loop
9+
; CHECK-LABEL: 'negative_step'
10+
; CHECK: LAA: Found a loop: loop
1011
; CHECK: LAA: Checking memory dependencies
1112
; CHECK-NEXT: LAA: Src Scev: {(4092 + %A),+,-4}<nw><%loop>Sink Scev: {(4088 + %A)<nuw>,+,-4}<nw><%loop>(Induction step: -1)
1213
; CHECK-NEXT: LAA: Distance for store i32 %add, ptr %gep.A.plus.1, align 4 to %l = load i32, ptr %gep.A, align 4: -4
@@ -37,7 +38,8 @@ exit:
3738
; A[i-1] = A[i] + 1;
3839
; }
3940

40-
; CHECK: LAA: Found a loop in positive_step: loop
41+
; CHECK-LABEL: 'positive_step'
42+
; CHECK: LAA: Found a loop: loop
4143
; CHECK: LAA: Checking memory dependencies
4244
; CHECK-NEXT: LAA: Src Scev: {(4 + %A)<nuw>,+,4}<nuw><%loop>Sink Scev: {%A,+,4}<nw><%loop>(Induction step: 1)
4345
; CHECK-NEXT: LAA: Distance for %l = load i32, ptr %gep.A, align 4 to store i32 %add, ptr %gep.A.minus.1, align 4: -4

llvm/test/Analysis/LoopAccessAnalysis/scalable-vector-regression-tests.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ loop.end:
2424
ret void
2525
}
2626

27-
; CHECK-LABEL: LAA: Found a loop in regression_test_loop_access_scalable_typesize
27+
; CHECK-LABEL: 'regression_test_loop_access_scalable_typesize'
2828
; CHECK: LAA: Bad stride - Scalable object:
2929
define void @regression_test_loop_access_scalable_typesize(ptr %input_ptr) {
3030
entry:
@@ -42,7 +42,7 @@ end:
4242
ret void
4343
}
4444

45-
; CHECK-LABEL: LAA: Found a loop in regression_test_loop_access_scalable_typesize_nonscalable_object
45+
; CHECK-LABEL: 'regression_test_loop_access_scalable_typesize_nonscalable_object'
4646
; CHECK: LAA: Bad stride - Scalable object:
4747
define void @regression_test_loop_access_scalable_typesize_nonscalable_object(ptr %input_ptr) {
4848
entry:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; RUN: opt -passes=loop-distribute -enable-loop-distribute \
2+
; RUN: -debug-only=loop-distribute -disable-output 2>&1 %s | FileCheck %s
3+
4+
define void @f(ptr noalias %a, ptr noalias %b, ptr noalias %c, ptr noalias %d, i64 %stride) {
5+
; CHECK-LABEL: 'f'
6+
; CHECK: LDist: Found a loop: for.body
7+
; CHECK: Backward dependences:
8+
; CHECK-NEXT: Backward:
9+
; CHECK-NEXT: %load.a = load i32, ptr %gep.a, align 4 ->
10+
; CHECK-NEXT: store i32 %mul.a, ptr %gep.a.plus4, align 4
11+
; CHECK: Seeded partitions:
12+
; CHECK: Partition 0
13+
; CHECK: Partition 1
14+
; CHECK: Partition 2
15+
; CHECK: Partition 3
16+
; CHECK: Distributing loop
17+
entry:
18+
br label %for.body
19+
20+
for.body: ; preds = %for.body, %entry
21+
%ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
22+
%gep.a = getelementptr inbounds i32, ptr %a, i64 %ind
23+
%load.a = load i32, ptr %gep.a, align 4
24+
%gep.b = getelementptr inbounds i32, ptr %b, i64 %ind
25+
%load.b = load i32, ptr %gep.b, align 4
26+
%mul.a = mul i32 %load.b, %load.a
27+
%add = add nuw nsw i64 %ind, 1
28+
%gep.a.plus4 = getelementptr inbounds i32, ptr %a, i64 %add
29+
store i32 %mul.a, ptr %gep.a.plus4, align 4
30+
%gep.d = getelementptr inbounds i32, ptr %d, i64 %ind
31+
%loadD = load i32, ptr %gep.d, align 4
32+
%mul = mul i64 %ind, %stride
33+
%gep.strided.a = getelementptr inbounds i32, ptr %a, i64 %mul
34+
%load.strided.a = load i32, ptr %gep.strided.a, align 4
35+
%mul.c = mul i32 %loadD, %load.strided.a
36+
%gep.c = getelementptr inbounds i32, ptr %c, i64 %ind
37+
store i32 %mul.c, ptr %gep.c, align 4
38+
%exitcond = icmp eq i64 %add, 20
39+
br i1 %exitcond, label %exit, label %for.body
40+
41+
exit: ; preds = %for.body
42+
ret void
43+
}

llvm/test/Transforms/LoopVectorize/ARM/mve-hoist-runtime-checks.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ target triple = "thumbv8.1m.main-none-unknown-eabi"
1818
; NOTE: The strides of the starting address values in the inner loop differ, i.e.
1919
; '(i * (n + 1))' vs '(i * n)'.
2020

21-
; DEBUG-LABEL: LAA: Found a loop in diff_checks:
21+
; DEBUG-LABEL: 'diff_checks'
2222
; DEBUG: LAA: Not creating diff runtime check, since these cannot be hoisted out of the outer loop
2323
; DEBUG: LAA: Adding RT check for range:
2424
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting

llvm/test/Transforms/LoopVectorize/runtime-checks-hoist.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
1717
; NOTE: The strides of the starting address values in the inner loop differ, i.e.
1818
; '(i * (n + 1))' vs '(i * n)'.
1919

20-
; DEBUG-LABEL: LAA: Found a loop in diff_checks:
20+
; DEBUG-LABEL: 'diff_checks'
2121
; DEBUG: LAA: Not creating diff runtime check, since these cannot be hoisted out of the outer loop
2222
; DEBUG: LAA: Adding RT check for range:
2323
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
@@ -149,7 +149,7 @@ outer.exit:
149149
; We decide to do full runtime checks here (as opposed to diff checks) due to
150150
; the additional load of 'dst[(i * n) + j]' in the loop.
151151

152-
; DEBUG-LABEL: LAA: Found a loop in full_checks:
152+
; DEBUG-LABEL: 'full_checks'
153153
; DEBUG-NOT: LAA: Creating diff runtime check for:
154154
; DEBUG: LAA: Adding RT check for range:
155155
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
@@ -272,7 +272,7 @@ outer.exit:
272272
; is accessed with a higher stride compared src, and therefore the inner loop
273273
; runtime checks will vary for each outer loop iteration.
274274

275-
; DEBUG-LABEL: LAA: Found a loop in full_checks_diff_strides:
275+
; DEBUG-LABEL: 'full_checks_diff_strides'
276276
; DEBUG-NOT: LAA: Creating diff runtime check for:
277277
; DEBUG: LAA: Adding RT check for range:
278278
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
@@ -402,7 +402,7 @@ outer.exit:
402402
; }
403403
; }
404404

405-
; DEBUG-LABEL: LAA: Found a loop in diff_checks_src_start_invariant:
405+
; DEBUG-LABEL: 'diff_checks_src_start_invariant'
406406
; DEBUG-NOT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
407407

408408
define void @diff_checks_src_start_invariant(ptr nocapture noundef writeonly %dst, ptr nocapture noundef readonly %src, i32 noundef %m, i32 noundef %n) {
@@ -508,7 +508,7 @@ outer.loop.exit:
508508
; }
509509
; }
510510

511-
; DEBUG-LABEL: LAA: Found a loop in full_checks_src_start_invariant:
511+
; DEBUG-LABEL: 'full_checks_src_start_invariant'
512512
; DEBUG: LAA: Adding RT check for range:
513513
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
514514
; DEBUG-NEXT: Start: %dst End: ((4 * (zext i32 %m to i64) * (zext i32 %n to i64)) + %dst)
@@ -629,7 +629,7 @@ outer.loop.exit:
629629
; The 'src' access varies with the outermost loop, rather than the parent of the
630630
; innermost loop. Hence we don't expand `src`, although in theory we could do.
631631

632-
; DEBUG-LABEL: LAA: Found a loop in triple_nested_loop_mixed_access:
632+
; DEBUG-LABEL: 'triple_nested_loop_mixed_access'
633633
; DEBUG-NOT: LAA: Creating diff runtime check for:
634634
; DEBUG: LAA: Adding RT check for range:
635635
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
@@ -795,7 +795,7 @@ exit:
795795
; }
796796
; Outer loop trip count is uncomputable so we shouldn't expand the ranges.
797797

798-
; DEBUG-LABEL: LAA: Found a loop in uncomputable_outer_tc:
798+
; DEBUG-LABEL: 'uncomputable_outer_tc'
799799
; DEBUG: LAA: Adding RT check for range:
800800
; DEBUG-NEXT: Start: {%dst,+,(4 * (zext i32 (1 + %n) to i64))<nuw><nsw>}<%outer.loop> End: {((4 * (zext i32 %n to i64))<nuw><nsw> + %dst),+,(4 * (zext i32 (1 + %n) to i64))<nuw><nsw>}<%outer.loop>
801801
; DEBUG-NEXT: LAA: Adding RT check for range:
@@ -945,7 +945,7 @@ while.end:
945945
; Inner IV is decreasing, but this isn't a problem and we can still expand the
946946
; runtime checks correctly to cover the whole loop.
947947

948-
; DEBUG-LABEL: LAA: Found a loop in decreasing_inner_iv:
948+
; DEBUG-LABEL: 'decreasing_inner_iv'
949949
; DEBUG: LAA: Adding RT check for range:
950950
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
951951
; DEBUG-NEXT: LAA: ... but need to check stride is positive: (4 * (sext i32 %stride1 to i64))<nsw>
@@ -1111,7 +1111,7 @@ exit:
11111111
; Outer IV is decreasing, but the direction of memory accesses also depends
11121112
; upon the signedness of stride1.
11131113

1114-
; DEBUG-LABEL: LAA: Found a loop in decreasing_outer_iv:
1114+
; DEBUG-LABEL: 'decreasing_outer_iv'
11151115
; DEBUG: LAA: Adding RT check for range:
11161116
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
11171117
; DEBUG-NEXT: LAA: ... but need to check stride is positive: (-4 * (sext i32 %stride1 to i64))<nsw>
@@ -1271,7 +1271,7 @@ exit:
12711271
; }
12721272

12731273

1274-
; DEBUG-LABEL: LAA: Found a loop in unknown_inner_stride:
1274+
; DEBUG-LABEL: 'unknown_inner_stride'
12751275
; DEBUG: LAA: Adding RT check for range:
12761276
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
12771277
; DEBUG-NEXT: Start: %dst End: ((4 * (zext i32 %n to i64))<nuw><nsw> + (4 * (zext i32 (1 + %n) to i64) * (-1 + (zext i32 %m to i64))<nsw>) + %dst)

0 commit comments

Comments
 (0)