59 return procedure.
entry();
79 name_ = std::move(
name);
84 if (next_ !=
nullptr) {
85 next_->prev_.remove_first_occurrence_and_reorder(*
this);
87 if (instruction !=
nullptr) {
88 instruction->
prev_.append(*
this);
95 if (params_[param_index] !=
nullptr) {
96 params_[param_index]->users_.remove_first_occurrence_and_reorder(
this);
98 if (variable !=
nullptr) {
100 const ParamType param_type = fn_->param_type(param_index);
103 variable->users_.append(
this);
105 params_[param_index] = variable;
118 if (condition_ !=
nullptr) {
119 condition_->users_.remove_first_occurrence_and_reorder(
this);
121 if (variable !=
nullptr) {
122 variable->users_.append(
this);
124 condition_ = variable;
129 if (branch_true_ !=
nullptr) {
130 branch_true_->prev_.remove_first_occurrence_and_reorder({*
this,
true});
132 if (instruction !=
nullptr) {
133 instruction->
prev_.append({*
this,
true});
135 branch_true_ = instruction;
140 if (branch_false_ !=
nullptr) {
141 branch_false_->prev_.remove_first_occurrence_and_reorder({*
this,
false});
143 if (instruction !=
nullptr) {
144 instruction->
prev_.append({*
this,
false});
146 branch_false_ = instruction;
151 if (variable_ !=
nullptr) {
152 variable_->users_.remove_first_occurrence_and_reorder(
this);
162 if (next_ !=
nullptr) {
163 next_->prev_.remove_first_occurrence_and_reorder(*
this);
165 if (instruction !=
nullptr) {
166 instruction->
prev_.append(*
this);
173 if (next_ !=
nullptr) {
174 next_->prev_.remove_first_occurrence_and_reorder(*
this);
176 if (instruction !=
nullptr) {
177 instruction->
prev_.append(*
this);
185 variable.name_ = std::move(name);
186 variable.data_type_ = data_type;
187 variable.index_in_graph_ = variables_.size();
188 variables_.append(&variable);
196 instruction.fn_ = &
fn;
197 instruction.params_ = allocator_.allocate_array<
Variable *>(
fn.param_amount());
198 instruction.params_.fill(
nullptr);
199 call_instructions_.append(&instruction);
207 branch_instructions_.append(&instruction);
215 destruct_instructions_.append(&instruction);
223 dummy_instructions_.append(&instruction);
231 return_instructions_.append(&instruction);
237 params_.append({interface_type, &variable});
242 if (entry_ !=
nullptr) {
252 instruction->~CallInstruction();
255 instruction->~BranchInstruction();
258 instruction->~DestructInstruction();
261 instruction->~DummyInstruction();
264 instruction->~ReturnInstruction();
266 for (
Variable *variable : variables_) {
267 variable->~Variable();
273 if (entry_ ==
nullptr) {
276 if (!this->validate_all_instruction_pointers_set()) {
279 if (!this->validate_all_params_provided()) {
282 if (!this->validate_same_variables_in_one_call()) {
285 if (!this->validate_parameters()) {
288 if (!this->validate_initialization()) {
297 instruction->fn().prepare_for_execution();
301bool Procedure::validate_all_instruction_pointers_set()
const
304 if (instruction->next_ ==
nullptr) {
308 for (
const DestructInstruction *instruction : destruct_instructions_) {
309 if (instruction->next_ ==
nullptr) {
313 for (
const BranchInstruction *instruction : branch_instructions_) {
314 if (instruction->branch_true_ ==
nullptr) {
317 if (instruction->branch_false_ ==
nullptr) {
321 for (
const DummyInstruction *instruction : dummy_instructions_) {
322 if (instruction->next_ ==
nullptr) {
329bool Procedure::validate_all_params_provided()
const
331 for (
const CallInstruction *instruction : call_instructions_) {
332 const MultiFunction &fn = instruction->fn();
333 for (
const int param_index : fn.param_indices()) {
334 const ParamType param_type = fn.param_type(param_index);
339 const Variable *variable = instruction->params_[param_index];
340 if (variable ==
nullptr) {
345 for (
const BranchInstruction *instruction : branch_instructions_) {
346 if (instruction->condition_ ==
nullptr) {
350 for (
const DestructInstruction *instruction : destruct_instructions_) {
351 if (instruction->variable_ ==
nullptr) {
358bool Procedure::validate_same_variables_in_one_call()
const
360 for (
const CallInstruction *instruction : call_instructions_) {
361 const MultiFunction &fn = *instruction->fn_;
362 for (
const int param_index : fn.param_indices()) {
363 const ParamType param_type = fn.param_type(param_index);
364 const Variable *variable = instruction->params_[param_index];
365 if (variable ==
nullptr) {
368 for (
const int other_param_index : fn.param_indices()) {
369 if (other_param_index == param_index) {
372 const Variable *other_variable = instruction->params_[other_param_index];
373 if (other_variable != variable) {
380 const ParamType other_param_type = fn.param_type(other_param_index);
391bool Procedure::validate_parameters()
const
394 for (
const Parameter ¶m : params_) {
403bool Procedure::validate_initialization()
const
406 for (
const DestructInstruction *instruction : destruct_instructions_) {
407 const Variable &variable = *instruction->variable_;
408 const InitState
state = this->find_initialization_state_before_instruction(*instruction,
410 if (!
state.can_be_initialized) {
414 for (
const BranchInstruction *instruction : branch_instructions_) {
415 const Variable &variable = *instruction->condition_;
416 const InitState
state = this->find_initialization_state_before_instruction(*instruction,
418 if (!
state.can_be_initialized) {
422 for (
const CallInstruction *instruction : call_instructions_) {
423 const MultiFunction &fn = *instruction->fn_;
424 for (
const int param_index : fn.param_indices()) {
425 const ParamType param_type = fn.param_type(param_index);
427 if (!instruction->params_[param_index]) {
430 const Variable &variable = *instruction->params_[param_index];
431 const InitState
state = this->find_initialization_state_before_instruction(*instruction,
433 switch (param_type.interface_type()) {
436 if (!
state.can_be_initialized) {
442 if (!
state.can_be_uninitialized) {
450 Set<const Variable *> variables_that_should_be_initialized_on_return;
451 for (
const Parameter ¶m : params_) {
453 variables_that_should_be_initialized_on_return.add_new(param.variable);
456 for (
const ReturnInstruction *instruction : return_instructions_) {
457 for (
const Variable *variable : variables_) {
458 const InitState init_state = this->find_initialization_state_before_instruction(*instruction,
460 if (variables_that_should_be_initialized_on_return.contains(variable)) {
461 if (!init_state.can_be_initialized) {
466 if (!init_state.can_be_uninitialized) {
475Procedure::InitState Procedure::find_initialization_state_before_instruction(
480 auto check_entry_instruction = [&]() {
481 bool caller_initialized_variable =
false;
482 for (
const Parameter ¶m : params_) {
483 if (param.variable == &target_variable) {
485 caller_initialized_variable =
true;
490 if (caller_initialized_variable) {
491 state.can_be_initialized =
true;
494 state.can_be_uninitialized =
true;
498 if (&target_instruction == entry_) {
499 check_entry_instruction();
502 Set<const Instruction *> checked_instructions;
503 Stack<const Instruction *> instructions_to_check;
504 for (
const InstructionCursor &cursor : target_instruction.prev_) {
505 if (cursor.instruction() !=
nullptr) {
506 instructions_to_check.push(cursor.instruction());
510 while (!instructions_to_check.is_empty()) {
511 const Instruction &instruction = *instructions_to_check.pop();
512 if (!checked_instructions.add(&instruction)) {
516 bool state_modified =
false;
517 switch (instruction.type_) {
519 const CallInstruction &call_instruction =
static_cast<const CallInstruction &
>(
521 const MultiFunction &fn = *call_instruction.fn_;
522 for (
const int param_index : fn.param_indices()) {
523 if (call_instruction.params_[param_index] == &target_variable) {
524 const ParamType param_type = fn.param_type(param_index);
526 state.can_be_initialized =
true;
527 state_modified =
true;
535 const DestructInstruction &destruct_instruction =
static_cast<const DestructInstruction &
>(
537 if (destruct_instruction.variable_ == &target_variable) {
538 state.can_be_uninitialized =
true;
539 state_modified =
true;
551 if (!state_modified) {
552 if (&instruction == entry_) {
553 check_entry_instruction();
555 for (
const InstructionCursor &cursor : instruction.prev_) {
556 if (cursor.instruction() !=
nullptr) {
557 instructions_to_check.push(cursor.instruction());
580 return digraph_.to_dot_string();
586 auto add_instructions = [&](
auto instructions) {
587 all_instructions.
extend(instructions.begin(), instructions.end());
589 add_instructions(procedure_.call_instructions_);
590 add_instructions(procedure_.branch_instructions_);
591 add_instructions(procedure_.destruct_instructions_);
592 add_instructions(procedure_.dummy_instructions_);
593 add_instructions(procedure_.return_instructions_);
597 for (
const Instruction *representative : all_instructions) {
598 if (handled_instructions.
contains(representative)) {
603 std::stringstream ss;
606 for (
const Instruction *current : block_instructions) {
607 handled_instructions.
add_new(current);
608 switch (current->type()) {
630 ss << R
"(<br align="left" />)";
636 dot_nodes_by_begin_.add_new(block_instructions.
first(), &dot_node);
637 dot_nodes_by_end_.add_new(block_instructions.
last(), &dot_node);
645 if (to_instruction ==
nullptr) {
648 return digraph_.new_edge(from_node, to_node);
651 return digraph_.new_edge(from_node, to_node);
654 for (
auto item : dot_nodes_by_end_.items()) {
657 switch (from_instruction.
type()) {
661 create_edge(from_node, to_instruction);
667 create_edge(from_node, to_instruction);
673 create_edge(from_node, to_instruction);
684 create_edge(from_node, to_true_instruction).attributes.set(
"color",
"#118811");
685 create_edge(from_node, to_false_instruction).attributes.set(
"color",
"#881111");
692 create_edge(entry_node, procedure_.entry());
697 if (instruction.
prev().size() != 1) {
700 if (
ELEM(instruction.
prev()[0].type(),
713 current = current->
prev()[0].instruction();
714 if (current == &representative) {
726 switch (instruction.
type()) {
744 if (next ==
nullptr) {
747 if (next == &block_begin) {
763 instructions.
append(current);
770 if (variable ==
nullptr) {
776 ss <<
"(" << variable->
name() <<
")";
790 for (
const int param_index :
fn.param_indices()) {
791 const ParamType param_type =
fn.param_type(param_index);
793 ss << R
"(<font color="grey30">)";
810 if (param_index <
fn.param_amount() - 1) {
834 outgoing_parameters.
append(param);
837 for (
const int param_index : outgoing_parameters.
index_range()) {
840 if (param_index < outgoing_parameters.
size() - 1) {
854 std::stringstream ss;
859 incoming_parameters.
append(param);
862 for (
const int param_index : incoming_parameters.
index_range()) {
865 if (param_index < incoming_parameters.
size() - 1) {
bool contains(const Key &key) const
void add_new(const Key &key)
constexpr int64_t size() const
constexpr IndexRange index_range() const
void append(const T &value)
const T & last(const int64_t n=0) const
IndexRange index_range() const
void extend(Span< T > array)
constexpr bool is_empty() const
void set_shape(Attr_shape shape)
void set_branch_true(Instruction *instruction)
Instruction * branch_false()
Instruction * branch_true()
void set_condition(Variable *variable)
void set_branch_false(Instruction *instruction)
Span< Variable * > params()
void set_param_variable(int param_index, Variable *variable)
const MultiFunction & fn() const
void set_params(Span< Variable * > variables)
void set_next(Instruction *instruction)
void set_variable(Variable *variable)
void set_next(Instruction *instruction)
void set_next(Instruction *instruction)
static InstructionCursor ForEntry()
void set_next(Procedure &procedure, Instruction *new_instruction) const
Instruction * next(Procedure &procedure) const
Span< InstructionCursor > prev() const
Vector< InstructionCursor > prev_
InstructionType type() const
const DataType & data_type() const
InterfaceType interface_type() const
dot_export::Node & create_entry_node()
void instruction_to_string(const DestructInstruction &instruction, std::stringstream &ss)
void variable_to_string(const Variable *variable, std::stringstream &ss)
void instruction_to_string(const BranchInstruction &instruction, std::stringstream &ss)
const Instruction & get_first_instruction_in_block(const Instruction &representative)
const Instruction * get_next_instruction_in_block(const Instruction &instruction, const Instruction &block_begin)
void instruction_to_string(const ReturnInstruction &, std::stringstream &ss)
void instruction_to_string(const CallInstruction &instruction, std::stringstream &ss)
void instruction_to_string(const DummyInstruction &, std::stringstream &ss)
ProcedureDotExport(const Procedure &procedure)
Vector< const Instruction * > get_instructions_in_block(const Instruction &representative)
void instruction_name_format(StringRef name, std::stringstream &ss)
bool has_to_be_block_begin(const Instruction &instruction)
BranchInstruction & new_branch_instruction()
std::string to_dot() const
CallInstruction & new_call_instruction(const MultiFunction &fn)
DestructInstruction & new_destruct_instruction()
Span< Variable * > variables()
void set_entry(Instruction &entry)
ReturnInstruction & new_return_instruction()
friend class ProcedureDotExport
void prepare_for_execution()
DummyInstruction & new_dummy_instruction()
void add_parameter(ParamType::InterfaceType interface_type, Variable &variable)
Variable & new_variable(DataType data_type, std::string name="")
void set_name(std::string name)
int index_in_procedure() const
StringRefNull name() const
DataType data_type() const
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const Variable * variable