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     NewScattering[*SI] = m;
249     index++;
250   }
251 
252   if (!D->isValidScattering(&NewScattering)) {
253     errs() << "JScop file contains a scattering that changes the "
254            << "dependences. Use -disable-polly-legality to continue anyways\n";
255     return false;
256   }
257 
258   for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
259     ScopStmt *Stmt = *SI;
260 
261     if (NewScattering.find(Stmt) != NewScattering.end())
262       Stmt->setScattering(NewScattering[Stmt]);
263   }
264 
265   int statementIdx = 0;
266   for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
267     ScopStmt *Stmt = *SI;
268 
269     int memoryAccessIdx = 0;
270     for (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(),
271          ME = Stmt->memacc_end(); MI != ME; ++MI) {
272       Json::Value accesses = jscop["statements"][statementIdx]
273                                   ["accesses"][memoryAccessIdx]["relation"];
274       isl_map *newAccessMap = isl_map_read_from_str(S->getIslCtx(),
275                                                     accesses.asCString());
276       isl_map *currentAccessMap = (*MI)->getAccessRelation();
277 
278       if (isl_map_dim(newAccessMap, isl_dim_param) !=
279           isl_map_dim(currentAccessMap, isl_dim_param)) {
280         errs() << "JScop file changes the number of parameter dimensions\n";
281         isl_map_free(currentAccessMap);
282         isl_map_free(newAccessMap);
283         return false;
284 
285       }
286 
287       // We need to copy the isl_ids for the parameter dimensions to the new
288       // map. Without doing this the current map would have different
289       // ids then the new one, even though both are named identically.
290       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
291            i++) {
292         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
293         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
294       }
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 
331 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
332                       "Polly - Import Scops from JSON"
333                       " (Reads a .jscop file for each Scop)", false, false)
334 INITIALIZE_PASS_DEPENDENCY(Dependences)
335 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
336                     "Polly - Import Scops from JSON"
337                     " (Reads a .jscop file for each Scop)", false, false)
338 
339 Pass *polly::createJSONImporterPass() {
340   return new JSONImporter();
341 }
342