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 1 commit
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
7 changes: 6 additions & 1 deletion llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_ANALYSIS_DXILMETADATA_H
#define LLVM_ANALYSIS_DXILMETADATA_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 +20,16 @@ namespace llvm {

namespace dxil {

struct FunctionProperties {
unsigned NumThreads[3];
};

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

std::unordered_map<Function *, FunctionProperties> FunctionPropertyMap{};
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we actually need to look up functions in a map for the function properties, or do we just need to be able to iterate through them? It might be nice to wire up the metadata analysis into DXILTranslateMetadata before adding more properties so that we can see how they'll be used.

In any case, std::unordered_map is very expensive as data structures go. Since this mapping is built up first and then queried later, we can probably just use a sorted vector instead of something heavyweight like this. There are some good tips on how to choose data structures for situations like this in the LLVM Programmer's Manual

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we actually need to look up functions in a map for the function properties, or do we just need to be able to iterate through them? It might be nice to wire up the metadata analysis into DXILTranslateMetadata before adding more properties so that we can see how they'll be used.

Function lookup is employed in an implementation I have as a follow on PR that consumes this information in DXILTranslateMetadata pass - see here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to MapVector.

void print(raw_ostream &OS) const;
};

Expand Down
50 changes: 50 additions & 0 deletions 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/StringRef.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.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,6 +38,37 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
MMDAI.ValidatorVersion =
VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
}

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

// Get numthreads attribute value
StringRef NumThreadsStr =
F.getFnAttribute("hlsl.numthreads").getValueAsString();
SmallVector<StringRef> NumThreadsVec;
NumThreadsStr.split(NumThreadsVec, ',');
if (NumThreadsVec.size() != 3) {
report_fatal_error(Twine(F.getName()) +
": Invalid numthreads specified",
/* gen_crash_diag */ false);
}
FunctionProperties EFP;
auto Zip =
llvm::zip(NumThreadsVec, MutableArrayRef<unsigned>(EFP.NumThreads));
for (auto It : Zip) {
StringRef Str = std::get<0>(It);
APInt V;
assert(!Str.getAsInteger(10, V) &&
"Failed to parse numthreads components as integer values");
unsigned &Num = std::get<1>(It);
Num = V.getLimitedValue();
}
MMDAI.FunctionPropertyMap.emplace(std::make_pair(std::addressof(F), EFP));
}
}
return MMDAI;
}

Expand All @@ -42,6 +78,20 @@ void ModuleMetadataInfo::print(raw_ostream &OS) const {
OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
<< "\n";
OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
for (auto MapItem : FunctionPropertyMap) {
MapItem.first->getReturnType()->print(OS, false, true);
OS << " " << MapItem.first->getName() << "(";
FunctionType *FT = MapItem.first->getFunctionType();
for (unsigned I = 0, Sz = FT->getNumParams(); I < Sz; ++I) {
if (I)
OS << ",";
FT->getParamType(I)->print(OS);
}
OS << ")\n";
OS << " NumThreads: " << MapItem.second.NumThreads[0] << ","
<< MapItem.second.NumThreads[1] << "," << MapItem.second.NumThreads[2]
<< "\n";
}
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ target triple = "dxil-pc-shadermodel6.8-compute"
; ANALYSIS-NEXT: DXIL Version : 1.8
; ANALYSIS-NEXT: Shader Stage : compute
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-NEXT: void entry()
; ANALYSIS-NEXT: NumThreads: 1,2,1
; ANALYSIS-EMPTY:

define void @entry() #0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1"
; ANALYSIS: Shader Model Version : 6.6
; ANALYSIS-NEXT: DXIL Version : 1.6
; ANALYSIS-NEXT: Shader Stage : compute
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-NEXT: Validator Version : 0.0
; ANALYSIS-NEXT: void entry()
; ANALYSIS-NEXT: NumThreads: 1,2,1
; ANALYSIS-EMPTY:
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ target triple = "dxil-pc-shadermodel6.6-compute"
; ANALYSIS-NEXT: DXIL Version : 1.6
; ANALYSIS-NEXT: Shader Stage : compute
; ANALYSIS-NEXT: Validator Version : 0
; ANALYSIS-NEXT: void entry()
; ANALYSIS-NEXT: NumThreads: 1,2,1
; ANALYSIS-EMPTY:

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