Skip to content

Commit c3622f0

Browse files
committed
improve debug messages in delinearization
1 parent e50ccb1 commit c3622f0

21 files changed

+164
-199
lines changed

llvm/lib/Analysis/Delinearization.cpp

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void llvm::collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr,
182182
LLVM_DEBUG({
183183
dbgs() << "Strides:\n";
184184
for (const SCEV *S : Strides)
185-
dbgs() << *S << "\n";
185+
dbgs() << " " << *S << "\n";
186186
});
187187

188188
for (const SCEV *S : Strides) {
@@ -193,7 +193,7 @@ void llvm::collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr,
193193
LLVM_DEBUG({
194194
dbgs() << "Terms:\n";
195195
for (const SCEV *T : Terms)
196-
dbgs() << *T << "\n";
196+
dbgs() << " " << *T << "\n";
197197
});
198198

199199
SCEVCollectAddRecMultiplies MulCollector(Terms, SE);
@@ -294,7 +294,7 @@ void llvm::findArrayDimensions(ScalarEvolution &SE,
294294
LLVM_DEBUG({
295295
dbgs() << "Terms:\n";
296296
for (const SCEV *T : Terms)
297-
dbgs() << *T << "\n";
297+
dbgs() << " " << *T << "\n";
298298
});
299299

300300
// Remove duplicates.
@@ -325,7 +325,7 @@ void llvm::findArrayDimensions(ScalarEvolution &SE,
325325
LLVM_DEBUG({
326326
dbgs() << "Terms after sorting:\n";
327327
for (const SCEV *T : NewTerms)
328-
dbgs() << *T << "\n";
328+
dbgs() << " " << *T << "\n";
329329
});
330330

331331
if (NewTerms.empty() || !findArrayDimensionsRec(SE, NewTerms, Sizes)) {
@@ -339,7 +339,7 @@ void llvm::findArrayDimensions(ScalarEvolution &SE,
339339
LLVM_DEBUG({
340340
dbgs() << "Sizes:\n";
341341
for (const SCEV *S : Sizes)
342-
dbgs() << *S << "\n";
342+
dbgs() << " " << *S << "\n";
343343
});
344344
}
345345

@@ -354,18 +354,27 @@ void llvm::computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr,
354354
if (!AR->isAffine())
355355
return;
356356

357+
// Clear output vector.
358+
Subscripts.clear();
359+
360+
LLVM_DEBUG(dbgs() << "\ncomputeAccessFunctions\n"
361+
<< "Linearized Memory Access Function: " << *Expr << "\n");
362+
357363
const SCEV *Res = Expr;
358364
int Last = Sizes.size() - 1;
365+
359366
for (int i = Last; i >= 0; i--) {
367+
const SCEV *Size = Sizes[i];
360368
const SCEV *Q, *R;
361-
SCEVDivision::divide(SE, Res, Sizes[i], &Q, &R);
369+
370+
SCEVDivision::divide(SE, Res, Size, &Q, &R);
362371

363372
LLVM_DEBUG({
364-
dbgs() << "Res: " << *Res << "\n";
365-
dbgs() << "Sizes[i]: " << *Sizes[i] << "\n";
366-
dbgs() << "Res divided by Sizes[i]:\n";
367-
dbgs() << "Quotient: " << *Q << "\n";
368-
dbgs() << "Remainder: " << *R << "\n";
373+
dbgs() << "Computing 'MemAccFn / Sizes[" << i << "]':\n";
374+
dbgs() << " MemAccFn: " << *Res << "\n";
375+
dbgs() << " Sizes[" << i << "]: " << *Size << "\n";
376+
dbgs() << " Quotient (Leftover): " << *Q << "\n";
377+
dbgs() << " Remainder (Subscript Access Function): " << *R << "\n";
369378
});
370379

371380
Res = Q;
@@ -385,6 +394,7 @@ void llvm::computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr,
385394
}
386395

387396
// Record the access function for the current subscript.
397+
LLVM_DEBUG(dbgs() << "Subscripts push_back Remainder: " << *R << "\n");
388398
Subscripts.push_back(R);
389399
}
390400

@@ -397,7 +407,8 @@ void llvm::computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr,
397407
LLVM_DEBUG({
398408
dbgs() << "Subscripts:\n";
399409
for (const SCEV *S : Subscripts)
400-
dbgs() << *S << "\n";
410+
dbgs() << " " << *S << "\n";
411+
dbgs() << "\n";
401412
});
402413
}
403414

@@ -454,6 +465,10 @@ void llvm::delinearize(ScalarEvolution &SE, const SCEV *Expr,
454465
SmallVectorImpl<const SCEV *> &Subscripts,
455466
SmallVectorImpl<const SCEV *> &Sizes,
456467
const SCEV *ElementSize) {
468+
// Clear output vectors.
469+
Subscripts.clear();
470+
Sizes.clear();
471+
457472
// First step: collect parametric terms.
458473
SmallVector<const SCEV *, 4> Terms;
459474
collectParametricTerms(SE, Expr, Terms);
@@ -469,21 +484,6 @@ void llvm::delinearize(ScalarEvolution &SE, const SCEV *Expr,
469484

470485
// Third step: compute the access functions for each subscript.
471486
computeAccessFunctions(SE, Expr, Subscripts, Sizes);
472-
473-
if (Subscripts.empty())
474-
return;
475-
476-
LLVM_DEBUG({
477-
dbgs() << "succeeded to delinearize " << *Expr << "\n";
478-
dbgs() << "ArrayDecl[UnknownSize]";
479-
for (const SCEV *S : Sizes)
480-
dbgs() << "[" << *S << "]";
481-
482-
dbgs() << "\nArrayRef";
483-
for (const SCEV *S : Subscripts)
484-
dbgs() << "[" << *S << "]";
485-
dbgs() << "\n";
486-
});
487487
}
488488

489489
static std::optional<APInt> tryIntoAPInt(const SCEV *S) {
@@ -646,6 +646,9 @@ bool llvm::delinearizeFixedSizeArray(ScalarEvolution &SE, const SCEV *Expr,
646646
SmallVectorImpl<const SCEV *> &Subscripts,
647647
SmallVectorImpl<const SCEV *> &Sizes,
648648
const SCEV *ElementSize) {
649+
// Clear output vectors.
650+
Subscripts.clear();
651+
Sizes.clear();
649652

650653
// First step: find the fixed array size.
651654
SmallVector<uint64_t, 4> ConstSizes;
@@ -671,6 +674,7 @@ bool llvm::getIndexExpressionsFromGEP(ScalarEvolution &SE,
671674
assert(Subscripts.empty() && Sizes.empty() &&
672675
"Expected output lists to be empty on entry to this function.");
673676
assert(GEP && "getIndexExpressionsFromGEP called with a null GEP");
677+
LLVM_DEBUG(dbgs() << "\nGEP to delinearize: " << *GEP << "\n");
674678
Type *Ty = nullptr;
675679
bool DroppedFirstDim = false;
676680
for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
@@ -683,28 +687,43 @@ bool llvm::getIndexExpressionsFromGEP(ScalarEvolution &SE,
683687
continue;
684688
}
685689
Subscripts.push_back(Expr);
690+
LLVM_DEBUG(dbgs() << "Subscripts push_back: " << *Expr << "\n");
686691
continue;
687692
}
688693

689694
auto *ArrayTy = dyn_cast<ArrayType>(Ty);
690695
if (!ArrayTy) {
696+
LLVM_DEBUG(dbgs() << "GEP delinearize failed: " << *Ty
697+
<< " is not an array type.\n");
691698
Subscripts.clear();
692699
Sizes.clear();
693700
return false;
694701
}
695702

696703
Subscripts.push_back(Expr);
704+
LLVM_DEBUG(dbgs() << "Subscripts push_back: " << *Expr << "\n");
697705
if (!(DroppedFirstDim && i == 2))
698706
Sizes.push_back(ArrayTy->getNumElements());
699707

700708
Ty = ArrayTy->getElementType();
701709
}
710+
LLVM_DEBUG({
711+
dbgs() << "Subscripts:\n";
712+
for (const SCEV *S : Subscripts)
713+
dbgs() << *S << "\n";
714+
dbgs() << "\n";
715+
});
716+
702717
return !Subscripts.empty();
703718
}
704719

705720
bool llvm::tryDelinearizeFixedSizeImpl(
706721
ScalarEvolution *SE, Instruction *Inst, const SCEV *AccessFn,
707722
SmallVectorImpl<const SCEV *> &Subscripts, SmallVectorImpl<int> &Sizes) {
723+
// Clear output vectors.
724+
Subscripts.clear();
725+
Sizes.clear();
726+
708727
Value *SrcPtr = getLoadStorePointerOperand(Inst);
709728

710729
// Check the simple case where the array dimensions are fixed size.
@@ -769,8 +788,7 @@ void printDelinearization(raw_ostream &O, Function *F, LoopInfo *LI,
769788

770789
O << "\n";
771790
O << "Inst:" << Inst << "\n";
772-
O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
773-
O << "AccessFunction: " << *AccessFn << "\n";
791+
O << "LinearAccessFunction: " << *AccessFn << "\n";
774792

775793
SmallVector<const SCEV *, 3> Subscripts, Sizes;
776794

@@ -787,22 +805,33 @@ void printDelinearization(raw_ostream &O, Function *F, LoopInfo *LI,
787805
SE->getElementSize(&Inst));
788806
}
789807

790-
if (IsDelinearizationFailed()) {
791-
O << "failed to delinearize\n";
792-
continue;
793-
}
808+
if (IsDelinearizationFailed()) {
809+
O << "failed to delinearize\n";
810+
continue;
811+
}
794812

795-
O << "Base offset: " << *BasePointer << "\n";
796-
O << "ArrayDecl[UnknownSize]";
797-
int Size = Subscripts.size();
798-
for (int i = 0; i < Size - 1; i++)
813+
O << "Base offset: " << *BasePointer << "\n";
814+
O << "ArrayDecl";
815+
int NumSubscripts = Subscripts.size();
816+
int NumSizes = Sizes.size();
817+
818+
// Handle different size relationships between Subscripts and Sizes.
819+
if (NumSizes > 0) {
820+
// Print array dimensions (all but the last size, which is element
821+
// size).
822+
for (int i = 0; i < NumSizes - 1; i++)
799823
O << "[" << *Sizes[i] << "]";
800-
O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
801824

802-
O << "ArrayRef";
803-
for (int i = 0; i < Size; i++)
804-
O << "[" << *Subscripts[i] << "]";
805-
O << "\n";
825+
// Print element size (last element in Sizes array).
826+
O << " with elements of " << *Sizes[NumSizes - 1] << " bytes.\n";
827+
} else {
828+
O << " unknown sizes.\n";
829+
}
830+
831+
O << "ArrayRef";
832+
for (int i = 0; i < NumSubscripts; i++)
833+
O << "[" << *Subscripts[i] << "]";
834+
O << "\n";
806835
}
807836
}
808837

llvm/test/Analysis/Delinearization/a.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
define void @foo(i64 %n, i64 %m, i64 %o, ptr nocapture %A) #0 {
1212
; CHECK-LABEL: 'foo'
1313
; CHECK-NEXT: Inst: store i32 1, ptr %arrayidx11.us.us, align 4
14-
; CHECK-NEXT: In Loop with Header: for.k
15-
; CHECK-NEXT: AccessFunction: {{\{\{\{}}(28 + (4 * (-4 + (3 * %m)) * %o)),+,(8 * %m * %o)}<%for.i>,+,(12 * %o)}<%for.j>,+,20}<%for.k>
14+
; CHECK-NEXT: LinearAccessFunction: {{\{\{\{}}(28 + (4 * (-4 + (3 * %m)) * %o)),+,(8 * %m * %o)}<%for.i>,+,(12 * %o)}<%for.j>,+,20}<%for.k>
1615
; CHECK-NEXT: Base offset: %A
17-
; CHECK-NEXT: ArrayDecl[UnknownSize][%m][%o] with elements of 4 bytes.
16+
; CHECK-NEXT: ArrayDecl[%m][%o] with elements of 4 bytes.
1817
; CHECK-NEXT: ArrayRef[{3,+,2}<nuw><%for.i>][{-4,+,3}<nw><%for.j>][{7,+,5}<nw><%for.k>]
1918
;
2019
entry:

llvm/test/Analysis/Delinearization/byte_offset.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
define void @foo(ptr %A, i64 %i2, i64 %arg, i1 %c) {
1414
; CHECK-LABEL: 'foo'
1515
; CHECK-NEXT: Inst: store float 0.000000e+00, ptr %arrayidx, align 4
16-
; CHECK-NEXT: In Loop with Header: inner.loop
17-
; CHECK-NEXT: AccessFunction: ({0,+,%i2}<%outer.loop> + %unknown)
16+
; CHECK-NEXT: LinearAccessFunction: ({0,+,%i2}<%outer.loop> + %unknown)
1817
; CHECK-NEXT: failed to delinearize
1918
;
2019
entry:

llvm/test/Analysis/Delinearization/constant_functions_multi_dim.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
77
define void @mat_mul(ptr %C, ptr %A, ptr %B, i64 %N) #0 !kernel_arg_addr_space !2 !kernel_arg_access_qual !3 !kernel_arg_type !4 !kernel_arg_base_type !4 !kernel_arg_type_qual !5 {
88
; CHECK-LABEL: 'mat_mul'
99
; CHECK-NEXT: Inst: %tmp = load float, ptr %arrayidx, align 4
10-
; CHECK-NEXT: In Loop with Header: for.inc
11-
; CHECK-NEXT: AccessFunction: {(4 * %N * %call),+,4}<%for.inc>
10+
; CHECK-NEXT: LinearAccessFunction: {(4 * %N * %call),+,4}<%for.inc>
1211
; CHECK-NEXT: Base offset: %A
13-
; CHECK-NEXT: ArrayDecl[UnknownSize][%N] with elements of 4 bytes.
12+
; CHECK-NEXT: ArrayDecl[%N] with elements of 4 bytes.
1413
; CHECK-NEXT: ArrayRef[%call][{0,+,1}<nuw><nsw><%for.inc>]
1514
; CHECK-EMPTY:
1615
; CHECK-NEXT: Inst: %tmp5 = load float, ptr %arrayidx4, align 4
17-
; CHECK-NEXT: In Loop with Header: for.inc
18-
; CHECK-NEXT: AccessFunction: {(4 * %call1),+,(4 * %N)}<%for.inc>
16+
; CHECK-NEXT: LinearAccessFunction: {(4 * %call1),+,(4 * %N)}<%for.inc>
1917
; CHECK-NEXT: Base offset: %B
20-
; CHECK-NEXT: ArrayDecl[UnknownSize][%N] with elements of 4 bytes.
18+
; CHECK-NEXT: ArrayDecl[%N] with elements of 4 bytes.
2119
; CHECK-NEXT: ArrayRef[{0,+,1}<nuw><nsw><%for.inc>][%call1]
2220
;
2321
entry:

llvm/test/Analysis/Delinearization/divide_by_one.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ target datalayout = "e-m:e-p:32:32-i1:32-i64:64-a:0-n32"
1414
define void @test(ptr nocapture %dst, i32 %stride, i32 %bs) {
1515
; CHECK-LABEL: 'test'
1616
; CHECK-NEXT: Inst: %0 = load i8, ptr %arrayidx, align 1
17-
; CHECK-NEXT: In Loop with Header: for.body3
18-
; CHECK-NEXT: AccessFunction: {{\{\{}}(-1 + ((1 + %bs) * %stride)),+,(-1 * %stride)}<%for.cond1.preheader>,+,1}<nw><%for.body3>
17+
; CHECK-NEXT: LinearAccessFunction: {{\{\{}}(-1 + ((1 + %bs) * %stride)),+,(-1 * %stride)}<%for.cond1.preheader>,+,1}<nw><%for.body3>
1918
; CHECK-NEXT: Base offset: %dst
20-
; CHECK-NEXT: ArrayDecl[UnknownSize][%stride] with elements of 1 bytes.
19+
; CHECK-NEXT: ArrayDecl[%stride] with elements of 1 bytes.
2120
; CHECK-NEXT: ArrayRef[{(1 + %bs),+,-1}<nw><%for.cond1.preheader>][{-1,+,1}<nw><%for.body3>]
2221
; CHECK-EMPTY:
2322
; CHECK-NEXT: Inst: store i8 %0, ptr %arrayidx7, align 1
24-
; CHECK-NEXT: In Loop with Header: for.body3
25-
; CHECK-NEXT: AccessFunction: {{\{\{}}(%stride * %bs),+,(-1 * %stride)}<%for.cond1.preheader>,+,1}<nsw><%for.body3>
23+
; CHECK-NEXT: LinearAccessFunction: {{\{\{}}(%stride * %bs),+,(-1 * %stride)}<%for.cond1.preheader>,+,1}<nsw><%for.body3>
2624
; CHECK-NEXT: Base offset: %dst
27-
; CHECK-NEXT: ArrayDecl[UnknownSize][%stride] with elements of 1 bytes.
25+
; CHECK-NEXT: ArrayDecl[%stride] with elements of 1 bytes.
2826
; CHECK-NEXT: ArrayRef[{%bs,+,-1}<nsw><%for.cond1.preheader>][{0,+,1}<nuw><nsw><%for.body3>]
2927
;
3028
entry:

0 commit comments

Comments
 (0)