@@ -384,6 +384,30 @@ static Value *buildVectorInput(IRBuilder<> &B, Function *F, llvm::Type *Ty) {
384384 return B.CreateCall (F, {B.getInt32 (0 )});
385385}
386386
387+ static void addSPIRVBuiltinDecoration (llvm::GlobalVariable *GV,
388+ unsigned BuiltIn) {
389+ LLVMContext &Ctx = GV->getContext ();
390+ IRBuilder<> B (GV->getContext ());
391+ MDNode *Operands = MDNode::get (
392+ Ctx,
393+ {ConstantAsMetadata::get (B.getInt32 (/* Spirv::Decoration::BuiltIn */ 11 )),
394+ ConstantAsMetadata::get (B.getInt32 (BuiltIn))});
395+ MDNode *Decoration = MDNode::get (Ctx, {Operands});
396+ GV->addMetadata (" spirv.Decorations" , *Decoration);
397+ }
398+
399+ static llvm::Value *createSPIRVBuiltinLoad (IRBuilder<> &B, llvm::Module &M,
400+ llvm::Type *Ty, const Twine &Name,
401+ unsigned BuiltInID) {
402+ auto *GV = new llvm::GlobalVariable (
403+ M, Ty, /* isConstant= */ true , llvm::GlobalValue::ExternalLinkage,
404+ /* Initializer= */ nullptr , Name, /* insertBefore= */ nullptr ,
405+ llvm::GlobalVariable::GeneralDynamicTLSModel,
406+ /* AddressSpace */ 7 , /* isExternallyInitialized= */ true );
407+ addSPIRVBuiltinDecoration (GV, BuiltInID);
408+ return B.CreateLoad (Ty, GV);
409+ }
410+
387411llvm::Value *CGHLSLRuntime::emitInputSemantic (IRBuilder<> &B,
388412 const ParmVarDecl &D,
389413 llvm::Type *Ty) {
@@ -407,6 +431,12 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
407431 llvm::Function *GroupIDIntrinsic = CGM.getIntrinsic (getGroupIdIntrinsic ());
408432 return buildVectorInput (B, GroupIDIntrinsic, Ty);
409433 }
434+ if (D.hasAttr <HLSLSV_PositionAttr>()) {
435+ if (getArch () == llvm::Triple::spirv)
436+ return createSPIRVBuiltinLoad (B, CGM.getModule (), Ty, " sv_position" ,
437+ /* BuiltIn::Position */ 0 );
438+ llvm_unreachable (" SV_Position semantic not implemented for this target." );
439+ }
410440 assert (false && " Unhandled parameter attribute" );
411441 return nullptr ;
412442}
@@ -626,16 +656,8 @@ void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
626656
627657void CGHLSLRuntime::handleGlobalVarDefinition (const VarDecl *VD,
628658 llvm::GlobalVariable *GV) {
629- if (auto Attr = VD->getAttr <HLSLVkExtBuiltinInputAttr>()) {
630- LLVMContext &Ctx = GV->getContext ();
631- IRBuilder<> B (GV->getContext ());
632- MDNode *Operands = MDNode::get (
633- Ctx, {ConstantAsMetadata::get (
634- B.getInt32 (/* Spirv::Decoration::BuiltIn */ 11 )),
635- ConstantAsMetadata::get (B.getInt32 (Attr->getBuiltIn ()))});
636- MDNode *Decoration = MDNode::get (Ctx, {Operands});
637- GV->addMetadata (" spirv.Decorations" , *Decoration);
638- }
659+ if (auto Attr = VD->getAttr <HLSLVkExtBuiltinInputAttr>())
660+ addSPIRVBuiltinDecoration (GV, Attr->getBuiltIn ());
639661}
640662
641663llvm::Instruction *CGHLSLRuntime::getConvergenceToken (BasicBlock &BB) {
0 commit comments