Преглед изворни кода

Code cleanup. (#1592)

* Code cleanup.
1. Move IsHLSLObjectType to dxilutil.
2. Add IsAnyTexture(Kind ResourceKind) to DxilResource.
3. Add SetMinPrecision for DxilTypeSystem and DxilOperation.
Xiang Li пре 6 година
родитељ
комит
1661bf8de6

+ 1 - 1
include/dxc/HLSL/ComputeViewIdState.h

@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
-// ComputeViewIdSets.h                                                       //
+// ComputeViewIdState.h                                                       //
 // Copyright (C) Microsoft Corporation. All rights reserved.                 //
 // This file is distributed under the University of Illinois Open Source     //
 // License. See LICENSE.TXT for details.                                     //

+ 2 - 0
include/dxc/HLSL/DxilOperations.h

@@ -66,6 +66,8 @@ public:
 
   // To check if operation uses strict precision types
   bool UseMinPrecision();
+  // Set if operation uses strict precision types or not.
+  void SetMinPrecision(bool bMinPrecision);
 
   // Get the size of the type for a given layout
   uint64_t GetAllocSizeForType(llvm::Type *Ty);

+ 2 - 0
include/dxc/HLSL/DxilResource.h

@@ -29,6 +29,8 @@ public:
   static unsigned GetNumDimensionsForCalcLOD(Kind ResourceKind);
   /// Total number of offsets (in [-8,7]) necessary to access resource.
   static unsigned GetNumOffsets(Kind ResourceKind);
+  /// Whether the resource kind is texture.
+  static bool IsAnyTexture(Kind ResourceKind);
 
   DxilResource();
 

+ 1 - 0
include/dxc/HLSL/DxilTypeSystem.h

@@ -189,6 +189,7 @@ public:
                               const DxilTypeSystem &src);
 
   bool UseMinPrecision();
+  void SetMinPrecision(bool bMinPrecision);
 
 private:
   llvm::Module *m_pModule;

+ 1 - 0
include/dxc/HLSL/DxilUtil.h

@@ -93,6 +93,7 @@ namespace dxilutil {
   void PrintDiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
   // Returns true if type contains HLSL Object type (resource)
   bool ContainsHLSLObjectType(llvm::Type *Ty);
+  bool IsHLSLObjectType(llvm::Type *Ty);
   bool IsSplat(llvm::ConstantDataVector *cdv);
 }
 

+ 0 - 1
include/dxc/HLSL/HLModule.h

@@ -181,7 +181,6 @@ public:
   // Type related methods.
   static bool IsStreamOutputPtrType(llvm::Type *Ty);
   static bool IsStreamOutputType(llvm::Type *Ty);
-  static bool IsHLSLObjectType(llvm::Type *Ty);
   static void GetParameterRowsAndCols(llvm::Type *Ty, unsigned &rows, unsigned &cols,
                                       DxilParameterAnnotation &paramAnnotation);
 

+ 1 - 1
lib/HLSL/ComputeViewIdState.cpp

@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
-// ViewIdAnalysis.cpp                                                        //
+// ComputeViewIdState.cpp                                                        //
 // Copyright (C) Microsoft Corporation. All rights reserved.                 //
 // This file is distributed under the University of Illinois Open Source     //
 // License. See LICENSE.TXT for details.                                     //

+ 1 - 1
lib/HLSL/DxilContainerReflection.cpp

