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/Dependences.h"
16 #include "polly/Options.h"
17 #include "polly/ScopInfo.h"
18 #include "polly/ScopPass.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/ToolOutputFile.h"
23 #include "llvm/Support/system_error.h"
24 
25 #include "json/reader.h"
26 #include "json/writer.h"
27 
28 #include "isl/set.h"
29 #include "isl/map.h"
30 #include "isl/constraint.h"
31 #include "isl/printer.h"
32 
33 #include <memory>
34 #include <string>
35 
36 using namespace llvm;
37 using namespace polly;
38 
39 #define DEBUG_TYPE "polly-import-jscop"
40 
41 STATISTIC(NewAccessMapFound, "Number of updated access functions");
42 
43 namespace {
44 static cl::opt<std::string>
45 ImportDir("polly-import-jscop-dir",
46           cl::desc("The directory to import the .jscop files from."),
47           cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired,
48           cl::init("."), cl::cat(PollyCategory));
49 
50 static cl::opt<std::string>
51 ImportPostfix("polly-import-jscop-postfix",
52               cl::desc("Postfix to append to the import .jsop files."),
53               cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired,
54               cl::init(""), cl::cat(PollyCategory));
55 
56 struct JSONExporter : public ScopPass {
57   static char ID;
58   Scop *S;
59   explicit JSONExporter() : ScopPass(ID) {}
60 
61   std::string getFileName(Scop *S) const;
62   Json::Value getJSON(Scop &scop) const;
63   virtual bool runOnScop(Scop &S);
64   void printScop(raw_ostream &OS) const;
65   void getAnalysisUsage(AnalysisUsage &AU) const;
66 };
67 
68 struct JSONImporter : public ScopPass {
69   static char ID;
70   Scop *S;
71   std::vector<std::string> newAccessStrings;
72   explicit JSONImporter() : ScopPass(ID) {}
73 
74   std::string getFileName(Scop *S) const;
75   virtual bool runOnScop(Scop &S);
76   void printScop(raw_ostream &OS) const;
77   void getAnalysisUsage(AnalysisUsage &AU) const;
78 };
79 }
80 
81 char JSONExporter::ID = 0;
82 std::string JSONExporter::getFileName(Scop *S) const {
83   std::string FunctionName = S->getRegion().getEntry()->getParent()->getName();
84   std::string FileName = FunctionName + "___" + S->getNameStr() + ".jscop";
85   return FileName;
86 }
87 
88 void JSONExporter::printScop(raw_ostream &OS) const { S->print(OS); }
89 
90 Json::Value JSONExporter::getJSON(Scop &scop) const {
91   Json::Value root;
92 
93   root["name"] = S->getRegion().getNameStr();
94   root["context"] = S->getContextStr();
95   root["statements"];
96 
97   for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
98     ScopStmt *Stmt = *SI;
99 
100     Json::Value statement;
101 
102     statement["name"] = Stmt->getBaseName();
103     statement["domain"] = Stmt->getDomainStr();
104     statement["schedule"] = Stmt->getScatteringStr();
105     statement["accesses"];
106 
107     for (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(),
108                                    ME = Stmt->memacc_end();
109          MI != ME; ++MI) {
110       Json::Value access;
111 
112       access["kind"] = (*MI)->isRead() ? "read" : "write";
113       access["relation"] = (*MI)->getAccessRelationStr();
114 
115       statement["accesses"].append(access);
116     }
117 
118     root["statements"].append(statement);
119   }
120 
121   return root;
122 }
123 
124 bool JSONExporter::runOnScop(Scop &scop) {
125   S = &scop;
126   Region &R = S->getRegion();
127 
128   std::string FileName = ImportDir + "/" + getFileName(S);
129 
130   Json::Value jscop = getJSON(scop);
131   Json::StyledWriter writer;
132   std::string fileContent = writer.write(jscop);
133 
134   // Write to file.
135   std::string ErrInfo;
136   tool_output_file F(FileName.c_str(), ErrInfo, llvm::sys::fs::F_Text);
137 
138   std::string FunctionName = R.getEntry()->getParent()->getName();
139   errs() << "Writing JScop '" << R.getNameStr() << "' in function '"
140          << FunctionName << "' to '" << FileName << "'.\n";
141 
142   if (ErrInfo.empty()) {
143     F.os() << fileContent;
144     F.os().close();
145     if (!F.os().has_error()) {
146       errs() << "\n";
147       F.keep();
148       return false;
149     }
150   }
151 
152   errs() << "  error opening file for writing!\n";
153   F.os().clear_error();
154 
155   return false;
156 }
157 
158 void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const {
159   AU.setPreservesAll();
160   AU.addRequired<ScopInfo>();
161 }
162 
163 Pass *polly::createJSONExporterPass() { return new JSONExporter(); }
164 
165 char JSONImporter::ID = 0;
166 std::string JSONImporter::getFileName(Scop *S) const {
167   std::string FunctionName = S->getRegion().getEntry()->getParent()->getName();
168   std::string FileName = FunctionName + "___" + S->getNameStr() + ".jscop";
169 
170   if (ImportPostfix != "")
171     FileName += "." + ImportPostfix;
172 
173   return FileName;
174 }
175 
176 void JSONImporter::printScop(raw_ostream &OS) const {
177   S->print(OS);
178   for (std::vector<std::string>::const_iterator I = newAccessStrings.begin(),
179                                                 E = newAccessStrings.end();
180        I != E; I++)
181     OS << "New access function '" << *I << "'detected in JSCOP file\n";
182 }
183 
184 typedef Dependences::StatementToIslMapTy StatementToIslMapTy;
185 
186 bool JSONImporter::runOnScop(Scop &scop) {
187   S = &scop;
188   Region &R = S->getRegion();
189   Dependences *D = &getAnalysis<Dependences>();
190 
191   std::string FileName = ImportDir + "/" + getFileName(S);
192 
193   std::string FunctionName = R.getEntry()->getParent()->getName();
194   errs() << "Reading JScop '" << R.getNameStr() << "' in function '"
195          << FunctionName << "' from '" << FileName << "'.\n";
196   std::unique_ptr<MemoryBuffer> result;
197   error_code ec = MemoryBuffer::getFile(FileName, result);
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->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 = *(new StatementToIslMapTy());
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(&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 (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(),
267                                    ME = Stmt->memacc_end();
268          MI != ME; ++MI) {
269       Json::Value accesses = jscop["statements"][statementIdx]["accesses"]
270                                   [memoryAccessIdx]["relation"];
271       isl_map *newAccessMap =
272           isl_map_read_from_str(S->getIslCtx(), accesses.asCString());
273       isl_map *currentAccessMap = (*MI)->getAccessRelation();
274 
275       if (isl_map_dim(newAccessMap, isl_dim_param) !=
276           isl_map_dim(currentAccessMap, isl_dim_param)) {
277         errs() << "JScop file changes the number of parameter dimensions\n";
278         isl_map_free(currentAccessMap);
279         isl_map_free(newAccessMap);
280         return false;
281       }
282 
283       // We need to copy the isl_ids for the parameter dimensions to the new
284       // map. Without doing this the current map would have different
285       // ids then the new one, even though both are named identically.
286       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
287            i++) {
288         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
289         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
290       }
291 
292       // Copy the old tuple id. This is necessary to retain the user pointer,
293       // that stores the reference to the ScopStmt this access belongs to.
294       isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in);
295       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id);
296 
297       if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) {
298         errs() << "JScop file contains access function with incompatible "
299                << "dimensions\n";
300         isl_map_free(currentAccessMap);
301         isl_map_free(newAccessMap);
302         return false;
303       }
304       if (isl_map_dim(newAccessMap, isl_dim_out) != 1) {
305         errs() << "New access map in JScop file should be single dimensional\n";
306         isl_map_free(currentAccessMap);
307         isl_map_free(newAccessMap);
308         return false;
309       }
310       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
311         // Statistics.
312         ++NewAccessMapFound;
313         newAccessStrings.push_back(accesses.asCString());
314         (*MI)->setNewAccessRelation(newAccessMap);
315       } else {
316         isl_map_free(newAccessMap);
317       }
318       isl_map_free(currentAccessMap);
319       memoryAccessIdx++;
320     }
321     statementIdx++;
322   }
323 
324   return false;
325 }
326 
327 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
328   ScopPass::getAnalysisUsage(AU);
329   AU.addRequired<Dependences>();
330 }
331 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
332 
333 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
334                       "Polly - Export Scops as JSON"
335                       " (Writes a .jscop file for each Scop)",
336                       false, false);
337 INITIALIZE_PASS_DEPENDENCY(Dependences)
338 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
339                     "Polly - Export Scops as JSON"
340                     " (Writes a .jscop file for each Scop)",
341                     false, false)
342 
343 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
344                       "Polly - Import Scops from JSON"
345                       " (Reads a .jscop file for each Scop)",
346                       false, false);
347 INITIALIZE_PASS_DEPENDENCY(Dependences)
348 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
349                     "Polly - Import Scops from JSON"
350                     " (Reads a .jscop file for each Scop)",
351                     false, false)
352