@@ -562,17 +562,16 @@ static llvm::Value *createSPIRVBuiltinLoad(IRBuilder<> &B, llvm::Module &M,
562562 return B.CreateLoad (Ty, GV);
563563}
564564
565- llvm::Value *
566- CGHLSLRuntime::emitSystemSemanticLoad (IRBuilder<> &B, llvm::Type *Type,
567- const clang::DeclaratorDecl *Decl,
568- SemanticInfo &ActiveSemantic) {
569- if (isa<HLSLSV_GroupIndexAttr>(ActiveSemantic.Semantic )) {
565+ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad (
566+ IRBuilder<> &B, llvm::Type *Type, const clang::DeclaratorDecl *Decl,
567+ Attr *Semantic, std::optional<unsigned > Index) {
568+ if (isa<HLSLSV_GroupIndexAttr>(Semantic)) {
570569 llvm::Function *GroupIndex =
571570 CGM.getIntrinsic (getFlattenedThreadIdInGroupIntrinsic ());
572571 return B.CreateCall (FunctionCallee (GroupIndex));
573572 }
574573
575- if (isa<HLSLSV_DispatchThreadIDAttr>(ActiveSemantic. Semantic )) {
574+ if (isa<HLSLSV_DispatchThreadIDAttr>(Semantic)) {
576575 llvm::Intrinsic::ID IntrinID = getThreadIdIntrinsic ();
577576 llvm::Function *ThreadIDIntrinsic =
578577 llvm::Intrinsic::isOverloaded (IntrinID)
@@ -581,7 +580,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
581580 return buildVectorInput (B, ThreadIDIntrinsic, Type);
582581 }
583582
584- if (isa<HLSLSV_GroupThreadIDAttr>(ActiveSemantic. Semantic )) {
583+ if (isa<HLSLSV_GroupThreadIDAttr>(Semantic)) {
585584 llvm::Intrinsic::ID IntrinID = getGroupThreadIdIntrinsic ();
586585 llvm::Function *GroupThreadIDIntrinsic =
587586 llvm::Intrinsic::isOverloaded (IntrinID)
@@ -590,7 +589,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
590589 return buildVectorInput (B, GroupThreadIDIntrinsic, Type);
591590 }
592591
593- if (isa<HLSLSV_GroupIDAttr>(ActiveSemantic. Semantic )) {
592+ if (isa<HLSLSV_GroupIDAttr>(Semantic)) {
594593 llvm::Intrinsic::ID IntrinID = getGroupIdIntrinsic ();
595594 llvm::Function *GroupIDIntrinsic =
596595 llvm::Intrinsic::isOverloaded (IntrinID)
@@ -599,8 +598,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
599598 return buildVectorInput (B, GroupIDIntrinsic, Type);
600599 }
601600
602- if (HLSLSV_PositionAttr *S =
603- dyn_cast<HLSLSV_PositionAttr>(ActiveSemantic.Semantic )) {
601+ if (HLSLSV_PositionAttr *S = dyn_cast<HLSLSV_PositionAttr>(Semantic)) {
604602 if (CGM.getTriple ().getEnvironment () == Triple::EnvironmentType::Pixel)
605603 return createSPIRVBuiltinLoad (B, CGM.getModule (), Type,
606604 S->getAttrName ()->getName (),
@@ -611,29 +609,56 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
611609}
612610
613611llvm::Value *
614- CGHLSLRuntime::handleScalarSemanticLoad (IRBuilder<> &B, llvm::Type *Type,
615- const clang::DeclaratorDecl *Decl,
616- SemanticInfo &ActiveSemantic) {
617-
618- if (!ActiveSemantic.Semantic ) {
619- ActiveSemantic.Semantic = Decl->getAttr <HLSLSemanticAttr>();
620- if (!ActiveSemantic.Semantic ) {
621- CGM.getDiags ().Report (Decl->getInnerLocStart (),
622- diag::err_hlsl_semantic_missing);
623- return nullptr ;
612+ CGHLSLRuntime::handleScalarSemanticLoad (IRBuilder<> &B, const FunctionDecl *FD,
613+ llvm::Type *Type,
614+ const clang::DeclaratorDecl *Decl) {
615+
616+ HLSLSemanticAttr *Semantic = nullptr ;
617+ for (HLSLSemanticAttr *Item : FD->specific_attrs <HLSLSemanticAttr>()) {
618+ if (Item->getTargetDecl () == Decl) {
619+ Semantic = Item;
620+ break ;
624621 }
625- ActiveSemantic.Index = ActiveSemantic.Semantic ->getSemanticIndex ();
626622 }
623+ // Sema must create one attribute per scalar field.
624+ assert (Semantic);
627625
628- return emitSystemSemanticLoad (B, Type, Decl, ActiveSemantic);
626+ std::optional<unsigned > Index = std::nullopt ;
627+ if (Semantic->isSemanticIndexExplicit ())
628+ Index = Semantic->getSemanticIndex ();
629+ return emitSystemSemanticLoad (B, Type, Decl, Semantic, Index);
629630}
630631
631632llvm::Value *
632- CGHLSLRuntime::handleSemanticLoad (IRBuilder<> &B, llvm::Type *Type,
633- const clang::DeclaratorDecl *Decl,
634- SemanticInfo &ActiveSemantic) {
635- assert (!Type->isStructTy ());
636- return handleScalarSemanticLoad (B, Type, Decl, ActiveSemantic);
633+ CGHLSLRuntime::handleStructSemanticLoad (IRBuilder<> &B, const FunctionDecl *FD,
634+ llvm::Type *Type,
635+ const clang::DeclaratorDecl *Decl) {
636+ const llvm::StructType *ST = cast<StructType>(Type);
637+ const clang::RecordDecl *RD = Decl->getType ()->getAsRecordDecl ();
638+
639+ assert (std::distance (RD->field_begin (), RD->field_end ()) ==
640+ ST->getNumElements ());
641+
642+ llvm::Value *Aggregate = llvm::PoisonValue::get (Type);
643+ auto FieldDecl = RD->field_begin ();
644+ for (unsigned I = 0 ; I < ST->getNumElements (); ++I) {
645+ llvm::Value *ChildValue =
646+ handleSemanticLoad (B, FD, ST->getElementType (I), *FieldDecl);
647+ assert (ChildValue);
648+ Aggregate = B.CreateInsertValue (Aggregate, ChildValue, I);
649+ ++FieldDecl;
650+ }
651+
652+ return Aggregate;
653+ }
654+
655+ llvm::Value *
656+ CGHLSLRuntime::handleSemanticLoad (IRBuilder<> &B, const FunctionDecl *FD,
657+ llvm::Type *Type,
658+ const clang::DeclaratorDecl *Decl) {
659+ if (Type->isStructTy ())
660+ return handleStructSemanticLoad (B, FD, Type, Decl);
661+ return handleScalarSemanticLoad (B, FD, Type, Decl);
637662}
638663
639664void CGHLSLRuntime::emitEntryFunction (const FunctionDecl *FD,
@@ -680,8 +705,25 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
680705 }
681706
682707 const ParmVarDecl *PD = FD->getParamDecl (Param.getArgNo () - SRetOffset);
683- SemanticInfo ActiveSemantic = {nullptr , 0 };
684- Args.push_back (handleSemanticLoad (B, Param.getType (), PD, ActiveSemantic));
708+ llvm::Value *SemanticValue = nullptr ;
709+ if ([[maybe_unused]] HLSLParamModifierAttr *MA =
710+ PD->getAttr <HLSLParamModifierAttr>()) {
711+ llvm_unreachable (" Not handled yet" );
712+ } else {
713+ llvm::Type *ParamType =
714+ Param.hasByValAttr () ? Param.getParamByValType () : Param.getType ();
715+ SemanticValue = handleSemanticLoad (B, FD, ParamType, PD);
716+ if (!SemanticValue)
717+ return ;
718+ if (Param.hasByValAttr ()) {
719+ llvm::Value *Var = B.CreateAlloca (Param.getParamByValType ());
720+ B.CreateStore (SemanticValue, Var);
721+ SemanticValue = Var;
722+ }
723+ }
724+
725+ assert (SemanticValue);
726+ Args.push_back (SemanticValue);
685727 }
686728
687729 CallInst *CI = B.CreateCall (FunctionCallee (Fn), Args, OB);
0 commit comments