Skip to content

Commit 81ee385

Browse files
authored
[DirectX] Register a few DXIL passes with the new PM
This wires up dxil-op-lower, dxil-intrinsic-expansion, dxil-translate-metadata, and dxil-pretty-printer to the new pass manager, both as a matter of future proofing the backend and so that they can be used more flexibly in tests. A few arbitrary tests are updated in order to test the new PM path, and we drop the "print-dxil-resource-md" pass since it's redundant with the pretty printer. Pull Request: #104250
1 parent a16f0dc commit 81ee385

17 files changed

+179
-101
lines changed

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
//===- DXILOpLower.cpp - Lowering LLVM intrinsic to DIXLOp function -------===//
1+
//===- DXILOpLowering.cpp - Lowering to DXIL operations -------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
///
9-
/// \file This file contains passes and utilities to lower llvm intrinsic call
10-
/// to DXILOp function call.
11-
//===----------------------------------------------------------------------===//
128

9+
#include "DXILOpLowering.h"
1310
#include "DXILConstants.h"
1411
#include "DXILIntrinsicExpansion.h"
1512
#include "DXILOpBuilder.h"
@@ -145,17 +142,11 @@ class OpLowerer {
145142
};
146143
} // namespace
147144

148-
namespace {
149-
/// A pass that transforms external global definitions into declarations.
150-
class DXILOpLowering : public PassInfoMixin<DXILOpLowering> {
151-
public:
152-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
153-
if (OpLowerer(M).lowerIntrinsics())
154-
return PreservedAnalyses::none();
155-
return PreservedAnalyses::all();
156-
}
157-
};
158-
} // namespace
145+
PreservedAnalyses DXILOpLowering::run(Module &M, ModuleAnalysisManager &) {
146+
if (OpLowerer(M).lowerIntrinsics())
147+
return PreservedAnalyses::none();
148+
return PreservedAnalyses::all();
149+
}
159150

