[llvm] 011fab8 - [DirectX] Move IR printing to DXILPrettyPrinter (#198318)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 06:41:39 PDT 2026
Author: Harald van Dijk
Date: 2026-06-04T14:41:34+01:00
New Revision: 011fab8c30c6ec7a937c33e21f5a586391a51de2
URL: https://github.com/llvm/llvm-project/commit/011fab8c30c6ec7a937c33e21f5a586391a51de2
DIFF: https://github.com/llvm/llvm-project/commit/011fab8c30c6ec7a937c33e21f5a586391a51de2.diff
LOG: [DirectX] Move IR printing to DXILPrettyPrinter (#198318)
By doing the IR printing inside DXILPrettyPrinter, we have the option to
customise what we print and include the info that we collect and
generate in DXILDebugInfo.
Added:
llvm/test/CodeGen/DirectX/DebugInfo/di-compile-unit-versioned-language.ll
llvm/test/CodeGen/DirectX/DebugInfo/di-subprogram.ll
Modified:
llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
llvm/test/CodeGen/DirectX/debug-info.ll
llvm/test/CodeGen/DirectX/llc-pipeline.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp b/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
index 12e54ad2cc73e..d28a2ef1095f8 100644
--- a/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
@@ -8,16 +8,24 @@
#include "DXILPrettyPrinter.h"
#include "DirectX.h"
+#include "DirectXIRPasses/DXILDebugInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/DXILResource.h"
+#include "llvm/IR/AssemblyAnnotationWriter.h"
+#include "llvm/IR/DebugInfo.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/FormatAdapters.h"
#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
+using namespace llvm::dxil;
static StringRef getRCName(dxil::ResourceClass RC) {
switch (RC) {
@@ -251,12 +259,102 @@ static void prettyPrintResources(raw_ostream &OS, const DXILResourceMap &DRM,
OS << ";\n";
}
+namespace {
+class DXILAssemblyAnnotationWriter : public llvm::AssemblyAnnotationWriter {
+private:
+ ModuleSlotTracker &MST;
+ AbstractSlotTrackerStorage &STS;
+ const DXILDebugInfoMap &DI;
+
+public:
+ DXILAssemblyAnnotationWriter(ModuleSlotTracker &MST,
+ AbstractSlotTrackerStorage &STS,
+ const DXILDebugInfoMap &DI)
+ : MST(MST), STS(STS), DI(DI) {}
+
+ void emitInstructionAnnot(const Instruction *OrigI,
+ formatted_raw_ostream &os) override {
+ if (const Instruction *I = &DI.getDXILInstruction(*OrigI); I != OrigI) {
+ os << "; DXIL: to be replaced with: ";
+ I->print(os, MST);
+ os << "\n";
+ }
+ }
+
+ void emitMDNodeAnnot(const MDNode *N, formatted_raw_ostream &os) override {
+ if (const Metadata *NewMD = DI.MDReplace.lookup(N)) {
+ if (const auto *NewN = dyn_cast<MDNode>(NewMD))
+ if (STS.getMetadataSlot(NewN) == -1)
+ STS.createMetadataSlot(NewN);
+
+ os << "; DXIL: ";
+ N->printAsOperand(os, MST);
+ os << ": to be replaced by: ";
+ NewMD->printAsOperand(os, MST);
+ os << "\n";
+ return;
+ }
+
+ if (const Metadata *ExtraMD = DI.MDExtra.lookup(N)) {
+ if (const auto *ExtraN = dyn_cast<MDNode>(ExtraMD))
+ if (STS.getMetadataSlot(ExtraN) == -1)
+ STS.createMetadataSlot(ExtraN);
+
+ os << "; DXIL: ";
+ N->printAsOperand(os, MST);
+ os << ": additional data: ";
+ ExtraMD->printAsOperand(os, MST);
+ os << "\n";
+ return;
+ }
+ }
+};
+} // namespace
+
+static void prettyPrint(raw_ostream &OS, Module &M, const DXILResourceMap &DRM,
+ DXILResourceTypeMap &DRTM) {
+ formatted_raw_ostream FOS(OS);
+
+ prettyPrintResources(FOS, DRM, DRTM);
+
+ DXILDebugInfoMap DI = DXILDebugInfoPass::run(M);
+
+ ModuleSlotTracker MST(&M);
+ AbstractSlotTrackerStorage *STS = nullptr;
+ unsigned NextMetadataSlot = 0;
+ MST.setProcessHook(
+ [&](AbstractSlotTrackerStorage *STS_, const Module *, bool) {
+ STS = STS_;
+ NextMetadataSlot = STS->getNextMetadataSlot();
+ });
+ // Force initialisation. ModuleSlotTracker does not have a dedicated function
+ // for this so trigger it through a dummy print.
+ MDNode::get(M.getContext(), {})->print(llvm::nulls(), MST);
+ assert(STS && "Slot tracker storage should have been initialised");
+
+ DXILAssemblyAnnotationWriter DAAW(MST, *STS, DI);
+ M.print(FOS, &DAAW);
+
+ ModuleSlotTracker::MachineMDNodeListType MDNodes;
+ MST.collectMDNodes(MDNodes, NextMetadataSlot, ~0u);
+ std::sort(MDNodes.begin(), MDNodes.end(),
+ [](const std::pair<unsigned, const MDNode *> &A,
+ const std::pair<unsigned, const MDNode *> &B) {
+ return A.first < B.first;
+ });
+ for (auto [_, MDNode] : MDNodes) {
+ DAAW.emitMDNodeAnnot(MDNode, FOS);
+ MDNode->print(FOS, MST);
+ FOS << "\n";
+ }
+}
+
PreservedAnalyses DXILPrettyPrinterPass::run(Module &M,
ModuleAnalysisManager &MAM) {
const DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
DXILResourceTypeMap &DRTM = MAM.getResult<DXILResourceTypeAnalysis>(M);
- prettyPrintResources(OS, DRM, DRTM);
- return PreservedAnalyses::all();
+ prettyPrint(OS, M, DRM, DRTM);
+ return PreservedAnalyses::none();
}
namespace {
@@ -268,13 +366,10 @@ class DXILPrettyPrinterLegacy : public llvm::ModulePass {
explicit DXILPrettyPrinterLegacy(raw_ostream &O) : ModulePass(ID), OS(O) {}
- StringRef getPassName() const override {
- return "DXIL Metadata Pretty Printer";
- }
+ StringRef getPassName() const override { return "DXIL Pretty Printer"; }
bool runOnModule(Module &M) override;
void getAnalysisUsage(AnalysisUsage &AU) const override {
- AU.setPreservesAll();
AU.addRequired<DXILResourceTypeWrapperPass>();
AU.addRequired<DXILResourceWrapperPass>();
}
@@ -283,18 +378,18 @@ class DXILPrettyPrinterLegacy : public llvm::ModulePass {
char DXILPrettyPrinterLegacy::ID = 0;
INITIALIZE_PASS_BEGIN(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
- "DXIL Metadata Pretty Printer", true, true)
+ "DXIL Pretty Printer", true, true)
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
INITIALIZE_PASS_END(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
- "DXIL Metadata Pretty Printer", true, true)
+ "DXIL Pretty Printer", true, true)
bool DXILPrettyPrinterLegacy::runOnModule(Module &M) {
const DXILResourceMap &DRM =
getAnalysis<DXILResourceWrapperPass>().getResourceMap();
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
- prettyPrintResources(OS, DRM, DRTM);
+ prettyPrint(OS, M, DRM, DRTM);
return false;
}
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index fbd8c95908917..ab264d82e0c80 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -171,7 +171,6 @@ bool DirectXTargetMachine::addPassesToEmitFile(
switch (FileType) {
case CodeGenFileType::AssemblyFile:
PM.add(createDXILPrettyPrinterLegacyPass(Out));
- PM.add(createPrintModulePass(Out, "", true));
break;
case CodeGenFileType::ObjectFile:
if (TargetPassConfig::willCompleteCodeGenPipeline()) {
diff --git a/llvm/test/CodeGen/DirectX/DebugInfo/di-compile-unit-versioned-language.ll b/llvm/test/CodeGen/DirectX/DebugInfo/di-compile-unit-versioned-language.ll
new file mode 100755
index 0000000000000..bcf27509deaa5
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/DebugInfo/di-compile-unit-versioned-language.ll
@@ -0,0 +1,15 @@
+; RUN: llc --filetype=asm %s -o - | FileCheck %s
+
+target triple = "dxil-unknown-shadermodel6.3-library"
+
+;; CHECK-DAG: [[CU:![0-9]+]] = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_C, sourceLanguageVersion: 199901,
+;; CHECK-DAG: DXIL: [[CU]]: to be replaced by: [[NEWCU:![0-9]+]]
+;; CHECK-DAG: [[NEWCU]] = distinct !DICompileUnit(language: DW_LANG_C99,
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(sourceLanguageName: DW_LNAME_C, sourceLanguageVersion: 199901, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "versioned-language.c", directory: "")
+!2 = !{i32 7, !"Dwarf Version", i32 6}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
diff --git a/llvm/test/CodeGen/DirectX/DebugInfo/di-subprogram.ll b/llvm/test/CodeGen/DirectX/DebugInfo/di-subprogram.ll
new file mode 100755
index 0000000000000..fd73830985e12
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/DebugInfo/di-subprogram.ll
@@ -0,0 +1,60 @@
+; RUN: llc --filetype=asm %s -o - | FileCheck %s
+target triple = "dxil-unknown-shadermodel6.7-library"
+
+define float @fmaf(float %x, float %y, float %z) !dbg !4 {
+ unreachable
+}
+
+declare !dbg !14 double @fma(double %x, double %y, double %z)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!12, !13}
+!llvm.used = !{!5}
+
+; CHECK: DXIL: !0: additional data: !16
+; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Some Compiler", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
+; CHECK: !1 = !DIFile(filename: "some-source", directory: "some-path")
+!1 = !DIFile(filename: "some-source", directory: "some-path")
+!2 = !{}
+
+; CHECK: !3 = !{i32 2, !"Dwarf Version", i32 4}
+; CHECK: !4 = !{i32 2, !"Debug Info Version", i32 3}
+
+; CHECK: DXIL: !8: to be replaced by: !17
+; CHECK: !8 = distinct !DISubprogram(name: "fmaf", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
+!4 = distinct !DISubprogram(name: "fmaf", scope: !1, file: !1, line: 1, type: !5, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
+
+; CHECK: !9 = !DISubroutineType(types: !10)
+!5 = !DISubroutineType(types: !6)
+
+; CHECK: !10 = !{!11, !11, !11, !11}
+!6 = !{!7, !7, !7, !7}
+
+; CHECK: !11 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!7 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+
+; CHECK: !12 = !{!13, !14, !15}
+!8 = !{!9, !10, !11}
+
+; CHECK: !13 = !DILocalVariable(name: "x", arg: 1, scope: !8, file: !1, line: 1, type: !11)
+!9 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !1, line: 1, type: !7)
+
+; CHECK: !14 = !DILocalVariable(name: "y", arg: 2, scope: !8, file: !1, line: 1, type: !11)
+!10 = !DILocalVariable(name: "y", arg: 2, scope: !4, file: !1, line: 1, type: !7)
+
+; CHECK: !15 = !DILocalVariable(name: "z", arg: 3, scope: !8, file: !1, line: 1, type: !11)
+!11 = !DILocalVariable(name: "z", arg: 3, scope: !4, file: !1, line: 1, type: !7)
+
+!12 = !{i32 2, !"Dwarf Version", i32 4}
+!13 = !{i32 2, !"Debug Info Version", i32 3}
+
+!14 = !DISubprogram(name: "fma", scope: !1, file: !1, line: 1, type: !15, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
+!15 = !DISubroutineType(types: !16)
+!16 = !{!17, !17, !17, !17}
+!17 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
+
+; CHECK: !16 = !{!8}
+
+; CHECK: DXIL: !17: additional data: ptr @fmaf
+; CHECK: !17 = !DISubprogram(name: "fmaf", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
diff --git a/llvm/test/CodeGen/DirectX/debug-info.ll b/llvm/test/CodeGen/DirectX/debug-info.ll
index b9cff28290d6a..e78b5ab5547aa 100644
--- a/llvm/test/CodeGen/DirectX/debug-info.ll
+++ b/llvm/test/CodeGen/DirectX/debug-info.ll
@@ -8,9 +8,12 @@ target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
; CHECK: define dso_local float @fma(float %0, float %1, float %2) local_unnamed_addr #0 !dbg [[Fn:[!][0-9]+]]
define dso_local float @fma(float %0, float %1, float %2) local_unnamed_addr #0 !dbg !6 {
-; CHECK-NEXT: call void @llvm.dbg.value(metadata float %0, metadata [[VarX:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1:[!][0-9]+]]
-; CHECK-NEXT: call void @llvm.dbg.value(metadata float %1, metadata [[VarY:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1]]
-; CHECK-NEXT: call void @llvm.dbg.value(metadata float %2, metadata [[VarZ:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1]]
+; CHECK-NEXT: DXIL: to be replaced with: tail call addrspace(0) void @llvm.dbg.value(metadata float %0, i64 0, metadata [[VarX:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1:[!][0-9]+]]
+; CHECK-NEXT: call void @llvm.dbg.value(metadata float %0, metadata [[VarX]], metadata !DIExpression()), !dbg [[Line1]]
+; CHECK-NEXT: DXIL: to be replaced with: tail call addrspace(0) void @llvm.dbg.value(metadata float %1, i64 0, metadata [[VarY:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1]]
+; CHECK-NEXT: call void @llvm.dbg.value(metadata float %1, metadata [[VarY]], metadata !DIExpression()), !dbg [[Line1]]
+; CHECK-NEXT: DXIL: to be replaced with: tail call addrspace(0) void @llvm.dbg.value(metadata float %2, i64 0, metadata [[VarZ:[!][0-9]+]], metadata !DIExpression()), !dbg [[Line1]]
+; CHECK-NEXT: call void @llvm.dbg.value(metadata float %2, metadata [[VarZ]], metadata !DIExpression()), !dbg [[Line1]]
call void @llvm.dbg.value(metadata float %0, metadata !11, metadata !DIExpression()), !dbg !14
call void @llvm.dbg.value(metadata float %1, metadata !12, metadata !DIExpression()), !dbg !14
call void @llvm.dbg.value(metadata float %2, metadata !13, metadata !DIExpression()), !dbg !14
diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
index f97ec9913e896..8419548bda78e 100644
--- a/llvm/test/CodeGen/DirectX/llc-pipeline.ll
+++ b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
@@ -52,8 +52,7 @@
; CHECK-NEXT: DXIL Op Lowering
; CHECK-NEXT: DXIL Prepare Module
-; CHECK-ASM-NEXT: DXIL Metadata Pretty Printer
-; CHECK-ASM-NEXT: Print Module IR
+; CHECK-ASM-NEXT: DXIL Pretty Printer
; CHECK-OBJ-NEXT: DXIL Embedder
; CHECK-OBJ-NEXT: DXContainer Global Emitter
More information about the llvm-commits
mailing list