text
stringlengths
0
2.2M
}
}
void buildBailOutIf(
const at::ArrayRef<Value*> block_outputs,
Node* outer_node) {
auto if_outputs = outer_node->outputs();
mapValues(block_outputs, if_outputs);
buildBailOutBlockFrom(outer_node->next());
}
std::shared_ptr<Graph> buildBailOutGraphFrom(Node* n) {
// add graph inputs for guard's input
// and loop counts for loops `n` is contained in
// to make sure we can line bailout grap's inputs up properly
// with arguments to this BailOut node.
for (auto bi : n->inputs()) {
getOrAddInputForValue(bi);
}
buildBailOutBlockFrom(n);
// add graph outputs
for (auto ov : graph_->outputs()) {
copy_graph_->registerOutput(getOrAddInputForValue(ov));
}
return copy_graph_;
}
std::shared_ptr<Graph> graph_;
std::shared_ptr<Graph> copy_graph_;
std::vector<Value*> live_inputs_;
std::unordered_map<Value*, Value*> old_to_new_;
};
// `BailOutInserter` replaces prim::Guard nodes with
// prim::BailOut nodes that allow interpreter to
// resume execution of the unoptimized(deoptimized)
// version of an original graph from a particular point
struct BailOutInserter {
explicit BailOutInserter(std::shared_ptr<Graph> graph)
: graph_(std::move(graph)), bailout_index_(0) {}
void run() {
liveness_sets_ = BuildLivenessSets(graph_);
insertBailOuts(graph_->block());
replaceGuardsWithBailouts();
// embed a full original graph
addUnoptimizedFuncToBailouts();
}
// Packs the original unoptimized graph into a Function constant
// and add it as the first input to every prim::BailOut point
// This graph will be used to compute a bailout graph for
// any given bailout point
void addUnoptimizedFuncToBailouts() {
auto unoptimized_graph = graph_->copy();
auto unopt_func = graph_->create(prim::BailoutTemplate)
->insertAfter(graph_->param_node());
// Returns an int so that we have an easy way to do graph traversal
unopt_func->output()->setType(IntType::get());
unopt_func->g_(attr::Subgraph, unoptimized_graph);
for (auto bn : bailouts_) {
bn->insertInput(0, unopt_func->output());
}
}
// Removes guards by hooking up the guarded tensor
// directly to its users and also clears
// profiling information on it.
void removeGuards(Block* b) {
for (auto it = b->nodes().begin(); it != b->nodes().end(); ++it) {
if (it->kind() == prim::Guard) {
// this will need to be profiled again
it->input()->setType(TensorType::get());
// destroy the guard
it->output()->replaceAllUsesWith(it->input());
it.destroyCurrent();
}
for (auto ib : it->blocks()) {
removeGuards(ib);
}
}
}
// replace each prim::Guard
// with its corresponding prim::BailOut
void replaceGuardsWithBailouts() {
for (auto e : replacements_) {
e.first->replaceAllUsesWith(e.second);
e.second->node()->insertAfter(e.first->node());
e.first->node()->destroy();
}
}
// Inserts prim::BailOut nodes for every prim::Guard
// Each BailOut point takes the set of inputs live
// at that particular execution point.
// An input is live if it's used beyond the guard/BailOut