public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Canonicalize types based on hashing than HirIds and TyTy equality
@ 2022-06-08 12:48 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:48 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee

commit 39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri May 20 13:35:54 2022 +0100

    Canonicalize types based on hashing than HirIds and TyTy equality
    
    When we compile types for GCC we need to make sure we canonicalize them,
    this means that for any user defined type such as Structs or Tuples we want
    all expressions or declarations to use the same tree. Even if we have
    duplicate tree's we can end up confusing the middle-end and lose out on
    optimizations as we will be using view_convert_exprs to maintain the
    consistancy which will marshall between the objects unnessecarily.
    
    The old method for doing this kept mappings of HirIds which was fine for
    simple cases but when generics are involved new Id's are generated so this
    meant we ended up having a vector of pairs {TyTy::BaseType, tree} and we
    did a liner search to find the relevant TyTy::BaseType that we equaled to.
    
    This was not only slow but did not handle the cases for generic associated
    types or nested generics. So we needed a more general faster implementation
    therefore hashing. This patch takes the gcc/tree.h type_hash_canon code
    and adds in hashing for RECORD and UNION types. This means we will generate
    a duplicate type then hash it and look for an existing type.
    
    This patch will also allow us to fix how we implement monomorphization of
    functions by nesting the hashes behind DefId's.

Diff:
---
 gcc/rust/Make-lang.in                             |   1 +
 gcc/rust/backend/rust-compile-block.h             |   1 -
 gcc/rust/backend/rust-compile-context.cc          | 139 ++++++++++++
 gcc/rust/backend/rust-compile-context.h           |  71 ++----
 gcc/rust/backend/rust-compile-expr.h              |   1 -
 gcc/rust/backend/rust-compile-resolve-path.h      |   1 -
 gcc/rust/backend/rust-compile-stmt.h              |   1 -
 gcc/rust/backend/rust-compile-struct-field-expr.h |   1 -
 gcc/rust/backend/rust-compile-type.cc             | 202 ++++++++++-------
 gcc/rust/backend/rust-compile-type.h              |  12 +-
 gcc/rust/backend/rust-compile-tyty.h              | 260 ----------------------
 gcc/rust/backend/rust-mangle.h                    |   3 +-
 12 files changed, 291 insertions(+), 402 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 11994740e78..5d97a162c30 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -130,6 +130,7 @@ GRS_OBJS = \
     rust/rust-constexpr.o \
     rust/rust-compile-base.o \
     rust/rust-tree.o \
+    rust/rust-compile-context.o \
     $(END)
 # removed object files from here
 
