Skip to content

Commit

Permalink
Introduce -print-before-changed, making -print-changed also print bef…
Browse files Browse the repository at this point in the history
…ore passes that modify IR

Summary:
Add an option -print-before-changed that modifies the print-changed
behaviour so that it prints the IR before a pass that changed it in
addition to printing the IR after the pass. Note that the option
does nothing in isolation. The filtering options work as expected.
Lit tests are included.

Author: Jamie Schmeiser <schmeise@ca.ibm.com>

Reviewed By: aeubanks (Arthur Eubanks)

Differential Revision: https://reviews.llvm.org/D88757
  • Loading branch information
jamieschmeiser authored and anhtuyenibm committed Nov 12, 2020
1 parent d85cc03 commit 782d6a6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
23 changes: 22 additions & 1 deletion llvm/lib/Passes/StandardInstrumentations.cpp
Expand Up @@ -68,7 +68,8 @@ static cl::opt<bool>
// or indicating that the IR has been filtered out. The extra options
// can be combined, allowing only changed IRs for certain passes on certain
// functions to be reported in different formats, with the rest being
// reported as filtered out.
// reported as filtered out. The -print-before-changed option will print
// the IR as it was before each pass that changed it.
static cl::opt<bool> PrintChanged("print-changed",
cl::desc("Print changed IRs"),
cl::init(false), cl::Hidden);
Expand All @@ -80,6 +81,13 @@ static cl::list<std::string>
cl::desc("Only consider IR changes for passes whose names "
"match for the print-changed option"),
cl::CommaSeparated, cl::Hidden);
// An option that supports the -print-changed option. See
// the description for -print-changed for an explanation of the use
// of this option. Note that this option has no effect without -print-changed.
static cl::opt<bool>
PrintChangedBefore("print-before-changed",
cl::desc("Print before passes that change them"),
cl::init(false), cl::Hidden);

