[llvm] [NFC][AsmWriter] Add emitMDNodeAnnot (PR #198317)

Harald van Dijk via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 06:58:50 PDT 2026


https://github.com/hvdijk updated https://github.com/llvm/llvm-project/pull/198317

>From 67e9d95206d34270dd6c428d8d8352174ceae293 Mon Sep 17 00:00:00 2001
From: Harald van Dijk <hdijk at accesssoftek.com>
Date: Mon, 18 May 2026 15:13:04 +0100
Subject: [PATCH] [NFC] Add AAW::emitMDNodeAnnot

Similarly to the other AAW::emit*Annot functions, this may be used to
emit a comment before a metadata node.
---
 llvm/include/llvm/IR/AssemblyAnnotationWriter.h | 5 +++++
 llvm/lib/IR/AsmWriter.cpp                       | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/llvm/include/llvm/IR/AssemblyAnnotationWriter.h b/llvm/include/llvm/IR/AssemblyAnnotationWriter.h
index d68bcbf85b2b7..bcc5244c0b93e 100644
--- a/llvm/include/llvm/IR/AssemblyAnnotationWriter.h
+++ b/llvm/include/llvm/IR/AssemblyAnnotationWriter.h
@@ -23,6 +23,7 @@ namespace llvm {
 class Function;
 class BasicBlock;
 class Instruction;
+class MDNode;
 class Value;
 class formatted_raw_ostream;
 
@@ -53,6 +54,10 @@ class LLVM_ABI AssemblyAnnotationWriter {
   virtual void emitInstructionAnnot(const Instruction *,
                                     formatted_raw_ostream &) {}
 
+  /// emitMDNodeAnnot - This may be implemented to emit a string right before a
+  /// metadata node is emitted.
+  virtual void emitMDNodeAnnot(const MDNode *, formatted_raw_ostream &) {}
+
   /// printInfoComment - This may be implemented to emit a comment to the
   /// right of an instruction or global value.
   virtual void printInfoComment(const Value &, formatted_raw_ostream &) {}
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 106fd6541360c..af27a19caaa81 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -4978,6 +4978,9 @@ void AssemblyWriter::printMetadataAttachments(
 }
 
 void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
+  if (AnnotationWriter)
+    AnnotationWriter->emitMDNodeAnnot(Node, Out);
+
   Out << '!' << Slot << " = ";
   printMDNodeBody(Node);
   Out << "\n";



More information about the llvm-commits mailing list