Issue 22466 - Small array initialization and assignment shouldn't defer to memset
Summary: Small array initialization and assignment shouldn't defer to memset
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: backend, betterC
Depends on:
Blocks:
 
Reported: 2021-11-01 18:31 UTC by Stanislav Blinov
Modified: 2023-01-15 07:59 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Stanislav Blinov 2021-11-01 18:31:15 UTC
// dmd -betterC

extern(C) void main(int argc, char** argv)
{
    int[4] arr = 1;
}

Results in:
bugs/smallarray.d:(.text.main[main]+0x1f): undefined reference to `_memset32'

That is a meager four 32-bit stores. It's an unnecessary pessimization to defer this to a *function call*, source of which may or may not be visible to the compiler, implementation of which would have branches. Compiler should just emit the four stores here.

This is not just for ints, other built-in types should also benefit. Upper bound for avoiding the function call may be up to 512 bits worth of stores (to correspond to current SIMD widths).
Comment 1 Walter Bright 2023-01-15 07:59:03 UTC
With the current master, Look Ma, no memset32!!

_D4testQfFZv:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 83 EC 10              sub       RSP,010h
0008:   48 B8 01 00 00 00 01 00 00 00  mov       RAX,0100000001h
0012:   48 89 45 F0              mov       -010h[RBP],RAX
0016:   48 89 45 F8              mov       -8[RBP],RAX
001a:   C9                       leave
001b:   C3                       ret