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 4 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
12 changes: 11 additions & 1 deletion llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef LLVM_ANALYSIS_DXILMETADATA_H
#define LLVM_ANALYSIS_DXILMETADATA_H

#include "llvm/ADT/MapVector.h"
#include "llvm/IR/Function.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A forward declaration should be sufficient for Function

#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/VersionTuple.h"
Expand All @@ -19,12 +21,20 @@ namespace llvm {

namespace dxil {

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

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

MapVector<Function *, EntryProperties> EntryPropertyMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a MapVector? Is the insertion order of these functions interesting to preserve for some reason, or is this just here so that it's easy to make the order that we print them in deterministic?

void print(raw_ostream &OS) const;
};

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

#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"
#include <memory>
#include <utility>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think <memory> or <utility> are used any more


#define DEBUG_TYPE "dxil-metadata-analysis"

Expand All @@ -33,15 +38,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{};
// 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
if (!llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10))
assert(false && "Failed to parse X component of numthreads");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (x) assert(false) is generally discouraged. It doesn't make much difference, but it's probably better

    [[maybe_unused]] bool Success =
        llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
    assert(Success&& "Failed to parse X component of numthreads");

if (!llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10))
assert(false && "Failed to parse Y component of numthreads");
if (!llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10))
assert(false && "Failed to parse Z component of numthreads");
}
MMDAI.EntryPropertyMap.insert(std::make_pair(std::addressof(F), EFP));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use std::addressof(F) here rather than just &F? Generally you only need std::addressof in certain template contexts

}
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 (auto MapItem : EntryPropertyMap) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use C++17 so you could use structured bindings to clean this up slightly if you want. In any case this should probably be const auto & to avoid copying the items unnecessarily

OS << " " << MapItem.first->getName() << "\n";
OS << " Function Shader Stage : "
<< Triple::getEnvironmentTypeName(MapItem.second.ShaderStage) << "\n";
OS << " NumThreads: " << MapItem.second.NumThreadsX << ","
<< MapItem.second.NumThreadsY << "," << MapItem.second.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