Skip to content

LoopInfo: introduce Loop::getLocStr; unify debug output #93051

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 4 commits into from
Jun 25, 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
6 changes: 5 additions & 1 deletion llvm/include/llvm/Analysis/LoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ class LLVM_EXTERNAL_VISIBILITY Loop : public LoopBase<BasicBlock, Loop> {
/// Return the source code span of the loop.
LocRange getLocRange() const;

/// Return a string containing the debug location of the loop (file name +
/// line number if present, otherwise module name). Meant to be used for debug
/// printing within LLVM_DEBUG.
std::string getLocStr() const;

StringRef getName() const {
if (BasicBlock *Header = getHeader())
if (Header->hasName())
Expand Down Expand Up @@ -690,7 +695,6 @@ llvm::MDNode *
makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID,
llvm::ArrayRef<llvm::StringRef> RemovePrefixes,
llvm::ArrayRef<llvm::MDNode *> AddAttrs);

} // namespace llvm

#endif
8 changes: 5 additions & 3 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,9 +2374,9 @@ void MemoryDepChecker::Dependence::print(

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

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

LLVM_DEBUG(dbgs() << "LAA: Found an analyzable loop: "
<< TheLoop->getHeader()->getName() << "\n");
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Analysis/LoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,17 @@ Loop::LocRange Loop::getLocRange() const {
return LocRange();
}

std::string Loop::getLocStr() const {
std::string Result;
raw_string_ostream OS(Result);
if (const DebugLoc LoopDbgLoc = getStartLoc())
LoopDbgLoc.print(OS);
else
// Just print the module name.
OS << getHeader()->getParent()->getParent()->getModuleIdentifier();
return Result;
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }

Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Transforms/Scalar/LoopDistribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,9 @@ class LoopDistributeForLoop {
bool processLoop() {
assert(L->isInnermost() && "Only process inner loops.");

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

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

LLVM_DEBUG(dbgs() << "LDist: Found a candidate loop: "
<< L->getHeader()->getName() << "\n");

InstPartitionContainer Partitions(L, LI, DT);

// First, go through each memory operation and assign them to consecutive
Expand Down
27 changes: 3 additions & 24 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,23 +1027,6 @@ static void reportVectorization(OptimizationRemarkEmitter *ORE, Loop *TheLoop,

} // end namespace llvm

#ifndef NDEBUG
/// \return string containing a file name and a line # for the given loop.
static std::string getDebugLocString(const Loop *L) {
std::string Result;
if (L) {
raw_string_ostream OS(Result);
if (const DebugLoc LoopDbgLoc = L->getStartLoc())
LoopDbgLoc.print(OS);
else
// Just print the module name.
OS << L->getHeader()->getParent()->getParent()->getModuleIdentifier();
OS.flush();
}
return Result;
}
#endif

namespace llvm {

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

#ifndef NDEBUG
const std::string DebugLocStr = getDebugLocString(L);
#endif /* NDEBUG */

LLVM_DEBUG(dbgs() << "\nLV: Checking a loop in '"
<< L->getHeader()->getParent()->getName() << "' from "
<< DebugLocStr << "\n");
<< L->getLocStr() << "\n");

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

Expand Down Expand Up @@ -10112,15 +10091,15 @@ bool LoopVectorizePass::processLoop(Loop *L) {
});
} else if (VectorizeLoop && !InterleaveLoop) {
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
<< ") in " << DebugLocStr << '\n');
<< ") in " << L->getLocStr() << '\n');
ORE->emit([&]() {
return OptimizationRemarkAnalysis(LV_NAME, IntDiagMsg.first,
L->getStartLoc(), L->getHeader())
<< IntDiagMsg.second;
});
} else if (VectorizeLoop && InterleaveLoop) {
LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
<< ") in " << DebugLocStr << '\n');
<< ") in " << L->getLocStr() << '\n');
LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
}

Expand Down
34 changes: 34 additions & 0 deletions llvm/test/Analysis/LoopAccessAnalysis/debug-loc.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; REQUIRES: asserts
; RUN: opt -passes='print<access-info>' -debug-only=loop-accesses \
; RUN: -disable-output -S %s 2>&1 | FileCheck %s

