Przeglądaj źródła

Add -precise-output (#2827)

Xiang Li 5 lat temu
rodzic
commit
8b92463c32

+ 1 - 0
include/dxc/Support/HLSLOptions.h

@@ -125,6 +125,7 @@ public:
   llvm::StringRef RootSignatureDefine; // OPT_rootsig_define
   llvm::StringRef RootSignatureDefine; // OPT_rootsig_define
   llvm::StringRef FloatDenormalMode; // OPT_denorm
   llvm::StringRef FloatDenormalMode; // OPT_denorm
   std::vector<std::string> Exports; // OPT_exports
   std::vector<std::string> Exports; // OPT_exports
+  std::vector<std::string> PreciseOutputs; // OPT_precise_output
   llvm::StringRef DefaultLinkage; // OPT_default_linkage
   llvm::StringRef DefaultLinkage; // OPT_default_linkage
   unsigned DefaultTextCodePage = DXC_CP_UTF8; // OPT_encoding
   unsigned DefaultTextCodePage = DXC_CP_UTF8; // OPT_encoding
 
 

+ 2 - 0
include/dxc/Support/HLSLOptions.td

@@ -255,6 +255,8 @@ def export_shaders_only : Flag<["-", "/"], "export-shaders-only">, Group<hlslcom
   HelpText<"Only export shaders when compiling a library">;
   HelpText<"Only export shaders when compiling a library">;
 def default_linkage : Separate<["-", "/"], "default-linkage">, Group<hlslcomp_Group>, Flags<[CoreOption]>,
 def default_linkage : Separate<["-", "/"], "default-linkage">, Group<hlslcomp_Group>, Flags<[CoreOption]>,
   HelpText<"Set default linkage for non-shader functions when compiling or linking to a library target (internal, external)">;
   HelpText<"Set default linkage for non-shader functions when compiling or linking to a library target (internal, external)">;
+def precise_output : Separate<["-", "/"], "precise-output">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
+    HelpText<"Mark output semantic as precise">;
 def encoding : Separate<["-", "/"], "encoding">, Group<hlslcomp_Group>, Flags<[CoreOption, RewriteOption, DriverOption]>,
 def encoding : Separate<["-", "/"], "encoding">, Group<hlslcomp_Group>, Flags<[CoreOption, RewriteOption, DriverOption]>,
   HelpText<"Set default encoding for text outputs (utf8|utf16) default=utf8">;
   HelpText<"Set default encoding for text outputs (utf8|utf16) default=utf8">;
 def validator_version : Separate<["-", "/"], "validator-version">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,
 def validator_version : Separate<["-", "/"], "validator-version">, Group<hlslcomp_Group>, Flags<[CoreOption, HelpHidden]>,

+ 2 - 0
lib/DxcSupport/HLSLOptions.cpp

@@ -385,6 +385,8 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
   DXASSERT(opts.ExternalLib.empty() == opts.ExternalFn.empty(),
   DXASSERT(opts.ExternalLib.empty() == opts.ExternalFn.empty(),
            "else flow above is incorrect");
            "else flow above is incorrect");
 
 
+  opts.PreciseOutputs = Args.getAllArgValues(OPT_precise_output);
+
   // when no-warnings option is present, do not output warnings.
   // when no-warnings option is present, do not output warnings.
   opts.OutputWarnings = Args.hasFlag(OPT_INVALID, OPT_no_warnings, true);
   opts.OutputWarnings = Args.hasFlag(OPT_INVALID, OPT_no_warnings, true);
   opts.EntryPoint = Args.getLastArgValue(OPT_entrypoint);
   opts.EntryPoint = Args.getLastArgValue(OPT_entrypoint);

+ 2 - 0
tools/clang/include/clang/Frontend/CodeGenOptions.h

@@ -195,6 +195,8 @@ public:
   unsigned HLSLValidatorMinorVer = 0;
   unsigned HLSLValidatorMinorVer = 0;
   /// Define macros passed in from command line
   /// Define macros passed in from command line
   std::vector<std::string> HLSLDefines;
   std::vector<std::string> HLSLDefines;
+  /// Precise output passed in from command line
+  std::vector<std::string> HLSLPreciseOutputs;
   /// Arguments passed in from command line
   /// Arguments passed in from command line
   std::vector<std::string> HLSLArguments;
   std::vector<std::string> HLSLArguments;
   /// Helper for generating llvm bitcode for hlsl extensions.
   /// Helper for generating llvm bitcode for hlsl extensions.

+ 14 - 1
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -27,6 +27,7 @@
 #include "clang/Lex/HLSLMacroExpander.h"
 #include "clang/Lex/HLSLMacroExpander.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Constants.h"
@@ -208,6 +209,7 @@ private:
                                           bool bDefaultRowMajor);
                                           bool bDefaultRowMajor);
 
 
   std::unordered_map<Constant*, DxilFieldAnnotation> m_ConstVarAnnotationMap;
   std::unordered_map<Constant*, DxilFieldAnnotation> m_ConstVarAnnotationMap;
