@@ -652,53 +652,61 @@ static constexpr uint16_t IntrinsicsToAttributesMap[] = {)";
652652
653653AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
654654 FunctionType *FT) {)" ;
655+ // Find the max number of attributes to create the local array.
656+ unsigned MaxNumAttrs = 0 ;
657+ for (const auto [IntPtr, UniqueID] : UniqAttributes) {
658+ const CodeGenIntrinsic &Int = *IntPtr;
659+ unsigned NumAttrs =
660+ llvm::count_if (Int.ArgumentAttributes ,
661+ [](const auto &Attrs) { return !Attrs.empty (); });
662+ NumAttrs += hasFnAttributes (Int);
663+ MaxNumAttrs = std::max (MaxNumAttrs, NumAttrs);
664+ }
655665
656666 OS << formatv (R"(
657667 if (id == 0)
658668 return AttributeList();
659669
660670 uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
661671 uint8_t FnAttrID = PackedID >> 8;
672+ std::pair<unsigned, AttributeSet> AS[{}];
673+ unsigned NumAttrs = 0;
674+ bool HasFnAttr = false;
662675 switch(PackedID & 0xFF) {{
663676 default: llvm_unreachable("Invalid attribute number");
664- )" );
677+ )" ,
678+ MaxNumAttrs);
665679
666680 for (const auto [IntPtr, UniqueID] : UniqAttributes) {
667681 OS << formatv (" case {}:\n " , UniqueID);
668682 const CodeGenIntrinsic &Int = *IntPtr;
669683
670- // Keep track of the number of attributes we're writing out.
671- unsigned NumAttrs =
672- llvm::count_if (Int.ArgumentAttributes ,
673- [](const auto &Attrs) { return !Attrs.empty (); });
674- NumAttrs += hasFnAttributes (Int);
675- if (NumAttrs == 0 ) {
676- OS << " return AttributeList();\n " ;
677- continue ;
678- }
684+ unsigned NumAttrs = 0 ;
679685
680- OS << " return AttributeList::get(C, {\n " ;
681- ListSeparator LS (" ,\n " );
682686 for (const auto &[AttrIdx, Attrs] : enumerate(Int.ArgumentAttributes )) {
683687 if (Attrs.empty ())
684688 continue ;
685689
686690 unsigned ArgAttrID = UniqArgAttributes.find (Attrs)->second ;
687- OS << LS
688- << formatv (" {{{}, getIntrinsicArgAttributeSet(C, {}, "
689- " FT->getContainedType({}))}" ,
690- AttrIdx, ArgAttrID, AttrIdx);
691+ OS << formatv (" AS[{}] = {{{}, getIntrinsicArgAttributeSet(C, {}, "
692+ " FT->getContainedType({}))};\n " ,
693+ NumAttrs++, AttrIdx, ArgAttrID, AttrIdx);
691694 }
692695
693- if (hasFnAttributes (Int)) {
694- OS << LS
695- << " {AttributeList::FunctionIndex, "
696- " getIntrinsicFnAttributeSet(C, FnAttrID)} " ;
697- }
698- OS << " \n }) ;\n " ;
696+ if (hasFnAttributes (Int))
697+ OS << " HasFnAttr = true; \n " ;
698+
699+ if (NumAttrs)
700+ OS << formatv ( " NumAttrs = {}; \n " , NumAttrs);
701+ OS << " break ;\n " ;
699702 }
700703
701704 OS << R"( }
705+ if (HasFnAttr) {
706+ AS[NumAttrs++] = {AttributeList::FunctionIndex,
707+ getIntrinsicFnAttributeSet(C, FnAttrID)};
708+ }
709+ return AttributeList::get(C, ArrayRef(AS, NumAttrs));
702710}
703711#endif // GET_INTRINSIC_ATTRIBUTES
704712
0 commit comments