@@ -633,7 +633,7 @@ static bool TryToDetectObjectType(
   llvm::StructType            *structType,
   D3D_SHADER_VARIABLE_TYPE    *outObjectType)
 {
-  // Note: This logic is largely duplicated from `HLModule::IsHLSLObjectType`
+  // Note: This logic is largely duplicated from `dxilutil::IsHLSLObjectType`
   // with the addition of returning the appropriate reflection type tag.
   //
   // That logic looks error-prone, since it relies on string tests against

+ 3 - 3
lib/HLSL/DxilGenerationPass.cpp

@@ -962,7 +962,7 @@ bool DxilPromoteLocalResources::PromoteLocalResource(Function &F) {
     // the entry node
     for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
       if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { // Is it an alloca?
-        if (HLModule::IsHLSLObjectType(dxilutil::GetArrayEltTy(AI->getAllocatedType()))) {
+        if (dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(AI->getAllocatedType()))) {
           if (isAllocaPromotable(AI))
             Allocas.push_back(AI);
         }
@@ -1015,7 +1015,7 @@ bool DxilPromoteStaticResources::PromoteStaticGlobalResources(
     //  optimized away for the exported function.
     for (auto &GV : M.globals()) {
       if (GV.getLinkage() == GlobalVariable::LinkageTypes::InternalLinkage &&
-        HLModule::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
+        dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
         if (!GV.user_empty()) {
           if (Instruction *I = dyn_cast<Instruction>(*GV.user_begin())) {
             dxilutil::EmitErrorOnInstruction(I, kStaticResourceLibErrorMsg);
@@ -1031,7 +1031,7 @@ bool DxilPromoteStaticResources::PromoteStaticGlobalResources(
   std::set<GlobalVariable *> staticResources;
   for (auto &GV : M.globals()) {
     if (GV.getLinkage() == GlobalVariable::LinkageTypes::InternalLinkage &&
-        HLModule::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
+        dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(GV.getType()))) {
       staticResources.insert(&GV);
     }
   }

+ 6 - 6
lib/HLSL/DxilModule.cpp

@@ -128,6 +128,9 @@ void DxilModule::SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision) {
   m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
   m_pMDHelper->SetShaderModel(m_pSM);
   m_bUseMinPrecision = bUseMinPrecision;
+  m_pOP->SetMinPrecision(m_bUseMinPrecision);
+  m_pTypeSystem->SetMinPrecision(m_bUseMinPrecision);
+
   if (!m_pSM->IsLib()) {
     // Always have valid entry props for non-lib case from this point on.
     DxilFunctionProps props;
@@ -1130,12 +1133,9 @@ void DxilModule::ClearDxilMetadata(Module &M) {
   // root signature, function properties.
   // Other cases for libs pending.
   // LLVM used is a global variable - handle separately.
-  Module::named_metadata_iterator
-    b = M.named_metadata_begin(),
-    e = M.named_metadata_end();
   SmallVector<NamedMDNode*, 8> nodes;
-  for (; b != e; ++b) {
-    StringRef name = b->getName();
+  for (NamedMDNode &b : M.named_metadata()) {
+    StringRef name = b.getName();
     if (name == DxilMDHelper::kDxilVersionMDName ||
       name == DxilMDHelper::kDxilValidatorVersionMDName ||
       name == DxilMDHelper::kDxilShaderModelMDName ||
@@ -1145,7 +1145,7 @@ void DxilModule::ClearDxilMetadata(Module &M) {
       name == DxilMDHelper::kDxilTypeSystemMDName ||
       name == DxilMDHelper::kDxilViewIdStateMDName ||
       name.startswith(DxilMDHelper::kDxilTypeSystemHelperVariablePrefix)) {
-      nodes.push_back(b);
+      nodes.push_back(&b);
     }
   }
   for (size_t i = 0; i < nodes.size(); ++i) {

+ 11 - 14
lib/HLSL/DxilOperations.cpp

@@ -12,7 +12,6 @@
 #include "dxc/HLSL/DxilOperations.h"
 #include "dxc/Support/Global.h"
 #include "dxc/HLSL/DxilModule.h"
-#include "dxc/HLSL/HLModule.h"
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/IR/LLVMContext.h"
@@ -1086,22 +1085,20 @@ bool OP::GetOpCodeClass(const Function *F, OP::OpCodeClass &opClass) {
 }
 
 bool OP::UseMinPrecision() {
-  if (m_LowPrecisionMode == DXIL::LowPrecisionMode::Undefined) {
-    if (m_pModule->HasDxilModule()) {
-      m_LowPrecisionMode = m_pModule->GetDxilModule().GetUseMinPrecision() ?
-        DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
-    }
-    else if (m_pModule->HasHLModule()) {
-      m_LowPrecisionMode = m_pModule->GetHLModule().GetHLOptions().bUseMinPrecision ?
-        DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
-    }
-    else {
-      DXASSERT(false, "otherwise module doesn't contain either HLModule or Dxil Module.");
-    }
-  }
   return m_LowPrecisionMode == DXIL::LowPrecisionMode::UseMinPrecision;
 }
 
+void OP::SetMinPrecision(bool bMinPrecision) {
+  DXIL::LowPrecisionMode mode =
+      bMinPrecision ? DXIL::LowPrecisionMode::UseMinPrecision
+                    : DXIL::LowPrecisionMode::UseNativeLowPrecision;
+  DXASSERT((mode == m_LowPrecisionMode ||
+            m_LowPrecisionMode == DXIL::LowPrecisionMode::Undefined),
+           "LowPrecisionMode should only be set once.");
+
+  m_LowPrecisionMode = mode;
+}
+
 uint64_t OP::GetAllocSizeForType(llvm::Type *Ty) {
   return m_pModule->getDataLayout().getTypeAllocSize(Ty);
 }

+ 6 - 1
lib/HLSL/DxilResource.cpp

@@ -104,7 +104,12 @@ void DxilResource::SetROV(bool bROV) {
 }
 
 bool DxilResource::IsAnyTexture() const {
-  return Kind::Texture1D <= GetKind() && GetKind() <= Kind::TextureCubeArray;
+  return IsAnyTexture(GetKind());
+}
+
+bool DxilResource::IsAnyTexture(Kind ResourceKind) {
+  return Kind::Texture1D <= ResourceKind &&
+         ResourceKind <= Kind::TextureCubeArray;
 }
 
 bool DxilResource::IsStructuredBuffer() const {

+ 11 - 14
lib/HLSL/DxilTypeSystem.cpp

@@ -9,7 +9,6 @@
 
 #include "dxc/HLSL/DxilTypeSystem.h"
 #include "dxc/HLSL/DxilModule.h"
-#include "dxc/HLSL/HLModule.h"
 #include "dxc/Support/Global.h"
 #include "dxc/Support/WinFunctions.h"
 
@@ -455,22 +454,20 @@ void RemapObsoleteSemantic(DxilParameterAnnotation &paramInfo, DXIL::SigPointKin
 }
 
 bool DxilTypeSystem::UseMinPrecision() {
-  if (m_LowPrecisionMode == DXIL::LowPrecisionMode::Undefined) {
-    if (m_pModule->HasDxilModule()) {
-      m_LowPrecisionMode = m_pModule->GetDxilModule().GetUseMinPrecision() ?
-        DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
-    }
-    else if (m_pModule->HasHLModule()) {
-      m_LowPrecisionMode = m_pModule->GetHLModule().GetHLOptions().bUseMinPrecision ?
-        DXIL::LowPrecisionMode::UseMinPrecision : DXIL::LowPrecisionMode::UseNativeLowPrecision;
-    }
-    else {
-      DXASSERT(false, "otherwise module doesn't contain either HLModule or Dxil Module.");
-    }
-  }
   return m_LowPrecisionMode == DXIL::LowPrecisionMode::UseMinPrecision;
 }
 
+void DxilTypeSystem::SetMinPrecision(bool bMinPrecision) {
+  DXIL::LowPrecisionMode mode =
+      bMinPrecision ? DXIL::LowPrecisionMode::UseMinPrecision
+                    : DXIL::LowPrecisionMode::UseNativeLowPrecision;
+  DXASSERT((mode == m_LowPrecisionMode ||
+            m_LowPrecisionMode == DXIL::LowPrecisionMode::Undefined),
+           "LowPrecisionMode should only be set once.");
+
+  m_LowPrecisionMode = mode;
+}
+
 DxilStructTypeIterator::DxilStructTypeIterator(llvm::StructType *sTy, DxilStructAnnotation *sAnnotation,
   unsigned idx)
   : STy(sTy), SAnnotation(sAnnotation), index(idx) {

+ 68 - 2
lib/HLSL/DxilUtil.cpp

@@ -14,7 +14,6 @@
 #include "dxc/HLSL/DxilTypeSystem.h"
 #include "dxc/HLSL/DxilUtil.h"
 #include "dxc/HLSL/DxilModule.h"
-#include "dxc/HLSL/HLModule.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/DiagnosticPrinter.h"
@@ -371,6 +370,73 @@ llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Function* F) {
     F->getEntryBlock().getFirstInsertionPt());
 }
 
+bool IsHLSLObjectType(llvm::Type *Ty) {
+  if (llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty)) {
+    StringRef name = ST->getName();
+    // TODO: don't check names.
+    if (name.startswith("dx.types.wave_t"))
+      return true;
+
+    if (name.endswith("_slice_type"))
+      return false;
+
+    name = name.ltrim("class.");
+    name = name.ltrim("struct.");
+
+    if (name == "SamplerState")
+      return true;
+    if (name == "SamplerComparisonState")
+      return true;
+
+    if (name.startswith("TriangleStream<"))
+      return true;
+    if (name.startswith("PointStream<"))
+      return true;
+    if (name.startswith("LineStream<"))
+      return true;
+
+    if (name.startswith("AppendStructuredBuffer<"))
+      return true;
+    if (name.startswith("ConsumeStructuredBuffer<"))
+      return true;
+
+    if (name.startswith("ConstantBuffer<"))
+      return true;
+
+    if (name == "RaytracingAccelerationStructure")
+      return true;
+
+    name = name.ltrim("RasterizerOrdered");
+    name = name.ltrim("RW");
+    if (name == "ByteAddressBuffer")
+      return true;
+
+    if (name.startswith("Buffer<"))
+      return true;
+    if (name.startswith("StructuredBuffer<"))
+      return true;
+    if (name.startswith("Texture1D<"))
+      return true;
+    if (name.startswith("Texture1DArray<"))
+      return true;
+    if (name.startswith("Texture2D<"))
+      return true;
+    if (name.startswith("Texture2DArray<"))
+      return true;
+    if (name.startswith("Texture3D<"))
+      return true;
+    if (name.startswith("TextureCube<"))
+      return true;
+    if (name.startswith("TextureCubeArray<"))
+      return true;
+    if (name.startswith("Texture2DMS<"))
+      return true;
+    if (name.startswith("Texture2DMSArray<"))
+      return true;
+  }
+  return false;
+}
+
 bool ContainsHLSLObjectType(llvm::Type *Ty) {
   // Unwrap pointer/array
   while (llvm::isa<llvm::PointerType>(Ty))
@@ -383,7 +449,7 @@ bool ContainsHLSLObjectType(llvm::Type *Ty) {
       return true;
     // TODO: How is this suppoed to check for Input/OutputPatch types if
     // these have already been eliminated in function arguments during CG?
-    if (HLModule::IsHLSLObjectType(Ty))
+    if (IsHLSLObjectType(Ty))
       return true;
     // Otherwise, recurse elements of UDT
     for (auto ETy : ST->elements()) {

+ 0 - 67
lib/HLSL/HLModule.cpp

@@ -789,73 +789,6 @@ bool HLModule::IsStreamOutputPtrType(llvm::Type *Ty) {
   return IsStreamOutputType(Ty);
 }
 
-bool HLModule::IsHLSLObjectType(llvm::Type *Ty) {
-  if (llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty)) {
-    StringRef name = ST->getName();
-    // TODO: don't check names.
-    if (name.startswith("dx.types.wave_t"))
-      return true;
-
-    if (name.endswith("_slice_type"))
-      return false;
-
-    name = name.ltrim("class.");
-    name = name.ltrim("struct.");
-
-    if (name == "SamplerState")
-      return true;
-    if (name == "SamplerComparisonState")
-      return true;
-
-    if (name.startswith("TriangleStream<"))
-      return true;
-    if (name.startswith("PointStream<"))
-      return true;
-    if (name.startswith("LineStream<"))
-      return true;
-
-    if (name.startswith("AppendStructuredBuffer<"))
-      return true;
-    if (name.startswith("ConsumeStructuredBuffer<"))
-      return true;
-
-    if (name.startswith("ConstantBuffer<"))
-      return true;
-
-    if (name == "RaytracingAccelerationStructure")
-      return true;
-
-    name = name.ltrim("RasterizerOrdered");
-    name = name.ltrim("RW");
-    if (name == "ByteAddressBuffer")
-      return true;
-
-    if (name.startswith("Buffer<"))
-      return true;
-    if (name.startswith("StructuredBuffer<"))
-      return true;
-    if (name.startswith("Texture1D<"))
-      return true;
-    if (name.startswith("Texture1DArray<"))
-      return true;
-    if (name.startswith("Texture2D<"))
-      return true;
-    if (name.startswith("Texture2DArray<"))
-      return true;
-    if (name.startswith("Texture3D<"))
-      return true;
-    if (name.startswith("TextureCube<"))
-      return true;
-    if (name.startswith("TextureCubeArray<"))
-      return true;
-    if (name.startswith("Texture2DMS<"))
-      return true;
-    if (name.startswith("Texture2DMSArray<"))
-      return true;
-  }
-  return false;
-}
-
 void HLModule::GetParameterRowsAndCols(Type *Ty, unsigned &rows, unsigned &cols,
                                        DxilParameterAnnotation &paramAnnotation) {
   if (Ty->isPointerTy())

+ 1 - 1
lib/HLSL/HLOperationLower.cpp

@@ -5588,7 +5588,7 @@ void TranslateCBAddressUserLegacy(Instruction *user, Value *handle,
     Type *Ty = ldInst->getType();
     Type *EltTy = Ty->getScalarType();
     // Resource inside cbuffer is lowered after GenerateDxilOperations.
-    if (HLModule::IsHLSLObjectType(Ty)) {
+    if (dxilutil::IsHLSLObjectType(Ty)) {
       CallInst *CI = cast<CallInst>(handle);
       GlobalVariable *CbGV = cast<GlobalVariable>(
           CI->getArgOperand(HLOperandIndex::kCreateHandleResourceOpIdx));

+ 4 - 4
lib/HLSL/ReducibilityAnalysis.cpp

@@ -106,12 +106,12 @@ bool ReducibilityAnalysis::runOnFunction(Function &F) {
   // Initialize.
   //
   unsigned iNode = 0;
-  for (auto itBB = F.begin(), endBB = F.end(); itBB != endBB; ++itBB) {
-    BasicBlockToNodeIdxMap[itBB] = iNode++;
+  for (BasicBlock &BB : F) {
+    BasicBlockToNodeIdxMap[&BB] = iNode++;
   }
 
-  for (auto itBB = F.begin(), endBB = F.end(); itBB != endBB; ++itBB) {
-    BasicBlock *pBB = itBB;
+  for (BasicBlock &BB : F) {
+    BasicBlock *pBB = &BB;
     unsigned N = BasicBlockToNodeIdxMap[pBB];
 
     for (succ_iterator itSucc = succ_begin(pBB), endSucc = succ_end(pBB); itSucc != endSucc; ++itSucc) {

+ 2 - 2
lib/Transforms/Scalar/SROA.cpp

@@ -56,7 +56,7 @@
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
 #include "llvm/Transforms/Utils/SSAUpdater.h"
-#include "dxc/HLSL/HLModule.h"  // HLSL Change - not sroa resource type.
+#include "dxc/HLSL/DxilUtil.h"  // HLSL Change - not sroa resource type.
 
 #if __cplusplus >= 201103L && !defined(NDEBUG)
 // We only use this for a debug check in C++11
@@ -4310,7 +4310,7 @@ bool SROA::runOnAlloca(AllocaInst &AI) {
 
   // Skip alloca forms that this analysis can't handle.
   if (AI.isArrayAllocation() || !AI.getAllocatedType()->isSized() ||
-      hlsl::HLModule::IsHLSLObjectType(AI.getAllocatedType()) || // HLSL Change - not sroa resource type.
+      hlsl::dxilutil::IsHLSLObjectType(AI.getAllocatedType()) || // HLSL Change - not sroa resource type.
       DL.getTypeAllocSize(AI.getAllocatedType()) == 0)
     return false;
 

+ 13 - 13
lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp

@@ -1716,7 +1716,7 @@ void SROA_HLSL::isSafeForScalarRepl(Instruction *I, uint64_t Offset,
                                     AllocaInfo &Info) {
   if (I->getType()->isPointerTy()) {
     // Don't check object pointers.
-    if (HLModule::IsHLSLObjectType(I->getType()->getPointerElementType()))
+    if (dxilutil::IsHLSLObjectType(I->getType()->getPointerElementType()))
       return;
   }
   const DataLayout &DL = I->getModule()->getDataLayout();
@@ -2232,7 +2232,7 @@ static bool IsMemCpyTy(Type *Ty, DxilTypeSystem &typeSys) {
     return false;
   if (HLMatrixLower::IsMatrixType(Ty))
     return false;
-  if (HLModule::IsHLSLObjectType(Ty))
+  if (dxilutil::IsHLSLObjectType(Ty))
     return false;
   if (StructType *ST = dyn_cast<StructType>(Ty)) {
     DxilStructAnnotation *STA = typeSys.GetStructAnnotation(ST);
@@ -2299,7 +2299,7 @@ static void SplitCpy(Type *Ty, Value *Dest, Value *Src,
           {DestGEP, Load}, *M);
     }
   } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
-    if (HLModule::IsHLSLObjectType(ST)) {
+    if (dxilutil::IsHLSLObjectType(ST)) {
       // Avoid split HLSL object.
       SimpleCopy(Dest, Src, idxList, Builder);
       return;
@@ -2360,7 +2360,7 @@ static void SplitPtr(Type *Ty, Value *Ptr, SmallVector<Value *, 16> &idxList,
     Value *GEP = Builder.CreateInBoundsGEP(Ptr, idxList);
     EltPtrList.emplace_back(GEP);
   } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
-    if (HLModule::IsHLSLObjectType(ST)) {
+    if (dxilutil::IsHLSLObjectType(ST)) {
       // Avoid split HLSL object.
       Value *GEP = Builder.CreateInBoundsGEP(Ptr, idxList);
       EltPtrList.emplace_back(GEP);
@@ -2666,7 +2666,7 @@ void SROA_Helper::RewriteForGEP(GEPOperator *GEP, IRBuilder<> &Builder) {
     // End at array of basic type.
     Type *Ty = GEP->getType()->getPointerElementType();
     if (Ty->isVectorTy() ||
-        (Ty->isStructTy() && !HLModule::IsHLSLObjectType(Ty)) ||
+        (Ty->isStructTy() && !dxilutil::IsHLSLObjectType(Ty)) ||
         Ty->isArrayTy()) {
       SmallVector<Value *, 8> NewArgs;
       NewArgs.append(GEP->idx_begin(), GEP->idx_end());
@@ -3366,7 +3366,7 @@ bool SROA_Helper::DoScalarReplacement(Value *V, std::vector<Value *> &Elts,
 
   if (StructType *ST = dyn_cast<StructType>(Ty)) {
     // Skip HLSL object types.
-    if (HLModule::IsHLSLObjectType(ST)) {
+    if (dxilutil::IsHLSLObjectType(ST)) {
       return false;
     }
 
@@ -3407,7 +3407,7 @@ bool SROA_Helper::DoScalarReplacement(Value *V, std::vector<Value *> &Elts,
     if (ElTy->isStructTy() &&
         // Skip Matrix type.
         !HLMatrixLower::IsMatrixType(ElTy)) {
-      if (!HLModule::IsHLSLObjectType(ElTy)) {
+      if (!dxilutil::IsHLSLObjectType(ElTy)) {
         // for array of struct
         // split into arrays of struct elements
         StructType *ElST = cast<StructType>(ElTy);
@@ -3550,7 +3550,7 @@ bool SROA_Helper::DoScalarReplacement(GlobalVariable *GV,
 
   if (StructType *ST = dyn_cast<StructType>(Ty)) {
     // Skip HLSL object types.
-    if (HLModule::IsHLSLObjectType(ST))
+    if (dxilutil::IsHLSLObjectType(ST))
       return false;
     unsigned numTypes = ST->getNumContainedTypes();
     Elts.reserve(numTypes);
@@ -5107,7 +5107,7 @@ Value *SROA_Parameter_HLSL::castResourceArgIfRequired(
   IRBuilder<> AllocaBuilder(dxilutil::FindAllocaInsertionPt(Builder.GetInsertPoint()));
 
   // Lower resource type to handle ty.
-  if (HLModule::IsHLSLObjectType(Ty) &&
+  if (dxilutil::IsHLSLObjectType(Ty) &&
     !HLModule::IsStreamOutputPtrType(V->getType())) {
     Value *Res = V;
     if (!bOut) {
@@ -5128,7 +5128,7 @@ Value *SROA_Parameter_HLSL::castResourceArgIfRequired(
       arraySize *= AT->getArrayNumElements();
       AT = AT->getArrayElementType();
     }
-    if (HLModule::IsHLSLObjectType(AT)) {
+    if (dxilutil::IsHLSLObjectType(AT)) {
       Value *Res = V;
       Type *Ty = ArrayType::get(HandleTy, arraySize);
       V = AllocaBuilder.CreateAlloca(Ty);
@@ -6962,14 +6962,14 @@ void ResourceToHandle::initialize(Module &M) {
 bool ResourceToHandle::needToLower(Value *V) {
   Type *Ty = V->getType()->getPointerElementType();
   Ty = dxilutil::GetArrayEltTy(Ty);
-  return (HLModule::IsHLSLObjectType(Ty) &&
+  return (dxilutil::IsHLSLObjectType(Ty) &&
           !HLModule::IsStreamOutputType(Ty)) &&
          // Skip lib profile.
          !m_bIsLib;
 }
 
 Type *ResourceToHandle::lowerType(Type *Ty) {
-  if ((HLModule::IsHLSLObjectType(Ty) && !HLModule::IsStreamOutputType(Ty))) {
+  if ((dxilutil::IsHLSLObjectType(Ty) && !HLModule::IsStreamOutputType(Ty))) {
     return m_HandleTy;
   }
 
@@ -7055,7 +7055,7 @@ void ResourceToHandle::ReplaceResourceGEPWithHandleGEP(
   if (Ty->isArrayTy()) {
     ReplaceResourceArrayWithHandleArray(GEP, newGEP);
   } else {
-    DXASSERT(HLModule::IsHLSLObjectType(Ty), "must be resource type here");
+    DXASSERT(dxilutil::IsHLSLObjectType(Ty), "must be resource type here");
     ReplaceResourceWithHandle(GEP, newGEP);
   }
 }

+ 18 - 15
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -389,6 +389,9 @@ CGMSHLSLRuntime::CGMSHLSLRuntime(CodeGenModule &CGM)
   opts.bFXCCompatMode = CGM.getLangOpts().EnableFXCCompatMode;
 
   m_pHLModule->SetHLOptions(opts);
+  m_pHLModule->GetOP()->SetMinPrecision(opts.bUseMinPrecision);
+  m_pHLModule->GetTypeSystem().SetMinPrecision(opts.bUseMinPrecision);
+
   m_pHLModule->SetAutoBindingSpace(CGM.getCodeGenOpts().HLSLDefaultSpace);
 
   m_pHLModule->SetValidatorVersion(CGM.getCodeGenOpts().HLSLValidatorMajorVer, CGM.getCodeGenOpts().HLSLValidatorMinorVer);
@@ -443,7 +446,7 @@ CGMSHLSLRuntime::CGMSHLSLRuntime(CodeGenModule &CGM)
 }
 
 bool CGMSHLSLRuntime::IsHlslObjectType(llvm::Type *Ty) {
-  return HLModule::IsHLSLObjectType(Ty);
+  return dxilutil::IsHLSLObjectType(Ty);
 }
 
 void CGMSHLSLRuntime::AddHLSLIntrinsicOpcodeToFunction(Function *F,
@@ -3437,7 +3440,7 @@ static void AddOpcodeParamForIntrinsic(HLModule &HLM, Function *F,
     llvm::Type *Ty = paramTyList[i];
     if (Ty->isPointerTy()) {
       Ty = Ty->getPointerElementType();
-      if (HLModule::IsHLSLObjectType(Ty) &&
+      if (dxilutil::IsHLSLObjectType(Ty) &&
           // StreamOutput don't need handle.
           !HLModule::IsStreamOutputType(Ty)) {
         // Use handle type for object type.
@@ -3498,7 +3501,7 @@ static void AddOpcodeParamForIntrinsic(HLModule &HLM, Function *F,
     gep_type_iterator GEPIt = gep_type_begin(objGEP), E = gep_type_end(objGEP);
     llvm::Type *resTy = nullptr;
     while (GEPIt != E) {
-      if (HLModule::IsHLSLObjectType(*GEPIt)) {
+      if (dxilutil::IsHLSLObjectType(*GEPIt)) {
         resTy = *GEPIt;
         break;
       }
@@ -3581,7 +3584,7 @@ static void AddOpcodeParamForIntrinsic(HLModule &HLM, Function *F,
       llvm::Type *Ty = arg->getType();
       if (Ty->isPointerTy()) {
         Ty = Ty->getPointerElementType();
-        if (HLModule::IsHLSLObjectType(Ty) &&
+        if (dxilutil::IsHLSLObjectType(Ty) &&
           // StreamOutput don't need handle.
           !HLModule::IsStreamOutputType(Ty)) {
           // Use object type directly, not by pointer.
@@ -4541,7 +4544,7 @@ static void CreateWriteEnabledStaticGlobals(llvm::Module *M,
   for (GlobalVariable &GV : M->globals()) {
     if (!GV.isConstant() && GV.getLinkage() != GlobalValue::InternalLinkage &&
         // skip globals which are HLSL objects or group shared
-        !HLModule::IsHLSLObjectType(GV.getType()->getPointerElementType()) &&
+        !dxilutil::IsHLSLObjectType(GV.getType()->getPointerElementType()) &&
         !dxilutil::IsSharedMemoryGlobal(&GV)) {
       if (GlobalHasStoreUser(&GV))
         worklist.emplace_back(&GV);
@@ -5092,7 +5095,7 @@ void CGMSHLSLRuntime::FlattenValToInitList(CodeGenFunction &CGF, SmallVector<Val
       } else {
         // Struct.
         StructType *ST = cast<StructType>(valEltTy);
-        if (HLModule::IsHLSLObjectType(ST)) {
+        if (dxilutil::IsHLSLObjectType(ST)) {
           // Save object directly like basic type.
           elts.emplace_back(Builder.CreateLoad(val));
           eltTys.emplace_back(Ty);
@@ -5207,7 +5210,7 @@ static void AddMissingCastOpsInInitList(SmallVector<Value *, 4> &elts, SmallVect
     for (unsigned i = 0; i < matSize; i++)
       AddMissingCastOpsInInitList(elts, eltTys, idx, EltTy, CGF);
   } else if (Ty->isRecordType()) {
-    if (HLModule::IsHLSLObjectType(CGF.ConvertType(Ty))) {
+    if (dxilutil::IsHLSLObjectType(CGF.ConvertType(Ty))) {
       // Skip hlsl object.
       idx++;
     } else {
@@ -5303,7 +5306,7 @@ static void StoreInitListToDestPtr(Value *DestPtr,
           {DestPtr, matVal}, M);
     }
   } else if (Ty->isStructTy()) {
-    if (HLModule::IsHLSLObjectType(Ty)) {
+    if (dxilutil::IsHLSLObjectType(Ty)) {
       Builder.CreateStore(elts[idx], DestPtr);
       idx++;
     } else {
@@ -6305,7 +6308,7 @@ void CGMSHLSLRuntime::FlattenAggregatePtrToGepList(
     }
 
   } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
-    if (HLModule::IsHLSLObjectType(ST)) {
+    if (dxilutil::IsHLSLObjectType(ST)) {
       // Avoid split HLSL object.
       Value *GEP = CGF.Builder.CreateInBoundsGEP(Ptr, idxList);
       GepList.push_back(GEP);
@@ -6451,7 +6454,7 @@ void CGMSHLSLRuntime::EmitHLSLAggregateCopy(
     Value *ldMat = EmitHLSLMatrixLoad(CGF, srcGEP, SrcType);
     EmitHLSLMatrixStore(CGF, ldMat, dstGEP, DestType);
   } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
-    if (HLModule::IsHLSLObjectType(ST)) {
+    if (dxilutil::IsHLSLObjectType(ST)) {
       // Avoid split HLSL object.
       SimpleCopy(DestPtr, SrcPtr, idxList, CGF.Builder);
       return;
@@ -6557,8 +6560,8 @@ void CGMSHLSLRuntime::EmitHLSLFlatConversionAggregateCopy(CodeGenFunction &CGF,
       CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, size, 1);
       return;
     }
-  } else if (HLModule::IsHLSLObjectType(dxilutil::GetArrayEltTy(SrcPtrTy)) &&
-             HLModule::IsHLSLObjectType(dxilutil::GetArrayEltTy(DestPtrTy))) {
+  } else if (dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(SrcPtrTy)) &&
+             dxilutil::IsHLSLObjectType(dxilutil::GetArrayEltTy(DestPtrTy))) {
     unsigned sizeSrc = TheModule.getDataLayout().getTypeAllocSize(SrcPtrTy);
     unsigned sizeDest = TheModule.getDataLayout().getTypeAllocSize(DestPtrTy);
     CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, std::max(sizeSrc, sizeDest), 1);
@@ -6668,7 +6671,7 @@ void CGMSHLSLRuntime::EmitHLSLFlatConversionToAggregate(
         CGF.Builder, HLOpcodeGroup::HLInit, 0, Ty, {VecMat}, TheModule);
     EmitHLSLMatrixStore(CGF, MatInit, dstGEP, Type);
   } else if (StructType *ST = dyn_cast<StructType>(Ty)) {
-    DXASSERT(!HLModule::IsHLSLObjectType(ST), "cannot cast to hlsl object, Sema should reject");
+    DXASSERT(!dxilutil::IsHLSLObjectType(ST), "cannot cast to hlsl object, Sema should reject");
 
     const clang::RecordType *RT = Type->getAsStructureType();
     RecordDecl *RD = RT->getDecl();
@@ -6879,7 +6882,7 @@ void CGMSHLSLRuntime::EmitHLSLOutParamConversionInit(
       castArgList.emplace_back(argLV);
     }
 
-    bool isObject = HLModule::IsHLSLObjectType(
+    bool isObject = dxilutil::IsHLSLObjectType(
         tmpArgAddr->getType()->getPointerElementType());
 
     // cast before the call
@@ -6936,7 +6939,7 @@ void CGMSHLSLRuntime::EmitHLSLOutParamConversionCopyBack(
     bool isAggrageteTy = ArgTy->isAggregateType();
     isAggrageteTy &= !IsHLSLVecMatType(ArgTy);
 
-    bool isObject = HLModule::IsHLSLObjectType(
+    bool isObject = dxilutil::IsHLSLObjectType(
        tmpArgAddr->getType()->getPointerElementType());
     if (!isObject) {
       if (!isAggrageteTy) {