DEEEER0 draft 3: SG14 [[move_relocates]] Document #: Date: Project: Reply-to:

DEEEER0 draft 3 2018-04-16 Programming Language C++ Evolution Working Group Niall Douglas

This proposes a new C++ attribute [[move_relocates]] which enables more aggressive optimisation of moves of those types than is possible at present. A major motivation behind this proposal is to enable the standard lightweight throwable error object as proposed by [P0709] Zero-overhead deterministic exceptions to directly encapsulate a std::exception_ptr, using CPU registers alone for storage. Changes since draft 2: • Replaced the compiler deducements with a simple C++ attribute named [[move_relocates]] and reduced down the size and complexity of paper considerably to the bare essentials. Changes since draft 1: • Added requirement for default constructor, move constructor and destructor to be defined in-class. • Added halting problem avoidance requirements. • Refactored text to clearly indicate that trivial relocatability is just an optimisation of move. • Mentioned STL allocators and effects thereupon.

Contents 1 Introduction 1.1 Prior work in this area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2 3

2 Impact on the Standard

3

3 Proposed Design

3

4 Design decisions, guidelines and rationale

6

5 Technical specifications

6

6 Frequently asked questions

6

7 Acknowledgements

6 1

8 References

1

7

Introduction

The most aggressive optimisations which the C++ compiler can perform are to types which meet the TriviallyCopyable requirements: • Every copy constructor is trivial or deleted. • Every move constructor is trivial or deleted. • Every copy assignment operator is trivial or deleted. • Every move assignment operator is trivial or deleted. • At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted. • Trivial non-deleted destructor. All the integral types meet TriviallyCopyable, as do C structures. The compiler is thus free to store such types in CPU registers, relocate them in memory as if by memcpy, and overwrite their storage as no destruction is needed. This greatly simplifies the job of the compiler optimiser, making for tighter codegen, faster compile times, and less stack usage, all highly desirable things. There are quite a lot of types in the standard library and in user code which do not meet TriviallyCopyable, yet are completely safe to be stored in CPU registers and can be relocated arbitrarily in memory as if by memcpy. For example, std::vector likely has a similar implementation to: 1 2 3 4 5 6 7 8 9

template class vector { T *_begin{nullptr}, *_end{nullptr}; public: vector() = default; vector(vector &&o) : _begin(o._begin), _end(o._end) { o._begin = o._end = nullptr; } ~vector() { delete _begin; _begin = _end = nullptr; } ... };

Such a vector implementation could be absolutely stored in CPU registers, and arbitrarily relocated in memory with no ill effect via the following as-if sequence: 1

vector *dest, *src;

2 3 4

// Copy bytes of src to dest memcpy(dest, src, sizeof(vector));

5 6 7 8

// Copy bytes of default constructed instance to src vector default_constructed; memcpy(src, &default_constructed, sizeof(vector));

2

This paper proposes a new C++ attribute [[move_relocates]] which tells the compiler and library code when the move of a type with non-trivial move constructor and non-trivial destructor can be instead performed using two as-if memcpy()’s.

1.1

Prior work in this area

• [N4034] Destructive Move This proposal differs from destructive moves in the following ways: – It is considerably simpler than N4034, we do not propose any new kind of operation, nor new semantics. We merely propose an alternative way of doing moves, valid under limited circumstances. • [P0023] Relocator: Efficiently moving objects. This proposal differs from relocators in the following ways: – It is considerably simpler than P0023, we do not propose any new kind of operation, nor new operators. We merely propose an alternative way of doing moves, valid under limited circumstances.

2

Impact on the Standard

Very limited. This is a limited optimisation of the implementation of move construction, and the STL move construct-destruct cycle only. We do not fiddle with allocators, the meaning nor semantics of moves, nor anything else. All we propose is that where it is safe to do so, we replace the calling of the move constructor with memcpy() (which can be elided by the compiler if it has no visible side effects, same as with all memcpy()). That in turn enables temporary storage in CPU registers, if the compiler chooses to do so.

