-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Changes from 4 commits
04400db
92ffbbb
c067683
ccb97cc
b3a5362
93b26bc
b54693a
804cf7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
#ifndef LLVM_ANALYSIS_DXILMETADATA_H | ||
#define LLVM_ANALYSIS_DXILMETADATA_H | ||
|
||
#include "llvm/ADT/MapVector.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm/Support/VersionTuple.h" | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||
|
||
#define DEBUG_TYPE "dxil-metadata-analysis" | ||
|
||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[[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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use |
||
} | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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"; | ||
} | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
|
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" } |
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" } |
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" } |
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: |
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" } |
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" } |
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" } |
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" } |
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" } |
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" } |
There was a problem hiding this comment.
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