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.
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
// Copyright (c) 2018-2021 Google LLC
|
2017-06-08 16:37:21 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/aggressive_dead_code_elim_pass.h"
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
2017-10-17 22:33:43 +00:00
|
|
|
#include <stack>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/cfa.h"
|
2020-08-13 18:54:14 +00:00
|
|
|
#include "source/opt/eliminate_dead_functions_util.h"
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
#include "source/opt/ir_builder.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/reflect.h"
|
2019-05-07 18:52:22 +00:00
|
|
|
#include "source/spirv_constant.h"
|
2021-12-08 17:01:26 +00:00
|
|
|
#include "source/util/string_utils.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace {
|
|
|
|
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr uint32_t kTypePointerStorageClassInIdx = 0;
|
|
|
|
constexpr uint32_t kEntryPointFunctionIdInIdx = 1;
|
|
|
|
constexpr uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
|
|
|
|
constexpr uint32_t kLoopMergeContinueBlockIdInIdx = 1;
|
|
|
|
constexpr uint32_t kCopyMemoryTargetAddrInIdx = 0;
|
|
|
|
constexpr uint32_t kCopyMemorySourceAddrInIdx = 1;
|
|
|
|
constexpr uint32_t kLoadSourceAddrInIdx = 0;
|
|
|
|
constexpr uint32_t kDebugDeclareOperandVariableIndex = 5;
|
|
|
|
constexpr uint32_t kGlobalVariableVariableIndex = 12;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Sorting functor to present annotation instructions in an easy-to-process
|
|
|
|
// order. The functor orders by opcode first and falls back on unique id
|
|
|
|
// ordering if both instructions have the same opcode.
|
|
|
|
//
|
|
|
|
// Desired priority:
|
2022-11-04 21:27:10 +00:00
|
|
|
// spv::Op::OpGroupDecorate
|
|
|
|
// spv::Op::OpGroupMemberDecorate
|
|
|
|
// spv::Op::OpDecorate
|
|
|
|
// spv::Op::OpMemberDecorate
|
|
|
|
// spv::Op::OpDecorateId
|
|
|
|
// spv::Op::OpDecorateStringGOOGLE
|
|
|
|
// spv::Op::OpDecorationGroup
|
2017-12-18 17:13:10 +00:00
|
|
|
struct DecorationLess {
|
2018-07-12 19:14:43 +00:00
|
|
|
bool operator()(const Instruction* lhs, const Instruction* rhs) const {
|
2017-12-18 17:13:10 +00:00
|
|
|
assert(lhs && rhs);
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op lhsOp = lhs->opcode();
|
|
|
|
spv::Op rhsOp = rhs->opcode();
|
2017-12-18 17:13:10 +00:00
|
|
|
if (lhsOp != rhsOp) {
|
2018-04-16 15:33:13 +00:00
|
|
|
#define PRIORITY_CASE(opcode) \
|
|
|
|
if (lhsOp == opcode && rhsOp != opcode) return true; \
|
|
|
|
if (rhsOp == opcode && lhsOp != opcode) return false;
|
2017-12-18 17:13:10 +00:00
|
|
|
// OpGroupDecorate and OpGroupMember decorate are highest priority to
|
|
|
|
// eliminate dead targets early and simplify subsequent checks.
|
2022-11-04 21:27:10 +00:00
|
|
|
PRIORITY_CASE(spv::Op::OpGroupDecorate)
|
|
|
|
PRIORITY_CASE(spv::Op::OpGroupMemberDecorate)
|
|
|
|
PRIORITY_CASE(spv::Op::OpDecorate)
|
|
|
|
PRIORITY_CASE(spv::Op::OpMemberDecorate)
|
|
|
|
PRIORITY_CASE(spv::Op::OpDecorateId)
|
|
|
|
PRIORITY_CASE(spv::Op::OpDecorateStringGOOGLE)
|
2017-12-18 17:13:10 +00:00
|
|
|
// OpDecorationGroup is lowest priority to ensure use/def chains remain
|
|
|
|
// usable for instructions that target this group.
|
2022-11-04 21:27:10 +00:00
|
|
|
PRIORITY_CASE(spv::Op::OpDecorationGroup)
|
2018-04-16 15:33:13 +00:00
|
|
|
#undef PRIORITY_CASE
|
2017-12-18 17:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to maintain total ordering (compare unique ids).
|
|
|
|
return *lhs < *rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
} // namespace
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId,
|
|
|
|
spv::StorageClass storageClass) {
|
2017-12-11 18:10:24 +00:00
|
|
|
if (varId == 0) return false;
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
|
2022-11-04 21:27:10 +00:00
|
|
|
const spv::Op op = varInst->opcode();
|
|
|
|
if (op != spv::Op::OpVariable) return false;
|
2017-06-08 16:37:21 +00:00
|
|
|
const uint32_t varTypeId = varInst->type_id();
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (varTypeInst->opcode() != spv::Op::OpTypePointer) return false;
|
|
|
|
return spv::StorageClass(varTypeInst->GetSingleWordInOperand(
|
|
|
|
kTypePointerStorageClassInIdx)) == storageClass;
|
2017-10-10 20:35:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 17:24:29 +00:00
|
|
|
bool AggressiveDCEPass::IsLocalVar(uint32_t varId, Function* func) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (IsVarOfStorage(varId, spv::StorageClass::Function)) {
|
2018-05-08 18:02:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-26 17:24:29 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!IsVarOfStorage(varId, spv::StorageClass::Private) &&
|
|
|
|
!IsVarOfStorage(varId, spv::StorageClass::Workgroup)) {
|
2018-05-08 18:02:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:24:29 +00:00
|
|
|
// For a variable in the Private or WorkGroup storage class, the variable will
|
|
|
|
// get a new instance for every call to an entry point. If the entry point
|
|
|
|
// does not have a call, then no other function can read or write to that
|
|
|
|
// instance of the variable.
|
|
|
|
return IsEntryPointWithNoCalls(func);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:08:48 +00:00
|
|
|
void AggressiveDCEPass::AddStores(Function* func, uint32_t ptrId) {
|
|
|
|
get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId, func](Instruction* user) {
|
|
|
|
// If the user is not a part of |func|, skip it.
|
|
|
|
BasicBlock* blk = context()->get_instr_block(user);
|
|
|
|
if (blk && blk->GetParent() != func) return;
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
switch (user->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpAccessChain:
|
|
|
|
case spv::Op::OpInBoundsAccessChain:
|
|
|
|
case spv::Op::OpCopyObject:
|
2020-06-29 17:08:48 +00:00
|
|
|
this->AddStores(func, user->result_id());
|
2017-11-14 19:11:50 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoad:
|
2017-06-08 16:37:21 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCopyMemory:
|
|
|
|
case spv::Op::OpCopyMemorySized:
|
2018-05-16 14:41:40 +00:00
|
|
|
if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) {
|
|
|
|
AddToWorklist(user);
|
|
|
|
}
|
|
|
|
break;
|
2017-11-14 19:11:50 +00:00
|
|
|
// If default, assume it stores e.g. frexp, modf, function call
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpStore:
|
2017-11-14 19:11:50 +00:00
|
|
|
default:
|
2018-01-18 19:15:54 +00:00
|
|
|
AddToWorklist(user);
|
2017-11-14 19:11:50 +00:00
|
|
|
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 {
|
2020-06-15 17:20:40 +00:00
|
|
|
// If any extension not in allowlist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2021-12-08 17:01:26 +00:00
|
|
|
const std::string extName = ei.GetInOperand(0).AsString();
|
2020-06-15 17:20:40 +00:00
|
|
|
if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
|
2017-07-19 00:57:26 +00:00
|
|
|
return false;
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
2021-09-15 18:38:53 +00:00
|
|
|
// Only allow NonSemantic.Shader.DebugInfo.100, we cannot safely optimise
|
|
|
|
// around unknown extended instruction sets even if they are non-semantic
|
|
|
|
for (auto& inst : context()->module()->ext_inst_imports()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(inst.opcode() == spv::Op::OpExtInstImport &&
|
2021-09-15 18:38:53 +00:00
|
|
|
"Expecting an import of an extension's instruction set.");
|
2021-12-08 17:01:26 +00:00
|
|
|
const std::string extension_name = inst.GetInOperand(0).AsString();
|
|
|
|
if (spvtools::utils::starts_with(extension_name, "NonSemantic.") &&
|
2023-05-10 08:03:40 +00:00
|
|
|
(extension_name != "NonSemantic.Shader.DebugInfo.100") &&
|
|
|
|
(extension_name != "NonSemantic.DebugPrintf")) {
|
2021-09-15 18:38:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 00:57:26 +00:00
|
|
|
return true;
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool AggressiveDCEPass::IsTargetDead(Instruction* inst) {
|
2017-06-08 16:37:21 +00:00
|
|
|
const uint32_t tId = inst->GetSingleWordInOperand(0);
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* tInst = get_def_use_mgr()->GetDef(tId);
|
|
|
|
if (IsAnnotationInst(tInst->opcode())) {
|
2017-12-18 17:13:10 +00:00
|
|
|
// This must be a decoration group. We go through annotations in a specific
|
|
|
|
// order. So if this is not used by any group or group member decorates, it
|
|
|
|
// is dead.
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(tInst->opcode() == spv::Op::OpDecorationGroup);
|
2017-12-18 17:13:10 +00:00
|
|
|
bool dead = true;
|
2018-07-12 19:14:43 +00:00
|
|
|
get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (user->opcode() == spv::Op::OpGroupDecorate ||
|
|
|
|
user->opcode() == spv::Op::OpGroupMemberDecorate)
|
2017-12-18 17:13:10 +00:00
|
|
|
dead = false;
|
|
|
|
});
|
|
|
|
return dead;
|
2017-08-04 21:04:37 +00:00
|
|
|
}
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
return !IsLive(tInst);
|
2017-08-04 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:08:48 +00:00
|
|
|
void AggressiveDCEPass::ProcessLoad(Function* func, uint32_t varId) {
|
2017-08-04 21:04:37 +00:00
|
|
|
// Only process locals
|
2021-10-26 17:24:29 +00:00
|
|
|
if (!IsLocalVar(varId, func)) 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
|
2020-06-29 17:08:48 +00:00
|
|
|
AddStores(func, varId);
|
2017-08-04 21:04:37 +00:00
|
|
|
// Cache varId as processed
|
|
|
|
live_local_vars_.insert(varId);
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void AggressiveDCEPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
|
|
|
|
std::unique_ptr<Instruction> newBranch(
|
2022-11-04 21:27:10 +00:00
|
|
|
new Instruction(context(), spv::Op::OpBranch, 0, 0,
|
2018-07-12 19:14:43 +00:00
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
|
2018-06-01 17:04:46 +00:00
|
|
|
context()->AnalyzeDefUse(&*newBranch);
|
|
|
|
context()->set_instr_block(&*newBranch, bp);
|
2017-10-17 22:33:43 +00:00
|
|
|
bp->AddInstruction(std::move(newBranch));
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:27:46 +00:00
|
|
|
void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
|
2018-08-21 15:54:44 +00:00
|
|
|
Instruction* mergeInst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(mergeInst->opcode() == spv::Op::OpSelectionMerge ||
|
|
|
|
mergeInst->opcode() == spv::Op::OpLoopMerge);
|
2018-08-21 15:54:44 +00:00
|
|
|
|
|
|
|
BasicBlock* header = context()->get_instr_block(mergeInst);
|
|
|
|
const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0);
|
2021-09-24 17:21:45 +00:00
|
|
|
get_def_use_mgr()->ForEachUser(mergeId, [header, this](Instruction* user) {
|
|
|
|
if (!user->IsBranch()) return;
|
|
|
|
BasicBlock* block = context()->get_instr_block(user);
|
|
|
|
if (BlockIsInConstruct(header, block)) {
|
|
|
|
// This is a break from the loop.
|
|
|
|
AddToWorklist(user);
|
|
|
|
// Add branch's merge if there is one.
|
|
|
|
Instruction* userMerge = GetMergeInstruction(user);
|
|
|
|
if (userMerge != nullptr) AddToWorklist(userMerge);
|
|
|
|
}
|
|
|
|
});
|
2018-08-21 15:54:44 +00:00
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (mergeInst->opcode() != spv::Op::OpLoopMerge) {
|
2018-08-21 15:54:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For loops we need to find the continues as well.
|
2017-12-12 21:27:46 +00:00
|
|
|
const uint32_t contId =
|
2018-08-21 15:54:44 +00:00
|
|
|
mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
|
2018-07-12 19:14:43 +00:00
|
|
|
get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) {
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op op = user->opcode();
|
|
|
|
if (op == spv::Op::OpBranchConditional || op == spv::Op::OpSwitch) {
|
2018-01-15 18:25:45 +00:00
|
|
|
// A conditional branch or switch can only be a continue if it does not
|
|
|
|
// have a merge instruction or its merge block is not the continue block.
|
2021-09-24 17:21:45 +00:00
|
|
|
Instruction* hdrMerge = GetMergeInstruction(user);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (hdrMerge != nullptr &&
|
|
|
|
hdrMerge->opcode() == spv::Op::OpSelectionMerge) {
|
2017-12-12 21:27:46 +00:00
|
|
|
uint32_t hdrMergeId =
|
|
|
|
hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
|
|
|
|
if (hdrMergeId == contId) return;
|
|
|
|
// Need to mark merge instruction too
|
2018-01-18 19:15:54 +00:00
|
|
|
AddToWorklist(hdrMerge);
|
2017-12-12 21:27:46 +00:00
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (op == spv::Op::OpBranch) {
|
2017-12-12 21:27:46 +00:00
|
|
|
// An unconditional branch can only be a continue if it is not
|
|
|
|
// branching to its own merge block.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* blk = context()->get_instr_block(user);
|
2021-09-24 17:21:45 +00:00
|
|
|
Instruction* hdrBranch = GetHeaderBranch(blk);
|
2017-12-12 21:27:46 +00:00
|
|
|
if (hdrBranch == nullptr) return;
|
2021-09-24 17:21:45 +00:00
|
|
|
Instruction* hdrMerge = GetMergeInstruction(hdrBranch);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (hdrMerge->opcode() == spv::Op::OpLoopMerge) return;
|
2017-12-12 21:27:46 +00:00
|
|
|
uint32_t hdrMergeId =
|
|
|
|
hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
|
|
|
|
if (contId == hdrMergeId) return;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-18 19:15:54 +00:00
|
|
|
AddToWorklist(user);
|
2017-11-28 23:18:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool AggressiveDCEPass::AggressiveDCE(Function* func) {
|
2021-10-04 12:33:10 +00:00
|
|
|
std::list<BasicBlock*> structured_order;
|
|
|
|
cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
|
2020-06-29 17:08:48 +00:00
|
|
|
live_local_vars_.clear();
|
2021-10-04 12:33:10 +00:00
|
|
|
InitializeWorkList(func, structured_order);
|
|
|
|
ProcessWorkList(func);
|
|
|
|
return KillDeadInstructions(func, structured_order);
|
|
|
|
}
|
2017-11-21 19:47:46 +00:00
|
|
|
|
2021-10-04 12:33:10 +00:00
|
|
|
bool AggressiveDCEPass::KillDeadInstructions(
|
|
|
|
const Function* func, std::list<BasicBlock*>& structured_order) {
|
|
|
|
bool modified = false;
|
|
|
|
for (auto bi = structured_order.begin(); bi != structured_order.end();) {
|
|
|
|
uint32_t merge_block_id = 0;
|
|
|
|
(*bi)->ForEachInst([this, &modified, &merge_block_id](Instruction* inst) {
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (IsLive(inst)) return;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpLabel) 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
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpSelectionMerge ||
|
|
|
|
inst->opcode() == spv::Op::OpLoopMerge)
|
2021-10-04 12:33:10 +00:00
|
|
|
merge_block_id = inst->GetSingleWordInOperand(0);
|
2017-12-18 17:13:10 +00:00
|
|
|
to_kill_.push_back(inst);
|
2017-06-08 16:37:21 +00:00
|
|
|
modified = true;
|
2017-11-21 19:47:46 +00:00
|
|
|
});
|
2017-10-25 00:58:48 +00:00
|
|
|
// If a structured if or loop was deleted, add a branch to its merge
|
|
|
|
// block, and traverse to the merge block and continue processing there.
|
|
|
|
// We know the block still exists because the label is not deleted.
|
2021-10-04 12:33:10 +00:00
|
|
|
if (merge_block_id != 0) {
|
|
|
|
AddBranch(merge_block_id, *bi);
|
|
|
|
for (++bi; (*bi)->id() != merge_block_id; ++bi) {
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
2019-04-03 14:30:12 +00:00
|
|
|
|
|
|
|
auto merge_terminator = (*bi)->terminator();
|
2022-11-04 21:27:10 +00:00
|
|
|
if (merge_terminator->opcode() == spv::Op::OpUnreachable) {
|
2019-04-03 14:30:12 +00:00
|
|
|
// The merge was unreachable. This is undefined behaviour so just
|
|
|
|
// return (or return an undef). Then mark the new return as live.
|
|
|
|
auto func_ret_type_inst = get_def_use_mgr()->GetDef(func->type_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (func_ret_type_inst->opcode() == spv::Op::OpTypeVoid) {
|
|
|
|
merge_terminator->SetOpcode(spv::Op::OpReturn);
|
2019-04-03 14:30:12 +00:00
|
|
|
} else {
|
|
|
|
// Find an undef for the return value and make sure it gets kept by
|
|
|
|
// the pass.
|
|
|
|
auto undef_id = Type2Undef(func->type_id());
|
|
|
|
auto undef = get_def_use_mgr()->GetDef(undef_id);
|
|
|
|
live_insts_.Set(undef->unique_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
merge_terminator->SetOpcode(spv::Op::OpReturnValue);
|
2019-04-03 14:30:12 +00:00
|
|
|
merge_terminator->SetInOperands({{SPV_OPERAND_TYPE_ID, {undef_id}}});
|
|
|
|
get_def_use_mgr()->AnalyzeInstUse(merge_terminator);
|
|
|
|
}
|
|
|
|
live_insts_.Set(merge_terminator->unique_id());
|
|
|
|
}
|
2017-11-08 17:40:02 +00:00
|
|
|
} else {
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
Instruction* inst = (*bi)->terminator();
|
|
|
|
if (!IsLive(inst)) {
|
|
|
|
// If the terminator is not live, this block has no live instructions,
|
|
|
|
// and it will be unreachable.
|
|
|
|
AddUnreachable(*bi);
|
|
|
|
}
|
2017-10-17 22:33:43 +00:00
|
|
|
++bi;
|
|
|
|
}
|
2017-06-08 16:37:21 +00:00
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2021-10-04 12:33:10 +00:00
|
|
|
void AggressiveDCEPass::ProcessWorkList(Function* func) {
|
|
|
|
while (!worklist_.empty()) {
|
|
|
|
Instruction* live_inst = worklist_.front();
|
|
|
|
worklist_.pop();
|
|
|
|
AddOperandsToWorkList(live_inst);
|
|
|
|
MarkBlockAsLive(live_inst);
|
|
|
|
MarkLoadedVariablesAsLive(func, live_inst);
|
|
|
|
AddDecorationsToWorkList(live_inst);
|
|
|
|
AddDebugInstructionsToWorkList(live_inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:10:17 +00:00
|
|
|
void AggressiveDCEPass::AddDebugScopeToWorkList(const Instruction* inst) {
|
|
|
|
auto scope = inst->GetDebugScope();
|
|
|
|
auto lex_scope_id = scope.GetLexicalScope();
|
|
|
|
if (lex_scope_id != kNoDebugScope)
|
|
|
|
AddToWorklist(get_def_use_mgr()->GetDef(lex_scope_id));
|
|
|
|
auto inlined_at_id = scope.GetInlinedAt();
|
|
|
|
if (inlined_at_id != kNoInlinedAt)
|
|
|
|
AddToWorklist(get_def_use_mgr()->GetDef(inlined_at_id));
|
|
|
|
}
|
|
|
|
|
2021-10-04 12:33:10 +00:00
|
|
|
void AggressiveDCEPass::AddDebugInstructionsToWorkList(
|
|
|
|
const Instruction* inst) {
|
|
|
|
for (auto& line_inst : inst->dbg_line_insts()) {
|
|
|
|
if (line_inst.IsDebugLineInst()) {
|
|
|
|
AddOperandsToWorkList(&line_inst);
|
|
|
|
}
|
2022-08-31 22:10:17 +00:00
|
|
|
AddDebugScopeToWorkList(&line_inst);
|
2021-10-04 12:33:10 +00:00
|
|
|
}
|
2022-08-31 22:10:17 +00:00
|
|
|
AddDebugScopeToWorkList(inst);
|
2021-10-04 12:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::AddDecorationsToWorkList(const Instruction* inst) {
|
|
|
|
// Add OpDecorateId instructions that apply to this instruction to the work
|
|
|
|
// list. We use the decoration manager to look through the group
|
|
|
|
// decorations to get to the OpDecorate* instructions themselves.
|
|
|
|
auto decorations =
|
|
|
|
get_decoration_mgr()->GetDecorationsFor(inst->result_id(), false);
|
|
|
|
for (Instruction* dec : decorations) {
|
|
|
|
// We only care about OpDecorateId instructions because the are the only
|
|
|
|
// decorations that will reference an id that will have to be kept live
|
|
|
|
// because of that use.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (dec->opcode() != spv::Op::OpDecorateId) {
|
2021-10-04 12:33:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Decoration(dec->GetSingleWordInOperand(1)) ==
|
|
|
|
spv::Decoration::HlslCounterBufferGOOGLE) {
|
2021-10-04 12:33:10 +00:00
|
|
|
// These decorations should not force the use id to be live. It will be
|
|
|
|
// removed if either the target or the in operand are dead.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AddToWorklist(dec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::MarkLoadedVariablesAsLive(Function* func,
|
|
|
|
Instruction* inst) {
|
|
|
|
std::vector<uint32_t> live_variables = GetLoadedVariables(inst);
|
|
|
|
for (uint32_t var_id : live_variables) {
|
|
|
|
ProcessLoad(func, var_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariables(Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpFunctionCall) {
|
2021-10-04 12:33:10 +00:00
|
|
|
return GetLoadedVariablesFromFunctionCall(inst);
|
|
|
|
}
|
|
|
|
uint32_t var_id = GetLoadedVariableFromNonFunctionCalls(inst);
|
|
|
|
if (var_id == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return {var_id};
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AggressiveDCEPass::GetLoadedVariableFromNonFunctionCalls(
|
|
|
|
Instruction* inst) {
|
|
|
|
std::vector<uint32_t> live_variables;
|
|
|
|
if (inst->IsAtomicWithLoad()) {
|
|
|
|
return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inst->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoad:
|
|
|
|
case spv::Op::OpImageTexelPointer:
|
2021-10-04 12:33:10 +00:00
|
|
|
return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCopyMemory:
|
|
|
|
case spv::Op::OpCopyMemorySized:
|
2021-10-04 12:33:10 +00:00
|
|
|
return GetVariableId(
|
|
|
|
inst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inst->GetCommonDebugOpcode()) {
|
|
|
|
case CommonDebugInfoDebugDeclare:
|
|
|
|
return inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
|
|
|
|
case CommonDebugInfoDebugValue: {
|
|
|
|
analysis::DebugInfoManager* debug_info_mgr =
|
|
|
|
context()->get_debug_info_mgr();
|
|
|
|
return debug_info_mgr->GetVariableIdOfDebugValueUsedForDeclare(inst);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariablesFromFunctionCall(
|
|
|
|
const Instruction* inst) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(inst->opcode() == spv::Op::OpFunctionCall);
|
2021-10-04 12:33:10 +00:00
|
|
|
std::vector<uint32_t> live_variables;
|
2023-07-17 19:16:25 +00:00
|
|
|
// NOTE: we should only be checking function call parameters here, not the
|
|
|
|
// function itself, however, `IsPtr` will trivially return false for
|
|
|
|
// OpFunction
|
2021-10-04 12:33:10 +00:00
|
|
|
inst->ForEachInId([this, &live_variables](const uint32_t* operand_id) {
|
|
|
|
if (!IsPtr(*operand_id)) return;
|
|
|
|
uint32_t var_id = GetVariableId(*operand_id);
|
|
|
|
live_variables.push_back(var_id);
|
|
|
|
});
|
|
|
|
return live_variables;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AggressiveDCEPass::GetVariableId(uint32_t ptr_id) {
|
|
|
|
assert(IsPtr(ptr_id) &&
|
|
|
|
"Cannot get the variable when input is not a pointer.");
|
|
|
|
uint32_t varId = 0;
|
|
|
|
(void)GetPtr(ptr_id, &varId);
|
|
|
|
return varId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::MarkBlockAsLive(Instruction* inst) {
|
|
|
|
BasicBlock* basic_block = context()->get_instr_block(inst);
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (basic_block == nullptr) {
|
|
|
|
return;
|
2021-10-04 12:33:10 +00:00
|
|
|
}
|
|
|
|
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
// If we intend to keep this instruction, we need the block label and
|
|
|
|
// block terminator to have a valid block for the instruction.
|
|
|
|
AddToWorklist(basic_block->GetLabelInst());
|
|
|
|
|
|
|
|
// We need to mark the successors blocks that follow as live. If this is
|
|
|
|
// header of the merge construct, the construct may be folded, but we will
|
|
|
|
// definitely need the merge label. If it is not a construct, the terminator
|
|
|
|
// must be live, and the successor blocks will be marked as live when
|
|
|
|
// processing the terminator.
|
|
|
|
uint32_t merge_id = basic_block->MergeBlockIdIfAny();
|
|
|
|
if (merge_id == 0) {
|
|
|
|
AddToWorklist(basic_block->terminator());
|
|
|
|
} else {
|
|
|
|
AddToWorklist(context()->get_def_use_mgr()->GetDef(merge_id));
|
2021-10-04 12:33:10 +00:00
|
|
|
}
|
|
|
|
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
// Mark the structured control flow constructs that contains this block as
|
|
|
|
// live. If |inst| is an instruction in the loop header, then it is part of
|
|
|
|
// the loop, so the loop construct must be live. We exclude the label because
|
|
|
|
// it does not matter how many times it is executed. This could be extended
|
|
|
|
// to more instructions, but we will need it for now.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() != spv::Op::OpLabel)
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
MarkLoopConstructAsLiveIfLoopHeader(basic_block);
|
|
|
|
|
|
|
|
Instruction* next_branch_inst = GetBranchForNextHeader(basic_block);
|
|
|
|
if (next_branch_inst != nullptr) {
|
|
|
|
AddToWorklist(next_branch_inst);
|
|
|
|
Instruction* mergeInst = GetMergeInstruction(next_branch_inst);
|
2021-10-04 12:33:10 +00:00
|
|
|
AddToWorklist(mergeInst);
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpLoopMerge ||
|
|
|
|
inst->opcode() == spv::Op::OpSelectionMerge) {
|
2021-10-04 12:33:10 +00:00
|
|
|
AddBreaksAndContinuesToWorklist(inst);
|
|
|
|
}
|
|
|
|
}
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
void AggressiveDCEPass::MarkLoopConstructAsLiveIfLoopHeader(
|
|
|
|
BasicBlock* basic_block) {
|
|
|
|
// If this is the header for a loop, then loop structure needs to keep as well
|
|
|
|
// because the loop header is also part of the loop.
|
|
|
|
Instruction* merge_inst = basic_block->GetLoopMergeInst();
|
|
|
|
if (merge_inst != nullptr) {
|
|
|
|
AddToWorklist(basic_block->terminator());
|
|
|
|
AddToWorklist(merge_inst);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 12:33:10 +00:00
|
|
|
|
|
|
|
void AggressiveDCEPass::AddOperandsToWorkList(const Instruction* inst) {
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
inst->ForEachInId([this](const uint32_t* iid) {
|
2021-10-04 12:33:10 +00:00
|
|
|
Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
|
|
|
|
AddToWorklist(inInst);
|
|
|
|
});
|
|
|
|
if (inst->type_id() != 0) {
|
|
|
|
AddToWorklist(get_def_use_mgr()->GetDef(inst->type_id()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 17:21:45 +00:00
|
|
|
void AggressiveDCEPass::InitializeWorkList(
|
2021-10-04 12:33:10 +00:00
|
|
|
Function* func, std::list<BasicBlock*>& structured_order) {
|
|
|
|
AddToWorklist(&func->DefInst());
|
|
|
|
MarkFunctionParameterAsLive(func);
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
MarkFirstBlockAsLive(func);
|
2021-10-04 12:33:10 +00:00
|
|
|
|
2021-09-24 17:21:45 +00:00
|
|
|
// Add instructions with external side effects to the worklist. Also add
|
|
|
|
// branches that are not attached to a structured construct.
|
|
|
|
// TODO(s-perron): The handling of branch seems to be adhoc. This needs to be
|
|
|
|
// cleaned up.
|
2021-10-04 12:33:10 +00:00
|
|
|
for (auto& bi : structured_order) {
|
|
|
|
for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Op op = ii->opcode();
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (ii->IsBranch()) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-24 17:21:45 +00:00
|
|
|
switch (op) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpStore: {
|
2021-10-04 12:33:10 +00:00
|
|
|
uint32_t var_id = 0;
|
|
|
|
(void)GetPtr(&*ii, &var_id);
|
2021-10-26 17:24:29 +00:00
|
|
|
if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
|
2021-09-24 17:21:45 +00:00
|
|
|
} break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpCopyMemory:
|
|
|
|
case spv::Op::OpCopyMemorySized: {
|
2021-10-04 12:33:10 +00:00
|
|
|
uint32_t var_id = 0;
|
2021-10-26 17:24:29 +00:00
|
|
|
uint32_t target_addr_id =
|
|
|
|
ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx);
|
|
|
|
(void)GetPtr(target_addr_id, &var_id);
|
|
|
|
if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
|
2021-09-24 17:21:45 +00:00
|
|
|
} break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoopMerge:
|
|
|
|
case spv::Op::OpSelectionMerge:
|
|
|
|
case spv::Op::OpUnreachable:
|
2021-09-24 17:21:45 +00:00
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
// Function calls, atomics, function params, function returns, etc.
|
|
|
|
if (!ii->IsOpcodeSafeToDelete()) {
|
|
|
|
AddToWorklist(&*ii);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
|
2018-03-13 17:48:48 +00:00
|
|
|
// Keep all execution modes.
|
2017-12-18 17:13:10 +00:00
|
|
|
for (auto& exec : get_module()->execution_modes()) {
|
|
|
|
AddToWorklist(&exec);
|
|
|
|
}
|
2018-03-13 17:48:48 +00:00
|
|
|
// Keep all entry points.
|
2017-12-18 17:13:10 +00:00
|
|
|
for (auto& entry : get_module()->entry_points()) {
|
2021-11-16 20:54:17 +00:00
|
|
|
if (!preserve_interface_) {
|
2019-05-07 18:52:22 +00:00
|
|
|
live_insts_.Set(entry.unique_id());
|
|
|
|
// The actual function is live always.
|
|
|
|
AddToWorklist(
|
|
|
|
get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(1u)));
|
|
|
|
for (uint32_t i = 3; i < entry.NumInOperands(); ++i) {
|
|
|
|
auto* var = get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
|
|
|
|
auto storage_class = var->GetSingleWordInOperand(0u);
|
2021-11-16 20:54:17 +00:00
|
|
|
// Vulkan support outputs without an associated input, but not inputs
|
2022-11-23 17:48:58 +00:00
|
|
|
// without an associated output. Don't remove outputs unless explicitly
|
|
|
|
// allowed.
|
|
|
|
if (!remove_outputs_ &&
|
|
|
|
spv::StorageClass(storage_class) == spv::StorageClass::Output) {
|
2019-05-07 18:52:22 +00:00
|
|
|
AddToWorklist(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AddToWorklist(&entry);
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
}
|
2018-03-13 17:48:48 +00:00
|
|
|
for (auto& anno : get_module()->annotations()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (anno.opcode() == spv::Op::OpDecorate) {
|
2019-07-10 18:12:19 +00:00
|
|
|
// Keep workgroup size.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Decoration(anno.GetSingleWordInOperand(1u)) ==
|
|
|
|
spv::Decoration::BuiltIn &&
|
|
|
|
spv::BuiltIn(anno.GetSingleWordInOperand(2u)) ==
|
|
|
|
spv::BuiltIn::WorkgroupSize) {
|
2018-03-13 17:48:48 +00:00
|
|
|
AddToWorklist(&anno);
|
|
|
|
}
|
2019-07-10 18:12:19 +00:00
|
|
|
|
|
|
|
if (context()->preserve_bindings()) {
|
|
|
|
// Keep all bindings.
|
2022-11-04 21:27:10 +00:00
|
|
|
if ((spv::Decoration(anno.GetSingleWordInOperand(1u)) ==
|
|
|
|
spv::Decoration::DescriptorSet) ||
|
|
|
|
(spv::Decoration(anno.GetSingleWordInOperand(1u)) ==
|
|
|
|
spv::Decoration::Binding)) {
|
2019-07-10 18:12:19 +00:00
|
|
|
AddToWorklist(&anno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context()->preserve_spec_constants()) {
|
|
|
|
// Keep all specialization constant instructions
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Decoration(anno.GetSingleWordInOperand(1u)) ==
|
|
|
|
spv::Decoration::SpecId) {
|
2019-07-10 18:12:19 +00:00
|
|
|
AddToWorklist(&anno);
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 17:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 20:10:09 +00:00
|
|
|
|
|
|
|
// For each DebugInfo GlobalVariable keep all operands except the Variable.
|
2021-10-14 17:29:54 +00:00
|
|
|
// Later, if the variable is killed with KillInst(), we will set the operand
|
|
|
|
// to DebugInfoNone. Create and save DebugInfoNone now for this possible
|
|
|
|
// later use. This is slightly unoptimal, but it avoids generating it during
|
|
|
|
// instruction killing when the module is not consistent.
|
|
|
|
bool debug_global_seen = false;
|
2020-07-21 20:10:09 +00:00
|
|
|
for (auto& dbg : get_module()->ext_inst_debuginfo()) {
|
2021-07-21 16:04:38 +00:00
|
|
|
if (dbg.GetCommonDebugOpcode() != CommonDebugInfoDebugGlobalVariable)
|
2020-07-21 20:10:09 +00:00
|
|
|
continue;
|
2021-10-14 17:29:54 +00:00
|
|
|
debug_global_seen = true;
|
2020-07-21 20:10:09 +00:00
|
|
|
dbg.ForEachInId([this](const uint32_t* iid) {
|
2021-10-04 12:33:10 +00:00
|
|
|
Instruction* in_inst = get_def_use_mgr()->GetDef(*iid);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (in_inst->opcode() == spv::Op::OpVariable) return;
|
2021-10-04 12:33:10 +00:00
|
|
|
AddToWorklist(in_inst);
|
2020-07-21 20:10:09 +00:00
|
|
|
});
|
|
|
|
}
|
2021-10-14 17:29:54 +00:00
|
|
|
if (debug_global_seen) {
|
|
|
|
auto dbg_none = context()->get_debug_info_mgr()->GetDebugInfoNone();
|
|
|
|
AddToWorklist(dbg_none);
|
|
|
|
}
|
2022-08-15 21:23:23 +00:00
|
|
|
|
|
|
|
// Add top level DebugInfo to worklist
|
|
|
|
for (auto& dbg : get_module()->ext_inst_debuginfo()) {
|
|
|
|
auto op = dbg.GetShader100DebugOpcode();
|
|
|
|
if (op == NonSemanticShaderDebugInfo100DebugCompilationUnit ||
|
|
|
|
op == NonSemanticShaderDebugInfo100DebugEntryPoint ||
|
|
|
|
op == NonSemanticShaderDebugInfo100DebugSourceContinued) {
|
|
|
|
AddToWorklist(&dbg);
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader))
|
2017-06-08 16:37:21 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
2019-04-03 16:47:51 +00:00
|
|
|
|
2017-12-11 18:10:24 +00:00
|
|
|
// Current functionality assumes relaxed logical addressing (see
|
|
|
|
// instruction.h)
|
2017-06-08 16:37:21 +00:00
|
|
|
// TODO(greg-lunarg): Handle non-logical addressing
|
2022-11-04 21:27:10 +00:00
|
|
|
if (context()->get_feature_mgr()->HasCapability(spv::Capability::Addresses))
|
2017-06-08 16:37:21 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
2019-04-03 16:47:51 +00:00
|
|
|
|
|
|
|
// The variable pointer extension is no longer needed to use the capability,
|
|
|
|
// so we have to look for the capability.
|
|
|
|
if (context()->get_feature_mgr()->HasCapability(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Capability::VariablePointersStorageBuffer))
|
2019-04-03 16:47:51 +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-12-18 17:13:10 +00:00
|
|
|
|
|
|
|
// Eliminate Dead functions.
|
|
|
|
bool modified = EliminateDeadFunctions();
|
|
|
|
|
|
|
|
InitializeModuleScopeLiveInstructions();
|
|
|
|
|
2022-07-05 16:23:32 +00:00
|
|
|
// Run |AggressiveDCE| on the remaining functions. The order does not matter,
|
|
|
|
// since |AggressiveDCE| is intra-procedural. This can mean that function
|
|
|
|
// will become dead if all function call to them are removed. These dead
|
|
|
|
// function will still be in the module after this pass. We expect this to be
|
|
|
|
// rare.
|
|
|
|
for (Function& fp : *context()->module()) {
|
|
|
|
modified |= AggressiveDCE(&fp);
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
|
2019-07-24 18:43:49 +00:00
|
|
|
// If the decoration manager is kept live then the context will try to keep it
|
|
|
|
// up to date. ADCE deals with group decorations by changing the operands in
|
|
|
|
// |OpGroupDecorate| instruction directly without informing the decoration
|
|
|
|
// manager. This can put it in an invalid state which will cause an error
|
|
|
|
// when the context tries to update it. To avoid this problem invalidate
|
|
|
|
// the decoration manager upfront.
|
|
|
|
//
|
|
|
|
// We kill it at now because it is used when processing the entry point
|
|
|
|
// functions.
|
|
|
|
context()->InvalidateAnalyses(IRContext::Analysis::kAnalysisDecorations);
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Process module-level instructions. Now that all live instructions have
|
|
|
|
// been marked, it is safe to remove dead global values.
|
|
|
|
modified |= ProcessGlobalValues();
|
|
|
|
|
2020-07-29 03:55:02 +00:00
|
|
|
assert((to_kill_.empty() || modified) &&
|
|
|
|
"A dead instruction was identified, but no change recorded.");
|
2019-08-30 20:27:22 +00:00
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Kill all dead instructions.
|
|
|
|
for (auto inst : to_kill_) {
|
|
|
|
context()->KillInst(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup all CFG including all unreachable blocks.
|
2022-07-05 16:23:32 +00:00
|
|
|
for (Function& fp : *context()->module()) {
|
|
|
|
modified |= CFGCleanup(&fp);
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
bool AggressiveDCEPass::EliminateDeadFunctions() {
|
|
|
|
// Identify live functions first. Those that are not live
|
2021-08-31 16:39:46 +00:00
|
|
|
// are dead.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<const Function*> live_function_set;
|
|
|
|
ProcessFunction mark_live = [&live_function_set](Function* fp) {
|
2017-12-18 17:13:10 +00:00
|
|
|
live_function_set.insert(fp);
|
|
|
|
return false;
|
|
|
|
};
|
2021-08-31 16:39:46 +00:00
|
|
|
context()->ProcessReachableCallTree(mark_live);
|
2017-12-18 17:13:10 +00:00
|
|
|
|
|
|
|
bool modified = false;
|
|
|
|
for (auto funcIter = get_module()->begin();
|
|
|
|
funcIter != get_module()->end();) {
|
|
|
|
if (live_function_set.count(&*funcIter) == 0) {
|
|
|
|
modified = true;
|
2020-08-13 18:54:14 +00:00
|
|
|
funcIter =
|
|
|
|
eliminatedeadfunctionsutil::EliminateFunction(context(), &funcIter);
|
2017-12-18 17:13:10 +00:00
|
|
|
} else {
|
|
|
|
++funcIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AggressiveDCEPass::ProcessGlobalValues() {
|
|
|
|
// 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.
|
|
|
|
bool modified = false;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* instruction = &*get_module()->debug2_begin();
|
2017-12-18 17:13:10 +00:00
|
|
|
while (instruction) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (instruction->opcode() != spv::Op::OpName) {
|
2017-12-18 17:13:10 +00:00
|
|
|
instruction = instruction->NextNode();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTargetDead(instruction)) {
|
|
|
|
instruction = context()->KillInst(instruction);
|
|
|
|
modified = true;
|
|
|
|
} else {
|
|
|
|
instruction = instruction->NextNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This code removes all unnecessary decorations safely (see #1174). It also
|
|
|
|
// does so in a more efficient manner than deleting them only as the targets
|
|
|
|
// are deleted.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> annotations;
|
2017-12-18 17:13:10 +00:00
|
|
|
for (auto& inst : get_module()->annotations()) annotations.push_back(&inst);
|
|
|
|
std::sort(annotations.begin(), annotations.end(), DecorationLess());
|
|
|
|
for (auto annotation : annotations) {
|
|
|
|
switch (annotation->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorate:
|
|
|
|
case spv::Op::OpMemberDecorate:
|
|
|
|
case spv::Op::OpDecorateStringGOOGLE:
|
|
|
|
case spv::Op::OpMemberDecorateStringGOOGLE:
|
2018-04-16 15:33:13 +00:00
|
|
|
if (IsTargetDead(annotation)) {
|
|
|
|
context()->KillInst(annotation);
|
|
|
|
modified = true;
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorateId:
|
2018-10-05 12:23:09 +00:00
|
|
|
if (IsTargetDead(annotation)) {
|
|
|
|
context()->KillInst(annotation);
|
|
|
|
modified = true;
|
|
|
|
} else {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (spv::Decoration(annotation->GetSingleWordInOperand(1)) ==
|
|
|
|
spv::Decoration::HlslCounterBufferGOOGLE) {
|
2018-10-05 12:23:09 +00:00
|
|
|
// HlslCounterBuffer will reference an id other than the target.
|
|
|
|
// If that id is dead, then the decoration can be removed as well.
|
|
|
|
uint32_t counter_buffer_id = annotation->GetSingleWordInOperand(2);
|
|
|
|
Instruction* counter_buffer_inst =
|
|
|
|
get_def_use_mgr()->GetDef(counter_buffer_id);
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (!IsLive(counter_buffer_inst)) {
|
2018-10-05 12:23:09 +00:00
|
|
|
context()->KillInst(annotation);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupDecorate: {
|
2017-12-18 17:13:10 +00:00
|
|
|
// Go through the targets of this group decorate. Remove each dead
|
|
|
|
// target. If all targets are dead, remove this decoration.
|
|
|
|
bool dead = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
bool removed_operand = false;
|
2017-12-18 17:13:10 +00:00
|
|
|
for (uint32_t i = 1; i < annotation->NumOperands();) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* opInst =
|
2017-12-18 17:13:10 +00:00
|
|
|
get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (!IsLive(opInst)) {
|
2017-12-18 17:13:10 +00:00
|
|
|
// Don't increment |i|.
|
|
|
|
annotation->RemoveOperand(i);
|
2018-04-16 15:33:13 +00:00
|
|
|
modified = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
removed_operand = true;
|
2017-12-18 17:13:10 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
dead = false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 15:33:13 +00:00
|
|
|
if (dead) {
|
|
|
|
context()->KillInst(annotation);
|
|
|
|
modified = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
} else if (removed_operand) {
|
|
|
|
context()->UpdateDefUse(annotation);
|
2018-04-16 15:33:13 +00:00
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpGroupMemberDecorate: {
|
2017-12-18 17:13:10 +00:00
|
|
|
// Go through the targets of this group member decorate. Remove each
|
|
|
|
// dead target (and member index). If all targets are dead, remove this
|
|
|
|
// decoration.
|
|
|
|
bool dead = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
bool removed_operand = false;
|
2017-12-18 17:13:10 +00:00
|
|
|
for (uint32_t i = 1; i < annotation->NumOperands();) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* opInst =
|
2017-12-18 17:13:10 +00:00
|
|
|
get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (!IsLive(opInst)) {
|
2017-12-18 17:13:10 +00:00
|
|
|
// Don't increment |i|.
|
|
|
|
annotation->RemoveOperand(i + 1);
|
|
|
|
annotation->RemoveOperand(i);
|
2018-04-16 15:33:13 +00:00
|
|
|
modified = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
removed_operand = true;
|
2017-12-18 17:13:10 +00:00
|
|
|
} else {
|
|
|
|
i += 2;
|
|
|
|
dead = false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 15:33:13 +00:00
|
|
|
if (dead) {
|
|
|
|
context()->KillInst(annotation);
|
|
|
|
modified = true;
|
2018-09-28 18:39:06 +00:00
|
|
|
} else if (removed_operand) {
|
|
|
|
context()->UpdateDefUse(annotation);
|
2018-04-16 15:33:13 +00:00
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpDecorationGroup:
|
2017-12-18 17:13:10 +00:00
|
|
|
// By the time we hit decoration groups we've checked everything that
|
|
|
|
// can target them. So if they have no uses they must be dead.
|
2018-04-16 15:33:13 +00:00
|
|
|
if (get_def_use_mgr()->NumUsers(annotation) == 0) {
|
2017-12-18 17:13:10 +00:00
|
|
|
context()->KillInst(annotation);
|
2018-04-16 15:33:13 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:10:09 +00:00
|
|
|
for (auto& dbg : get_module()->ext_inst_debuginfo()) {
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (IsLive(&dbg)) continue;
|
2020-07-21 20:10:09 +00:00
|
|
|
// Save GlobalVariable if its variable is live, otherwise null out variable
|
|
|
|
// index
|
2021-07-21 16:04:38 +00:00
|
|
|
if (dbg.GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
|
2020-07-21 20:10:09 +00:00
|
|
|
auto var_id = dbg.GetSingleWordOperand(kGlobalVariableVariableIndex);
|
|
|
|
Instruction* var_inst = get_def_use_mgr()->GetDef(var_id);
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (IsLive(var_inst)) continue;
|
2020-07-21 20:10:09 +00:00
|
|
|
context()->ForgetUses(&dbg);
|
|
|
|
dbg.SetOperand(
|
|
|
|
kGlobalVariableVariableIndex,
|
|
|
|
{context()->get_debug_info_mgr()->GetDebugInfoNone()->result_id()});
|
|
|
|
context()->AnalyzeUses(&dbg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
to_kill_.push_back(&dbg);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Since ADCE is disabled for non-shaders, we don't check for export linkage
|
|
|
|
// attributes here.
|
|
|
|
for (auto& val : get_module()->types_values()) {
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (!IsLive(&val)) {
|
2019-08-08 13:45:59 +00:00
|
|
|
// Save forwarded pointer if pointer is live since closure does not mark
|
|
|
|
// this live as it does not have a result id. This is a little too
|
|
|
|
// conservative since it is not known if the structure type that needed
|
|
|
|
// it is still live. TODO(greg-lunarg): Only save if needed.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (val.opcode() == spv::Op::OpTypeForwardPointer) {
|
2019-08-08 13:45:59 +00:00
|
|
|
uint32_t ptr_ty_id = val.GetSingleWordInOperand(0);
|
|
|
|
Instruction* ptr_ty_inst = get_def_use_mgr()->GetDef(ptr_ty_id);
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (IsLive(ptr_ty_inst)) continue;
|
2019-08-08 13:45:59 +00:00
|
|
|
}
|
2017-12-18 17:13:10 +00:00
|
|
|
to_kill_.push_back(&val);
|
2019-08-30 20:27:22 +00:00
|
|
|
modified = true;
|
2017-12-18 17:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:54:17 +00:00
|
|
|
if (!preserve_interface_) {
|
2019-05-07 18:52:22 +00:00
|
|
|
// Remove the dead interface variables from the entry point interface list.
|
|
|
|
for (auto& entry : get_module()->entry_points()) {
|
|
|
|
std::vector<Operand> new_operands;
|
|
|
|
for (uint32_t i = 0; i < entry.NumInOperands(); ++i) {
|
|
|
|
if (i < 3) {
|
|
|
|
// Execution model, function id and name are always valid.
|
|
|
|
new_operands.push_back(entry.GetInOperand(i));
|
|
|
|
} else {
|
|
|
|
auto* var =
|
|
|
|
get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
if (IsLive(var)) {
|
2019-05-07 18:52:22 +00:00
|
|
|
new_operands.push_back(entry.GetInOperand(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_operands.size() != entry.NumInOperands()) {
|
|
|
|
entry.SetInOperands(std::move(new_operands));
|
|
|
|
get_def_use_mgr()->UpdateDefUse(&entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status AggressiveDCEPass::Process() {
|
2020-06-15 17:20:40 +00:00
|
|
|
// Initialize extensions allowlist
|
2018-07-12 13:08:45 +00:00
|
|
|
InitExtensions();
|
2017-06-08 16:37:21 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
void AggressiveDCEPass::InitExtensions() {
|
2020-06-15 17:20:40 +00:00
|
|
|
extensions_allowlist_.clear();
|
2023-10-16 19:03:33 +00:00
|
|
|
|
|
|
|
// clang-format off
|
2020-06-15 17:20:40 +00:00
|
|
|
extensions_allowlist_.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",
|
2020-09-08 18:13:01 +00:00
|
|
|
"SPV_KHR_8bit_storage",
|
2017-11-11 01:26:55 +00:00
|
|
|
"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",
|
2018-03-08 13:54:00 +00:00
|
|
|
"SPV_EXT_shader_stencil_export",
|
|
|
|
"SPV_EXT_shader_viewport_index_layer",
|
|
|
|
"SPV_AMD_shader_image_load_store_lod",
|
|
|
|
"SPV_AMD_shader_fragment_mask",
|
|
|
|
"SPV_EXT_fragment_fully_covered",
|
|
|
|
"SPV_AMD_gpu_shader_half_float_fetch",
|
2018-03-08 20:33:28 +00:00
|
|
|
"SPV_GOOGLE_decorate_string",
|
|
|
|
"SPV_GOOGLE_hlsl_functionality1",
|
2019-06-19 16:18:13 +00:00
|
|
|
"SPV_GOOGLE_user_type",
|
2018-04-06 14:18:34 +00:00
|
|
|
"SPV_NV_shader_subgroup_partitioned",
|
2020-03-18 03:36:02 +00:00
|
|
|
"SPV_EXT_demote_to_helper_invocation",
|
2018-04-06 14:18:34 +00:00
|
|
|
"SPV_EXT_descriptor_indexing",
|
2018-09-19 18:53:33 +00:00
|
|
|
"SPV_NV_fragment_shader_barycentric",
|
|
|
|
"SPV_NV_compute_shader_derivatives",
|
|
|
|
"SPV_NV_shader_image_footprint",
|
|
|
|
"SPV_NV_shading_rate",
|
|
|
|
"SPV_NV_mesh_shader",
|
2024-01-30 17:13:46 +00:00
|
|
|
"SPV_EXT_mesh_shader",
|
2018-10-25 18:07:46 +00:00
|
|
|
"SPV_NV_ray_tracing",
|
2020-03-23 14:31:05 +00:00
|
|
|
"SPV_KHR_ray_tracing",
|
2020-12-02 23:46:19 +00:00
|
|
|
"SPV_KHR_ray_query",
|
2018-11-23 15:21:19 +00:00
|
|
|
"SPV_EXT_fragment_invocation_density",
|
2019-08-08 13:45:59 +00:00
|
|
|
"SPV_EXT_physical_storage_buffer",
|
2023-09-06 16:35:57 +00:00
|
|
|
"SPV_KHR_physical_storage_buffer",
|
2020-07-22 15:45:02 +00:00
|
|
|
"SPV_KHR_terminate_invocation",
|
2020-12-07 19:42:25 +00:00
|
|
|
"SPV_KHR_shader_clock",
|
2021-06-10 12:36:59 +00:00
|
|
|
"SPV_KHR_vulkan_memory_model",
|
2021-06-15 14:07:42 +00:00
|
|
|
"SPV_KHR_subgroup_uniform_control_flow",
|
2021-06-23 17:32:24 +00:00
|
|
|
"SPV_KHR_integer_dot_product",
|
2021-07-14 12:43:35 +00:00
|
|
|
"SPV_EXT_shader_image_int64",
|
2021-09-15 18:38:53 +00:00
|
|
|
"SPV_KHR_non_semantic_info",
|
2022-03-25 12:32:50 +00:00
|
|
|
"SPV_KHR_uniform_group_instructions",
|
2022-05-25 13:20:39 +00:00
|
|
|
"SPV_KHR_fragment_shader_barycentric",
|
2023-05-24 15:01:11 +00:00
|
|
|
"SPV_NV_bindless_texture",
|
2023-07-27 23:04:50 +00:00
|
|
|
"SPV_EXT_shader_atomic_float_add",
|
2023-09-05 16:10:16 +00:00
|
|
|
"SPV_EXT_fragment_shader_interlock",
|
2024-06-26 12:00:29 +00:00
|
|
|
"SPV_NV_compute_shader_derivatives",
|
|
|
|
"SPV_NV_cooperative_matrix",
|
|
|
|
"SPV_KHR_cooperative_matrix"
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
2023-10-16 19:03:33 +00:00
|
|
|
// clang-format on
|
2017-07-19 00:57:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 17:21:45 +00:00
|
|
|
Instruction* AggressiveDCEPass::GetHeaderBranch(BasicBlock* blk) {
|
|
|
|
if (blk == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
BasicBlock* header_block = GetHeaderBlock(blk);
|
|
|
|
if (header_block == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return header_block->terminator();
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock* AggressiveDCEPass::GetHeaderBlock(BasicBlock* blk) const {
|
|
|
|
if (blk == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock* header_block = nullptr;
|
|
|
|
if (blk->IsLoopHeader()) {
|
|
|
|
header_block = blk;
|
|
|
|
} else {
|
|
|
|
uint32_t header =
|
|
|
|
context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
|
|
|
|
header_block = context()->get_instr_block(header);
|
|
|
|
}
|
|
|
|
return header_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* AggressiveDCEPass::GetMergeInstruction(Instruction* inst) {
|
|
|
|
BasicBlock* bb = context()->get_instr_block(inst);
|
|
|
|
if (bb == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return bb->GetMergeInst();
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* AggressiveDCEPass::GetBranchForNextHeader(BasicBlock* blk) {
|
|
|
|
if (blk == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blk->IsLoopHeader()) {
|
|
|
|
uint32_t header =
|
|
|
|
context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
|
|
|
|
blk = context()->get_instr_block(header);
|
|
|
|
}
|
|
|
|
return GetHeaderBranch(blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::MarkFunctionParameterAsLive(const Function* func) {
|
|
|
|
func->ForEachParam(
|
|
|
|
[this](const Instruction* param) {
|
|
|
|
AddToWorklist(const_cast<Instruction*>(param));
|
|
|
|
},
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AggressiveDCEPass::BlockIsInConstruct(BasicBlock* header_block,
|
|
|
|
BasicBlock* bb) {
|
|
|
|
if (bb == nullptr || header_block == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t current_header = bb->id();
|
|
|
|
while (current_header != 0) {
|
|
|
|
if (current_header == header_block->id()) return true;
|
|
|
|
current_header = context()->GetStructuredCFGAnalysis()->ContainingConstruct(
|
|
|
|
current_header);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:24:29 +00:00
|
|
|
bool AggressiveDCEPass::IsEntryPointWithNoCalls(Function* func) {
|
|
|
|
auto cached_result = entry_point_with_no_calls_cache_.find(func->result_id());
|
|
|
|
if (cached_result != entry_point_with_no_calls_cache_.end()) {
|
|
|
|
return cached_result->second;
|
|
|
|
}
|
|
|
|
bool result = IsEntryPoint(func) && !HasCall(func);
|
|
|
|
entry_point_with_no_calls_cache_[func->result_id()] = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AggressiveDCEPass::IsEntryPoint(Function* func) {
|
|
|
|
for (const Instruction& entry_point : get_module()->entry_points()) {
|
|
|
|
uint32_t entry_point_id =
|
|
|
|
entry_point.GetSingleWordInOperand(kEntryPointFunctionIdInIdx);
|
|
|
|
if (entry_point_id == func->result_id()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AggressiveDCEPass::HasCall(Function* func) {
|
2022-11-04 21:27:10 +00:00
|
|
|
return !func->WhileEachInst([](Instruction* inst) {
|
|
|
|
return inst->opcode() != spv::Op::OpFunctionCall;
|
|
|
|
});
|
2021-10-26 17:24:29 +00:00
|
|
|
}
|
|
|
|
|
Change branch handling in ADCE to fix errors (#4596)
Consider the new test case. The conditional branch in the continue
block is never marked as live. However, `IsDead` will say it is not
dead, so it does not get deleted. Because it was never marked as live,
`%false` was not mark as live either, but it gets deleted. This results
in invalid code.
To fix this properly, we had to reconsider how branches are handle. We
make the following changes:
1) Terminator instructions that are not branch or OpUnreachable must be
kept, so they are marked as live when initializing the worklist.
2) Branches and OpUnreachable instructions are marked as live if
a) the block does not have a merge instruction and another instruction
in the block is marked as live, or
b) the merge instruction in the same block is marked as live.
3) Any instruction that is not marked as live is removed.
4) If a terminator is to be removed, an OpUnreachable is added. This
happens when the entire block is dead, and the block will be removed.
The OpUnreachable is generated to make sure the block still has a
terminator, and is valid.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/4509.
2021-10-29 14:46:43 +00:00
|
|
|
void AggressiveDCEPass::MarkFirstBlockAsLive(Function* func) {
|
|
|
|
BasicBlock* first_block = &*func->begin();
|
|
|
|
MarkBlockAsLive(first_block->GetLabelInst());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDCEPass::AddUnreachable(BasicBlock*& block) {
|
|
|
|
InstructionBuilder builder(
|
|
|
|
context(), block,
|
|
|
|
IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
|
|
|
|
builder.AddUnreachable();
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|