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 #include "polly/Support/ScopLocation.h"
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 #include "isl/constraint.h"
27 #include "isl/map.h"
28 #include "isl/printer.h"
29 #include "isl/set.h"
30 #include "isl/union_map.h"
31 #include "json/reader.h"
32 #include "json/writer.h"
33 #include <memory>
34 #include <string>
35 #include <system_error>
36 
37 using namespace llvm;
38 using namespace polly;
39 
40 #define DEBUG_TYPE "polly-import-jscop"
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("."), cl::cat(PollyCategory));
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(""), cl::cat(PollyCategory));
56 
57 struct JSONExporter : public ScopPass {
58   static char ID;
59   explicit JSONExporter() : ScopPass(ID) {}
60 
61   std::string getFileName(Scop &S) const;
62   Json::Value getJSON(Scop &S) const;
63   virtual bool runOnScop(Scop &S);
64   void printScop(raw_ostream &OS, Scop &S) const;
65   void getAnalysisUsage(AnalysisUsage &AU) const;
66 };
67 
68 struct JSONImporter : public ScopPass {
69   static char ID;
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, Scop &S) 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, Scop &S) const { S.print(OS); }
88 
89 Json::Value JSONExporter::getJSON(Scop &S) const {
90   Json::Value root;
91   unsigned LineBegin, LineEnd;
92   std::string FileName;
93 
94   getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName);
95   std::string Location;
96   if (LineBegin != (unsigned)-1)
97     Location = FileName + ":" + std::to_string(LineBegin) + "-" +
98                std::to_string(LineEnd);
99 
100   root["name"] = S.getRegion().getNameStr();
101   root["context"] = S.getContextStr();
102   if (LineBegin != (unsigned)-1)
103     root["location"] = Location;
104   root["statements"];
105 
106   for (ScopStmt &Stmt : S) {
107     Json::Value statement;
108 
109     statement["name"] = Stmt.getBaseName();
110     statement["domain"] = Stmt.getDomainStr();
111     statement["schedule"] = Stmt.getScheduleStr();
112     statement["accesses"];
113 
114     for (MemoryAccess *MA : Stmt) {
115       Json::Value access;
116 
117       access["kind"] = MA->isRead() ? "read" : "write";
118       access["relation"] = MA->getOriginalAccessRelationStr();
119 
120       statement["accesses"].append(access);
121     }
122 
123     root["statements"].append(statement);
124   }
125 
126   return root;
127 }
128 
129 bool JSONExporter::runOnScop(Scop &S) {
130   Region &R = S.getRegion();
131 
132   std::string FileName = ImportDir + "/" + getFileName(S);
133 
134   Json::Value jscop = getJSON(S);
135   Json::StyledWriter writer;
136   std::string fileContent = writer.write(jscop);
137 
138   // Write to file.
139   std::error_code EC;
140   tool_output_file F(FileName, EC, llvm::sys::fs::F_Text);
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 (!EC) {
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 Pass *polly::createJSONExporterPass() { return new JSONExporter(); }
168 
169 char JSONImporter::ID = 0;
170 std::string JSONImporter::getFileName(Scop &S) const {
171   std::string FunctionName = S.getRegion().getEntry()->getParent()->getName();
172   std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
173 
174   if (ImportPostfix != "")
175     FileName += "." + ImportPostfix;
176 
177   return FileName;
178 }
179 
180 void JSONImporter::printScop(raw_ostream &OS, Scop &S) const {
181   S.print(OS);
182   for (std::vector<std::string>::const_iterator I = newAccessStrings.begin(),
183                                                 E = newAccessStrings.end();
184        I != E; I++)
185     OS << "New access function '" << *I << "'detected in JSCOP file\n";
186 }
187 
188 typedef Dependences::StatementToIslMapTy StatementToIslMapTy;
189 
190 bool JSONImporter::runOnScop(Scop &S) {
191   Region &R = S.getRegion();
192   const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
193   const DataLayout &DL =
194       S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
195 
196   std::string FileName = ImportDir + "/" + getFileName(S);
197 
198   std::string FunctionName = R.getEntry()->getParent()->getName();
199   errs() << "Reading JScop '" << R.getNameStr() << "' in function '"
200          << FunctionName << "' from '" << FileName << "'.\n";
201   ErrorOr<std::unique_ptr<MemoryBuffer>> result =
202       MemoryBuffer::getFile(FileName);
203   std::error_code ec = result.getError();
204 
205   if (ec) {
206     errs() << "File could not be read: " << ec.message() << "\n";
207     return false;
208   }
209 
210   Json::Reader reader;
211   Json::Value jscop;
212 
213   bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop);
214 
215   if (!parsingSuccessful) {
216     errs() << "JSCoP file could not be parsed\n";
217     return false;
218   }
219 
220   isl_set *OldContext = S.getContext();
221   isl_set *NewContext =
222       isl_set_read_from_str(S.getIslCtx(), jscop["context"].asCString());
223 
224   for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) {
225     isl_id *id = isl_set_get_dim_id(OldContext, isl_dim_param, i);
226     NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, id);
227   }
228 
229   isl_set_free(OldContext);
230   S.setContext(NewContext);
231 
232   StatementToIslMapTy NewSchedule;
233 
234   int index = 0;
235 
236   for (ScopStmt &Stmt : S) {
237     Json::Value schedule = jscop["statements"][index]["schedule"];
238     isl_map *m = isl_map_read_from_str(S.getIslCtx(), schedule.asCString());
239     isl_space *Space = Stmt.getDomainSpace();
240 
241     // Copy the old tuple id. This is necessary to retain the user pointer,
242     // that stores the reference to the ScopStmt this schedule belongs to.
243     m = isl_map_set_tuple_id(m, isl_dim_in,
244                              isl_space_get_tuple_id(Space, isl_dim_set));
245     for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
246       isl_id *id = isl_space_get_dim_id(Space, isl_dim_param, i);
247       m = isl_map_set_dim_id(m, isl_dim_param, i, id);
248     }
249     isl_space_free(Space);
250     NewSchedule[&Stmt] = m;
251     index++;
252   }
253 
254   if (!D.isValidSchedule(S, &NewSchedule)) {
255     errs() << "JScop file contains a schedule that changes the "
256            << "dependences. Use -disable-polly-legality to continue anyways\n";
257     for (StatementToIslMapTy::iterator SI = NewSchedule.begin(),
258                                        SE = NewSchedule.end();
259          SI != SE; ++SI)
260       isl_map_free(SI->second);
261     return false;
262   }
263 
264   auto ScheduleMap = isl_union_map_empty(S.getParamSpace());
265   for (ScopStmt &Stmt : S) {
266     if (NewSchedule.find(&Stmt) != NewSchedule.end())
267       ScheduleMap = isl_union_map_add_map(ScheduleMap, NewSchedule[&Stmt]);
268     else
269       ScheduleMap = isl_union_map_add_map(ScheduleMap, Stmt.getSchedule());
270   }
271 
272   S.setSchedule(ScheduleMap);
273 
274   int statementIdx = 0;
275   for (ScopStmt &Stmt : S) {
276     int memoryAccessIdx = 0;
277     for (MemoryAccess *MA : Stmt) {
278       Json::Value accesses = jscop["statements"][statementIdx]["accesses"]
279                                   [memoryAccessIdx]["relation"];
280       isl_map *newAccessMap =
281           isl_map_read_from_str(S.getIslCtx(), accesses.asCString());
282       isl_map *currentAccessMap = MA->getAccessRelation();
283 
284       if (isl_map_dim(newAccessMap, isl_dim_param) !=
285           isl_map_dim(currentAccessMap, isl_dim_param)) {
286         errs() << "JScop file changes the number of parameter dimensions\n";
287         isl_map_free(currentAccessMap);
288         isl_map_free(newAccessMap);
289         return false;
290       }
291 
292       isl_id *OutId = isl_map_get_tuple_id(currentAccessMap, isl_dim_out);
293       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_out, OutId);
294 
295       // We keep the old alignment, thus we cannot allow accesses to memory
296       // locations that were not accessed before if the alignment of the access
297       // is not the default alignment.
298       bool SpecialAlignment = true;
299       if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
300         SpecialAlignment =
301             DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
302       } else if (StoreInst *StoreI =
303                      dyn_cast<StoreInst>(MA->getAccessInstruction())) {
304         SpecialAlignment =
305             DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
306             StoreI->getAlignment();
307       }
308 
309       if (SpecialAlignment) {
310         isl_set *newAccessSet = isl_map_range(isl_map_copy(newAccessMap));
311         isl_set *currentAccessSet =
312             isl_map_range(isl_map_copy(currentAccessMap));
313         bool isSubset = isl_set_is_subset(newAccessSet, currentAccessSet);
314         isl_set_free(newAccessSet);
315         isl_set_free(currentAccessSet);
316 
317         if (!isSubset) {
318           errs() << "JScop file changes the accessed memory\n";
319           isl_map_free(currentAccessMap);
320           isl_map_free(newAccessMap);
321           return false;
322         }
323       }
324 
325       // We need to copy the isl_ids for the parameter dimensions to the new
326       // map. Without doing this the current map would have different
327       // ids then the new one, even though both are named identically.
328       for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param);
329            i++) {
330         isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i);
331         newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id);
332       }
333 
334       // Copy the old tuple id. This is necessary to retain the user pointer,
335       // that stores the reference to the ScopStmt this access belongs to.
336       isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in);
337       newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id);
338 
339       if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) {
340         errs() << "JScop file contains access function with incompatible "
341                << "dimensions\n";
342         isl_map_free(currentAccessMap);
343         isl_map_free(newAccessMap);
344         return false;
345       }
346 
347       auto NewAccessDomain = isl_map_domain(isl_map_copy(newAccessMap));
348       auto CurrentAccessDomain = isl_map_domain(isl_map_copy(currentAccessMap));
349 
350       NewAccessDomain =
351           isl_set_intersect_params(NewAccessDomain, S.getContext());
352       CurrentAccessDomain =
353           isl_set_intersect_params(CurrentAccessDomain, S.getContext());
354 
355       if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
356           isl_bool_false) {
357         errs() << "Mapping not defined for all iteration domain elements\n";
358         isl_set_free(CurrentAccessDomain);
359         isl_set_free(NewAccessDomain);
360         isl_map_free(currentAccessMap);
361         isl_map_free(newAccessMap);
362         return false;
363       }
364 
365       isl_set_free(CurrentAccessDomain);
366       isl_set_free(NewAccessDomain);
367 
368       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
369         // Statistics.
370         ++NewAccessMapFound;
371         newAccessStrings.push_back(accesses.asCString());
372         MA->setNewAccessRelation(newAccessMap);
373       } else {
374         isl_map_free(newAccessMap);
375       }
376       isl_map_free(currentAccessMap);
377       memoryAccessIdx++;
378     }
379     statementIdx++;
380   }
381 
382   return false;
383 }
384 
385 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
386   ScopPass::getAnalysisUsage(AU);
387   AU.addRequired<DependenceInfo>();
388 }
389 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
390 
391 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
392                       "Polly - Export Scops as JSON"
393                       " (Writes a .jscop file for each Scop)",
394                       false, false);
395 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
396 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
397                     "Polly - Export Scops as JSON"
398                     " (Writes a .jscop file for each Scop)",
399                     false, false)
400 
401 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
402                       "Polly - Import Scops from JSON"
403                       " (Reads a .jscop file for each Scop)",
404                       false, false);
405 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
406 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
407                     "Polly - Import Scops from JSON"
408                     " (Reads a .jscop file for each Scop)",
409                     false, false)
410