+  StringSet<> m_PreciseOutputSet;
 
 
 public:
 public:
   CGMSHLSLRuntime(CodeGenModule &CGM);
   CGMSHLSLRuntime(CodeGenModule &CGM);
@@ -342,6 +344,11 @@ CGMSHLSLRuntime::CGMSHLSLRuntime(CodeGenModule &CGM)
   const bool skipInit = true;
   const bool skipInit = true;
   m_pHLModule = &TheModule.GetOrCreateHLModule(skipInit);
   m_pHLModule = &TheModule.GetOrCreateHLModule(skipInit);
 
 
+  // Precise Output.
+  for (auto &preciseOutput : CGM.getCodeGenOpts().HLSLPreciseOutputs) {
+    m_PreciseOutputSet.insert(StringRef(preciseOutput).lower());
+  }
+
   // Set Option.
   // Set Option.
   HLOptions opts;
   HLOptions opts;
   opts.bIEEEStrict = CGM.getCodeGenOpts().UnsafeFPMath;
   opts.bIEEEStrict = CGM.getCodeGenOpts().UnsafeFPMath;
@@ -464,6 +471,8 @@ CGMSHLSLRuntime::SetSemantic(const NamedDecl *decl,
     if (it->getKind() == hlsl::UnusualAnnotation::UA_SemanticDecl) {
     if (it->getKind() == hlsl::UnusualAnnotation::UA_SemanticDecl) {
       const hlsl::SemanticDecl *sd = cast<hlsl::SemanticDecl>(it);
       const hlsl::SemanticDecl *sd = cast<hlsl::SemanticDecl>(it);
       paramInfo.SetSemanticString(sd->SemanticName);
       paramInfo.SetSemanticString(sd->SemanticName);
+      if (m_PreciseOutputSet.count(StringRef(sd->SemanticName).lower()))
+        paramInfo.SetPrecise();
       return it->Loc;
       return it->Loc;
     }
     }
   }
   }
@@ -965,8 +974,12 @@ unsigned CGMSHLSLRuntime::ConstructStructAnnotation(DxilStructAnnotation *annota
 
 
     fieldAnnotation.SetCBufferOffset(CBufferOffset);
     fieldAnnotation.SetCBufferOffset(CBufferOffset);
     fieldAnnotation.SetFieldName(fieldDecl->getName());
     fieldAnnotation.SetFieldName(fieldDecl->getName());
-    if (!fieldSemName.empty())
+    if (!fieldSemName.empty()) {
       fieldAnnotation.SetSemanticString(fieldSemName);
       fieldAnnotation.SetSemanticString(fieldSemName);
+
+      if (m_PreciseOutputSet.count(StringRef(fieldSemName).lower()))
+        fieldAnnotation.SetPrecise();
+    }
   }
   }
 
 
   annotation->SetCBufferSize(offset);
   annotation->SetCBufferSize(offset);

+ 20 - 0
tools/clang/test/HLSLFileCheck/hlsl/compile_options/precise_output.hlsl

@@ -0,0 +1,20 @@
+// RUN: %dxc -E main -T vs_6_0 -precise-output d1 -precise-output SV_Position %s | FileCheck %s
+
+// Make sure get ir.
+// CHECK:@main
+// Make sure precise removed fast.
+// CHECK-NOT:fast
+
+struct T {
+  float4 p : SV_Position;
+  float a : A;
+};
+
+T main(float4 a:A, float b:B, float c:C, out float d:D1) {
+
+   T t;
+  t.p = a + b;
+  t.a = b;
+   d = b*c;
+  return t;
+}

+ 1 - 0
tools/clang/tools/dxcompiler/dxcompilerobj.cpp

@@ -1113,6 +1113,7 @@ public:
     compiler.getCodeGenOpts().HLSLNotUseLegacyCBufLoad = Opts.NotUseLegacyCBufLoad;
     compiler.getCodeGenOpts().HLSLNotUseLegacyCBufLoad = Opts.NotUseLegacyCBufLoad;
     compiler.getCodeGenOpts().HLSLLegacyResourceReservation = Opts.LegacyResourceReservation;
     compiler.getCodeGenOpts().HLSLLegacyResourceReservation = Opts.LegacyResourceReservation;
     compiler.getCodeGenOpts().HLSLDefines = defines;
     compiler.getCodeGenOpts().HLSLDefines = defines;
+    compiler.getCodeGenOpts().HLSLPreciseOutputs = Opts.PreciseOutputs;
     compiler.getCodeGenOpts().MainFileName = pMainFile;
     compiler.getCodeGenOpts().MainFileName = pMainFile;
 
 
     // Translate signature packing options
     // Translate signature packing options