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

[SPIR-V] DRAFT: ext_builtin_input/ext_builtin_output #115187

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/AddressSpaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ enum class LangAS : unsigned {
// HLSL specific address spaces.
hlsl_groupshared,

// Vulkan specific address spaces.
vulkan_input,
vulkan_output,

// Wasm specific address spaces.
wasm_funcref,

Expand Down
26 changes: 26 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def HLSLBufferObj : SubsetSubject<HLSLBuffer,
[{isa<HLSLBufferDecl>(S)}],
"cbuffer/tbuffer">;

def HLSLInputBuiltin : SubsetSubject<Var,
[{S->hasGlobalStorage() && S->getType().isConstQualified() &&
S->getStorageClass()==StorageClass::SC_Extern}],
"static const global variables">;

def HLSLOutputBuiltin : SubsetSubject<Var,
[{S->hasGlobalStorage() && !S->getType().isConstQualified() &&
S->getStorageClass()==StorageClass::SC_Extern}],
"static const global variables">;

def ClassTmpl : SubsetSubject<CXXRecord, [{S->getDescribedClassTemplate()}],
"class templates">;

Expand Down Expand Up @@ -4588,6 +4598,22 @@ def HLSLNumThreads: InheritableAttr {
let Documentation = [NumThreadsDocs];
}

def HLSLVkExtBuiltinInput: InheritableAttr {
let Spellings = [CXX11<"vk", "ext_builtin_input">];
let Args = [IntArgument<"BuiltIn">];
let Subjects = SubjectList<[HLSLInputBuiltin], ErrorDiag>;
let LangOpts = [HLSL];
let Documentation = [VkExtBuiltinInputDocs];
}

def HLSLVkExtBuiltinOutput: InheritableAttr {
let Spellings = [CXX11<"vk", "ext_builtin_output">];
let Args = [IntArgument<"BuiltIn">];
let Subjects = SubjectList<[HLSLOutputBuiltin], ErrorDiag>;
let LangOpts = [HLSL];
let Documentation = [VkExtBuiltinOutputDocs];
}

def HLSLSV_GroupIndex: HLSLAnnotationAttr {
let Spellings = [HLSLAnnotation<"SV_GroupIndex">];
let Subjects = SubjectList<[ParmVar, GlobalVar]>;
Expand Down
20 changes: 20 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -7670,6 +7670,26 @@ The full documentation is available here: https://docs.microsoft.com/en-us/windo
}];
}

def VkExtBuiltinInputDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
The ``vk::ext_builtin_input`` attribute applies to HLSL global variables.
The ``BuiltIn`` value is the ID of the BuiltIn in the SPIR-V specification.

The full documentation is available here: https://microsoft.github.io/hlsl-specs/proposals/0011-inline-spirv.html
}];
}

def VkExtBuiltinOutputDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
The ``vk::ext_builtin_output`` attribute applies to HLSL global variables.
The ``BuiltIn`` value is the ID of the BuiltIn in the SPIR-V specification.

The full documentation is available here: https://microsoft.github.io/hlsl-specs/proposals/0011-inline-spirv.html
}];
}

def HLSLSV_ShaderTypeAttrDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Sema/ParsedAttr.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ class ParsedAttr final
}
}

LangAS asVulkanLangAS() const {
switch (getParsedKind()) {
case ParsedAttr::AT_HLSLVkExtBuiltinInput:
return LangAS::vulkan_input;
case ParsedAttr::AT_HLSLVkExtBuiltinOutput:
return LangAS::vulkan_output;
default:
return LangAS::Default;
}
}

AttributeCommonInfo::Kind getKind() const {
return AttributeCommonInfo::Kind(Info.AttrKind);
}
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Sema/SemaHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class SemaHLSL : public SemaBase {
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);