define void @negative_step(ptr nocapture %A) {
; CHECK-LABEL: LAA: Checking a loop in 'negative_step' from negative_step.c:5:2
entry:
%A.plus.1 = getelementptr i32, ptr %A, i64 1
br label %loop

loop:
%iv = phi i64 [ 1022, %entry ], [ %iv.next, %loop ]
%gep.A = getelementptr inbounds i32, ptr %A, i64 %iv
%l = load i32, ptr %gep.A, align 4
%add = add nsw i32 %l, 1
%gep.A.plus.1 = getelementptr i32, ptr %A.plus.1, i64 %iv
store i32 %add, ptr %gep.A.plus.1, align 4
%iv.next = add nsw i64 %iv, -1
%cmp.not = icmp eq i64 %iv, 0
br i1 %cmp.not, label %exit, label %loop, !dbg !2

exit:
ret void
}

!llvm.module.flags = !{!5, !6, !7}

!0 = !DIFile(filename: "negative_step.c", directory: "/")
!1 = distinct !DISubprogram(name: "negative_step", scope: !0, file: !0, unit: !4)
!2 = !DILocation(line: 5, column: 2, scope: !1)
!4 = distinct !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang")
!5 = !{i32 1, !"Debug Info Version", i32 3}
!6 = !{i32 2, !"Dwarf Version", i32 2}
!7 = !{i32 1, !"PIC Level", i32 2}
6 changes: 4 additions & 2 deletions llvm/test/Analysis/LoopAccessAnalysis/print-order.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
; A[i+1] = A[i] + 1;
; }

; CHECK: LAA: Found a loop in negative_step: loop
; CHECK-LABEL: 'negative_step'
; CHECK: LAA: Found an analyzable loop: loop
; CHECK: LAA: Checking memory dependencies
; CHECK-NEXT: LAA: Src Scev: {(4092 + %A),+,-4}<nw><%loop>Sink Scev: {(4088 + %A)<nuw>,+,-4}<nw><%loop>(Induction step: -1)
; 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
Expand Down Expand Up @@ -37,7 +38,8 @@ exit:
; A[i-1] = A[i] + 1;
; }

; CHECK: LAA: Found a loop in positive_step: loop
; CHECK-LABEL: 'positive_step'
; CHECK: LAA: Found an analyzable loop: loop
; CHECK: LAA: Checking memory dependencies
; CHECK-NEXT: LAA: Src Scev: {(4 + %A)<nuw>,+,4}<nuw><%loop>Sink Scev: {%A,+,4}<nw><%loop>(Induction step: 1)
; 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ loop.end:
ret void
}

; CHECK-LABEL: LAA: Found a loop in regression_test_loop_access_scalable_typesize
; CHECK-LABEL: 'regression_test_loop_access_scalable_typesize'
; CHECK: LAA: Found an analyzable loop: vector.body
; CHECK: LAA: Bad stride - Scalable object:
define void @regression_test_loop_access_scalable_typesize(ptr %input_ptr) {
entry:
Expand All @@ -42,7 +43,8 @@ end:
ret void
}

