[llvm] 839093d - [IR] Avoid collecting COMDATs for non-module printing (#221460)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 18:45:55 PDT 2026


Author: Yaxun (Sam) Liu
Date: 2026-09-16T21:45:50-04:00
New Revision: 839093dddb95ca2680310ae880267e558d9f492c

URL: https://github.com/llvm/llvm-project/commit/839093dddb95ca2680310ae880267e558d9f492c
DIFF: https://github.com/llvm/llvm-project/commit/839093dddb95ca2680310ae880267e558d9f492c.diff

LOG: [IR] Avoid collecting COMDATs for non-module printing (#221460)

`AssemblyWriter` scans every global object in its constructor to collect
referenced COMDATs. That collection is only used when printing a
complete module, but the constructor also runs for standalone value and
instruction printing.

Move COMDAT emission into `printModule()`. Print each group at its first
global-object use and keep only a local set to avoid duplicate
declarations.

On `bench/sqlite/original/shell.ll` from `llvm-opt-benchmark`, the
median `opt -passes=dot-cfg` time dropped from 1.65 s to 0.40 s, about
4.1x faster. All 687 generated DOT files matched after normalizing
process-specific node IDs.

Added: 
    

Modified: 
    llvm/lib/IR/AsmWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 8e57bff1d36c9..9f298020d6795 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -22,7 +22,6 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
-#include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
@@ -2960,7 +2959,6 @@ class AssemblyWriter {
   SlotTracker &Machine;
   TypePrinting TypePrinter;
   AssemblyAnnotationWriter *AnnotationWriter = nullptr;
-  SetVector<const Comdat *> Comdats;
   bool IsForDebug;
   bool ShouldPreserveUseListOrder;
   UseListOrderMap UseListOrders;
@@ -3068,13 +3066,7 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
       ShouldPreserveUseListOrder(
           PreserveAssemblyUseListOrder.getNumOccurrences()
               ? PreserveAssemblyUseListOrder
-              : ShouldPreserveUseListOrder) {
-  if (!TheModule)
-    return;
-  for (const GlobalObject &GO : TheModule->global_objects())
-    if (const Comdat *C = GO.getComdat())
-      Comdats.insert(C);
-}
+              : ShouldPreserveUseListOrder) {}
 
 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
                                const ModuleSummaryIndex *Index, bool IsForDebug)
@@ -3243,13 +3235,16 @@ void AssemblyWriter::printModule(const Module *M) {
 
   printTypeIdentities();
 
-  // Output all comdats.
-  if (!Comdats.empty())
-    Out << '\n';
-  for (const Comdat *C : Comdats) {
-    printComdat(C);
-    if (C != Comdats.back())
+  {
+    // Output each used comdat at its first global object.
+    SmallPtrSet<const Comdat *, 8> PrintedComdats;
+    for (const GlobalObject &GO : M->global_objects()) {
+      const Comdat *C = GO.getComdat();
+      if (!C || !PrintedComdats.insert(C).second)
+        continue;
       Out << '\n';
+      printComdat(C);
+    }
   }
 
   // Output all globals.


        


More information about the llvm-commits mailing list