[PATCH] D126189: [C++20][Modules] Build module static initializers per P1874R1.

Iain Sandoe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 23 05:02:50 PDT 2022


iains created this revision.
Herald added a project: All.
iains added reviewers: urnathan, Bigcheese, ChuanqiXu, jansvoboda11.
iains added a subscriber: clang-modules.
iains edited the summary of this revision.
iains edited the summary of this revision.
iains edited the summary of this revision.
iains edited the summary of this revision.
iains edited the summary of this revision.
iains updated this revision to Diff 431313.
iains added a comment.
iains published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

just updated description


Currently we only implement this for the Itanium ABI since the correct
mangling for the initializers in other ABIs is not yet known.

Intended result:

For a module (instead of the generic CXX initializer) we emit a module init
that:

- wraps the contained initializations in a control variable to ensure that the inits only happen once, even if a module is imported many times by imports of the main unit.
- calls module initialisers for imported modules first.  Note that the order of module import is not significant, and therefore neither is the order of imported module initializers.
- We then call initializers for the Global Module Fragment (if present)
- We then call initializers for the current module.
- We then call initializers for the Private Module Fragment (if present)

For a non-module TU that imports at least one module we emit a regular CXX
init that:

- Calls the initializers for any imported modules first.
- Then proceeds as normal with remaining inits.

Implementation:

- We provide the module pointer in the AST Context so that CodeGen can act on it and its sub-modules.
- We need to account for module build lines like this: ` clang -cc1 -std=c++20 Foo.pcm -emit-obj -o Foo.o` or ` clang -cc1 -std=c++20 -xc++-module Foo.cpp -emit-obj -o Foo.o`
- in order to do this, we add to ParseAST to set the module pointer in the ASTContext, once we establish that this is a module build and we know the module pointer. To be able to do this, we make the query for current module public in Sema.
- iIn CodeGen, we determine if the current build requires a CXX20-style module init and, if so, we defer any module initializers during the "Eagerly Emitted" phase.
- We then walk the module initializers at the end of the TU but before emitting deferred inits (which adds any hidden and static ones, fixing https://github.com/llvm/llvm-project/issues/51873 ).
- We then proceed to emit the deferred inits and continue to emit the CXX init function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126189

Files:
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/Sema/Sema.h
  clang/lib/CodeGen/CGDeclCXX.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Parse/ParseAST.cpp
  clang/test/CodeGen/module-intializer.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126189.431313.patch
Type: text/x-patch
Size: 21873 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220523/cf421bc5/attachment-0001.bin>


More information about the cfe-commits mailing list