Skip to content
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

[DirectX backend] generate ISG1, OSG1 part for compute shader #90508

Merged
merged 6 commits into from
May 1, 2024
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
51 changes: 40 additions & 11 deletions llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/Constants.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/DXContainerPSVInfo.h"
#include "llvm/Pass.h"
#include "llvm/Support/MD5.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

using namespace llvm;
using namespace llvm::dxil;
using namespace llvm::mcdxbc;

namespace {
class DXContainerGlobals : public llvm::ModulePass {

GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
StringRef Name, StringRef SectionName);
GlobalVariable *getFeatureFlags(Module &M);
GlobalVariable *computeShaderHash(Module &M);
GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
StringRef SectionName);
void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);

public:
static char ID; // Pass identification, replacement for typeid
Expand All @@ -55,7 +62,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
llvm::SmallVector<GlobalValue *> Globals;
Globals.push_back(getFeatureFlags(M));
Globals.push_back(computeShaderHash(M));

addSignature(M, Globals);
appendToCompilerUsed(M, Globals);
return true;
}
Expand All @@ -68,12 +75,7 @@ GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {

Constant *FeatureFlagsConstant =
ConstantInt::get(M.getContext(), APInt(64, FeatureFlags));
auto *GV = new llvm::GlobalVariable(M, FeatureFlagsConstant->getType(), true,
GlobalValue::PrivateLinkage,
FeatureFlagsConstant, "dx.sfi0");
GV->setSection("SFI0");
GV->setAlignment(Align(4));
return GV;
return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
}

GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
Expand All @@ -96,14 +98,41 @@ GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {

Constant *ModuleConstant =
ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
GlobalValue::PrivateLinkage,
ModuleConstant, "dx.hash");
GV->setSection("HASH");
return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
}

GlobalVariable *DXContainerGlobals::buildContainerGlobal(
Module &M, Constant *Content, StringRef Name, StringRef SectionName) {
auto *GV = new llvm::GlobalVariable(
M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
GV->setSection(SectionName);
GV->setAlignment(Align(4));
return GV;
llvm-beanz marked this conversation as resolved.
Show resolved Hide resolved
}

GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
StringRef Name,
StringRef SectionName) {
SmallString<256> Data;
raw_svector_ostream OS(Data);
Sig.write(OS);
Constant *Constant =
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
return buildContainerGlobal(M, Constant, Name, SectionName);
}

void DXContainerGlobals::addSignature(Module &M,
SmallVector<GlobalValue *> &Globals) {
// FIXME: support graphics shader.
// see issue https://github.com/llvm/llvm-project/issues/90504.

Signature InputSig;
Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));

Signature OutputSig;
Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
}

char DXContainerGlobals::ID = 0;
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
"DXContainer Global Emitter", false, true)
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
target triple = "dxil-unknown-shadermodel6.0-compute"

; CHECK: @dx.isg1 = private constant [8 x i8] c"\00\00\00\00\08\00\00\00", section "ISG1", align 4
; CHECK: @dx.osg1 = private constant [8 x i8] c"\00\00\00\00\08\00\00\00", section "OSG1", align 4

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

attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }

!dx.valver = !{!0}

!0 = !{i32 1, i32 7}

; DXC: - Name: ISG1
; DXC-NEXT: Size: 8
; DXC-NEXT: Signature:
; DXC-NEXT: Parameters: []
; DXC: - Name: OSG1
; DXC-NEXT: Size: 8
; DXC-NEXT: Signature:
; DXC-NEXT: Parameters: []
Loading