Bug 119989 - [AVR] Incorrect code generation with __memx pointers when optimization is enabled (-O1 and above) on AVR (ATmega328P)
Summary: [AVR] Incorrect code generation with __memx pointers when optimization is ena...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 14.2.1
: P3 normal
Target Milestone: 15.2
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2025-04-29 00:22 UTC by Gilhad
Modified: 2025-04-30 10:29 UTC (History)
0 users

See Also:
Host:
Target: avr
Build:
Known to work: 12.5.1, 13.4.1, 14.3.1, 15.2.1
Known to fail: 12.4.1, 13.3.1, 14.2.1, 15.1.1
Last reconfirmed:


Attachments
simple example (257 bytes, text/x-csrc)
2025-04-29 00:22 UTC, Gilhad
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Gilhad 2025-04-29 00:22:56 UTC
Created attachment 61232 [details]
simple example

wrong optimisation of:
    DT = *IP;   // DT get correct value
    DT = *IP--; // DT get wrong value

---

When using __memx pointers and dereferencing them multiple times in a function, the generated code calls __xload_3 / __xload_4 helper functions, which internally modify r30 (Z) by using Z+. However, the compiler incorrectly reuses the (now incremented) r30 without reloading it from the original pointer (IP).

This causes reading from an unintended memory address.

This problem occurs with optimization levels -O1, -O2, -O3, and -Os.
It does not occur with -O0.

Minimal example demonstrating the issue:

#include <avr/pgmspace.h>

const __memx uint32_t some_data[] = {1,2,3,4,5};
const __memx uint32_t * IP;
uint32_t DT,a,b;

void do_test1() {
    DT = *IP;
    DT = *IP--;
}

void do_test2() {
    DT = *IP;
    asm volatile ("" ::: "memory"); // Prevents unwanted optimization
    DT = *IP--;
}

uint32_t difference(void) {
    IP = &some_data[3];
    do_test1();
    a = DT;
    IP = &some_data[3];
    do_test2();
    b = DT;
    return (a - b); // Expected: 0
}

Expected result:
difference() should return 0.

Actual result:
difference() returns a nonzero value (incorrect).

Root cause analysis:
Compiling with:

avr-gcc -mmcu=atmega328p -Os -S demo.c -o demo.Os.s

shows that:

- Functions like __xload_3 and __xload_4 use r30:r31 (Z) and r21 as arguments.
- These functions perform memory reads using ld/lpm with post-increment (Z+), modifying r30.
- After returning from __xload_3/__xload_4, r30:r31 no longer points to the same memory location.

However, the optimized version of the code does not reload IP into r30:r31, but incorrectly reuses the already incremented register contents, leading to wrong memory accesses.

Inserting an inline assembly memory clobber (asm volatile ("" ::: "memory");) between accesses forces the compiler to reload IP correctly, avoiding the issue.

The same happens for IP++ used instead of IP--.

I found it when program worked with debugging function (in place of the asm ... statement), but failed without it.

Environment:

    Target: AVR ATmega328P
    Compiler: gcc version 14.2.1 20241221 (Gentoo 14.2.1_p20241221 p7)
Comment 1 GCC Commits 2025-04-30 07:14:42 UTC
The releases/gcc-13 branch has been updated by Georg-Johann Lay <gjl@gcc.gnu.org>:

https://gcc.gnu.org/g:67d13d11edd070b57d9166e828dc0b734f4e524b

commit r13-9625-g67d13d11edd070b57d9166e828dc0b734f4e524b
Author: Georg-Johann Lay <avr@gjlay.de>
Date:   Wed Apr 30 08:43:51 2025 +0200

    AVR: target/119989 - Add missing clobbers to xload_<mode>_libgcc.
    
    libgcc's __xload_1...4 is clobbering Z (and also R21 is some cases),
    but avr.md had clobbers of respective GPRs only up to reload.
    Outcome was that code reading from the same __memx address twice
    could be wrong.  This patch adds respective clobbers.
    
          Backport from 2025-04-30 r14-11703
          PR target/119989
    gcc/
            * config/avr/avr.md (xload_<mode>_libgcc): Clobber R21, Z.
    
    gcc/testsuite/
            * gcc.target/avr/torture/pr119989.h: New file.
            * gcc.target/avr/torture/pr119989-memx-1.c: New test.
            * gcc.target/avr/torture/pr119989-memx-2.c: New test.
            * gcc.target/avr/torture/pr119989-memx-3.c: New test.
            * gcc.target/avr/torture/pr119989-memx-4.c: New test.
    
    (cherry picked from commit 1ca1c1fc3b58ae5e1d3db4f5a2014132fe69f82a)
Comment 2 GCC Commits 2025-04-30 07:25:32 UTC
The releases/gcc-12 branch has been updated by Georg-Johann Lay <gjl@gcc.gnu.org>:

https://gcc.gnu.org/g:21aa0abcf1aba3a5f410b01628e8fc7f55ee8786