3

Proposed Design 1. That a new C++ attribute [[move_relocates]] become applicable to type definitions. 2. This attribute shall be silently ignored if the type does not have a public, non-deleted, constexpr, in-class defined default constructor and if there is not a public, non-deleted, non-virtual move constructor. 3. If a type T has attribute [[move_relocates]], instead of calling the defined move constructor, the compiler will implement move construction as-if by memcpy(dest, src, sizeof(T)), followed by as-if memcpy(src, &T{}, sizeof(T)). Note that by ‘as-if’, we mean that the compiler can fully optimise the sequence, including the elision of calling the destructor if the

3

destructor would do nothing when supplied with a default constructed instance, which in turn would elide entirely the second memory copy. 4. It is considered good practice that the move constructor be defaulted with an explanatory comment mentioning the [[move_relocates]], as the move constructor is never called on types with non ignored [[move_relocates]]. For backwards compatibility with older compilers, it is acceptable to implement the move constructor to cause the exact same effects as [[move_relocates]] i.e. copying the bits of source to destination followed by copying the bits of a default constructed instance to source. 5. If a type T has attribute [[move_relocates]], the trait std::is_relocatable shall be defaulted to true. Note that the programmer may specialise std::is_relocatable to any value they like for some type T. 6. If STL containers see that for their type T that std::is_relocatable is true, that std::has_virtual_destructor is false, and if the Allocator they are configured with has a defaulted construct() and destroy(), they will relocate the storage for type T in the move construction + destruction cycle by a method equivalent to copying bits, but without calling the move constructor, nor the destructor on the moved-from storage. This introduces an important corner case: the programmer is free to write a non-virtual destructor for a [[move_relocates]] type which when called on a default constructed instance is non-trivial, however it will not be called when the type is used inside a STL container with the default Allocator. 7. [[move_relocates]] types with a non-virtual destructor are eligible for being automatically treated as having a trivial ABI1 i.e. can be passed between function in CPU registers if the compiler can easily deduce2 that the destructor, when called on a default constructed instance, would become trivial. Let us take a worked example. Imagine the following partial implementation of unique_ptr: 1 2 3 4 5 6 7

