Skip to content

[DXIL][Analysis] Collect Function properties in Metadata Analysis #105728

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
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
16 changes: 14 additions & 2 deletions llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,34 @@
#ifndef LLVM_ANALYSIS_DXILMETADATA_H
#define LLVM_ANALYSIS_DXILMETADATA_H

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

namespace llvm {

class Function;
namespace dxil {

struct EntryProperties {
const Function *Entry;
// Specific target shader stage may be specified for entry functions
Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
unsigned NumThreadsX{0}; // X component
unsigned NumThreadsY{0}; // Y component
unsigned NumThreadsZ{0}; // Z component

EntryProperties(const Function &Fn) : Entry(&Fn) {};
};

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

SmallVector<EntryProperties> EntryPropertyVec{};
void print(raw_ostream &OS) const;
};

Expand Down
44 changes: 43 additions & 1 deletion llvm/lib/Analysis/DXILMetadataAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.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"
#include "llvm/Support/ErrorHandling.h"

#define DEBUG_TYPE "dxil-metadata-analysis"

Expand All @@ -33,15 +36,54 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
MMDAI.ValidatorVersion =
VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
}

// For all HLSL Shader functions
for (auto &F : M.functions()) {
if (!F.hasFnAttribute("hlsl.shader"))
continue;

EntryProperties EFP(F);
// Get "hlsl.shader" attribute
Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
assert(EntryAttr.isValid() &&
"Invalid value specified for HLSL function attribute hlsl.shader");
StringRef EntryProfile = EntryAttr.getValueAsString();
Triple T("", "", "", EntryProfile);
EFP.ShaderStage = T.getEnvironment();
// Get numthreads attribute value, if one exists
StringRef NumThreadsStr =
F.getFnAttribute("hlsl.numthreads").getValueAsString();
if (!NumThreadsStr.empty()) {
SmallVector<StringRef> NumThreadsVec;
NumThreadsStr.split(NumThreadsVec, ',');
assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified");
// Read in the three component values of numthreads
[[maybe_unused]] bool Success =
llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
assert(Success && "Failed to parse X component of numthreads");
Success = llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10);
assert(Success && "Failed to parse Y component of numthreads");
Success = llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10);
assert(Success && "Failed to parse Z component of numthreads");
}
MMDAI.EntryPropertyVec.push_back(EFP);
}
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)
OS << "Target Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
<< "\n";
OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
for (const auto &EP : EntryPropertyVec) {
OS << " " << EP.Entry->getName() << "\n";
OS << " Function Shader Stage : "
<< Triple::getEnvironmentTypeName(EP.ShaderStage) << "\n";
OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << ","
<< EP.NumThreadsZ << "\n";
}
}

//===----------------------------------------------------------------------===//
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/dxilVer-1.0.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.0-vertex"

; CHECK: Shader Model Version : 6.0
; CHECK-NEXT: DXIL Version : 1.0
; CHECK-NEXT: Shader Stage : vertex
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : vertex
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="vertex" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/dxilVer-1.8.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.8-compute"

; CHECK: Shader Model Version : 6.8
; CHECK-NEXT: DXIL Version : 1.8
; CHECK-NEXT: Shader Stage : compute
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : compute
; CHECK-NEXT: NumThreads: 1,2,1
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
36 changes: 36 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/entry-properties.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.8-library"

; CHECK: Shader Model Version : 6.8
; CHECK-NEXT: DXIL Version : 1.8
; CHECK-NEXT: Target Shader Stage : library
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry_as
; CHECK-NEXT: Function Shader Stage : amplification
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-NEXT: entry_ms
; CHECK-NEXT: Function Shader Stage : mesh
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-NEXT: entry_cs
; CHECK-NEXT: Function Shader Stage : compute
; CHECK-NEXT: NumThreads: 1,2,1
; CHECK-EMPTY:

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

define i32 @entry_ms(i32 %a) #1 {
entry:
ret i32 %a
}

define float @entry_cs(float %f) #3 {
entry:
ret float %f
}

attributes #0 = { noinline nounwind "hlsl.shader"="amplification" }
attributes #1 = { noinline nounwind "hlsl.shader"="mesh" }
attributes #3 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-as.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6-amplification"

; CHECK: Shader Model Version : 6
; CHECK-NEXT: DXIL Version : 1.0
; CHECK-NEXT: Target Shader Stage : amplification
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : amplification
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="amplification" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s

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

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

attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }

!dx.valver = !{!0}

!0 = !{i32 0, i32 0}

; CHECK: Shader Model Version : 6.6
; CHECK-NEXT: DXIL Version : 1.6
; CHECK-NEXT: Target Shader Stage : compute
; CHECK-NEXT: Validator Version : 0.0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : compute
; CHECK-NEXT: NumThreads: 1,2,1
; CHECK-EMPTY:
19 changes: 19 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-cs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s

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

; CHECK: Shader Model Version : 6.6
; CHECK-NEXT: DXIL Version : 1.6
; CHECK-NEXT: Target Shader Stage : compute
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : compute
; CHECK-NEXT: NumThreads: 1,2,1
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-gs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.6-geometry"

; CHECK: Shader Model Version : 6.6
; CHECK-NEXT: DXIL Version : 1.6
; CHECK-NEXT: Shader Stage : geometry
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : geometry
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="geometry" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-hs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.6-hull"

; CHECK: Shader Model Version : 6.6
; CHECK-NEXT: DXIL Version : 1.6
; CHECK-NEXT: Target Shader Stage : hull
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : hull
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="hull" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-ms.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel6.6-mesh"

; CHECK: Shader Model Version : 6.6
; CHECK-NEXT: DXIL Version : 1.6
; CHECK-NEXT: Shader Stage : mesh
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : mesh
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="mesh" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-ps.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel5.0-pixel"

; CHECK: Shader Model Version : 5.0
; CHECK-NEXT: DXIL Version : 1.0
; CHECK-NEXT: Shader Stage : pixel
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : pixel
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="pixel" }
18 changes: 18 additions & 0 deletions llvm/test/Analysis/DXILMetadataAnalysis/shaderModel-vs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
target triple = "dxil-pc-shadermodel-vertex"

; CHECK: Shader Model Version : 0
; CHECK-NEXT: DXIL Version : 1.0
; CHECK-NEXT: Shader Stage : vertex
; CHECK-NEXT: Validator Version : 0
; CHECK-NEXT: entry
; CHECK-NEXT: Function Shader Stage : vertex
; CHECK-NEXT: NumThreads: 0,0,0
; CHECK-EMPTY:

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

attributes #0 = { noinline nounwind "hlsl.shader"="vertex" }
7 changes: 0 additions & 7 deletions llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
; RUN: opt -S -passes=dxil-translate-metadata %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-NEXT: DXIL Version : 1.0
; ANALYSIS-NEXT: Shader Stage : vertex
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-EMPTY:

define void @entry() #0 {
entry:
ret void
Expand Down
7 changes: 0 additions & 7 deletions llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
; RUN: opt -S -passes=dxil-translate-metadata %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-NEXT: DXIL Version : 1.8
; ANALYSIS-NEXT: Shader Stage : compute
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-EMPTY:

define void @entry() #0 {
entry:
ret void
Expand Down
7 changes: 0 additions & 7 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
; RUN: opt -S -dxil-translate-metadata %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-NEXT: DXIL Version : 1.0
; ANALYSIS-NEXT: Shader Stage : amplification
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-EMPTY:

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