diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-05-20 13:35:54 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-05-20 15:57:22 +0100 |
commit | 39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee (patch) | |
tree | 802a3e75033824c712556d092fb5d36760d9c399 /gcc/rust/backend/rust-compile-tyty.h | |
parent | 5ad0ea3e0ed288569d52556b9aa796beea73d8a3 (diff) | |
download | gcc-39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee.zip gcc-39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee.tar.gz gcc-39ae84825c5cc15edbb2fa9f5ecc70f93870e3ee.tar.bz2 |
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.
Diffstat (limited to 'gcc/rust/backend/rust-compile-tyty.h')
-rw-r--r-- | gcc/rust/backend/rust-compile-tyty.h | 260 |
1 files changed, 0 insertions, 260 deletions
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h deleted file mode 100644 index 52ad2f9..0000000 --- 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 ¶ms : 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 |