template class [[move_relocates]] unique_ptr { T *_v{nullptr}; public: // Has public, non-deleted, constexpr default constructor unique_ptr() = default;

8 9

constexpr explicit unique_ptr(T *v) : _v(v) {}

10 11 12

unique_ptr(const unique_ptr &) = delete; unique_ptr &operator=(const unique_ptr &) = delete;

13 14 15 16 17

#if __cplusplus >= 202300 // Has a public, non-deleted, move constructor unique_ptr(unique_ptr &&) = default; // implemented via [[move_relocates]] #else 1

https://clang.llvm.org/docs/AttributeReference.html#trivial-abi-clang-trivial-abi

2

‘Easily deduce’ is actually quite hard to implement. A compiler might simply insist on there also being a

[[trivial_abi]] attribute on the type, if it supported such an attribute.

4

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

constexpr unique_ptr(unique_ptr &&o) noexcept : _v(o._v) { o._v = nullptr; } #endif unique_ptr &operator=(unique_ptr &&o) noexcept { delete _v; _v = o._v; o._v = nullptr; return *this; } ~unique_ptr() { delete _v; _v = nullptr; } };

The default constructor is not deleted, constexpr and public and it sets the single member data _v to nullptr. Additionally, the move constructor is not deleted, not virtual and public, so [[move_relocates]] is not ignored. The destructor, when called on a default constructed instance, is trivial (operator delete does nothing when fed a null pointer, and setting a null pointer to a null pointer leaves the object with exactly the same memory representation as a default constructed instance). We thus get, for this program: 1 2 3 4

unique_ptr __attribute__((noinline)) foo() { return unique_ptr(new int); }

5 6 7 8 9 10

int main() { auto a = foo(); return 0; }

... that current C++ compilers will generate the following x64 assembler: 1 2 3 4 5 6 7 8 9 10 11 12 13

foo(): # @foo() push rbx mov rbx, rdi mov edi, 4 call operator new(unsigned long) mov qword ptr [rbx], rax mov rax, rbx pop rbx ret main: # @main push rax mov rdi, rsp call foo()

5

14 15 16 17 18 19 20 21

mov rdi, qword ptr [rsp] test rdi, rdi je .LBB2_2 call operator delete(void*) .LBB2_2: xor eax, eax pop rcx ret

But under this proposal, the following x64 assembler would be generated instead: 1 2 3 4 5 6 7 8 9 10 11 12 13

foo(): # @foo() mov edi, 4 jmp operator new(unsigned long) # TAILCALL main: # @main push rax call foo() test rax, rax je .LBB2_2 call operator delete(void*) xor eax, eax .LBB2_2: pop rcx ret

Due to moves being known by the compiler to relocate as-if by memcpy(), the optimiser has spotted that unique_ptr can be elided entirely, and just the newed pointer it contains is passed directly between functions, by CPU register.

4

Design decisions, guidelines and rationale

Previous work in this area has tended towards the complex. This proposal proposes the bare essentials for address relocatable types in the hope that the committee will be able to get this passed.

5

Technical specifications

No Technical Specifications are involved in this proposal.

6

Frequently asked questions

7

Acknowledgements

Thanks to Richard Smith for his extensive thoughts on the feasibility, and best formulation, of this proposal.

6

Thanks to Arthur O’Dwyer for his feedback from his alternative relocatable proposal.

8

References

[N4034] Pablo Halpern, Destructive Move https://wg21.link/N4034

[P0023] Denis Bider, Relocator: Efficiently moving objects https://wg21.link/P0023

[P0709] Herb Sutter, Zero-overhead deterministic exceptions https://wg21.link/P0709

[P0784] Dionne, Smith, Ranns and Vandevoorde, Standard containers and constexpr https://wg21.link/P0784

7

DEEEER0 draft 3: SG14 [[move_relocates]] -

Apr 16, 2018 - storage as no destruction is needed. This greatly simplifies the job of the compiler optimiser, making for tighter codegen, faster compile times, ...

195KB Sizes 7 Downloads 310 Views

Recommend Documents

DEEEER0 draft 4: SG14 [[move_relocates]] -
Apr 19, 2018 - to slots in global memory. The second major .... If there is a virtual inheritance anywhere in the inheritance tree. • Not all member data types are ...

SG14 -
of our titles to Linux and OS X.ааClang will parse the complete class template when it encounters it, rather than parsing the necessary functions as they are called.ааThis means that std::function is unavailable to nonRTTI projects built using Cl

Presentations schedule draft 3.pdf
Mar 3, 2018 - Finding My Black Card: Racial and Ethnic ... Narrative Integrity: Writing a Love Story of. Change. - Nicholas ... Presentations schedule draft 3.pdf.

3 Draft SOP Perguliran.pdf
Whoops! There was a problem loading more pages. Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps.

Waikaremoana Community Plan - Draft 3
and social service providers in the Wairoa and greater Hawke‟s Bay area. ..... nga kaupapa tuku iho; whanau opportunity; best whanau outcomes; coherent .... Te reo me ona tikanga, Fruit in schools, Garden, Recycling/ zero waste, Kapa haka ...

PBL Pyramid WWSU Draft-3.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. PBL Pyramid WWSU Draft-3.pdf. PBL Pyramid WWSU Draft-3.pdf. Open. Extract. Open with. Sign In. Main menu.

Press Release Translation - Concert 3 (Final Draft).pdf
Press Release Translation - Concert 3 (Final Draft).pdf. Press Release Translation - Concert 3 (Final Draft).pdf. Open. Extract. Open with. Sign In. Main menu.

145COM-1314 East Extended Essays draft 3.pdf
Congratulations on such great work and I hope you find them as. inspiring as we did. Page 3 of 175. 145COM-1314 East Extended Essays draft 3.pdf.

RPI Plan - Revised Working Draft 3.pdf
... Prosperity Region - Region 8 Prosperity Plan 3. 'It is the uniqueness of our regions – that makes. the world take notice. These regions drive. Michigan's economy and define its character.' 'Stronger local and regional collaboration will. drive

draft 3-Appointment of the Chief Executive Director.pdf
Health and Demographic Surveillance System for evaluating health system interventions ... Joint Health Systems Research Initiative and in a number of journals ...

Repossession Hearings before the Cork County Registrar Draft 3.pdf
Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Repossession Hearings before the Cork County Registrar Draft 3.pdf. Repossession Hearings before the

DRAFT 3: February 11, 2018 BACK TO THE ... - Inverse Condemnation
Feb 11, 2018 - Takings Clause is not designed to prohibit government action, but to secure compensation “in the event of otherwise proper interference amounting to a taking. .... islands; (3) surfers, divers, and canoe paddlers who use the Maui har

FKRSU IMFER Draft 3-7-2016 Revised.pdf
The paper discusses in detail the construction of the data ... affiliations: Fernández: InterAmerican Development Bank; Klein: Fletcher School, Tufts University &.

Board Minutes - July 10, 2017 DRAFT 3 Final.pdf
Misty Abbott cook at KEMS. Board appointments required by statute. Clerk of the Board Max Clark. Deputy Clerk Darla Wagner. Deputy Treasurer Megan Bough. Treasurer Diana Thornton. KPERS Agent Darla Wagner. Federal Program Administrator Max Clark. Sch

ANREP Final Draft Nov 3.pdf
... of solar, wind, hydro, geothermal, and biomass that can. provide a secure and sustainable source of energy supply. These renewable energy resources have.

ANREP Final Draft Nov 3.pdf
Rehabilitation and Development. 8. Da Afghanistan Breshna Sherkat (DABS). 9. Renewable Energy Coordination Committee. 10. National Environmental Protection Agency/NEPA Islamic Republic of Afghanistan. 11. USAID. 12. GiZ ESRA. Page 3 of 27. ANREP Fina

Repossession Hearings before the Cork County Registrar Draft 3.pdf
Page 1 of 24. CORK REPOSSESSION CASES 1. Mortgage Repossession Hearings before the Cork County Registrar: Myth versus Reality. Seamus Coffey. Department of Economics, University College Cork. Page 1 of 24 ...

4.1 MS Workshop Report- draft 3 - European Medicines Agency
Oct 9, 2017 - The MS registry landscape in Europe comprises regional and national registries that are broadly affiliated with one of ..... external systems. - Communication to patients to encourage enrolment. Recommendations. •. Raise awareness of

DRAFT 3: February 11, 2018 BACK TO THE ... - Inverse Condemnation
Feb 11, 2018 - in In re Maui Electric Co.,29 the court concluded the Sierra Club possesses a constitutional property right in a “clean and healthful environment” entitling the organization to due process protections. This allowed it to intervene

Draft 4.5.2: Complete Draft
community, guidelines to help data modellers and database designers, and part ...... C. J. Date, explores data modelling: the design of the fundamental sets qua ...... as a directed graph using open source software and automatically examined.

SG14 design guidelines for latency preserving -
2.1 Have the user supply memory to you; don't allocate it yourself . ..... And the fastest and most predictable program is always, by definition, the one which does ...

DRAFT FORMS IRO DRAFT RULES FOR CHAPTER X OF ... - Taxmann
Digital signature of the authorizing officer. This e-form is hereby approved/rejected ... (Name, Signature and. Seal of the Auditor). Attachments: 1 Optional ...

DRAFT October 31, 2012, 1:22pm DRAFT -
Figure 3.13. The isosurface (two-dimensional surface defining a contour of a function of three parameters) defining the boundary of the likelihood-based 99%.

draft - uncorrected
[3] and computer interfaces [4,5]. Myoelectric ... by Fatronik Health Research Division, CIHR grant 109291 (Canada) and ... feedback bar on a computer monitor.