[PATCH 3/3] gccrs: borrowck: Polonius dump

Owen Avery powerboat9.gamer@gmail.com
Tue Dec 3 07:06:32 GMT 2024


These are some of the older easier-to-upstream patches -- I've added 
"gccrs: " to their subject fields but they should be otherwise unmodified.

On 12/3/24 01:26, Owen Avery wrote:
> From: Jakub Dupak <dev@jakubdupak.com>
>
> gcc/rust/ChangeLog:
>
> 	* checks/errors/borrowck/polonius/rust-polonius.h (struct FullPoint):
> 	Polonius facts dump.
> 	(struct Facts): Polonius facts dump.
> 	* checks/errors/borrowck/rust-bir-dump.cc (Dump::go):
> 	Polonius facts dump.
> 	(Dump::visit): Polonius facts dump.
> 	(Dump::visit_place): Polonius facts dump.
> 	(Dump::visit_move_place): Polonius facts dump.
> 	(Dump::visit_scope): Polonius facts dump.
> 	* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go): Polonius facts dump.
>
> Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
> ---
>   .../errors/borrowck/polonius/rust-polonius.h  |  2 +-
>   .../checks/errors/borrowck/rust-bir-dump.cc   | 25 +++++--
>   .../errors/borrowck/rust-borrow-checker.cc    | 69 ++++++++++++++++++-
>   3 files changed, 88 insertions(+), 8 deletions(-)
>
> diff --git a/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h b/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
> index 239cc344011..1534260552b 100644
> --- a/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
> +++ b/gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h
> @@ -148,7 +148,7 @@ struct Facts
>     void dump_var_used_at (std::ostream &os) const
>     {
>       for (auto &e : var_used_at)
> -      os << e.first - 1 << " " << FullPoint (e.second) << "\n";
> +      os << e.first << " " << FullPoint (e.second) << "\n";
>     }
>   
>     void dump_var_defined_at (std::ostream &os) const
> diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
> index d3398b6f405..03e2b8ea404 100644
> --- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
> +++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
> @@ -115,7 +115,7 @@ Dump::go (bool enable_simplify_cfg)
>     if (enable_simplify_cfg)
>       simplify_cfg (func, bb_fold_map);
>   
> -  renumber_places (func, place_map);
> +  // renumber_places (func, place_map);
>   
>     stream << "fn " << name << "(";
>     print_comma_separated (stream, func.arguments, [this] (PlaceId place_id) {
> @@ -214,6 +214,8 @@ Dump::visit (const Statement &stmt)
>         visit_place (stmt.get_place ());
>         stream << ")";
>         break;
> +    default:
> +      rust_internal_error_at (UNKNOWN_LOCATION, "Unknown statement kind.");
>       }
>     statement_place = INVALID_PLACE;
>   }
> @@ -251,7 +253,8 @@ Dump::visit_place (PlaceId place_id)
>         stream << "const " << get_tyty_name (place.tyty);
>         break;
>       case Place::INVALID:
> -      stream << "_INVALID";
> +      if (place_id == INVALID_PLACE)
> +	stream << "_INVALID";
>       }
>   }
>   
> @@ -259,7 +262,7 @@ void
>   Dump::visit_move_place (PlaceId place_id)
>   {
>     const Place &place = func.place_db[place_id];
> -  if (!place.is_constant ())
> +  if (place.should_be_moved ())
>       stream << "move ";
>     visit_place (place_id);
>   }
> @@ -267,7 +270,11 @@ Dump::visit_move_place (PlaceId place_id)
>   void
>   Dump::visit (const BorrowExpr &expr)
>   {
> -  stream << "&";
> +  stream << "&"
> +	 << "'?" << expr.get_origin () << " ";
> +  if (func.place_db.get_loans ()[expr.get_loan ()].mutability
> +      == Mutability::Mut)
> +    stream << "mut ";
>     visit_place (expr.get_place ());
>   }
>   
> @@ -360,7 +367,15 @@ Dump::visit_scope (ScopeId id, size_t depth)
>         indent (depth + 1) << "let _";
>         stream << place_map[local] << ": "
>   	     << get_tyty_name (func.place_db[local].tyty);
> -      stream << ";\n";
> +      stream << ";\t";
> +
> +      stream << "[";
> +      print_comma_separated (stream,
> +			     func.place_db[local].regions.get_regions (),
> +			     [this] (FreeRegion region_id) {
> +			       stream << "'?" << region_id;
> +			     });
> +      stream << "]\n";
>       }
>     for (auto &child : scope.children)
>       visit_scope (child, (id >= 1) ? depth + 1 : depth);
> diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
> index ae06aadaa5b..a2351c57eb4 100644
> --- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
> +++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
> @@ -18,9 +18,10 @@
>   
>   #include "rust-borrow-checker.h"
>   #include "rust-function-collector.h"
> +#include "rust-bir-fact-collector.h"
>   #include "rust-bir-builder.h"
>   #include "rust-bir-dump.h"
> -#include "rust-bir-fact-collector.h"
> +#include "polonius/rust-polonius.h"
>   
>   namespace Rust {
>   namespace HIR {
> @@ -36,7 +37,7 @@ mkdir_wrapped (const std::string &dirname)
>   #elif __APPLE__
>     ret = mkdir (dirname.c_str (), 0775);
>   #endif
> -  (void) ret;
> +  rust_assert (ret == 0 || errno == EEXIST);
>   }
>   
>   void
> @@ -68,6 +69,8 @@ BorrowChecker::go (HIR::Crate &crate)
>   	= mappings->get_crate_name (crate.get_mappings ().get_crate_num (),
>   				    crate_name);
>         rust_assert (ok);
> +
> +      mkdir_wrapped ("nll_facts_gccrs");
>       }
>   
>     FunctionCollector collector;
> @@ -75,6 +78,9 @@ BorrowChecker::go (HIR::Crate &crate)
>   
>     for (auto func : collector.get_functions ())
>       {
> +      rust_debug_loc (func->get_locus (), "\nChecking function %s\n",
> +		      func->get_function_name ().as_string ().c_str ());
> +
>         BIR::BuilderContext ctx;
>         BIR::Builder builder (ctx);
>         auto bir = builder.build (*func);
> @@ -89,6 +95,65 @@ BorrowChecker::go (HIR::Crate &crate)
>   	}
>   
>         auto facts = BIR::FactCollector::collect (bir);
> +
> +      if (enable_dump_bir)
> +	{
> +	  mkdir_wrapped ("nll_facts_gccrs/"
> +			 + func->get_function_name ().as_string ());
> +	  auto dump_facts_to_file
> +	    = [&] (const std::string &suffix,
> +		   void (Polonius::Facts::*fn) (std::ostream &) const) {
> +		std::string filename = "nll_facts_gccrs/"
> +				       + func->get_function_name ().as_string ()
> +				       + "/" + suffix + ".facts";
> +		std::ofstream file;
> +		file.open (filename);
> +		if (file.fail ())
> +		  {
> +		    abort ();
> +		  }
> +
> +		// Run dump
> +		// BEWARE: this callback charade is a workaround because gcc48
> +		// won't let me return a file from a function
> +		(facts.*fn) (file);
> +	      };
> +
> +	  dump_facts_to_file ("loan_issued_at",
> +			      &Polonius::Facts::dump_loan_issued_at);
> +	  dump_facts_to_file ("loan_killed_at",
> +			      &Polonius::Facts::dump_loan_killed_at);
> +	  dump_facts_to_file ("loan_invalidated_at",
> +			      &Polonius::Facts::dump_loan_invalidated_at);
> +	  dump_facts_to_file ("subset_base",
> +			      &Polonius::Facts::dump_subset_base);
> +	  dump_facts_to_file ("universal_region",
> +			      &Polonius::Facts::dump_universal_region);
> +	  dump_facts_to_file ("cfg_edge", &Polonius::Facts::dump_cfg_edge);
> +	  dump_facts_to_file ("var_used_at",
> +			      &Polonius::Facts::dump_var_used_at);
> +	  dump_facts_to_file ("var_defined_at",
> +			      &Polonius::Facts::dump_var_defined_at);
> +	  dump_facts_to_file ("var_dropped_at",
> +			      &Polonius::Facts::dump_var_dropped_at);
> +	  dump_facts_to_file ("use_of_var_derefs_origin",
> +			      &Polonius::Facts::dump_use_of_var_derefs_origin);
> +	  dump_facts_to_file ("drop_of_var_derefs_origin",
> +			      &Polonius::Facts::dump_drop_of_var_derefs_origin);
> +	  dump_facts_to_file ("child_path", &Polonius::Facts::dump_child_path);
> +	  dump_facts_to_file ("path_is_var",
> +			      &Polonius::Facts::dump_path_is_var);
> +	  dump_facts_to_file ("known_placeholder_subset",
> +			      &Polonius::Facts::dump_known_placeholder_subset);
> +	  dump_facts_to_file ("path_moved_at_base",
> +			      &Polonius::Facts::dump_path_moved_at_base);
> +	  dump_facts_to_file ("path_accessed_at_base",
> +			      &Polonius::Facts::dump_path_accessed_at_base);
> +	  dump_facts_to_file ("path_assigned_at_base",
> +			      &Polonius::Facts::dump_path_assigned_at_base);
> +	  dump_facts_to_file ("placeholder",
> +			      &Polonius::Facts::dump_placeholder);
> +	}
>       }
>   
>     for (auto closure ATTRIBUTE_UNUSED : collector.get_closures ())


More information about the Gcc-rust mailing list