commit r12-11077-g21aa0abcf1aba3a5f410b01628e8fc7f55ee8786
Author: Georg-Johann Lay <avr@gjlay.de>
Date:   Wed Apr 30 08:43:51 2025 +0200

    AVR: target/119989 - Add missing clobbers to xload_<mode>_libgcc.
    
    libgcc's __xload_1...4 is clobbering Z (and also R21 is some cases),
    but avr.md had clobbers of respective GPRs only up to reload.
    Outcome was that code reading from the same __memx address twice
    could be wrong.  This patch adds respective clobbers.
    
          Backport from 2025-04-30 r14-11703
          PR target/119989
    gcc/
            * config/avr/avr.md (xload_<mode>_libgcc): Clobber R21, Z.
    
    gcc/testsuite/
            * gcc.target/avr/torture/pr119989.h: New file.
            * gcc.target/avr/torture/pr119989-memx-1.c: New test.
            * gcc.target/avr/torture/pr119989-memx-2.c: New test.
            * gcc.target/avr/torture/pr119989-memx-3.c: New test.
            * gcc.target/avr/torture/pr119989-memx-4.c: New test.
    
    (cherry picked from commit 1ca1c1fc3b58ae5e1d3db4f5a2014132fe69f82a)
Comment 3 GCC Commits 2025-04-30 09:02:01 UTC
The releases/gcc-15 branch has been updated by Georg-Johann Lay <gjl@gcc.gnu.org>:

https://gcc.gnu.org/g:e268cb27332a1c39a5fc8426ae368c9878f3f241

commit r15-9602-ge268cb27332a1c39a5fc8426ae368c9878f3f241
Author: Georg-Johann Lay <avr@gjlay.de>
Date:   Wed Apr 30 08:43:51 2025 +0200

    AVR: target/119989 - Add missing clobbers to xload_<mode>_libgcc.
    
    libgcc's __xload_1...4 is clobbering Z (and also R21 is some cases),
    but avr.md had clobbers of respective GPRs only up to reload.
    Outcome was that code reading from the same __memx address twice
    could be wrong.  This patch adds respective clobbers.
    
          Forward-port from 2025-04-30 r14-11703
          PR target/119989
    gcc/
            * config/avr/avr.md (xload_<mode>_libgcc): Clobber R21, Z.
    
    gcc/testsuite/
            * gcc.target/avr/torture/pr119989.h: New file.
            * gcc.target/avr/torture/pr119989-memx-1.c: New test.
            * gcc.target/avr/torture/pr119989-memx-2.c: New test.
            * gcc.target/avr/torture/pr119989-memx-3.c: New test.
            * gcc.target/avr/torture/pr119989-memx-4.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-1.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-2.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-3.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-4.c: New test.
    
    (cherry picked from commit 1ca1c1fc3b58ae5e1d3db4f5a2014132fe69f82a)
Comment 4 GCC Commits 2025-04-30 09:32:21 UTC
The master branch has been updated by Georg-Johann Lay <gjl@gcc.gnu.org>:

https://gcc.gnu.org/g:4dc40eddbe69566869f7aafb78c31c4850b8aeb7

commit r16-301-g4dc40eddbe69566869f7aafb78c31c4850b8aeb7
Author: Georg-Johann Lay <avr@gjlay.de>
Date:   Wed Apr 30 08:43:51 2025 +0200

    AVR: target/119989 - Add missing clobbers to xload_<mode>_libgcc.
    
    libgcc's __xload_1...4 is clobbering Z (and also R21 is some cases),
    but avr.md had clobbers of respective GPRs only up to reload.
    Outcome was that code reading from the same __memx address twice
    could be wrong.  This patch adds respective clobbers.
    
          Forward-port from 2025-04-30 r14-11703
          PR target/119989
    gcc/
            * config/avr/avr.md (xload_<mode>_libgcc): Clobber R21, Z.
    
    gcc/testsuite/
            * gcc.target/avr/torture/pr119989.h: New file.
            * gcc.target/avr/torture/pr119989-memx-1.c: New test.
            * gcc.target/avr/torture/pr119989-memx-2.c: New test.
            * gcc.target/avr/torture/pr119989-memx-3.c: New test.
            * gcc.target/avr/torture/pr119989-memx-4.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-1.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-2.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-3.c: New test.
            * gcc.target/avr/torture/pr119989-flashx-4.c: New test.
    
    (cherry picked from commit 1ca1c1fc3b58ae5e1d3db4f5a2014132fe69f82a)
Comment 5 Georg-Johann Lay 2025-04-30 09:40:53 UTC
Thank you for the report.

The issue has been fixed in v12.5, v13.4, v14.3 and v15.2+

The v14 commit is here: r14-11703
Comment 6 Georg-Johann Lay 2025-04-30 10:29:01 UTC
The issue was introduced by the cc0 -> CCmode conversion in r12-226 3ba781d3b5c8efadb60866c9743b657e8f0eb222