Skip to content

Commit a88709a

Browse files
authored
Add -print-before and -print-before-all flags (microsoft#5590)
These match `-print-after` and `-print-after-all`, and are needed if we want the opt pipeline view to work in compiler explorer.
1 parent 7efd6ee commit a88709a

File tree

10 files changed

+85
-27
lines changed

10 files changed

+85
-27
lines changed

include/dxc/Support/HLSLOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ class DxcOpts {
223223
std::set<std::string> IgnoreSemDefs; // OPT_ignore_semdef
224224
std::map<std::string, std::string> OverrideSemDefs; // OPT_override_semdef
225225

226+
bool PrintBeforeAll; // OPT_print_before_all
227+
std::set<std::string> PrintBefore; // OPT_print_before
226228
bool PrintAfterAll; // OPT_print_after_all
227229
std::set<std::string> PrintAfter; // OPT_print_after
228230
bool EnablePayloadQualifiers = false; // OPT_enable_payload_qualifiers

include/dxc/Support/HLSLOptions.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,14 @@ def encoding : Separate<["-", "/"], "encoding">, Group<hlslcomp_Group>, Flags<[C
307307
HelpText<"Set default encoding for source inputs and text outputs (utf8|utf16(win)|utf32(*nix)|wide) default=utf8">;
308308
def validator_version : Separate<["-", "/"], "validator-version">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
309309
HelpText<"Override validator version for module. Format: <major.minor> ; Default: DXIL.dll version or current internal version.">;
310+
def print_before_all : Flag<["-", "/"], "print-before-all">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
311+
HelpText<"Print LLVM IR before each pass.">;
312+
def print_before : Separate<["-", "/"], "print-before">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
313+
HelpText<"Print LLVM IR before a specific pass. May be specificied multiple times.">;
310314
def print_after_all : Flag<["-", "/"], "print-after-all">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
311315
HelpText<"Print LLVM IR after each pass.">;
312316
def print_after : Separate<["-", "/"], "print-after">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
313-
HelpText<"Print LLVM IR after a specific pass.">;
317+
HelpText<"Print LLVM IR after a specific pass. May be specificied multiple times.">;
314318
def ignore_opt_semdefs : Flag<["-", "/"], "ignore-opt-semdefs">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
315319
HelpText<"Ignore optional semantic defines which are not required to ensure program correctness.">;
316320
def ignore_semdef : Separate<["-", "/"], "ignore-semdef">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,

include/llvm/IR/LegacyPassManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class FunctionPassManagerImpl;
3636
/// it is.
3737
class PassManagerBase {
3838
public:
39+
bool HLSLPrintBeforeAll = false; // HLSL Change
40+
std::set<std::string> HLSLPrintBefore; // HLSL Change
3941
bool HLSLPrintAfterAll = false; // HLSL Change
4042
std::set<std::string> HLSLPrintAfter; // HLSL Change
4143

include/llvm/IR/LegacyPassManagers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ class PMTopLevelManager {
180180
virtual PassManagerType getTopLevelPassManagerType() = 0;
181181

182182
public:
183+
bool HLSLPrintBeforeAll = false; // HLSL Change
184+
std::set<std::string> HLSLPrintBefore; // HLSL Change
183185
bool HLSLPrintAfterAll = false; // HLSL Change
184186
std::set<std::string> HLSLPrintAfter; // HLSL Change
185187

lib/DxcSupport/HLSLOptions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
776776
opts.LegacyMacroExpansion = Args.hasFlag(OPT_flegacy_macro_expansion, OPT_INVALID, false);
777777
opts.LegacyResourceReservation = Args.hasFlag(OPT_flegacy_resource_reservation, OPT_INVALID, false);
778778
opts.ExportShadersOnly = Args.hasFlag(OPT_export_shaders_only, OPT_INVALID, false);
779+
opts.PrintBeforeAll = Args.hasFlag(OPT_print_before_all, OPT_INVALID, false);
779780
opts.PrintAfterAll = Args.hasFlag(OPT_print_after_all, OPT_INVALID, false);
780781
opts.ResMayAlias = Args.hasFlag(OPT_res_may_alias, OPT_INVALID, false);
781782
opts.ResMayAlias = Args.hasFlag(OPT_res_may_alias_, OPT_INVALID, opts.ResMayAlias);
@@ -795,6 +796,9 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
795796
opts.EnablePayloadQualifiers = Args.hasFlag(OPT_enable_payload_qualifiers, OPT_INVALID,
796797
DXIL::CompareVersions(Major, Minor, 6, 7) >= 0);
797798

799+
for (const std::string &value : Args.getAllArgValues(OPT_print_before)) {
800+
opts.PrintBefore.insert(value);
801+
}
798802
for (const std::string &value : Args.getAllArgValues(OPT_print_after)) {
799803
opts.PrintAfter.insert(value);
800804
}

lib/IR/LegacyPassManager.cpp

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,19 @@ static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
103103
}
104104
return false;
105105
}
106-
#endif
107106

108107
/// This is a utility to check whether a pass should have IR dumped
109108
/// before it.
110109
static bool ShouldPrintBeforePass(const PassInfo *PI) {
111-
return false; // HLSL Change - return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
110+
return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
112111
}
113112

114113
/// This is a utility to check whether a pass should have IR dumped
115114
/// after it.
116115
static bool ShouldPrintAfterPass(const PassInfo *PI) {
117-
return false; // HLSL Change -PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
116+
return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
118117
}
118+
#endif // HLSL Change Ends
119119

120120
/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
121121
/// or higher is specified.
@@ -679,36 +679,44 @@ void PMTopLevelManager::schedulePass(Pass *P) {
679679
return;
680680
}
681681

682-
if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
683-
Pass *PP = P->createPrinterPass(dbgs(), std::string("*** IR Dump Before ") +
684-
P->getPassName().str() + " ***");
682+
// HLSL Change - begin
683+
class direct_stderr_stream : public raw_ostream {
684+
uint64_t current_pos() const override { return 0; }
685+
/// See raw_ostream::write_impl.
686+
void write_impl(const char *Ptr, size_t Size) override {
687+
fwrite(Ptr, Size, 1, stderr);
688+
}
689+
};
690+
691+
static direct_stderr_stream stderr_stream;
692+
693+
auto ShouldPrint =
694+
[](const PassInfo *PI, bool AllOpt, std::set<std::string> &ByNameOpt) {
695+
return AllOpt ||
696+
(ByNameOpt.size() && ByNameOpt.count(PI->getPassArgument()));
697+
};
698+
699+
if (PI && !PI->isAnalysis() &&
700+
ShouldPrint(PI, this->HLSLPrintBeforeAll, this->HLSLPrintBefore)) {
701+
Pass *PP = P->createPrinterPass(stderr_stream,
702+
std::string("*** IR Dump Before ") +
703+
P->getPassName().str() + " (" +
704+
PI->getPassArgument() + ") ***");
685705
PP->assignPassManager(activeStack, getTopLevelPassManagerType());
686706
}
707+
// HLSL Change - end
687708

688709
// Add the requested pass to the best available pass manager.
689710
PPtr.release(); // HLSL Change - assignPassManager takes ownership
690711
P->assignPassManager(activeStack, getTopLevelPassManagerType());
691712

692-
if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
693-
Pass *PP = P->createPrinterPass(dbgs(), std::string("*** IR Dump After ") +
694-
P->getPassName().str() + " ***");
695-
PP->assignPassManager(activeStack, getTopLevelPassManagerType());
696-
}
697-
698713
// HLSL Change - begin
699-
if (PI && !PI->isAnalysis() && (this->HLSLPrintAfterAll || (this->HLSLPrintAfter.size() && this->HLSLPrintAfter.count(PI->getPassArgument())))) {
700-
class direct_stderr_stream : public raw_ostream {
701-
uint64_t current_pos() const override { return 0; }
702-
/// See raw_ostream::write_impl.
703-
void write_impl(const char *Ptr, size_t Size) override {
704-
fwrite(Ptr, Size, 1, stderr);
705-
}
706-
};
707-
708-
static direct_stderr_stream stderr_stream;
709-
710-
Pass *PP = P->createPrinterPass(
711-
stderr_stream, std::string("*** IR Dump After ") + P->getPassName().str() + " (" + PI->getPassArgument() + ") ***");
714+
if (PI && !PI->isAnalysis() &&
715+
ShouldPrint(PI, this->HLSLPrintAfterAll, this->HLSLPrintAfter)) {
716+
Pass *PP = P->createPrinterPass(stderr_stream,
717+
std::string("*** IR Dump After ") +
718+
P->getPassName().str() + " (" +
719+
PI->getPassArgument() + ") ***");
712720
PP->assignPassManager(activeStack, getTopLevelPassManagerType());
713721
}
714722
// HLSL Change - end
@@ -1420,6 +1428,8 @@ FunctionPassManager::~FunctionPassManager() {
14201428

14211429
void FunctionPassManager::add(Pass *P) {
14221430
// HLSL Change Starts
1431+
FPM->HLSLPrintBeforeAll = this->HLSLPrintBeforeAll;
1432+
FPM->HLSLPrintBefore = this->HLSLPrintBefore;
14231433
FPM->HLSLPrintAfterAll = this->HLSLPrintAfterAll;
14241434
FPM->HLSLPrintAfter = this->HLSLPrintAfter;
14251435
std::unique_ptr<Pass> PPtr(P); // take ownership of P, even on failure paths
@@ -1784,6 +1794,8 @@ PassManager::~PassManager() {
17841794

17851795
void PassManager::add(Pass *P) {
17861796
// HLSL Change Starts
1797+
PM->HLSLPrintBeforeAll = this->HLSLPrintBeforeAll;
1798+
PM->HLSLPrintBefore = this->HLSLPrintBefore;
17871799
PM->HLSLPrintAfterAll = this->HLSLPrintAfterAll;
17881800
PM->HLSLPrintAfter = this->HLSLPrintAfter;
17891801
std::unique_ptr<Pass> PPtr(P); // take ownership of P, even on failure paths

tools/clang/include/clang/Frontend/CodeGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
232232
unsigned ScanLimit = 0;
233233
/// Optimization pass enables, disables and selects
234234
hlsl::options::OptimizationToggles HLSLOptimizationToggles;
235+
/// Debug option to print IR before every pass
236+
bool HLSLPrintBeforeAll = false;
237+
/// Debug option to print IR before specific pass
238+
std::set<std::string> HLSLPrintBefore;
235239
/// Debug option to print IR after every pass
236240
bool HLSLPrintAfterAll = false;
237241
/// Debug option to print IR after specific pass

tools/clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class EmitAssemblyHelper {
9393
legacy::PassManager *getPerModulePasses() const {
9494
if (!PerModulePasses) {
9595
PerModulePasses = new legacy::PassManager();
96+
PerModulePasses->HLSLPrintBeforeAll =
97+
this->CodeGenOpts.HLSLPrintBeforeAll;
98+
PerModulePasses->HLSLPrintBefore = this->CodeGenOpts.HLSLPrintBefore;
9699
PerModulePasses->HLSLPrintAfterAll = this->CodeGenOpts.HLSLPrintAfterAll;
97100
PerModulePasses->HLSLPrintAfter = this->CodeGenOpts.HLSLPrintAfter;
98101
PerModulePasses->TrackPassOS = &PerModulePassesConfigOS;
@@ -105,7 +108,11 @@ class EmitAssemblyHelper {
105108
legacy::FunctionPassManager *getPerFunctionPasses() const {
106109
if (!PerFunctionPasses) {
107110
PerFunctionPasses = new legacy::FunctionPassManager(TheModule);
108-
PerFunctionPasses->HLSLPrintAfterAll = this->CodeGenOpts.HLSLPrintAfterAll;
111+
PerFunctionPasses->HLSLPrintBeforeAll =
112+
this->CodeGenOpts.HLSLPrintBeforeAll;
113+
PerFunctionPasses->HLSLPrintBefore = this->CodeGenOpts.HLSLPrintBefore;
114+
PerFunctionPasses->HLSLPrintAfterAll =
115+
this->CodeGenOpts.HLSLPrintAfterAll;
109116
PerFunctionPasses->HLSLPrintAfter = this->CodeGenOpts.HLSLPrintAfter;
110117
PerFunctionPasses->TrackPassOS = &PerFunctionPassesConfigOS;
111118
PerFunctionPasses->add(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %dxc -E main -T vs_6_0 %s -print-before-all 2>&1 | FileCheck -check-prefix=BEFORE1 --check-prefix=BEFORE2 %s
2+
// RUN: %dxc -E main -T vs_6_0 %s -print-after-all 2>&1 | FileCheck -check-prefix=AFTER1 --check-prefix=AFTER2 %s
3+
// RUN: %dxc -E main -T vs_6_0 %s -print-before-all -print-after-all 2>&1 | FileCheck -check-prefix=BEFORE1 -check-prefix=BEFORE2 -check-prefix=AFTER1 --check-prefix=AFTER2 %s
4+
// RUN: %dxc -E main -T vs_6_0 %s -print-before verify 2>&1 | FileCheck -check-prefix=BEFORE1 %s
5+
// RUN: %dxc -E main -T vs_6_0 %s -print-after hlsl-dxilemit 2>&1 | FileCheck -check-prefix=AFTER2 %s
6+
// RUN: %dxc -E main -T vs_6_0 %s -print-before verify -print-before hlsl-dxilemit 2>&1 | FileCheck -check-prefix=BEFORE1 -check-prefix=BEFORE2 %s
7+
// RUN: %dxc -E main -T vs_6_0 %s -print-after hlsl-dxilemit -print-after verify 2>&1 | FileCheck -check-prefix=AFTER1 -check-prefix=AFTER2 %s
8+
// RUN: %dxc -E main -T vs_6_0 %s -print-after hlsl-dxilemit -print-before verify 2>&1 | FileCheck -check-prefix=BEFORE1 -check-prefix=AFTER2 %s
9+
10+
// BEFORE1: *** IR Dump Before Module Verifier (verify) ***
11+
// BEFORE1: define void @main
12+
// AFTER1: *** IR Dump After Module Verifier (verify) ***
13+
// AFTER1: define void @main
14+
// BEFORE2: *** IR Dump Before HLSL DXIL Metadata Emit (hlsl-dxilemit) ***
15+
// BEFORE2: define void @main
16+
// AFTER2: *** IR Dump After HLSL DXIL Metadata Emit (hlsl-dxilemit) ***
17+
// AFTER2: define void @main
18+
19+
void main() {}

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,8 @@ class DxcCompiler : public IDxcCompiler3,
14181418
compiler.getCodeGenOpts().HLSLDefines = defines;
14191419
compiler.getCodeGenOpts().HLSLPreciseOutputs = Opts.PreciseOutputs;
14201420
compiler.getCodeGenOpts().MainFileName = pMainFile;
1421+
compiler.getCodeGenOpts().HLSLPrintBeforeAll = Opts.PrintBeforeAll;
1422+
compiler.getCodeGenOpts().HLSLPrintBefore = Opts.PrintBefore;
14211423
compiler.getCodeGenOpts().HLSLPrintAfterAll = Opts.PrintAfterAll;
14221424
compiler.getCodeGenOpts().HLSLPrintAfter = Opts.PrintAfter;
14231425
compiler.getCodeGenOpts().HLSLForceZeroStoreLifetimes = Opts.ForceZeroStoreLifetimes;

0 commit comments

Comments
 (0)