11 The term ``modules`` has a lot of meanings. For the users of Clang, modules may
12 refer to ``Objective-C Modules``, ``Clang C++ Modules`` (or ``Clang Header Modules``,
13 etc.) or ``Standard C++ Modules``. The implementation of all these kinds of modules in Clang
14 has a lot of shared code, but from the perspective of users, their semantics and
15 command line interfaces are very different. This document focuses on
16 an introduction of how to use standard C++ modules in Clang.
18 There is already a detailed document about `Clang modules <Modules.html>`_, it
19 should be helpful to read `Clang modules <Modules.html>`_ if you want to know
20 more about the general idea of modules. Since standard C++ modules have different semantics
21 (and work flows) from `Clang modules`, this page describes the background and use of
22 Clang with standard C++ modules.
24 Modules exist in two forms in the C++ Language Specification. They can refer to
25 either "Named Modules" or to "Header Units". This document covers both forms.
27 Standard C++ Named modules
28 ==========================
30 This document was intended to be a manual first and foremost, however, we consider it helpful to
31 introduce some language background here for readers who are not familiar with
32 the new language feature. This document is not intended to be a language
33 tutorial; it will only introduce necessary concepts about the
34 structure and building of the project.
36 Background and terminology
37 --------------------------
42 In this document, the term ``Modules``/``modules`` refers to standard C++ modules
43 feature if it is not decorated by ``Clang``.
48 In this document, the term ``Clang Modules``/``Clang modules`` refer to Clang
49 c++ modules extension. These are also known as ``Clang header modules``,
50 ``Clang module map modules`` or ``Clang c++ modules``.
52 Module and module unit
53 ~~~~~~~~~~~~~~~~~~~~~~
55 A module consists of one or more module units. A module unit is a special
56 translation unit. Every module unit must have a module declaration. The syntax
57 of the module declaration is:
61 [export] module module_name[:partition_name];
63 Terms enclosed in ``[]`` are optional. The syntax of ``module_name`` and ``partition_name``
64 in regex form corresponds to ``[a-zA-Z_][a-zA-Z_0-9\.]*``. In particular, a literal dot ``.``
65 in the name has no semantic meaning (e.g. implying a hierarchy).
67 In this document, module units are classified into:
69 * Primary module interface unit.
71 * Module implementation unit.
73 * Module interface partition unit.
75 * Internal module partition unit.
77 A primary module interface unit is a module unit whose module declaration is
78 ``export module module_name;``. The ``module_name`` here denotes the name of the
79 module. A module should have one and only one primary module interface unit.
81 A module implementation unit is a module unit whose module declaration is
82 ``module module_name;``. A module could have multiple module implementation
83 units with the same declaration.
85 A module interface partition unit is a module unit whose module declaration is
86 ``export module module_name:partition_name;``. The ``partition_name`` should be
87 unique within any given module.
89 An internal module partition unit is a module unit whose module declaration
90 is ``module module_name:partition_name;``. The ``partition_name`` should be
91 unique within any given module.
93 In this document, we use the following umbrella terms:
95 * A ``module interface unit`` refers to either a ``primary module interface unit``
96 or a ``module interface partition unit``.
98 * An ``importable module unit`` refers to either a ``module interface unit``
99 or a ``internal module partition unit``.
101 * A ``module partition unit`` refers to either a ``module interface partition unit``
102 or a ``internal module partition unit``.
104 Built Module Interface file
105 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 A ``Built Module Interface file`` stands for the precompiled result of an importable module unit.
108 It is also called the acronym ``BMI`` genrally.
110 Global module fragment
111 ~~~~~~~~~~~~~~~~~~~~~~
113 In a module unit, the section from ``module;`` to the module declaration is called the global module fragment.
116 How to build projects using modules
117 -----------------------------------
122 Let's see a "hello world" example that uses modules.
130 export void hello() {
131 std::cout << "Hello World!\n";
143 .. code-block:: console
145 $ clang++ -std=c++20 Hello.cppm --precompile -o Hello.pcm
146 $ clang++ -std=c++20 use.cpp -fprebuilt-module-path=. Hello.pcm -o Hello.out
150 In this example, we make and use a simple module ``Hello`` which contains only a
151 primary module interface unit ``Hello.cppm``.
153 Then let's see a little bit more complex "hello world" example which uses the 4 kinds of module units.
159 export import :interface_part;
163 // interface_part.cppm
164 export module M:interface_part;
172 import :interface_part;
174 std::string W = "World.";
176 std::cout << W << std::endl;
184 std::cout << "Hello ";
195 Then we are able to compile the example by the following command:
197 .. code-block:: console
199 # Precompiling the module
200 $ clang++ -std=c++20 interface_part.cppm --precompile -o M-interface_part.pcm
201 $ clang++ -std=c++20 impl_part.cppm --precompile -fprebuilt-module-path=. -o M-impl_part.pcm
202 $ clang++ -std=c++20 M.cppm --precompile -fprebuilt-module-path=. -o M.pcm
203 $ clang++ -std=c++20 Impl.cpp -fmodule-file=M.pcm -c -o Impl.o
206 $ clang++ -std=c++20 User.cpp -fprebuilt-module-path=. -c -o User.o
208 # Compiling the module and linking it together
209 $ clang++ -std=c++20 M-interface_part.pcm -c -o M-interface_part.o
210 $ clang++ -std=c++20 M-impl_part.pcm -c -o M-impl_part.o
211 $ clang++ -std=c++20 M.pcm -c -o M.o
212 $ clang++ User.o M-interface_part.o M-impl_part.o M.o Impl.o -o a.out
214 We explain the options in the following sections.
216 How to enable standard C++ modules
217 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219 Currently, standard C++ modules are enabled automatically
220 if the language standard is ``-std=c++20`` or newer.
221 The ``-fmodules-ts`` option is deprecated and is planned to be removed.
226 It is possible to generate a BMI for an importable module unit by specifying the ``--precompile`` option.
228 File name requirement
229 ~~~~~~~~~~~~~~~~~~~~~
231 The file name of an ``importable module unit`` should end with ``.cppm``
232 (or ``.ccm``, ``.cxxm``, ``.c++m``). The file name of a ``module implementation unit``
233 should end with ``.cpp`` (or ``.cc``, ``.cxx``, ``.c++``).
235 The file name of BMIs should end with ``.pcm``.
236 The file name of the BMI of a ``primary module interface unit`` should be ``module_name.pcm``.
237 The file name of BMIs of ``module partition unit`` should be ``module_name-partition_name.pcm``.
239 If the file names use different extensions, Clang may fail to build the module.
240 For example, if the filename of an ``importable module unit`` ends with ``.cpp`` instead of ``.cppm``,
241 then we can't generate a BMI for the ``importable module unit`` by ``--precompile`` option
242 since ``--precompile`` option now would only run preprocessor, which is equal to `-E` now.
243 If we want the filename of an ``importable module unit`` ends with other suffixes instead of ``.cppm``,
244 we could put ``-x c++-module`` in front of the file. For example,
252 export void hello() {
253 std::cout << "Hello World!\n";
263 Now the filename of the ``module interface`` ends with ``.cpp`` instead of ``.cppm``,
264 we can't compile them by the original command lines. But we are still able to do it by:
266 .. code-block:: console
268 $ clang++ -std=c++20 -x c++-module Hello.cpp --precompile -o Hello.pcm
269 $ clang++ -std=c++20 use.cpp -fprebuilt-module-path=. Hello.pcm -o Hello.out
273 How to specify the dependent BMIs
274 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
276 The option ``-fprebuilt-module-path`` tells the compiler the path where to search for dependent BMIs.
277 It may be used multiple times just like ``-I`` for specifying paths for header files. The look up rule here is:
279 * (1) When we import module M. The compiler would look up M.pcm in the directories specified
280 by ``-fprebuilt-module-path``.
281 * (2) When we import partition module unit M:P. The compiler would look up M-P.pcm in the
282 directories specified by ``-fprebuilt-module-path``.
284 Another way to specify the dependent BMIs is to use ``-fmodule-file``. The main difference
285 is that ``-fprebuilt-module-path`` takes a directory, whereas ``-fmodule-file`` requires a
286 specific file. In case both the ``-fprebuilt-module-path`` and ``-fmodule-file`` exist, the
287 ``-fmodule-file`` option takes higher precedence. In another word, if the compiler finds the wanted
288 BMI specified by ``-fmodule-file``, the compiler wouldn't look up again in the directories specified
289 by ``-fprebuilt-module-path``.
291 When we compile a ``module implementation unit``, we must pass the BMI of the corresponding
292 ``primary module interface unit`` by ``-fmodule-file``
293 since the language specification says a module implementation unit implicitly imports
294 the primary module interface unit.
298 A module-declaration that contains neither an export-keyword nor a module-partition implicitly
299 imports the primary module interface unit of the module as if by a module-import-declaration.
301 Again, the option ``-fmodule-file`` may occur multiple times.
302 For example, the command line to compile ``M.cppm`` in
303 the above example could be rewritten into:
305 .. code-block:: console
307 $ clang++ -std=c++20 M.cppm --precompile -fmodule-file=M-interface_part.pcm -fmodule-file=M-impl_part.pcm -o M.pcm
309 ``-fprebuilt-module-path`` is more convenient and ``-fmodule-file`` is faster since
310 it saves time for file lookup.
312 Remember that module units still have an object counterpart to the BMI
313 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
315 It is easy to forget to compile BMIs at first since we may envision module interfaces like headers.
316 However, this is not true.
317 Module units are translation units. We need to compile them to object files
318 and link the object files like the example shows.
320 For example, the traditional compilation processes for headers are like:
324 src1.cpp -+> clang++ src1.cpp --> src1.o ---,
325 hdr1.h --' +-> clang++ src1.o src2.o -> executable
327 src2.cpp -+> clang++ src2.cpp --> src2.o ---'
329 And the compilation process for module units are like:
333 src1.cpp ----------------------------------------+> clang++ src1.cpp -------> src1.o -,
334 (header unit) hdr1.h -> clang++ hdr1.h ... -> hdr1.pcm --' +-> clang++ src1.o mod1.o src2.o -> executable
335 mod1.cppm -> clang++ mod1.cppm ... -> mod1.pcm --,--> clang++ mod1.pcm ... -> mod1.o -+
336 src2.cpp ----------------------------------------+> clang++ src2.cpp -------> src2.o -'
338 As the diagrams show, we need to compile the BMI from module units to object files and link the object files.
339 (But we can't do this for the BMI from header units. See the later section for the definition of header units)
341 If we want to create a module library, we can't just ship the BMIs in an archive.
342 We must compile these BMIs(``*.pcm``) into object files(``*.o``) and add those object files to the archive instead.
344 Consistency Requirement
345 ~~~~~~~~~~~~~~~~~~~~~~~
347 If we envision modules as a cache to speed up compilation, then - as with other caching techniques -
348 it is important to keep cache consistency.
349 So **currently** Clang will do very strict check for consistency.
354 The language option of module units and their non-module-unit users should be consistent.
355 The following example is not allowed:
365 .. code-block:: console
367 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
368 $ clang++ -std=c++2b Use.cpp -fprebuilt-module-path=.
370 The compiler would reject the example due to the inconsistent language options.
371 Not all options are language options.
372 For example, the following example is allowed:
374 .. code-block:: console
376 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
377 # Inconsistent optimization level.
378 $ clang++ -std=c++20 -O3 Use.cpp -fprebuilt-module-path=.
379 # Inconsistent debugging level.
380 $ clang++ -std=c++20 -g Use.cpp -fprebuilt-module-path=.
382 Although the two examples have inconsistent optimization and debugging level, both of them are accepted.
384 Note that **currently** the compiler doesn't consider inconsistent macro definition a problem. For example:
386 .. code-block:: console
388 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
389 # Inconsistent optimization level.
390 $ clang++ -std=c++20 -O3 -DNDEBUG Use.cpp -fprebuilt-module-path=.
392 Currently Clang would accept the above example. But it may produce surprising results if the
393 debugging code depends on consistent use of ``NDEBUG`` also in other translation units.
395 Source content consistency
396 ^^^^^^^^^^^^^^^^^^^^^^^^^^
398 When the compiler reads a BMI, the compiler will check the consistency of the corresponding
399 source files. For example:
405 export template <class T>
416 .. code-block:: console
418 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
420 $ clang++ -std=c++20 Use.cpp -fmodule-file=M.pcm
422 The compiler would reject the example since the compiler failed to find the source file to check the consistency.
423 So the following example would be rejected too.
425 .. code-block:: console
427 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
428 $ echo "int i=0;" >> M.cppm
429 $ clang++ -std=c++20 Use.cpp -fmodule-file=M.pcm
431 The compiler would reject it too since the compiler detected the file was changed.
433 But it is OK to move the BMI as long as the source files remain:
435 .. code-block:: console
437 $ clang++ -std=c++20 M.cppm --precompile -o M.pcm
440 $ clang++ -std=c++20 Use.cpp -fmodule-file=tmp/M.pcm
442 The above example would be accepted.
444 If the user doesn't want to follow the consistency requirement due to some reasons (e.g., distributing BMI),
445 the user could try to use ``-Xclang -fmodules-embed-all-files`` when producing BMI. For example:
447 .. code-block:: console
449 $ clang++ -std=c++20 M.cppm --precompile -Xclang -fmodules-embed-all-files -o M.pcm
451 $ clang++ -std=c++20 Use.cpp -fmodule-file=M.pcm
453 Now the compiler would accept the above example.
454 Important note: Xclang options are intended to be used by compiler internally and its semantics
455 are not guaranteed to be preserved in future versions.
457 Also the compiler will record the path to the header files included in the global module fragment and compare the
458 headers when imported. For example,
465 std::cout << "Hello World.\n";
472 export using ::Hello;
480 Then it is problematic if we remove ``foo.h`` before import `foo` module.
482 .. code-block:: console
484 $ clang++ -std=c++20 foo.cppm --precompile -o foo.pcm
485 $ mv foo.h foo.orig.h
486 # The following one is rejected
487 $ clang++ -std=c++20 Use.cpp -fmodule-file=foo.pcm -c
489 The above case will rejected. And we're still able to workaround it by ``-Xclang -fmodules-embed-all-files`` option:
491 .. code-block:: console
493 $ clang++ -std=c++20 foo.cppm --precompile -Xclang -fmodules-embed-all-files -o foo.pcm
494 $ mv foo.h foo.orig.h
495 $ clang++ -std=c++20 Use.cpp -fmodule-file=foo.pcm -c -o Use.o
496 $ clang++ Use.o foo.pcm
501 The declarations in a module unit which are not in the global module fragment have new linkage names.
512 The linkage name of ``NS::foo()`` would be ``_ZN2NSW1M3fooEv``.
513 This couldn't be demangled by previous versions of the debugger or demangler.
514 As of LLVM 15.x, users can utilize ``llvm-cxxfilt`` to demangle this:
516 .. code-block:: console
518 $ llvm-cxxfilt _ZN2NSW1M3fooEv
520 The result would be ``NS::foo@M()``, which reads as ``NS::foo()`` in module ``M``.
522 The ABI implies that we can't declare something in a module unit and define it in a non-module unit (or vice-versa),
523 as this would result in linking errors.
528 The following describes issues in the current implementation of modules.
529 Please see https://github.com/llvm/llvm-project/labels/clang%3Amodules for more issues
530 or file a new issue if you don't find an existing one.
531 If you're going to create a new issue for standard C++ modules,
532 please start the title with ``[C++20] [Modules]`` (or ``[C++2b] [Modules]``, etc)
533 and add the label ``clang:modules`` (if you have permissions for that).
535 For higher level support for proposals, you could visit https://clang.llvm.org/cxx_status.html.
537 Support for clang-scan-deps
538 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
540 The support for clang-scan-deps may be the most urgent problem for modules now.
541 Without the support for clang-scan-deps, it's hard to involve build systems.
542 This means that users could only play with modules through makefiles or by writing a parser by hand.
543 It blocks more uses for modules, which will block more defect reports or requirements.
545 This is tracked in: https://github.com/llvm/llvm-project/issues/51792.
547 Ambiguous deduction guide
548 ~~~~~~~~~~~~~~~~~~~~~~~~~
550 Currently, when we call deduction guides in global module fragment,
551 we may get incorrect diagnosing message like: `ambiguous deduction`.
553 So if we're using deduction guide from global module fragment, we probably need to write:
557 std::lock_guard<std::mutex> lk(mutex);
563 std::lock_guard lk(mutex);
565 This is tracked in: https://github.com/llvm/llvm-project/issues/56916
567 Ignored PreferredName Attribute
568 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
570 Due to a tricky problem, when Clang writes BMIs, Clang will ignore the ``preferred_name`` attribute, if any.
571 This implies that the ``preferred_name`` wouldn't show in debugger or dumping.
573 This is tracked in: https://github.com/llvm/llvm-project/issues/56490
575 Don't emit macros about module declaration
576 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
578 This is covered by P1857R3. We mention it again here since users may abuse it before we implement it.
580 Someone may want to write code which could be compiled both by modules or non-modules.
581 A direct idea would be use macros like:
587 EXPORT_MODULE MODULE_NAME;
591 So this file could be triggered like a module unit or a non-module unit depending on the definition
593 However, this kind of usage is forbidden by P1857R3 but we haven't implemented P1857R3 yet.
594 This means that is possible to write illegal modules code now, and obviously this will stop working
595 once P1857R3 is implemented.
596 A simple suggestion would be "Don't play macro tricks with module declarations".
598 This is tracked in: https://github.com/llvm/llvm-project/issues/56917
600 In consistent filename suffix requirement for importable module units
601 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
603 Currently, clang requires the file name of an ``importable module unit`` should end with ``.cppm``
604 (or ``.ccm``, ``.cxxm``, ``.c++m``). However, the behavior is inconsistent with other compilers.
606 This is tracked in: https://github.com/llvm/llvm-project/issues/57416
611 How to build projects using header unit
612 ---------------------------------------
617 For the following example,
623 std::cout << "Hello World.\n";
626 we could compile it as
628 .. code-block:: console
630 $ clang++ -std=c++20 -xc++-system-header --precompile iostream -o iostream.pcm
631 $ clang++ -std=c++20 -fmodule-file=iostream.pcm main.cpp
636 Similar to named modules, we could use ``--precompile`` to produce the BMI.
637 But we need to specify that the input file is a header by ``-xc++-system-header`` or ``-xc++-user-header``.
639 Also we could use `-fmodule-header={user,system}` option to produce the BMI for header units
640 which has suffix like `.h` or `.hh`.
641 The value of `-fmodule-header` means the user search path or the system search path.
642 The default value for `-fmodule-header` is `user`.
650 std::cout << "Hello World.\n";
659 We could compile it as:
661 .. code-block:: console
663 $ clang++ -std=c++20 -fmodule-header foo.h -o foo.pcm
664 $ clang++ -std=c++20 -fmodule-file=foo.pcm use.cpp
666 For headers which don't have a suffix, we need to pass ``-xc++-header``
667 (or ``-xc++-system-header`` or ``-xc++-user-header``) to mark it as a header.
678 .. code-block:: console
680 $ clang++ -std=c++20 -fmodule-header=system -xc++-header iostream -o iostream.pcm
681 $ clang++ -std=c++20 -fmodule-file=iostream.pcm use.cpp
683 How to specify the dependent BMIs
684 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686 We could use ``-fmodule-file`` to specify the BMIs, and this option may occur multiple times as well.
688 With the existing implementation ``-fprebuilt-module-path`` cannot be used for header units
689 (since they are nominally anonymous).
690 For header units, use ``-fmodule-file`` to include the relevant PCM file for each header unit.
692 This is expect to be solved in future editions of the compiler either by the tooling finding and specifying
693 the -fmodule-file or by the use of a module-mapper that understands how to map the header name to their PCMs.
695 Don't compile the BMI
696 ~~~~~~~~~~~~~~~~~~~~~
698 Another difference with modules is that we can't compile the BMI from a header unit.
701 .. code-block:: console
703 $ clang++ -std=c++20 -xc++-system-header --precompile iostream -o iostream.pcm
704 # This is not allowed!
705 $ clang++ iostream.pcm -c -o iostream.o
707 It makes sense due to the semantics of header units, which are just like headers.
712 The C++ spec allows the vendors to convert ``#include header-name`` to ``import header-name;`` when possible.
713 Currently, Clang would do this translation for the ``#include`` in the global module fragment.
715 For example, the following two examples are the same:
722 export void Hello() {
723 std::cout << "Hello.\n";
726 with the following one:
733 export void Hello() {
734 std::cout << "Hello.\n";
737 .. code-block:: console
739 $ clang++ -std=c++20 -xc++-system-header --precompile iostream -o iostream.pcm
740 $ clang++ -std=c++20 -fmodule-file=iostream.pcm --precompile M.cppm -o M.cpp
742 In the latter example, the Clang could find the BMI for the ``<iostream>``
743 so it would try to replace the ``#include <iostream>`` to ``import <iostream>;`` automatically.
746 Relationships between Clang modules
747 -----------------------------------
749 Header units have pretty similar semantics with Clang modules.
750 The semantics of both of them are like headers.
752 In fact, we could even "mimic" the sytle of header units by Clang modules:
758 header "/path/to/libstdcxx/iostream"
761 .. code-block:: console
763 $ clang++ -std=c++20 -fimplicit-modules -fmodule-map-file=.modulemap main.cpp
765 It would be simpler if we are using libcxx:
767 .. code-block:: console
769 $ clang++ -std=c++20 main.cpp -fimplicit-modules -fimplicit-module-maps
771 Since there is already one
772 `module map <https://github.com/llvm/llvm-project/blob/main/libcxx/include/module.modulemap.in>`_
773 in the source of libcxx.
775 Then immediately leads to the question: why don't we implement header units through Clang header modules?
777 The main reason for this is that Clang modules have more semantics like hierarchy or
778 wrapping multiple headers together as a big module.
779 However, these things are not part of Standard C++ Header units,
780 and we want to avoid the impression that these additional semantics get interpreted as Standard C++ behavior.
782 Another reason is that there are proposals to introduce module mappers to the C++ standard
783 (for example, https://wg21.link/p1184r2).
784 If we decide to reuse Clang's modulemap, we may get in trouble once we need to introduce another module mapper.
786 So the final answer for why we don't reuse the interface of Clang modules for header units is that
787 there are some differences between header units and Clang modules and that ignoring those
788 differences now would likely become a problem in the future.
793 How modules speed up compilation
794 --------------------------------
796 A classic theory for the reason why modules speed up the compilation is:
797 if there are ``n`` headers and ``m`` source files and each header is included by each source file,
798 then the complexity of the compilation is ``O(n*m)``;
799 But if there are ``n`` module interfaces and ``m`` source files, the complexity of the compilation is
800 ``O(n+m)``. So, using modules would be a big win when scaling.
801 In a simpler word, we could get rid of many redundant compilations by using modules.
803 Roughly, this theory is correct. But the problem is that it is too rough.
804 The behavior depends on the optimization level, as we will illustrate below.
806 First is ``O0``. The compilation process is described in the following graph.
810 ├-------------frontend----------┼-------------middle end----------------┼----backend----┤
812 └---parsing----sema----codegen--┴----- transformations ---- codegen ----┴---- codegen --┘
814 ┌---------------------------------------------------------------------------------------┐
818 └---------------------------------------------------------------------------------------┘
828 Here we can see that the source file (could be a non-module unit or a module unit) would get processed by the
830 But the imported code would only get involved in semantic analysis, which is mainly about name lookup,
831 overload resolution and template instantiation.
832 All of these processes are fast relative to the whole compilation process.
833 More importantly, the imported code only needs to be processed once in frontend code generation,
834 as well as the whole middle end and backend.
835 So we could get a big win for the compilation time in O0.
837 But with optimizations, things are different:
839 (we omit ``code generation`` part for each end due to the limited space)
843 ├-------- frontend ---------┼--------------- middle end --------------------┼------ backend ----┤
845 └--- parsing ---- sema -----┴--- optimizations --- IPO ---- optimizations---┴--- optimizations -┘
847 ┌-----------------------------------------------------------------------------------------------┐
851 └-----------------------------------------------------------------------------------------------┘
852 ┌---------------------------------------┐
858 └---------------------------------------┘
860 It would be very unfortunate if we end up with worse performance after using modules.
861 The main concern is that when we compile a source file, the compiler needs to see the function body
862 of imported module units so that it can perform IPO (InterProcedural Optimization, primarily inlining
863 in practice) to optimize functions in current source file with the help of the information provided by
864 the imported module units.
865 In other words, the imported code would be processed again and again in importee units
866 by optimizations (including IPO itself).
867 The optimizations before IPO and the IPO itself are the most time-consuming part in whole compilation process.
868 So from this perspective, we might not be able to get the improvements described in the theory.
869 But we could still save the time for optimizations after IPO and the whole backend.
871 Overall, at ``O0`` the implementations of functions defined in a module will not impact module users,
872 but at higher optimization levels the definitions of such functions are provided to user compilations for the
873 purposes of optimization (but definitions of these functions are still not included in the use's object file)-
874 this means the build speedup at higher optimization levels may be lower than expected given ``O0`` experience,
875 but does provide by more optimization opportunities.