HLSLOptions.h 8.5 KB

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