; CHECK-LABEL: LAA: Found a loop in regression_test_loop_access_scalable_typesize_nonscalable_object
; CHECK-LABEL: 'regression_test_loop_access_scalable_typesize_nonscalable_object'
; CHECK: LAA: Found an analyzable loop: vector.body
; CHECK: LAA: Bad stride - Scalable object:
define void @regression_test_loop_access_scalable_typesize_nonscalable_object(ptr %input_ptr) {
entry:
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/Transforms/LoopDistribute/debug-loc.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; REQUIRES: asserts
; RUN: opt -passes=loop-distribute -enable-loop-distribute \
; RUN: -debug-only=loop-distribute -disable-output 2>&1 %s | FileCheck %s

define void @f(ptr noalias %a, ptr noalias %b, ptr noalias %c, ptr noalias %d, i64 %stride) {
; CHECK-LABEL: LDist: Checking a loop in 'f' from f.c:5:2
entry:
br label %for.body

for.body: ; preds = %for.body, %entry
%ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
%gep.a = getelementptr inbounds i32, ptr %a, i64 %ind
%load.a = load i32, ptr %gep.a, align 4
%gep.b = getelementptr inbounds i32, ptr %b, i64 %ind
%load.b = load i32, ptr %gep.b, align 4
%mul.a = mul i32 %load.b, %load.a
%add = add nuw nsw i64 %ind, 1
%gep.a.plus4 = getelementptr inbounds i32, ptr %a, i64 %add
store i32 %mul.a, ptr %gep.a.plus4, align 4
%gep.d = getelementptr inbounds i32, ptr %d, i64 %ind
%loadD = load i32, ptr %gep.d, align 4
%mul = mul i64 %ind, %stride
%gep.strided.a = getelementptr inbounds i32, ptr %a, i64 %mul
%load.strided.a = load i32, ptr %gep.strided.a, align 4
%mul.c = mul i32 %loadD, %load.strided.a
%gep.c = getelementptr inbounds i32, ptr %c, i64 %ind
store i32 %mul.c, ptr %gep.c, align 4
%exitcond = icmp eq i64 %add, 20
br i1 %exitcond, label %exit, label %for.body, !dbg !2

exit: ; preds = %for.body
ret void
}

!llvm.module.flags = !{!5, !6, !7}

!0 = !DIFile(filename: "f.c", directory: "/")
!1 = distinct !DISubprogram(name: "f", scope: !0, file: !0, unit: !4)
!2 = !DILocation(line: 5, column: 2, scope: !1)
!4 = distinct !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang")
!5 = !{i32 1, !"Debug Info Version", i32 3}
!6 = !{i32 2, !"Dwarf Version", i32 2}
!7 = !{i32 1, !"PIC Level", i32 2}
44 changes: 44 additions & 0 deletions llvm/test/Transforms/LoopDistribute/debug-print.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; REQUIRES: asserts
; RUN: opt -passes=loop-distribute -enable-loop-distribute \
; RUN: -debug-only=loop-distribute -disable-output 2>&1 %s | FileCheck %s

define void @f(ptr noalias %a, ptr noalias %b, ptr noalias %c, ptr noalias %d, i64 %stride) {
; CHECK-LABEL: 'f'
; CHECK: LDist: Found a candidate loop: for.body
; CHECK: Backward dependences:
; CHECK-NEXT: Backward:
; CHECK-NEXT: %load.a = load i32, ptr %gep.a, align 4 ->
; CHECK-NEXT: store i32 %mul.a, ptr %gep.a.plus4, align 4
; CHECK: Seeded partitions:
; CHECK: Partition 0
; CHECK: Partition 1
; CHECK: Partition 2
; CHECK: Partition 3
; CHECK: Distributing loop
entry:
br label %for.body

for.body: ; preds = %for.body, %entry
%ind = phi i64 [ 0, %entry ], [ %add, %for.body ]
%gep.a = getelementptr inbounds i32, ptr %a, i64 %ind
%load.a = load i32, ptr %gep.a, align 4
%gep.b = getelementptr inbounds i32, ptr %b, i64 %ind
%load.b = load i32, ptr %gep.b, align 4
%mul.a = mul i32 %load.b, %load.a
%add = add nuw nsw i64 %ind, 1
%gep.a.plus4 = getelementptr inbounds i32, ptr %a, i64 %add
store i32 %mul.a, ptr %gep.a.plus4, align 4
%gep.d = getelementptr inbounds i32, ptr %d, i64 %ind
%loadD = load i32, ptr %gep.d, align 4
%mul = mul i64 %ind, %stride
%gep.strided.a = getelementptr inbounds i32, ptr %a, i64 %mul
%load.strided.a = load i32, ptr %gep.strided.a, align 4
%mul.c = mul i32 %loadD, %load.strided.a
%gep.c = getelementptr inbounds i32, ptr %c, i64 %ind
store i32 %mul.c, ptr %gep.c, align 4
%exitcond = icmp eq i64 %add, 20
br i1 %exitcond, label %exit, label %for.body

exit: ; preds = %for.body
ret void
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ target triple = "thumbv8.1m.main-none-unknown-eabi"
; NOTE: The strides of the starting address values in the inner loop differ, i.e.
; '(i * (n + 1))' vs '(i * n)'.

; DEBUG-LABEL: LAA: Found a loop in diff_checks:
; DEBUG-LABEL: 'diff_checks'
; DEBUG: LAA: Found an analyzable loop: inner.loop
; DEBUG: LAA: Not creating diff runtime check, since these cannot be hoisted out of the outer loop
; DEBUG: LAA: Adding RT check for range:
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
Expand Down
Loading
Loading