diff --git a/gcc/rust/backend/rust-compile-block.h b/gcc/rust/backend/rust-compile-block.h
index 4a3c6f52c3d..0595ee905ab 100644
--- a/gcc/rust/backend/rust-compile-block.h
+++ b/gcc/rust/backend/rust-compile-block.h
@@ -20,7 +20,6 @@
 #define RUST_COMPILE_BLOCK
 
 #include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
 
 namespace Rust {
 namespace Compile {
diff --git a/gcc/rust/backend/rust-compile-context.cc b/gcc/rust/backend/rust-compile-context.cc
new file mode 100644
index 00000000000..463ac27aa5a
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-context.cc
@@ -0,0 +1,139 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-compile-context.h"
+#include "rust-compile-type.h"
+
+namespace Rust {
+namespace Compile {
+
+Context::Context (::Backend *backend)
+  : backend (backend), resolver (Resolver::Resolver::get ()),
+    tyctx (Resolver::TypeCheckContext::get ()),
+    mappings (Analysis::Mappings::get ()), mangler (Mangler ())
+{
+  setup_builtins ();
+}
+
+void
+Context::setup_builtins ()
+{
+  auto builtins = resolver->get_builtin_types ();
+  for (auto it = builtins.begin (); it != builtins.end (); it++)
+    {
+      HirId ref;
+      bool ok = tyctx->lookup_type_by_node_id ((*it)->get_node_id (), &ref);
+      rust_assert (ok);
+
+      TyTy::BaseType *lookup;
+      ok = tyctx->lookup_type (ref, &lookup);
+      rust_assert (ok);
+
+      TyTyResolveCompile::compile (this, lookup);
+    }
+}
+
+hashval_t
+Context::type_hasher (tree type)
+{
+  inchash::hash hstate;
+
+  hstate.add_int (TREE_CODE (type));
+
+  if (TYPE_NAME (type))
+    {
+      hashval_t record_name_hash
+	= IDENTIFIER_HASH_VALUE (DECL_NAME (TYPE_NAME (type)));
+      hstate.add_object (record_name_hash);
+    }
+
+  if (TREE_TYPE (type))
+    hstate.add_object (TYPE_HASH (TREE_TYPE (type)));
+
+  for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
+    /* Just the identifier is adequate to distinguish.  */
+    hstate.add_object (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (t)));
+
+  switch (TREE_CODE (type))
+    {
+    case METHOD_TYPE:
+      hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
+      /* FALLTHROUGH. */
+    case FUNCTION_TYPE:
+      for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
+	if (TREE_VALUE (t) != error_mark_node)
+	  hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
+      break;
+
+    case OFFSET_TYPE:
+      hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
+      break;
+
+      case ARRAY_TYPE: {
+	if (TYPE_DOMAIN (type))
+	  hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
+	if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
+	  {
+	    unsigned typeless = TYPE_TYPELESS_STORAGE (type);
+	    hstate.add_object (typeless);
+	  }
+      }
+      break;
+
+      case INTEGER_TYPE: {
+	tree t = TYPE_MAX_VALUE (type);
+	if (!t)
+	  t = TYPE_MIN_VALUE (type);
+	for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
+	  hstate.add_object (TREE_INT_CST_ELT (t, i));
+	break;
+      }
+
+    case REAL_TYPE:
+      case FIXED_POINT_TYPE: {
+	unsigned prec = TYPE_PRECISION (type);
+	hstate.add_object (prec);
+	break;
+      }
+
+    case VECTOR_TYPE:
+      hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
+      break;
+
+    case RECORD_TYPE:
+    case UNION_TYPE:
+      case QUAL_UNION_TYPE: {
+	for (tree t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
+	  {
+	    hashval_t name_hash = IDENTIFIER_HASH_VALUE (DECL_NAME (t));
+	    hashval_t type_hash = type_hasher (TREE_TYPE (t));
+	    hstate.add_object (name_hash);
+	    hstate.add_object (type_hash);
+	  }
+      }
+      break;
+
+    default:
+      break;
+    }
+
+  return hstate.end ();
+}
+
+} // namespace Compile
+} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index d17034b51df..83785549259 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -24,7 +24,6 @@
 #include "rust-name-resolver.h"
 #include "rust-hir-type-check.h"
 #include "rust-backend.h"
-#include "rust-compile-tyty.h"
 #include "rust-hir-full.h"
 #include "rust-mangle.h"
 #include "rust-tree.h"
@@ -41,50 +40,14 @@ struct fncontext
 class Context
 {
 public:
-  Context (::Backend *backend)
-    : backend (backend), resolver (Resolver::Resolver::get ()),
-      tyctx (Resolver::TypeCheckContext::get ()),
-      mappings (Analysis::Mappings::get ()), mangler (Mangler ())
-  {
-    // insert the builtins
-    auto builtins = resolver->get_builtin_types ();
-    for (auto it = builtins.begin (); it != builtins.end (); it++)
-      {
-	HirId ref;
-	bool ok = tyctx->lookup_type_by_node_id ((*it)->get_node_id (), &ref);
-	rust_assert (ok);
-
-	TyTy::BaseType *lookup;
-	ok = tyctx->lookup_type (ref, &lookup);
-	rust_assert (ok);
+  Context (::Backend *backend);
 
-	tree compiled = TyTyCompile::compile (backend, lookup);
-	compiled_type_map.insert (std::pair<HirId, tree> (ref, compiled));
-	builtin_range.insert (ref);
-      }
-  }
+  void setup_builtins ();
 
-  bool lookup_compiled_types (HirId id, tree *type,
-			      const TyTy::BaseType *ref = nullptr)
+  bool lookup_compiled_types (tree t, tree *type)
   {
-    if (ref != nullptr)
-      {
-	for (auto it = mono.begin (); it != mono.end (); it++)
-	  {
-	    std::pair<HirId, tree> &val = it->second;
-	    const TyTy::BaseType *r = it->first;
-
-	    if (ref->is_equal (*r))
-	      {
-		*type = val.second;
-
-		return true;
-	      }
-	  }
-	return false;
-      }
-
-    auto it = compiled_type_map.find (id);
+    hashval_t h = type_hasher (t);
+    auto it = compiled_type_map.find (h);
     if (it == compiled_type_map.end ())
       return false;
 
@@ -92,16 +55,16 @@ public:
     return true;
   }
 
-  void insert_compiled_type (HirId id, tree type,
-			     const TyTy::BaseType *ref = nullptr)
+  tree insert_compiled_type (tree type)
   {
-    rust_assert (builtin_range.find (id) == builtin_range.end ());
-    compiled_type_map.insert (std::pair<HirId, tree> (id, type));
-    if (ref != nullptr)
-      {
-	std::pair<HirId, tree> elem (id, type);
-	mono[ref] = std::move (elem);
-      }
+    hashval_t h = type_hasher (type);
+    auto it = compiled_type_map.find (h);
+    if (it != compiled_type_map.end ())
+      return it->second;
+
+    compiled_type_map.insert ({h, type});
+    push_type (type);
+    return type;
   }
 
   ::Backend *get_backend () { return backend; }
@@ -328,18 +291,19 @@ public:
   std::vector<tree> &get_const_decls () { return const_decls; }
   std::vector<tree> &get_func_decls () { return func_decls; }
 
+  static hashval_t type_hasher (tree type);
+
 private:
   ::Backend *backend;
   Resolver::Resolver *resolver;
   Resolver::TypeCheckContext *tyctx;
   Analysis::Mappings *mappings;
-  std::set<HirId> builtin_range;
   Mangler mangler;
 
   // state
   std::vector<fncontext> fn_stack;
   std::map<HirId, ::Bvariable *> compiled_var_decls;
-  std::map<HirId, tree> compiled_type_map;
+  std::map<hashval_t, tree> compiled_type_map;
   std::map<HirId, tree> compiled_fn_map;
   std::map<HirId, tree> compiled_consts;
   std::map<HirId, tree> compiled_labels;
@@ -347,7 +311,6 @@ private:
   std::vector<tree> scope_stack;
   std::vector<::Bvariable *> loop_value_stack;
   std::vector<tree> loop_begin_labels;
-  std::map<const TyTy::BaseType *, std::pair<HirId, tree>> mono;
   std::map<DefId, std::vector<std::pair<const TyTy::BaseType *, tree>>>
     mono_fns;
   std::map<HirId, tree> implicit_pattern_bindings;
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index b05b129324c..655ffbb263e 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -20,7 +20,6 @@
 #define RUST_COMPILE_EXPR
 
 #include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
 #include "rust-compile-resolve-path.h"
 #include "rust-compile-block.h"
 #include "rust-compile-struct-field-expr.h"
diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h
index 37dec0b0e1b..f0360bdc739 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.h
+++ b/gcc/rust/backend/rust-compile-resolve-path.h
@@ -20,7 +20,6 @@
 #define RUST_COMPILE_RESOLVE_PATH
 
 #include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
 
 namespace Rust {
 namespace Compile {
diff --git a/gcc/rust/backend/rust-compile-stmt.h b/gcc/rust/backend/rust-compile-stmt.h
index ad3425332fc..5f1777736db 100644
--- a/gcc/rust/backend/rust-compile-stmt.h
+++ b/gcc/rust/backend/rust-compile-stmt.h
@@ -20,7 +20,6 @@
 #define RUST_COMPILE_STMT
 
 #include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
 #include "rust-compile-expr.h"
 
 namespace Rust {
diff --git a/gcc/rust/backend/rust-compile-struct-field-expr.h b/gcc/rust/backend/rust-compile-struct-field-expr.h
index c5e986e0b9b..6968c069f0f 100644
--- a/gcc/rust/backend/rust-compile-struct-field-expr.h
+++ b/gcc/rust/backend/rust-compile-struct-field-expr.h
@@ -20,7 +20,6 @@
 #define RUST_COMPILE_STRUCT_FIELD_EXPR
 
 #include "rust-compile-base.h"
-#include "rust-compile-tyty.h"
 
 namespace Rust {
 namespace Compile {
diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc
index 03c2ca487f2..38740274a44 100644
--- a/gcc/rust/backend/rust-compile-type.cc
+++ b/gcc/rust/backend/rust-compile-type.cc
@@ -21,11 +21,27 @@
 #include "rust-constexpr.h"
 
 #include "tree.h"
-#include "print-tree.h"
 
 namespace Rust {
 namespace Compile {
 
+tree
+TyTyResolveCompile::compile (Context *ctx, const TyTy::BaseType *ty,
+			     bool trait_object_mode)
+{
+  TyTyResolveCompile compiler (ctx, trait_object_mode);
+  ty->accept_vis (compiler);
+
+  if (compiler.translated != error_mark_node
+      && TYPE_NAME (compiler.translated) != NULL)
+    {
+      // canonicalize the type
+      compiler.translated = ctx->insert_compiled_type (compiler.translated);
+    }
+
+  return compiler.translated;
+}
+
 static const std::string RUST_ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR";
 
 // see: gcc/c/c-decl.cc:8230-8241
@@ -96,6 +112,7 @@ TyTyResolveCompile::visit (const TyTy::PlaceholderType &type)
 void
 TyTyResolveCompile::visit (const TyTy::ParamType &param)
 {
+  // FIXME make this reuse the same machinery from constexpr code
   recursion_count++;
   rust_assert (recursion_count < kDefaultRecusionLimit);
 
@@ -163,9 +180,6 @@ TyTyResolveCompile::visit (const TyTy::FnPtr &type)
 void
 TyTyResolveCompile::visit (const TyTy::ADTType &type)
 {
-  if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
-    return;
-
   tree type_record = error_mark_node;
   if (!type.is_enum ())
     {
@@ -302,14 +316,8 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
 
   std::string named_struct_str
     = type.get_ident ().path.get () + type.subst_as_string ();
-  tree named_struct
-    = ctx->get_backend ()->named_type (named_struct_str, type_record,
-				       type.get_ident ().locus);
-
-  ctx->push_type (named_struct);
-  translated = named_struct;
-
-  ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
+  translated = ctx->get_backend ()->named_type (named_struct_str, type_record,
+						type.get_ident ().locus);
 }
 
 void
@@ -321,10 +329,6 @@ TyTyResolveCompile::visit (const TyTy::TupleType &type)
       return;
     }
 
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type);
-  if (ok)
-    return;
-
   // create implicit struct
   std::vector<Backend::typed_identifier> fields;
   for (size_t i = 0; i < type.num_fields (); i++)
@@ -345,13 +349,9 @@ TyTyResolveCompile::visit (const TyTy::TupleType &type)
     }
 
   tree struct_type_record = ctx->get_backend ()->struct_type (fields);
-  tree named_struct
+  translated
     = ctx->get_backend ()->named_type (type.as_string (), struct_type_record,
 				       type.get_ident ().locus);
-
-  ctx->push_type (named_struct);
-  ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
-  translated = named_struct;
 }
 
 void
@@ -369,9 +369,6 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
 void
 TyTyResolveCompile::visit (const TyTy::SliceType &type)
 {
-  if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
-    return;
-
   std::vector<Backend::typed_identifier> fields;
 
   tree element_type
@@ -393,77 +390,141 @@ TyTyResolveCompile::visit (const TyTy::SliceType &type)
 
   std::string named_struct_str
     = std::string ("[") + type.get_element_type ()->get_name () + "]";
-  tree named_struct
-    = ctx->get_backend ()->named_type (named_struct_str, type_record,
-				       type.get_ident ().locus);
-
-  ctx->push_type (named_struct);
-  translated = named_struct;
-
-  ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
+  translated = ctx->get_backend ()->named_type (named_struct_str, type_record,
+						type.get_ident ().locus);
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::BoolType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  translated
+    = ctx->get_backend ()->named_type ("bool",
+				       ctx->get_backend ()->bool_type (),
+				       Linemap::predeclared_location ());
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::IntType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  switch (type.get_int_kind ())
+    {
+    case TyTy::IntType::I8:
+      translated = ctx->get_backend ()->named_type (
+	"i8", ctx->get_backend ()->integer_type (false, 8),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::IntType::I16:
+      translated = ctx->get_backend ()->named_type (
+	"i16", ctx->get_backend ()->integer_type (false, 16),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::IntType::I32:
+      translated = ctx->get_backend ()->named_type (
+	"i32", ctx->get_backend ()->integer_type (false, 32),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::IntType::I64:
+      translated = ctx->get_backend ()->named_type (
+	"i64", ctx->get_backend ()->integer_type (false, 64),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::IntType::I128:
+      translated = ctx->get_backend ()->named_type (
+	"i128", ctx->get_backend ()->integer_type (false, 128),
+	Linemap::predeclared_location ());
+      return;
+    }
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::UintType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  switch (type.get_uint_kind ())
+    {
+    case TyTy::UintType::U8:
+      translated = ctx->get_backend ()->named_type (
+	"u8", ctx->get_backend ()->integer_type (true, 8),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::UintType::U16:
+      translated = ctx->get_backend ()->named_type (
+	"u16", ctx->get_backend ()->integer_type (true, 16),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::UintType::U32:
+      translated = ctx->get_backend ()->named_type (
+	"u32", ctx->get_backend ()->integer_type (true, 32),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::UintType::U64:
+      translated = ctx->get_backend ()->named_type (
+	"u64", ctx->get_backend ()->integer_type (true, 64),
+	Linemap::predeclared_location ());
+      return;
+
+    case TyTy::UintType::U128:
+      translated = ctx->get_backend ()->named_type (
+	"u128", ctx->get_backend ()->integer_type (true, 128),
+	Linemap::predeclared_location ());
+      return;
+    }
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::FloatType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  switch (type.get_float_kind ())
+    {
+    case TyTy::FloatType::F32:
+      translated
+	= ctx->get_backend ()->named_type ("f32",
+					   ctx->get_backend ()->float_type (32),
+					   Linemap::predeclared_location ());
+      return;
+
+    case TyTy::FloatType::F64:
+      translated
+	= ctx->get_backend ()->named_type ("f64",
+					   ctx->get_backend ()->float_type (64),
+					   Linemap::predeclared_location ());
+      return;
+    }
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::USizeType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  translated = ctx->get_backend ()->named_type (
+    "usize",
+    ctx->get_backend ()->integer_type (
+      true, ctx->get_backend ()->get_pointer_size ()),
+    Linemap::predeclared_location ());
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::ISizeType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  translated = ctx->get_backend ()->named_type (
+    "isize",
+    ctx->get_backend ()->integer_type (
+      false, ctx->get_backend ()->get_pointer_size ()),
+    Linemap::predeclared_location ());
 }
 
 void
 TyTyResolveCompile::visit (const TyTy::CharType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  translated
+    = ctx->get_backend ()->named_type ("char",
+				       ctx->get_backend ()->wchar_type (),
+				       Linemap::predeclared_location ());
 }
 
 void
@@ -501,10 +562,10 @@ TyTyResolveCompile::visit (const TyTy::PointerType &type)
 void
 TyTyResolveCompile::visit (const TyTy::StrType &type)
 {
-  tree compiled_type = nullptr;
-  bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &compiled_type);
-  rust_assert (ok);
-  translated = compiled_type;
+  tree raw_str = ctx->get_backend ()->raw_str_type ();
+  translated
+    = ctx->get_backend ()->named_type ("str", raw_str,
+				       Linemap::predeclared_location ());
 }
 
 void
@@ -523,9 +584,6 @@ TyTyResolveCompile::visit (const TyTy::DynamicObjectType &type)
       return;
     }
 
-  if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
-    return;
-
   // create implicit struct
   auto items = type.get_object_items ();
   std::vector<Backend::typed_identifier> fields;
@@ -547,14 +605,8 @@ TyTyResolveCompile::visit (const TyTy::DynamicObjectType &type)
   fields.push_back (std::move (vtf));
 
   tree type_record = ctx->get_backend ()->struct_type (fields);
-  tree named_struct
-    = ctx->get_backend ()->named_type (type.get_name (), type_record,
-				       type.get_ident ().locus);
-
-  ctx->push_type (named_struct);
-  translated = named_struct;
-
-  ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
+  translated = ctx->get_backend ()->named_type (type.get_name (), type_record,
+						type.get_ident ().locus);
 }
 
 } // namespace Compile
diff --git a/gcc/rust/backend/rust-compile-type.h b/gcc/rust/backend/rust-compile-type.h
index 4f9c40349e4..3e1f903f761 100644
--- a/gcc/rust/backend/rust-compile-type.h
+++ b/gcc/rust/backend/rust-compile-type.h
@@ -28,12 +28,7 @@ class TyTyResolveCompile : public TyTy::TyConstVisitor
 {
 public:
   static tree compile (Context *ctx, const TyTy::BaseType *ty,
-		       bool trait_object_mode = false)
-  {
-    TyTyResolveCompile compiler (ctx, trait_object_mode);
-    ty->accept_vis (compiler);
-    return compiler.translated;
-  }
+		       bool trait_object_mode = false);
 
   static tree get_implicit_enumeral_node_type (Context *ctx);
 
@@ -62,6 +57,9 @@ public:
   void visit (const TyTy::DynamicObjectType &) override;
   void visit (const TyTy::ClosureType &) override;
 
+public:
+  static hashval_t type_hasher (tree type);
+
 private:
   TyTyResolveCompile (Context *ctx, bool trait_object_mode)
     : ctx (ctx), trait_object_mode (trait_object_mode),
@@ -72,8 +70,8 @@ private:
   bool trait_object_mode;
   tree translated;
 
+  // FIXME this needs to be derived from the gcc config option
   size_t recursion_count;
-
   static const size_t kDefaultRecusionLimit = 5;
 };
 
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
deleted file mode 100644
index 52ad2f94c12..00000000000
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ /dev/null
@@ -1,260 +0,0 @@
-// Copyright (C) 2020-2022 Free Software Foundation, Inc.
-
-// This file is part of GCC.
-
-// GCC is free software; you can redistribute it and/or modify it under
-// the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3, or (at your option) any later
-// version.
-
-// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
-// WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-// for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with GCC; see the file COPYING3.  If not see
-// <http://www.gnu.org/licenses/>.
-
-#ifndef RUST_COMPILE_TYTY
-#define RUST_COMPILE_TYTY
-
-#include "rust-system.h"
-#include "rust-location.h"
-#include "rust-diagnostics.h"
-#include "rust-backend.h"
-#include "rust-tyty.h"
-#include "rust-tyty-visitor.h"
-#include "rust-hir-map.h"
-#include "rust-hir-full.h"
-
-namespace Rust {
-namespace Compile {
-
-class TyTyCompile : public TyTy::TyVisitor
-{
-public:
-  static tree compile (::Backend *backend, TyTy::BaseType *ty)
-  {
-    TyTyCompile compiler (backend);
-    ty->accept_vis (compiler);
-    rust_assert (compiler.translated != nullptr);
-    return compiler.translated;
-  }
-
-  void visit (TyTy::ErrorType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::InferType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::ADTType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::PlaceholderType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::ProjectionType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::TupleType &type) override
-  {
-    // this interface is only for unit-type the -type interface takes into
-    // account the context
-    rust_assert (type.num_fields () == 0);
-    translated = backend->unit_type ();
-  }
-
-  void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::SliceType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::PointerType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::ParamType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
-
-  void visit (TyTy::FnType &type) override
-  {
-    Backend::typed_identifier receiver;
-    std::vector<Backend::typed_identifier> parameters;
-    std::vector<Backend::typed_identifier> results;
-
-    if (!type.get_return_type ()->is_unit ())
-      {
-	auto hir_type = type.get_return_type ();
-	auto ret = TyTyCompile::compile (backend, hir_type);
-	results.push_back (Backend::typed_identifier (
-	  "_", ret, mappings->lookup_location (hir_type->get_ref ())));
-      }
-
-    for (auto &params : type.get_params ())
-      {
-	auto param_pattern = params.first;
-	auto param_tyty = params.second;
-	auto compiled_param_type = TyTyCompile::compile (backend, param_tyty);
-
-	auto compiled_param = Backend::typed_identifier (
-	  param_pattern->as_string (), compiled_param_type,
-	  mappings->lookup_location (param_tyty->get_ref ()));
-
-	parameters.push_back (compiled_param);
-      }
-
-    if (!type.is_varadic ())
-      translated
-	= backend->function_type (receiver, parameters, results, NULL,
-				  mappings->lookup_location (type.get_ref ()));
-    else
-      translated
-	= backend->function_type_varadic (receiver, parameters, results, NULL,
-					  mappings->lookup_location (
-					    type.get_ref ()));
-  }
-
-  void visit (TyTy::BoolType &) override
-  {
-    translated = backend->named_type ("bool", backend->bool_type (),
-				      Linemap::predeclared_location ());
-  }
-
-  void visit (TyTy::IntType &type) override
-  {
-    switch (type.get_int_kind ())
-      {
-      case TyTy::IntType::I8:
-	translated
-	  = backend->named_type ("i8", backend->integer_type (false, 8),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::IntType::I16:
-	translated
-	  = backend->named_type ("i16", backend->integer_type (false, 16),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::IntType::I32:
-	translated
-	  = backend->named_type ("i32", backend->integer_type (false, 32),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::IntType::I64:
-	translated
-	  = backend->named_type ("i64", backend->integer_type (false, 64),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::IntType::I128:
-	translated
-	  = backend->named_type ("i128", backend->integer_type (false, 128),
-				 Linemap::predeclared_location ());
-	return;
-      }
-    gcc_unreachable ();
-  }
-
-  void visit (TyTy::UintType &type) override
-  {
-    switch (type.get_uint_kind ())
-      {
-      case TyTy::UintType::U8:
-	translated = backend->named_type ("u8", backend->integer_type (true, 8),
-					  Linemap::predeclared_location ());
-	return;
-
-      case TyTy::UintType::U16:
-	translated
-	  = backend->named_type ("u16", backend->integer_type (true, 16),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::UintType::U32:
-	translated
-	  = backend->named_type ("u32", backend->integer_type (true, 32),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::UintType::U64:
-	translated
-	  = backend->named_type ("u64", backend->integer_type (true, 64),
-				 Linemap::predeclared_location ());
-	return;
-
-      case TyTy::UintType::U128:
-	translated
-	  = backend->named_type ("u128", backend->integer_type (true, 128),
-				 Linemap::predeclared_location ());
-	return;
-      }
-    gcc_unreachable ();
-  }
-
-  void visit (TyTy::FloatType &type) override
-  {
-    switch (type.get_float_kind ())
-      {
-      case TyTy::FloatType::F32:
-	translated = backend->named_type ("f32", backend->float_type (32),
-					  Linemap::predeclared_location ());
-	return;
-
-      case TyTy::FloatType::F64:
-	translated = backend->named_type ("f64", backend->float_type (64),
-					  Linemap::predeclared_location ());
-	return;
-      }
-
-    gcc_unreachable ();
-  }
-
-  void visit (TyTy::USizeType &) override
-  {
-    translated = backend->named_type (
-      "usize", backend->integer_type (true, backend->get_pointer_size ()),
-      Linemap::predeclared_location ());
-  }
-
-  void visit (TyTy::ISizeType &) override
-  {
-    translated = backend->named_type (
-      "isize", backend->integer_type (false, backend->get_pointer_size ()),
-      Linemap::predeclared_location ());
-  }
-
-  void visit (TyTy::CharType &) override
-  {
-    translated = backend->named_type ("char", backend->wchar_type (),
-				      Linemap::predeclared_location ());
-  }
-
-  void visit (TyTy::StrType &) override
-  {
-    tree raw_str = backend->raw_str_type ();
-    translated
-      = backend->named_type ("str", raw_str, Linemap::predeclared_location ());
-  }
-
-  void visit (TyTy::NeverType &) override
-  {
-    translated = backend->unit_type ();
-  }
-
-  void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); }
-
-  void visit (TyTy::ClosureType &) override { gcc_unreachable (); }
-
-private:
-  TyTyCompile (::Backend *backend)
-    : backend (backend), translated (nullptr),
-      mappings (Analysis::Mappings::get ())
-  {}
-
-  ::Backend *backend;
-  tree translated;
-  Analysis::Mappings *mappings;
-};
-
-} // namespace Compile
-} // namespace Rust
-
-#endif // RUST_COMPILE_TYTY
diff --git a/gcc/rust/backend/rust-mangle.h b/gcc/rust/backend/rust-mangle.h
index 03e1dc622a1..6d5a64f8bce 100644
--- a/gcc/rust/backend/rust-mangle.h
+++ b/gcc/rust/backend/rust-mangle.h
@@ -17,7 +17,8 @@
 #ifndef RUST_MANGLE_H
 #define RUST_MANGLE_H
 
-#include "rust-compile-tyty.h"
+#include "rust-system.h"
+#include "rust-tyty.h"
 
 namespace Rust {
 namespace Compile {


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:48 [gcc/devel/rust/master] Canonicalize types based on hashing than HirIds and TyTy equality Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).