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