This is an archive of the discontinued LLVM Phabricator instance.

[trivial-auto-var-init] Do not emit initialization code for empty class
AbandonedPublic

Authored by serge-sans-paille on Jul 18 2023, 4:41 AM.

Details

Summary

Empty class still use one byte, but there is no way to read that byte
programmatically in a legitimate way. Yet trivial auto var init always
generate a store for it and there is no programmatic way to prevent the
generation of that extra store.

This patch harmonizes Clang behavior with GCC and does not generate
initialization code for empty classes under -ftrivial-auto-var-init

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 4:41 AM
serge-sans-paille requested review of this revision.Jul 18 2023, 4:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 4:41 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
cor3ntin added inline comments.
clang/lib/CodeGen/CGDecl.cpp
1911–1924

Can we simplify the condition here? this cascade of ternaries is becoming hard to read.

switch from cascading ternary operator to cascading if for readability.

serge-sans-paille marked an inline comment as done.Jul 18 2023, 5:37 AM
cor3ntin added inline comments.Jul 18 2023, 5:50 AM
clang/lib/CodeGen/CGDecl.cpp
1913–1923
clang/lib/CodeGen/CGDecl.cpp
1858

inconsistent east const vs west const

1859–1860

You check if CxxRecordTy is nullptr; do you need to check if Ty is nullptr as well?

1860

What about RecordDecl's in general?

jfb added a comment.EditedJul 18 2023, 2:08 PM

(sorry for sending twice, looks like email reply failed)

This is the same as padding, and is initialized on purpose. If it’s truly unused then the optimizer will eliminate it… unless it can’t prove whether the store is dead.

Does this show up in any meaningless performance case? If so, can we optimize it away?

Serge provided this example: https://godbolt.org/z/o5asYGq8G

This seems to be "working as designed". Padding can cause leaks, and the design of this feature is to avoid the leaks. Seems like a GCC bug to not fill empty classes.

@jfb ok, makes sense. For the record it does show up in our profile but I have been able to workaround it without touching the compiler. This probably mean that the GCC implementation is having a small hole, @jakubjelinek do you think it's worth opening a bug in GCC concerning the lack of memsetting on empty class byte under -ftrivial-auto-var-init?