diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-05-13 11:50:31 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-05-13 12:56:30 +0100 |
commit | d326288aa4f32707f1340e2f39b56bd6ab608802 (patch) | |
tree | 8f987d715d879f369620061a5e2d4c336f5bae38 /gcc/rust/backend/rust-compile-context.h | |
parent | a1c148518de1cb8f60f779dc206f663e8593191a (diff) | |
download | gcc-d326288aa4f32707f1340e2f39b56bd6ab608802.zip gcc-d326288aa4f32707f1340e2f39b56bd6ab608802.tar.gz gcc-d326288aa4f32707f1340e2f39b56bd6ab608802.tar.bz2 |
Fix duplicated function generation for generics
When emitting the backend IR we need to check if we have already compiled
one already. This is all due to the fact when we do generic substitutions
in type resolution its only upon usage of a function the function is
substituted it gains a new unique ty_ref HIR ID to make sure new
substituted versions of a type have unique ids.
This means we can end up with multiple substitued versions of the same
function which end up being compiled multiple times. This was also the case
for generic data types but is already fixed.
Fixes #403
Diffstat (limited to 'gcc/rust/backend/rust-compile-context.h')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 7db4ea2..45fb6c2 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -157,13 +157,38 @@ public: return true; } - void insert_function_decl (HirId id, ::Bfunction *fn) + void insert_function_decl (HirId id, ::Bfunction *fn, + const TyTy::BaseType *ref) { + rust_assert (compiled_fn_map.find (id) == compiled_fn_map.end ()); compiled_fn_map[id] = fn; + if (ref != nullptr) + { + std::pair<HirId, ::Bfunction *> elem (id, fn); + mono_fns[ref] = std::move (elem); + } } - bool lookup_function_decl (HirId id, ::Bfunction **fn) + bool lookup_function_decl (HirId id, ::Bfunction **fn, + const TyTy::BaseType *ref = nullptr) { + // for for any monomorphized fns + if (ref != nullptr) + { + for (auto it = mono_fns.begin (); it != mono_fns.end (); it++) + { + std::pair<HirId, ::Bfunction *> &val = it->second; + const TyTy::BaseType *r = it->first; + if (ref->is_equal (*r)) + { + *fn = val.second; + + return true; + } + } + return false; + } + auto it = compiled_fn_map.find (id); if (it == compiled_fn_map.end ()) return false; @@ -282,6 +307,7 @@ private: std::vector< ::Bvariable *> loop_value_stack; std::vector< ::Blabel *> loop_begin_labels; std::map<const TyTy::BaseType *, std::pair<HirId, ::Btype *> > mono; + std::map<const TyTy::BaseType *, std::pair<HirId, ::Bfunction *> > mono_fns; // To GCC middle-end std::vector< ::Btype *> type_decls; |