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