From patchwork Tue Jun 2 08:45:33 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 136280 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 14A674BA2E39 for ; Tue, 2 Jun 2026 09:27:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14A674BA2E39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=X6KqZbbT X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id BDF854BA23E2 for ; Tue, 2 Jun 2026 08:46:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BDF854BA23E2 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BDF854BA23E2 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780389995; cv=none; b=l4pStV8MUgd//SXDxllqhpn5vGQOzG04P+3tQJdiY7FWZ/ctvgtTGfHPTB02jUDDeelUFhZD7KJ8QOMJPXaLERDa5A7LJyI6hWNKpnes8pkFhuc1jF3SCFYYp0S+jRYMdrAw4HEqvhLNCkFafuu9lqJUCPwfNOa4wHbQECusL9g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780389995; c=relaxed/simple; bh=UHMpeNIHOuDMwzUzNzfLi/ZN3k6Uzy8SxJvBywrK8pU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Rmo2QE3+18xUeVq+PBfBdHcWePAVEKKdxF2NqBMxqoIUymEduROrJoxximODGPS4HT5oV9oL/UQkH+ErzIWK2Z7td4fV7TqCovnsWmLtJmbI+If4Wbrzg7c9zlbCUofAlkPts1JfxzK3rWBYxrI54fyk9tagRzaA5DZ4fxAbbmI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=X6KqZbbT DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDF854BA23E2 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-490686877a1so66170945e9.0 for ; Tue, 02 Jun 2026 01:46:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1780389994; x=1780994794; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9/b7tf5Nukm2eEQ+/MdOEGXlnFakgKCsPaHWCERMNfM=; b=X6KqZbbTlP1Oc1+A9umNj/MaGFFqwj9Pb25fQxQ6GdE/VUXEvcng1lhKMWJnvszBCT h0jlA85o8d2jwDUaOWJRBpWEe3Wy4avvi6Eo1EtZ9/TSQ/2wSE5uQy8UIRwlZp27pkLa WD9rd2KSQOmwln/5UT0/yfPkIJGT1PC1X/OmKXJgaN7XmyQ5D4LTN5uCOIMREPwN3gB2 UhjL4oX6Ul7eaW9Sed6SQH9Nq73ah30RBiFZ1OLhI/P2vJ5mgR7EGKrWUBhY5EYJGUmL CEiu34jmtXyIeaSUySQY0wXhPQ0b/OsjTEfl2//Mr2L20xdeapRdJ4dR1bZFTSnE/J0t uuIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780389994; x=1780994794; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=9/b7tf5Nukm2eEQ+/MdOEGXlnFakgKCsPaHWCERMNfM=; b=WAnkwrBVIvf1zk20TIINrAnEjELh/WCSrpn3j0V0QapBHuj5S4mtncsMJyypwGq9u0 /Ne54aJat+S/doPZ7oFcfNiGzPR8T3+uP9GQevFFwwWwQ+oD+NKbYepYst79vBLPjss+ YEIQB3Plqmu+DJxdpxv8USXaOLx7GL8G8gRgeD2TcupVMl0DI6VPfdX23QEBJoluytoi IDMD+9mqnYm0kD4s/bvhsQ3Od56rZhSvG5F5ZVks8G1lyXch4tvR6AK9Gboil6KgH7s5 2C7gNhY8qVFq2wxF/vzhTkcObeF3GURkckHjSeqYZOF8YBnczT9k96caRW2CtrgdZ5+Q 1Yqw== X-Gm-Message-State: AOJu0Yw6meRQ8HC8H2lwZ8Popg0ZjsNqWD/mdtm8MJ/xiiJNvvzbmiw8 tfXGUJMf7MhXvlNlc/fFN/j/EhIR4GHwGncFNo7vCLdiaK2Lt6MD6amSTWcpgpoNAMzpX/TobOE kgm0= X-Gm-Gg: Acq92OGvajX6UtIcrRQznSSfjl+cngyE/RveFIZdWg1v9fvU0AxGechebjO4T0vU96K ywUUFi+ux4hx5Odjtihr9JpGegPO5Wl7btmIQWdrbrkCJT2PEImeeDtUpxAkI6A4wqvk/F8nvjq MbjxrDNVHXh04vHQjUhZ4dmqsaAE/Lnk7wUmTu3g8L8On5kDatmL8+i/iQSIHblE/qIbLJyA13D vkoozqBsYnCUk4E0JNhVegoKIsM6uQv+UVDlwzuVy19TK0KmTMUkXgmnZwCvvl79MEwam6FCGrH 3VkJX6p8Ry+nxyoksHwU8ao/iY2M62kec1XmlGiIFHj6xw+2lzYL1YOpb5eL5CgNRHrESs3zaCv oGIxrDKy7BF02iOAtWbOdff8Xbbi8CGw3VEpBmxVKVBKu+0J856Rf5n0pnOsRk2cs/giXc/Vj1K e/sUpo+B+4qxMyUvlZct4/seuZcClZ1K7N2kSeCRYsENflTw8UpnAjgQKWFAuMZkfd5Wur6009+ UCsKJS/eQ7XXR7tReICHks0MJVsD1w= X-Received: by 2002:a05:600c:c0d1:20b0:490:4b89:5372 with SMTP id 5b1f17b1804b1-490b0e80be1mr32966695e9.11.1780389993672; Tue, 02 Jun 2026 01:46:33 -0700 (PDT) Received: from mecano.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490ab55d39csm33907625e9.35.2026.06.02.01.46.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2026 01:46:33 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 45/51] ada: Adjust RESULT_DECL of functions after updating their profile if needed Date: Tue, 2 Jun 2026 10:45:33 +0200 Message-ID: <20260602084541.3829876-45-poulhies@adacore.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260602084541.3829876-1-poulhies@adacore.com> References: <20260602084541.3829876-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 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 From: Eric Botcazou Adjusting the RESULT_DECL of functions is necessary before handing them over to the middle-end when they return by invisible reference and are defined in the current translation unit, so that's done in Subprogram_Body_to_gnu. That's also needed when the functions are generated by the middle-end, like GNU thunks. But, in this case, the transformation can only be applied when the full view of the type is available, which is not guaranteed by the time maybe_make_gnu_thunk is invoked, so the transformation needs to be delayed and applied by update_profile when the full view is eventually encountered. gcc/ada/ChangeLog: PR ada/89609 * gcc-interface/gigi.h (adjust_result_decl_for_invisible_reference): New inline function. * gcc-interface/decl.cc (update_profile): Adjust the RESULT_DECL of the function if it returns by invisible reference. * gcc-interface/trans.cc (Subprogram_Body_to_gnu): Replace manual adjustment by a call to adjust_result_decl_for_invisible_reference. (maybe_make_gnu_thunk): Likewise. * gcc-interface/targtyps.cc: Include stor-layout.h header file. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/gcc-interface/decl.cc | 3 +++ gcc/ada/gcc-interface/gigi.h | 24 ++++++++++++++++++++++++ gcc/ada/gcc-interface/targtyps.cc | 1 + gcc/ada/gcc-interface/trans.cc | 25 ++++--------------------- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc index 7f39113186f..d9b70019c4a 100644 --- a/gcc/ada/gcc-interface/decl.cc +++ b/gcc/ada/gcc-interface/decl.cc @@ -6018,6 +6018,9 @@ update_profile (Entity_Id gnat_subprog) DECL_ARGUMENTS (gnu_subprog) = gnu_param_list; finish_subprog_decl (gnu_subprog, gnu_ext_name, gnu_type); + + /* If the function returns by invisible reference, make it explicit. */ + adjust_result_decl_for_invisible_reference (gnu_subprog); } } diff --git a/gcc/ada/gcc-interface/gigi.h b/gcc/ada/gcc-interface/gigi.h index 793d7145b5e..f4d79119518 100644 --- a/gcc/ada/gcc-interface/gigi.h +++ b/gcc/ada/gcc-interface/gigi.h @@ -1208,6 +1208,30 @@ return_type_with_variable_size_p (tree type) return false; } +/* Adjust RESULT_DECL of a function that returns by invisible reference. + + Adjusting the RESULT_DECL of functions is needed before handing them over + to the middle-end when they return by invisible reference and are defined + in the current translation unit, which is done in Subprogram_Body_to_gnu. + + That's also needed when the functions are generated by the middle-end, like + GNU thunks. But, in this case, the transformation can only be applied when + the full view of the type is available, which is not guaranteed by the time + maybe_make_gnu_thunk is invoked, so the transformation needs to be delayed + and applied by update_profile when the full view is eventually seen. */ + +static inline void +adjust_result_decl_for_invisible_reference (tree subprog) +{ + if (TREE_ADDRESSABLE (TREE_TYPE (subprog)) + && TREE_CODE (TREE_TYPE (DECL_RESULT (subprog))) != REFERENCE_TYPE) + { + TREE_TYPE (DECL_RESULT (subprog)) + = build_reference_type (TREE_TYPE (DECL_RESULT (subprog))); + relayout_decl (DECL_RESULT (subprog)); + } +} + /* Return the unsigned version of TYPE_NODE, a scalar type. */ static inline tree diff --git a/gcc/ada/gcc-interface/targtyps.cc b/gcc/ada/gcc-interface/targtyps.cc index 4fc97cfb3fd..4768322e15a 100644 --- a/gcc/ada/gcc-interface/targtyps.cc +++ b/gcc/ada/gcc-interface/targtyps.cc @@ -31,6 +31,7 @@ #include "tm.h" #include "target.h" #include "tree.h" +#include "stor-layout.h" #include "ada.h" #include "types.h" diff --git a/gcc/ada/gcc-interface/trans.cc b/gcc/ada/gcc-interface/trans.cc index 6eca5a49cd5..f66d0b99ba3 100644 --- a/gcc/ada/gcc-interface/trans.cc +++ b/gcc/ada/gcc-interface/trans.cc @@ -3995,17 +3995,8 @@ Subprogram_Body_to_gnu (Node_Id gnat_node) else gnu_return_var_elmt = NULL_TREE; - /* If the function returns by invisible reference, make it explicit in the - function body, but beware that maybe_make_gnu_thunk may already have done - it if the function is inlined across units. See gnat_to_gnu_subprog_type - for more details. */ - if (TREE_ADDRESSABLE (gnu_subprog_type) - && TREE_CODE (TREE_TYPE (gnu_result_decl)) != REFERENCE_TYPE) - { - TREE_TYPE (gnu_result_decl) - = build_reference_type (TREE_TYPE (gnu_result_decl)); - relayout_decl (gnu_result_decl); - } + /* If the function returns by invisible reference, make it explicit. */ + adjust_result_decl_for_invisible_reference (gnu_subprog); /* Set the line number in the decl to correspond to that of the body. */ if (DECL_IGNORED_P (gnu_subprog)) @@ -11514,16 +11505,8 @@ maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk) indirect_offset = (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT); } - /* If the target returns by invisible reference and is external, apply the - same transformation as Subprogram_Body_to_gnu here. */ - if (TREE_ADDRESSABLE (TREE_TYPE (gnu_target)) - && DECL_EXTERNAL (gnu_target) - && TREE_CODE (TREE_TYPE (DECL_RESULT (gnu_target))) != REFERENCE_TYPE) - { - TREE_TYPE (DECL_RESULT (gnu_target)) - = build_reference_type (TREE_TYPE (DECL_RESULT (gnu_target))); - relayout_decl (DECL_RESULT (gnu_target)); - } + /* If the function returns by invisible reference, make it explicit. */ + adjust_result_decl_for_invisible_reference (gnu_target); /* The thunk expander requires the return types of thunk and target to be compatible, which is not fully the case with the CICO mechanism. */