From patchwork Tue Jan 31 13:27:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 63994 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9D7D3AA980C for ; Tue, 31 Jan 2023 13:24:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id B6AB33836931 for ; Tue, 31 Jan 2023 13:23:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6AB33836931 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x329.google.com with SMTP id j29-20020a05600c1c1d00b003dc52fed235so5018609wms.1 for ; Tue, 31 Jan 2023 05:23:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=a9h0j59/qszUM37MGzWMZBjMHGpd5COVI59zMDKwp3s=; b=J0zt/+d4kMbLF7eyscUO8vJs6IQ+H5DBdTHt32LWpG+gPTCM1YQwMyP23t6yEjL8b6 qTZPq8RTwE2nngk8UqIoYu4AyiBau2WckynOhcSFZYLoeTEEpN7zg05eAgwDmAMrlRzI JE1X/igekVQBcT0GphQQgxKjarw2jNv73rGusy06JqQG1F58eML6E+tFCHsWs/Di+nHO R6g8v6rSR6BxosmecnizsU6YDQy/h5e/SI2uTV993QTFQ7nRrEz0P62tIec31JM11qER QMEiMMdMMPatgnfEJ06RW8/TeoRqq9k3I03AjZJ6S+UUyMt3ybAmHHWIPNlh5TsQTTol LRNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=a9h0j59/qszUM37MGzWMZBjMHGpd5COVI59zMDKwp3s=; b=EN6C/u/t1MrwbhWMBT9WSAZvA5DyaOhjpn4j1vEv++2iLzFBv7VX6yZCIcL2jkSEBl 7IxivbjPo4qAmHwR+WbCGKF3+8HYsULP8cTxxXMWQlPtFTQDleJ3tPIZhtRLnSKocgiL jBtG9m99X+Isfp4LwwrEI5rWQzrB7SWwYy69iT6AltnDUdKb65158wYiNrktl9l3Z+Ht gu59MgE8WzGug2406xn5ap+hYu+M1mDQtjusU1d27W6104u/6J/CTq87C4U+3hW631YV CZsweXf61b/9Mbb+wFK2E8aPpYFa9jrnMIZaFna1ERgjM1YoyNONlfW3GuMdK0H+5C7I KYjg== X-Gm-Message-State: AO0yUKUKhPv/xIlrLcj7GEMM1PwIzbIKcsxVW8k9MB+2kJC85E1ISF7M odK4VdYO5Ayu9andDlblGC1bjp4e2vQRcl8L0g== X-Google-Smtp-Source: AK7set8o+RreY/hW0XQ2/LSaPUjkIu2hYs0O9fW6Huww6eOrSVb3kfVxrM3DWEz2WqDZKh19y3tp4g== X-Received: by 2002:a05:600c:3d96:b0:3dd:af7a:53db with SMTP id bi22-20020a05600c3d9600b003ddaf7a53dbmr1373531wmb.11.1675171418291; Tue, 31 Jan 2023 05:23:38 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id w8-20020a05600c2a0800b003db06224953sm16612000wme.41.2023.01.31.05.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:23:37 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [COMMITTED] gccrs: Fix duplicated function generation on higher ranked trait bounds Date: Tue, 31 Jan 2023 14:27:23 +0100 Message-Id: <20230131132723.663982-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-15.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Philip Herron Deuplicate function elimination can fail when we compile helpers during higher ranked trait bound monomorphization. This because the TyTy::BaseType info can be lost/reset during the compilation process. This adds a second mechanism to match based on the manged names which is a bit more reliable. This patch is required since the query based refactor of the type system so this issue was likely hidden to to using duplicated type info for higher ranked trait bounds. gcc/rust/ChangeLog: * backend/rust-compile-context.h: Add new optional `asm_name` string argument to `lookup_function_decl`. * backend/rust-compile-item.cc (CompileItem::visit): Compute assembly name and pass it to `lookup_function_decl` when calling it. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/rust/backend/rust-compile-context.h | 21 ++++++++++++++++++++- gcc/rust/backend/rust-compile-item.cc | 14 ++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 2d379c2a5fa..49f78e19b20 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -148,7 +148,8 @@ public: } bool lookup_function_decl (HirId id, tree *fn, DefId dId = UNKNOWN_DEFID, - const TyTy::BaseType *ref = nullptr) + const TyTy::BaseType *ref = nullptr, + const std::string &asm_name = std::string ()) { // for for any monomorphized fns if (ref != nullptr) @@ -163,11 +164,29 @@ public: { const TyTy::BaseType *r = e.first; tree f = e.second; + if (ref->is_equal (*r)) { *fn = f; return true; } + + if (DECL_ASSEMBLER_NAME_SET_P (f) && !asm_name.empty ()) + { + tree raw = DECL_ASSEMBLER_NAME_RAW (f); + const char *rptr = IDENTIFIER_POINTER (raw); + + bool lengths_match_p + = IDENTIFIER_LENGTH (raw) == asm_name.size (); + if (lengths_match_p + && strncmp (rptr, asm_name.c_str (), + IDENTIFIER_LENGTH (raw)) + == 0) + { + *fn = f; + return true; + } + } } return false; } diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc index d1cdc3b6698..b2e9b3fbf6d 100644 --- a/gcc/rust/backend/rust-compile-item.cc +++ b/gcc/rust/backend/rust-compile-item.cc @@ -134,11 +134,18 @@ CompileItem::visit (HIR::Function &function) } } + const Resolver::CanonicalPath *canonical_path = nullptr; + bool ok = ctx->get_mappings ()->lookup_canonical_path ( + function.get_mappings ().get_nodeid (), &canonical_path); + rust_assert (ok); + + const std::string asm_name = ctx->mangle_item (fntype, *canonical_path); + // items can be forward compiled which means we may not need to invoke this // code. We might also have already compiled this generic function as well. tree lookup = NULL_TREE; if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup, - fntype->get_id (), fntype)) + fntype->get_id (), fntype, asm_name)) { // has this been added to the list then it must be finished if (ctx->function_completed (lookup)) @@ -160,11 +167,6 @@ CompileItem::visit (HIR::Function &function) fntype->override_context (); } - const Resolver::CanonicalPath *canonical_path = nullptr; - bool ok = ctx->get_mappings ()->lookup_canonical_path ( - function.get_mappings ().get_nodeid (), &canonical_path); - rust_assert (ok); - if (function.get_qualifiers ().is_const ()) ctx->push_const_context ();