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