Skip to content

Commit 0e7199c

Browse files
authored
Replace print-at-pass-number cl::opt with print-before-pass-number (#76211)
The existing option prints the IR after the pass, but it's not clear from its name. In this patch I change the option to print the IR before the pass and change the name to make the behavior clear. Printing the IR before the pass is slightly simpler than after as I don't need to worry about printAfterPassInvalidated case. Either before or after the pass would be ok for the original use case this option was introduced for.
1 parent 12101ca commit 0e7199c

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PrintIRInstrumentation {
6565
bool shouldPrintBeforePass(StringRef PassID);
6666
bool shouldPrintAfterPass(StringRef PassID);
6767
bool shouldPrintPassNumbers();
68-
bool shouldPrintAtPassNumber();
68+
bool shouldPrintBeforePassNumber();
6969

7070
void pushPassRunDescriptor(StringRef PassID, Any IR,
7171
std::string &DumpIRFilename);

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ static cl::opt<bool> PrintPassNumbers(
118118
"print-pass-numbers", cl::init(false), cl::Hidden,
119119
cl::desc("Print pass names and their ordinals"));
120120

121-
static cl::opt<unsigned>
122-
PrintAtPassNumber("print-at-pass-number", cl::init(0), cl::Hidden,
123-
cl::desc("Print IR at pass with this number as "
124-
"reported by print-passes-names"));
121+
static cl::opt<unsigned> PrintBeforePassNumber(
122+
"print-before-pass-number", cl::init(0), cl::Hidden,
123+
cl::desc("Print IR before the pass with this number as "
124+
"reported by print-pass-numbers"));
125125

126126
static cl::opt<std::string> IRDumpDirectory(
127127
"ir-dump-directory",
@@ -806,8 +806,7 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
806806
// Note: here we rely on a fact that we do not change modules while
807807
// traversing the pipeline, so the latest captured module is good
808808
// for all print operations that has not happen yet.
809-
if (shouldPrintPassNumbers() || shouldPrintAtPassNumber() ||
810-
shouldPrintAfterPass(PassID))
809+
if (shouldPrintAfterPass(PassID))
811810
pushPassRunDescriptor(PassID, IR, DumpIRFilename);
812811

813812
if (!shouldPrintIR(IR))
@@ -823,8 +822,10 @@ void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
823822
return;
824823

825824
auto WriteIRToStream = [&](raw_ostream &Stream) {
826-
Stream << "; *** IR Dump Before " << PassID << " on " << getIRName(IR)
827-
<< " ***\n";
825+
Stream << "; *** IR Dump Before ";
826+
if (shouldPrintBeforePassNumber())
827+
Stream << CurrentPassNumber << "-";
828+
Stream << PassID << " on " << getIRName(IR) << " ***\n";
828829
unwrapAndPrint(Stream, IR);
829830
};
830831

@@ -842,8 +843,7 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
842843
if (isIgnored(PassID))
843844
return;
844845

845-
if (!shouldPrintAfterPass(PassID) && !shouldPrintPassNumbers() &&
846-
!shouldPrintAtPassNumber())
846+
if (!shouldPrintAfterPass(PassID))
847847
return;
848848

849849
auto [M, DumpIRFilename, IRName, StoredPassID] = popPassRunDescriptor(PassID);
@@ -853,10 +853,7 @@ void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
853853
return;
854854

855855
auto WriteIRToStream = [&](raw_ostream &Stream, const StringRef IRName) {
856-
Stream << "; *** IR Dump "
857-
<< (shouldPrintAtPassNumber()
858-
? StringRef(formatv("At {0}-{1}", CurrentPassNumber, PassID))
859-
: StringRef(formatv("After {0}", PassID)))
856+
Stream << "; *** IR Dump " << StringRef(formatv("After {0}", PassID))
860857
<< " on " << IRName << " ***\n";
861858
unwrapAndPrint(Stream, IR);
862859
};
@@ -879,8 +876,7 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
879876
if (isIgnored(PassID))
880877
return;
881878

882-
if (!shouldPrintAfterPass(PassID) && !shouldPrintPassNumbers() &&
883-
!shouldPrintAtPassNumber())
879+
if (!shouldPrintAfterPass(PassID))
884880
return;
885881

886882
auto [M, DumpIRFilename, IRName, StoredPassID] = popPassRunDescriptor(PassID);
@@ -893,12 +889,8 @@ void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
893889
auto WriteIRToStream = [&](raw_ostream &Stream, const Module *M,
894890
const StringRef IRName) {
895891
SmallString<20> Banner;
896-
if (shouldPrintAtPassNumber())
897-
Banner = formatv("; *** IR Dump At {0}-{1} on {2} (invalidated) ***",
898-
CurrentPassNumber, PassID, IRName);
899-
else
900-
Banner = formatv("; *** IR Dump After {0} on {1} (invalidated) ***",
901-
PassID, IRName);
892+
Banner = formatv("; *** IR Dump After {0} on {1} (invalidated) ***", PassID,
893+
IRName);
902894
Stream << Banner << "\n";
903895
printIR(Stream, M);
904896
};
@@ -921,6 +913,10 @@ bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
921913
if (shouldPrintBeforeAll())
922914
return true;
923915

916+
if (shouldPrintBeforePassNumber() &&
917+
CurrentPassNumber == PrintBeforePassNumber)
918+
return true;
919+
924920
StringRef PassName = PIC->getPassNameForClassName(PassID);
925921
return is_contained(printBeforePasses(), PassName);
926922
}
@@ -929,9 +925,6 @@ bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
929925
if (shouldPrintAfterAll())
930926
return true;
931927

932-
if (shouldPrintAtPassNumber() && CurrentPassNumber == PrintAtPassNumber)
933-
return true;
934-
935928
StringRef PassName = PIC->getPassNameForClassName(PassID);
936929
return is_contained(printAfterPasses(), PassName);
937930
}
@@ -940,8 +933,8 @@ bool PrintIRInstrumentation::shouldPrintPassNumbers() {
940933
return PrintPassNumbers;
941934
}
942935

943-
bool PrintIRInstrumentation::shouldPrintAtPassNumber() {
944-
return PrintAtPassNumber > 0;
936+
bool PrintIRInstrumentation::shouldPrintBeforePassNumber() {
937+
return PrintBeforePassNumber > 0;
945938
}
946939

947940
void PrintIRInstrumentation::registerCallbacks(
@@ -950,13 +943,12 @@ void PrintIRInstrumentation::registerCallbacks(
950943

951944
// BeforePass callback is not just for printing, it also saves a Module
952945
// for later use in AfterPassInvalidated.
953-
if (shouldPrintPassNumbers() || shouldPrintAtPassNumber() ||
946+
if (shouldPrintPassNumbers() || shouldPrintBeforePassNumber() ||
954947
shouldPrintBeforeSomePass() || shouldPrintAfterSomePass())
955948
PIC.registerBeforeNonSkippedPassCallback(
956949
[this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
957950

958-
if (shouldPrintPassNumbers() || shouldPrintAtPassNumber() ||
959-
shouldPrintAfterSomePass()) {
951+
if (shouldPrintAfterSomePass()) {
960952
PIC.registerAfterPassCallback(
961953
[this](StringRef P, Any IR, const PreservedAnalyses &) {
962954
this->printAfterPass(P, IR);

llvm/test/Other/print-at-pass-number.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
; RUN: opt -passes="loop(indvars,loop-deletion,loop-unroll-full)" -print-pass-numbers -S -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=NUMBER
2-
; RUN: opt -passes="loop(indvars,loop-deletion,loop-unroll-full)" -print-module-scope -print-at-pass-number=3 -S -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=AT
3-
; RUN: opt -passes="loop(indvars,loop-deletion,loop-unroll-full)" -print-module-scope -print-at-pass-number=4 -S -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=AT-INVALIDATE
2+
; RUN: opt -passes="loop(indvars,loop-deletion,loop-unroll-full)" -print-module-scope -print-before-pass-number=3 -S -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=BEFORE
43

54
define i32 @bar(i32 %arg) {
6-
; AT: *** IR Dump At 3-IndVarSimplifyPass on bb1 ***
7-
; AT: define i32 @bar(i32 %arg) {
8-
9-
; AT-INVALIDATE: *** IR Dump At 4-LoopDeletionPass on bb1 (invalidated) ***
10-
; AT-INVALIDATE: define i32 @bar(i32 %arg) {
5+
; BEFORE: *** IR Dump Before 3-IndVarSimplifyPass on bb1 ***
6+
; BEFORE: define i32 @bar(i32 %arg) {
117

128
bb:
139
br label %bb1

0 commit comments

Comments
 (0)