2016-05-22 18:11:24 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +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
|
2016-05-22 18:11:24 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-22 18:11:24 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/function.h"
|
2016-05-22 18:11:24 +00:00
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2019-09-19 14:26:24 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "ir_context.h"
|
|
|
|
#include "source/util/bit_vector.h"
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
Function* Function::Clone(IRContext* ctx) const {
|
2017-11-14 19:11:50 +00:00
|
|
|
Function* clone =
|
2018-03-06 16:20:28 +00:00
|
|
|
new Function(std::unique_ptr<Instruction>(DefInst().Clone(ctx)));
|
2017-11-14 19:11:50 +00:00
|
|
|
clone->params_.reserve(params_.size());
|
|
|
|
ForEachParam(
|
2018-03-06 16:20:28 +00:00
|
|
|
[clone, ctx](const Instruction* inst) {
|
|
|
|
clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(ctx)));
|
2017-07-13 00:16:51 +00:00
|
|
|
},
|
|
|
|
true);
|
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
for (const auto& i : debug_insts_in_header_) {
|
|
|
|
clone->AddDebugInstructionInHeader(
|
|
|
|
std::unique_ptr<Instruction>(i.Clone(ctx)));
|
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
clone->blocks_.reserve(blocks_.size());
|
|
|
|
for (const auto& b : blocks_) {
|
2018-03-06 16:20:28 +00:00
|
|
|
std::unique_ptr<BasicBlock> bb(b->Clone(ctx));
|
2017-11-14 19:11:50 +00:00
|
|
|
clone->AddBasicBlock(std::move(bb));
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
clone->SetFunctionEnd(std::unique_ptr<Instruction>(EndInst()->Clone(ctx)));
|
2020-08-13 18:54:14 +00:00
|
|
|
|
|
|
|
clone->non_semantic_.reserve(non_semantic_.size());
|
|
|
|
for (auto& non_semantic : non_semantic_) {
|
|
|
|
clone->AddNonSemanticInstruction(
|
|
|
|
std::unique_ptr<Instruction>(non_semantic->Clone(ctx)));
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
return clone;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
void Function::ForEachInst(const std::function<void(Instruction*)>& f,
|
2020-08-13 18:54:14 +00:00
|
|
|
bool run_on_debug_line_insts,
|
|
|
|
bool run_on_non_semantic_insts) {
|
2018-12-18 19:34:03 +00:00
|
|
|
WhileEachInst(
|
|
|
|
[&f](Instruction* inst) {
|
|
|
|
f(inst);
|
|
|
|
return true;
|
|
|
|
},
|
2020-08-13 18:54:14 +00:00
|
|
|
run_on_debug_line_insts, run_on_non_semantic_insts);
|
2016-05-22 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
|
2020-08-13 18:54:14 +00:00
|
|
|
bool run_on_debug_line_insts,
|
|
|
|
bool run_on_non_semantic_insts) const {
|
2018-12-18 19:34:03 +00:00
|
|
|
WhileEachInst(
|
|
|
|
[&f](const Instruction* inst) {
|
|
|
|
f(inst);
|
|
|
|
return true;
|
|
|
|
},
|
2020-08-13 18:54:14 +00:00
|
|
|
run_on_debug_line_insts, run_on_non_semantic_insts);
|
2018-12-18 19:34:03 +00:00
|
|
|
}
|
2016-08-20 13:47:00 +00:00
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f,
|
2020-08-13 18:54:14 +00:00
|
|
|
bool run_on_debug_line_insts,
|
|
|
|
bool run_on_non_semantic_insts) {
|
2018-12-18 19:34:03 +00:00
|
|
|
if (def_inst_) {
|
|
|
|
if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& param : params_) {
|
|
|
|
if (!param->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:29:36 +00:00
|
|
|
if (!debug_insts_in_header_.empty()) {
|
|
|
|
Instruction* di = &debug_insts_in_header_.front();
|
|
|
|
while (di != nullptr) {
|
|
|
|
Instruction* next_instruction = di->NextNode();
|
|
|
|
if (!di->WhileEachInst(f, run_on_debug_line_insts)) return false;
|
|
|
|
di = next_instruction;
|
2020-03-23 15:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
for (auto& bb : blocks_) {
|
|
|
|
if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:54:14 +00:00
|
|
|
if (end_inst_) {
|
|
|
|
if (!end_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (run_on_non_semantic_insts) {
|
|
|
|
for (auto& non_semantic : non_semantic_) {
|
|
|
|
if (!non_semantic->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 19:34:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
|
2020-08-13 18:54:14 +00:00
|
|
|
bool run_on_debug_line_insts,
|
|
|
|
bool run_on_non_semantic_insts) const {
|
2018-12-18 19:34:03 +00:00
|
|
|
if (def_inst_) {
|
|
|
|
if (!static_cast<const Instruction*>(def_inst_.get())
|
|
|
|
->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-20 13:47:00 +00:00
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
for (const auto& param : params_) {
|
|
|
|
if (!static_cast<const Instruction*>(param.get())
|
|
|
|
->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:18 +00:00
|
|
|
for (const auto& di : debug_insts_in_header_) {
|
2020-04-13 13:29:36 +00:00
|
|
|
if (!static_cast<const Instruction*>(&di)->WhileEachInst(
|
|
|
|
f, run_on_debug_line_insts))
|
2020-03-23 15:01:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:34:03 +00:00
|
|
|
for (const auto& bb : blocks_) {
|
|
|
|
if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
|
|
|
|
f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-20 13:47:00 +00:00
|
|
|
|
2020-08-13 18:54:14 +00:00
|
|
|
if (end_inst_) {
|
|
|
|
if (!static_cast<const Instruction*>(end_inst_.get())
|
|
|
|
->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (run_on_non_semantic_insts) {
|
|
|
|
for (auto& non_semantic : non_semantic_) {
|
|
|
|
if (!static_cast<const Instruction*>(non_semantic.get())
|
|
|
|
->WhileEachInst(f, run_on_debug_line_insts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 19:34:03 +00:00
|
|
|
|
|
|
|
return true;
|
2016-05-22 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:06:17 +00:00
|
|
|
void Function::ForEachParam(const std::function<void(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts) {
|
|
|
|
for (auto& param : params_)
|
|
|
|
static_cast<Instruction*>(param.get())
|
|
|
|
->ForEachInst(f, run_on_debug_line_insts);
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts) const {
|
|
|
|
for (const auto& param : params_)
|
|
|
|
static_cast<const Instruction*>(param.get())
|
|
|
|
->ForEachInst(f, run_on_debug_line_insts);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:09:43 +00:00
|
|
|
void Function::ForEachDebugInstructionsInHeader(
|
|
|
|
const std::function<void(Instruction*)>& f) {
|
|
|
|
if (debug_insts_in_header_.empty()) return;
|
|
|
|
|
|
|
|
Instruction* di = &debug_insts_in_header_.front();
|
|
|
|
while (di != nullptr) {
|
|
|
|
Instruction* next_instruction = di->NextNode();
|
|
|
|
di->ForEachInst(f);
|
|
|
|
di = next_instruction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
BasicBlock* Function::InsertBasicBlockAfter(
|
|
|
|
std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
|
|
|
|
for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
|
|
|
|
if (&*bb_iter == position) {
|
|
|
|
new_block->SetParent(this);
|
|
|
|
++bb_iter;
|
|
|
|
bb_iter = bb_iter.InsertBefore(std::move(new_block));
|
|
|
|
return &*bb_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(false && "Could not find insertion point.");
|
2019-05-09 16:56:10 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock* Function::InsertBasicBlockBefore(
|
|
|
|
std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
|
|
|
|
for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
|
|
|
|
if (&*bb_iter == position) {
|
|
|
|
new_block->SetParent(this);
|
|
|
|
bb_iter = bb_iter.InsertBefore(std::move(new_block));
|
|
|
|
return &*bb_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(false && "Could not find insertion point.");
|
2018-03-06 16:20:28 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:31:24 +00:00
|
|
|
bool Function::HasEarlyReturn() const {
|
|
|
|
auto post_dominator_analysis =
|
|
|
|
blocks_.front()->GetLabel()->context()->GetPostDominatorAnalysis(this);
|
|
|
|
for (auto& block : blocks_) {
|
|
|
|
if (spvOpcodeIsReturn(block->tail()->opcode()) &&
|
|
|
|
!post_dominator_analysis->Dominates(block.get(), entry().get())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
bool Function::IsRecursive() const {
|
|
|
|
IRContext* ctx = blocks_.front()->GetLabel()->context();
|
|
|
|
IRContext::ProcessFunction mark_visited = [this](Function* fp) {
|
|
|
|
return fp == this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process the call tree from all of the function called by |this|. If it get
|
|
|
|
// back to |this|, then we have a recursive function.
|
|
|
|
std::queue<uint32_t> roots;
|
|
|
|
ctx->AddCalls(this, &roots);
|
|
|
|
return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
std::ostream& operator<<(std::ostream& str, const Function& func) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
str << func.PrettyPrint();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:57:12 +00:00
|
|
|
void Function::Dump() const {
|
|
|
|
std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
|
|
|
|
}
|
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
std::string Function::PrettyPrint(uint32_t options) const {
|
|
|
|
std::ostringstream str;
|
2018-07-12 19:14:43 +00:00
|
|
|
ForEachInst([&str, options](const Instruction* inst) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
str << inst->PrettyPrint(options);
|
2018-01-10 19:23:47 +00:00
|
|
|
if (inst->opcode() != SpvOpFunctionEnd) {
|
|
|
|
str << std::endl;
|
|
|
|
}
|
|
|
|
});
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
return str.str();
|
2018-01-10 19:23:47 +00:00
|
|
|
}
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2016-05-22 18:11:24 +00:00
|
|
|
} // namespace spvtools
|