@@ -31,15 +31,55 @@ SPDX-License-Identifier: MIT
3131#include " llvm/Support/raw_ostream.h"
3232#include " llvm/Transforms/Utils/Cloning.h"
3333#include " llvm/Transforms/Utils/ValueMapper.h"
34- using namespace llvm ;
34+
35+ #include " GenXUtil.h"
3536
3637#include " llvmWrapper/IR/LegacyPassManagers.h"
3738#include " llvmWrapper/IR/PassTimingInfo.h"
38- #include " Probe/Assertion.h"
3939
40+ #include " Probe/Assertion.h"
4041
4142#define DEBUG_TYPE " functiongroup-passmgr"
4243
44+ using namespace llvm ;
45+
46+ bool FunctionGroup::verify () const {
47+ for (auto I = Functions.begin (), E = Functions.end (); I != E; ++I) {
48+ Function *F = &(**I);
49+ for (auto *U : F->users ()) {
50+ auto *CI = genx::checkFunctionCall (U, F);
51+ if (!CI)
52+ continue ;
53+
54+ // Note: it is expected that all users of any function from
55+ // Functions array belong to the same FG
56+ Function *Caller = CI->getFunction ();
57+ auto *OtherGroup = FGA->getAnyGroup (Caller);
58+ IGC_ASSERT_MESSAGE (OtherGroup == this ,
59+ " inconsisten function group detected!" );
60+ if (OtherGroup != this )
61+ return false ;
62+ }
63+ }
64+ return true ;
65+ }
66+
67+ #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
68+ void FunctionGroup::print (raw_ostream &OS) const {
69+ OS << " {{{" << getName () << " }}}\n " ;
70+ for (const Function *F : Functions) {
71+ OS << " " << F->getName () << " \n " ;
72+ }
73+
74+ for (const auto &EnItem : enumerate(Subgroups)) {
75+ OS << " --SGR[" << EnItem.index () << " ]: " ;
76+ OS << " <" << EnItem.value ()->getHead ()->getName () << " >]\n " ;
77+ }
78+ }
79+
80+ void FunctionGroup::dump () const { print (dbgs ()); }
81+ #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
82+
4383/* **********************************************************************
4484 * FunctionGroupAnalysis implementation
4585 */
@@ -57,13 +97,9 @@ void FunctionGroupAnalysis::clear() {
5797 for (auto T : TypesToProcess)
5898 GroupMap[T].clear ();
5999
60- for (auto i = begin (), e = end (); i != e; ++i)
61- delete *i;
62- for (auto i = NonMainGroups.begin (), e = NonMainGroups.end (); i != e; ++i)
63- delete *i;
64-
65100 Groups.clear ();
66101 NonMainGroups.clear ();
102+ Visited.clear ();
67103 M = nullptr ;
68104}
69105
@@ -140,11 +176,12 @@ void FunctionGroupAnalysis::addToFunctionGroup(FunctionGroup *FG, Function *F,
140176// createFunctionGroup : create new FunctionGroup for which F is the head
141177FunctionGroup *FunctionGroupAnalysis::createFunctionGroup (Function *F,
142178 FGType Type) {
143- auto FG = new FunctionGroup (this );
179+ auto *FG = new FunctionGroup (this );
180+ auto FGOwner = std::unique_ptr<FunctionGroup>(FG);
144181 if (Type == FGType::GROUP)
145- Groups.push_back (FG );
182+ Groups.push_back (std::move (FGOwner) );
146183 else
147- NonMainGroups.push_back (FG );
184+ NonMainGroups.push_back (std::move (FGOwner) );
148185 addToFunctionGroup (FG, F, Type);
149186 return FG;
150187}
@@ -211,7 +248,7 @@ bool FunctionGroupAnalysis::buildGroup(CallGraph &Callees, Function *F,
211248 }
212249 }
213250 } else if (!Visited.count (F)) {
214- Visited[F] = true ;
251+ Visited. insert (F) ;
215252 // group is created either on a function with a corresponding attribute
216253 // or on a root of a whole function tree that is kernel (genx_main)
217254 if (F->hasFnAttribute (TypeToAttr (Type)) ||
@@ -240,6 +277,30 @@ bool FunctionGroupAnalysis::buildGroup(CallGraph &Callees, Function *F,
240277 return result;
241278}
242279
280+ bool FunctionGroupAnalysis::verify () const {
281+ return std::all_of (Groups.begin (), Groups.end (),
282+ [](const auto &GR) { return GR->verify (); });
283+ }
284+
285+ #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
286+ void FunctionGroupAnalysis::print (raw_ostream &OS) const {
287+ OS << " Number of Groups = " << Groups.size () << " \n " ;
288+ for (const auto &X : enumerate(Groups)) {
289+ OS << " GR[" << X.index () << " ] = <\n " ;
290+ X.value ()->print (OS);
291+ OS << " >\n " ;
292+ }
293+ OS << " Number of SubGroups = " << NonMainGroups.size () << " \n " ;
294+ for (const auto &X : enumerate(NonMainGroups)) {
295+ OS << " SGR[" << X.index () << " ] = <\n " ;
296+ X.value ()->print (OS);
297+ OS << " >\n " ;
298+ }
299+ }
300+
301+ void FunctionGroupAnalysis::dump () const { print (dbgs ()); }
302+ #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
303+
243304// ===----------------------------------------------------------------------===//
244305// FGPassManager
245306//
0 commit comments