2017-06-08 16:37:21 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "aggressive_dead_code_elim_pass.h"
|
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
#include "cfa.h"
|
2017-06-08 16:37:21 +00:00
|
|
|
#include "iterator.h"
|
|
|
|
#include "spirv/1.0/GLSL.std.450.h"
|
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
#include <stack>
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint32_t kTypePointerStorageClassInIdx = 0;
|
2017-10-10 20:35:53 +00:00
|
|
|
const uint32_t kEntryPointFunctionIdInIdx = 1;
|
2017-10-17 22:33:43 +00:00
|
|
|
const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
|
|
|
|
const uint32_t kLoopMergeMergeBlockIdInIdx = 0;
|
2017-11-28 23:18:05 +00:00
|
|
|
const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
} // namespace
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) {
|
2017-10-25 17:26:25 +00:00
|
|
|
const ir::Instruction* varInst = get_def_use_mgr()->GetDef(varId);
|
2017-08-10 22:42:16 +00:00
|
|
|
const SpvOp op = varInst->opcode();
|
2017-11-08 17:40:02 +00:00
|
|
|
if (op != SpvOpVariable) return false;
|
2017-06-08 16:37:21 +00:00
|
|
|
const uint32_t varTypeId = varInst->type_id();
|
2017-10-25 17:26:25 +00:00
|
|
|
const ir::Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (varTypeInst->opcode() != SpvOpTypePointer) return false;
|
2017-06-08 16:37:21 +00:00
|
|
|
return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) ==
|
2017-11-08 17:40:02 +00:00
|
|
|
storageClass;
|
2017-10-10 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AggressiveDCEPass::IsLocalVar(uint32_t varId) {
|
2017-11-08 17:40:02 +00:00
|
|
|
return IsVarOfStorage(varId, SpvStorageClassFunction) ||
|
|
|
|
(IsVarOfStorage(varId, SpvStorageClassPrivate) && private_like_local_);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::AddStores(uint32_t ptrId) {
|
2017-11-14 19:11:50 +00:00
|
|
|
get_def_use_mgr()->ForEachUser(ptrId, [this](ir::Instruction* user) {
|
|
|
|
switch (user->opcode()) {
|
2017-06-08 16:37:21 +00:00
|
|
|
case SpvOpAccessChain:
|
|
|
|
case SpvOpInBoundsAccessChain:
|
2017-11-14 19:11:50 +00:00
|
|
|
case SpvOpCopyObject:
|
|
|
|
this->AddStores(user->result_id());
|
|
|
|
break;
|
2017-06-08 16:37:21 +00:00
|
|
|
case SpvOpLoad:
|
|
|
|
break;
|
2017-11-14 19:11:50 +00:00
|
|
|
// If default, assume it stores e.g. frexp, modf, function call
|
2017-06-08 16:37:21 +00:00
|
|
|
case SpvOpStore:
|
2017-11-14 19:11:50 +00:00
|
|
|
default:
|
|
|
|
if (!IsLive(user)) AddToWorklist(user);
|
|
|
|
break;
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
});
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
bool AggressiveDCEPass::AllExtensionsSupported() const {
|
|
|
|
// If any extension not in whitelist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2017-11-08 17:40:02 +00:00
|
|
|
const char* extName =
|
|
|
|
reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
|
2017-07-19 00:57:26 +00:00
|
|
|
if (extensions_whitelist_.find(extName) == extensions_whitelist_.end())
|
|
|
|
return false;
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-07-19 00:57:26 +00:00
|
|
|
return true;
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 19:47:46 +00:00
|
|
|
bool AggressiveDCEPass::IsTargetDead(ir::Instruction* inst) {
|
2017-06-08 16:37:21 +00:00
|
|
|
const uint32_t tId = inst->GetSingleWordInOperand(0);
|
2017-10-25 17:26:25 +00:00
|
|
|
const ir::Instruction* tInst = get_def_use_mgr()->GetDef(tId);
|
2017-08-04 21:04:37 +00:00
|
|
|
if (dead_insts_.find(tInst) != dead_insts_.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::ProcessLoad(uint32_t varId) {
|
|
|
|
// Only process locals
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!IsLocalVar(varId)) return;
|
2017-08-04 21:04:37 +00:00
|
|
|
// Return if already processed
|
2017-11-08 17:40:02 +00:00
|
|
|
if (live_local_vars_.find(varId) != live_local_vars_.end()) return;
|
2017-08-04 21:04:37 +00:00
|
|
|
// Mark all stores to varId as live
|
|
|
|
AddStores(varId);
|
|
|
|
// Cache varId as processed
|
|
|
|
live_local_vars_.insert(varId);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
bool AggressiveDCEPass::IsStructuredIfHeader(ir::BasicBlock* bp,
|
2017-11-08 17:40:02 +00:00
|
|
|
ir::Instruction** mergeInst,
|
|
|
|
ir::Instruction** branchInst,
|
|
|
|
uint32_t* mergeBlockId) {
|
2017-10-17 22:33:43 +00:00
|
|
|
auto ii = bp->end();
|
|
|
|
--ii;
|
2017-11-08 17:40:02 +00:00
|
|
|
if (ii->opcode() != SpvOpBranchConditional) return false;
|
|
|
|
if (ii == bp->begin()) return false;
|
2017-10-17 22:33:43 +00:00
|
|
|
if (branchInst != nullptr) *branchInst = &*ii;
|
|
|
|
--ii;
|
2017-11-08 17:40:02 +00:00
|
|
|
if (ii->opcode() != SpvOpSelectionMerge) return false;
|
|
|
|
if (mergeInst != nullptr) *mergeInst = &*ii;
|
2017-10-17 22:33:43 +00:00
|
|
|
if (mergeBlockId != nullptr)
|
|
|
|
*mergeBlockId =
|
|
|
|
ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::ComputeBlock2HeaderMaps(
|
2017-11-08 17:40:02 +00:00
|
|
|
std::list<ir::BasicBlock*>& structuredOrder) {
|
2017-10-17 22:33:43 +00:00
|
|
|
block2headerMerge_.clear();
|
|
|
|
block2headerBranch_.clear();
|
|
|
|
std::stack<ir::Instruction*> currentMergeInst;
|
|
|
|
std::stack<ir::Instruction*> currentBranchInst;
|
|
|
|
std::stack<uint32_t> currentMergeBlockId;
|
|
|
|
currentMergeInst.push(nullptr);
|
|
|
|
currentBranchInst.push(nullptr);
|
|
|
|
currentMergeBlockId.push(0);
|
|
|
|
for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) {
|
|
|
|
if ((*bi)->id() == currentMergeBlockId.top()) {
|
|
|
|
currentMergeBlockId.pop();
|
|
|
|
currentMergeInst.pop();
|
|
|
|
currentBranchInst.pop();
|
|
|
|
}
|
|
|
|
block2headerMerge_[*bi] = currentMergeInst.top();
|
|
|
|
block2headerBranch_[*bi] = currentBranchInst.top();
|
|
|
|
ir::Instruction* mergeInst;
|
|
|
|
ir::Instruction* branchInst;
|
|
|
|
uint32_t mergeBlockId;
|
|
|
|
if (IsStructuredIfHeader(*bi, &mergeInst, &branchInst, &mergeBlockId)) {
|
|
|
|
currentMergeBlockId.push(mergeBlockId);
|
|
|
|
currentMergeInst.push(mergeInst);
|
|
|
|
currentBranchInst.push(branchInst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::ComputeInst2BlockMap(ir::Function* func) {
|
|
|
|
for (auto& blk : *func) {
|
2017-11-08 17:40:02 +00:00
|
|
|
blk.ForEachInst(
|
|
|
|
[&blk, this](ir::Instruction* ip) { inst2block_[ip] = &blk; });
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::AddBranch(uint32_t labelId, ir::BasicBlock* bp) {
|
2017-11-08 17:40:02 +00:00
|
|
|
std::unique_ptr<ir::Instruction> newBranch(new ir::Instruction(
|
2017-11-14 19:11:50 +00:00
|
|
|
context(), SpvOpBranch, 0, 0,
|
2017-11-08 17:40:02 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
|
2017-10-17 22:33:43 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(&*newBranch);
|
|
|
|
bp->AddInstruction(std::move(newBranch));
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:18:05 +00:00
|
|
|
void AggressiveDCEPass::AddBranchesToWorklist(uint32_t labelId) {
|
|
|
|
get_def_use_mgr()->ForEachUser(labelId, [this](ir::Instruction* user) {
|
|
|
|
SpvOp op = user->opcode();
|
|
|
|
if (op == SpvOpBranchConditional || op == SpvOpBranch)
|
|
|
|
if (!IsLive(user)) AddToWorklist(user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
bool AggressiveDCEPass::AggressiveDCE(ir::Function* func) {
|
2017-10-17 22:33:43 +00:00
|
|
|
// Compute map from instruction to block
|
|
|
|
ComputeInst2BlockMap(func);
|
|
|
|
// Compute map from block to controlling conditional branch
|
|
|
|
std::list<ir::BasicBlock*> structuredOrder;
|
2017-10-30 21:42:26 +00:00
|
|
|
cfg()->ComputeStructuredOrder(func, &*func->begin(), &structuredOrder);
|
2017-10-17 22:33:43 +00:00
|
|
|
ComputeBlock2HeaderMaps(structuredOrder);
|
2017-06-08 16:37:21 +00:00
|
|
|
bool modified = false;
|
2017-10-17 22:33:43 +00:00
|
|
|
// Add instructions with external side effects to worklist. Also add branches
|
|
|
|
// EXCEPT those immediately contained in an "if" selection construct.
|
2017-06-08 16:37:21 +00:00
|
|
|
// TODO(greg-lunarg): Handle Frexp, Modf more optimally
|
2017-10-10 20:35:53 +00:00
|
|
|
call_in_func_ = false;
|
|
|
|
func_is_entry_point_ = false;
|
|
|
|
private_stores_.clear();
|
2017-10-17 22:33:43 +00:00
|
|
|
// Stacks to keep track of when we are inside an if-construct. When not
|
2017-11-08 17:40:02 +00:00
|
|
|
// immediately inside an in-construct, we must assume all branches are live.
|
2017-10-17 22:33:43 +00:00
|
|
|
std::stack<bool> assume_branches_live;
|
|
|
|
std::stack<uint32_t> currentMergeBlockId;
|
|
|
|
// Push sentinel values on stack for when outside of any control flow.
|
|
|
|
assume_branches_live.push(true);
|
|
|
|
currentMergeBlockId.push(0);
|
|
|
|
for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) {
|
|
|
|
if ((*bi)->id() == currentMergeBlockId.top()) {
|
|
|
|
assume_branches_live.pop();
|
|
|
|
currentMergeBlockId.pop();
|
|
|
|
}
|
|
|
|
for (auto ii = (*bi)->begin(); ii != (*bi)->end(); ++ii) {
|
|
|
|
SpvOp op = ii->opcode();
|
2017-06-08 16:37:21 +00:00
|
|
|
switch (op) {
|
|
|
|
case SpvOpStore: {
|
|
|
|
uint32_t varId;
|
2017-11-08 17:40:02 +00:00
|
|
|
(void)GetPtr(&*ii, &varId);
|
2017-10-10 20:35:53 +00:00
|
|
|
// Mark stores as live if their variable is not function scope
|
|
|
|
// and is not private scope. Remember private stores for possible
|
|
|
|
// later inclusion
|
|
|
|
if (IsVarOfStorage(varId, SpvStorageClassPrivate))
|
2017-10-17 22:33:43 +00:00
|
|
|
private_stores_.push_back(&*ii);
|
2017-10-10 20:35:53 +00:00
|
|
|
else if (!IsVarOfStorage(varId, SpvStorageClassFunction))
|
2017-10-17 22:33:43 +00:00
|
|
|
AddToWorklist(&*ii);
|
2017-06-08 16:37:21 +00:00
|
|
|
} break;
|
2017-10-17 22:33:43 +00:00
|
|
|
case SpvOpLoopMerge: {
|
|
|
|
// Assume loops live (for now)
|
|
|
|
// TODO(greg-lunarg): Add dead loop elimination
|
|
|
|
assume_branches_live.push(true);
|
|
|
|
currentMergeBlockId.push(
|
|
|
|
ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx));
|
|
|
|
AddToWorklist(&*ii);
|
|
|
|
} break;
|
|
|
|
case SpvOpSelectionMerge: {
|
|
|
|
auto brii = ii;
|
|
|
|
++brii;
|
|
|
|
bool is_structured_if = brii->opcode() == SpvOpBranchConditional;
|
|
|
|
assume_branches_live.push(!is_structured_if);
|
|
|
|
currentMergeBlockId.push(
|
|
|
|
ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx));
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!is_structured_if) AddToWorklist(&*ii);
|
2017-10-17 22:33:43 +00:00
|
|
|
} break;
|
|
|
|
case SpvOpBranch:
|
|
|
|
case SpvOpBranchConditional: {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (assume_branches_live.top()) AddToWorklist(&*ii);
|
2017-06-08 16:37:21 +00:00
|
|
|
} break;
|
|
|
|
default: {
|
2017-10-17 22:33:43 +00:00
|
|
|
// Function calls, atomics, function params, function returns, etc.
|
2017-08-04 21:04:37 +00:00
|
|
|
// TODO(greg-lunarg): function calls live only if write to non-local
|
2017-11-11 01:26:55 +00:00
|
|
|
if (!context()->IsCombinatorInstruction(&*ii)) {
|
|
|
|
AddToWorklist(&*ii);
|
|
|
|
}
|
2017-10-10 20:35:53 +00:00
|
|
|
// Remember function calls
|
2017-11-08 17:40:02 +00:00
|
|
|
if (op == SpvOpFunctionCall) call_in_func_ = true;
|
2017-06-08 16:37:21 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 20:35:53 +00:00
|
|
|
// See if current function is an entry point
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->entry_points()) {
|
2017-10-10 20:35:53 +00:00
|
|
|
if (ei.GetSingleWordInOperand(kEntryPointFunctionIdInIdx) ==
|
|
|
|
func->result_id()) {
|
|
|
|
func_is_entry_point_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the current function is an entry point and has no function calls,
|
|
|
|
// we can optimize private variables as locals
|
|
|
|
private_like_local_ = func_is_entry_point_ && !call_in_func_;
|
|
|
|
// If privates are not like local, add their stores to worklist
|
|
|
|
if (!private_like_local_)
|
2017-11-08 17:40:02 +00:00
|
|
|
for (auto& ps : private_stores_) AddToWorklist(ps);
|
2017-06-08 16:37:21 +00:00
|
|
|
// Add OpGroupDecorates to worklist because they are a pain to remove
|
|
|
|
// ids from.
|
|
|
|
// TODO(greg-lunarg): Handle dead ids in OpGroupDecorate
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ai : get_module()->annotations()) {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (ai.opcode() == SpvOpGroupDecorate) AddToWorklist(&ai);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
// Perform closure on live instruction set.
|
2017-06-08 16:37:21 +00:00
|
|
|
while (!worklist_.empty()) {
|
|
|
|
ir::Instruction* liveInst = worklist_.front();
|
|
|
|
// Add all operand instructions if not already live
|
|
|
|
liveInst->ForEachInId([this](const uint32_t* iid) {
|
2017-10-25 17:26:25 +00:00
|
|
|
ir::Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!IsLive(inInst)) AddToWorklist(inInst);
|
2017-06-08 16:37:21 +00:00
|
|
|
});
|
2017-10-17 22:33:43 +00:00
|
|
|
// If in a structured if construct, add the controlling conditional branch
|
|
|
|
// and its merge. Any containing if construct is marked live when the
|
|
|
|
// the merge and branch are processed out of the worklist.
|
|
|
|
ir::BasicBlock* blk = inst2block_[liveInst];
|
|
|
|
ir::Instruction* branchInst = block2headerBranch_[blk];
|
|
|
|
if (branchInst != nullptr && !IsLive(branchInst)) {
|
|
|
|
AddToWorklist(branchInst);
|
|
|
|
AddToWorklist(block2headerMerge_[blk]);
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
// If local load, add all variable's stores if variable not already live
|
|
|
|
if (liveInst->opcode() == SpvOpLoad) {
|
|
|
|
uint32_t varId;
|
2017-11-08 17:40:02 +00:00
|
|
|
(void)GetPtr(liveInst, &varId);
|
2017-08-04 21:04:37 +00:00
|
|
|
ProcessLoad(varId);
|
|
|
|
}
|
|
|
|
// If function call, treat as if it loads from all pointer arguments
|
|
|
|
else if (liveInst->opcode() == SpvOpFunctionCall) {
|
|
|
|
liveInst->ForEachInId([this](const uint32_t* iid) {
|
|
|
|
// Skip non-ptr args
|
|
|
|
if (!IsPtr(*iid)) return;
|
|
|
|
uint32_t varId;
|
2017-11-08 17:40:02 +00:00
|
|
|
(void)GetPtr(*iid, &varId);
|
2017-08-04 21:04:37 +00:00
|
|
|
ProcessLoad(varId);
|
|
|
|
});
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-11-28 23:18:05 +00:00
|
|
|
// If loop merge, add all branches to continue and merge blocks
|
|
|
|
// to worklist
|
|
|
|
else if (liveInst->opcode() == SpvOpLoopMerge) {
|
|
|
|
AddBranchesToWorklist(
|
|
|
|
liveInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx));
|
|
|
|
AddBranchesToWorklist(
|
|
|
|
liveInst->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx));
|
|
|
|
}
|
2017-08-10 22:42:16 +00:00
|
|
|
// If function parameter, treat as if it's result id is loaded from
|
|
|
|
else if (liveInst->opcode() == SpvOpFunctionParameter) {
|
|
|
|
ProcessLoad(liveInst->result_id());
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
worklist_.pop();
|
|
|
|
}
|
2017-10-17 22:33:43 +00:00
|
|
|
// Mark all non-live instructions dead, except branches which are not
|
|
|
|
// at the end of an if-header, which indicate a dead if.
|
|
|
|
for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) {
|
|
|
|
for (auto ii = (*bi)->begin(); ii != (*bi)->end(); ++ii) {
|
2017-11-08 17:40:02 +00:00
|
|
|
if (IsLive(&*ii)) continue;
|
2017-11-17 13:59:25 +00:00
|
|
|
if (ii->IsBranch() &&
|
2017-10-17 22:33:43 +00:00
|
|
|
!IsStructuredIfHeader(*bi, nullptr, nullptr, nullptr))
|
|
|
|
continue;
|
|
|
|
dead_insts_.insert(&*ii);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove debug and annotation statements referencing dead instructions.
|
|
|
|
// This must be done before killing the instructions, otherwise there are
|
|
|
|
// dead objects in the def/use database.
|
2017-11-21 19:47:46 +00:00
|
|
|
ir::Instruction* instruction = &*get_module()->debug2_begin();
|
|
|
|
while (instruction) {
|
|
|
|
if (instruction->opcode() != SpvOpName) {
|
|
|
|
instruction = instruction->NextNode();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTargetDead(instruction)) {
|
|
|
|
instruction = context()->KillInst(instruction);
|
|
|
|
modified = true;
|
|
|
|
} else {
|
|
|
|
instruction = instruction->NextNode();
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-11-21 19:47:46 +00:00
|
|
|
|
|
|
|
instruction = &*get_module()->annotation_begin();
|
|
|
|
while (instruction) {
|
|
|
|
if (instruction->opcode() != SpvOpDecorate &&
|
|
|
|
instruction->opcode() != SpvOpDecorateId) {
|
|
|
|
instruction = instruction->NextNode();
|
2017-06-08 16:37:21 +00:00
|
|
|
continue;
|
2017-11-21 19:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTargetDead(instruction)) {
|
|
|
|
instruction = context()->KillInst(instruction);
|
|
|
|
modified = true;
|
|
|
|
} else {
|
|
|
|
instruction = instruction->NextNode();
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-11-21 19:47:46 +00:00
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
// Kill dead instructions and remember dead blocks
|
|
|
|
for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();) {
|
|
|
|
uint32_t mergeBlockId = 0;
|
2017-11-21 19:47:46 +00:00
|
|
|
(*bi)->ForEachInst([this, &modified, &mergeBlockId](ir::Instruction* inst) {
|
|
|
|
if (dead_insts_.find(inst) == dead_insts_.end()) return;
|
2017-10-17 22:33:43 +00:00
|
|
|
// If dead instruction is selection merge, remember merge block
|
|
|
|
// for new branch at end of block
|
2017-11-21 19:47:46 +00:00
|
|
|
if (inst->opcode() == SpvOpSelectionMerge)
|
2017-11-08 17:40:02 +00:00
|
|
|
mergeBlockId =
|
2017-11-21 19:47:46 +00:00
|
|
|
inst->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
|
|
|
|
context()->KillInst(inst);
|
2017-06-08 16:37:21 +00:00
|
|
|
modified = true;
|
2017-11-21 19:47:46 +00:00
|
|
|
});
|
2017-10-17 22:33:43 +00:00
|
|
|
// If a structured if was deleted, add a branch to its merge block,
|
|
|
|
// and traverse to the merge block, continuing processing there.
|
|
|
|
// The block still exists as the OpLabel at least is still intact.
|
|
|
|
if (mergeBlockId != 0) {
|
|
|
|
AddBranch(mergeBlockId, *bi);
|
|
|
|
for (++bi; (*bi)->id() != mergeBlockId; ++bi) {
|
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
} else {
|
2017-10-17 22:33:43 +00:00
|
|
|
++bi;
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2017-10-17 22:33:43 +00:00
|
|
|
// Cleanup all CFG including all unreachable blocks
|
|
|
|
CFGCleanup(func);
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
void AggressiveDCEPass::Initialize(ir::IRContext* c) {
|
|
|
|
InitializeProcessing(c);
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-07-10 15:45:59 +00:00
|
|
|
// Clear collections
|
|
|
|
worklist_ = std::queue<ir::Instruction*>{};
|
|
|
|
live_insts_.clear();
|
|
|
|
live_local_vars_.clear();
|
|
|
|
dead_insts_.clear();
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
// Initialize extensions whitelist
|
|
|
|
InitExtensions();
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pass::Status AggressiveDCEPass::ProcessImpl() {
|
2017-11-08 17:40:02 +00:00
|
|
|
// Current functionality assumes shader capability
|
2017-06-08 16:37:21 +00:00
|
|
|
// TODO(greg-lunarg): Handle additional capabilities
|
2017-10-25 17:26:25 +00:00
|
|
|
if (!get_module()->HasCapability(SpvCapabilityShader))
|
2017-06-08 16:37:21 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
|
|
|
// Current functionality assumes logical addressing only
|
|
|
|
// TODO(greg-lunarg): Handle non-logical addressing
|
2017-10-25 17:26:25 +00:00
|
|
|
if (get_module()->HasCapability(SpvCapabilityAddresses))
|
2017-06-08 16:37:21 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
2017-07-10 15:45:59 +00:00
|
|
|
// If any extensions in the module are not explicitly supported,
|
2017-11-08 17:40:02 +00:00
|
|
|
// return unmodified.
|
|
|
|
if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
|
2017-07-19 00:57:26 +00:00
|
|
|
// Process all entry point functions
|
2017-11-08 17:40:02 +00:00
|
|
|
ProcessFunction pfn = [this](ir::Function* fp) { return AggressiveDCE(fp); };
|
2017-10-25 17:26:25 +00:00
|
|
|
bool modified = ProcessEntryPointCallTree(pfn, get_module());
|
2017-06-08 16:37:21 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2017-07-27 19:30:12 +00:00
|
|
|
AggressiveDCEPass::AggressiveDCEPass() {}
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Pass::Status AggressiveDCEPass::Process(ir::IRContext* c) {
|
|
|
|
Initialize(c);
|
2017-06-08 16:37:21 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
void AggressiveDCEPass::InitExtensions() {
|
|
|
|
extensions_whitelist_.clear();
|
|
|
|
extensions_whitelist_.insert({
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_AMD_shader_explicit_vertex_parameter",
|
2017-11-11 01:26:55 +00:00
|
|
|
"SPV_AMD_shader_trinary_minmax",
|
|
|
|
"SPV_AMD_gcn_shader",
|
|
|
|
"SPV_KHR_shader_ballot",
|
|
|
|
"SPV_AMD_shader_ballot",
|
|
|
|
"SPV_AMD_gpu_shader_half_float",
|
|
|
|
"SPV_KHR_shader_draw_parameters",
|
|
|
|
"SPV_KHR_subgroup_vote",
|
|
|
|
"SPV_KHR_16bit_storage",
|
|
|
|
"SPV_KHR_device_group",
|
|
|
|
"SPV_KHR_multiview",
|
|
|
|
"SPV_NVX_multiview_per_view_attributes",
|
|
|
|
"SPV_NV_viewport_array2",
|
|
|
|
"SPV_NV_stereo_view_rendering",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_NV_sample_mask_override_coverage",
|
2017-11-11 01:26:55 +00:00
|
|
|
"SPV_NV_geometry_shader_passthrough",
|
|
|
|
"SPV_AMD_texture_gather_bias_lod",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_storage_buffer_storage_class",
|
|
|
|
// SPV_KHR_variable_pointers
|
|
|
|
// Currently do not support extended pointer expressions
|
2017-11-11 01:26:55 +00:00
|
|
|
"SPV_AMD_gpu_shader_int16",
|
|
|
|
"SPV_KHR_post_depth_coverage",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_shader_atomic_counter_ops",
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|