Skip to content

Commit 0e24179

Browse files
authored
[SelectionDAG] Add support to filter SelectionDAG dumps during ISel by function names (#72696)
`-debug-only=isel-dump` is the new debug type for printing SelectionDAG after each ISel phase. This can be furthered filter by `-filter-print-funcs=<function names>`. Note that the existing `-debug-only=isel` will take precedence over the new behavior and print SelectionDAG dumps of every single function regardless of `-filter-print-funcs`'s values.
1 parent a1e2c65 commit 0e24179

File tree

5 files changed

+119
-43
lines changed

5 files changed

+119
-43
lines changed

llvm/docs/CodeGenerator.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,12 @@ SelectionDAG-based instruction selection consists of the following steps:
857857
After all of these steps are complete, the SelectionDAG is destroyed and the
858858
rest of the code generation passes are run.
859859

860+
One of the most common ways to debug these steps is using ``-debug-only=isel``,
861+
which prints out the DAG, along with other information like debug info,
862+
after each of these steps. Alternatively, ``-debug-only=isel-dump`` shows only
863+
the DAG dumps, but the results can be filtered by function names using
864+
``-filter-print-funcs=<function names>``.
865+
860866
One great way to visualize what is going on here is to take advantage of a few
861867
LLC command line options. The following options pop up a window displaying the
862868
SelectionDAG at specific times (if you only get errors printed to the console

llvm/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ Changes to the C API
201201
Changes to the CodeGen infrastructure
202202
-------------------------------------
203203

204+
* A new debug type ``isel-dump`` is added to show only the SelectionDAG dumps
205+
after each ISel phase (i.e. ``-debug-only=isel-dump``). This new debug type
206+
can be filtered by function names using ``-filter-print-funcs=<function names>``,
207+
the same flag used to filter IR dumps after each Pass. Note that the existing
208+
``-debug-only=isel`` will take precedence over the new behavior and
209+
print SelectionDAG dumps of every single function regardless of
210+
``-filter-print-funcs``'s values.
211+
204212
* ``PrologEpilogInserter`` no longer supports register scavenging
205213
during forwards frame index elimination. Targets should use
206214
backwards frame index elimination instead.

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class SelectionDAGISel : public MachineFunctionPass {
6161
/// Used to report things like combines and FastISel failures.
6262
std::unique_ptr<OptimizationRemarkEmitter> ORE;
6363

64+
/// True if the function currently processing is in the function printing list
65+
/// (i.e. `-filter-print-funcs`).
66+
/// This is primarily used by ISEL_DUMP, which spans in multiple member
67+
/// functions. Storing the filter result here so that we only need to do the
68+
/// filtering once.
69+
bool MatchFilterFuncName = false;
70+
6471
explicit SelectionDAGISel(char &ID, TargetMachine &tm,
6572
CodeGenOptLevel OL = CodeGenOptLevel::Default);
6673
~SelectionDAGISel() override;

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "llvm/IR/Intrinsics.h"
7979
#include "llvm/IR/IntrinsicsWebAssembly.h"
8080
#include "llvm/IR/Metadata.h"
81+
#include "llvm/IR/PrintPasses.h"
8182
#include "llvm/IR/Statepoint.h"
8283
#include "llvm/IR/Type.h"
8384
#include "llvm/IR/User.h"
@@ -113,6 +114,7 @@
113114
using namespace llvm;
114115

115116
#define DEBUG_TYPE "isel"
117+
#define ISEL_DUMP_DEBUG_TYPE DEBUG_TYPE "-dump"
116118

117119
STATISTIC(NumFastIselFailures, "Number of instructions fast isel failed on");
118120
STATISTIC(NumFastIselSuccess, "Number of instructions fast isel selected");
@@ -180,6 +182,19 @@ static const bool ViewDAGCombine1 = false, ViewLegalizeTypesDAGs = false,
180182
ViewSchedDAGs = false, ViewSUnitDAGs = false;
181183
#endif
182184

185+
#ifndef NDEBUG
186+
#define ISEL_DUMP(X) \
187+
do { \
188+
if (llvm::DebugFlag && \
189+
(isCurrentDebugType(DEBUG_TYPE) || \
190+
(isCurrentDebugType(ISEL_DUMP_DEBUG_TYPE) && MatchFilterFuncName))) { \
191+
X; \
192+
} \
193+
} while (false)
194+
#else
195+
#define ISEL_DUMP(X) do { } while (false)
196+
#endif
197+
183198
//===---------------------------------------------------------------------===//
184199
///
185200
/// RegisterScheduler class - Track the registration of instruction schedulers.
@@ -403,6 +418,13 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
403418
const Function &Fn = mf.getFunction();
404419
MF = &mf;
405420

421+
#ifndef NDEBUG
422+
StringRef FuncName = Fn.getName();
423+
MatchFilterFuncName = isFunctionInPrintList(FuncName);
424+
#else
425+
(void)MatchFilterFuncName;
426+
#endif
427+
406428
// Decide what flavour of variable location debug-info will be used, before
407429
// we change the optimisation level.
408430
bool InstrRef = mf.shouldUseDebugInstrRef();
@@ -436,7 +458,7 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
436458
if (isAssignmentTrackingEnabled(*Fn.getParent()))
437459
FnVarLocs = getAnalysis<AssignmentTrackingAnalysis>().getResults();
438460

439-
LLVM_DEBUG(dbgs() << "\n\n\n=== " << Fn.getName() << "\n");
461+
ISEL_DUMP(dbgs() << "\n\n\n=== " << FuncName << "\n");
440462

441463
UniformityInfo *UA = nullptr;
442464
if (auto *UAPass = getAnalysisIfAvailable<UniformityInfoWrapperPass>())
@@ -668,8 +690,8 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
668690
// at this point.
669691
FuncInfo->clear();
670692

671-
LLVM_DEBUG(dbgs() << "*** MachineFunction at end of ISel ***\n");
672-
LLVM_DEBUG(MF->print(dbgs()));
693+
ISEL_DUMP(dbgs() << "*** MachineFunction at end of ISel ***\n");
694+
ISEL_DUMP(MF->print(dbgs()));
673695

674696
return true;
675697
}
@@ -777,10 +799,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
777799
BlockName =
778800
(MF->getName() + ":" + FuncInfo->MBB->getBasicBlock()->getName()).str();
779801
}
780-
LLVM_DEBUG(dbgs() << "\nInitial selection DAG: "
781-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
782-
<< "'\n";
783-
CurDAG->dump());
802+
ISEL_DUMP(dbgs() << "\nInitial selection DAG: "
803+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
804+
<< "'\n";
805+
CurDAG->dump());
784806

785807
#ifndef NDEBUG
786808
if (TTI.hasBranchDivergence())
@@ -797,10 +819,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
797819
CurDAG->Combine(BeforeLegalizeTypes, AA, OptLevel);
798820
}
799821

