Skip to content

Commit 8aa8c05

Browse files
authored
[DXIL][Analysis] Collect Function properties in Metadata Analysis (#105728)
Basic infrastructure to collect Function properties in Metadata Analysis - Add a `SmallVector` of entry properties to the metadata information. - Add a structure to represent function properties. Currently `numthreads` and shader kind properties of shader entry functions are represented.
1 parent a3e2936 commit 8aa8c05

24 files changed

+279
-79
lines changed

llvm/include/llvm/Analysis/DXILMetadataAnalysis.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,34 @@
99
#ifndef LLVM_ANALYSIS_DXILMETADATA_H
1010
#define LLVM_ANALYSIS_DXILMETADATA_H
1111

12+
#include "llvm/ADT/SmallVector.h"
1213
#include "llvm/IR/PassManager.h"
1314
#include "llvm/Pass.h"
1415
#include "llvm/Support/VersionTuple.h"
1516
#include "llvm/TargetParser/Triple.h"
16-
#include <memory>
1717

1818
namespace llvm {
1919

20+
class Function;
2021
namespace dxil {
2122

23+
struct EntryProperties {
24+
const Function *Entry;
25+
// Specific target shader stage may be specified for entry functions
26+
Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
27+
unsigned NumThreadsX{0}; // X component
28+
unsigned NumThreadsY{0}; // Y component
29+
unsigned NumThreadsZ{0}; // Z component
30+
31+
EntryProperties(const Function &Fn) : Entry(&Fn) {};
32+
};
33+
2234
struct ModuleMetadataInfo {
2335
VersionTuple DXILVersion{};
2436
VersionTuple ShaderModelVersion{};
2537
Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
2638
VersionTuple ValidatorVersion{};
27-
39+
SmallVector<EntryProperties> EntryPropertyVec{};
2840
void print(raw_ostream &OS) const;
2941
};
3042

llvm/lib/Analysis/DXILMetadataAnalysis.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1010
#include "llvm/ADT/APInt.h"
11+
#include "llvm/ADT/StringExtras.h"
12+
#include "llvm/ADT/StringRef.h"
1113
#include "llvm/IR/Constants.h"
1214
#include "llvm/IR/Instructions.h"
1315
#include "llvm/IR/Metadata.h"
1416
#include "llvm/IR/Module.h"
1517
#include "llvm/InitializePasses.h"
18+
#include "llvm/Support/ErrorHandling.h"
1619

1720
#define DEBUG_TYPE "dxil-metadata-analysis"
1821

@@ -33,15 +36,54 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
3336
MMDAI.ValidatorVersion =
3437
VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
3538
}
39+
40+
// For all HLSL Shader functions
41+
for (auto &F : M.functions()) {
42+
if (!F.hasFnAttribute("hlsl.shader"))
43+
continue;
44+
45+
EntryProperties EFP(F);
46+
// Get "hlsl.shader" attribute
47+
Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
48+
assert(EntryAttr.isValid() &&
49+
"Invalid value specified for HLSL function attribute hlsl.shader");
50+
StringRef EntryProfile = EntryAttr.getValueAsString();
51+
Triple T("", "", "", EntryProfile);
52+
EFP.ShaderStage = T.getEnvironment();
53+
// Get numthreads attribute value, if one exists
54+
StringRef NumThreadsStr =
55+
F.getFnAttribute("hlsl.numthreads").getValueAsString();
56+
if (!NumThreadsStr.empty()) {
57+
SmallVector<StringRef> NumThreadsVec;
58+
NumThreadsStr.split(NumThreadsVec, ',');
59+
assert(NumThreadsVec.size() == 3 && "Invalid numthreads specified");
60+
// Read in the three component values of numthreads
61+
[[maybe_unused]] bool Success =
62+
llvm::to_integer(NumThreadsVec[0], EFP.NumThreadsX, 10);
63+
assert(Success && "Failed to parse X component of numthreads");
64+
Success = llvm::to_integer(NumThreadsVec[1], EFP.NumThreadsY, 10);
65+
assert(Success && "Failed to parse Y component of numthreads");
66+
Success = llvm::to_integer(NumThreadsVec[2], EFP.NumThreadsZ, 10);
67+
assert(Success && "Failed to parse Z component of numthreads");
68+
}
69+
MMDAI.EntryPropertyVec.push_back(EFP);
70+
}
3671
return MMDAI;
3772
}
3873

3974
void ModuleMetadataInfo::print(raw_ostream &OS) const {
4075
OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
4176
OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
42-
OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
77+
OS << "Target Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
4378
<< "\n";
4479
OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
80+
for (const auto &EP : EntryPropertyVec) {
81+
OS << " " << EP.Entry->getName() << "\n";
82+
OS << " Function Shader Stage : "
83+
<< Triple::getEnvironmentTypeName(EP.ShaderStage) << "\n";
84+
OS << " NumThreads: " << EP.NumThreadsX << "," << EP.NumThreadsY << ","
85+
<< EP.NumThreadsZ << "\n";
86+
}
4587
}
4688

4789
//===----------------------------------------------------------------------===//
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.0-vertex"
3+
4+
; CHECK: Shader Model Version : 6.0
5+
; CHECK-NEXT: DXIL Version : 1.0
6+
; CHECK-NEXT: Shader Stage : vertex
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : vertex
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="vertex" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.8-compute"
3+
4+
; CHECK: Shader Model Version : 6.8
5+
; CHECK-NEXT: DXIL Version : 1.8
6+
; CHECK-NEXT: Shader Stage : compute
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : compute
10+
; CHECK-NEXT: NumThreads: 1,2,1
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.8-library"
3+
4+
; CHECK: Shader Model Version : 6.8
5+
; CHECK-NEXT: DXIL Version : 1.8
6+
; CHECK-NEXT: Target Shader Stage : library
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry_as
9+
; CHECK-NEXT: Function Shader Stage : amplification
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-NEXT: entry_ms
12+
; CHECK-NEXT: Function Shader Stage : mesh
13+
; CHECK-NEXT: NumThreads: 0,0,0
14+
; CHECK-NEXT: entry_cs
15+
; CHECK-NEXT: Function Shader Stage : compute
16+
; CHECK-NEXT: NumThreads: 1,2,1
17+
; CHECK-EMPTY:
18+
19+
define void @entry_as() #0 {
20+
entry:
21+
ret void
22+
}
23+
24+
define i32 @entry_ms(i32 %a) #1 {
25+
entry:
26+
ret i32 %a
27+
}
28+
29+
define float @entry_cs(float %f) #3 {
30+
entry:
31+
ret float %f
32+
}
33+
34+
attributes #0 = { noinline nounwind "hlsl.shader"="amplification" }
35+
attributes #1 = { noinline nounwind "hlsl.shader"="mesh" }
36+
attributes #3 = { noinline nounwind "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6-amplification"
3+
4+
; CHECK: Shader Model Version : 6
5+
; CHECK-NEXT: DXIL Version : 1.0
6+
; CHECK-NEXT: Target Shader Stage : amplification
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : amplification
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="amplification" }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
3+
target triple = "dxil-pc-shadermodel6.6-compute"
4+
5+
define void @entry() #0 {
6+
entry:
7+
ret void
8+
}
9+
10+
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
11+
12+
!dx.valver = !{!0}
13+
14+
!0 = !{i32 0, i32 0}
15+
16+
; CHECK: Shader Model Version : 6.6
17+
; CHECK-NEXT: DXIL Version : 1.6
18+
; CHECK-NEXT: Target Shader Stage : compute
19+
; CHECK-NEXT: Validator Version : 0.0
20+
; CHECK-NEXT: entry
21+
; CHECK-NEXT: Function Shader Stage : compute
22+
; CHECK-NEXT: NumThreads: 1,2,1
23+
; CHECK-EMPTY:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
3+
target triple = "dxil-pc-shadermodel6.6-compute"
4+
5+
; CHECK: Shader Model Version : 6.6
6+
; CHECK-NEXT: DXIL Version : 1.6
7+
; CHECK-NEXT: Target Shader Stage : compute
8+
; CHECK-NEXT: Validator Version : 0
9+
; CHECK-NEXT: entry
10+
; CHECK-NEXT: Function Shader Stage : compute
11+
; CHECK-NEXT: NumThreads: 1,2,1
12+
; CHECK-EMPTY:
13+
14+
define void @entry() #0 {
15+
entry:
16+
ret void
17+
}
18+
19+
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.6-geometry"
3+
4+
; CHECK: Shader Model Version : 6.6
5+
; CHECK-NEXT: DXIL Version : 1.6
6+
; CHECK-NEXT: Shader Stage : geometry
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : geometry
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="geometry" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.6-hull"
3+
4+
; CHECK: Shader Model Version : 6.6
5+
; CHECK-NEXT: DXIL Version : 1.6
6+
; CHECK-NEXT: Target Shader Stage : hull
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : hull
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="hull" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel6.6-mesh"
3+
4+
; CHECK: Shader Model Version : 6.6
5+
; CHECK-NEXT: DXIL Version : 1.6
6+
; CHECK-NEXT: Shader Stage : mesh
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : mesh
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="mesh" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel5.0-pixel"
3+
4+
; CHECK: Shader Model Version : 5.0
5+
; CHECK-NEXT: DXIL Version : 1.0
6+
; CHECK-NEXT: Shader Stage : pixel
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : pixel
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="pixel" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s
2+
target triple = "dxil-pc-shadermodel-vertex"
3+
4+
; CHECK: Shader Model Version : 0
5+
; CHECK-NEXT: DXIL Version : 1.0
6+
; CHECK-NEXT: Shader Stage : vertex
7+
; CHECK-NEXT: Validator Version : 0
8+
; CHECK-NEXT: entry
9+
; CHECK-NEXT: Function Shader Stage : vertex
10+
; CHECK-NEXT: NumThreads: 0,0,0
11+
; CHECK-EMPTY:
12+
13+
define void @entry() #0 {
14+
entry:
15+
ret void
16+
}
17+
18+
attributes #0 = { noinline nounwind "hlsl.shader"="vertex" }

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

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

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

8-
; ANALYSIS: Shader Model Version : 6.0
9-
; ANALYSIS-NEXT: DXIL Version : 1.0
10-
; ANALYSIS-NEXT: Shader Stage : vertex
11-
; ANALYSIS-NEXT: Validator Version : 0
12-
; ANALYSIS-EMPTY:
13-
147
define void @entry() #0 {
158
entry:
169
ret void

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

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

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

8-
; ANALYSIS: Shader Model Version : 6.8
9-
; ANALYSIS-NEXT: DXIL Version : 1.8
10-
; ANALYSIS-NEXT: Shader Stage : compute
11-
; ANALYSIS-NEXT: Validator Version : 0
12-
; ANALYSIS-EMPTY:
13-
147
define void @entry() #0 {
158
entry:
169
ret void

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

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

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

8-
; ANALYSIS: Shader Model Version : 6
9-
; ANALYSIS-NEXT: DXIL Version : 1.0
10-
; ANALYSIS-NEXT: Shader Stage : amplification
11-
; ANALYSIS-NEXT: Validator Version : 0
12-
; ANALYSIS-EMPTY:
13-
147
define void @entry() #0 {
158
entry:
169
ret void

0 commit comments

Comments
 (0)