void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
void handleVkExtBuiltinInput(Decl *D, const ParsedAttr &AL);
void handleVkExtBuiltinOutput(Decl *D, const ParsedAttr &AL);
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,10 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
return "__funcref";
case LangAS::hlsl_groupshared:
return "groupshared";
case LangAS::vulkan_input:
return "Input";
case LangAS::vulkan_output:
return "Output";
default:
return std::to_string(toTargetAddressSpace(AS));
}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const LangASMap FakeAddrSpaceMap = {
11, // ptr32_uptr
12, // ptr64
13, // hlsl_groupshared
14, // vulkan_input
15, // vulkan_output
20, // wasm_funcref
};

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static const unsigned ARM64AddrSpaceMap[] = {
static_cast<unsigned>(AArch64AddrSpace::ptr32_uptr),
static_cast<unsigned>(AArch64AddrSpace::ptr64),
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
llvm::AMDGPUAS::FLAT_ADDRESS, // vulkan_input
llvm::AMDGPUAS::FLAT_ADDRESS, // vulkan_output
};

const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
Expand All @@ -83,6 +85,8 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
llvm::AMDGPUAS::FLAT_ADDRESS, // vulkan_input
llvm::AMDGPUAS::FLAT_ADDRESS, // vulkan_output

};
} // namespace targets
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/DirectX.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static const unsigned DirectXAddrSpaceMap[] = {
0, // ptr32_uptr
0, // ptr64
3, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static const unsigned NVPTXAddrSpaceMap[] = {
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const unsigned SPIRDefIsPrivMap[] = {
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
7, // vulkan_input
8, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down Expand Up @@ -80,6 +82,8 @@ static const unsigned SPIRDefIsGenMap[] = {
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
7, // vulkan_input
8, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/SystemZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static const unsigned ZOSAddressMap[] = {
1, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
0 // wasm_funcref
};

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/TCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
42 changes: 22 additions & 20 deletions clang/lib/Basic/Targets/WebAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ namespace clang {
namespace targets {

static const unsigned WebAssemblyAddrSpaceMap[] = {
0, // Default
0, // opencl_global
0, // opencl_local
0, // opencl_constant
0, // opencl_private
0, // opencl_generic
0, // opencl_global_device
0, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // sycl_global
0, // sycl_global_device
0, // sycl_global_host
0, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
0, // Default
0, // opencl_global
0, // opencl_local
0, // opencl_constant
0, // opencl_private
0, // opencl_generic
0, // opencl_global_device
0, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // sycl_global
0, // sycl_global_device
0, // sycl_global_host
0, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0, // ptr64
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
20, // wasm_funcref
};

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ static const unsigned X86AddrSpaceMap[] = {
271, // ptr32_uptr
272, // ptr64
0, // hlsl_groupshared
0, // vulkan_input
0, // vulkan_output
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
Expand Down
23 changes: 23 additions & 0 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,29 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {

void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
llvm::GlobalVariable *GV) {

if (HLSLVkExtBuiltinInputAttr *BuiltinAttr =
VD->getAttr<HLSLVkExtBuiltinInputAttr>()) {
LLVMContext &Ctx = CGM.getLLVMContext();
IRBuilder<> B(Ctx);
MDNode *Decoration = MDNode::get(
Ctx, {ConstantAsMetadata::get(B.getInt32(11 /* BuiltIn */)),
ConstantAsMetadata::get(B.getInt32(BuiltinAttr->getBuiltIn()))});
MDNode *Val = MDNode::get(Ctx, {Decoration});
GV->setMetadata("spirv.Decorations", Val);
}

if (HLSLVkExtBuiltinOutputAttr *BuiltinAttr =
VD->getAttr<HLSLVkExtBuiltinOutputAttr>()) {
LLVMContext &Ctx = CGM.getLLVMContext();
IRBuilder<> B(Ctx);
MDNode *Decoration = MDNode::get(
Ctx, {ConstantAsMetadata::get(B.getInt32(11 /* BuiltIn */)),
ConstantAsMetadata::get(B.getInt32(BuiltinAttr->getBuiltIn()))});
MDNode *Val = MDNode::get(Ctx, {Decoration});
GV->setMetadata("spirv.Decorations", Val);
}

// If the global variable has resource binding, add it to the list of globals
// that need resource binding initialization.
const HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5047,6 +5047,10 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
getOpenMPRuntime().registerTargetGlobalVariable(D, GV);

// HLSL related end of code gen work items.
if (LangOpts.HLSL)
getHLSLRuntime().handleGlobalVarDefinition(D, GV);

// FIXME: This code is overly simple and should be merged with other global
// handling.
GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
Expand Down Expand Up @@ -5628,9 +5632,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
getCUDARuntime().handleVarRegistration(D, *GV);
}

if (LangOpts.HLSL)
getHLSLRuntime().handleGlobalVarDefinition(D, GV);

GV->setInitializer(Init);
if (emitter)
emitter->finalize(GV);
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6987,6 +6987,12 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_HLSLNumThreads:
S.HLSL().handleNumThreadsAttr(D, AL);
break;
case ParsedAttr::AT_HLSLVkExtBuiltinInput:
S.HLSL().handleVkExtBuiltinInput(D, AL);
break;
case ParsedAttr::AT_HLSLVkExtBuiltinOutput:
S.HLSL().handleVkExtBuiltinOutput(D, AL);
break;
case ParsedAttr::AT_HLSLWaveSize:
S.HLSL().handleWaveSizeAttr(D, AL);
break;
Expand Down
63 changes: 63 additions & 0 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,69 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
}

namespace {

std::optional<uint32_t> validateVkExtBuiltin(Decl *D, const ParsedAttr &AL,
ASTContext &Ctx, Sema &SemaRef) {
llvm::Triple::OSType OSType = Ctx.getTargetInfo().getTriple().getOS();
if (!isa<VarDecl>(D)) {
// FIXME
SemaRef.Diag(AL.getLoc(), diag::err_hlsl_missing_resource_class);
return std::nullopt;
}

if (OSType != llvm::Triple::OSType::Vulkan) {
// FIXME
SemaRef.Diag(AL.getLoc(), diag::err_hlsl_missing_resource_class);
return std::nullopt;
}

uint32_t ID;
if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), ID)) {
// FIXME
SemaRef.Diag(AL.getLoc(), diag::err_hlsl_missing_resource_class);
return std::nullopt;
}

return ID;
}

} // anonymous namespace

void SemaHLSL::handleVkExtBuiltinInput(Decl *D, const ParsedAttr &AL) {
std::optional<uint32_t> ID =
validateVkExtBuiltin(D, AL, getASTContext(), SemaRef);
if (!ID)
return;

VarDecl *VD = cast<VarDecl>(D);
QualType NewType =
SemaRef.Context.getAddrSpaceQualType(VD->getType(), LangAS::vulkan_input);
VD->setType(NewType);

HLSLVkExtBuiltinInputAttr *NewAttr = ::new (getASTContext())
HLSLVkExtBuiltinInputAttr(getASTContext(), AL, *ID);
assert(NewAttr);
VD->addAttr(NewAttr);
}

void SemaHLSL::handleVkExtBuiltinOutput(Decl *D, const ParsedAttr &AL) {
std::optional<uint32_t> ID =
validateVkExtBuiltin(D, AL, getASTContext(), SemaRef);
if (!ID)
return;

VarDecl *VD = cast<VarDecl>(D);
QualType NewType = SemaRef.Context.getAddrSpaceQualType(
VD->getType(), LangAS::vulkan_output);
VD->setType(NewType);

HLSLVkExtBuiltinOutputAttr *NewAttr = ::new (getASTContext())
HLSLVkExtBuiltinOutputAttr(getASTContext(), AL, *ID);
assert(NewAttr);
VD->addAttr(NewAttr);
}

void SemaHLSL::handleNumThreadsAttr(Decl *D, const ParsedAttr &AL) {
llvm::VersionTuple SMVersion =
getASTContext().getTargetInfo().getTriple().getOSVersion();
Expand Down
Loading