HLSLOptions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Link = false; // OPT_link
  128. bool WarningAsError = false; // OPT__SLASH_WX
  129. bool IEEEStrict = false; // OPT_Gis
  130. bool IgnoreLineDirectives = false; // OPT_ignore_line_directives
  131. bool DefaultColMajor = false; // OPT_Zpc
  132. bool DefaultRowMajor = false; // OPT_Zpr
  133. bool DisableValidation = false; // OPT_VD
  134. unsigned OptLevel = 0; // OPT_O0/O1/O2/O3
  135. bool DisableOptimizations = false; // OPT_Od
  136. bool AvoidFlowControl = false; // OPT_Gfa
  137. bool PreferFlowControl = false; // OPT_Gfp
  138. bool EnableStrictMode = false; // OPT_Ges
  139. bool EnableDX9CompatMode = false; // OPT_Gec
  140. bool EnableFXCCompatMode = false; // internal flag
  141. unsigned long HLSLVersion = 0; // OPT_hlsl_version (2015-2018)
  142. bool Enable16BitTypes = false; // OPT_enable_16bit_types
  143. bool OptDump = false; // OPT_ODump - dump optimizer commands
  144. bool OutputWarnings = true; // OPT_no_warnings
  145. bool ShowHelp = false; // OPT_help
  146. bool ShowHelpHidden = false; // OPT__help_hidden
  147. bool ShowOptionNames = false; // OPT_fdiagnostics_show_option
  148. bool UseColor = false; // OPT_Cc
  149. bool UseHexLiterals = false; // OPT_Lx
  150. bool UseInstructionByteOffsets = false; // OPT_No
  151. bool UseInstructionNumbers = false; // OPT_Ni
  152. bool NotUseLegacyCBufLoad = false; // OPT_no_legacy_cbuf_layout
  153. bool PackPrefixStable = false; // OPT_pack_prefix_stable
  154. bool PackOptimized = false; // OPT_pack_optimized
  155. bool DisplayIncludeProcess = false; // OPT__vi
  156. bool RecompileFromBinary = false; // OPT _Recompile (Recompiling the DXBC binary file not .hlsl file)
  157. bool StripDebug = false; // OPT Qstrip_debug
  158. bool EmbedDebug = false; // OPT Qembed_debug
  159. bool SourceInDebugModule = false; // OPT Zs
  160. bool SourceOnlyDebug = false; // OPT Qsource_only_debug
  161. bool PdbInPrivate = false; // OPT Qpdb_in_private
  162. bool StripRootSignature = false; // OPT_Qstrip_rootsignature
  163. bool StripPrivate = false; // OPT_Qstrip_priv
  164. bool StripReflection = false; // OPT_Qstrip_reflect
  165. bool KeepReflectionInDxil = false; // OPT_Qkeep_reflect_in_dxil
  166. bool StripReflectionFromDxil = false; // OPT_Qstrip_reflect_from_dxil
  167. bool ExtractRootSignature = false; // OPT_extractrootsignature
  168. bool DisassembleColorCoded = false; // OPT_Cc
  169. bool DisassembleInstNumbers = false; //OPT_Ni
  170. bool DisassembleByteOffset = false; //OPT_No
  171. bool DisaseembleHex = false; //OPT_Lx
  172. bool LegacyMacroExpansion = false; // OPT_flegacy_macro_expansion
  173. bool LegacyResourceReservation = false; // OPT_flegacy_resource_reservation
  174. unsigned long AutoBindingSpace = UINT_MAX; // OPT_auto_binding_space
  175. bool ExportShadersOnly = false; // OPT_export_shaders_only
  176. bool ResMayAlias = false; // OPT_res_may_alias
  177. unsigned long ValVerMajor = UINT_MAX, ValVerMinor = UINT_MAX; // OPT_validator_version
  178. unsigned ScanLimit = 0; // OPT_memdep_block_scan_limit
  179. bool ForceZeroStoreLifetimes = false; // OPT_force_zero_store_lifetimes
  180. bool EnableLifetimeMarkers = false; // OPT_enable_lifetime_markers
  181. // Optimization pass enables, disables and selects
  182. std::map<std::string, bool> DxcOptimizationToggles; // OPT_opt_enable & OPT_opt_disable
  183. std::map<std::string, std::string> DxcOptimizationSelects; // OPT_opt_select
  184. bool PrintAfterAll; // OPT_print_after_all
  185. bool EnablePayloadQualifiers = false; // OPT_enable_payload_qualifiers
  186. bool HandleExceptions = false; // OPT_disable_exception_handling
  187. // Rewriter Options
  188. RewriterOpts RWOpt;
  189. std::vector<std::string> Warnings;
  190. bool IsRootSignatureProfile();
  191. bool IsLibraryProfile();
  192. // Helpers to clarify interpretation of flags for behavior in implementation
  193. bool GenerateFullDebugInfo(); // Zi
  194. bool GeneratePDB(); // Zi or Zs
  195. bool EmbedDebugInfo(); // Qembed_debug
  196. bool EmbedPDBName(); // Zi or Fd
  197. bool DebugFileIsDirectory(); // Fd ends in '\\'
  198. llvm::StringRef GetPDBName(); // Fd name
  199. // SPIRV Change Starts
  200. #ifdef ENABLE_SPIRV_CODEGEN
  201. bool GenSPIRV; // OPT_spirv
  202. clang::spirv::SpirvCodeGenOptions SpirvOptions; // All SPIR-V CodeGen-related options
  203. #endif
  204. // SPIRV Change Ends
  205. };
  206. /// Use this class to capture, convert and handle the lifetime for the
  207. /// command-line arguments to a program.
  208. class MainArgs {
  209. public:
  210. llvm::SmallVector<std::string, 8> Utf8StringVector;
  211. llvm::SmallVector<const char *, 8> Utf8CharPtrVector;
  212. MainArgs() = default;
  213. MainArgs(int argc, const wchar_t **argv, int skipArgCount = 1);
  214. MainArgs(int argc, const char **argv, int skipArgCount = 1);
  215. MainArgs(llvm::ArrayRef<llvm::StringRef> args);
  216. MainArgs& operator=(const MainArgs &other);
  217. llvm::ArrayRef<const char *> getArrayRef() const {
  218. return llvm::ArrayRef<const char *>(Utf8CharPtrVector.data(),
  219. Utf8CharPtrVector.size());
  220. }
  221. };
  222. /// Use this class to convert a StringRef into a wstring, handling empty values as nulls.
  223. class StringRefUtf16 {
  224. private:
  225. std::wstring m_value;
  226. public:
  227. StringRefUtf16(llvm::StringRef value);
  228. operator LPCWSTR() const { return m_value.size() ? m_value.data() : nullptr; }
  229. };
  230. /// Reads all options from the given argument strings, populates opts, and
  231. /// validates reporting errors and warnings.
  232. int ReadDxcOpts(const llvm::opt::OptTable *optionTable, unsigned flagsToInclude,
  233. const MainArgs &argStrings, DxcOpts &opts,
  234. llvm::raw_ostream &errors);
  235. /// Sets up the specified DxcDllSupport instance as per the given options.
  236. int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
  237. llvm::raw_ostream &errors);
  238. void CopyArgsToWStrings(const llvm::opt::InputArgList &inArgs,
  239. unsigned flagsToInclude,
  240. std::vector<std::wstring> &outArgs);
  241. }
  242. }
  243. #endif