Skip to content

[DXIL][Analysis] Add DXILMetadataAnalysis pass #102079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//=- DXILMetadataAnalysis.h - Representation of Module metadata --*- C++ -*-=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_DXILMETADATA_H
#define LLVM_ANALYSIS_DXILMETADATA_H

#include "llvm/IR/PassManager.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/TargetParser/Triple.h"
#include <memory>

namespace llvm {

namespace dxil {

struct ModuleMetadataInfo {
VersionTuple DXILVersion{};
VersionTuple ShaderModelVersion{};
Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;

void print(raw_ostream &OS) const;
};

} // namespace dxil

// Module metadata analysis pass for new pass manager
class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
friend AnalysisInfoMixin<DXILMetadataAnalysis>;

static AnalysisKey Key;

public:
using Result = dxil::ModuleMetadataInfo;
/// Gather module metadata info for the module \c M.
dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM);
};

/// Printer pass for the \c DXILMetadataAnalysis results.
class DXILMetadataAnalysisPrinterPass
: public PassInfoMixin<DXILMetadataAnalysisPrinterPass> {
raw_ostream &OS;

public:
explicit DXILMetadataAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}

PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);

static bool isRequired() { return true; }
};

/// Legacy pass
class DXILMetadataAnalysisWrapperPass : public ModulePass {
std::unique_ptr<dxil::ModuleMetadataInfo> MetadataInfo;

public:
static char ID; // Class identification, replacement for typeinfo

DXILMetadataAnalysisWrapperPass();
~DXILMetadataAnalysisWrapperPass() override;

const dxil::ModuleMetadataInfo &getModuleMetadata() const {
return *MetadataInfo;
}
dxil::ModuleMetadataInfo &getModuleMetadata() { return *MetadataInfo; }

void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnModule(Module &M) override;
void releaseMemory() override;

void print(raw_ostream &OS, const Module *M) const override;
void dump() const;
};

} // namespace llvm

#endif // LLVM_ANALYSIS_DXILMETADATA_H
2 changes: 2 additions & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ void initializeCycleInfoWrapperPassPass(PassRegistry &);
void initializeDAEPass(PassRegistry&);
void initializeDAHPass(PassRegistry&);
void initializeDCELegacyPassPass(PassRegistry&);
void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
void initializeDXILMetadataAnalysisWrapperPrinterPass(PassRegistry &);
void initializeDeadMachineInstructionElimPass(PassRegistry&);
void initializeDebugifyMachineModulePass(PassRegistry &);
void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_llvm_component_library(LLVMAnalysis
DomTreeUpdater.cpp
DominanceFrontier.cpp
DXILResource.cpp
DXILMetadataAnalysis.cpp
FunctionPropertiesAnalysis.cpp
GlobalsModRef.cpp
GuardUtils.cpp
Expand Down
96 changes: 96 additions & 0 deletions llvm/lib/Analysis/DXILMetadataAnalysis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//=- DXILMetadataAnalysis.cpp - Representation of Module metadata -*- C++ -*=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"

#define DEBUG_TYPE "dxil-metadata-analysis"

using namespace llvm;
using namespace dxil;

static ModuleMetadataInfo collectMetadataInfo(Module &M) {
ModuleMetadataInfo MMDAI;
Triple TT(Triple(M.getTargetTriple()));
MMDAI.DXILVersion = TT.getDXILVersion();
MMDAI.ShaderModelVersion = TT.getOSVersion();
MMDAI.ShaderStage = TT.getEnvironment();
return MMDAI;
}

void ModuleMetadataInfo::print(raw_ostream &OS) const {
OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
<< "\n";
}

//===----------------------------------------------------------------------===//
// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass

// Provide an explicit template instantiation for the static ID.
AnalysisKey DXILMetadataAnalysis::Key;

llvm::dxil::ModuleMetadataInfo
DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
return collectMetadataInfo(M);
}

PreservedAnalyses
DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(M);

Data.print(OS);
return PreservedAnalyses::all();
}

//===----------------------------------------------------------------------===//
// DXILMetadataAnalysisWrapperPass

DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
: ModulePass(ID) {
initializeDXILMetadataAnalysisWrapperPassPass(
*PassRegistry::getPassRegistry());
}

DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;

void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
AnalysisUsage &AU) const {
AU.setPreservesAll();
}

bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
return false;
}

void DXILMetadataAnalysisWrapperPass::releaseMemory() { MetadataInfo.reset(); }

void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
const Module *) const {
if (!MetadataInfo) {
OS << "No module metadata info has been built!\n";
return;
}
MetadataInfo->print(dbgs());
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD
void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
#endif

INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
"DXIL Module Metadata analysis", false, true)
char DXILMetadataAnalysisWrapperPass::ID = 0;
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "llvm/Analysis/CycleAnalysis.h"
#include "llvm/Analysis/DDG.h"
#include "llvm/Analysis/DDGPrinter.h"
#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/Analysis/Delinearization.h"
#include "llvm/Analysis/DemandedBits.h"
#include "llvm/Analysis/DependenceAnalysis.h"
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
MODULE_ANALYSIS("ctx-prof-analysis", CtxProfAnalysis(UseCtxProfile))
MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
Expand Down Expand Up @@ -117,6 +118,7 @@ MODULE_PASS("print-must-be-executed-contexts",
MustBeExecutedContextPrinterPass(dbgs()))
MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs()))
MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.0-vertex"

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

; ANALYSIS: Shader Model Version : 6.0
; ANALYSIS: DXIL Version : 1.0
; ANALYSIS: Shader Stage : vertex

define void @entry() #0 {
entry:
ret void
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.8-compute"

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

; ANALYSIS: Shader Model Version : 6.8
; ANALYSIS: DXIL Version : 1.8
; ANALYSIS: Shader Stage : compute

define void @entry() #0 {
entry:
ret void
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6-amplification"

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

; ANALYSIS: Shader Model Version : 6
; ANALYSIS: DXIL Version : 1.0
; ANALYSIS: Shader Stage : amplification

define void @entry() #0 {
entry:
ret void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -S -dxil-prepare %s | FileCheck %s
; RUN: opt -S -dxil-prepare %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS

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

Expand All @@ -8,9 +9,13 @@ entry:
}

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

!dx.valver = !{!0}

!0 = !{i32 0, i32 0}

; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS: DXIL Version : 1.6
; ANALYSIS: Shader Stage : compute
7 changes: 6 additions & 1 deletion llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -dxil-prepare %s | FileCheck %s --check-prefix=REMOVE_EXTRA_ATTRIBUTE
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS

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

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

; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS: DXIL Version : 1.6
; ANALYSIS: Shader Stage : compute

define void @entry() #0 {
entry:
ret void
}

; Make sure extra attribute like hlsl.numthreads are removed.
; And experimental attribute is removed when validator version is not 0.0.
; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind }
; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind }
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.6-geometry"

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

; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS: DXIL Version : 1.6
; ANALYSIS: Shader Stage : geometry

define void @entry() #0 {
entry:
ret void
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.6-hull"

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

; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS: DXIL Version : 1.6
; ANALYSIS: Shader Stage : hull

define void @entry() #0 {
entry:
ret void
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.3-library"

; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
; CHECK: ![[SM]] = !{!"lib", i32 6, i32 3}

; ANALYSIS: Shader Model Version : 6.3
; ANALYSIS: DXIL Version : 1.3
; ANALYSIS: Shader Stage : library
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel6.6-mesh"

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

; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS: DXIL Version : 1.6
; ANALYSIS: Shader Stage : mesh

define void @entry() #0 {
entry:
ret void
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@

; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel5.0-pixel"

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

; ANALYSIS: Shader Model Version : 5.0
; ANALYSIS: DXIL Version : 1.0
; ANALYSIS: Shader Stage : pixel

define void @entry() #0 {
entry:
ret void
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
target triple = "dxil-pc-shadermodel-vertex"

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

; ANALYSIS: Shader Model Version : 0
; ANALYSIS: DXIL Version : 1.0
; ANALYSIS: Shader Stage : vertex

define void @entry() #0 {
entry:
ret void
Expand Down
Loading