1 //===-- JSONExporter.cpp  - Export Scops as JSON  -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Export the Scops build by ScopInfo pass as a JSON file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "polly/LinkAllPasses.h"
15 #include "polly/DependenceInfo.h"
16 #include "polly/Options.h"
17 #include "polly/ScopInfo.h"
18 #include "polly/ScopPass.h"
19 
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/RegionInfo.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/ToolOutputFile.h"
25 
26 #include "json/reader.h"
27 #include "json/writer.h"
28 
29 #include "isl/set.h"
30 #include "isl/map.h"
31 #include "isl/constraint.h"
32 #include "isl/printer.h"
33 
34 #include <memory>
35 #include <string>
36 #include <system_error>
37 
38 using namespace llvm;
39 using namespace polly;
40 
41 #define DEBUG_TYPE "polly-import-jscop"
42 
43 STATISTIC(NewAccessMapFound, "Number of updated access functions");
44 
45 namespace {
46 static cl::opt<std::string>
47     ImportDir("polly-import-jscop-dir",
48               cl::desc("The directory to import the .jscop files from."),
49               cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired,
50               cl::init("."), cl::cat(PollyCategory));
51 
52 static cl::opt<std::string>
53     ImportPostfix("polly-import-jscop-postfix",
54                   cl::desc("Postfix to append to the import .jsop files."),
55                   cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired,
56                   cl::init(""), cl::cat(PollyCategory));
57 
58 struct JSONExporter : public ScopPass {
59   static char ID;
60   explicit JSONExporter() : ScopPass(ID) {}
61 
62   std::string getFileName(Scop &S) const;
63   Json::Value getJSON(Scop &S) const;
64   virtual bool runOnScop(Scop &S);
65   void printScop(raw_ostream &OS, Scop &S) const;
66   void getAnalysisUsage(AnalysisUsage &AU) const;
67 };
68 
69 struct JSONImporter : public ScopPass {
70   static char ID;
71   std::vector<std::string> newAccessStrings;
72   explicit JSONImporter() : ScopPass(ID) {}
73 
74   std::string getFileName(Scop &S) const;
75   virtual bool runOnScop(Scop &S);
76   void printScop(raw_ostream &OS, Scop &S) const;
77   void getAnalysisUsage(AnalysisUsage &AU) const;
78 };
79 }
80 
81 char JSONExporter::ID = 0;
82 std::string JSONExporter::getFileName(Scop &S) const {
83   std::string FunctionName = S.getRegion().getEntry()->getParent()->getName();
84   std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
85   return FileName;
86 }
87 
88 void JSONExporter::printScop(raw_ostream &OS, Scop &S) const { S.print(OS); }
89 
90 Json::Value JSONExporter::getJSON(Scop &S) const {
91   Json::Value root;
92 
93   root["name"] = S.getRegion().getNameStr();
94   root["context"] = S.getContextStr();
95   root["statements"];
96 
97   for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
98     ScopStmt *Stmt = *SI;
99 
100     Json::Value statement;
101 
102     statement["name"] = Stmt->getBaseName();
103     statement["domain"] = Stmt->getDomainStr();
104     statement["schedule"] = Stmt->getScatteringStr();
105     statement["accesses"];
106 
107     for (MemoryAccess *MA : *Stmt) {
108       Json::Value access;
109 
110       access["kind"] = MA->isRead() ? "read" : "write";
111       access["relation"] = MA->getOriginalAccessRelationStr();
112 
113       statement["accesses"].append(access);
114     }
115 
116     root["statements"].append(statement);
117   }
118 
119   return root;
120 }
121 
122 bool JSONExporter::runOnScop(Scop &S) {
123   Region &R = S.getRegion();
124 
125   std::string FileName = ImportDir + "/" + getFileName(S);
126 
127   Json::Value jscop = getJSON(S);
128   Json::StyledWriter writer;
129   std::string fileContent = writer.write(jscop);
130 
131   // Write to file.
132   std::error_code EC;
133   tool_output_file F(FileName, EC, llvm::sys::fs::F_Text);
134 
135   std::string FunctionName = R.getEntry()->getParent()->getName();
136   errs() << "Writing JScop '" << R.getNameStr() << "' in function '"
137          << FunctionName << "' to '" << FileName << "'.\n";
138 
139   if (!EC) {
140     F.os() << fileContent;
141     F.os().close();
142     if (!F.os().has_error()) {
143       errs() << "\n";
144       F.keep();
145       return false;
146     }
147   }
148 
149   errs() << "  error opening file for writing!\n";
150   F.os().clear_error();
151 
152   return false;
153 }
154 
155 void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const {
156   AU.setPreservesAll();
157   AU.addRequired<ScopInfo>();
158 }
159 
160 Pass *polly::createJSONExporterPass() { return new JSONExporter(); }
161 
162 char JSONImporter::ID = 0;
163 std::string JSONImporter::getFileName(Scop &S) const {
164   std::string FunctionName = S.getRegion().getEntry()->getParent()->getName();
165   std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
166 
167   if (ImportPostfix != "")
168     FileName += "." + ImportPostfix;
169 
170   return FileName;
171 }
172 
173 void JSONImporter::printScop(raw_ostream &OS, Scop &S) const {
174   S.print(OS);
175   for (std::vector<std::string>::const_iterator I = newAccessStrings.begin(),
176                                                 E = newAccessStrings.end();
177        I != E; I++)
178     OS << "New access function '" << *I << "'detected in JSCOP file\n";
179 }
180 
181 typedef Dependences::StatementToIslMapTy StatementToIslMapTy;
182 
183 bool JSONImporter::runOnScop(Scop &S) {
184   Region &R = S.getRegion();
185   const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
186   const DataLayout &DL = getAnalysis<DataLayoutPass>().getDataLayout();
187 
188   std::string FileName = ImportDir + "/" + getFileName(S);
189 
190   std::string FunctionName = R.getEntry()->getParent()->getName();
191   errs() << "Reading JScop '" << R.getNameStr() << "' in function '"
192          << FunctionName << "' from '" << FileName << "'.\n";
193   ErrorOr<std::unique_ptr<MemoryBuffer>> result =
194       MemoryBuffer::getFile(FileName);
195   std::error_code ec = result.getError();
196 
197   if (ec) {
198     errs() << "File could not be read: " << ec.message() << "\n";
199     return false;
200   }
201 
202   Json::Reader reader;
203   Json::Value jscop;
204 
205   bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop);
206 
207   if (!parsingSuccessful) {
208     errs() << "JSCoP file could not be parsed\n";
209     return false;
210   }
211 
212   isl_set *OldContext = S.getContext();
213   isl_set *NewContext =
214       isl_set_read_from_str(S.getIslCtx(), jscop["context"].asCString());
215 
216   for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) {
217     isl_id *id = isl_set_get_dim_id(OldContext, isl_dim_param, i);
218     NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, id);
219   }
220 
221   isl_set_free(OldContext);
222   S.setContext(NewContext);
223 
224   StatementToIslMapTy &NewScattering = *(new StatementToIslMapTy());
225 
226   int index = 0;
227 
228   for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
229     Json::Value schedule = jscop["statements"][index]["schedule"];
230     isl_map *m = isl_map_read_from_str(S.getIslCtx(), schedule.asCString());
231     isl_space *Space = (*SI)->getDomainSpace();
232 
233     // Copy the old tuple id. This is necessary to retain the user pointer,
234     // that stores the reference to the ScopStmt this scattering belongs to.
235     m = isl_map_set_tuple_id(m, isl_dim_in,
236                              isl_space_get_tuple_id(Space, isl_dim_set));
237     isl_space_free(Space);
238     NewScattering[*SI] = m;
239     index++;
240   }
241 
242   if (!D.isValidScattering(S, &NewScattering)) {
243     errs() << "JScop file contains a scattering that changes the "
244            << "dependences. Use -disable-polly-legality to continue anyways\n";
245     for (StatementToIslMapTy::iterator SI = NewScattering.begin(),
246                                        SE = NewScattering.end();
247          SI != SE; ++SI)
248       isl_map_free(SI->second);
249     return false;
250   }
251 
252   for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
253     ScopStmt *Stmt = *SI;
254 
255     if (NewScattering.find(Stmt) != NewScattering.end())
256       Stmt->setScattering(NewScattering[Stmt]);
257   }
258 
259   int statementIdx = 0;
260   for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
261     ScopStmt *Stmt = *SI;
262 
263     int memoryAccessIdx = 0;
264     for (MemoryAccess *MA : *Stmt) {
265       Json::Value accesses = jscop["statements"][statementIdx]["accesses"]
266                                   [memoryAccessIdx]["relation"];
267       isl_map *newAccessMap =
268           isl_map_read_from_str(S.getIslCtx(), accesses.asCString());
269       isl_map *currentAccessMap = MA->getAccessRelation();
270 
271       if (isl_map_dim(newAccessMap, isl_dim_param) !=
272           isl_map_dim(currentAccessMap, isl_dim_param)) {
273         errs() << "JScop file changes the number of parameter dimensions\n";
274         isl_map_free(currentAccessMap);
275         isl_map_free(newAccessMap);
276         return false;
277       }
278 
279       isl_id *OutId = isl_map_get_tuple_id(currentAccessMap, isl_dim_out);
280       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_out, OutId);
281 
282       // We keep the old alignment, thus we cannot allow accesses to memory
283       // locations that were not accessed before if the alignment of the access
284       // is not the default alignment.
285       bool SpecialAlignment = true;
286       if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
287         SpecialAlignment =
288             DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
289       } else if (StoreInst *StoreI =
290                      dyn_cast<StoreInst>(MA->getAccessInstruction())) {
291         SpecialAlignment =
292             DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
293             StoreI->getAlignment();
294       }
295 
296       if (SpecialAlignment) {
297         isl_set *newAccessSet = isl_map_range(isl_map_copy(newAccessMap));
298         isl_set *currentAccessSet =
299             isl_map_range(isl_map_copy(currentAccessMap));
300         bool isSubset = isl_set_is_subset(newAccessSet, currentAccessSet);
301         isl_set_free(newAccessSet);
302         isl_set_free(currentAccessSet);
303 
304         if (!isSubset) {
305           errs() << "JScop file changes the accessed memory\n";
306           isl_map_free(currentAccessMap);
307           isl_map_free(newAccessMap);
308           return false;
309         }
310       }
311 
312       // We need to copy the isl_ids for the parameter dimensions to the new
313       // map. Without doing this the current map would have different
314       // ids then the new one, even though both are named identically.
315       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
316            i++) {
317         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
318         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
319       }
320 
321       // Copy the old tuple id. This is necessary to retain the user pointer,
322       // that stores the reference to the ScopStmt this access belongs to.
323       isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in);
324       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id);
325 
326       if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) {
327         errs() << "JScop file contains access function with incompatible "
328                << "dimensions\n";
329         isl_map_free(currentAccessMap);
330         isl_map_free(newAccessMap);
331         return false;
332       }
333       if (isl_map_dim(newAccessMap, isl_dim_out) != 1) {
334         errs() << "New access map in JScop file should be single dimensional\n";
335         isl_map_free(currentAccessMap);
336         isl_map_free(newAccessMap);
337         return false;
338       }
339       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
340         // Statistics.
341         ++NewAccessMapFound;
342         newAccessStrings.push_back(accesses.asCString());
343         MA->setNewAccessRelation(newAccessMap);
344       } else {
345         isl_map_free(newAccessMap);
346       }
347       isl_map_free(currentAccessMap);
348       memoryAccessIdx++;
349     }
350     statementIdx++;
351   }
352 
353   return false;
354 }
355 
356 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
357   ScopPass::getAnalysisUsage(AU);
358   AU.addRequired<DependenceInfo>();
359   AU.addRequired<DataLayoutPass>();
360 }
361 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
362 
363 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
364                       "Polly - Export Scops as JSON"
365                       " (Writes a .jscop file for each Scop)",
366                       false, false);
367 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
368 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
369                     "Polly - Export Scops as JSON"
370                     " (Writes a .jscop file for each Scop)",
371                     false, false)
372 
373 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
374                       "Polly - Import Scops from JSON"
375                       " (Reads a .jscop file for each Scop)",
376                       false, false);
377 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
378 INITIALIZE_PASS_DEPENDENCY(DataLayoutPass)
379 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
380                     "Polly - Import Scops from JSON"
381                     " (Reads a .jscop file for each Scop)",
382                     false, false)
383