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

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 17:01:11 PDT 2026


https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/221460

>From 109a7c1ecf7145bc329b2255704038e21b4fc82e Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Sat, 15 Aug 2026 00:43:35 -0400
Subject: [PATCH] [IR] Avoid collecting COMDATs for non-module printing

AssemblyWriter currently scans all module global objects in its constructor to collect referenced COMDATs. This is needed only when printing a complete module, but the scan 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.
---
 llvm/lib/IR/AsmWriter.cpp | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

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