From patchwork Thu Jun 13 13:33:16 2024 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: 1947403 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=FxlCT+Qj; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W0Nkj3jc2z1ydW for ; Thu, 13 Jun 2024 23:37:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B86EA388265D for ; Thu, 13 Jun 2024 13:37:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id EE0A9388217C for ; Thu, 13 Jun 2024 13:33:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE0A9388217C 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 EE0A9388217C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::336 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718285640; cv=none; b=kcyQQUNkbZITd+Ug1rtr7m0o1oJTu1u7v/e3drTu/nJ6toHbpGi63br145CcTLVwykcWNh4KEB6NVIlrrONLXvUXhZuc9qrOtkYgJ0hO76BcqkhJbF73aqkqfwkvS2t7x5zvz4qPq0QDs/YablNMel3WJYjuJMaFnfHnFxzsf6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718285640; c=relaxed/simple; bh=qygIfbmQPN5ufwEFZEDKwmZlcL9CjYF67Jd3G3yiVTo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Cz7xQr8PB3OXNgCWICqc2ezfazxqstFaF/nlW6tMSx3eRDzkxDf9htp+DEWOTWecdU2GZliWgYTChoMacb5WVGx17cSpVs+M1QQwcGlFuaRuPp9T2MfRzCXyLwB3+pl1iaE/r0Vc1HRXtwSLuvm8b3fVktZlyiACt9Jr1Fw6Oec= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-42278f3aea4so10426535e9.1 for ; Thu, 13 Jun 2024 06:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1718285638; x=1718890438; 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=+fwcGGztIdAQ0BmLO3vfqo3C7ViiaJz+3H63NQrg0IU=; b=FxlCT+QjcSXOxFqJPC/NGK7kSAuOTn3mTvZwLqZklWpLyAWFZrebmrTO/+wmPt5Z9L EHWVEqFYNNTWLsRTdCiPIx9FbtSlDNO4Q3X74AOTRbihebybt5Oldorj2BO2XxO71o9H /tayZ2t0n8P45K6eCOXHR5Xo4BJ+0zeNgSErzcJjZs5bRGMxElH9EqiYqBQjZaN7Fd3j cIAFI4sQFDEdMCx+qFJTFJuUhHBYIKhmCVvidgQBxmJD30Q9UpJpkIkXN02BX3bSGX/2 jl9WxBHRRIUhL5sWB5V06U9yZhbBNhpdG9IpnMI52yme925RK1vR37+ZDzi3Px8AOifQ jb2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718285638; x=1718890438; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+fwcGGztIdAQ0BmLO3vfqo3C7ViiaJz+3H63NQrg0IU=; b=BYoGoMIz9Xj5O33F263HU1MIyLDyk5AembY3KuGqc6NJebffgJgbjKmzDdRvHKBPpr E9UZgkv098PkWYvr4Fb82+2JTdoIMzrvRC9sux0PPzDzAFA0j6JUYr9hCO875UXjvP3p 0tkWdAXhf8WpYguk/4kB0ilhaC5vTV886fXmBnPcNQiobUUgepUtnWPSlvljLSq58zvr 1D4PtYF+/nHUx4XQl5fVaZpN45Omvmmtdtgr5cHIveWB/cYbO1y+crP2X3+e22qwDYYb xoy6ivBVSZI1h+fSB1E30bm3OOh2ZFH69ep7ZEIz8ATKYjfXEL2X3j+yabJSSvcqPCEu EnFA== X-Gm-Message-State: AOJu0Yy4WJQu7j4TNf3WLZVpmo6anp/N4cJmQX6fc0Fj0qYLVKLIAgNc NYmAG2lYQJkxLHfbcZeGHtun2D8SJWzpHoijixKQl93INTsCBDq9YTre9daJ20K+yLFnlo+fJxE = X-Google-Smtp-Source: AGHT+IEs19Bia2Hzno5DJPNqb91GdpQ5JUX6TtZF77FB2UVLPw4q+BLxpriPXNdcggKzAmiMBKKf7w== X-Received: by 2002:a05:600c:5117:b0:421:7e76:b85c with SMTP id 5b1f17b1804b1-422864aebd5mr38411635e9.23.1718285637757; Thu, 13 Jun 2024 06:33:57 -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 5b1f17b1804b1-422870e9145sm62391955e9.22.2024.06.13.06.33.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 06:33:57 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Yannick Moy Subject: [COMMITTED 10/30] ada: Do not inline subprogram which could cause SPARK violation Date: Thu, 13 Jun 2024 15:33:16 +0200 Message-ID: <20240613133338.1809385-10-poulhies@adacore.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613133338.1809385-1-poulhies@adacore.com> References: <20240613133338.1809385-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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 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+incoming=patchwork.ozlabs.org@gcc.gnu.org From: Yannick Moy Inlining in GNATprove a subprogram containing a constant declaration with an address clause/aspect might lead to a spurious error if the address expression is based on a constant view of a mutable object at call site. Do not allow such inlining in GNATprove. gcc/ada/ * inline.adb (Can_Be_Inlined_In_GNATprove_Mode): Do not inline when constant with address clause is found. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/inline.adb | 83 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/gcc/ada/inline.adb b/gcc/ada/inline.adb index 04cf1194009..8e98fb5ad10 100644 --- a/gcc/ada/inline.adb +++ b/gcc/ada/inline.adb @@ -1094,7 +1094,6 @@ package body Inline is -- If the body of the subprogram includes a call that returns an -- unconstrained type, the secondary stack is involved, and it is -- not worth inlining. - ------------------------- -- Has_Extended_Return -- ------------------------- @@ -1462,6 +1461,14 @@ package body Inline is (Spec_Id : Entity_Id; Body_Id : Entity_Id) return Boolean is + function Has_Constant_With_Address_Clause + (Body_Node : Node_Id) + return Boolean; + -- Returns true if the subprogram contains a declaration of a constant + -- with an address clause, which could become illegal in SPARK after + -- inlining, if the address clause mentions a constant view of a mutable + -- object at call site. + function Has_Formal_Or_Result_Of_Deep_Type (Id : Entity_Id) return Boolean; -- Returns true if the subprogram has at least one formal parameter or @@ -1502,6 +1509,70 @@ package body Inline is -- knowledge of the SPARK boundary is needed to determine exactly -- traversal functions. + -------------------------------------- + -- Has_Constant_With_Address_Clause -- + -------------------------------------- + + function Has_Constant_With_Address_Clause + (Body_Node : Node_Id) + return Boolean + is + function Check_Constant_With_Addresss_Clause + (N : Node_Id) + return Traverse_Result; + -- Returns Abandon on node N if this is a declaration of a constant + -- object with an address clause. + + ----------------------------------------- + -- Check_Constant_With_Addresss_Clause -- + ----------------------------------------- + + function Check_Constant_With_Addresss_Clause + (N : Node_Id) + return Traverse_Result + is + begin + case Nkind (N) is + when N_Object_Declaration => + declare + Obj : constant Entity_Id := Defining_Entity (N); + begin + if Constant_Present (N) + and then + (Present (Address_Clause (Obj)) + or else Has_Aspect (Obj, Aspect_Address)) + then + return Abandon; + else + return OK; + end if; + end; + + -- Skip locally declared subprogram bodies inside the body to + -- inline, as the declarations inside those do not count. + + when N_Subprogram_Body => + if N = Body_Node then + return OK; + else + return Skip; + end if; + + when others => + return OK; + end case; + end Check_Constant_With_Addresss_Clause; + + function Check_All_Constants_With_Address_Clause is new + Traverse_Func (Check_Constant_With_Addresss_Clause); + + -- Start of processing for Has_Constant_With_Address_Clause + + begin + return Check_All_Constants_With_Address_Clause + (Body_Node) = Abandon; + end Has_Constant_With_Address_Clause; + --------------------------------------- -- Has_Formal_Or_Result_Of_Deep_Type -- --------------------------------------- @@ -2009,6 +2080,16 @@ package body Inline is elsif Has_Hide_Unhide_Annotation (Spec_Id, Body_Id) then return False; + -- Do not inline subprograms containing constant declarations with an + -- address clause, as inlining could lead to a spurious violation of + -- SPARK rules. + + elsif Present (Body_Id) + and then + Has_Constant_With_Address_Clause (Unit_Declaration_Node (Body_Id)) + then + return False; + -- Otherwise, this is a subprogram declared inside the private part of a -- package, or inside a package body, or locally in a subprogram, and it -- does not have any contract. Inline it.