https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128106
Bug ID: 128106
Summary: Passing more than 256 params to a lambda with capture
gives miscompoiled code due the IPA SRA
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: berteun at gmail dot com
Target Milestone: ---
The following code *miscompiles* under GCC 16.2 (and 15.2) rather than giving a
compile time error when compiled with -O2 (or higher):
```
#include <array>
#include <string_view>
#include <tuple>
struct F { std::string_view name = "x"; };
constexpr std::array<F, 256> fields{};
[[gnu::noinline]] void emit(std::string_view name) {
if (name != "x") __builtin_puts("miscompiled");
}
int main() {
long value{};
std::apply([&](auto... f) { ((emit(f.name), value += value), ...); },
fields);
return value;
}
```
The issue lies with the 256 function params, if you change 256 to 255, it
works. Under GCC 17.0 trunk it does not miscompile, but rather gives an ICE (I
could only test this on Godbolt) this is then the output:
```
during IPA pass: sra
<source>: In lambda function:
<source>:19:1: internal compiler error: in set_single_param_flow_source, at
ipa-sra.cc:887
19 | }
| ^
0x2c69d98 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
???:0
0x2c5e9db internal_error(char const*, ...)
???:0
0xb9c512 fancy_abort(char const*, int, char const*)
???:0
0x14cb64a execute_ipa_summary_passes(ipa_opt_pass_d*)
???:0
0x1066d06 symbol_table::finalize_compilation_unit()
???:0
/cefs/b2/b23b881c7b2b30ba33960fa9_gcc-trunk-20261023/bin/../libexec/gcc/x86_64-linux-gnu/17.0.0/cc1plus
-quiet -imultiarch x86_64-linux-gnu -iprefix
/cefs/b2/b23b881c7b2b30ba33960fa9_gcc-trunk-20261023/bin/../lib/gcc/x86_64-linux-gnu/17.0.0/
-D_GNU_SOURCE -isystem /opt/compiler-explorer/libs/fmt/12.0.0/include -isystem
/opt/compiler-explorer/libs/nlohmann_json/v3.11.1/single_include <source>
-quiet -dumpdir /app/output.s- -dumpbase example.cpp -dumpbase-ext .cpp
-mtune=generic -march=x86-64 -g -O2 -std=c++26 -fdiagnostics-color=always
-fno-verbose-asm -o /tmp/cc3afzxh.s
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
```
Adding `-fno-ipa-sra` avoids the bug.
This possibly relates to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124958
and and also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117308 which
mentions limitations but also states that it isn't expected it would miscompile
code.
I would therefore not expect that 256+ function params are necessarily
supported, but would rather expect a compile time reject than an ICE /
miscompile.
Note that if you increase 256 to 257 you will find two lines of 'miscompiled'
in the output.
I tried to reduce the example as much as possible, as far as I can see all
elements here are needed to trigger it (the capture, the function call, and the
extra fumbling with captured 'value') but perhaps it can be reduced further.
If you remove the `value += value` part, then on GCC 16.2 you also get an ice,
so when replacing main with:
```
int main() {
long value{};
std::apply([&](auto... f) { ((emit(f.name)), ...); }, fields);
return value;
}
```
You get:
```
during IPA pass: sra
<source>:16:1: internal compiler error: in process_edge_to_unknown_caller, at
ipa-sra.cc:3345
16 | }
| ^
0x22a6498 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
???:0
0x229f78b internal_error(char const*, ...)
???:0
0x7f904e fancy_abort(char const*, int, char const*)
???:0
/cefs/17/17a2c76db590a13a8a92c74c_consolidated/compilers_c++_x86_gcc_16.2.0/bin/../libexec/gcc/x86_64-linux-gnu/16.2.0/cc1plus
-quiet -imultiarch x86_64-linux-gnu -iprefix
/cefs/17/17a2c76db590a13a8a92c74c_consolidated/compilers_c++_x86_gcc_16.2.0/bin/../lib/gcc/x86_64-linux-gnu/16.2.0/
-D_GNU_SOURCE <source> -quiet -dumpdir /app/ -dumpbase output.cpp -dumpbase-ext
.cpp -masm=intel -mtune=generic -march=x86-64 -g -O2 -fdiagnostics-color=always
-fno-verbose-asm -o /app/output.s
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1
```
Godbolt example: https://gcc.godbolt.org/z/zxaPf563G