Skip to content

Commit f004841

Browse files
artagnonAlexisPerry
authored andcommitted
LoopInfo: introduce Loop::getLocStr; unify debug output (llvm#93051)
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 3afe536 commit f004841

File tree

12 files changed

+181
-46
lines changed

12 files changed

+181
-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 debug location of the loop (file name +
389+
/// line 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())
@@ -693,7 +698,6 @@ llvm::MDNode *
693698
makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
694699
llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
695700
llvm::ArrayRef<llvm::MDNode *> AddAttrs);
696-
697701
} // namespace llvm
698702

699703
#endif

llvm/lib/Analysis/LoopAccessAnalysis.cpp

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

23562356
bool LoopAccessInfo::canAnalyzeLoop() {
23572357
// We need to have a loop header.
2358-
LLVM_DEBUG(dbgs() << "LAA: Found a loop in "
2359-
<< TheLoop->getHeader()->getParent()->getName() << ": "
2360-
<< TheLoop->getHeader()->getName() << '\n');
2358+
LLVM_DEBUG(dbgs() << "\nLAA: Checking a loop in '"
2359+
<< TheLoop->getHeader()->getParent()->getName() << "' from "
2360+
<< TheLoop->getLocStr() << "\n");
23612361

23622362
// We can only analyze innermost loops.
23632363
if (!TheLoop->isInnermost()) {
@@ -2386,6 +2386,8 @@ bool LoopAccessInfo::canAnalyzeLoop() {
23862386
return false;
23872387
}
23882388

2389+
LLVM_DEBUG(dbgs() << "LAA: Found an analyzable loop: "
2390+
<< TheLoop->getHeader()->getName() << "\n");
23892391
return true;
23902392
}
23912393

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 candidate loop: "
690+
<< L->getHeader()->getName() << "\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
@@ -9758,13 +9741,9 @@ bool LoopVectorizePass::processLoop(Loop *L) {
97589741
assert((EnableVPlanNativePath || L->isInnermost()) &&
97599742
"VPlan-native path is not enabled. Only process inner loops.");
97609743

9761-
#ifndef NDEBUG
9762-
const std::string DebugLocStr = getDebugLocString(L);
9763-
#endif /* NDEBUG */
9764-
97659744
LLVM_DEBUG(dbgs() << "\nLV: Checking a loop in '"
97669745
<< L->getHeader()->getParent()->getName() << "' from "
9767-
<< DebugLocStr << "\n");
9746+
<< L->getLocStr() << "\n");
97689747

97699748
LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE, TTI);
97709749

@@ -10034,15 +10013,15 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1003410013
});
1003510014
} else if (VectorizeLoop && !InterleaveLoop) {
1003610015
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10037-
<< ") in " << DebugLocStr << '\n');
10016+
<< ") in " << L->getLocStr() << '\n');
1003810017
ORE->emit([&]() {
1003910018
return OptimizationRemarkAnalysis(LV_NAME, IntDiagMsg.first,
1004010019
L->getStartLoc(), L->getHeader())
1004110020
<< IntDiagMsg.second;
1004210021
});
1004310022
} else if (VectorizeLoop && InterleaveLoop) {
1004410023
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10045-
<< ") in " << DebugLocStr << '\n');
10024+
<< ") in " << L->getLocStr() << '\n');
1004610025
LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
1004710026
}
1004810027

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -passes='print<access-info>' -debug-only=loop-accesses \
3+
; RUN: -disable-output -S %s 2>&1 | FileCheck %s
4+
5+
define void @negative_step(ptr nocapture %A) {
6+
; CHECK-LABEL: LAA: Checking a loop in 'negative_step' from negative_step.c:5:2
7+
entry:
8+
%A.plus.1 = getelementptr i32, ptr %A, i64 1
9+
br label %loop
10+
11+
loop:
12+
%iv = phi i64 [ 1022, %entry ], [ %iv.next, %loop ]
13+
%gep.A = getelementptr inbounds i32, ptr %A, i64 %iv
14+
%l = load i32, ptr %gep.A, align 4
15+
%add = add nsw i32 %l, 1
16+
%gep.A.plus.1 = getelementptr i32, ptr %A.plus.1, i64 %iv
17+
store i32 %add, ptr %gep.A.plus.1, align 4
18+
%iv.next = add nsw i64 %iv, -1
19+
%cmp.not = icmp eq i64 %iv, 0
20+
br i1 %cmp.not, label %exit, label %loop, !dbg !2
21+
22+
exit:
23+
ret void
24+
}
25+
26+
!llvm.module.flags = !{!5, !6, !7}
27+
28+
!0 = !DIFile(filename: "negative_step.c", directory: "/")
29+
!1 = distinct !DISubprogram(name: "negative_step", scope: !0, file: !0, unit: !4)
30+
!2 = !DILocation(line: 5, column: 2, scope: !1)
31+
!4 = distinct !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang")
32+
!5 = !{i32 1, !"Debug Info Version", i32 3}
33+
!6 = !{i32 2, !"Dwarf Version", i32 2}
34+
!7 = !{i32 1, !"PIC Level", i32 2}

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 an analyzable 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 an analyzable 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ 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'
28+
; CHECK: LAA: Found an analyzable loop: vector.body
2829
; CHECK: LAA: Bad stride - Scalable object:
2930
define void @regression_test_loop_access_scalable_typesize(ptr %input_ptr) {
3031
entry:
@@ -42,7 +43,8 @@ end:
4243
ret void
4344
}
4445

