https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122036
Bug ID: 122036
Summary: [d] inner static struct methods not inlined
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: d
Assignee: ibuclaw at gdcproject dot org
Reporter: witold.baryluk+gcc at gmail dot com
Target Milestone: ---
gdc
(Compiler-Explorer-Build-gcc-f07d1f3135d3bd0cb29afe3a93af329c7254c778-binutils-2.44)
16.0.0 20250919 (experimental)
Same in gdc 142.0 and gdc 15.2.0
https://godbolt.org/z/xaM4x3Gnn
---
```d
bool Compare2(size_t hash_, size_t hash2) {
return hash_ == hash2;
}
final struct HashMap(K, V) {
import core.builtins;
static struct Slot {
size_t hash_;
import gcc.attributes : attribute;
pragma(inline)
@attribute("forceinline")
bool Compare(size_t hash2) const {
return hash_ == hash2;
}
}
Slot[1] slots;
import gcc.attributes : attribute;
@attribute("flatten")
Slot* GetWithHash(size_t hash) {
Slot* slot = &slots[0];
// if (slot.hash_ == hash) { // fine
// if (Compare2(slot.hash_, hash)) { // inlined
if (slot.Compare(0)) { // not inlined
return slot;
}
return null;
}
}
alias HashMap0 = HashMap!(ulong, ulong);
// Same
HashMap0.Slot* GetWithHash(size_t hash) {
HashMap0.Slot* slot;
// if (slot.hash_ == hash) { // fine
// if (Compare2(slot.hash_, hash)) { // inlined
if (slot.Compare(0)) { // not inlined
return slot;
}
return null;
}
struct StandaloneSlot {
size_t hash_;
import gcc.attributes : attribute;
pragma(inline)
@attribute("forceinline")
bool Compare(size_t hash2) {
return hash_ == hash2;
}
}
// Works
StandaloneSlot* StandaloneGetWithHash(StandaloneSlot *slot, size_t hash) {
if (slot.Compare(hash)) { // finally indlined
return slot;
}
return null;
}
```
Using (static) inner struct method `Compare` it is never inlined, even if asked
to, or done outside of the class, even if using simplified arguments. Method is
infered pure, safe, nothrow. I also added const, but effect is same.
Example:
ldc has no problem with this.
Using a standalone struct at module level, the method is properly inlined.
Assembly, at x86 / amd64, -O2 / -O3 -frelease -fno-bounds-check
```
"bool example.Compare2(ulong, ulong)":
cmp rdi, rsi
sete al
ret
"const pure nothrow @nogc @safe bool example.HashMap!(ulong,
ulong).HashMap.Slot.Compare(ulong)":
cmp QWORD PTR [rdi], rsi
sete al
ret
"example.HashMap!(ulong, ulong).HashMap.Slot* example.GetWithHash(ulong)":
sub rsp, 8
xor esi, esi
xor edi, edi
call "const pure nothrow @nogc @safe bool example.HashMap!(ulong,
ulong).HashMap.Slot.Compare(ulong)" # -- Should be inlined
xor eax, eax
add rsp, 8
ret
"bool example.StandaloneSlot.Compare(ulong)":
cmp QWORD PTR [rdi], rsi
sete al
ret
"example.StandaloneSlot* example.StandaloneGetWithHash(example.StandaloneSlot*,
ulong)":
xor eax, eax
cmp rsi, QWORD PTR [rdi] # -- correctly inlined method that is in
a struct that is not a static member of a class
cmove rax, rdi
ret
"pure nothrow @nogc @safe example.HashMap!(ulong, ulong).HashMap.Slot*
example.HashMap!(ulong, ulong).HashMap.GetWithHash(ulong)":
push rbx
xor esi, esi
mov rbx, rdi
call "const pure nothrow @nogc @safe bool example.HashMap!(ulong,
ulong).HashMap.Slot.Compare(ulong)" # -- Should be inlined
test al, al
mov eax, 0
cmovne rax, rbx
pop rbx
ret
```