From patchwork Wed Sep 11 08:23:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 1983770 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=gihKpcuc; 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 4X3YYz4MPqz1y1l for ; Wed, 11 Sep 2024 18:26:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6EF083858C32 for ; Wed, 11 Sep 2024 08:26:13 +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 69F0F3858D28 for ; Wed, 11 Sep 2024 08:25:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 69F0F3858D28 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 69F0F3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726043154; cv=none; b=WZLhtpnxI+7AHgZDoRaglGj4FHeILzIWTZPdpkBAP2KQuvDUrQESikg5Ym4gyjOinx1wPpmLcMAjH4bPfio56DXDfd4oPtiz2EQ/g++w8Qr2QnPDSvc8Sd9cuMffmWfqpaYzRLYdMwLSx9SYoDMD1ci1lbfUjbff78sKnZ5CNbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726043154; c=relaxed/simple; bh=Bmf+eTP+sFHZQcU0hAaO785bSO5lUwbinUBgeK2AmXI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JEaI+6f7TqJbov0/5soK8dLAXIfCqWLnVCQY5DjwMTnbDcEbLpU9z8ICxJm+BSctGXF90gYWH+Y4xFZusCUzxMSK4idA+WqHZ7oGbdL8KiIHwWEzwxERCXR+um/HD9squ8nuRVr7N5E1rWDWp+TtYdBm2STukGVia+mjQmvj/Zk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4280ca0791bso60554985e9.1 for ; Wed, 11 Sep 2024 01:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1726043150; x=1726647950; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=NqKFcDCiUNQ7yxXbUAVg0iBzexWNvCsG0YKKTKZ5vB8=; b=gihKpcucxBYoTtqctrW544+X9fgJCVETYo/VYpgXUGWIABaxb0CHoOsh94tmJa1tOC e2OY4jnbUK0zeUNcrIpIhIjLmo3WX0iIA8UFozlgWWGGIV5r5vGKNC3fp5jOZtMYzlnj 6KFH/fOMfL+faGSe3Qq+Eyh/TNQaIQmD2lONXQGDbtjL7MkVYUmIBb/ep8QrNwIQTGn9 swoY8Gdixrvim26ZzbOLwMQXCLrf0RygvSxfc76rcvvfbfJ10Guw0oiVa28wvb7wE3/E Vj8zA7KFC7NtKU1oXJs02/N4ANoOJwPA42BegdZ+6ZaStQW7JiJa64+Va14fumzqzoK5 FqfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726043150; x=1726647950; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NqKFcDCiUNQ7yxXbUAVg0iBzexWNvCsG0YKKTKZ5vB8=; b=MDYFgZFP+EZa5da6aAafXsBaXSmg16XFBs7uWUlZLyQ3FtxbWxIHGsAdRJPzLCCk/V TeF5Ma0yKYbjhMBexfVr8nJ3NlrfDkkFVFGO6rE0BjFzieGMPwmsjXIC00OhzbaJKya0 s5iqel5gyGDNYPQCGHvcbOMG868DzA6vhpUTKcRDSv9FxvxPP0vc3EaBvXgkoxyUFXBN UsN/vFbI9m6/FeaVxcskUFvSoJ9qNWqvLVuRTnmSQug/DznP9FNMFxkKrikKhm2RtuyJ 6YAYqqZKvf/GWoXXBA0i4JD9/B+10UDIqim3Hc2+GfhYDP6Ds+pw7bPS53KLwIRb63h5 W4FA== X-Gm-Message-State: AOJu0Yyd6vbg3VOHUpPNcCitTwytIR8GuYOEECRA/C3PPuD6+671l7az cxyp5BrWPJok5yXg7HHM0LkqvIQPmONRT2lxkcVG7B/voQPDWmpcL377uPkwzN6iycXuXsxRHqU = X-Google-Smtp-Source: AGHT+IEnz8DddBRjLRvQG0t8DnfFdH0RN9Fg464qvJS2ZbHqMNOBLcDqd7qwrwU1FRI0ktgZc1nCHg== X-Received: by 2002:adf:f411:0:b0:371:7e3f:6cc with SMTP id ffacd0b85a97d-378895c5338mr10691752f8f.9.1726043149487; Wed, 11 Sep 2024 01:25:49 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378956de3f0sm10795317f8f.113.2024.09.11.01.25.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:25:48 -0700 (PDT) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix wrong code out of NRV + RSO + inlining Date: Wed, 11 Sep 2024 10:23:51 +0200 Message-ID: <4949065.31r3eYUQgx@fomalhaut> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, the attached Ada testcase compiled with -O -flto exhibits a wrong code issue when the 3 optimizations NRV + RSO + inlining are applied to the same call: if the LHS of the call is marked write-only before inlining, then it will keep the mark after inlining although it may be read in GIMPLE from that point on. The proposed fix is to always clear the flag during inlining in the RSO case. Tested on x86-64/Linux, OK for the mainline? 2024-09-11 Eric Botcazou * tree-inline.cc (declare_return_variable): Clear writeonly flag on a global variable used directly as the return slot. 2024-09-11 Eric Botcazou * gnat.dg/lto27.adb: New test. * gnat.dg/lto27_pkg1.ads, gnat.dg/lto27_pkg2.ads, gnat.dg/lto27_pkg2.adb, gnat.dg/lto27_pkg3.ads: New helper. diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc index f31a34ac410..2bb1e1602b2 100644 --- a/gcc/tree-inline.cc +++ b/gcc/tree-inline.cc @@ -3782,6 +3782,11 @@ declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest, gcc_assert (TREE_CODE (var) != SSA_NAME); if (TREE_ADDRESSABLE (result)) mark_addressable (var); + /* RESULT may also be read in the callee, typically because the NRV + optimization has been applied to the function, so VAR may also be + read from now on. */ + if (VAR_P (var) && (TREE_STATIC (var) || DECL_EXTERNAL (var))) + varpool_node::get (var)->writeonly = 0; } if (DECL_NOT_GIMPLE_REG_P (result) && DECL_P (var))