Clang implements [[clang::trivial_abi]] (docs) to allow passing classes that are non-trivial for the purposes of calls as-if they were trivial. For example, this allows passing std::unique_ptrs in registers instead of on the stack, which results in a significant performance and code size win (https://godbolt.org/z/aTsG34abP).
It would be nice to standardize the attribute to allow it to be used across compilers.
I think it should be enough to modify the definition of non-trivial for the purposes of calls to something like:
A type is considered non-trivial for the purposes of calls if:
it isn't marked [[itanium::trivial_abi]], or
the type directly declares a virtual base of virtual member functions, or
all of its copy and move constructors are deleted, or
the type has a base class that is non-trivial for the purposes of calls, or
the type has a non-static data member whose type is non-trivial for the purposes of calls, which includes:
classes that are non-trivial for the purposes of calls,
arrays of classes that are non-trivial for the purposes of calls
and
it has a non-trivial copy constructor, move constructor, or destructor, or
all of its copy and move constructors are deleted.
This mostly is the list of things when [[clang::trivial_abi]] is ignored. Suggestions to simplify the definition are welcome.
Should there be a note that classes marked [[trivial_abi]] have to be trivially relocatable (move + destroy are equivalent to a memcpy)?
Clang implements
[[clang::trivial_abi]]
(docs) to allow passing classes that are non-trivial for the purposes of calls as-if they were trivial. For example, this allows passingstd::unique_ptr
s in registers instead of on the stack, which results in a significant performance and code size win (https://godbolt.org/z/aTsG34abP).It would be nice to standardize the attribute to allow it to be used across compilers.
I think it should be enough to modify the definition of non-trivial for the purposes of calls to something like:
A type is considered non-trivial for the purposes of calls if:
[[itanium::trivial_abi]]
, orThis mostly is the list of things when
[[clang::trivial_abi]]
is ignored. Suggestions to simplify the definition are welcome. Should there be a note that classes marked[[trivial_abi]]
have to be trivially relocatable (move + destroy are equivalent to a memcpy)?CC @ldionne @jwakely