HLSLOptions.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. ///////////////////////////////////////////////////////////////////////////////
  47. // Helper classes to deal with options.
  48. /// Flags for IDxcCompiler APIs.
  49. static const unsigned CompilerFlags = HlslFlags::CoreOption;
  50. /// Flags for dxc.exe command-line tool.
  51. static const unsigned DxcFlags = HlslFlags::CoreOption | HlslFlags::DriverOption;
  52. /// Flags for dxr.exe command-line tool.
  53. static const unsigned DxrFlags = HlslFlags::CoreOption | HlslFlags::DriverOption;
  54. /// Flags for IDxcIntelliSense APIs.
  55. static const unsigned ISenseFlags = HlslFlags::CoreOption | HlslFlags::ISenseOption;
  56. /// Use this class to capture preprocessor definitions and manage their lifetime.
  57. class DxcDefines {
  58. public:
  59. void push_back(llvm::StringRef value);
  60. LPWSTR DefineValues = nullptr;
  61. llvm::SmallVector<llvm::StringRef, 8> DefineStrings;
  62. llvm::SmallVector<DxcDefine, 8> DefineVector;
  63. ~DxcDefines() { delete[] DefineValues; }
  64. DxcDefines(const DxcDefines &) = delete;
  65. DxcDefines() {}
  66. void BuildDefines(); // Must be called after all defines are pushed back
  67. UINT32 ComputeNumberOfWCharsNeededForDefines();
  68. const DxcDefine *data() const { return DefineVector.data(); }
  69. unsigned size() const { return DefineVector.size(); }
  70. };
  71. /// Use this class to capture all options.
  72. class DxcOpts {
  73. public:
  74. DxcDefines Defines;
  75. llvm::opt::InputArgList Args = llvm::opt::InputArgList(nullptr, nullptr); // Original arguments.
  76. llvm::StringRef AssemblyCode; // OPT_Fc
  77. llvm::StringRef DebugFile; // OPT_Fd
  78. llvm::StringRef EntryPoint; // OPT_entrypoint
  79. llvm::StringRef ExternalFn; // OPT_external_fn
  80. llvm::StringRef ExternalLib; // OPT_external_lib
  81. llvm::StringRef ExtractPrivateFile; // OPT_getprivate
  82. llvm::StringRef ForceRootSigVer; // OPT_force_rootsig_ver
  83. llvm::StringRef InputFile; // OPT_INPUT
  84. llvm::StringRef OutputHeader; // OPT_Fh
  85. llvm::StringRef OutputObject; // OPT_Fo
  86. llvm::StringRef OutputWarningsFile; // OPT_Fe
  87. llvm::StringRef Preprocess; // OPT_P
  88. llvm::StringRef TargetProfile; // OPT_target_profile
  89. llvm::StringRef VariableName; // OPT_Vn
  90. llvm::StringRef PrivateSource; // OPT_setprivate
  91. llvm::StringRef RootSignatureSource; // OPT_setrootsignature
  92. llvm::StringRef VerifyRootSignatureSource; //OPT_verifyrootsignature
  93. llvm::StringRef RootSignatureDefine; // OPT_rootsig_define
  94. bool AllResourcesBound; // OPT_all_resources_bound
  95. bool AstDump; // OPT_ast_dump
  96. bool GenSPIRV; // OPT_spirv // SPIRV change
  97. bool ColorCodeAssembly; // OPT_Cc
  98. bool CodeGenHighLevel; // OPT_fcgl
  99. bool DebugInfo; // OPT__SLASH_Zi
  100. bool DebugNameForBinary; // OPT_Zsb
  101. bool DebugNameForSource; // OPT_Zss
  102. bool DumpBin; // OPT_dumpbin
  103. bool WarningAsError; // OPT__SLASH_WX
  104. bool IEEEStrict; // OPT_Gis
  105. bool DefaultColMajor; // OPT_Zpc
  106. bool DefaultRowMajor; // OPT_Zpr
  107. bool DisableValidation; // OPT_VD
  108. unsigned OptLevel; // OPT_O0/O1/O2/O3
  109. bool DisableOptimizations; // OPT_Od
  110. bool AvoidFlowControl; // OPT_Gfa
  111. bool PreferFlowControl; // OPT_Gfp
  112. bool EnableStrictMode; // OPT_Ges
  113. bool HLSL2015; // OPT_hlsl_version (=2015)
  114. bool HLSL2016; // OPT_hlsl_version (=2016)
  115. bool HLSL2017; // OPT_hlsl_version (=2017)
  116. bool OptDump; // OPT_ODump - dump optimizer commands
  117. bool OutputWarnings = true; // OPT_no_warnings
  118. bool ShowHelp = false; // OPT_help
  119. bool UseColor; // OPT_Cc
  120. bool UseHexLiterals; // OPT_Lx
  121. bool UseInstructionByteOffsets; // OPT_No
  122. bool UseInstructionNumbers; // OPT_Ni
  123. bool NotUseLegacyCBufLoad; // OPT_not_use_legacy_cbuf_load
  124. bool PackPrefixStable; // OPT_pack_prefix_stable
  125. bool PackOptimized; // OPT_pack_optimized
  126. bool DisplayIncludeProcess; // OPT__vi
  127. bool RecompileFromBinary; // OPT _Recompile (Recompiling the DXBC binary file not .hlsl file)
  128. bool StripDebug; // OPT Qstrip_debug
  129. bool StripRootSignature; // OPT_Qstrip_rootsignature
  130. bool StripPrivate; // OPT_Qstrip_priv
  131. bool StripReflection; // OPT_Qstrip_reflect
  132. bool ExtractRootSignature; // OPT_extractrootsignature
  133. bool DisassembleColorCoded; // OPT_Cc
  134. bool DisassembleInstNumbers; //OPT_Ni
  135. bool DisassembleByteOffset; //OPT_No
  136. bool DisaseembleHex; //OPT_Lx
  137. bool IsRootSignatureProfile();
  138. };
  139. /// Use this class to capture, convert and handle the lifetime for the
  140. /// command-line arguments to a program.
  141. class MainArgs {
  142. public:
  143. llvm::SmallVector<std::string, 8> Utf8StringVector;
  144. llvm::SmallVector<const char *, 8> Utf8CharPtrVector;
  145. MainArgs() = default;
  146. MainArgs(int argc, const wchar_t **argv, int skipArgCount = 1);
  147. MainArgs(llvm::ArrayRef<llvm::StringRef> args);
  148. MainArgs& operator=(const MainArgs &other);
  149. llvm::ArrayRef<const char *> getArrayRef() const {
  150. return llvm::ArrayRef<const char *>(Utf8CharPtrVector.data(),
  151. Utf8CharPtrVector.size());
  152. }
  153. };
  154. /// Use this class to convert a StringRef into a wstring, handling empty values as nulls.
  155. class StringRefUtf16 {
  156. private:
  157. std::wstring m_value;
  158. public:
  159. StringRefUtf16(llvm::StringRef value);
  160. operator LPCWSTR() const { return m_value.size() ? m_value.data() : nullptr; }
  161. };
  162. /// Reads all options from the given argument strings, populates opts, and
  163. /// validates reporting errors and warnings.
  164. int ReadDxcOpts(const llvm::opt::OptTable *optionTable, unsigned flagsToInclude,
  165. const MainArgs &argStrings, DxcOpts &opts,
  166. llvm::raw_ostream &errors);
  167. /// Sets up the specified DxcDllSupport instance as per the given options.
  168. int SetupDxcDllSupport(const DxcOpts &opts, dxc::DxcDllSupport &dxcSupport,
  169. llvm::raw_ostream &errors);
  170. void CopyArgsToWStrings(const llvm::opt::InputArgList &inArgs,
  171. unsigned flagsToInclude,
  172. std::vector<std::wstring> &outArgs);
  173. }
  174. }
  175. #endif