HLSLOptions.h 10 KB

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