Skip to content

Commit fc796a0

Browse files
authored
[NFC] Refactor -opt-enable and -opt-disable (microsoft#5516)
Refactored -opt-enable and -opt-disable. - Added `hlsl::option::OptimizationToggles` to manage the toggles instead of checking a `std::map`. - All the options are moved into `hlsl::options::TOGGLE_*` constants, where each constant contains both the option's name and whether it's default on or off. + Previously, every check had to be either `toggles.count("my-flag") && toggles.find("my-flag")->second` for default off, and `!toggles.count("my-flag") || toggles.map.find("my-flag")->second` for default on. + Now, a check is simply `toggles.IsEnabled(TOGGLE_MY_FLAG)`.
1 parent daeba0c commit fc796a0

File tree

9 files changed

+108
-54
lines changed

9 files changed

+108
-54
lines changed

include/dxc/HLSL/HLSLExtensionsCodegenHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#pragma once
1313
#include "dxc/DXIL/DxilOperations.h"
14+
#include "dxc/Support/DxcOptToggles.h"
1415
#include <vector>
1516
#include <string>
1617

@@ -69,7 +70,7 @@ class HLSLExtensionsCodegenHelper {
6970
virtual void UpdateCodeGenOptions(clang::CodeGenOptions &CGO) = 0;
7071
// Query the named option enable
7172
// Needed because semantic defines may have set it since options were copied
72-
virtual bool IsOptionEnabled(std::string option) = 0;
73+
virtual bool IsOptionEnabled(hlsl::options::Toggle toggle) = 0;
7374

7475
// Get the name to use for the dxil intrinsic function.
7576
virtual std::string GetIntrinsicName(unsigned opcode) = 0;

include/dxc/Support/DxcOptToggles.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// //
3+
// DxcOptToggles.h //
4+
// Copyright (C) Microsoft Corporation. All rights reserved. //
5+
// This file is distributed under the University of Illinois Open Source //
6+
// License. See LICENSE.TXT for details. //
7+
// //
8+
// Helper code for representing -opt-disable, -opt-enable, -opt-select //
9+
// options //
10+
// //
11+
///////////////////////////////////////////////////////////////////////////////
12+
13+
#pragma once
14+
15+
#ifndef LLVM_HLSL_DXC_OPT_TOGGLES_H
16+
#define LLVM_HLSL_DXC_OPT_TOGGLES_H
17+
18+
#include "llvm/ADT/StringRef.h"
19+
#include <map>
20+
#include <set>
21+
#include <string>
22+
23+
namespace hlsl {
24+
25+
namespace options {
26+
27+
struct Toggle {
28+
llvm::StringRef Name;
29+
bool Default = false;
30+
Toggle(llvm::StringRef Name, bool Default) : Name(Name), Default(Default) {}
31+
};
32+
33+
enum {
34+
DEFAULT_ON = 1,
35+
DEFAULT_OFF = 0,
36+
};
37+
38+
static const Toggle TOGGLE_GVN = {"gvn", DEFAULT_ON};
39+
static const Toggle TOGGLE_LICM = {"licm", DEFAULT_ON};
40+
static const Toggle TOGGLE_SINK = {"sink", DEFAULT_ON};
41+
static const Toggle TOGGLE_LIFETIME_MARKERS = {"lifetime-markers", DEFAULT_ON};
42+
static const Toggle TOGGLE_PARTIAL_LIFETIME_MARKERS = {
43+
"partial-lifetime-markers", DEFAULT_OFF};
44+
static const Toggle TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL = {
45+
"structurize-loop-exits-for-unroll", DEFAULT_ON};
46+
static const Toggle TOGGLE_DEBUG_NOPS = {"debug-nops", DEFAULT_ON};
47+
static const Toggle TOGGLE_STRUCTURIZE_RETURNS = {"structurize-returns", DEFAULT_OFF};
48+
49+
struct OptimizationToggles {
50+
// Optimization pass enables, disables and selects
51+
std::map<std::string, bool> Toggles; // OPT_opt_enable & OPT_opt_disable
52+
std::map<std::string, std::string> Selects; // OPT_opt_select
53+
54+
void Set(Toggle Opt, bool Value) {
55+
Toggles[Opt.Name] = Value;
56+
}
57+
bool IsSet(Toggle Opt) const {
58+
return Toggles.find(Opt.Name) != Toggles.end();
59+
}
60+
bool IsEnabled(Toggle Opt) const {
61+
auto It = Toggles.find(Opt.Name);
62+
const bool Found = It != Toggles.end();
63+
if (Found)
64+
return It->second;
65+
return Opt.Default;
66+
}
67+
};
68+
69+
} // namespace options
70+
} // namespace hlsl
71+
72+
#endif

include/dxc/Support/HLSLOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "dxc/dxcapi.h"
2121
#include "dxc/Support/HLSLVersion.h"
2222
#include "dxc/Support/SPIRVOptions.h"
23+
#include "dxc/Support/DxcOptToggles.h"
2324
#include <map>
2425
#include <set>
2526

@@ -213,8 +214,7 @@ class DxcOpts {
213214
bool VerifyDiagnostics = false; // OPT_verify
214215

215216
// Optimization pass enables, disables and selects
216-
std::map<std::string, bool> DxcOptimizationToggles; // OPT_opt_enable & OPT_opt_disable
217-
std::map<std::string, std::string> DxcOptimizationSelects; // OPT_opt_select
217+
OptimizationToggles OptToggles; // OPT_opt_enable, OPT_opt_disable, OPT_opt_select
218218

219219
std::set<std::string> IgnoreSemDefs; // OPT_ignore_semdef
220220
std::map<std::string, std::string> OverrideSemDefs; // OPT_override_semdef

lib/DxcSupport/HLSLOptions.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,17 +604,17 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
604604
opts.ScanLimit = std::stoul(std::string(limit));
605605

606606
for (std::string opt : Args.getAllArgValues(OPT_opt_disable))
607-
opts.DxcOptimizationToggles[llvm::StringRef(opt).lower()] = false;
607+
opts.OptToggles.Toggles[llvm::StringRef(opt).lower()] = false;
608608

609609
for (std::string opt : Args.getAllArgValues(OPT_opt_enable)) {
610610
std::string optimization = llvm::StringRef(opt).lower();
611-
if (opts.DxcOptimizationToggles.count(optimization) &&
612-
!opts.DxcOptimizationToggles[optimization]) {
611+
if (opts.OptToggles.Toggles.count(optimization) &&
612+
!opts.OptToggles.Toggles[optimization]) {
613613
errors << "Contradictory use of -opt-disable and -opt-enable with \""
614614
<< llvm::StringRef(opt).lower() << "\"";
615615
return 1;
616616
}
617-
opts.DxcOptimizationToggles[optimization] = true;
617+
opts.OptToggles.Toggles[optimization] = true;
618618
}
619619

620620
std::vector<std::string> ignoreSemDefs = Args.getAllArgValues(OPT_ignore_semdef);
@@ -638,13 +638,13 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
638638
for (unsigned i = 0; i + 1 < optSelects.size(); i+=2) {
639639
std::string optimization = llvm::StringRef(optSelects[i]).lower();
640640
std::string selection = optSelects[i+1];
641-
if (opts.DxcOptimizationSelects.count(optimization) &&
642-
selection.compare(opts.DxcOptimizationSelects[optimization])) {
641+
if (opts.OptToggles.Selects.count(optimization) &&
642+
selection.compare(opts.OptToggles.Selects[optimization])) {
643643
errors << "Contradictory -opt-selects for \""
644644
<< optimization << "\"";
645645
return 1;
646646
}
647-
opts.DxcOptimizationSelects[optimization] = selection;
647+
opts.OptToggles.Selects[optimization] = selection;
648648
}
649649

650650
if (!opts.ForceRootSigVer.empty() && opts.ForceRootSigVer != "rootsig_1_0" &&

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
#include <set>
2424
#include "dxc/HLSL/HLSLExtensionsCodegenHelper.h" // HLSL change
2525
#include "dxc/Support/SPIRVOptions.h" // SPIR-V Change
26-
#include "dxc/DxcBindingTable/DxcBindingTable.h" // HLSL chanhge
26+
#include "dxc/DxcBindingTable/DxcBindingTable.h" // HLSL change
27+
#include "dxc/Support/DxcOptToggles.h" // HLSL change
2728

2829
namespace clang {
2930

@@ -229,9 +230,8 @@ class CodeGenOptions : public CodeGenOptionsBase {
229230
bool HLSLResMayAlias = false;
230231
/// Lookback scan limit for memory dependencies
231232
unsigned ScanLimit = 0;
232-
// Optimization pass enables, disables and selects
233-
std::map<std::string, bool> HLSLOptimizationToggles;
234-
std::map<std::string, std::string> HLSLOptimizationSelects;
233+
/// Optimization pass enables, disables and selects
234+
hlsl::options::OptimizationToggles HLSLOptimizationToggles;
235235
/// Debug option to print IR after every pass
236236
bool HLSLPrintAfterAll = false;
237237
/// Debug option to print IR after specific pass
@@ -240,8 +240,6 @@ class CodeGenOptions : public CodeGenOptionsBase {
240240
bool HLSLForceZeroStoreLifetimes = false;
241241
/// Enable lifetime marker generation
242242
bool HLSLEnableLifetimeMarkers = false;
243-
/// Enable lifetime marker generation only for lifetime.start
244-
bool HLSLEnablePartialLifetimeMarkers = false;
245243
/// Put shader sources and options in the module
246244
bool HLSLEmbedSourcesInModule = false;
247245
/// Enable generation of payload access qualifier metadata.

tools/clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -337,26 +337,19 @@ void EmitAssemblyHelper::CreatePasses() {
337337
PMBuilder.HLSLResMayAlias = CodeGenOpts.HLSLResMayAlias;
338338
PMBuilder.ScanLimit = CodeGenOpts.ScanLimit;
339339

340-
PMBuilder.EnableGVN = !CodeGenOpts.HLSLOptimizationToggles.count("gvn") ||
341-
CodeGenOpts.HLSLOptimizationToggles.find("gvn")->second;
342-
343-
PMBuilder.HLSLNoSink = CodeGenOpts.HLSLOptimizationToggles.count("sink") &&
344-
!CodeGenOpts.HLSLOptimizationToggles.find("sink")->second;
345-
346-
PMBuilder.StructurizeLoopExitsForUnroll =
347-
!CodeGenOpts.HLSLOptimizationToggles.count("structurize-loop-exits-for-unroll") ||
348-
CodeGenOpts.HLSLOptimizationToggles.find("structurize-loop-exits-for-unroll")->second;
349-
350-
PMBuilder.HLSLEnableDebugNops =
351-
!CodeGenOpts.HLSLOptimizationToggles.count("debug-nops") ||
352-
CodeGenOpts.HLSLOptimizationToggles.find("debug-nops")->second;
353-
340+
// Opt toggles
341+
const hlsl::options::OptimizationToggles &OptToggles =
342+
CodeGenOpts.HLSLOptimizationToggles;
343+
PMBuilder.EnableGVN = OptToggles.IsEnabled(hlsl::options::TOGGLE_GVN);
344+
PMBuilder.HLSLNoSink = !OptToggles.IsEnabled(hlsl::options::TOGGLE_SINK);
345+
PMBuilder.StructurizeLoopExitsForUnroll = OptToggles.IsEnabled(
346+
hlsl::options::TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL);
347+
PMBuilder.HLSLEnableDebugNops = OptToggles.IsEnabled(hlsl::options::TOGGLE_DEBUG_NOPS);
354348
PMBuilder.HLSLEnableLifetimeMarkers =
355349
CodeGenOpts.HLSLEnableLifetimeMarkers &&
356-
(!CodeGenOpts.HLSLOptimizationToggles.count("lifetime-markers") ||
357-
CodeGenOpts.HLSLOptimizationToggles.find("lifetime-markers")->second);
358-
359-
PMBuilder.HLSLEnablePartialLifetimeMarkers = CodeGenOpts.HLSLEnablePartialLifetimeMarkers;
350+
OptToggles.IsEnabled(hlsl::options::TOGGLE_LIFETIME_MARKERS);
351+
PMBuilder.HLSLEnablePartialLifetimeMarkers =
352+
OptToggles.IsEnabled(hlsl::options::TOGGLE_PARTIAL_LIFETIME_MARKERS);
360353
// HLSL Change - end
361354

362355
PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;

tools/clang/lib/CodeGen/CGDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ llvm::Value *CodeGenFunction::EmitLifetimeStart(uint64_t Size,
891891
llvm::CallInst *C =
892892
Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
893893
C->setDoesNotThrow();
894-
if (CGM.getCodeGenOpts().HLSLEnablePartialLifetimeMarkers) return nullptr; // HLSL Change - Returning nullptr prevents generating lifetime.end
894+
if (CGM.getCodeGenOpts().HLSLOptimizationToggles.IsEnabled(hlsl::options::TOGGLE_PARTIAL_LIFETIME_MARKERS)) return nullptr; // HLSL Change - Returning nullptr prevents generating lifetime.end
895895
return SizeV;
896896
}
897897

tools/clang/lib/CodeGen/CGHLSLMSFinishCodeGen.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,15 +3696,10 @@ void StructurizeMultiRet(Module &M, clang::CodeGen::CodeGenModule &CGM,
36963696
bool bWaveEnabledStage,
36973697
SmallVector<BranchInst *, 16> &DxBreaks) {
36983698
if (CGM.getCodeGenOpts().HLSLExtensionsCodegen) {
3699-
if (!CGM.getCodeGenOpts().HLSLExtensionsCodegen->IsOptionEnabled(
3700-
"structurize-returns"))
3699+
if (!CGM.getCodeGenOpts().HLSLExtensionsCodegen->IsOptionEnabled(hlsl::options::TOGGLE_STRUCTURIZE_RETURNS))
37013700
return;
37023701
} else {
3703-
if (!CGM.getCodeGenOpts().HLSLOptimizationToggles.count(
3704-
"structurize-returns") ||
3705-
!CGM.getCodeGenOpts()
3706-
.HLSLOptimizationToggles.find("structurize-returns")
3707-
->second)
3702+
if (!CGM.getCodeGenOpts().HLSLOptimizationToggles.IsEnabled(hlsl::options::TOGGLE_STRUCTURIZE_RETURNS))
37083703
return;
37093704
}
37103705

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ class HLSLExtensionsCodegenHelperImpl : public HLSLExtensionsCodegenHelper {
277277
const std::string disableStr("_DISABLE_");
278278
const std::string selectStr("_SELECT_");
279279

280-
auto &optToggles = m_CI.getCodeGenOpts().HLSLOptimizationToggles;
281-
auto &optSelects = m_CI.getCodeGenOpts().HLSLOptimizationSelects;
280+
auto &optToggles = m_CI.getCodeGenOpts().HLSLOptimizationToggles.Toggles;
281+
auto &optSelects = m_CI.getCodeGenOpts().HLSLOptimizationToggles.Selects;
282282

283283
const llvm::SmallVector<std::string, 2> &semDefPrefixes =
284284
m_langExtensionsHelper.GetSemanticDefines();
@@ -372,12 +372,11 @@ class HLSLExtensionsCodegenHelperImpl : public HLSLExtensionsCodegenHelper {
372372
void UpdateCodeGenOptions(clang::CodeGenOptions &CGO) override {
373373
auto &CodeGenOpts = m_CI.getCodeGenOpts();
374374
CGO.HLSLEnableLifetimeMarkers &=
375-
(!CodeGenOpts.HLSLOptimizationToggles.count("lifetime-markers") ||
376-
CodeGenOpts.HLSLOptimizationToggles.find("lifetime-markers")->second);
375+
CodeGenOpts.HLSLOptimizationToggles.IsEnabled(
376+
hlsl::options::TOGGLE_LIFETIME_MARKERS);
377377
}
378-
virtual bool IsOptionEnabled(std::string option) override {
379-
return m_CI.getCodeGenOpts().HLSLOptimizationToggles.count(option) &&
380-
m_CI.getCodeGenOpts().HLSLOptimizationToggles.find(option)->second;
378+
virtual bool IsOptionEnabled(hlsl::options::Toggle toggle) override {
379+
return m_CI.getCodeGenOpts().HLSLOptimizationToggles.IsEnabled(toggle);
381380
}
382381

383382
virtual std::string GetIntrinsicName(UINT opcode) override {
@@ -1362,9 +1361,6 @@ class DxcCompiler : public IDxcCompiler3,
13621361
compiler.getLangOpts().HLSLProfile =
13631362
compiler.getCodeGenOpts().HLSLProfile = Opts.TargetProfile;
13641363

1365-
compiler.getCodeGenOpts().HLSLEnablePartialLifetimeMarkers =
1366-
Opts.DxcOptimizationToggles.count("partial-lifetime-markers") && Opts.DxcOptimizationToggles.find("partial-lifetime-markers")->second;
1367-
13681364
// Enable dumping implicit top level decls either if it was specifically
13691365
// requested or if we are not dumping the ast from the command line. That
13701366
// allows us to dump implicit AST nodes in the debugger.
@@ -1410,8 +1406,7 @@ class DxcCompiler : public IDxcCompiler3,
14101406
compiler.getCodeGenOpts().HLSLOnlyWarnOnUnrollFail = Opts.EnableFXCCompatMode;
14111407
compiler.getCodeGenOpts().HLSLResMayAlias = Opts.ResMayAlias;
14121408
compiler.getCodeGenOpts().ScanLimit = Opts.ScanLimit;
1413-
compiler.getCodeGenOpts().HLSLOptimizationToggles = Opts.DxcOptimizationToggles;
1414-
compiler.getCodeGenOpts().HLSLOptimizationSelects = Opts.DxcOptimizationSelects;
1409+
compiler.getCodeGenOpts().HLSLOptimizationToggles = Opts.OptToggles;
14151410
compiler.getCodeGenOpts().HLSLAllResourcesBound = Opts.AllResourcesBound;
14161411
compiler.getCodeGenOpts().HLSLIgnoreOptSemDefs = Opts.IgnoreOptSemDefs;
14171412
compiler.getCodeGenOpts().HLSLIgnoreSemDefs = Opts.IgnoreSemDefs;

0 commit comments

Comments
 (0)