[PATCH] D112124: [ThinLTO] Append original object file name to ThinLTO generated object file's name, so that it can be recorded in link map file.

徐霜晴 via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 19 23:03:58 PDT 2021


xushuangqing created this revision.
xushuangqing added reviewers: steven_wu, tejohnson, dexonsmith.
Herald added subscribers: ormris, pengfei, hiraditya, inglorion.
xushuangqing requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Problem
=======

When I use ThinLTO,  ThinLTOCodeGenerator will generate object file whose filename looks like this:

  0.x86_64.thinlto.o
  1.x86_64.thinlto.o
  2.x86_64.thinlto.o
  3.x86_64.thinlto.o

So when I use `-map` option during linking, the generated link map file will contain these object files. It looks like:

  # Path: ...
  # Arch: x86_64
  # Object files:
  [  0] linker synthesized
  ...
  [ 10] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/0.x86_64.thinlto.o
  [ 11] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/1.x86_64.thinlto.o
  [ 12] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/2.x86_64.thinlto.o
  [ 13] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/3.x86_64.thinlto.o
  [ 14] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/4.x86_64.thinlto.o
  
  # Sections:
  # Address Size      Segment Section
  ...
  
  # Symbols:
  # Address Size      File  Name
  ...
  
  # Dead Stripped Symbols:
  #         Size      File  Name
  ...

If I want to figure out each source file's size in the binary file, the `0.x86_64.thinlto.o` confuses me. It contains few information about the original source file.

Optimize
========

As filenames like `0.x86_64.thinlto.o` are joint in  ThinLTOCodeGenerator::writeGeneratedObject, we can append the original object file name to it.

So the file name would looks like:

  [ 10] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/0.x86_64.ViewController.o0.thinlto.o
  [ 11] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/1.x86_64.AppDelegate.o1.thinlto.o
  [ 12] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/2.x86_64.main.o2.thinlto.o
  [ 13] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/3.x86_64.SceneDelegate.o3.thinlto.o
  [ 14] /Users/xushuangqing/Library/Developer/Xcode/DerivedData/XSQLTODemo-flxqbdftarzrkoeyxgvupfvnvbue/Build/Intermediates.noindex/XSQLTODemo.build/Release-iphonesimulator/XSQLTODemo.build/Objects-normal/x86_64/XSQLTODemo_lto.o/4.x86_64.libXSQStaticLib.a(XSQObject.o)4.thinlto.o

When we analyze the link map, we can figure out each source file's size in the binary file.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112124

Files:
  llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
  llvm/lib/LTO/ThinLTOCodeGenerator.cpp


Index: llvm/lib/LTO/ThinLTOCodeGenerator.cpp
===================================================================
--- llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -964,11 +964,16 @@
 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
 std::string
 ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath,
-                                           const MemoryBuffer &OutputBuffer) {
+                                           const MemoryBuffer &OutputBuffer,
+                                           StringRef objectFileName) {
   auto ArchName = TMBuilder.TheTriple.getArchName();
   SmallString<128> OutputPath(SavedObjectsDirectoryPath);
-  llvm::sys::path::append(OutputPath,
-                          Twine(count) + "." + ArchName + ".thinlto.o");
+  if (objectFileName.empty())
+    llvm::sys::path::append(OutputPath,
+                            Twine(count) + "." + ArchName + ".thinlto.o");
+  else
+    llvm::sys::path::append(OutputPath, Twine(count) + "." + ArchName + "." +
+                                            objectFileName + ".thinlto.o");
   OutputPath.c_str(); // Ensure the string is null terminated.
   if (sys::fs::exists(OutputPath))
     sys::fs::remove(OutputPath);
@@ -1164,6 +1169,10 @@
       auto &Mod = Modules[IndexCount];
       Pool.async([&](int count) {
         auto ModuleIdentifier = Mod->getName();
+
+        StringRef moduleIdentifierFileName =
+            llvm::sys::path::filename(ModuleIdentifier);
+
         auto &ExportList = ExportLists[ModuleIdentifier];
 
         auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
@@ -1188,7 +1197,8 @@
               ProducedBinaries[count] = std::move(ErrOrBuffer.get());
             else
               ProducedBinaryFiles[count] = writeGeneratedObject(
-                  count, CacheEntryPath, *ErrOrBuffer.get());
+                  count, CacheEntryPath, *ErrOrBuffer.get(),
+                  moduleIdentifierFileName);
             return;
           }
         }
@@ -1246,7 +1256,7 @@
           return;
         }
         ProducedBinaryFiles[count] = writeGeneratedObject(
-            count, CacheEntryPath, *OutputBuffer);
+            count, CacheEntryPath, *OutputBuffer, moduleIdentifierFileName);
       }, IndexCount);
     }
   }
Index: llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
===================================================================
--- llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
+++ llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h
@@ -299,7 +299,8 @@
    * SavedObjectsDirectoryPath.
    */
   std::string writeGeneratedObject(int count, StringRef CacheEntryPath,
-                                   const MemoryBuffer &OutputBuffer);
+                                   const MemoryBuffer &OutputBuffer,
+                                   StringRef objectFileName = "");
   /**@}*/
 
 private:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112124.380859.patch
Type: text/x-patch
Size: 2961 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211020/2469a215/attachment.bin>


More information about the llvm-commits mailing list