[PATCH] D138552: [docs] Document that the modules can improve the linking time

Chuanqi Xu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 23 01:43:27 PST 2022


ChuanqiXu created this revision.
ChuanqiXu added reviewers: dblaikie, iains, aaronmondal, MaskRay.
Herald added a subscriber: StephenFan.
Herald added a project: All.
ChuanqiXu requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

We found that the linking time decreases significantly after using modules. It is a big surprise and I feel like it is good to document it. So that other people whose project is pretty slow at linking time now may have stronger motivation to use modules.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138552

Files:
  clang/docs/StandardCPlusPlusModules.rst


Index: clang/docs/StandardCPlusPlusModules.rst
===================================================================
--- clang/docs/StandardCPlusPlusModules.rst
+++ clang/docs/StandardCPlusPlusModules.rst
@@ -910,6 +910,70 @@
 this means the build speedup at higher optimization levels may be lower than expected given ``O0`` experience, 
 but does provide by more optimization opportunities.
 
+Can modules speedup linking?
+----------------------------
+
+It is possible. Since the use of modules can reduce the symbols in the object files.
+
+Here is an example,
+
+.. code-block:: c++
+
+  // A.h
+  inline void notDirectlyUsed() {}
+  inline void DirectlyUsed() { notDirectlyUsed(); }
+
+  // A.cc
+  #include "A.h"
+  void foo() { DirectlyUsed(); }
+
+then compile it and look the generated symbol with the following commands
+
+.. code-block:: console
+
+  $ clang++ -std=c++20 A.cc -c -o A.o
+  $ nm -C A.o
+  0000000000000000 W DirectlyUsed()
+  0000000000000000 W notDirectlyUsed()
+  0000000000000000 T foo()
+
+now let's check the result after using modules with the following example:
+
+.. code-block:: c++
+
+  // A.cppm
+  export module A;
+  void notDirectlyUsed() {}
+  export void DirectlyUsed() { notDirectlyUsed(); }
+  
+  // A.cc
+  #include "A.h"
+  void foo() { DirectlyUsed(); }
+
+compile it with the following commands:
+
+.. code-block:: console
+
+  $ clang++ -std=c++20 A.cppm --precompile -o A.pcm
+  $ clang++ -std=c++20 A.cc -fprebuilt-module-path=. -c -o A.o
+  $ nm -C A.o
+  0000000000000000 t _GLOBAL__sub_I_Use.cc
+  0000000000000000 T foo()
+                   U _ZGIW1A
+                   U _ZW1A12DirectlyUsedv
+
+First we can find that there are 2 additional symbols ``_GLOBAL__sub_I_Use`` and
+``_ZGIW1A``. These 2 symbols are used to initialize the current object file and
+the module A.
+
+The interesting part here is that there is not the symbol for ``notDirectlyUsed``.
+In a real world project, the number of ``notDirectlyUsed`` symbols should
+usually be much more than the ``DirectlyUsed`` ones.
+
+So that we can reduce many symbols in the object files and so we can speedup linking
+by using modules. Note that things may be better with optimizations, since the
+``DirectlyUsed`` ones may be optimized out.
+
 Interoperability with Clang Modules
 -----------------------------------
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138552.477417.patch
Type: text/x-patch
Size: 2338 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221123/d5a4aee2/attachment.bin>


More information about the cfe-commits mailing list