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 #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       // We keep the old alignment, thus we cannot allow accesses to memory
308       // locations that were not accessed before if the alignment of the access
309       // is not the default alignment.
310       bool SpecialAlignment = true;
311       if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
312         SpecialAlignment =
313             DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
314       } else if (StoreInst *StoreI =
315                      dyn_cast<StoreInst>(MA->getAccessInstruction())) {
316         SpecialAlignment =
317             DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
318             StoreI->getAlignment();
319       }
320 
321       if (SpecialAlignment) {
322         isl_set *newAccessSet = isl_map_range(isl_map_copy(newAccessMap));
323         isl_set *currentAccessSet =
324             isl_map_range(isl_map_copy(currentAccessMap));
325         bool isSubset = isl_set_is_subset(newAccessSet, currentAccessSet);
326         isl_set_free(newAccessSet);
327         isl_set_free(currentAccessSet);
328 
329         if (!isSubset) {
330           errs() << "JScop file changes the accessed memory\n";
331           isl_map_free(currentAccessMap);
332           isl_map_free(newAccessMap);
333           return false;
334         }
335       }
336 
337       // We need to copy the isl_ids for the parameter dimensions to the new
338       // map. Without doing this the current map would have different
339       // ids then the new one, even though both are named identically.
340       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
341            i++) {
342         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
343         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
344       }
345 
346       // Copy the old tuple id. This is necessary to retain the user pointer,
347       // that stores the reference to the ScopStmt this access belongs to.
348       isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in);
349       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id);
350 
351       if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) {
352         errs() << "JScop file contains access function with incompatible "
353                << "dimensions\n";
354         isl_map_free(currentAccessMap);
355         isl_map_free(newAccessMap);
356         return false;
357       }
358 
359       auto NewAccessDomain = isl_map_domain(isl_map_copy(newAccessMap));
360       auto CurrentAccessDomain = isl_map_domain(isl_map_copy(currentAccessMap));
361 
362       NewAccessDomain =
363           isl_set_intersect_params(NewAccessDomain, S.getContext());
364       CurrentAccessDomain =
365           isl_set_intersect_params(CurrentAccessDomain, S.getContext());
366 
367       if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
368           isl_bool_false) {
369         errs() << "Mapping not defined for all iteration domain elements\n";
370         isl_set_free(CurrentAccessDomain);
371         isl_set_free(NewAccessDomain);
372         isl_map_free(currentAccessMap);
373         isl_map_free(newAccessMap);
374         return false;
375       }
376 
377       isl_set_free(CurrentAccessDomain);
378       isl_set_free(NewAccessDomain);
379 
380       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
381         // Statistics.
382         ++NewAccessMapFound;
383         newAccessStrings.push_back(accesses.asCString());
384         MA->setNewAccessRelation(newAccessMap);
385       } else {
386         isl_map_free(newAccessMap);
387       }
388       isl_map_free(currentAccessMap);
389       memoryAccessIdx++;
390     }
391     statementIdx++;
392   }
393 
394   return false;
395 }
396 
397 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
398   ScopPass::getAnalysisUsage(AU);
399   AU.addRequired<DependenceInfo>();
400 }
401 
402 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
403 
404 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
405                       "Polly - Export Scops as JSON"
406                       " (Writes a .jscop file for each Scop)",
407                       false, false);
408 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
409 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
410                     "Polly - Export Scops as JSON"
411                     " (Writes a .jscop file for each Scop)",
412                     false, false)
413 
414 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
415                       "Polly - Import Scops from JSON"
416                       " (Reads a .jscop file for each Scop)",
417                       false, false);
418 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
419 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
420                     "Polly - Import Scops from JSON"
421                     " (Reads a .jscop file for each Scop)",
422                     false, false)
423