[Bug c/117573] New: RISC-V target -O2 generates Misaligned Access despite -mstrict-align

2024-11-13 Thread jasmin--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117573

Bug ID: 117573
   Summary: RISC-V target -O2 generates Misaligned Access despite
-mstrict-align
   Product: gcc
   Version: unknown
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: jas...@chili-chips.com
  Target Milestone: ---

___Issue Summary___

Enabling GCC optimizations (e.g., -O2) for our RISC-V project results in
program failures due to misaligned memory accesses, despite using the
-mstrict-align flag. Disabling optimizations (-O0) leads to increased binary
size and reduced performance.

___Performance Impact___

Without Optimizations (-O0):
Binary Size: ~4 KB
Performance: 8.87 DMIPS

With Optimizations (-O2):
Binary Size: ~2.8 KB
Performance: 24.15 DMIPS

This indicates a potential performance gain of approximately 2.7x when
optimizations are enabled.

___Observed Behavior___

Despite applying the -mstrict-align flag, GCC generates misaligned memory
accesses under optimization levels like -O2, leading to program crashes.

For additional detail, please refer to: 
https://github.com/chili-chips-ba/wireguard-fpga/issues/5#issue-2651618418

[Bug c++/120393] New: Link-time optimization vs. Os/O2; baremetal memcpy elimination

2025-05-21 Thread jasmin--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120393

Bug ID: 120393
   Summary: Link-time optimization vs. Os/O2; baremetal memcpy
elimination
   Product: gcc
   Version: unknown
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: jas...@chili-chips.com
  Target Milestone: ---

Use of `-flto' option generates an error for initialized char string variables.
Here is an example:

   char test[] = "Hello world!\r\n";

   for (i = 0; i < 14; i++) { 
  uart_send_char(csr, test[i]);
   }

  /usr/lib/gcc/riscv64-unknown-elf/13.2.0/../../../riscv64-unknown-elf/bin/ld:
/tmp/ccerPl2Y.ltrans0.ltrans.o: in function `main':
:(.text.startup+0x314): undefined reference to `memcpy'

The error comes from premature optimization, where linker concludes that the
`memcpy' function is not used in the source code, and eliminates it. The
workaround is to accompany the `memcpy' declaration with __attribute__((used)),
such as:

__attribute__((used)) void *memcpy(void *dest, const void *src, size_t
len);

Hence the questions:

 Q1) why does `-flto' option make the compile flow optimize out `memcpy'
function, which was actually inserted by the flow, to copy the initialized
strings?

Q2) can other functions outside of `main.cpp', including calls from boot CRT
Assembly code, be similarly eliminated by `-flto'?

This issue is uncovered in Wireguard-FPGA project, where tracked under:
   https://github.com/chili-chips-ba/wireguard-fpga/issues/17

It seems related to:
   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119424