From patchwork Tue Sep 16 08:22:56 2025 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: 120339 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 E579C385734D for ; Tue, 16 Sep 2025 08:24:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E579C385734D 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=WNGUV+9E X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 2259D385734D for ; Tue, 16 Sep 2025 08:23:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2259D385734D 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 2259D385734D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758010990; cv=none; b=vrjTqI0zkorFB/bP5Is8q70KvAXkSEx1bjoa3CdIbtsLaPFH6XZ6hTwaWbmR4OKk9meSYhAwVpIf9Gfbx84yg2i/TmnJidXLx1EaltssrjV7huoBtUoYTpsTuAdS1Qd+I4bGsEprj34WYWNswJe2SH58pjY+jToy6ofyDl5gD/o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758010990; c=relaxed/simple; bh=hSSoAszZ5jOdhUax/fqAxZDncmo0Hoqql2GBx+KJKQw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aOgNXLr5geenVlqLYCqveK0jbGQPbNA7IyYDIbONH/VDl+lGt9IRwSF6oqawT0JSEje+KtHRWQE8hKK91jJqeDHEGiJxmTBwedNbwvKexXgmczgD6LdPtn156nfDwOq+u1IBjJ84WMJsgMbU2pfA9hNsnoJNdoJI6ETohdaGaKI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2259D385734D Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-45decc9e83eso56064105e9.3 for ; Tue, 16 Sep 2025 01:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1758010989; x=1758615789; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=AMhbnkbtv5MCpwGqV+KwYbViFZjERvuvY5bEM/3eSxA=; b=WNGUV+9Ex92NRThOIgqjdzgi9ja6gSB/nBqi6AasFP/iPa2lTuneMlBqhhb+4UMNMV TU2Q5tn0zPsFwLQ67myyxkQDgTiKbGmJh4lKtPVfc3ezZoYXZ/MLG0sKlFcqc9gwNX+8 haREMu7k1sBUj0AdvBYGrANXjfdQo5J3dN5EJWdniN0JmWroBEWP6kuxDqQ1dX38N1ko g68PK9n0RdK2b3AS2Rrtg+n2TxEC4K5BuEGmNg+qmSYr1ZwVRNzLoICxRRr9UpMwU83h 7Ob5gJRoKk1vXP8wDI/C7ytAzVGhbxowTDV9x736A334dZNpTietdskHX8Ih2wPTfku/ 1Qxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758010989; x=1758615789; 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=AMhbnkbtv5MCpwGqV+KwYbViFZjERvuvY5bEM/3eSxA=; b=d0jWWWRRBEx0T0KBSHaqmpfVOmd+3UB9xTpKOCq0vzLzsrLmkwaH1UeaMHL5u4M8d6 3ZWQXPW2wphLWETmd6qJkz5DmLIWqokmMp6Bq90x3hAN47QJvEpeyVo9ulN3yB4Bex2o zcuWBsIzKeZBEjABZajG70ipt3IoNiWkZHBhBj3mN1zLlW9+Umlu4ua9z4tvYEn9CtjZ RTrR42J0urVBZcwCWxsnQZjiFRIAzxZz40nScaAm9LhFWQC9LLo6Tapocmq2bIsQ+sNr S1UDN+6Al0N4z256/LWHXSLJcTAdZ5Oi6w3IcYQezaq24fi2ecVH6Zc+/UD60dYiYtBW crAQ== X-Gm-Message-State: AOJu0YzbTCx83APaMK0zAz9am/ynCSP5W3VZjlK9PYwb9RIuLc8iFFxT 1cAXUedS6BQpaQnTjE8qDTDEMjOM72+m5tCoA955+B/S25fuu0Ish8AdQZuALl4NeMgJtmxmeov K/lY= X-Gm-Gg: ASbGncsUH3lIyCj7AQWqxmnIshEXdX6Maeux6UvIHybp7mVVYZKCN7migfB0nGNcQts DlhB6sTtVehAxC1ZFm+OyrABgCJmJdqLz5UuOl3/j/fmI/I9psyt8OeWMZMGgGRNi7GKnfmKHyQ uXFOkVkVV0ebzx5OTVZB+ZIVHmYrsDthatB3duS/oucCp/0xb7tH+Am9k1ltSMZdMV0nVKMxYLi JsDIXlHHXvNjrfNeKfTkbg2t3EVXcChm8rhlF9/lQoS7S66mrrA2+PmMfcGbYFB1h30oPv9b0vn pTI/+g9Isk6dlm3FI19QyH3wOrXmoYAisYdvGiUsOWq/Zyey4lyKrTIIe26aiV3fP5gGUsl570U fXMERBDMYsSvR2jU0XynICNjGkern0VEzY9AqZyU= X-Google-Smtp-Source: AGHT+IETw3/DQ2wKoUNuUWnzc+bo3cAjNeXDbe0NW8lvF63zIS9rzuCnXBHMTYIDAjv7M8gAnZLxVQ== X-Received: by 2002:a05:600c:1507:b0:45c:b549:2241 with SMTP id 5b1f17b1804b1-45f21200dcamr78161395e9.27.1758010988547; Tue, 16 Sep 2025 01:23:08 -0700 (PDT) Received: from localhost.localdomain ([2a01:cb15:8541:af00:f92d:585:510e:3226]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45f3252260csm12826285e9.2.2025.09.16.01.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Sep 2025 01:23:07 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Gary Dismukes Subject: [COMMITTED 1/2] ada: Revert "Remove dependence on secondary stack for type with controlled component" Date: Tue, 16 Sep 2025 10:22:56 +0200 Message-ID: <20250916082257.2900903-1-poulhies@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 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=ham 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.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: Gary Dismukes This reverts commit 91b51fc42b167eedaaded6360c490a4306bc5c55. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch6.adb | 49 ++++++++++++++++++++++++++++++--------------- gcc/ada/exp_ch6.ads | 6 ++++-- gcc/ada/exp_ch7.adb | 20 +++++++++--------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index 58361e10bd9c..5056b1f990fa 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -9093,6 +9093,27 @@ package body Exp_Ch6 is -- tagged, the called function itself must perform the allocation of -- the return object, so we pass parameters indicating that. + -- But that's also the case when the result subtype needs finalization + -- actions because the caller side allocation may result in undesirable + -- finalization. Consider the following example: + -- + -- function Make_Lim_Ctrl return Lim_Ctrl is + -- begin + -- return Result : Lim_Ctrl := raise Program_Error do + -- null; + -- end return; + -- end Make_Lim_Ctrl; + -- + -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl); + -- + -- Even though the size of limited controlled type Lim_Ctrl is known, + -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's + -- finalization collection. The subsequent call to Make_Lim_Ctrl will + -- fail during the initialization actions for Result, which means that + -- Result (and Obj by extension) should not be finalized. However Obj + -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope + -- since it is already attached on the its finalization collection. + if Needs_BIP_Alloc_Form (Function_Id) then Temp_Init := Empty; @@ -9257,7 +9278,11 @@ package body Exp_Ch6 is end if; end; - -- Add implicit actuals for the BIP formal parameters, if any + -- When the function has a controlling result, an allocation-form + -- parameter must be passed indicating that the caller is allocating + -- the result object. This is needed because such a function can be + -- called as a dispatching operation and must be treated similarly + -- to functions with unconstrained result subtypes. Add_Unconstrained_Actuals_To_Build_In_Place_Call (Func_Call, @@ -9282,14 +9307,6 @@ package body Exp_Ch6 is Add_Access_Actual_To_Build_In_Place_Call (Func_Call, Function_Id, Return_Obj_Actual); - -- If the allocation is done in the caller, create a custom Allocate - -- procedure if need be. - - if not Needs_BIP_Alloc_Form (Function_Id) then - Build_Allocate_Deallocate_Proc - (Declaration_Node (Return_Obj_Access), Mark => Allocator); - end if; - -- Finally, replace the allocator node with a reference to the temp Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc)); @@ -9751,7 +9768,7 @@ package body Exp_Ch6 is -- ensure that the heap allocation can properly chain the object -- and later finalize it when the library unit goes out of scope. - if Needs_BIP_Collection (Function_Id) then + if Needs_BIP_Collection (Func_Call) then Build_Finalization_Collection (Typ => Ptr_Typ, For_Lib_Level => True, @@ -10314,12 +10331,6 @@ package body Exp_Ch6 is Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); begin - -- No need for BIP_Collection if allocation is always done in the caller - - if not Needs_BIP_Alloc_Form (Func_Id) then - return False; - end if; - -- A formal for the finalization collection is needed for build-in-place -- functions whose result type needs finalization or is a tagged type. -- Tagged primitive build-in-place functions need such a formal because @@ -10344,6 +10355,12 @@ package body Exp_Ch6 is Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); begin + -- See Make_Build_In_Place_Call_In_Allocator for the rationale + + if Needs_BIP_Collection (Func_Id) then + return True; + end if; + -- A formal giving the allocation method is needed for build-in-place -- functions whose result type is returned on the secondary stack or -- is a tagged type. Tagged primitive build-in-place functions need diff --git a/gcc/ada/exp_ch6.ads b/gcc/ada/exp_ch6.ads index 3867270e71a9..5919627a4e7e 100644 --- a/gcc/ada/exp_ch6.ads +++ b/gcc/ada/exp_ch6.ads @@ -301,8 +301,10 @@ package Exp_Ch6 is -- BIP_Alloc_Form parameter (see type BIP_Formal_Kind). function Needs_BIP_Collection (Func_Id : Entity_Id) return Boolean; - -- Ada 2005 (AI-318-02): Return True if the function needs an implicit - -- BIP_Collection parameter (see type BIP_Formal_Kind). + -- Ada 2005 (AI-318-02): Return True if the result subtype of function + -- Func_Id might need finalization actions. This includes build-in-place + -- functions with tagged result types, since they can be invoked via + -- dispatching calls, and descendant types may require finalization. function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean; -- Return True if the function returns an object of a type that has tasks. diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb index d60c6edecdff..62e9d2cbb73f 100644 --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -4758,18 +4758,18 @@ package body Exp_Ch7 is -- We mark the secondary stack if it is used in this construct, and -- we're not returning a function result on the secondary stack, except - -- that a build-in-place function that only conditionally returns on - -- the secondary stack will also need a mark. A run-time test for doing - -- the release call is needed in the case where the build-in-place - -- function has a BIP_Alloc_Form parameter (see Create_Finalizer). + -- that a build-in-place function that might or might not return on the + -- secondary stack always needs a mark. A run-time test is required in + -- the case where the build-in-place function has a BIP_Alloc extra + -- parameter (see Create_Finalizer). Needs_Sec_Stack_Mark : constant Boolean := - Uses_Sec_Stack (Scop) - and then - (not Sec_Stack_Needed_For_Return (Scop) - or else - (Is_Build_In_Place_Function (Scop) - and then Needs_BIP_Alloc_Form (Scop))); + (Uses_Sec_Stack (Scop) + and then + not Sec_Stack_Needed_For_Return (Scop)) + or else + (Is_Build_In_Place_Function (Scop) + and then Needs_BIP_Alloc_Form (Scop)); Needs_Custom_Cleanup : constant Boolean := Nkind (N) = N_Block_Statement