namespace {

Expand Down Expand Up @@ -392,6 +400,19 @@ void IRChangePrinter::handleAfter(StringRef PassID, std::string &Name,
StringRef AfterRef = After;
StringRef Banner =
AfterRef.take_until([](char C) -> bool { return C == '\n'; });

// Report the IR before the changes when requested.
if (PrintChangedBefore) {
Out << "*** IR Dump Before" << Banner.substr(17);
// LazyCallGraph::SCC already has "(scc:..." in banner so only add
// in the name if it isn't already there.
if (Name.substr(0, 6) != " (scc:" && !llvm::forcePrintModuleIR())
Out << Name;

StringRef BeforeRef = Before;
Out << BeforeRef.substr(Banner.size());
}

Out << Banner;

// LazyCallGraph::SCC already has "(scc:..." in banner so only add
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/Other/change-printer.ll
Expand Up @@ -34,6 +34,16 @@
; others (including g) are filtered out. Note that the second time
; instsimplify is run on f, it does not change the IR
; RUN: opt -S -print-changed -passes="instsimplify,instsimplify" -filter-print-funcs=f 2>&1 -o /dev/null < %s | FileCheck %s --check-prefix=CHECK-MULT-PASSES-FILTER-FUNC
;
; Simple print-before-changed functionality check.
; RUN: opt -S -print-changed -print-before-changed -passes=instsimplify 2>&1 -o /dev/null < %s | FileCheck %s --check-prefix=CHECK-SIMPLE-BEFORE
;
; Check print-before-changed obeys the function filtering
; RUN: opt -S -print-changed -print-before-changed -passes=instsimplify -filter-print-funcs=f 2>&1 -o /dev/null < %s | FileCheck %s --check-prefix=CHECK-FUNC-FILTER-BEFORE
;
; Check that the reporting of IRs with -print-before-changed respects -print-module-scope
; RUN: opt -S -print-changed -print-before-changed -passes=instsimplify -print-module-scope 2>&1 -o /dev/null < %s | FileCheck %s --check-prefix=CHECK-PRINT-MOD-SCOPE-BEFORE
;

define i32 @g() {
entry:
Expand Down Expand Up @@ -126,3 +136,40 @@ entry:
; CHECK-MULT-PASSES-FILTER-FUNC: *** IR Dump After InstSimplifyPass *** (function: f)
; CHECK-MULT-PASSES-FILTER-FUNC-NEXT: define i32 @f()
; CHECK-MULT-PASSES-FILTER-FUNC: *** IR Dump After InstSimplifyPass (function: f) omitted because no change ***

; CHECK-SIMPLE-BEFORE: *** IR Dump At Start: ***
; CHECK-SIMPLE-BEFORE-NEXT: ; ModuleID = {{.+}}
; CHECK-SIMPLE-BEFORE: *** IR Dump After VerifierPass (module) omitted because no change ***
; CHECK-SIMPLE-BEFORE: *** IR Dump Before InstSimplifyPass *** (function: g)
; CHECK-SIMPLE-BEFORE-NEXT: define i32 @g()
; CHECK-SIMPLE-BEFORE: *** IR Dump After InstSimplifyPass *** (function: g)
; CHECK-SIMPLE-BEFORE-NEXT: define i32 @g()
; CHECK-SIMPLE-BEFORE: *** IR Pass PassManager{{.*}} (function: g) ignored ***
; CHECK-SIMPLE-BEFORE: *** IR Dump Before InstSimplifyPass *** (function: f)
; CHECK-SIMPLE-BEFORE-NEXT: define i32 @f()
; CHECK-SIMPLE-BEFORE: *** IR Dump After InstSimplifyPass *** (function: f)
; CHECK-SIMPLE-BEFORE-NEXT: define i32 @f()
; CHECK-SIMPLE-BEFORE: *** IR Pass PassManager{{.*}} (function: f) ignored ***
; CHECK-SIMPLE-BEFORE: *** IR Pass ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}> (module) ignored ***
; CHECK-SIMPLE-BEFORE: *** IR Dump After VerifierPass (module) omitted because no change ***
; CHECK-SIMPLE-BEFORE: *** IR Dump After PrintModulePass (module) omitted because no change ***
; CHECK-SIMPLE-BEFORE-NOT: *** IR

; CHECK-FUNC-FILTER-BEFORE: *** IR Dump At Start: ***
; CHECK-FUNC-FILTER-BEFORE-NEXT: ; ModuleID = {{.+}}
; CHECK-FUNC-FILTER-BEFORE: *** IR Dump After InstSimplifyPass (function: g) filtered out ***
; CHECK-FUNC-FILTER-BEFORE: *** IR Dump Before InstSimplifyPass *** (function: f)
; CHECK-FUNC-FILTER-BEFORE-NEXT: define i32 @f()
; CHECK-FUNC-FILTER-BEFORE: *** IR Dump After InstSimplifyPass *** (function: f)
; CHECK-FUNC-FILTER-BEFORE-NEXT: define i32 @f()

; CHECK-PRINT-MOD-SCOPE-BEFORE: *** IR Dump At Start: ***
; CHECK-PRINT-MOD-SCOPE-BEFORE-NEXT: ModuleID = {{.+}}
; CHECK-PRINT-MOD-SCOPE-BEFORE: *** IR Dump Before InstSimplifyPass *** (function: g)
; CHECK-PRINT-MOD-SCOPE-BEFORE-NEXT: ModuleID = {{.+}}
; CHECK-PRINT-MOD-SCOPE-BEFORE: *** IR Dump After InstSimplifyPass *** (function: g)
; CHECK-PRINT-MOD-SCOPE-BEFORE-NEXT: ModuleID = {{.+}}
; CHECK-PRINT-MOD-SCOPE-BEFORE: *** IR Dump Before InstSimplifyPass *** (function: f)
; CHECK-PRINT-MOD-SCOPE-BEFORE-NEXT: ModuleID = {{.+}}
; CHECK-PRINT-MOD-SCOPE-BEFORE: *** IR Dump After InstSimplifyPass *** (function: f)
; CHECK-PRINT-MOD-SCOPE-BEFORE-NEXT: ModuleID = {{.+}}

0 comments on commit 782d6a6

Please sign in to comment.