45-
; CHECK-LABEL: LAA: Found a loop in regression_test_loop_access_scalable_typesize_nonscalable_object
46+
; CHECK-LABEL: 'regression_test_loop_access_scalable_typesize_nonscalable_object'
47+
; CHECK: LAA: Found an analyzable loop: vector.body
4648
; CHECK: LAA: Bad stride - Scalable object:
4749
define void @regression_test_loop_access_scalable_typesize_nonscalable_object(ptr %input_ptr) {
4850
entry:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -passes=loop-distribute -enable-loop-distribute \
3+
; RUN: -debug-only=loop-distribute -disable-output 2>&1 %s | FileCheck %s
4+
5+
define void @f(ptr noalias %a, ptr noalias %b, ptr noalias %c, ptr noalias %d, i64 %stride) {
6+
; CHECK-LABEL: LDist: Checking a loop in 'f' from f.c:5:2
7+
entry:
8+
br label %for.body
9+
10+
for.body: ; preds = %for.body, %entry
11+
%ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
12+
%gep.a = getelementptr inbounds i32, ptr %a, i64 %ind
13+
%load.a = load i32, ptr %gep.a, align 4
14+
%gep.b = getelementptr inbounds i32, ptr %b, i64 %ind
15+
%load.b = load i32, ptr %gep.b, align 4
16+
%mul.a = mul i32 %load.b, %load.a
17+
%add = add nuw nsw i64 %ind, 1
18+
%gep.a.plus4 = getelementptr inbounds i32, ptr %a, i64 %add
19+
store i32 %mul.a, ptr %gep.a.plus4, align 4
20+
%gep.d = getelementptr inbounds i32, ptr %d, i64 %ind
21+
%loadD = load i32, ptr %gep.d, align 4
22+
%mul = mul i64 %ind, %stride
23+
%gep.strided.a = getelementptr inbounds i32, ptr %a, i64 %mul
24+
%load.strided.a = load i32, ptr %gep.strided.a, align 4
25+
%mul.c = mul i32 %loadD, %load.strided.a
26+
%gep.c = getelementptr inbounds i32, ptr %c, i64 %ind
27+
store i32 %mul.c, ptr %gep.c, align 4
28+
%exitcond = icmp eq i64 %add, 20
29+
br i1 %exitcond, label %exit, label %for.body, !dbg !2
30+
31+
exit: ; preds = %for.body
32+
ret void
33+
}
34+
35+
!llvm.module.flags = !{!5, !6, !7}
36+
37+
!0 = !DIFile(filename: "f.c", directory: "/")
38+
!1 = distinct !DISubprogram(name: "f", scope: !0, file: !0, unit: !4)
39+
!2 = !DILocation(line: 5, column: 2, scope: !1)
40+
!4 = distinct !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang")
41+
!5 = !{i32 1, !"Debug Info Version", i32 3}
42+
!6 = !{i32 2, !"Dwarf Version", i32 2}
43+
!7 = !{i32 1, !"PIC Level", i32 2}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -passes=loop-distribute -enable-loop-distribute \
3+
; RUN: -debug-only=loop-distribute -disable-output 2>&1 %s | FileCheck %s
4+
5+
define void @f(ptr noalias %a, ptr noalias %b, ptr noalias %c, ptr noalias %d, i64 %stride) {
6+
; CHECK-LABEL: 'f'
7+
; CHECK: LDist: Found a candidate loop: for.body
8+
; CHECK: Backward dependences:
9+
; CHECK-NEXT: Backward:
10+
; CHECK-NEXT: %load.a = load i32, ptr %gep.a, align 4 ->
11+
; CHECK-NEXT: store i32 %mul.a, ptr %gep.a.plus4, align 4
12+
; CHECK: Seeded partitions:
13+
; CHECK: Partition 0
14+
; CHECK: Partition 1
15+
; CHECK: Partition 2
16+
; CHECK: Partition 3
17+
; CHECK: Distributing loop
18+
entry:
19+
br label %for.body
20+
21+
for.body: ; preds = %for.body, %entry
22+
%ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
23+
%gep.a = getelementptr inbounds i32, ptr %a, i64 %ind
24+
%load.a = load i32, ptr %gep.a, align 4
25+
%gep.b = getelementptr inbounds i32, ptr %b, i64 %ind
26+
%load.b = load i32, ptr %gep.b, align 4
27+
%mul.a = mul i32 %load.b, %load.a
28+
%add = add nuw nsw i64 %ind, 1
29+
%gep.a.plus4 = getelementptr inbounds i32, ptr %a, i64 %add
30+
store i32 %mul.a, ptr %gep.a.plus4, align 4
31+
%gep.d = getelementptr inbounds i32, ptr %d, i64 %ind
32+
%loadD = load i32, ptr %gep.d, align 4
33+
%mul = mul i64 %ind, %stride
34+
%gep.strided.a = getelementptr inbounds i32, ptr %a, i64 %mul
35+
%load.strided.a = load i32, ptr %gep.strided.a, align 4
36+
%mul.c = mul i32 %loadD, %load.strided.a
37+
%gep.c = getelementptr inbounds i32, ptr %c, i64 %ind
38+
store i32 %mul.c, ptr %gep.c, align 4
39+
%exitcond = icmp eq i64 %add, 20
40+
br i1 %exitcond, label %exit, label %for.body
41+
42+
exit: ; preds = %for.body
43+
ret void
44+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ 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'
22+
; DEBUG: LAA: Found an analyzable loop: inner.loop
2223
; DEBUG: LAA: Not creating diff runtime check, since these cannot be hoisted out of the outer loop
2324
; DEBUG: LAA: Adding RT check for range:
2425
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting

0 commit comments

Comments
 (0)