From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id BA4E73860770; Tue, 16 Jan 2024 18:17:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA4E73860770 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705429036; bh=+XfHdujPVWjHa4fJsqBx5MPqk659WZr5N7b9Qaun+qg=; h=From:To:Subject:Date:From; b=KUOlMFLrC3U8mNN75IpjYFCpQwb+Ifp0oWUAI1xFAw9kaxYSEqTcf2viEyjKur0wl +MmHIdhH5+//y876Sb3R0GqIOu2Hcd2hgI5OLWnl9LGr0osJzCMUGlYx/x4kM0sFjE vpkMJ4B0EXKSnnPE6nCptDVPpHjt/yHAvXTdCzl4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8084] gccrs: borrowck: Dump improve jumps X-Act-Checkin: gcc X-Git-Author: Jakub Dupak X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 308e34c8febc019a686163366684f841a215d5c3 X-Git-Newrev: acfc1f31af8a33b34eebd2e1db6995e6d0632733 Message-Id: <20240116181716.BA4E73860770@sourceware.org> Date: Tue, 16 Jan 2024 18:17:16 +0000 (GMT) List-Id: https://gcc.gnu.org/g:acfc1f31af8a33b34eebd2e1db6995e6d0632733 commit r14-8084-gacfc1f31af8a33b34eebd2e1db6995e6d0632733 Author: Jakub Dupak Date: Wed Oct 18 23:27:48 2023 +0200 gccrs: borrowck: Dump improve jumps gcc/rust/ChangeLog: * checks/errors/borrowck/rust-bir-dump.cc (Dump::go): Improve jumps dump. (Dump::visit): Improve jumps dump. * checks/errors/borrowck/rust-bir-dump.h (class Dump): Improve jumps dump. Signed-off-by: Jakub Dupak Diff: --- gcc/rust/checks/errors/borrowck/rust-bir-dump.cc | 77 ++++++++++++++++-------- gcc/rust/checks/errors/borrowck/rust-bir-dump.h | 4 +- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc index 709d36a7cd3..ba530983dad 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc @@ -6,13 +6,6 @@ namespace BIR { constexpr auto indentation = " "; -uint32_t -get_place_name (PlaceId place_id) -{ - rust_assert (place_id >= FIRST_VARIABLE_PLACE); - return place_id - FIRST_VARIABLE_PLACE; -} - uint32_t get_lifetime_name (Lifetime lifetime_id) { @@ -44,6 +37,28 @@ print_comma_separated (std::ostream &stream, const std::vector &collection, } } +void +renumber_places (const Function &func, std::vector &place_map) +{ + // Renumbering places to avoid gaps in the place id space. + // This is needed to match MIR shape. + size_t next_out_id = 0; + + for (size_t in_id = FIRST_VARIABLE_PLACE; in_id < func.place_db.size (); + ++in_id) + { + const Place &place = func.place_db[in_id]; + if (place.kind == Place::VARIABLE || place.kind == Place::TEMPORARY) + { + place_map[in_id] = next_out_id++; + } + else + { + place_map[in_id] = INVALID_PLACE; + } + } +} + void simplify_cfg (Function &func, std::vector &bb_fold_map) { @@ -74,12 +89,16 @@ Dump::go (bool enable_simplify_cfg) // To avoid mutation of the BIR, we use indirection through bb_fold_map. std::iota (bb_fold_map.begin (), bb_fold_map.end (), 0); + std::iota (place_map.begin (), place_map.end (), 0); + if (enable_simplify_cfg) simplify_cfg (func, bb_fold_map); + renumber_places (func, place_map); + stream << "fn " << name << "("; print_comma_separated (stream, func.arguments, [this] (PlaceId place_id) { - stream << "_" << get_place_name (place_id) << ": " + stream << "_" << place_map[place_id] << ": " << get_tyty_name (place_db[place_id].tyty); }); stream << ") -> " << get_tyty_name (place_db[RETURN_VALUE_PLACE].tyty) @@ -93,33 +112,30 @@ Dump::go (bool enable_simplify_cfg) if (std::find (func.arguments.begin (), func.arguments.end (), id) != func.arguments.end ()) continue; - stream << indentation << "let _" << get_place_name (id) << ": " + stream << indentation << "let _" << place_map[id] << ": " << get_tyty_name (place_db[id].tyty) << ";\n"; } } - for (BasicBlockId id = 0; id < func.basic_blocks.size (); ++id) + for (node_bb = 0; node_bb < func.basic_blocks.size (); ++node_bb) { - if (bb_fold_map[id] != id) + if (bb_fold_map[node_bb] != node_bb) continue; // This BB was folded. - if (func.basic_blocks[id].statements.empty () - && func.basic_blocks[id].successors.empty ()) + if (func.basic_blocks[node_bb].statements.empty () + && func.basic_blocks[node_bb].successors.empty ()) continue; - BasicBlock &bb = func.basic_blocks[id]; + BasicBlock &bb = func.basic_blocks[node_bb]; stream << "\n"; - stream << indentation << "bb" << bb_fold_map[id] << ": {\n"; + stream << indentation << "bb" << bb_fold_map[node_bb] << ": {\n"; for (auto &stmt : bb.statements) { stream << indentation << indentation; visit (stmt); stream << ";\n"; } - stream << indentation << "} -> ["; - for (auto succ : bb.successors) - stream << "bb" << bb_fold_map[succ] << ", "; - stream << "]\n"; + stream << indentation << "}\n"; } stream << "}\n\n"; @@ -131,19 +147,26 @@ Dump::visit (Node &node) switch (node.get_kind ()) { case Node::Kind::ASSIGNMENT: { - stream << "_" << get_place_name (node.get_place ()) << " = "; + stream << "_" << place_map[node.get_place ()] << " = "; node.get_expr ().accept_vis (*this); break; } case Node::Kind::SWITCH: - stream << "switch "; + stream << "switchInt("; visit_move_place (node.get_place ()); + stream << ") -> ["; + print_comma_separated (stream, func.basic_blocks[node_bb].successors, + [this] (BasicBlockId succ) { + stream << "bb" << bb_fold_map[succ]; + }); + stream << "]"; break; case Node::Kind::RETURN: stream << "return"; break; case Node::Kind::GOTO: - stream << "goto"; + stream << "goto -> bb" + << bb_fold_map[func.basic_blocks[node_bb].successors.at (0)]; break; case Node::Kind::STORAGE_DEAD: stream << "StorageDead("; @@ -167,7 +190,7 @@ Dump::visit_place (PlaceId place_id) { case Place::TEMPORARY: case Place::VARIABLE: - stream << "_" << get_place_name (place_id); + stream << "_" << place_map[place_id]; break; case Place::DEREF: stream << "("; @@ -255,7 +278,13 @@ Dump::visit (CallExpr &expr) visit_move_place (place); stream << ", "; } - stream << ")"; + stream << ") -> ["; + print_comma_separated (stream, + func.basic_blocks[bb_fold_map[node_bb]].successors, + [this] (const BasicBlockId &dst) { + stream << "bb" << bb_fold_map[dst]; + }); + stream << "]"; } void diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h index 1f8de045c22..edf7d1ea1ec 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h @@ -36,13 +36,15 @@ class Dump : public Visitor const std::string &name; std::vector bb_fold_map; + std::vector place_map; PlaceId node_place = INVALID_PLACE; + BasicBlockId node_bb = INVALID_BB; public: Dump (std::ostream &os, Function &func, const std::string &name) : stream (os), place_db (func.place_db), func (func), name (name), - bb_fold_map (func.basic_blocks.size ()) + bb_fold_map (func.basic_blocks.size ()), place_map (func.place_db.size ()) {} void go (bool enable_simplify_cfg = false);