800-
LLVM_DEBUG(dbgs() << "\nOptimized lowered selection DAG: "
801-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
802-
<< "'\n";
803-
CurDAG->dump());
822+
ISEL_DUMP(dbgs() << "\nOptimized lowered selection DAG: "
823+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
824+
<< "'\n";
825+
CurDAG->dump());
804826

805827
#ifndef NDEBUG
806828
if (TTI.hasBranchDivergence())
@@ -819,10 +841,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
819841
Changed = CurDAG->LegalizeTypes();
820842
}
821843

822-
LLVM_DEBUG(dbgs() << "\nType-legalized selection DAG: "
823-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
824-
<< "'\n";
825-
CurDAG->dump());
844+
ISEL_DUMP(dbgs() << "\nType-legalized selection DAG: "
845+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
846+
<< "'\n";
847+
CurDAG->dump());
826848

827849
#ifndef NDEBUG
828850
if (TTI.hasBranchDivergence())
@@ -843,10 +865,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
843865
CurDAG->Combine(AfterLegalizeTypes, AA, OptLevel);
844866
}
845867

846-
LLVM_DEBUG(dbgs() << "\nOptimized type-legalized selection DAG: "
847-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
848-
<< "'\n";
849-
CurDAG->dump());
868+
ISEL_DUMP(dbgs() << "\nOptimized type-legalized selection DAG: "
869+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
870+
<< "'\n";
871+
CurDAG->dump());
850872

851873
#ifndef NDEBUG
852874
if (TTI.hasBranchDivergence())
@@ -861,10 +883,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
861883
}
862884

863885
if (Changed) {
864-
LLVM_DEBUG(dbgs() << "\nVector-legalized selection DAG: "
865-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
866-
<< "'\n";
867-
CurDAG->dump());
886+
ISEL_DUMP(dbgs() << "\nVector-legalized selection DAG: "
887+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
888+
<< "'\n";
889+
CurDAG->dump());
868890

869891
#ifndef NDEBUG
870892
if (TTI.hasBranchDivergence())
@@ -877,10 +899,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
877899
CurDAG->LegalizeTypes();
878900
}
879901

