From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1B8563858D20; Thu, 2 May 2024 12:51:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B8563858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1714654278; bh=pNTLTtN/cjLHQaW1iIPtUPckCHksW+JnpIVccjxoq8w=; h=From:To:Subject:Date:From; b=aWR/A+JWT4yTtpn+9MsZD4KMdnKF/7uTfC4xau0AlBb5CoblFCqZ+eeeazDBnx8jl bQpdW0t41zqRDNrPUAgCviBtsOOLjngtmuU8l/Kkxopvw+p9YavJP7V4w7ihccZe5o vfzPEGPzN0q5qrU+GrblsdoINF1gyF8aQ70L1RbQ= From: "nfxjfg at googlemail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/114923] New: gcc ignores escaping pointer and applies invalid optimization Date: Thu, 02 May 2024 12:51:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: nfxjfg at googlemail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114923 Bug ID: 114923 Summary: gcc ignores escaping pointer and applies invalid optimization Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: nfxjfg at googlemail dot com Target Milestone: --- Created attachment 58088 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D58088&action=3Dedit Test case In this test case, the pointer to a stack-allocated variable is passed to a volatile variable. That means the pointer definitely escapes. But the compi= ler seems to assume that the memory can't change, and returns the initialization value of the buf variable. I tried multiple variations of this. Sometimes it generates the correct cod= e, even though the situation is exactly the same. A call to an external functi= on often fixes it, but sometimes not (could not produce a reduced test case of= the latter). This is reduced from some real-word code. The first register access passes = the memory pointer to the hardware, the second access starts the DMA. In the real-world code there was some more stuff such as waiting for the DMA to finish, but it doesn't matter for code generation. The code started to fail after a DMA buffer was moved from a global variable to the stack. Originally experienced on 12.2.0 / riscv32-unknown-elf, but on godlbolt I c= an reproduce it with trunk and some other architectures.=