HLSLOptions.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. bool AllResourcesBound; // OPT_all_resources_bound
  98. bool AstDump; // OPT_ast_dump
  99. bool ColorCodeAssembly; // OPT_Cc
  100. bool CodeGenHighLevel; // OPT_fcgl
  101. bool DebugInfo; // OPT__SLASH_Zi
  102. bool DebugNameForBinary; // OPT_Zsb
  103. bool DebugNameForSource; // OPT_Zss
  104. bool DumpBin; // OPT_dumpbin
  105. bool WarningAsError; // OPT__SLASH_WX
  106. bool IEEEStrict; // OPT_Gis
  107. bool IgnoreLineDirectives; // OPT_ignore_line_directives
  108. bool DefaultColMajor; // OPT_Zpc
  109. bool DefaultRowMajor; // OPT_Zpr
  110. bool DisableValidation; // OPT_VD
  111. unsigned OptLevel; // OPT_O0/O1/O2/O3
  112. bool DisableOptimizations; // OPT_Od
  113. bool AvoidFlowControl; // OPT_Gfa
  114. bool PreferFlowControl; // OPT_Gfp
  115. bool EnableStrictMode; // OPT_Ges
  116. unsigned long HLSLVersion; // OPT_hlsl_version (2015-2018)
  117. bool Enable16BitTypes; // OPT_enable_16bit_types
  118. bool OptDump; // OPT_ODump - dump optimizer commands
  119. bool OutputWarnings = true; // OPT_no_warnings
  120. bool ShowHelp = false; // OPT_help
  121. bool UseColor; // OPT_Cc
  122. bool UseHexLiterals; // OPT_Lx
  123. bool UseInstructionByteOffsets; // OPT_No
  124. bool UseInstructionNumbers; // OPT_Ni
  125. bool NotUseLegacyCBufLoad; // OPT_not_use_legacy_cbuf_load
  126. bool PackPrefixStable; // OPT_pack_prefix_stable
  127. bool PackOptimized; // OPT_pack_optimized
  128. bool DisplayIncludeProcess; // OPT__vi
  129. bool RecompileFromBinary; // OPT _Recompile (Recompiling the DXBC binary file not .hlsl file)
  130. bool StripDebug; // OPT Qstrip_debug
  131. bool StripRootSignature; // OPT_Qstrip_rootsignature
  132. bool StripPrivate; // OPT_Qstrip_priv
  133. bool StripReflection; // OPT_Qstrip_reflect
  134. bool ExtractRootSignature; // OPT_extractrootsignature
  135. bool DisassembleColorCoded; // OPT_Cc
  136. bool DisassembleInstNumbers; //OPT_Ni
  137. bool DisassembleByteOffset; //OPT_No
  138. bool DisaseembleHex; //OPT_Lx
  139. bool IsRootSignatureProfile();
  140. bool IsLibraryProfile();
  141. // SPIRV Change Starts
  142. #ifdef ENABLE_SPIRV_CODEGEN
  143. bool GenSPIRV; // OPT_spirv
  144. bool VkIgnoreUnusedResources; // OPT_fvk_ignore_used_resources
  145. bool VkInvertY; // OPT_fvk_invert_y
  146. llvm::StringRef VkStageIoOrder; // OPT_fvk_stage_io_order
  147. llvm::SmallVector<uint32_t, 4> VkBShift; // OPT_fvk_b_shift
  148. llvm::SmallVector<uint32_t, 4> VkTShift; // OPT_fvk_t_shift
  149. llvm::SmallVector<uint32_t, 4> VkSShift; // OPT_fvk_s_shift
  150. llvm::SmallVector<uint32_t, 4> VkUShift; // OPT_fvk_u_shift
  151. #endif
  152. // SPIRV Change Ends
  153. };
  154. /// Use this class to capture, convert and handle the lifetime for the
  155. /// command-line arguments to a program.
  156. class MainArgs {
  157. public:
  158. llvm::SmallVector<std::string, 8> Utf8StringVector;
  159. llvm::SmallVector<const char *, 8> Utf8CharPtrVector;
  160. MainArgs() = default;
  161. MainArgs(int argc, const wchar_t **argv, int skipArgCount = 1);
  162. MainArgs(llvm::ArrayRef<llvm::StringRef> args);
  163. MainArgs& operator=(const MainArgs &other);
  164. llvm::ArrayRef<const char *> getArrayRef() const {
  165. return llvm::ArrayRef<const char *>(Utf8CharPtrVector.data(),
  166. Utf8CharPtrVector.size());
  167. }
  168. };
  169. /// Use this class to convert a StringRef into a wstring, handling empty values as nulls.
  170. class StringRefUtf16 {
  171. private:
  172. std::wstring m_value;
  173. public:
  174. StringRefUtf16(llvm::StringRef value);
  175. operator LPCWSTR() const { return m_value.size() ? m_value.data() : nullptr; }
  176. };
  177. /// Reads all options from the given argument strings, populates opts, and
  178. /// validates reporting errors and warnings.
  179. int ReadDxcOpts(const llvm::opt::OptTable *optionTable, unsigned flagsToInclude,
  180. const MainArgs &argStrings, DxcOpts &opts,
  181. llvm::raw_ostream &errors);
  182. /// Sets up the specified DxcDllSupport instance as per the given options.
  183. int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
  184. llvm::raw_ostream &errors);
  185. void CopyArgsToWStrings(const llvm::opt::InputArgList &inArgs,
  186. unsigned flagsToInclude,
  187. std::vector<std::wstring> &outArgs);
  188. }
  189. }
  190. #endif