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