This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Initialize large arrays by copying from a global
ClosedPublic

Authored by kosarev on Feb 12 2018, 5:21 AM.

Details

Summary

Currently, clang compiles explicit initializers for array elements into series of store instructions. For large arrays of built-in types this results in bloated output code and significant amount of time spent on the instruction selection phase. This patch fixes the issue by initializing such arrays with global constants that store the binary image of the initializer.

Diff Detail

Repository
rL LLVM

Event Timeline

kosarev created this revision.Feb 12 2018, 5:21 AM
rjmccall added inline comments.Feb 12 2018, 9:18 AM
lib/CodeGen/CGExprAgg.cpp
421 ↗(On Diff #133835)

Is there a good reason to use an element-count heuristic instead of a total-size heuristic here?

Why only builtin types? That seems to pointlessly rule out nested arrays, complex types, vectors, C structs, and so on. I think the predicate you probably want here is isTriviallyCopyableType.

kosarev added inline comments.Feb 13 2018, 7:54 AM
lib/CodeGen/CGExprAgg.cpp
421 ↗(On Diff #133835)

Is there a good reason to use an element-count heuristic instead of a total-size heuristic here?

Yes, the code below generates per-element initialization only for explicitly specified initializers. The rest, if any, is initialized with a filler, so it doesn't affect the size of the resulting code much.

kosarev updated this revision to Diff 134042.Feb 13 2018, 7:56 AM

Improved as suggested to cover all trivially-copyable types.

rjmccall added inline comments.Feb 13 2018, 10:48 AM
lib/CodeGen/CGExprAgg.cpp
421 ↗(On Diff #133835)

That makes sense, but you could still base it on the total size being initialized with explicit initializers. Such initializers, even when constant, are likely to require code size basically proportionate to the number of bytes initialized — sizes of immediate operands and all that.

kosarev updated this revision to Diff 134590.Feb 16 2018, 3:29 AM

Updated to consider the total size of the explicit initializers instead of their number. The threshold value is adjusted respectively.

rjmccall accepted this revision.Feb 16 2018, 9:58 AM

LGTM, thanks!

This revision is now accepted and ready to land.Feb 16 2018, 9:58 AM
This revision was automatically updated to reflect the committed changes.