160151
namespace {
161152
class DXILOpLoweringLegacy : public ModulePass {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- DXILOpLowering.h - Lowering to DXIL operations -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// \file Pass for lowering llvm intrinsics into DXIL operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_LIB_TARGET_DIRECTX_DXILOPLOWERING_H
14+
#define LLVM_LIB_TARGET_DIRECTX_DXILOPLOWERING_H
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
class DXILOpLowering : public PassInfoMixin<DXILOpLowering> {
21+
public:
22+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
23+
};
24+
25+
} // namespace llvm
26+
27+
#endif // LLVM_LIB_TARGET_DIRECTX_DXILOPLOWERING_H
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
//===- DXILPrettyPrinter.cpp - DXIL Resource helper objects ---------------===//
1+
//===- DXILPrettyPrinter.cpp - Print resources for textual DXIL -----------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
///
9-
/// \file This file contains a pass for pretty printing DXIL metadata into IR
10-
/// comments when printing assembly output.
11-
///
12-
//===----------------------------------------------------------------------===//
138

9+
#include "DXILPrettyPrinter.h"
1410
#include "DXILResourceAnalysis.h"
1511
#include "DirectX.h"
1612
#include "llvm/ADT/StringRef.h"
@@ -20,18 +16,30 @@
2016

2117
using namespace llvm;
2218

19+
static void prettyPrintResources(raw_ostream &OS,
20+
const dxil::Resources &MDResources) {
21+
MDResources.print(OS);
22+
}
23+
24+
PreservedAnalyses DXILPrettyPrinterPass::run(Module &M,
25+
ModuleAnalysisManager &MAM) {
26+
const dxil::Resources &MDResources = MAM.getResult<DXILResourceMDAnalysis>(M);
27+
prettyPrintResources(OS, MDResources);
28+
return PreservedAnalyses::all();
29+
}
30+
2331
namespace {
24-
class DXILPrettyPrinter : public llvm::ModulePass {
32+
class DXILPrettyPrinterLegacy : public llvm::ModulePass {
2533
raw_ostream &OS; // raw_ostream to print to.
2634

2735
public:
2836
static char ID;
29-
DXILPrettyPrinter() : ModulePass(ID), OS(dbgs()) {
30-
initializeDXILPrettyPrinterPass(*PassRegistry::getPassRegistry());
37+
DXILPrettyPrinterLegacy() : ModulePass(ID), OS(dbgs()) {
38+
initializeDXILPrettyPrinterLegacyPass(*PassRegistry::getPassRegistry());
3139
}
3240

33-
explicit DXILPrettyPrinter(raw_ostream &O) : ModulePass(ID), OS(O) {
34-
initializeDXILPrettyPrinterPass(*PassRegistry::getPassRegistry());
41+
explicit DXILPrettyPrinterLegacy(raw_ostream &O) : ModulePass(ID), OS(O) {
42+
initializeDXILPrettyPrinterLegacyPass(*PassRegistry::getPassRegistry());
3543
}
3644

3745
StringRef getPassName() const override {
@@ -46,19 +54,19 @@ class DXILPrettyPrinter : public llvm::ModulePass {
4654
};
4755
} // namespace
4856

49-
char DXILPrettyPrinter::ID = 0;
50-
INITIALIZE_PASS_BEGIN(DXILPrettyPrinter, "dxil-pretty-printer",
57+
char DXILPrettyPrinterLegacy::ID = 0;
58+
INITIALIZE_PASS_BEGIN(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
5159
"DXIL Metadata Pretty Printer", true, true)
5260
INITIALIZE_PASS_DEPENDENCY(DXILResourceMDWrapper)
53-
INITIALIZE_PASS_END(DXILPrettyPrinter, "dxil-pretty-printer",
61+
INITIALIZE_PASS_END(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
5462
"DXIL Metadata Pretty Printer", true, true)
5563

56-
bool DXILPrettyPrinter::runOnModule(Module &M) {
64+
bool DXILPrettyPrinterLegacy::runOnModule(Module &M) {
5765
dxil::Resources &Res = getAnalysis<DXILResourceMDWrapper>().getDXILResource();
5866
Res.print(OS);
5967
return false;
6068
}
6169

62-
ModulePass *llvm::createDXILPrettyPrinterPass(raw_ostream &OS) {
63-
return new DXILPrettyPrinter(OS);
70+
ModulePass *llvm::createDXILPrettyPrinterLegacyPass(raw_ostream &OS) {
71+
return new DXILPrettyPrinterLegacy(OS);
6472
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- DXILPrettyPrinter.h - Print resources for textual DXIL ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// \file This file contains a pass for pretty printing DXIL metadata into IR
10+
// comments when printing assembly output.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TARGET_DIRECTX_DXILPRETTYPRINTER_H
15+
#define LLVM_TARGET_DIRECTX_DXILPRETTYPRINTER_H
16+
17+
#include "llvm/IR/PassManager.h"
18+
19+
namespace llvm {
20+
21+
/// A pass that prints resources in a format suitable for textual DXIL.
22+
class DXILPrettyPrinterPass : public PassInfoMixin<DXILPrettyPrinterPass> {
23+
raw_ostream &OS;
24+
25+
public:
26+
explicit DXILPrettyPrinterPass(raw_ostream &OS) : OS(OS) {}
27+
28+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
29+
};
30+
31+
} // namespace llvm
32+
33+
#endif // LLVM_TARGET_DIRECTX_DXILPRETTYPRINTER_H

llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ dxil::Resources DXILResourceMDAnalysis::run(Module &M,
2727

2828
AnalysisKey DXILResourceMDAnalysis::Key;
2929

30-
PreservedAnalyses DXILResourceMDPrinterPass::run(Module &M,
31-
ModuleAnalysisManager &AM) {
32-
dxil::Resources Res = AM.getResult<DXILResourceMDAnalysis>(M);
33-
Res.print(OS);
34-
return PreservedAnalyses::all();
35-
}
36-
3730
char DXILResourceMDWrapper::ID = 0;
3831
INITIALIZE_PASS_BEGIN(DXILResourceMDWrapper, DEBUG_TYPE,
3932
"DXIL resource Information", true, true)
@@ -46,7 +39,3 @@ bool DXILResourceMDWrapper::runOnModule(Module &M) {
4639
}
4740

4841
DXILResourceMDWrapper::DXILResourceMDWrapper() : ModulePass(ID) {}
49-
50-
void DXILResourceMDWrapper::print(raw_ostream &OS, const Module *) const {
51-
Resources.print(OS);
52-
}

llvm/lib/Target/DirectX/DXILResourceAnalysis.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ class DXILResourceMDAnalysis
3030
dxil::Resources run(Module &M, ModuleAnalysisManager &AM);
3131
};
3232

33-
/// Printer pass for the \c DXILResourceMDAnalysis results.
34-
class DXILResourceMDPrinterPass
35-
: public PassInfoMixin<DXILResourceMDPrinterPass> {
36-
raw_ostream &OS;
37-
38-
public:
39-
explicit DXILResourceMDPrinterPass(raw_ostream &OS) : OS(OS) {}
40-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
41-
static bool isRequired() { return true; }
42-
};
43-
4433
/// The legacy pass manager's analysis pass to compute DXIL resource
4534
/// information.
4635
class DXILResourceMDWrapper : public ModulePass {
@@ -60,8 +49,6 @@ class DXILResourceMDWrapper : public ModulePass {
6049
void getAnalysisUsage(AnalysisUsage &AU) const override {
6150
AU.setPreservesAll();
6251
}
63-
64-
void print(raw_ostream &O, const Module *M = nullptr) const override;
6552
};
6653
} // namespace llvm
6754

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
//===- DXILTranslateMetadata.cpp - Pass to emit DXIL metadata ---*- C++ -*-===//
1+
//===- DXILTranslateMetadata.cpp - Pass to emit DXIL metadata -------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
///
9-
//===----------------------------------------------------------------------===//
108

9+
#include "DXILTranslateMetadata.h"
1110
#include "DXILMetadata.h"
1211
#include "DXILResource.h"
1312
#include "DXILResourceAnalysis.h"
@@ -23,11 +22,35 @@
2322
using namespace llvm;
2423
using namespace llvm::dxil;
2524

25+
static void translateMetadata(Module &M, const dxil::Resources &MDResources,
26+
const ComputedShaderFlags &ShaderFlags) {
27+
dxil::ValidatorVersionMD ValVerMD(M);
28+
if (ValVerMD.isEmpty())
29+
ValVerMD.update(VersionTuple(1, 0));
30+
dxil::createShaderModelMD(M);
31+
dxil::createDXILVersionMD(M);
32+
33+
MDResources.write(M);
34+
35+
dxil::createEntryMD(M, static_cast<uint64_t>(ShaderFlags));
36+
}
37+
38+
PreservedAnalyses DXILTranslateMetadata::run(Module &M,
39+
ModuleAnalysisManager &MAM) {
40+
const dxil::Resources &MDResources = MAM.getResult<DXILResourceMDAnalysis>(M);
41+
const ComputedShaderFlags &ShaderFlags =
42+
MAM.getResult<ShaderFlagsAnalysis>(M);
43+
44+
translateMetadata(M, MDResources, ShaderFlags);
45+
46+
return PreservedAnalyses::all();
47+
}
48+
2649
namespace {
27-
class DXILTranslateMetadata : public ModulePass {
50+
class DXILTranslateMetadataLegacy : public ModulePass {
2851
public:
2952
static char ID; // Pass identification, replacement for typeid
30-
explicit DXILTranslateMetadata() : ModulePass(ID) {}
53+
explicit DXILTranslateMetadataLegacy() : ModulePass(ID) {}
3154

3255
StringRef getPassName() const override { return "DXIL Translate Metadata"; }
3356

@@ -37,39 +60,28 @@ class DXILTranslateMetadata : public ModulePass {
3760
AU.addRequired<ShaderFlagsAnalysisWrapper>();
3861
}
3962

40-
bool runOnModule(Module &M) override;
63+
bool runOnModule(Module &M) override {
64+
const dxil::Resources &MDResources =
65+
getAnalysis<DXILResourceMDWrapper>().getDXILResource();
66+
const ComputedShaderFlags &ShaderFlags =
67+
getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags();
68+
69+
translateMetadata(M, MDResources, ShaderFlags);
70+
return true;
71+
}
4172
};
4273

4374
} // namespace
4475

45-
bool DXILTranslateMetadata::runOnModule(Module &M) {
46-
47-
dxil::ValidatorVersionMD ValVerMD(M);
48-
if (ValVerMD.isEmpty())
49-
ValVerMD.update(VersionTuple(1, 0));
50-
dxil::createShaderModelMD(M);
51-
dxil::createDXILVersionMD(M);
52-
53-
const dxil::Resources &Res =
54-
getAnalysis<DXILResourceMDWrapper>().getDXILResource();
55-
Res.write(M);
56-
57-
const uint64_t Flags = static_cast<uint64_t>(
58-
getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags());
59-
dxil::createEntryMD(M, Flags);
60-
61-
return false;
62-
}
63-
64-
char DXILTranslateMetadata::ID = 0;
76+
char DXILTranslateMetadataLegacy::ID = 0;
6577

66-
ModulePass *llvm::createDXILTranslateMetadataPass() {
67-
return new DXILTranslateMetadata();
78+
ModulePass *llvm::createDXILTranslateMetadataLegacyPass() {
79+
return new DXILTranslateMetadataLegacy();
6880
}
6981

70-
INITIALIZE_PASS_BEGIN(DXILTranslateMetadata, "dxil-translate-metadata",
82+
INITIALIZE_PASS_BEGIN(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
7183
"DXIL Translate Metadata", false, false)
7284
INITIALIZE_PASS_DEPENDENCY(DXILResourceMDWrapper)
7385
INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
74-
INITIALIZE_PASS_END(DXILTranslateMetadata, "dxil-translate-metadata",
86+
INITIALIZE_PASS_END(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
7587
"DXIL Translate Metadata", false, false)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- DXILTranslateMetadata.h - Pass to emit DXIL metadata -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TARGET_DIRECTX_DXILTRANSLATEMETADATA_H
10+
#define LLVM_TARGET_DIRECTX_DXILTRANSLATEMETADATA_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
/// A pass that transforms DXIL Intrinsics that don't have DXIL opCodes
17+
class DXILTranslateMetadata : public PassInfoMixin<DXILTranslateMetadata> {
18+
public:
19+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
20+
};
21+
22+
} // namespace llvm
23+
24+
#endif // LLVM_TARGET_DIRECTX_DXILTRANSLATEMETADATA_H

llvm/lib/Target/DirectX/DirectX.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ void initializeDXILOpLoweringLegacyPass(PassRegistry &);
4141
ModulePass *createDXILOpLoweringLegacyPass();
4242

4343
/// Initializer for DXILTranslateMetadata.
44-
void initializeDXILTranslateMetadataPass(PassRegistry &);
44+
void initializeDXILTranslateMetadataLegacyPass(PassRegistry &);
4545

4646
/// Pass to emit metadata for DXIL.
47-
ModulePass *createDXILTranslateMetadataPass();
47+
ModulePass *createDXILTranslateMetadataLegacyPass();
4848

4949
/// Initializer for DXILTranslateMetadata.
5050
void initializeDXILResourceMDWrapperPass(PassRegistry &);
5151

5252
/// Pass to pretty print DXIL metadata.
53-
ModulePass *createDXILPrettyPrinterPass(raw_ostream &OS);
53+
ModulePass *createDXILPrettyPrinterLegacyPass(raw_ostream &OS);
5454

5555
/// Initializer for DXILPrettyPrinter.
56-
void initializeDXILPrettyPrinterPass(PassRegistry &);
56+
void initializeDXILPrettyPrinterLegacyPass(PassRegistry &);
5757

5858
/// Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
5959
void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &);

0 commit comments

Comments
 (0)