Forráskód Böngészése

Fixed global constructors evaluated at codegen aren't removed (#3389)

Adam Yang 4 éve
szülő
commit
d08b307146

+ 9 - 3
lib/HLSL/ComputeViewIdStateBuilder.cpp

@@ -509,6 +509,14 @@ void DxilViewIdStateBuilder::CollectValuesContributingToOutputRec(EntryInfo &Ent
     return;
   }
 
+  BasicBlock *pBB = pContributingInst->getParent();
+  Function *F = pBB->getParent();
+  auto FuncInfoIt = m_FuncInfo.find(F);
+  DXASSERT_NOMSG(FuncInfoIt != m_FuncInfo.end());
+  if (FuncInfoIt == m_FuncInfo.end()) {
+    return;
+  }
+
   auto itInst = ContributingInstructions.emplace(pContributingInst);
   // Already visited instruction.
   if (!itInst.second) return;
@@ -552,9 +560,7 @@ void DxilViewIdStateBuilder::CollectValuesContributingToOutputRec(EntryInfo &Ent
   }
 
   // Handle control dependence of this instruction BB.
-  BasicBlock *pBB = pContributingInst->getParent();
-  Function *F = pBB->getParent();
-  FuncInfo *pFuncInfo = m_FuncInfo[F].get();
+  FuncInfo *pFuncInfo = FuncInfoIt->second.get();
   const BasicBlockSet &CtrlDepSet = pFuncInfo->CtrlDep.GetCDBlocks(pBB);
   for (BasicBlock *B : CtrlDepSet) {
     CollectValuesContributingToOutputRec(Entry, B->getTerminator(), ContributingInstructions);

+ 8 - 0
tools/clang/lib/CodeGen/CGHLSLMSFinishCodeGen.cpp

@@ -2387,6 +2387,7 @@ void CollectCtorFunctions(llvm::Module &M, llvm::StringRef globalName,
 
   DenseSet<Function *> Callers = CollectExternalFunctionCallers(M);
 
+  bool allEvaluated = true;
   for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
     if (isa<ConstantAggregateZero>(*i))
       continue;
@@ -2407,6 +2408,7 @@ void CollectCtorFunctions(llvm::Module &M, llvm::StringRef globalName,
         // Try to build imm initilizer.
         // If not work, add global call to entry func.
         if (BuildImmInit(F) == false) {
+          allEvaluated = false;
           if (IsValidCtorFunction(F, Callers)) {
             Ctors.emplace_back(F);
           } else {
@@ -2419,6 +2421,12 @@ void CollectCtorFunctions(llvm::Module &M, llvm::StringRef globalName,
       }
     }
   }
+
+  // If all globals constructors are replaced with initializers, just get rid
+  // of the GV.
+  if (allEvaluated) {
+    GV->eraseFromParent();
+  }
 }
 
 void ProcessCtorFunctions(llvm::Module &M,

+ 23 - 0
tools/clang/test/CodeGenHLSL/global_initializer.hlsl

@@ -0,0 +1,23 @@
+// RUN: %dxc -E main -T ps_6_0 -O0 %s | FileCheck %s
+
+// Make sure global initializer is correctly removed
+// when the initial values are constants at codegen
+// time.
+
+// CHECK: @main
+
+static float2 x[5] = {
+  float2(1, 1) / 2,
+  float2(2, 2) / 2,
+  float2(3, 3) / 2,
+  float2(4, 4) / 2,
+  float2(5, 5) / 2,
+};
+
+[RootSignature("CBV(b0)")]
+float2 main(int i : I) : SV_Target{ 
+  return x[i];
+}
+
+
+