text
stringlengths 0
2.2M
|
---|
for (auto& v : true_vars->definedVariables()) {
|
if (false_vars->findInAnyFrame(v)) {
|
mutated_variables.insert(v);
|
}
|
}
|
for (auto& v : false_vars->definedVariables()) {
|
if (true_vars->findInAnyFrame(v)) {
|
mutated_variables.insert(v);
|
}
|
}
|
// Following the same logic as emitIfElseBlocks in ir_emitter.cpp,
|
// we emit a node output if the variable is defined in each block
|
// and the types of each block can be unified
|
for (const auto& x : mutated_variables) {
|
auto true_type = true_vars->findInAnyFrame(x);
|
auto false_type = false_vars->findInAnyFrame(x);
|
auto unified =
|
unifyTypes(true_type, false_type, /*default_to_union=*/true);
|
addBlockOutput(true_block, true_type, x);
|
addBlockOutput(false_block, false_type, x);
|
addNodeOutput(n, *unified, x);
|
}
|
}
|
// loop_carried_outputs* = Loop(max_trip_count, start_condition,
|
// loop_carried_inputs*)
|
// block0(loop_counter, loop_carried_block*) {
|
// <body>
|
// -> (continue_condition, loop_carried_block_outputs*)
|
// }
|
// all loop_carried_... lists are the same length and represent the value of
|
// loop-carried variables whose definitions are updated as the loop executes
|
// in a way that ensure single static assignment.
|
void addLoopLoadStores(Node* n) {
|
auto body_block = n->blocks().at(0);
|
auto loop_vars = addControlFlowLoadStores(body_block);
|
for (const auto& name : loop_vars->definedVariables()) {
|
// if the variable local to the loop body, then
|
// we do not need a loop carried variable for it
|
auto parent_type = environment_stack->findInAnyFrame(name);
|
if (!parent_type) {
|
continue;
|
}
|
// since the loop may execute 0 or many times, the output types
|
// of the loop and the input loop carried dependencies are conservatively
|
// the union of the output of the body and the input to the loop
|
auto block_type = loop_vars->findInThisFrame(name);
|
auto unified_type = unifyTypes(parent_type, block_type).value();
|
// Insert a store at the beginning of the loop block, so that all
|
// loads of the variable will use the loop carried value
|
addNodeInput(n, parent_type, name);
|
addBlockInput(body_block, unified_type, name);
|
addBlockOutput(body_block, block_type, name);
|
addNodeOutput(n, unified_type, name);
|
}
|
}
|
std::shared_ptr<TypeEnvironment> addControlFlowLoadStores(Block* block) {
|
pushFrame(block);
|
for (Node* n : block->nodes()) {
|
switch (n->kind()) {
|
case prim::If: {
|
addIfLoadStores(n);
|
} break;
|
case prim::Loop: {
|
addLoopLoadStores(n);
|
} break;
|
case prim::Closure: {
|
for (auto b : n->blocks()) {
|
addControlFlowLoadStores(b);
|
}
|
} break;
|
case prim::Store: {
|
environment_stack->setVar(n->s(attr::name), n->input()->type());
|
} break;
|
case prim::ComprehensionScope: {
|
addControlFlowLoadStores(n->blocks().at(0));
|
} break;
|
}
|
}
|
return popFrame();
|
}
|
void pushFrame(Block* b) {
|
environment_stack = std::make_shared<TypeEnvironment>(b, environment_stack);
|
}
|
std::shared_ptr<TypeEnvironment> popFrame() {
|
auto old_frame = environment_stack;
|
environment_stack = environment_stack->next;
|
return old_frame;
|
}
|
void run(std::shared_ptr<Graph>& graph) {
|
Subsets and Splits