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 =
205       getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
206   const DataLayout &DL =
207       S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
208 
209   std::string FileName = ImportDir + "/" + getFileName(S);
210 
211   std::string FunctionName = R.getEntry()->getParent()->getName();
212   errs() << "Reading JScop '" << R.getNameStr() << "' in function '"
213          << FunctionName << "' from '" << FileName << "'.\n";
214   ErrorOr<std::unique_ptr<MemoryBuffer>> result =
215       MemoryBuffer::getFile(FileName);
216   std::error_code ec = result.getError();
217 
218   if (ec) {
219     errs() << "File could not be read: " << ec.message() << "\n";
220     return false;
221   }
222 
223   Json::Reader reader;
224   Json::Value jscop;
225 
226   bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop);
227 
228   if (!parsingSuccessful) {
229     errs() << "JSCoP file could not be parsed\n";
230     return false;
231   }
232 
233   isl_set *OldContext = S.getContext();
234   isl_set *NewContext =
235       isl_set_read_from_str(S.getIslCtx(), jscop["context"].asCString());
236 
237   for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) {
238     isl_id *id = isl_set_get_dim_id(OldContext, isl_dim_param, i);
239     NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, id);
240   }
241 
242   isl_set_free(OldContext);
243   S.setContext(NewContext);
244 
245   StatementToIslMapTy NewSchedule;
246 
247   int index = 0;
248 
249   for (ScopStmt &Stmt : S) {
250     Json::Value schedule = jscop["statements"][index]["schedule"];
251     isl_map *m = isl_map_read_from_str(S.getIslCtx(), schedule.asCString());
252     isl_space *Space = Stmt.getDomainSpace();
253 
254     // Copy the old tuple id. This is necessary to retain the user pointer,
255     // that stores the reference to the ScopStmt this schedule belongs to.
256     m = isl_map_set_tuple_id(m, isl_dim_in,
257                              isl_space_get_tuple_id(Space, isl_dim_set));
258     for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
259       isl_id *id = isl_space_get_dim_id(Space, isl_dim_param, i);
260       m = isl_map_set_dim_id(m, isl_dim_param, i, id);
261     }
262     isl_space_free(Space);
263     NewSchedule[&Stmt] = m;
264     index++;
265   }
266 
267   if (!D.isValidSchedule(S, &NewSchedule)) {
268     errs() << "JScop file contains a schedule that changes the "
269            << "dependences. Use -disable-polly-legality to continue anyways\n";
270     for (StatementToIslMapTy::iterator SI = NewSchedule.begin(),
271                                        SE = NewSchedule.end();
272          SI != SE; ++SI)
273       isl_map_free(SI->second);
274     return false;
275   }
276 
277   auto ScheduleMap = isl_union_map_empty(S.getParamSpace());
278   for (ScopStmt &Stmt : S) {
279     if (NewSchedule.find(&Stmt) != NewSchedule.end())
280       ScheduleMap = isl_union_map_add_map(ScheduleMap, NewSchedule[&Stmt]);
281     else
282       ScheduleMap = isl_union_map_add_map(ScheduleMap, Stmt.getSchedule());
283   }
284 
285   S.setSchedule(ScheduleMap);
286 
287   int statementIdx = 0;
288   for (ScopStmt &Stmt : S) {
289     int memoryAccessIdx = 0;
290     for (MemoryAccess *MA : Stmt) {
291       Json::Value accesses = jscop["statements"][statementIdx]["accesses"]
292                                   [memoryAccessIdx]["relation"];
293       isl_map *newAccessMap =
294           isl_map_read_from_str(S.getIslCtx(), accesses.asCString());
295       isl_map *currentAccessMap = MA->getAccessRelation();
296 
297       if (isl_map_dim(newAccessMap, isl_dim_param) !=
298           isl_map_dim(currentAccessMap, isl_dim_param)) {
299         errs() << "JScop file changes the number of parameter dimensions\n";
300         isl_map_free(currentAccessMap);
301         isl_map_free(newAccessMap);
302         return false;
303       }
304 
305       isl_id *OutId = isl_map_get_tuple_id(currentAccessMap, isl_dim_out);
306       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_out, OutId);
307 
308       if (MA->isArrayKind()) {
309         // We keep the old alignment, thus we cannot allow accesses to memory
310         // locations that were not accessed before if the alignment of the
311         // access is not the default alignment.
312         bool SpecialAlignment = true;
313         if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
314           SpecialAlignment =
315               DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
316         } else if (StoreInst *StoreI =
317                        dyn_cast<StoreInst>(MA->getAccessInstruction())) {
318           SpecialAlignment =
319               DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
320               StoreI->getAlignment();
321         }
322 
323         if (SpecialAlignment) {
324           isl_set *newAccessSet = isl_map_range(isl_map_copy(newAccessMap));
325           isl_set *currentAccessSet =
326               isl_map_range(isl_map_copy(currentAccessMap));
327           bool isSubset = isl_set_is_subset(newAccessSet, currentAccessSet);
328           isl_set_free(newAccessSet);
329           isl_set_free(currentAccessSet);
330 
331           if (!isSubset) {
332             errs() << "JScop file changes the accessed memory\n";
333             isl_map_free(currentAccessMap);
334             isl_map_free(newAccessMap);
335             return false;
336           }
337         }
338       }
339 
340       // We need to copy the isl_ids for the parameter dimensions to the new
341       // map. Without doing this the current map would have different
342       // ids then the new one, even though both are named identically.
343       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
344            i++) {
345         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
346         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
347       }
348 
349       // Copy the old tuple id. This is necessary to retain the user pointer,
350       // that stores the reference to the ScopStmt this access belongs to.
351       isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in);
352       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id);
353 
354       if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) {
355         errs() << "JScop file contains access function with incompatible "
356                << "dimensions\n";
357         isl_map_free(currentAccessMap);
358         isl_map_free(newAccessMap);
359         return false;
360       }
361 
362       auto NewAccessDomain = isl_map_domain(isl_map_copy(newAccessMap));
363       auto CurrentAccessDomain = isl_map_domain(isl_map_copy(currentAccessMap));
364 
365       NewAccessDomain =
366           isl_set_intersect_params(NewAccessDomain, S.getContext());
367       CurrentAccessDomain =
368           isl_set_intersect_params(CurrentAccessDomain, S.getContext());
369 
370       if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
371           isl_bool_false) {
372         errs() << "Mapping not defined for all iteration domain elements\n";
373         isl_set_free(CurrentAccessDomain);
374         isl_set_free(NewAccessDomain);
375         isl_map_free(currentAccessMap);
376         isl_map_free(newAccessMap);
377         return false;
378       }
379 
380       isl_set_free(CurrentAccessDomain);
381       isl_set_free(NewAccessDomain);
382 
383       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
384         // Statistics.
385         ++NewAccessMapFound;
386         newAccessStrings.push_back(accesses.asCString());
387         MA->setNewAccessRelation(newAccessMap);
388       } else {
389         isl_map_free(newAccessMap);
390       }
391       isl_map_free(currentAccessMap);
392       memoryAccessIdx++;
393     }
394     statementIdx++;
395   }
396 
397   return false;
398 }
399 
400 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
401   ScopPass::getAnalysisUsage(AU);
402   AU.addRequired<DependenceInfo>();
403 }
404 
405 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
406 
407 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
408                       "Polly - Export Scops as JSON"
409                       " (Writes a .jscop file for each Scop)",
410                       false, false);
411 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
412 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
413                     "Polly - Export Scops as JSON"
414                     " (Writes a .jscop file for each Scop)",
415                     false, false)
416 
417 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
418                       "Polly - Import Scops from JSON"
419                       " (Reads a .jscop file for each Scop)",
420                       false, false);
421 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
422 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
423                     "Polly - Import Scops from JSON"
424                     " (Reads a .jscop file for each Scop)",
425                     false, false)
426