diff --git a/clang/include/clang/Basic/TargetOptions.h b/clang/include/clang/Basic/TargetOptions.h --- a/clang/include/clang/Basic/TargetOptions.h +++ b/clang/include/clang/Basic/TargetOptions.h @@ -113,6 +113,9 @@ /// The validator version for dxil. std::string DxilValidatorVersion; + + /// Keep the information about disable optimization for ShaderFlags. + bool DxcOptDisable = false; }; } // end namespace clang diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -6854,6 +6854,14 @@ "as_6_5, as_6_6, as_6_7">; def dxc_D : Option<["--", "/", "-"], "D", KIND_JOINED_OR_SEPARATE>, Group, Flags<[DXCOption, NoXarchOption]>, Alias; +def dxc_O : Option<["--", "/", "-"], "O", KIND_JOINED>, + Group, Flags<[DXCOption, NoXarchOption]>, Alias; +def dxc_Od : Option<["--", "/", "-"], "Od", KIND_FLAG>, + Group, Flags<[DXCOption, NoXarchOption]>, + HelpText<"Disable optimizations.">; +def dxc_opt_disable : Flag<["-"], "dxc-opt-disable">, Group, Flags<[CC1Option]>, + MarshallingInfoFlag>, + HelpText<"HLSL only. This option disables all optimizations. By default optimizations are enabled.">; def emit_pristine_llvm : DXCFlag<"emit-pristine-llvm">, HelpText<"Emit pristine LLVM IR from the frontend by not running any LLVM passes at all." "Same as -S + -emit-llvm + -disable-llvm-passes.">; diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -42,6 +42,11 @@ StringRef DxilValKey = "dx.valver"; M.addModuleFlag(llvm::Module::ModFlagBehavior::AppendUnique, DxilValKey, Val); } +void addDisableOptimizations(llvm::Module &M) { + StringRef Key = "dx.disable_optimizations"; + M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1); +} + } // namespace void CGHLSLRuntime::finishCodeGen() { @@ -49,4 +54,6 @@ llvm::Module &M = CGM.getModule(); addDxilValVersion(TargetOpts.DxilValidatorVersion, M); + if (TargetOpts.DxcOptDisable) + addDisableOptimizations(M); } diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3482,6 +3482,8 @@ const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version, options::OPT_D, options::OPT_S, + options::OPT_O, + options::OPT_dxc_opt_disable, options::OPT_emit_llvm, options::OPT_disable_llvm_passes, options::OPT_fnative_half_type}; diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp --- a/clang/lib/Driver/ToolChains/HLSL.cpp +++ b/clang/lib/Driver/ToolChains/HLSL.cpp @@ -158,6 +158,12 @@ if (!isLegalValidatorVersion(ValVerStr, getDriver())) continue; } + if (A->getOption().getID() == options::OPT_dxc_Od) { + DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_dxc_opt_disable)); + DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_O0)); + A->claim(); + continue; + } if (A->getOption().getID() == options::OPT_emit_pristine_llvm) { // Translate fcgl into -S -emit-llvm and -disable-llvm-passes. DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_S)); @@ -177,6 +183,9 @@ Opts.getOption(options::OPT_dxil_validator_version), DefaultValidatorVer); } + if (!DAL->hasArg(options::OPT_O_Group)) { + DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3"); + } // FIXME: add validation for enable_16bit_types should be after HLSL 2018 and // shader model 6.2. return DAL; diff --git a/clang/test/CodeGenHLSL/disable_opt.hlsl b/clang/test/CodeGenHLSL/disable_opt.hlsl new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenHLSL/disable_opt.hlsl @@ -0,0 +1,12 @@ +// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -dxc-opt-disable -emit-llvm -xhlsl -o - %s | FileCheck %s +// RUN: %clang -cc1 -S -triple dxil-pc-shadermodel6.3-library -emit-llvm -xhlsl -o - %s | FileCheck %s --check-prefix=OPT + +// CHECK:!"dx.disable_optimizations", i32 1} + +// OPT-NOT:"dx.disable_optimizations" + +float bar(float a, float b); + +float foo(float a, float b) { + return bar(a, b); +} diff --git a/clang/test/Driver/dxc_O.hlsl b/clang/test/Driver/dxc_O.hlsl new file mode 100644 --- /dev/null +++ b/clang/test/Driver/dxc_O.hlsl @@ -0,0 +1,19 @@ +// RUN: %clang_dxc -T lib_6_7 foo.hlsl -### %s 2>&1 | FileCheck %s +// RUN: %clang_dxc -T lib_6_7 -Od foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O0 +// RUN: %clang_dxc -T lib_6_7 -O0 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O0 +// RUN: %clang_dxc -T lib_6_7 -O1 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O1 +// RUN: %clang_dxc -T lib_6_7 -O2 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O2 +// RUN: %clang_dxc -T lib_6_7 -O3 foo.hlsl -### %s 2>&1 | FileCheck %s --check-prefix=O3 + +// Make sure default is O3. +// CHECK: "-O3" + +// Make sure Od option flag which translated into "-O0" "-dxc-opt-disable" +// Od: "-O0" +// Od-SAME: "-dxc-opt-disable" + +// Make sure O0/O1/O2/O3 is send to cc1. +// O0: "-O0" +// O1: "-O1" +// O2: "-O2" +// O3: "-O3"