Skip to content

Commit f712c01

Browse files
committed
[HLSL]Add -O and -Od option for dxc mode.
Two new dxc mode options -O and -Od are added for dxc mode. -O is just alias of existing cc1 -O option. -Od will be lowered into -O0 and -dxc-opt-disable. -dxc-opt-disable is cc1 option added to for build ShaderFlags. Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D128845
1 parent c63e05d commit f712c01

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6402,6 +6402,9 @@ def cl_ignored_Group : OptionGroup<"<clang-cl ignored options>">,
64026402
class CLFlag<string name> : Option<["/", "-"], name, KIND_FLAG>,
64036403
Group<cl_Group>, Flags<[CLOption, NoXarchOption]>;
64046404

6405+
class CLDXCFlag<string name> : Option<["/", "-"], name, KIND_FLAG>,
6406+
Group<cl_Group>, Flags<[CLDXCOption, NoXarchOption]>;
6407+
64056408
class CLCompileFlag<string name> : Option<["/", "-"], name, KIND_FLAG>,
64066409
Group<cl_compile_Group>, Flags<[CLOption, NoXarchOption]>;
64076410

@@ -6411,6 +6414,9 @@ class CLIgnoredFlag<string name> : Option<["/", "-"], name, KIND_FLAG>,
64116414
class CLJoined<string name> : Option<["/", "-"], name, KIND_JOINED>,
64126415
Group<cl_Group>, Flags<[CLOption, NoXarchOption]>;
64136416

6417+
class CLDXCJoined<string name> : Option<["/", "-"], name, KIND_JOINED>,
6418+
Group<cl_Group>, Flags<[CLDXCOption, NoXarchOption]>;
6419+
64146420
class CLCompileJoined<string name> : Option<["/", "-"], name, KIND_JOINED>,
64156421
Group<cl_compile_Group>, Flags<[CLOption, NoXarchOption]>;
64166422

@@ -6507,7 +6513,7 @@ def _SLASH_J : CLFlag<"J">, HelpText<"Make char type unsigned">,
65076513

65086514
// The _SLASH_O option handles all the /O flags, but we also provide separate
65096515
// aliased options to provide separate help messages.
6510-
def _SLASH_O : CLJoined<"O">,
6516+
def _SLASH_O : CLDXCJoined<"O">,
65116517
HelpText<"Set multiple /O flags at once; e.g. '/O2y-' for '/O2 /Oy-'">,
65126518
MetaVarName<"<flags>">;
65136519
def : CLFlag<"O1">, Alias<_SLASH_O>, AliasArgs<["1"]>,
@@ -6520,7 +6526,7 @@ def : CLFlag<"Ob1">, Alias<_SLASH_O>, AliasArgs<["b1"]>,
65206526
HelpText<"Only inline functions explicitly or implicitly marked inline">;
65216527
def : CLFlag<"Ob2">, Alias<_SLASH_O>, AliasArgs<["b2"]>,
65226528
HelpText<"Inline functions as deemed beneficial by the compiler">;
6523-
def : CLFlag<"Od">, Alias<_SLASH_O>, AliasArgs<["d"]>,
6529+
def : CLDXCFlag<"Od">, Alias<_SLASH_O>, AliasArgs<["d"]>,
65246530
HelpText<"Disable optimization">;
65256531
def : CLFlag<"Og">, Alias<_SLASH_O>, AliasArgs<["g"]>,
65266532
HelpText<"No effect">;

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
4646
auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
4747
DXILValMD->addOperand(Val);
4848
}
49+
void addDisableOptimizations(llvm::Module &M) {
50+
StringRef Key = "dx.disable_optimizations";
51+
M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
52+
}
53+
4954
} // namespace
5055

5156
void CGHLSLRuntime::finishCodeGen() {
@@ -56,6 +61,8 @@ void CGHLSLRuntime::finishCodeGen() {
5661
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
5762

5863
generateGlobalCtorDtorCalls();
64+
if (CGM.getCodeGenOpts().OptimizationLevel == 0)
65+
addDisableOptimizations(M);
5966
}
6067

6168
void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,6 +3520,7 @@ static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
35203520
options::OPT_D,
35213521
options::OPT_I,
35223522
options::OPT_S,
3523+
options::OPT_O,
35233524
options::OPT_emit_llvm,
35243525
options::OPT_emit_obj,
35253526
options::OPT_disable_llvm_passes,

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
164164
A->claim();
165165
continue;
166166
}
167+
if (A->getOption().getID() == options::OPT__SLASH_O) {
168+
StringRef OStr = A->getValue();
169+
if (OStr == "d") {
170+
DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_O0));
171+
A->claim();
172+
continue;
173+
} else {
174+
DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), OStr);
175+
A->claim();
176+
continue;
177+
}
178+
}
167179
if (A->getOption().getID() == options::OPT_emit_pristine_llvm) {
168180
// Translate fcgl into -S -emit-llvm and -disable-llvm-passes.
169181
DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_S));
@@ -192,6 +204,9 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
192204
Opts.getOption(options::OPT_dxil_validator_version),
193205
DefaultValidatorVer);
194206
}
207+
if (!DAL->hasArg(options::OPT_O_Group)) {
208+
DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
209+
}
195210
// FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
196211
// shader model 6.2.
197212
return DAL;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -O0 -emit-llvm -xhlsl -o - %s | FileCheck %s
2+
// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -O3 -emit-llvm -xhlsl -o - %s | FileCheck %s --check-prefix=OPT
3+
4+
// CHECK:!"dx.disable_optimizations", i32 1}
5+
6+
// OPT-NOT:"dx.disable_optimizations"
7+
8+
float bar(float a, float b);
9+
10+
float foo(float a, float b) {
11+
return bar(a, b);
12+
}

clang/test/Driver/dxc_O.hlsl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_dxc -T lib_6_7 foo.hlsl -### %s 2>&1 | FileCheck %s
2+
// RUN: %clang_dxc -T lib_6_7 -Od foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=Od
3+
// RUN: %clang_dxc -T lib_6_7 -O0 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O0
4+
// RUN: %clang_dxc -T lib_6_7 -O1 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O1
5+
// RUN: %clang_dxc -T lib_6_7 -O2 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O2
6+
// RUN: %clang_dxc -T lib_6_7 -O3 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O3
7+
8+
// Make sure default is O3.
9+
// CHECK: "-O3"
10+
11+
// Make sure Od/O0 option flag which translated into "-O0".
12+
// Od: "-O0"
13+
// O0: "-O0"
14+
15+
// Make sure O1/O2/O3 is send to cc1.
16+
// O1: "-O1"
17+
// O2: "-O2"
18+
// O3: "-O3"

clang/test/Driver/dxc_fcgl.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_dxc -fcgl -T lib_6_7 foo.hlsl -### %s 2>&1 | FileCheck %s
22

33
// Make sure fcgl option flag which translated into "-S" "-emit-llvm" "-disable-llvm-passes".
4-
// CHECK:"-S" "-emit-llvm" "-disable-llvm-passes"
4+
// CHECK:"-S"
5+
// CHECK-SAME:"-emit-llvm" "-disable-llvm-passes"
56

0 commit comments

Comments
 (0)