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

This commit is contained in:
Adam Yang 2021-01-26 13:35:48 -08:00 коммит произвёл GitHub
Родитель 3d918a239d
Коммит d08b307146
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 40 добавлений и 3 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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,

Просмотреть файл

@ -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];
}