@@ -81,6 +81,7 @@ using ClangLoopSwappingServerResponse = AnalysisResponsePass<
8181 GlobalsAAWrapperPass, DIMemoryTraitPoolWrapper, DIMemoryEnvironmentWrapper,
8282 ClangLoopSwappingServerProvider>;
8383
84+
8485// / This provider access to function-level analysis results on client.
8586using ClangLoopSwappingProvider =
8687 FunctionPassAAProvider<AnalysisSocketImmutableWrapper>;
@@ -235,16 +236,16 @@ class LoopVisitor : public RecursiveASTVisitor<LoopVisitor> {
235236 }
236237
237238 void PrintLocations () {
238- outs ( ) << " 'for' loop locations:\n " ;
239+ LLVM_DEBUG ( dbgs ( ) << " 'for' loop locations:\n " ) ;
239240 for (auto locs: mRangePairs ) {
240241 for (auto location : locs) {
241242 SourceLocation begin = location.getBegin ();
242243 SourceLocation end = location.getEnd ();
243- outs ( ) << " Begin: " ;
244- begin.print (outs (), mSrcMgr );
245- outs ( ) << " ; End: " ;
246- end.print (outs (), mSrcMgr );
247- outs ( ) << ' \n ' ;
244+ LLVM_DEBUG ( dbgs ( ) << " Begin: " ) ;
245+ begin.print (dbgs (), mSrcMgr );
246+ LLVM_DEBUG ( dbgs ( ) << " ; End: " ) ;
247+ end.print (dbgs (), mSrcMgr );
248+ LLVM_DEBUG ( dbgs ( ) << ' \n ' ) ;
248249 }
249250 }
250251 }
@@ -327,7 +328,6 @@ void ClangLoopSwapping::initializeProviderOnServer() {
327328 [DIMTraitPoolServer](DIMemoryTraitPoolWrapper &Wrapper) {
328329 Wrapper.set (**DIMTraitPoolServer);
329330 });
330-
331331 auto &GlobalsAAServer = R->value <GlobalsAAWrapperPass *>()->getResult ();
332332 ClangLoopSwappingServerProvider::initialize<GlobalsAAResultImmutableWrapper>(
333333 [&GlobalsAAServer](GlobalsAAResultImmutableWrapper &Wrapper) {
@@ -348,7 +348,7 @@ void ClangLoopSwapping::releaseMemory() {
348348
349349bool ClangLoopSwapping::IsNoLoopID (MDNode *LoopID) {
350350 if (!LoopID || !(LoopID = getLoopID (LoopID))) {
351- LLVM_DEBUG ( dbgs () << " [LOOP SWAPPING]: ignore loop without ID." ) ;
351+ dbgs () << " [LOOP SWAPPING]: ignore loop without ID." ;
352352 return true ;
353353 }
354354 return false ;
@@ -359,30 +359,45 @@ std::vector<DIAliasNode *> ClangLoopSwapping::GetLoopNodes(MDNode *LoopID) {
359359 assert (DepItr != DIDepInfo->end () && " Loop must be analyzed!" );
360360 auto &DIDepSet = DepItr->get <DIDependenceSet>();
361361 DenseSet<const DIAliasNode *> Coverage;
362- accessCoverage<bcl::SimpleInserter>(DIDepSet, *DIAT, Coverage,
363- mGlobalOpts ->IgnoreRedundantMemory );
362+ auto tmp = mGlobalOpts ->IgnoreRedundantMemory ;
363+ auto &tmp2 = *DIAT;
364+ dbgs () << " Here1\n " ;
365+ accessCoverage<bcl::SimpleInserter>(DIDepSet, tmp2, Coverage,
366+ tmp);
367+ dbgs () << " Here2\n " ;
364368 std::vector<DIAliasNode *> nodes;
365369 for (auto &TS : DIDepSet) {
366370 auto node = TS.getNode ();
371+ // TS.print(dbgs() << "[LOOP SWAPPING]: current node is: ");
367372 if (!Coverage.count (node))
368373 continue ;
369- if (TS.is <trait::Readonly>()) {
370- outs () << " [LOOP SWAPPING]: readonly node found (" << TS.getNode () << " )\n " ;
374+ MemoryDescriptor Dptr = TS;
375+ if (Dptr.is <trait::Readonly>()) {
376+ dbgs () << " [LOOP SWAPPING]: readonly node found (" << TS.getNode () << " )\n " ;
371377 continue ;
372378 }
373- if (TS.is <trait::Reduction>()) {
374- outs () << " [LOOP SWAPPING]: reduction node found (" << TS.getNode () << " )\n " ;
379+ if (Dptr.is <trait::Reduction>()) {
380+ dbgs () << " [LOOP SWAPPING]: reduction node found (" << TS.getNode () << " ); type: " ;
381+ auto I = TS.begin (), EI = TS.end ();
382+ auto *Red = (**I).get <trait::Reduction>();
383+ auto Kind = Red->getKind ();
384+ if (!Red || Kind == trait::DIReduction::RK_NoReduction) {
385+ dbgs () << " No Reduction\n " ;
386+ } else if (Kind == trait::DIReduction::RK_Add) {
387+ dbgs () << " Add\n " ;
388+ } else if (Kind == trait::DIReduction::RK_Mult) {
389+ dbgs () << " Mult\n " ;
390+ }
375391 continue ;
376392 }
377- if (TS .is <trait::Shared>()) {
378- outs () << " [LOOP SWAPPING]: shared node found (" << TS.getNode () << " )\n " ;
393+ if (Dptr .is <trait::Shared>()) {
394+ dbgs () << " [LOOP SWAPPING]: shared node found (" << TS.getNode () << " )\n " ;
379395 continue ;
380396 }
381- /*
382397 if (TS.is <trait::Induction>()) {
383- outs () << "[LOOP SWAPPING]: induction node found (" << TS.getNode() << ")\n";
398+ dbgs () << " [LOOP SWAPPING]: induction node found (" << TS.getNode () << " )\n " ;
384399 continue ;
385- }*/
400+ }
386401 nodes.push_back (const_cast <DIAliasNode *>(node));
387402 }
388403 return nodes;
@@ -395,15 +410,14 @@ bool ClangLoopSwapping::IsSwappingAvailable(std::vector<Loop *> loops) {
395410 auto *Loop0_ID = loop0->getLoopID ();
396411 auto *Loop1_ID = loop1->getLoopID ();
397412
413+
398414 if (IsNoLoopID (Loop0_ID) || IsNoLoopID (Loop1_ID)) {
399- outs () << " [LOOP SWAPPING]: No loop ID.\n " ;
415+ dbgs () << " [LOOP SWAPPING]: No loop ID.\n " ;
400416 return false ;
401417 }
402-
403418 std::vector<DIAliasNode *> nodes0 = GetLoopNodes (Loop0_ID);
404419 std::vector<DIAliasNode *> nodes1 = GetLoopNodes (Loop1_ID);
405420 SpanningTreeRelation<DIAliasTree *> STR (DIAT);
406-
407421 for (auto *node0: nodes0) {
408422 for (auto *node1: nodes1) {
409423 if (!STR.isUnreachable (node0, node1))
@@ -422,13 +436,12 @@ void ClangLoopSwapping::SwapLoops(const std::vector<std::vector<SourceRange>> &m
422436 std::vector<Loop *> loops = mLoopPairs [i];
423437
424438 if (ranges.size () < 2 ) {
425- outs () << " [LOOP SWAPPING]: Too few loops for swap. Ignore.\n " ;
439+ dbgs () << " [LOOP SWAPPING]: Too few loops for swap. Ignore.\n " ;
426440 continue ;
427441 }
428442 if (ranges.size () > 2 ) {
429- outs () << " [LOOP SWAPPING]: Too many loops for swap. Ignore additional loops.\n " ;
443+ dbgs () << " [LOOP SWAPPING]: Too many loops for swap. Ignore additional loops.\n " ;
430444 }
431-
432445 if (IsSwappingAvailable (loops)) {
433446 SourceRange first = ranges[0 ];
434447 SourceRange second = ranges[1 ];
@@ -437,7 +450,7 @@ void ClangLoopSwapping::SwapLoops(const std::vector<std::vector<SourceRange>> &m
437450 mRewriter .ReplaceText (first, second_loop);
438451 mRewriter .ReplaceText (second, first_loop);
439452 } else {
440- outs () << " [LOOP SWAPPING]: Failed to swap loops: shared memory\n " ;
453+ dbgs () << " [LOOP SWAPPING]: Failed to swap loops: shared memory\n " ;
441454 }
442455 }
443456}
@@ -459,7 +472,7 @@ bool ClangLoopSwapping::LoadDependenceAnalysisInfo(Function &F) {
459472 }
460473 }
461474 if (!DIAT || !DIDepInfo) {
462- LLVM_DEBUG ( dbgs () << " [LOOP SWAPPING]: analysis server is not available\n " ) ;
475+ dbgs () << " [LOOP SWAPPING]: analysis server is not available\n " ;
463476 if (auto *P = getAnalysisIfAvailable<DIEstimateMemoryPass>())
464477 DIAT = &P->getAliasTree ();
465478 else
@@ -468,14 +481,13 @@ bool ClangLoopSwapping::LoadDependenceAnalysisInfo(Function &F) {
468481 DIDepInfo = &P->getDependencies ();
469482 else
470483 return false ;
471- LLVM_DEBUG (
472- dbgs () << " [LOOP SWAPPING]: use dependence analysis from client\n " );
484+ dbgs () << " [LOOP SWAPPING]: use dependence analysis from client\n " ;
473485 }
474486 return true ;
475487}
476488
477489bool ClangLoopSwapping::runOnFunction (Function &F) {
478- outs () << " test\n " ;
490+ dbgs () << " test\n " ;
479491 auto *M = F.getParent ();
480492 mTfmCtx = getAnalysis<TransformationEnginePass>().getContext (*M);
481493 if (!mTfmCtx || !mTfmCtx ->hasInstance ()) {
@@ -487,28 +499,23 @@ bool ClangLoopSwapping::runOnFunction(Function &F) {
487499 if (!FuncDecl)
488500 return false ;
489501
490- auto amSocket = &getAnalysis<AnalysisSocketImmutableWrapper>().get ();
491- auto amMemoryMatcher = &getAnalysis<MemoryMatcherImmutableWrapper>().get ();
492- auto amGlobalsAA = &getAnalysis<GlobalsAAWrapperPass>().getResult ();
502+ mSocket = &getAnalysis<AnalysisSocketImmutableWrapper>().get ();
503+ mMemoryMatcher = &getAnalysis<MemoryMatcherImmutableWrapper>().get ();
504+ mGlobalOpts = &getAnalysis<GlobalOptionsImmutableWrapper>().getOptions ();
505+ mGlobalsAA = &getAnalysis<GlobalsAAWrapperPass>().getResult ();
493506 initializeProviderOnClient (*M);
494507 initializeProviderOnServer ();
495- return false ;
496-
497508 if (!LoadDependenceAnalysisInfo (F))
498509 return false ;
499-
500- mGlobalOpts = &getAnalysis<GlobalOptionsImmutableWrapper>().getOptions ();
501510
502511 auto &mLoopInfo = getAnalysis<LoopMatcherPass>().getMatcher ();
503512 LoopVisitor lv (mTfmCtx ->getRewriter (), mLoopInfo );
504513 lv.TraverseDecl (FuncDecl);
505514 lv.PrintLocations ();
506-
507515 std::vector<std::vector<SourceRange>> mRangePairs = lv.getRangePairs ();
508516 std::vector<std::vector<Loop *>> mLoopPairs = lv.getLoopPairs ();
509517
510- auto &Provider = getAnalysis<ClangLoopSwappingProvider>(F);
511-
518+ // auto &Provider = getAnalysis<ClangLoopSwappingProvider>(F);
512519 SwapLoops (mRangePairs , mLoopPairs );
513520 return false ;
514521}
@@ -524,6 +531,8 @@ void ClangLoopSwapping::getAnalysisUsage(AnalysisUsage &AU) const {
524531 AU.addRequired <GlobalOptionsImmutableWrapper>();
525532 AU.addRequired <DIEstimateMemoryPass>();
526533 AU.addRequired <GlobalsAAWrapperPass>();
534+ AU.addRequired <DIMemoryEnvironmentWrapper>();
535+ AU.addRequired <DIMemoryTraitPoolWrapper>();
527536 AU.setPreservesAll ();
528537}
529538
@@ -542,6 +551,7 @@ INITIALIZE_PASS(ClangLoopSwappingServerResponse, "clang-loop-swapping-response",
542551char ClangLoopSwappingServer::ID = 0;
543552INITIALIZE_PASS (ClangLoopSwappingServer, " clang-loop-swapping-server" ,
544553 " Loop Swapping (Clang, Server)" , false , false )
554+
545555
546556INITIALIZE_PROVIDER(ClangLoopSwappingProvider,
547557 " clang-loop-swapping-provider" ,
@@ -561,6 +571,9 @@ INITIALIZE_PASS_DEPENDENCY(DIMemoryEnvironmentWrapper);
561571INITIALIZE_PASS_DEPENDENCY (DIMemoryTraitPoolWrapper);
562572INITIALIZE_PASS_DEPENDENCY (MemoryMatcherImmutableWrapper)
563573INITIALIZE_PASS_DEPENDENCY(ClangLoopSwappingProvider);
574+ INITIALIZE_PASS_DEPENDENCY (AnalysisSocketImmutableWrapper);
575+ INITIALIZE_PASS_DEPENDENCY (GlobalsAAWrapperPass);
576+ INITIALIZE_PASS_DEPENDENCY (GlobalsAAResultImmutableWrapper);
564577INITIALIZE_PASS_DEPENDENCY (ClangLoopSwappingServerProvider);
565578INITIALIZE_PASS_DEPENDENCY (ClangLoopSwappingServerResponse);
566579INITIALIZE_PASS_IN_GROUP_END (ClangLoopSwapping," clang-l-swap" ,
0 commit comments