From 20d70196c9a4da344d0944f3c78447c3bd7079c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= Date: Wed, 4 Jun 2025 13:22:37 +0200 Subject: [HLSL][SPIR-V] Implement vk::ext_builtin_input attribute (#138530) This variable attribute is used in HLSL to add Vulkan specific builtins in a shader. The attribute is documented here: https://github.com/microsoft/hlsl-specs/blob/17727e88fd1cb09013cb3a144110826af05f4dd5/proposals/0011-inline-spirv.md Those variable, even if marked as `static` are externally initialized by the pipeline/driver/GPU. This is handled by moving them to a specific address space `hlsl_input`, also added by this commit. The design for input variables in Clang can be found here: https://github.com/llvm/wg-hlsl/blob/355771361ef69259fef39a65caef8bff9cb4046d/proposals/0019-spirv-input-builtin.md Co-authored-by: Justin Bogner --- clang/lib/CodeGen/CodeGenModule.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 9383c57..468fc6e 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5763,7 +5763,17 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, getCUDARuntime().handleVarRegistration(D, *GV); } - GV->setInitializer(Init); + if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) { + // HLSL Input variables are considered to be set by the driver/pipeline, but + // only visible to a single thread/wave. + GV->setExternallyInitialized(true); + } else { + GV->setInitializer(Init); + } + + if (LangOpts.HLSL) + getHLSLRuntime().handleGlobalVarDefinition(D, GV); + if (emitter) emitter->finalize(GV); @@ -5806,6 +5816,12 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, !D->hasAttr()) Linkage = llvm::GlobalValue::InternalLinkage; + // HLSL variables in the input address space maps like memory-mapped + // variables. Even if they are 'static', they are externally initialized and + // read/write by the hardware/driver/pipeline. + if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) + Linkage = llvm::GlobalValue::ExternalLinkage; + GV->setLinkage(Linkage); if (D->hasAttr()) GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); -- cgit v1.1