880-
LLVM_DEBUG(dbgs() << "\nVector/type-legalized selection DAG: "
881-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
882-
<< "'\n";
883-
CurDAG->dump());
902+
ISEL_DUMP(dbgs() << "\nVector/type-legalized selection DAG: "
903+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
904+
<< "'\n";
905+
CurDAG->dump());
884906

885907
#ifndef NDEBUG
886908
if (TTI.hasBranchDivergence())
@@ -897,10 +919,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
897919
CurDAG->Combine(AfterLegalizeVectorOps, AA, OptLevel);
898920
}
899921

900-
LLVM_DEBUG(dbgs() << "\nOptimized vector-legalized selection DAG: "
901-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
902-
<< "'\n";
903-
CurDAG->dump());
922+
ISEL_DUMP(dbgs() << "\nOptimized vector-legalized selection DAG: "
923+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
924+
<< "'\n";
925+
CurDAG->dump());
904926

905927
#ifndef NDEBUG
906928
if (TTI.hasBranchDivergence())
@@ -917,10 +939,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
917939
CurDAG->Legalize();
918940
}
919941

920-
LLVM_DEBUG(dbgs() << "\nLegalized selection DAG: "
921-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
922-
<< "'\n";
923-
CurDAG->dump());
942+
ISEL_DUMP(dbgs() << "\nLegalized selection DAG: "
943+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
944+
<< "'\n";
945+
CurDAG->dump());
924946

925947
#ifndef NDEBUG
926948
if (TTI.hasBranchDivergence())
@@ -937,10 +959,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
937959
CurDAG->Combine(AfterLegalizeDAG, AA, OptLevel);
938960
}
939961

940-
LLVM_DEBUG(dbgs() << "\nOptimized legalized selection DAG: "
941-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
942-
<< "'\n";
943-
CurDAG->dump());
962+
ISEL_DUMP(dbgs() << "\nOptimized legalized selection DAG: "
963+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
964+
<< "'\n";
965+
CurDAG->dump());
944966

945967
#ifndef NDEBUG
946968
if (TTI.hasBranchDivergence())
@@ -961,10 +983,10 @@ void SelectionDAGISel::CodeGenAndEmitDAG() {
961983
DoInstructionSelection();
962984
}
963985

964-
LLVM_DEBUG(dbgs() << "\nSelected selection DAG: "
965-
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
966-
<< "'\n";
967-
CurDAG->dump());
986+
ISEL_DUMP(dbgs() << "\nSelected selection DAG: "
987+
<< printMBBReference(*FuncInfo->MBB) << " '" << BlockName
988+
<< "'\n";
989+
CurDAG->dump());
968990

969991
if (ViewSchedDAGs && MatchFilterBB)
970992
CurDAG->viewGraph("scheduler input for " + BlockName);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llc -debug-only=isel-dump -filter-print-funcs=foo < %s 2>&1 | FileCheck %s --check-prefix=FOO
2+
; RUN: llc -debug-only=isel-dump -filter-print-funcs=bar < %s 2>&1 | FileCheck %s --check-prefix=BAR
3+
; RUN: llc -debug-only=isel-dump -filter-print-funcs=foo,zap < %s 2>&1 | FileCheck %s --check-prefixes=FOO,ZAP
4+
; Make sure the original -debug-only=isel still works.
5+
; RUN: llc -debug-only=isel < %s 2>&1 | FileCheck %s --check-prefixes=FOO,BAR,ZAP
6+
; REQUIRES: asserts
7+
8+
; FOO: === foo
9+
; BAR-NOT: === foo
10+
; ZAP-NOT: === foo
11+
; FOO: # Machine code for function foo
12+
define i32 @foo(i32 %a, i32 %b) {
13+
%r = add i32 %a, %b
14+
ret i32 %r
15+
}
16+
17+
; BAR: === bar
18+
; FOO-NOT: === bar
19+
; ZAP-NOT: === bar
20+
; BAR: # Machine code for function bar
21+
define i32 @bar(i32 %a, i32 %b) {
22+
%r = mul i32 %a, %b
23+
ret i32 %r
24+
}
25+
26+
; ZAP: === zap
27+
; FOO-NOT: === zap
28+
; BAR-NOT: === zap
29+
; ZAP: # Machine code for function zap
30+
define i32 @zap(i32 %a, i32 %b) {
31+
%r = sub i32 %a, %b
32+
ret i32 %r
33+
}

0 commit comments

Comments
 (0)