Skip to content

Commit

Permalink
MIR - Rename If terminator's target members to avoid bugs (consteva…
Browse files Browse the repository at this point in the history
…l had true/false switched)
  • Loading branch information
thepowersgang committed Jan 2, 2024
1 parent 28ae543 commit fd059c1
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/hir/serialise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@
),
(If,
serialise(e.cond);
m_out.write_count(e.bb0);
m_out.write_count(e.bb1);
m_out.write_count(e.bb_true);
m_out.write_count(e.bb_false);
),
(Switch,
serialise(e.val);
Expand Down
2 changes: 1 addition & 1 deletion src/hir_conv/constant_evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ namespace HIR {
TU_ARMA(If, e) {
bool res = U128(0) != local_state.get_lval(e.cond).read_uint(state, 1);
DEBUG(state << " IF " << res);
return res ? e.bb1 : e.bb0;
return res ? e.bb_true : e.bb_false;
}
TU_ARMA(Switch, e) {
HIR::TypeRef tmp;
Expand Down
8 changes: 4 additions & 4 deletions src/mir/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ void MIR_Validate_ValState(::MIR::TypeResolve& state, const ::MIR::Function& fcn
TU_ARMA(If, e) {
// Push blocks
val_state.ensure_valid( state, e.cond );
add_to_visit_copy( e.bb0, path, val_state );
add_to_visit_move( e.bb1, mv$(path), mv$(val_state) );
add_to_visit_copy( e.bb_true , path, val_state );
add_to_visit_move( e.bb_false, mv$(path), mv$(val_state) );
}
TU_ARMA(Switch, e) {
val_state.ensure_valid( state, e.val );
Expand Down Expand Up @@ -684,8 +684,8 @@ void MIR_Validate(const StaticTraitResolve& resolve, const ::HIR::ItemPath& path
PUSH_BB(e.dst, "Panic");
),
(If,
PUSH_BB(e.bb0, "If true");
PUSH_BB(e.bb1, "If false");
PUSH_BB(e.bb_true , "If true");
PUSH_BB(e.bb_false, "If false");
),
(Switch,
for(unsigned int i = 0; i < e.targets.size(); i++ ) {
Expand Down
4 changes: 2 additions & 2 deletions src/mir/check_full.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,8 @@ void MIR_Validate_FullValState(::MIR::TypeResolve& mir_res, const ::MIR::Functio
),
(If,
state.ensure_lvalue_valid(mir_res, te.cond);
todo_queue.push_back( ::std::make_pair(te.bb0, state.clone()) );
todo_queue.push_back( ::std::make_pair(te.bb1, mv$(state)) );
todo_queue.push_back( ::std::make_pair(te.bb_true, state.clone()) );
todo_queue.push_back( ::std::make_pair(te.bb_false, mv$(state)) );
),
(Switch,
state.ensure_lvalue_valid(mir_res, te.val);
Expand Down
2 changes: 1 addition & 1 deletion src/mir/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace {
m_os << "panic bb" << e.dst << ";\n";
),
(If,
m_os << "if " << FMT_M(e.cond) << " { goto bb" << e.bb0 << "; } else { goto bb" << e.bb1 << "; }\n";
m_os << "if " << FMT_M(e.cond) << " { goto bb" << e.bb_true << "; } else { goto bb" << e.bb_false << "; }\n";
),
(Switch,
m_os << "switch " << FMT_M(e.val) << " {";
Expand Down
4 changes: 2 additions & 2 deletions src/mir/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,8 @@ void MIR_Helper_GetLifetimes_DetermineValueLifetime(
m_states_to_do.push_back( ::std::make_pair(te.dst, mv$(state)) );
}
TU_ARMA(If, te) {
m_states_to_do.push_back( ::std::make_pair(te.bb0, state.clone()) );
m_states_to_do.push_back( ::std::make_pair(te.bb1, mv$(state)) );
m_states_to_do.push_back( ::std::make_pair(te.bb_true, state.clone()) );
m_states_to_do.push_back( ::std::make_pair(te.bb_false, mv$(state)) );
}
TU_ARMA(Switch, te) {
for(size_t i = 0; i < te.targets.size(); i ++)
Expand Down
4 changes: 2 additions & 2 deletions src/mir/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ namespace visit {
}
TU_ARMA(If, e) {
rv |= visit_lvalue(e.cond, ValUsage::Read);
rv |= visit_block_id(e.bb0);
rv |= visit_block_id(e.bb1);
rv |= visit_block_id(e.bb_true);
rv |= visit_block_id(e.bb_false);
}
TU_ARMA(Switch, e) {
rv |= visit_lvalue(e.val, ValUsage::Read);
Expand Down
6 changes: 3 additions & 3 deletions src/mir/mir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ namespace MIR {
os << "Panic(" << e.dst << ";)";
),
(If,
os << "If( " << e.cond << " : " << e.bb0 << ", " << e.bb1 << ")";
os << "If( " << e.cond << " : " << e.bb_true << ", " << e.bb_false << ")";
),
(Switch,
os << "Switch( " << e.val << " : ";
Expand Down Expand Up @@ -498,9 +498,9 @@ namespace MIR {
(If,
if( ae.cond != be.cond )
return false;
if( ae.bb0 != be.bb0 )
if( ae.bb_true != be.bb_true )
return false;
if( ae.bb1 != be.bb1 )
if( ae.bb_false != be.bb_false )
return false;
),
(Switch,
Expand Down
4 changes: 2 additions & 2 deletions src/mir/mir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ TAGGED_UNION(Terminator, Incomplete,
(Panic, struct { BasicBlockId dst; }), // ?
(If, struct {
LValue cond;
BasicBlockId bb0; // true
BasicBlockId bb1; // false
BasicBlockId bb_true;
BasicBlockId bb_false;
}),
(Switch, struct {
LValue val;
Expand Down
24 changes: 12 additions & 12 deletions src/mir/optimise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ namespace
cb(e.dst);
}
TU_ARMA(If, e) {
cb(e.bb0);
cb(e.bb1);
cb(e.bb_true );
cb(e.bb_false);
}
TU_ARMA(Switch, e) {
for(auto& target : e.targets)
Expand Down Expand Up @@ -1462,8 +1462,8 @@ bool MIR_Optimise_Inlining(::MIR::TypeResolve& state, ::MIR::Function& fcn, bool
(If,
return ::MIR::Terminator::make_If({
this->clone_lval(se.cond),
se.bb0 + this->bb_base,
se.bb1 + this->bb_base
se.bb_true + this->bb_base,
se.bb_false + this->bb_base
});
),
(Switch,
Expand Down Expand Up @@ -3168,9 +3168,9 @@ bool MIR_Optimise_UnifyBlocks(::MIR::TypeResolve& state, ::MIR::Function& fcn)
(If,
if( ae.cond != be.cond )
return false;
if( ae.bb0 != be.bb0 )
if( ae.bb_true != be.bb_true )
return false;
if( ae.bb1 != be.bb1 )
if( ae.bb_false != be.bb_false )
return false;
),
(Switch,
Expand Down Expand Up @@ -4544,7 +4544,7 @@ bool MIR_Optimise_ConstPropagate(::MIR::TypeResolve& state, ::MIR::Function& fcn
}
else {
MIR_ASSERT(state, it->second.is_Bool(), "Terminator::If with known value not Bool - " << it->second);
auto new_bb = (it->second.as_Bool().v ? te.bb0 : te.bb1);
auto new_bb = (it->second.as_Bool().v ? te.bb_true : te.bb_false);
DEBUG(state << "Convert " << bb.terminator << " into Goto(" << new_bb << ") because condition known to be " << it->second);
bb.terminator = ::MIR::Terminator::make_Goto(new_bb);

Expand Down Expand Up @@ -4615,7 +4615,7 @@ bool MIR_Optimise_ConstPropagate(::MIR::TypeResolve& state, ::MIR::Function& fcn
if( val_known )
{
DEBUG("bb" << bbidx << ": Condition known to be " << known_val);
bb.terminator = ::MIR::Terminator::make_Goto( known_val ? te.bb0 : te.bb1 );
bb.terminator = ::MIR::Terminator::make_Goto( known_val ? te.bb_true : te.bb_false );
changed = true;
}
}
Expand Down Expand Up @@ -6294,8 +6294,8 @@ bool MIR_Optimise_GarbageCollect(::MIR::TypeResolve& state, ::MIR::Function& fcn
(Panic,
),
(If,
e.bb0 = block_rewrite_table[e.bb0];
e.bb1 = block_rewrite_table[e.bb1];
e.bb_true = block_rewrite_table[e.bb_true ];
e.bb_false = block_rewrite_table[e.bb_false];
),
(Switch,
for(auto& target : e.targets)
Expand Down Expand Up @@ -6404,8 +6404,8 @@ void MIR_SortBlocks(const StaticTraitResolve& resolve, const ::HIR::ItemPath& pa
todo.push_back(Todo { te.dst, info.branch_count, info.level + 1 });
),
(If,
todo.push_back(Todo { te.bb0, ++branches, info.level + 1 });
todo.push_back(Todo { te.bb1, ++branches, info.level + 1 });
todo.push_back(Todo { te.bb_true , ++branches, info.level + 1 });
todo.push_back(Todo { te.bb_false, ++branches, info.level + 1 });
),
(Switch,
for(auto dst : te.targets)
Expand Down
2 changes: 1 addition & 1 deletion src/trans/codegen_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,7 @@ namespace {
m_of << "\tgoto bb" << e << "; /* panic */\n";
}
TU_ARMA(If, e) {
m_of << "\tif("; emit_lvalue(e.cond); m_of << ") goto bb" << e.bb0 << "; else goto bb" << e.bb1 << ";\n";
m_of << "\tif("; emit_lvalue(e.cond); m_of << ") goto bb" << e.bb_true << "; else goto bb" << e.bb_false << ";\n";
}
TU_ARMA(Switch, e) {

Expand Down
32 changes: 16 additions & 16 deletions src/trans/codegen_c_structured.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,34 +146,34 @@ class Converter
bb_idx = SIZE_MAX;
),
(If,
auto arm0 = process_node_ref(te.bb0);
auto arm1 = process_node_ref(te.bb1);
auto arm_t = process_node_ref(te.bb_true );
auto arm_f = process_node_ref(te.bb_false);
bb_idx = SIZE_MAX;
if( arm0.has_target() && arm1.has_target() ) {
if( arm0.target() == arm1.target() ) {
DEBUG("If targets " << arm0.target() << " == " << arm1.target());
bb_idx = arm0.target();
if( arm_t.has_target() && arm_f.has_target() ) {
if( arm_t.target() == arm_f.target() ) {
DEBUG("If targets " << arm_t.target() << " == " << arm_f.target());
bb_idx = arm_t.target();
}
else {
stop = true;
DEBUG("If targets " << arm0.target() << " != " << arm1.target());
DEBUG("If targets " << arm_t.target() << " != " << arm_f.target());
// TODO: Pick one?
}
}
else if( arm0.has_target() ) {
DEBUG("If targets " << arm0.target() << ", NONE");
bb_idx = arm0.target();
else if( arm_t.has_target() ) {
DEBUG("If targets " << arm_t.target() << ", NONE");
bb_idx = arm_t.target();
}
else if( arm1.has_target() ) {
DEBUG("If targets NONE, " << arm1.target());
bb_idx = arm1.target();
else if( arm_f.has_target() ) {
DEBUG("If targets NONE, " << arm_f.target());
bb_idx = arm_f.target();
}
else {
// No target from either arm
DEBUG("If targets NONE, NONE");
stop = true;
}
refs.push_back(Node::make_If({ bb_idx, &te.cond, mv$(arm0), mv$(arm1) }));
refs.push_back(Node::make_If({ bb_idx, &te.cond, mv$(arm_t), mv$(arm_f) }));
),
(Switch,
::std::vector<NodeRef> arms;
Expand Down Expand Up @@ -338,8 +338,8 @@ ::std::vector<Node> MIR_To_Structured(const ::MIR::Function& fcn)
(Return,
),
(If,
conv.m_block_ref_count[te.bb0] += 1;
conv.m_block_ref_count[te.bb1] += 1;
conv.m_block_ref_count[te.bb_true ] += 1;
conv.m_block_ref_count[te.bb_false] += 1;
),
(Switch,
for(auto tgt : te.targets)
Expand Down
2 changes: 1 addition & 1 deletion src/trans/codegen_mmir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ namespace
m_of << "PANIC " << e.dst << "\n";
break;
TU_ARM(term, If, e)
m_of << "IF " << fmt(e.cond) << " goto " << e.bb0 << " else " << e.bb1 << "\n";
m_of << "IF " << fmt(e.cond) << " goto " << e.bb_true << " else " << e.bb_false << "\n";
break;
TU_ARM(term, Switch, e) {
m_of << "SWITCH " << fmt(e.val) << " { ";
Expand Down
2 changes: 1 addition & 1 deletion src/trans/monomorphise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ ::MIR::FunctionPointer Trans_Monomorphise(const ::StaticTraitResolve& resolve, c
(If,
terminator = ::MIR::Terminator::make_If({
monomorph_LValue(resolve, params, e.cond),
e.bb0, e.bb1
e.bb_true, e.bb_false
});
),
(Switch,
Expand Down
4 changes: 2 additions & 2 deletions tools/mir_opt_test/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ namespace {
e.panic_block = translate_bb(e.panic_block);
}
TU_ARMA(If, e) {
e.bb0 = translate_bb(e.bb0);
e.bb1 = translate_bb(e.bb1);
e.bb_true = translate_bb(e.bb_true);
e.bb_false = translate_bb(e.bb_false);
}
TU_ARMA(Switch, e) {
for(auto& tgt : e.targets)
Expand Down
4 changes: 2 additions & 2 deletions tools/standalone_miri/mir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ namespace MIR {
os << "Goto(" << e << ")";
),
(Panic,
os << "Panic(" << e.dst << ";)";
os << "Panic(" << e.dst << ")";
),
(If,
os << "If( " << e.cond << " : " << e.bb0 << ", " << e.bb1 << ")";
os << "If( " << e.cond << " goto " << e.bb_true << " else " << e.bb_false << ")";
),
(Switch,
os << "Switch( " << e.val << " : ";
Expand Down
2 changes: 1 addition & 1 deletion tools/standalone_miri/miri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ bool InterpreterThread::step_one(Value& out_thread_result)
TU_ARM(bb.terminator, If, te) {
uint8_t v = state.get_value_ref(te.cond).read_u8(0);
LOG_ASSERT(v == 0 || v == 1, "Boolean isn't 0/1 - instead " << int(v));
cur_frame.bb_idx = v ? te.bb0 : te.bb1;
cur_frame.bb_idx = v ? te.bb_true : te.bb_false;
} break;
TU_ARM(bb.terminator, Switch, te) {
::HIR::TypeRef ty;
Expand Down

0 comments on commit fd059c1

Please sign in to comment.