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