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