HLSLOptions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // HLSLOptions.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. // Support for command-line-style option parsing. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #ifndef LLVM_HLSL_OPTIONS_H
  13. #define LLVM_HLSL_OPTIONS_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Option/ArgList.h"
  17. #include "dxc/dxcapi.h"
  18. #include "dxc/Support/SPIRVOptions.h"
  19. #include <map>
  20. namespace llvm {
  21. namespace opt {
  22. class OptTable;
  23. class raw_ostream;
  24. }
  25. }
  26. namespace dxc {
  27. class DxcDllSupport;
  28. }
  29. namespace hlsl {
  30. namespace options {
  31. /// Flags specifically for clang options. Must not overlap with
  32. /// llvm::opt::DriverFlag or (for clarity) with clang::driver::options.
  33. enum HlslFlags {
  34. DriverOption = (1 << 13),
  35. NoArgumentUnused = (1 << 14),
  36. CoreOption = (1 << 15),
  37. ISenseOption = (1 << 16),
  38. RewriteOption = (1 << 17),
  39. };
  40. enum ID {
  41. OPT_INVALID = 0, // This is not an option ID.
  42. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  43. HELPTEXT, METAVAR) OPT_##ID,
  44. #include "dxc/Support/HLSLOptions.inc"
  45. LastOption
  46. #undef OPTION
  47. };
  48. const llvm::opt::OptTable *getHlslOptTable();
  49. std::error_code initHlslOptTable();
  50. void cleanupHlslOptTable();
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // Helper classes to deal with options.
  53. /// Flags for IDxcCompiler APIs.
  54. static const unsigned CompilerFlags = HlslFlags::CoreOption;
  55. /// Flags for dxc.exe command-line tool.
  56. static const unsigned DxcFlags = HlslFlags::CoreOption | HlslFlags::DriverOption;
  57. /// Flags for dxr.exe command-line tool.
  58. static const unsigned DxrFlags = HlslFlags::RewriteOption | HlslFlags::DriverOption;
  59. /// Flags for IDxcIntelliSense APIs.
  60. static const unsigned ISenseFlags = HlslFlags::CoreOption | HlslFlags::ISenseOption;
  61. /// Use this class to capture preprocessor definitions and manage their lifetime.
  62. class DxcDefines {
  63. public:
  64. void push_back(llvm::StringRef value);
  65. LPWSTR DefineValues = nullptr;
  66. llvm::SmallVector<llvm::StringRef, 8> DefineStrings;
  67. llvm::SmallVector<DxcDefine, 8> DefineVector;
  68. ~DxcDefines() { delete[] DefineValues; }
  69. DxcDefines(const DxcDefines &) = delete;
  70. DxcDefines() {}
  71. void BuildDefines(); // Must be called after all defines are pushed back
  72. UINT32 ComputeNumberOfWCharsNeededForDefines();
  73. const DxcDefine *data() const { return DefineVector.data(); }
  74. unsigned size() const { return DefineVector.size(); }
  75. };
  76. struct RewriterOpts {
  77. bool Unchanged = false; // OPT_rw_unchanged
  78. bool SkipFunctionBody = false; // OPT_rw_skip_function_body
  79. bool SkipStatic = false; // OPT_rw_skip_static
  80. bool GlobalExternByDefault = false; // OPT_rw_global_extern_by_default
  81. bool KeepUserMacro = false; // OPT_rw_keep_user_macro
  82. bool ExtractEntryUniforms = false; // OPT_rw_extract_entry_uniforms
  83. bool RemoveUnusedGlobals = false; // OPT_rw_remove_unused_globals
  84. bool RemoveUnusedFunctions = false; // OPT_rw_remove_unused_functions
  85. bool WithLineDirective = false; // OPT_rw_line_directive
  86. };
  87. /// Use this class to capture all options.
  88. class DxcOpts {
  89. public:
  90. DxcDefines Defines;
  91. llvm::opt::InputArgList Args = llvm::opt::InputArgList(nullptr, nullptr); // Original arguments.
  92. llvm::StringRef AssemblyCode; // OPT_Fc
  93. llvm::StringRef DebugFile; // OPT_Fd
  94. llvm::StringRef EntryPoint; // OPT_entrypoint
  95. llvm::StringRef ExternalFn; // OPT_external_fn
  96. llvm::StringRef ExternalLib; // OPT_external_lib
  97. llvm::StringRef ExtractPrivateFile; // OPT_getprivate
  98. llvm::StringRef ForceRootSigVer; // OPT_force_rootsig_ver
  99. llvm::StringRef InputFile; // OPT_INPUT
  100. llvm::StringRef OutputHeader; // OPT_Fh
  101. llvm::StringRef OutputObject; // OPT_Fo
  102. llvm::StringRef OutputWarningsFile; // OPT_Fe
  103. llvm::StringRef OutputReflectionFile; // OPT_Fre
  104. llvm::StringRef OutputRootSigFile; // OPT_Frs
  105. llvm::StringRef OutputShaderHashFile; // OPT_Fsh
  106. llvm::StringRef Preprocess; // OPT_P
  107. llvm::StringRef TargetProfile; // OPT_target_profile
  108. llvm::StringRef VariableName; // OPT_Vn
  109. llvm::StringRef PrivateSource; // OPT_setprivate
  110. llvm::StringRef RootSignatureSource; // OPT_setrootsignature
  111. llvm::StringRef VerifyRootSignatureSource; //OPT_verifyrootsignature
  112. llvm::StringRef RootSignatureDefine; // OPT_rootsig_define
  113. llvm::StringRef FloatDenormalMode; // OPT_denorm
  114. std::vector<std::string> Exports; // OPT_exports
  115. std::vector<std::string> PreciseOutputs; // OPT_precise_output
  116. llvm::StringRef DefaultLinkage; // OPT_default_linkage
  117. unsigned DefaultTextCodePage = DXC_CP_UTF8; // OPT_encoding
  118. bool AllResourcesBound = false; // OPT_all_resources_bound
  119. bool AstDump = false; // OPT_ast_dump
  120. bool ColorCodeAssembly = false; // OPT_Cc
  121. bool CodeGenHighLevel = false; // OPT_fcgl
  122. bool AllowPreserveValues = false; // OPT_preserve_intermediate_values
  123. bool DebugInfo = false; // OPT__SLASH_Zi
  124. bool DebugNameForBinary = false; // OPT_Zsb
  125. bool DebugNameForSource = false; // OPT_Zss
  126. bool DumpBin = false; // OPT_dumpbin
  127. bool WarningAsError = false; // OPT__SLASH_WX
  128. bool IEEEStrict = false; // OPT_Gis
  129. bool IgnoreLineDirectives = false; // OPT_ignore_line_directives
  130. bool DefaultColMajor = false; // OPT_Zpc
  131. bool DefaultRowMajor = false; // OPT_Zpr
  132. bool DisableValidation = false; // OPT_VD
  133. unsigned OptLevel = 0; // OPT_O0/O1/O2/O3
  134. bool DisableOptimizations = false; // OPT_Od
  135. bool AvoidFlowControl = false; // OPT_Gfa
  136. bool PreferFlowControl = false; // OPT_Gfp
  137. bool EnableStrictMode = false; // OPT_Ges
  138. bool EnableDX9CompatMode = false; // OPT_Gec
  139. bool EnableFXCCompatMode = false; // internal flag
  140. unsigned long HLSLVersion = 0; // OPT_hlsl_version (2015-2018)
  141. bool Enable16BitTypes = false; // OPT_enable_16bit_types
  142. bool OptDump = false; // OPT_ODump - dump optimizer commands
  143. bool OutputWarnings = true; // OPT_no_warnings
  144. bool ShowHelp = false; // OPT_help
  145. bool ShowHelpHidden = false; // OPT__help_hidden
  146. bool ShowOptionNames = false; // OPT_fdiagnostics_show_option
  147. bool UseColor = false; // OPT_Cc
  148. bool UseHexLiterals = false; // OPT_Lx
  149. bool UseInstructionByteOffsets = false; // OPT_No
  150. bool UseInstructionNumbers = false; // OPT_Ni
  151. bool NotUseLegacyCBufLoad = false; // OPT_no_legacy_cbuf_layout
  152. bool PackPrefixStable = false; // OPT_pack_prefix_stable
  153. bool PackOptimized = false; // OPT_pack_optimized
  154. bool DisplayIncludeProcess = false; // OPT__vi
  155. bool RecompileFromBinary = false; // OPT _Recompile (Recompiling the DXBC binary file not .hlsl file)
  156. bool StripDebug = false; // OPT Qstrip_debug
  157. bool EmbedDebug = false; // OPT Qembed_debug
  158. bool SourceInDebugModule = false; // OPT Qsource_in_debug_module
  159. bool SourceOnlyDebug = false; // OPT Qsource_only_debug
  160. bool FullDebug = false; // OPT Qfull_debug
  161. bool StripRootSignature = false; // OPT_Qstrip_rootsignature
  162. bool StripPrivate = false; // OPT_Qstrip_priv
  163. bool StripReflection = false; // OPT_Qstrip_reflect
  164. bool KeepReflectionInDxil = false; // OPT_Qkeep_reflect_in_dxil
  165. bool StripReflectionFromDxil = false; // OPT_Qstrip_reflect_from_dxil
  166. bool ExtractRootSignature = false; // OPT_extractrootsignature
  167. bool DisassembleColorCoded = false; // OPT_Cc
  168. bool DisassembleInstNumbers = false; //OPT_Ni
  169. bool DisassembleByteOffset = false; //OPT_No
  170. bool DisaseembleHex = false; //OPT_Lx
  171. bool LegacyMacroExpansion = false; // OPT_flegacy_macro_expansion
  172. bool LegacyResourceReservation = false; // OPT_flegacy_resource_reservation
  173. unsigned long AutoBindingSpace = UINT_MAX; // OPT_auto_binding_space
  174. bool ExportShadersOnly = false; // OPT_export_shaders_only
  175. bool ResMayAlias = false; // OPT_res_may_alias
  176. unsigned long ValVerMajor = UINT_MAX, ValVerMinor = UINT_MAX; // OPT_validator_version
  177. unsigned ScanLimit = 0; // OPT_memdep_block_scan_limit
  178. bool ForceZeroStoreLifetimes = false; // OPT_force_zero_store_lifetimes
  179. bool EnableLifetimeMarkers = false; // OPT_enable_lifetime_markers
  180. // Optimization pass enables, disables and selects
  181. std::map<std::string, bool> DxcOptimizationToggles; // OPT_opt_enable & OPT_opt_disable
  182. std::map<std::string, std::string> DxcOptimizationSelects; // OPT_opt_select
  183. bool PrintAfterAll; // OPT_print_after_all
  184. // Rewriter Options
  185. RewriterOpts RWOpt;
  186. std::vector<std::string> Warnings;
  187. bool IsRootSignatureProfile();
  188. bool IsLibraryProfile();
  189. // Helpers to clarify interpretation of flags for behavior in implementation
  190. bool IsDebugInfoEnabled(); // Zi
  191. bool EmbedDebugInfo(); // Qembed_debug
  192. bool EmbedPDBName(); // Zi or Fd
  193. bool DebugFileIsDirectory(); // Fd ends in '\\'
  194. llvm::StringRef GetPDBName(); // Fd name
  195. // SPIRV Change Starts
  196. #ifdef ENABLE_SPIRV_CODEGEN
  197. bool GenSPIRV; // OPT_spirv
  198. clang::spirv::SpirvCodeGenOptions SpirvOptions; // All SPIR-V CodeGen-related options
  199. #endif
  200. // SPIRV Change Ends
  201. };
  202. /// Use this class to capture, convert and handle the lifetime for the
  203. /// command-line arguments to a program.
  204. class MainArgs {
  205. public:
  206. llvm::SmallVector<std::string, 8> Utf8StringVector;
  207. llvm::SmallVector<const char *, 8> Utf8CharPtrVector;
  208. MainArgs() = default;
  209. MainArgs(int argc, const wchar_t **argv, int skipArgCount = 1);
  210. MainArgs(int argc, const char **argv, int skipArgCount = 1);
  211. MainArgs(llvm::ArrayRef<llvm::StringRef> args);
  212. MainArgs& operator=(const MainArgs &other);
  213. llvm::ArrayRef<const char *> getArrayRef() const {
  214. return llvm::ArrayRef<const char *>(Utf8CharPtrVector.data(),
  215. Utf8CharPtrVector.size());
  216. }
  217. };
  218. /// Use this class to convert a StringRef into a wstring, handling empty values as nulls.
  219. class StringRefUtf16 {
  220. private:
  221. std::wstring m_value;
  222. public:
  223. StringRefUtf16(llvm::StringRef value);
  224. operator LPCWSTR() const { return m_value.size() ? m_value.data() : nullptr; }
  225. };
  226. /// Reads all options from the given argument strings, populates opts, and
  227. /// validates reporting errors and warnings.
  228. int ReadDxcOpts(const llvm::opt::OptTable *optionTable, unsigned flagsToInclude,
  229. const MainArgs &argStrings, DxcOpts &opts,
  230. llvm::raw_ostream &errors);
  231. /// Sets up the specified DxcDllSupport instance as per the given options.
  232. int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
  233. llvm::raw_ostream &errors);
  234. void CopyArgsToWStrings(const llvm::opt::InputArgList &inArgs,
  235. unsigned flagsToInclude,
  236. std::vector<std::wstring> &outArgs);
  237. }
  238. }
  239. #endif