IR2Vec
Loading...
Searching...
No Matches
FlowAware.h
1//===- FlowAware.h - Flow-aware embeddings of IR2Vec ------------*- C++ -*-===//
2//
3// Part of the IR2Vec Project, under the Apache License v2.0 with LLVM
4// Exceptions. See the LICENSE file for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __IR2Vec_FA_H__
10#define __IR2Vec_FA_H__
11
12#include "utils.h"
13
14#include "llvm/ADT/MapVector.h"
15#include "llvm/ADT/SmallSet.h"
16#include "llvm/Analysis/CallGraph.h"
17#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/Dominators.h"
20#include "llvm/IR/Function.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/raw_ostream.h"
23#include <fstream>
24#include <unordered_map>
25
26class IR2Vec_FA {
27
28private:
29 llvm::Module &M;
30 std::string res;
31 IR2Vec::Vector pgmVector;
32 unsigned dataMissCounter;
33 unsigned cyclicCounter;
34
35 llvm::SmallDenseMap<llvm::StringRef, unsigned> memWriteOps;
36 llvm::SmallDenseMap<const llvm::Instruction *, bool> livelinessMap;
37 llvm::SmallDenseMap<llvm::StringRef, unsigned> memAccessOps;
38
39 llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector, 128>
40 instVecMap;
41 llvm::SmallMapVector<const llvm::Function *, IR2Vec::Vector, 16> funcVecMap;
42
43 llvm::SmallMapVector<const llvm::Function *,
44 llvm::SmallVector<const llvm::Function *, 10>, 16>
45 funcCallMap;
46
47 llvm::SmallMapVector<const llvm::Instruction *,
48 llvm::SmallVector<const llvm::Instruction *, 10>, 16>
49 writeDefsMap;
50
51 llvm::SmallMapVector<const llvm::Instruction *,
52 llvm::SmallVector<const llvm::Instruction *, 10>, 16>
53 instReachingDefsMap;
54
55 // Reverse instReachingDefsMap
56 llvm::SmallMapVector<const llvm::Instruction *,
57 llvm::SmallVector<const llvm::Instruction *, 10>, 16>
58 reverseReachingDefsMap;
59
60 llvm::SmallVector<const llvm::Instruction *, 20> instSolvedBySolver;
61
62 llvm::SmallVector<llvm::SmallVector<const llvm::Instruction *, 10>, 10>
63 allSCCs;
64
65 llvm::SmallMapVector<const llvm::Instruction *,
66 llvm::SmallVector<llvm::Instruction *, 16>, 16>
67 killMap;
68
69 std::map<int, std::vector<int>> SCCAdjList;
70
71 void getAllSCC();
72
73 IR2Vec::Vector getValue(std::string key);
74 void collectWriteDefsMap(llvm::Module &M);
75 void getTransitiveUse(
76 const llvm::Instruction *root, const llvm::Instruction *def,
77 llvm::SmallVector<const llvm::Instruction *, 100> &visitedList,
78 llvm::SmallVector<const llvm::Instruction *, 10> toAppend = {});
79 llvm::SmallVector<const llvm::Instruction *, 10>
80 getReachingDefs(const llvm::Instruction *, unsigned i);
81
82 void solveSingleComponent(
83 const llvm::Instruction &I,
84 llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector, 16>
85 &instValMap);
86 void getPartialVec(const llvm::Instruction &I,
87 llvm::SmallMapVector<const llvm::Instruction *,
88 IR2Vec::Vector, 16> &instValMap);
89
90 void solveInsts(llvm::SmallMapVector<const llvm::Instruction *,
91 IR2Vec::Vector, 16> &instValMap);
92 std::vector<int> topoOrder(int size);
93
94 void topoDFS(int vertex, std::vector<bool> &Visited,
95 std::vector<int> &visitStack);
96
97 void inst2Vec(const llvm::Instruction &I,
98 llvm::SmallVector<llvm::Function *, 15> &funcStack,
99 llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector,
100 16> &instValMap);
101 void traverseRD(const llvm::Instruction *inst,
102 std::unordered_map<const llvm::Instruction *, bool> &Visited,
103 llvm::SmallVector<const llvm::Instruction *, 10> &timeStack);
104
105 void DFSUtil(const llvm::Instruction *inst,
106 std::unordered_map<const llvm::Instruction *, bool> &Visited,
107 llvm::SmallVector<const llvm::Instruction *, 10> &set);
108
109 void bb2Vec(llvm::BasicBlock &B,
110 llvm::SmallVector<llvm::Function *, 15> &funcStack);
111 IR2Vec::Vector func2Vec(llvm::Function &F,
112 llvm::SmallVector<llvm::Function *, 15> &funcStack);
113
114 bool isMemOp(llvm::StringRef opcode, unsigned &operand,
115 llvm::SmallDenseMap<llvm::StringRef, unsigned> map);
116 std::string splitAndPipeFunctionName(std::string s);
117
118 void TransitiveReads(llvm::SmallVector<llvm::Instruction *, 16> &Killlist,
119 llvm::Instruction *Inst, llvm::BasicBlock *ParentBB);
120 llvm::SmallVector<llvm::Instruction *, 16>
121 createKilllist(llvm::Instruction *Arg, llvm::Instruction *writeInst);
122
123 // For Debugging
124 void print(IR2Vec::Vector t, unsigned pos) { llvm::outs() << t[pos]; }
125
126 void updateFuncVecMap(
127 llvm::Function *function,
128 llvm::SmallSet<const llvm::Function *, 16> &visitedFunctions);
129
130 void updateFuncVecMapWithCallee(const llvm::Function *function);
131
132public:
133 IR2Vec_FA(llvm::Module &M) : M{M} {
134
135 pgmVector = IR2Vec::Vector(DIM, 0);
136 res = "";
137
138 memWriteOps.try_emplace("store", 1);
139 memWriteOps.try_emplace("cmpxchg", 0);
140 memWriteOps.try_emplace("atomicrmw", 0);
141
142 memAccessOps.try_emplace("getelementptr", 0);
143 memAccessOps.try_emplace("load", 0);
144
145 dataMissCounter = 0;
146 cyclicCounter = 0;
147
148 collectWriteDefsMap(M);
149
150 llvm::CallGraph cg = llvm::CallGraph(M);
151
152 for (auto callItr = cg.begin(); callItr != cg.end(); callItr++) {
153 if (callItr->first && !callItr->first->isDeclaration()) {
154 auto ParentFunc = callItr->first;
155 llvm::CallGraphNode *cgn = callItr->second.get();
156 if (cgn) {
157
158 for (auto It = cgn->begin(); It != cgn->end(); It++) {
159
160 auto func = It->second->getFunction();
161 if (func && !func->isDeclaration()) {
162 funcCallMap[ParentFunc].push_back(func);
163 }
164 }
165 }
166 }
167 }
168 }
169
170 void generateFlowAwareEncodings(std::ostream *o = nullptr,
171 std::ostream *missCount = nullptr,
172 std::ostream *cyclicCount = nullptr);
173
174 // newly added
175
176 void generateFlowAwareEncodingsForFunction(
177 std::ostream *o = nullptr, std::string name = "",
178 std::ostream *missCount = nullptr, std::ostream *cyclicCount = nullptr);
179
180 llvm::SmallMapVector<const llvm::Instruction *, IR2Vec::Vector, 128>
181 getInstVecMap() {
182 return instVecMap;
183 }
184
185 llvm::SmallMapVector<const llvm::Function *, IR2Vec::Vector, 16>
186 getFuncVecMap() {
187 return funcVecMap;
188 }
189
190 IR2Vec::Vector getProgramVector() { return pgmVector; }
191};
192
193#endif
Definition FlowAware.h:26