From c95c076f520f93888eccd2f9794ebc9b03e1a873 Mon Sep 17 00:00:00 2001 From: Chris B Date: Mon, 7 Feb 2022 16:08:01 -0600 Subject: [PATCH] Pass compiler options to preprocessor (#4234) * Pass compiler options to preprocessor If we don't pass the compiler options to the preprocessor the preprocessor doesn't get initialized correctly. Without this change the preprocessor step identifies the shader model version as 0.0, pipeline stage as invalid, and language version as the default. * Remove arugment duplication --- tools/clang/tools/dxclib/dxc.cpp | 24 +++++++++---------- .../clang/tools/dxcompiler/dxcompilerobj.cpp | 4 ++-- utils/hct/cmdtestfiles/PipelineStage.hlsl | 21 ++++++++++++++++ utils/hct/cmdtestfiles/VersionMacro.hlsl | 1 + utils/hct/hcttestcmds.cmd | 24 +++++++++++++++++++ 5 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 utils/hct/cmdtestfiles/PipelineStage.hlsl create mode 100644 utils/hct/cmdtestfiles/VersionMacro.hlsl diff --git a/tools/clang/tools/dxclib/dxc.cpp b/tools/clang/tools/dxclib/dxc.cpp index 64a0833c4..eb6cff736 100644 --- a/tools/clang/tools/dxclib/dxc.cpp +++ b/tools/clang/tools/dxclib/dxc.cpp @@ -924,28 +924,26 @@ void DxcContext::Preprocess() { CComPtr pCompiler; CComPtr pPreprocessResult; CComPtr pSource; + + std::vector argStrings; + CopyArgsToWStrings(m_Opts.Args, CoreOption, argStrings); + std::vector args; + args.reserve(argStrings.size()); + for (const std::wstring &a : argStrings) + args.push_back(a.data()); CComPtr pLibrary; CComPtr pIncludeHandler; IFT(CreateInstance(CLSID_DxcLibrary, &pLibrary)); IFT(pLibrary->CreateIncludeHandler(&pIncludeHandler)); - // Carry forward the options that control preprocessor - if (m_Opts.LegacyMacroExpansion) - args.push_back(L"-flegacy-macro-expansion"); - - std::vector includePath; - for (const llvm::opt::Arg *A : m_Opts.Args.filtered(hlsl::options::OPT_I)) - includePath.emplace_back(Unicode::UTF8ToUTF16StringOrThrow(A->getValue())); - for (const std::wstring &directory : includePath) { - args.emplace_back(L"-I"); - args.emplace_back(directory.c_str()); // The strings are kept alive in the includePath vector - } - ReadFileIntoBlob(m_dxcSupport, StringRefUtf16(m_Opts.InputFile), &pSource); IFT(CreateInstance(CLSID_DxcCompiler, &pCompiler)); - IFT(pCompiler->Preprocess(pSource, StringRefUtf16(m_Opts.InputFile), args.data(), args.size(), m_Opts.Defines.data(), m_Opts.Defines.size(), pIncludeHandler, &pPreprocessResult)); + IFT(pCompiler->Preprocess(pSource, StringRefUtf16(m_Opts.InputFile), + args.data(), args.size(), m_Opts.Defines.data(), + m_Opts.Defines.size(), pIncludeHandler, + &pPreprocessResult)); WriteOperationErrorsToConsole(pPreprocessResult, m_Opts.OutputWarnings); HRESULT status; diff --git a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp index 360e9f447..5780257f9 100644 --- a/tools/clang/tools/dxcompiler/dxcompilerobj.cpp +++ b/tools/clang/tools/dxcompiler/dxcompilerobj.cpp @@ -815,8 +815,6 @@ public: } else { compiler.getLangOpts().HLSLEntryFunction = compiler.getCodeGenOpts().HLSLEntryFunction = pUtf8EntryPoint; - compiler.getLangOpts().HLSLProfile = - compiler.getCodeGenOpts().HLSLProfile = opts.TargetProfile; // Parse and apply if (opts.BindingTableDefine.size()) { @@ -1415,6 +1413,8 @@ public: compiler.getLangOpts().EnablePayloadAccessQualifiers = Opts.EnablePayloadQualifiers; compiler.getLangOpts().EnableShortCircuit = Opts.EnableShortCircuit; compiler.getLangOpts().EnableBitfields = Opts.EnableBitfields; + compiler.getLangOpts().HLSLProfile = + compiler.getCodeGenOpts().HLSLProfile = Opts.TargetProfile; // SPIRV change starts #ifdef ENABLE_SPIRV_CODEGEN diff --git a/utils/hct/cmdtestfiles/PipelineStage.hlsl b/utils/hct/cmdtestfiles/PipelineStage.hlsl new file mode 100644 index 000000000..c4dce40da --- /dev/null +++ b/utils/hct/cmdtestfiles/PipelineStage.hlsl @@ -0,0 +1,21 @@ +#if __SHADER_TARGET_STAGE == __SHADER_STAGE_VERTEX +#define __SHADER_STAGE_PREFIX vs +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_PIXEL +#define __SHADER_STAGE_PREFIX ps +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_GEOMETRY +#define __SHADER_STAGE_PREFIX gs +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_HULL +#define __SHADER_STAGE_PREFIX hs +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_DOMAIN +#define __SHADER_STAGE_PREFIX ds +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_COMPUTE +#define __SHADER_STAGE_PREFIX cs +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_AMPLIFICATION +#define __SHADER_STAGE_PREFIX as +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_MESH +#define __SHADER_STAGE_PREFIX ms +#elif __SHADER_TARGET_STAGE == __SHADER_STAGE_LIBRARY +#define __SHADER_STAGE_PREFIX lib +#endif + +__SHADER_STAGE_PREFIX __SHADER_TARGET_MAJOR __SHADER_TARGET_MINOR) diff --git a/utils/hct/cmdtestfiles/VersionMacro.hlsl b/utils/hct/cmdtestfiles/VersionMacro.hlsl new file mode 100644 index 000000000..c62f8c333 --- /dev/null +++ b/utils/hct/cmdtestfiles/VersionMacro.hlsl @@ -0,0 +1 @@ +__HLSL_VERSION diff --git a/utils/hct/hcttestcmds.cmd b/utils/hct/hcttestcmds.cmd index d91aec0ab..f9669751c 100644 --- a/utils/hct/hcttestcmds.cmd +++ b/utils/hct/hcttestcmds.cmd @@ -443,6 +443,30 @@ if %Failed% neq 0 goto :failed call :run dxc.exe -P include-main.hlsl.pp -I inc subfolder\include-main.hlsl if %Failed% neq 0 goto :failed +set testname=Test Version macro +for %%v in (2016 2017 2018 2021) do ( + call :run dxc.exe -HV %%v -P %%v.hlsl.pp %testfiles%\VersionMacro.hlsl + if %Failed% neq 0 goto :failed + call :check_file %%v.hlsl.pp find %%v del + if %Failed% neq 0 goto :failed +) + +set testname=Test shader profile macro +for %%p in (vs ps gs hs ds cs lib) do ( + for %%v in (5 6) do ( + if "%%p" == "lib" ( if %%v leq 2 goto :next ) + if "%%p" == "ms" ( if %%v leq 4 goto :next ) + if "%%p" == "as" ( if %%v leq 4 goto :next ) + call :run dxc.exe -T %%p_6_%%v -P %%p_%%v.hlsl.pp %testfiles%\PipelineStage.hlsl + if %Failed% neq 0 goto :failed + call :check_file %%p_%%v.hlsl.pp find "%%p 6 %%v" del + if %Failed% neq 0 goto :failed + + :next + rem Skip to the next iteration + ) +) + set testname=Byte Order Markers call :run dxc.exe /T ps_6_0 "%testfiles%\bom-main-ascii.hlsl" call :run dxc.exe /T ps_6_0 "%testfiles%\bom-main-utf8.hlsl"