@@ -373,7 +373,6 @@ bool GenXCodeGenModule::runOnModule(Module& M)
373373
374374 pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
375375 CallGraph& CG = getAnalysis<CallGraphWrapperPass>().getCallGraph ();
376- FGA->setDefaultkernel (nullptr );
377376
378377 std::vector<std::vector<CallGraphNode*>*> SCCVec;
379378 for (auto I = scc_begin (&CG), IE = scc_end (&CG); I != IE; ++I)
@@ -394,28 +393,10 @@ bool GenXCodeGenModule::runOnModule(Module& M)
394393 {
395394 FGA->setSubGroupMap (F, F);
396395 FGA->createFunctionGroup (F);
397- // There may be multiple kernels, set one as the default
398- if (FGA->getDefaultKernel () == nullptr )
399- FGA->setDefaultkernel (F);
400396 }
401397 else if (F->hasFnAttribute (" IndirectlyCalled" ))
402398 {
403- // Add all externally linked functions into the default kernel group
404- Function* defaultKernel = FGA->getDefaultKernel ();
405- assert (defaultKernel && " kernel does not exist in this group" );
406- FunctionGroup* defaultFG = FGA->getGroupForHead (defaultKernel);
407- assert (defaultFG && " default kernel group does not exist" );
408- FGA->addToFunctionGroup (F, defaultFG, F);
409-
410- // Mark caller group if it calls or uses this function
411- for (auto U : F->users ())
412- {
413- if (CallInst * CI = dyn_cast<CallInst>(U))
414- {
415- Function* Caller = CI->getParent ()->getParent ();
416- FGA->getGroup (Caller)->m_hasExternFCall = true ;
417- }
418- }
399+ continue ;
419400 }
420401 else if (SCCNodes->size () == 1 )
421402 {
@@ -431,6 +412,9 @@ bool GenXCodeGenModule::runOnModule(Module& M)
431412 delete SCCNodes;
432413 }
433414
415+ // Add all indirect functions to the default kernel group
416+ FGA->addIndirectFuncsToKernelGroup (&M);
417+
434418 // By swapping, we sort the function list to ensure codegen order for
435419 // functions. This relies on llvm module pass manager's implementation detail.
436420 SmallVector<Function*, 16 > OrderedList;
@@ -465,7 +449,6 @@ bool GenXCodeGenModule::runOnModule(Module& M)
465449 }
466450 }
467451
468-
469452#if defined(_DEBUG)
470453 assert (FGA->verify ());
471454#endif
@@ -545,6 +528,50 @@ bool GenXFunctionGroupAnalysis::useStackCall(llvm::Function* F)
545528 return (F->hasFnAttribute (" visaStackCall" ));
546529}
547530
531+ void GenXFunctionGroupAnalysis::addIndirectFuncsToKernelGroup (llvm::Module* pModule)
532+ {
533+ auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
534+
535+ Function* defaultKernel = nullptr ;
536+ // Set the first kernel as the default
537+ for (auto I = pModule->begin (), E = pModule->end (); I != E; ++I)
538+ {
539+ Function* F = &(*I);
540+ if (isEntryFunc (pMdUtils, F))
541+ {
542+ defaultKernel = F;
543+ break ;
544+ }
545+ }
546+ assert (defaultKernel && " kernel does not exist in this group" );
547+
548+ FunctionGroup* defaultFG = getGroupForHead (defaultKernel);
549+ assert (defaultFG && " default kernel group does not exist" );
550+
551+ // Add all externally linked functions into the default kernel group
552+ for (auto I = pModule->begin (), E = pModule->end (); I != E; ++I)
553+ {
554+ Function* F = &(*I);
555+ if (F->isDeclaration () || isEntryFunc (pMdUtils, F)) continue ;
556+
557+ if (F->hasFnAttribute (" IndirectlyCalled" ))
558+ {
559+ addToFunctionGroup (F, defaultFG, F);
560+ defaultFG->m_hasIndirectFuncs = true ;
561+
562+ // Mark caller group if it calls or uses this function
563+ for (auto U : F->users ())
564+ {
565+ if (CallInst * CI = dyn_cast<CallInst>(U))
566+ {
567+ Function* Caller = CI->getParent ()->getParent ();
568+ getGroup (Caller)->m_hasExternFCall = true ;
569+ }
570+ }
571+ }
572+ }
573+ }
574+
548575bool GenXFunctionGroupAnalysis::rebuild (llvm::Module* Mod) {
549576 clear ();
550577 auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
@@ -570,6 +597,10 @@ bool GenXFunctionGroupAnalysis::rebuild(llvm::Module* Mod) {
570597 CurFG = createFunctionGroup (F);
571598 CurSubGrpH = F;
572599 }
600+ else if (F->hasFnAttribute (" IndirectlyCalled" ))
601+ {
602+ continue ;
603+ }
573604 else
574605 {
575606 if (useStackCall (F))
@@ -584,6 +615,9 @@ bool GenXFunctionGroupAnalysis::rebuild(llvm::Module* Mod) {
584615 }
585616 }
586617
618+ // Re-add all indirect functions to the default kernel group
619+ addIndirectFuncsToKernelGroup (Mod);
620+
587621 // Verification.
588622 if (!verify ())
589623 {
0 commit comments