Skip to content

Commit 03e6675

Browse files
authored
[DXIL][Analysis] Add DXILMetadataAnalysis pass (#102079)
DXIL Metadata Analysis passes (one for legacy PM and one for new PM) that collect following DXIL module metadata information in a structure are added. 1. Shader Model version 2. DXIL version 3. Shader Stage Information collected using the legacy pass is verified by adding additional test commands to existing metadata test sources.
1 parent e40915b commit 03e6675

17 files changed

+244
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//=- DXILMetadataAnalysis.h - Representation of Module 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_ANALYSIS_DXILMETADATA_H
10+
#define LLVM_ANALYSIS_DXILMETADATA_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
#include "llvm/IR/Value.h"
14+
#include "llvm/Pass.h"
15+
#include "llvm/Support/VersionTuple.h"
16+
#include "llvm/TargetParser/Triple.h"
17+
#include <memory>
18+
19+
namespace llvm {
20+
21+
namespace dxil {
22+
23+
struct ModuleMetadataInfo {
24+
VersionTuple DXILVersion{};
25+
VersionTuple ShaderModelVersion{};
26+
Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
27+
28+
void print(raw_ostream &OS) const;
29+
};
30+
31+
} // namespace dxil
32+
33+
// Module metadata analysis pass for new pass manager
34+
class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
35+
friend AnalysisInfoMixin<DXILMetadataAnalysis>;
36+
37+
static AnalysisKey Key;
38+
39+
public:
40+
using Result = dxil::ModuleMetadataInfo;
41+
/// Gather module metadata info for the module \c M.
42+
dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM);
43+
};
44+
45+
/// Printer pass for the \c DXILMetadataAnalysis results.
46+
class DXILMetadataAnalysisPrinterPass
47+
: public PassInfoMixin<DXILMetadataAnalysisPrinterPass> {
48+
raw_ostream &OS;
49+
50+
public:
51+
explicit DXILMetadataAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
52+
53+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
54+
55+
static bool isRequired() { return true; }
56+
};
57+
58+
/// Legacy pass
59+
class DXILMetadataAnalysisWrapperPass : public ModulePass {
60+
std::unique_ptr<dxil::ModuleMetadataInfo> MetadataInfo;
61+
62+
public:
63+
static char ID; // Class identification, replacement for typeinfo
64+
65+
DXILMetadataAnalysisWrapperPass();
66+
~DXILMetadataAnalysisWrapperPass() override;
67+
68+
const dxil::ModuleMetadataInfo &getModuleMetadata() const {
69+
return *MetadataInfo;
70+
}
71+
dxil::ModuleMetadataInfo &getModuleMetadata() { return *MetadataInfo; }
72+
73+
void getAnalysisUsage(AnalysisUsage &AU) const override;
74+
bool runOnModule(Module &M) override;
75+
void releaseMemory() override;
76+
77+
void print(raw_ostream &OS, const Module *M) const override;
78+
void dump() const;
79+
};
80+
81+
} // namespace llvm
82+
83+
#endif // LLVM_ANALYSIS_DXILMETADATA_H

