Coverage Report

Created: 2023-10-30 17:15

/builds/2mk6rsew/0/parcoach/parcoach/src/aSSA/CollListLoopAnalysis.cpp
Line
Count
Source
1
#include "parcoach/CollListLoopAnalysis.h"
2
3
#include "PTACallGraph.h"
4
#include "Utils.h"
5
6
#include "llvm/IR/Function.h"
7
#include "llvm/IR/PassManager.h"
8
9
#define DEBUG_TYPE "coll-list-loop"
10
11
using namespace llvm;
12
13
namespace parcoach {
14
namespace {
15
struct CollListLoopVisitor : LoopVisitor<CollListLoopVisitor> {
16
  CollListLoopVisitor(PTACallGraph const &CG,
17
                      SmallPtrSetImpl<Value *> const &Comms)
18
1.82k
      : PTACG(CG), Communicators(Comms){};
19
  PTACallGraph const &PTACG;
20
  SmallPtrSetImpl<Value *> const &Communicators;
21
  CollectiveList::CommToBBToCollListMap CollListsPerComm;
22
1.40k
  void visitBB(Loop &L, BasicBlock *BB) {
23
1.40k
    LLVM_DEBUG({
24
1.40k
      dbgs() << "visitBB in loop:";
25
1.40k
      BB->printAsOperand(dbgs());
26
1.40k
      dbgs() << "\n";
27
1.40k
    });
28
1.40k
    if (BB == L.getHeader()) {
29
468
      for (Value *Comm : Communicators) {
30
468
        CollListsPerComm[Comm].insert({BB, CollectiveList()});
31
468
      }
32
456
      return;
33
456
    }
34
974
    for (Value *Comm : Communicators) {
35
      // assert(CollListsPerComm.count(Comm) && "Predecessor must be computed");
36
974
      auto &Lists = CollListsPerComm[Comm];
37
974
      auto ItSet = Lists.find(BB);
38
974
      if (ItSet != Lists.end()) {
39
        // It's the header of a nested loop.
40
9
        LLVM_DEBUG({
41
9
          dbgs() << "Using already inserted set: " << ItSet->second.toString()
42
9
                 << "\n";
43
9
        });
44
9
        return;
45
9
      }
46
965
      CollectiveList const &PredSet = Lists.lookup(*pred_begin(BB));
47
965
      bool IsNAVS = (LAI_.LoopHeaderToIncomingBlock.count(BB) == 0) &&
48
965
                    CollectiveList::NeighborsAreNAVS(Lists, BB, pred_begin(BB),
49
965
                                                     pred_end(BB));
50
965
      CollectiveList Current =
51
965
          CollectiveList::CreateFromBB(Lists, PTACG, IsNAVS, *BB, Comm);
52
965
      if (!IsNAVS) {
53
963
        Current.prependWith(PredSet);
54
963
      }
55
965
      LLVM_DEBUG({
56
965
        dbgs() << "Comm::Insert: " << Current.toString() << " for bb ";
57
965
        BB->printAsOperand(dbgs());
58
965
        dbgs() << " and comm ";
59
965
        Comm->print(dbgs());
60
965
        dbgs() << "\n";
61
965
      });
62
965
      Lists.insert({BB, std::move(Current)});
63
965
    }
64
950
  }
65
66
456
  void endOfLoop(Loop &L) {
67
456
    BasicBlock *Incoming{};
68
456
    BasicBlock *BackEdge{};
69
456
    L.getIncomingAndBackEdge(Incoming, BackEdge);
70
468
    for (Value *Comm : Communicators) {
71
468
      auto &Lists = CollListsPerComm[Comm];
72
468
      assert(Lists.count(L.getHeader()) && Lists[L.getHeader()].empty() &&
73
468
             "header not empty?!");
74
468
      Lists[L.getHeader()] = CollectiveList(L.getHeader(), Lists[BackEdge]);
75
468
    }
76
456
  }
77
78
1.82k
  LoopCFGInfo Visit(Function &F, FunctionAnalysisManager &FAM) {
79
1.82k
    LoopVisitor<CollListLoopVisitor>::Visit(F, FAM);
80
1.82k
    return {std::move(LAI_), std::move(CollListsPerComm)};
81
1.82k
  }
82
};
83
} // namespace
84
85
LoopCFGInfo CollListLoopAnalysis::run(Function &F,
86
1.82k
                                      FunctionAnalysisManager &FAM) {
87
1.82k
  TimeTraceScope TTS("CollListLoopAnalysis");
88
1.82k
  CollListLoopVisitor Visitor = CollListLoopVisitor(PTACG, Communicators);
89
1.82k
  LLVM_DEBUG(
90
1.82k
      { dbgs() << "Running CollListLoopAnalysis on " << F.getName() << "\n"; });
91
1.82k
  return Visitor.Visit(F, FAM);
92
1.82k
}
93
94
#ifndef NDEBUG
95
void LoopCFGInfo::dump() const {
96
  LLVM_DEBUG({
97
    dbgs() << "header to successorsMap:\n";
98
    for (auto &[L, Successors] : LAI.LoopHeaderToSuccessors) {
99
      dbgs() << "LHeader: ";
100
      L->printAsOperand(dbgs());
101
      dbgs() << ", succ: ";
102
      for (auto *BB : Successors) {
103
        BB->printAsOperand(dbgs());
104
        dbgs() << ", ";
105
      }
106
      dbgs() << "\n";
107
    }
108
    dbgs() << "Successor to loop header:\n";
109
    for (auto const &[BB, Pred] : LAI.LoopSuccessorToLoopHeader) {
110
      dbgs() << "BB: ";
111
      BB->printAsOperand(dbgs());
112
      dbgs() << ", preds: ";
113
      Pred->printAsOperand(dbgs());
114
      dbgs() << "\n";
115
    }
116
    dbgs() << "Header to incoming:\n";
117
    for (auto const &[BB, Incoming] : LAI.LoopHeaderToIncomingBlock) {
118
      dbgs() << "BB: ";
119
      BB->printAsOperand(dbgs());
120
      dbgs() << ", Incoming: ";
121
      Incoming->printAsOperand(dbgs());
122
      dbgs() << "\n";
123
    }
124
    dbgs() << "Coll List per Comm:\n";
125
    for (auto const &[Comm, ListPerBB] : CommToBBToCollList) {
126
      dbgs() << "Communicator: ";
127
      Comm->print(dbgs());
128
      dbgs() << "\n";
129
      for (auto const &[BB, List] : ListPerBB) {
130
        BB->printAsOperand(dbgs());
131
        dbgs() << ": " << List.toString() << "\n";
132
      }
133
      dbgs() << "------------\n";
134
    }
135
  });
136
}
137
#endif
138
139
} // namespace parcoach