From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 7F5993858288 for ; Tue, 27 Jun 2023 12:08:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F5993858288 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-313e714342cso3529606f8f.0 for ; Tue, 27 Jun 2023 05:08:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1687867685; x=1690459685; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HHkkSjg+tIVUfHWXly3Q1IUmKVGyvFPvsV2JiygW88A=; b=UjT1NWPg620qk6R3geA2nAdmabrHHN5OZFSVKqF576WVnMwOG2Qeo00gjYkVVOzWwo Lv/CSDxnz73Gc6w1nxn/Zm3Ybg7o5a/aOD5V5OH6mEufdLuoSMNIYW735OopggXIGsUM cJ+PppsqsnVE0JKxZYNWfjvTir0KzejTqtrp84u/uPopwXp55fKmoncwh2aPP0HipvdR 5emV799sGFjcWNmZshjJjMeKPHCPuNkuT08F0qhKHgctmOwL1HNO76LgYB3rfBBLp5AC bc9a5ROCH75Gdy6q6PNRUbgf4n5NyswXg0gnSGiwYHZqNXpHQjW8POhoPOPIAa2pGRu1 feRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687867685; x=1690459685; 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=HHkkSjg+tIVUfHWXly3Q1IUmKVGyvFPvsV2JiygW88A=; b=lZVnePnGIcdrOGvb/wBI2atUmG3lFvloz/Hnalop2XcRVpLpPDs8Z2nkHSyMYezcxa D22+zrkcgYOY1l1oHOsRtepXH/FUkru79a2wE7UDqotIAB+Pf6BKC0L2hhEWVHvjRvnc bNunx8s4HyHMcdflqGT1F4FNTj7fzh3Ln/NZokzO0z5F5ZuL6+DBIoZFQhfoqEErmd3X IlRiKULRRRwef4CcdX0Fq+EvL4jh1Woouk4AzNlJn4w4UJl+vvB0mCV8OBEOQzgIfivC MSuy70J/i0vjrBAy5GZC+7NO3G0RKxbiKqZa69gyiq3QPhjIxi/hWCoOraXxw1aI9fhL nG4Q== X-Gm-Message-State: AC+VfDyGAQG+VEjmH8Atza+HxN07XAjPUY6OaamPzVffv9KVEcdNRNTG XKSc9gbuie05wDOXeTneL0QyN8Fdd+blQP4Afb0t2w== X-Google-Smtp-Source: ACHHUZ64TfhILik72wFgSJJIbCk3sRJm5qW6Tsz9w36jHmsiqUAIC3Rt6z6abMn/A/Tx5Aa4YHCb6A== X-Received: by 2002:a05:6000:181b:b0:313:f783:2627 with SMTP id m27-20020a056000181b00b00313f7832627mr3635096wrh.35.1687867685204; Tue, 27 Jun 2023 05:08:05 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id t15-20020a0560001a4f00b00313f676832bsm4050648wry.93.2023.06.27.05.08.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 05:08:04 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix too late finalization and secondary stack release in iterator loops Date: Tue, 27 Jun 2023 14:08:03 +0200 Message-Id: <20230627120803.3419993-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Eric Botcazou Sem_Ch5 contains an entire machinery to deal with finalization actions and secondary stack releases around iterator loops, so this removes a recent fix that was made in a narrower case and instead refines the condition under which this machinery is triggered. As a side effect, given that finalization and secondary stack management are still entangled in this machinery, this also fixes the counterpart of a leak for the former, which is a finalization occurring too late. gcc/ada/ * exp_ch4.adb (Expand_N_Quantified_Expression): Revert the latest change as it is subsumed by the machinery in Sem_Ch5. * sem_ch5.adb (Prepare_Iterator_Loop): Also wrap the loop statement in a block in the name contains a function call that returns on the secondary stack. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch4.adb | 26 -------------------------- gcc/ada/sem_ch5.adb | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index 7b6e997e3e7..fdaeb50512f 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -11116,32 +11116,6 @@ package body Exp_Ch4 is Freeze_Before (P, Etype (Var)); end; - -- For an expression of the form "for all/some X of F(...) => ...", - -- where F(...) is a function call that returns on the secondary stack, - -- we need to mark an enclosing scope as Uses_Sec_Stack. We must do - -- this before expansion, which can obscure the tree. Note that we - -- might be inside another quantified expression. Skip blocks and - -- loops that were generated by expansion. - - if Present (Iterator_Specification (N)) - and then Nkind (Name (Iterator_Specification (N))) = N_Function_Call - and then Needs_Secondary_Stack - (Etype (Name (Iterator_Specification (N)))) - then - declare - Source_Scope : Entity_Id := Current_Scope; - begin - while Ekind (Source_Scope) in E_Block | E_Loop - and then not Comes_From_Source (Source_Scope) - loop - Source_Scope := Scope (Source_Scope); - end loop; - - Set_Uses_Sec_Stack (Source_Scope); - Check_Restriction (No_Secondary_Stack, N); - end; - end if; - -- Create the declaration of the flag which tracks the status of the -- quantified expression. Generate: diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb index fa36a5a0741..72e7d186baa 100644 --- a/gcc/ada/sem_ch5.adb +++ b/gcc/ada/sem_ch5.adb @@ -91,9 +91,14 @@ package body Sem_Ch5 is function Has_Sec_Stack_Call (N : Node_Id) return Boolean; -- N is the node for an arbitrary construct. This function searches the - -- construct N to see if any expressions within it contain function - -- calls that use the secondary stack, returning True if any such call - -- is found, and False otherwise. + -- construct N to see if it contains a function call that returns on the + -- secondary stack, returning True if any such call is found, and False + -- otherwise. + + -- ??? The implementation invokes Sem_Util.Requires_Transient_Scope so it + -- will return True if N contains a function call that needs finalization, + -- in addition to the above specification. See Analyze_Loop_Statement for + -- a similar comment about this entanglement. procedure Preanalyze_Range (R_Copy : Node_Id); -- Determine expected type of range or domain of iteration of Ada 2012 @@ -3626,9 +3631,13 @@ package body Sem_Ch5 is Cont_Typ := Etype (Nam_Copy); -- The iterator loop is traversing an array. This case does not - -- require any transformation. + -- require any transformation, unless the name contains a call + -- that returns on the secondary stack since we need to release + -- the space allocated there. - if Is_Array_Type (Cont_Typ) then + if Is_Array_Type (Cont_Typ) + and then not Has_Sec_Stack_Call (Nam_Copy) + then null; -- Otherwise unconditionally wrap the loop statement within -- 2.40.0