llvm/include/llvm/InitializePasses.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void initializeCycleInfoWrapperPassPass(PassRegistry &);
8282
void initializeDAEPass(PassRegistry&);
8383
void initializeDAHPass(PassRegistry&);
8484
void initializeDCELegacyPassPass(PassRegistry&);
85+
void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
86+
void initializeDXILMetadataAnalysisWrapperPrinterPass(PassRegistry &);
8587
void initializeDeadMachineInstructionElimPass(PassRegistry&);
8688
void initializeDebugifyMachineModulePass(PassRegistry &);
8789
void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_llvm_component_library(LLVMAnalysis
6161
DomTreeUpdater.cpp
6262
DominanceFrontier.cpp
6363
DXILResource.cpp
64+
DXILMetadataAnalysis.cpp
6465
FunctionPropertiesAnalysis.cpp
6566
GlobalsModRef.cpp
6667
GuardUtils.cpp
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//=- DXILMetadataAnalysis.cpp - Representation of Module 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+
#include "llvm/Analysis/DXILMetadataAnalysis.h"
10+
#include "llvm/ADT/APInt.h"
11+
#include "llvm/IR/Constants.h"
12+
#include "llvm/IR/Instructions.h"
13+
#include "llvm/IR/Metadata.h"
14+
#include "llvm/IR/Module.h"
15+
#include "llvm/InitializePasses.h"
16+
17+
#define DEBUG_TYPE "dxil-metadata-analysis"
18+
19+
using namespace llvm;
20+
using namespace dxil;
21+
22+
static ModuleMetadataInfo collectMetadataInfo(Module &M) {
23+
ModuleMetadataInfo MMDAI;
24+
Triple TT(Triple(M.getTargetTriple()));
25+
MMDAI.DXILVersion = TT.getDXILVersion();
26+
MMDAI.ShaderModelVersion = TT.getOSVersion();
27+
MMDAI.ShaderStage = TT.getEnvironment();
28+
return MMDAI;
29+
}
30+
31+
void ModuleMetadataInfo::print(raw_ostream &OS) const {
32+
OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
33+
OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
34+
OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
35+
<< "\n";
36+
}
37+
38+
//===----------------------------------------------------------------------===//
39+
// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
40+
41+
// Provide an explicit template instantiation for the static ID.
42+
AnalysisKey DXILMetadataAnalysis::Key;
43+
44+
llvm::dxil::ModuleMetadataInfo
45+
DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
46+
return collectMetadataInfo(M);
47+
}
48+
49+
PreservedAnalyses
50+
DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
51+
llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(M);
52+
53+
Data.print(OS);
54+
return PreservedAnalyses::all();
55+
}
56+
57+
//===----------------------------------------------------------------------===//
58+
// DXILMetadataAnalysisWrapperPass
59+
60+
DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
61+
: ModulePass(ID) {
62+
initializeDXILMetadataAnalysisWrapperPassPass(
63+
*PassRegistry::getPassRegistry());
64+
}
65+
66+
DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;
67+
68+
void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
69+
AnalysisUsage &AU) const {
70+
AU.setPreservesAll();
71+
}
72+
73+
bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
74+
MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
75+
return false;
76+
}
77+
78+
void DXILMetadataAnalysisWrapperPass::releaseMemory() { MetadataInfo.reset(); }
79+
80+
void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
81+
const Module *) const {
82+
if (!MetadataInfo) {
83+
OS << "No module metadata info has been built!\n";
84+
return;
85+
}
86+
MetadataInfo->print(dbgs());
87+
}
88+
89+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
90+
LLVM_DUMP_METHOD
91+
void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
92+
#endif
93+
94+
INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
95+
"DXIL Module Metadata analysis", false, true)
96+
char DXILMetadataAnalysisWrapperPass::ID = 0;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/Analysis/CycleAnalysis.h"
3333
#include "llvm/Analysis/DDG.h"
3434
#include "llvm/Analysis/DDGPrinter.h"
35+
#include "llvm/Analysis/DXILMetadataAnalysis.h"
3536
#include "llvm/Analysis/Delinearization.h"
3637
#include "llvm/Analysis/DemandedBits.h"
3738
#include "llvm/Analysis/DependenceAnalysis.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
2222
MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
2323
MODULE_ANALYSIS("ctx-prof-analysis", CtxProfAnalysis(UseCtxProfile))
24+
MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
2425
MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
2526
MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
2627
MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
@@ -117,6 +118,7 @@ MODULE_PASS("print-must-be-executed-contexts",
117118
MustBeExecutedContextPrinterPass(dbgs()))
118119
MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
119120
MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs()))
121+
MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
120122
MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
121123
MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
122124
MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))

llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.0-vertex"
34

45
; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
56
; CHECK: ![[DXVER]] = !{i32 1, i32 0}
67

8+
; ANALYSIS: Shader Model Version : 6.0
9+
; ANALYSIS: DXIL Version : 1.0
10+
; ANALYSIS: Shader Stage : vertex
11+
712
define void @entry() #0 {
813
entry:
914
ret void

llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.8-compute"
34

45
; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
56
; CHECK: ![[DXVER]] = !{i32 1, i32 8}
67

8+
; ANALYSIS: Shader Model Version : 6.8
9+
; ANALYSIS: DXIL Version : 1.8
10+
; ANALYSIS: Shader Stage : compute
11+
712
define void @entry() #0 {
813
entry:
914
ret void

llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6-amplification"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"as", i32 6, i32 0}
67

8+
; ANALYSIS: Shader Model Version : 6
9+
; ANALYSIS: DXIL Version : 1.0
10+
; ANALYSIS: Shader Stage : amplification
11+
712
define void @entry() #0 {
813
entry:
914
ret void
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; RUN: opt -S -dxil-prepare %s | FileCheck %s
1+
; RUN: opt -S -dxil-prepare %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23

34
target triple = "dxil-pc-shadermodel6.6-compute"
45

@@ -8,9 +9,13 @@ entry:
89
}
910

1011
; Make sure experimental attribute is left when validation version is 0.0.
11-
; CHECK:attributes #0 = { noinline nounwind "exp-shader"="cs" }
12+
; CHECK:attributes #0 = { noinline nounwind "exp-shader"="cs" }
1213
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
1314

1415
!dx.valver = !{!0}
1516

1617
!0 = !{i32 0, i32 0}
18+
19+
; ANALYSIS: Shader Model Version : 6.6
20+
; ANALYSIS: DXIL Version : 1.6
21+
; ANALYSIS: Shader Stage : compute
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
22
; RUN: opt -S -dxil-prepare %s | FileCheck %s --check-prefix=REMOVE_EXTRA_ATTRIBUTE
3+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
34

45
target triple = "dxil-pc-shadermodel6.6-compute"
56

67
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
78
; CHECK: ![[SM]] = !{!"cs", i32 6, i32 6}
89

10+
; ANALYSIS: Shader Model Version : 6.6
11+
; ANALYSIS: DXIL Version : 1.6
12+
; ANALYSIS: Shader Stage : compute
13+
914
define void @entry() #0 {
1015
entry:
1116
ret void
1217
}
1318

1419
; Make sure extra attribute like hlsl.numthreads are removed.
1520
; And experimental attribute is removed when validator version is not 0.0.
16-
; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind }
21+
; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind }
1722
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }

llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.6-geometry"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"gs", i32 6, i32 6}
67

8+
; ANALYSIS: Shader Model Version : 6.6
9+
; ANALYSIS: DXIL Version : 1.6
10+
; ANALYSIS: Shader Stage : geometry
11+
712
define void @entry() #0 {
813
entry:
914
ret void

llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.6-hull"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"hs", i32 6, i32 6}
67

8+
; ANALYSIS: Shader Model Version : 6.6
9+
; ANALYSIS: DXIL Version : 1.6
10+
; ANALYSIS: Shader Stage : hull
11+
712
define void @entry() #0 {
813
entry:
914
ret void
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.3-library"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"lib", i32 6, i32 3}
7+
8+
; ANALYSIS: Shader Model Version : 6.3
9+
; ANALYSIS: DXIL Version : 1.3
10+
; ANALYSIS: Shader Stage : library

llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel6.6-mesh"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"ms", i32 6, i32 6}
67

8+
; ANALYSIS: Shader Model Version : 6.6
9+
; ANALYSIS: DXIL Version : 1.6
10+
; ANALYSIS: Shader Stage : mesh
11+
712
define void @entry() #0 {
813
entry:
914
ret void

llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
12
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
3+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
24
target triple = "dxil-pc-shadermodel5.0-pixel"
35

46
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
57
; CHECK: ![[SM]] = !{!"ps", i32 5, i32 0}
68

9+
; ANALYSIS: Shader Model Version : 5.0
10+
; ANALYSIS: DXIL Version : 1.0
11+
; ANALYSIS: Shader Stage : pixel
12+
713
define void @entry() #0 {
814
entry:
915
ret void

llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
2+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
23
target triple = "dxil-pc-shadermodel-vertex"
34

45
; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
56
; CHECK: ![[SM]] = !{!"vs", i32 0, i32 0}
67

8+
; ANALYSIS: Shader Model Version : 0
9+
; ANALYSIS: DXIL Version : 1.0
10+
; ANALYSIS: Shader Stage : vertex
11+
712
define void @entry() #0 {
813
entry:
914
ret void

0 commit comments

Comments
 (0)