HLSLOptions.h 9.2 KB

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