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/JSONExporter.h"
15 #include "polly/DependenceInfo.h"
16 #include "polly/LinkAllPasses.h"
17 #include "polly/Options.h"
18 #include "polly/ScopInfo.h"
19 #include "polly/ScopPass.h"
20 #include "polly/Support/ScopLocation.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/RegionInfo.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/ToolOutputFile.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "isl/constraint.h"
29 #include "isl/map.h"
30 #include "isl/printer.h"
31 #include "isl/set.h"
32 #include "isl/union_map.h"
33 #include "json/reader.h"
34 #include "json/writer.h"
35 #include <memory>
36 #include <string>
37 #include <system_error>
38 
39 using namespace llvm;
40 using namespace polly;
41 
42 #define DEBUG_TYPE "polly-import-jscop"
43 
44 STATISTIC(NewAccessMapFound, "Number of updated access functions");
45 
46 namespace {
47 static cl::opt<std::string>
48     ImportDir("polly-import-jscop-dir",
49               cl::desc("The directory to import the .jscop files from."),
50               cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired,
51               cl::init("."), cl::cat(PollyCategory));
52 
53 static cl::opt<std::string>
54     ImportPostfix("polly-import-jscop-postfix",
55                   cl::desc("Postfix to append to the import .jsop files."),
56                   cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired,
57                   cl::init(""), cl::cat(PollyCategory));
58 
59 struct JSONExporter : public ScopPass {
60   static char ID;
61   explicit JSONExporter() : ScopPass(ID) {}
62 
63   /// Export the SCoP @p S to a JSON file.
64   bool runOnScop(Scop &S) override;
65 
66   /// Print the SCoP @p S as it is exported.
67   void printScop(raw_ostream &OS, Scop &S) const override;
68 
69   /// Register all analyses and transformation required.
70   void getAnalysisUsage(AnalysisUsage &AU) const override;
71 };
72 
73 struct JSONImporter : public ScopPass {
74   static char ID;
75   std::vector<std::string> NewAccessStrings;
76   explicit JSONImporter() : ScopPass(ID) {}
77   /// Import new access functions for SCoP @p S from a JSON file.
78   bool runOnScop(Scop &S) override;
79 
80   /// Print the SCoP @p S and the imported access functions.
81   void printScop(raw_ostream &OS, Scop &S) const override;
82 
83   /// Register all analyses and transformation required.
84   void getAnalysisUsage(AnalysisUsage &AU) const override;
85 };
86 } // namespace
87 
88 static std::string getFileName(Scop &S, StringRef Suffix = "") {
89   std::string FunctionName = S.getFunction().getName();
90   std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
91 
92   if (Suffix != "")
93     FileName += "." + Suffix.str();
94 
95   return FileName;
96 }
97 
98 /// Export all arrays from the Scop.
99 ///
100 /// @param S The Scop containing the arrays.
101 ///
102 /// @returns Json::Value containing the arrays.
103 static Json::Value exportArrays(const Scop &S) {
104   Json::Value Arrays;
105   std::string Buffer;
106   llvm::raw_string_ostream RawStringOstream(Buffer);
107 
108   for (auto &SAI : S.arrays()) {
109     if (!SAI->isArrayKind())
110       continue;
111 
112     Json::Value Array;
113     Array["name"] = SAI->getName();
114     unsigned i = 0;
115     if (!SAI->getDimensionSize(i)) {
116       Array["sizes"].append("*");
117       i++;
118     }
119     for (; i < SAI->getNumberOfDimensions(); i++) {
120       SAI->getDimensionSize(i)->print(RawStringOstream);
121       Array["sizes"].append(RawStringOstream.str());
122       Buffer.clear();
123     }
124     SAI->getElementType()->print(RawStringOstream);
125     Array["type"] = RawStringOstream.str();
126     Buffer.clear();
127     Arrays.append(Array);
128   }
129   return Arrays;
130 }
131 
132 static Json::Value getJSON(Scop &S) {
133   Json::Value root;
134   unsigned LineBegin, LineEnd;
135   std::string FileName;
136 
137   getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName);
138   std::string Location;
139   if (LineBegin != (unsigned)-1)
140     Location = FileName + ":" + std::to_string(LineBegin) + "-" +
141                std::to_string(LineEnd);
142 
143   root["name"] = S.getNameStr();
144   root["context"] = S.getContextStr();
145   if (LineBegin != (unsigned)-1)
146     root["location"] = Location;
147 
148   root["arrays"] = exportArrays(S);
149 
150   root["statements"];
151 
152   for (ScopStmt &Stmt : S) {
153     Json::Value statement;
154 
155     statement["name"] = Stmt.getBaseName();
156     statement["domain"] = Stmt.getDomainStr();
157     statement["schedule"] = Stmt.getScheduleStr();
158     statement["accesses"];
159 
160     for (MemoryAccess *MA : Stmt) {
161       Json::Value access;
162 
163       access["kind"] = MA->isRead() ? "read" : "write";
164       access["relation"] = MA->getAccessRelationStr();
165 
166       statement["accesses"].append(access);
167     }
168 
169     root["statements"].append(statement);
170   }
171 
172   return root;
173 }
174 
175 static void exportScop(Scop &S) {
176   std::string FileName = ImportDir + "/" + getFileName(S);
177 
178   Json::Value jscop = getJSON(S);
179   Json::StyledWriter writer;
180   std::string fileContent = writer.write(jscop);
181 
182   // Write to file.
183   std::error_code EC;
184   ToolOutputFile F(FileName, EC, llvm::sys::fs::F_Text);
185 
186   std::string FunctionName = S.getFunction().getName();
187   errs() << "Writing JScop '" << S.getNameStr() << "' in function '"
188          << FunctionName << "' to '" << FileName << "'.\n";
189 
190   if (!EC) {
191     F.os() << fileContent;
192     F.os().close();
193     if (!F.os().has_error()) {
194       errs() << "\n";
195       F.keep();
196       return;
197     }
198   }
199 
200   errs() << "  error opening file for writing!\n";
201   F.os().clear_error();
202 }
203 
204 typedef Dependences::StatementToIslMapTy StatementToIslMapTy;
205 
206 /// Import a new context from JScop.
207 ///
208 /// @param S The scop to update.
209 /// @param JScop The JScop file describing the new schedule.
210 ///
211 /// @returns True if the import succeeded, otherwise False.
212 static bool importContext(Scop &S, Json::Value &JScop) {
213   isl::set OldContext = S.getContext();
214 
215   // Check if key 'context' is present.
216   if (!JScop.isMember("context")) {
217     errs() << "JScop file has no key named 'context'.\n";
218     return false;
219   }
220 
221   isl::set NewContext =
222       isl::set{S.getIslCtx().get(), JScop["context"].asString()};
223 
224   // Check whether the context was parsed successfully.
225   if (!NewContext) {
226     errs() << "The context was not parsed successfully by ISL.\n";
227     return false;
228   }
229 
230   // Check if the isl_set is a parameter set.
231   if (!NewContext.is_params()) {
232     errs() << "The isl_set is not a parameter set.\n";
233     return false;
234   }
235 
236   unsigned OldContextDim = OldContext.dim(isl::dim::param);
237   unsigned NewContextDim = NewContext.dim(isl::dim::param);
238 
239   // Check if the imported context has the right number of parameters.
240   if (OldContextDim != NewContextDim) {
241     errs() << "Imported context has the wrong number of parameters : "
242            << "Found " << NewContextDim << " Expected " << OldContextDim
243            << "\n";
244     return false;
245   }
246 
247   for (unsigned i = 0; i < OldContextDim; i++) {
248     isl::id Id = OldContext.get_dim_id(isl::dim::param, i);
249     NewContext = NewContext.set_dim_id(isl::dim::param, i, Id);
250   }
251 
252   S.setContext(NewContext);
253   return true;
254 }
255 
256 /// Import a new schedule from JScop.
257 ///
258 /// ... and verify that the new schedule does preserve existing data
259 /// dependences.
260 ///
261 /// @param S The scop to update.
262 /// @param JScop The JScop file describing the new schedule.
263 /// @param D The data dependences of the @p S.
264 ///
265 /// @returns True if the import succeeded, otherwise False.
266 static bool importSchedule(Scop &S, Json::Value &JScop, const Dependences &D) {
267   StatementToIslMapTy NewSchedule;
268 
269   // Check if key 'statements' is present.
270   if (!JScop.isMember("statements")) {
271     errs() << "JScop file has no key name 'statements'.\n";
272     return false;
273   }
274 
275   Json::Value statements = JScop["statements"];
276 
277   // Check whether the number of indices equals the number of statements
278   if (statements.size() != S.getSize()) {
279     errs() << "The number of indices and the number of statements differ.\n";
280     return false;
281   }
282 
283   int Index = 0;
284   for (ScopStmt &Stmt : S) {
285     // Check if key 'schedule' is present.
286     if (!statements[Index].isMember("schedule")) {
287       errs() << "Statement " << Index << " has no 'schedule' key.\n";
288       for (auto Element : NewSchedule) {
289         isl_map_free(Element.second);
290       }
291       return false;
292     }
293     Json::Value Schedule = statements[Index]["schedule"];
294     assert(!Schedule.asString().empty() &&
295            "Schedules that contain extension nodes require special handling.");
296     isl_map *Map =
297         isl_map_read_from_str(S.getIslCtx().get(), Schedule.asCString());
298 
299     // Check whether the schedule was parsed successfully
300     if (!Map) {
301       errs() << "The schedule was not parsed successfully (index = " << Index
302              << ").\n";
303       for (auto Element : NewSchedule) {
304         isl_map_free(Element.second);
305       }
306       return false;
307     }
308 
309     isl_space *Space = Stmt.getDomainSpace().release();
310 
311     // Copy the old tuple id. This is necessary to retain the user pointer,
312     // that stores the reference to the ScopStmt this schedule belongs to.
313     Map = isl_map_set_tuple_id(Map, isl_dim_in,
314                                isl_space_get_tuple_id(Space, isl_dim_set));
315     for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
316       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i);
317       Map = isl_map_set_dim_id(Map, isl_dim_param, i, Id);
318     }
319     isl_space_free(Space);
320     NewSchedule[&Stmt] = Map;
321     Index++;
322   }
323 
324   // Check whether the new schedule is valid or not.
325   if (!D.isValidSchedule(S, &NewSchedule)) {
326     errs() << "JScop file contains a schedule that changes the "
327            << "dependences. Use -disable-polly-legality to continue anyways\n";
328     for (auto Element : NewSchedule)
329       isl_map_free(Element.second);
330     return false;
331   }
332 
333   auto ScheduleMap = isl::union_map::empty(S.getParamSpace());
334   for (ScopStmt &Stmt : S) {
335     if (NewSchedule.find(&Stmt) != NewSchedule.end())
336       ScheduleMap = ScheduleMap.add_map(isl::manage(NewSchedule[&Stmt]));
337     else
338       ScheduleMap = ScheduleMap.add_map(Stmt.getSchedule());
339   }
340 
341   S.setSchedule(ScheduleMap);
342 
343   return true;
344 }
345 
346 /// Import new memory accesses from JScop.
347 ///
348 /// @param S The scop to update.
349 /// @param JScop The JScop file describing the new schedule.
350 /// @param DL The data layout to assume.
351 /// @param NewAccessStrings optionally record the imported access strings
352 ///
353 /// @returns True if the import succeeded, otherwise False.
354 static bool
355 importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL,
356                std::vector<std::string> *NewAccessStrings = nullptr) {
357   int StatementIdx = 0;
358 
359   // Check if key 'statements' is present.
360   if (!JScop.isMember("statements")) {
361     errs() << "JScop file has no key name 'statements'.\n";
362     return false;
363   }
364   Json::Value statements = JScop["statements"];
365 
366   // Check whether the number of indices equals the number of statements
367   if (statements.size() != S.getSize()) {
368     errs() << "The number of indices and the number of statements differ.\n";
369     return false;
370   }
371 
372   for (ScopStmt &Stmt : S) {
373     int MemoryAccessIdx = 0;
374 
375     // Check if key 'accesses' is present.
376     if (!statements[StatementIdx].isMember("accesses")) {
377       errs()
378           << "Statement from JScop file has no key name 'accesses' for index "
379           << StatementIdx << ".\n";
380       return false;
381     }
382 
383     // Check whether the number of indices equals the number of memory
384     // accesses
385     if (Stmt.size() != statements[StatementIdx]["accesses"].size()) {
386       errs() << "The number of memory accesses in the JSop file and the number "
387                 "of memory accesses differ for index "
388              << StatementIdx << ".\n";
389       return false;
390     }
391 
392     for (MemoryAccess *MA : Stmt) {
393       // Check if key 'relation' is present.
394       Json::Value JsonMemoryAccess =
395           statements[StatementIdx]["accesses"][MemoryAccessIdx];
396       if (!JsonMemoryAccess.isMember("relation")) {
397         errs() << "Memory access number " << MemoryAccessIdx
398                << " has no key name 'relation' for statement number "
399                << StatementIdx << ".\n";
400         return false;
401       }
402       Json::Value Accesses = JsonMemoryAccess["relation"];
403       isl_map *NewAccessMap =
404           isl_map_read_from_str(S.getIslCtx().get(), Accesses.asCString());
405 
406       // Check whether the access was parsed successfully
407       if (!NewAccessMap) {
408         errs() << "The access was not parsed successfully by ISL.\n";
409         return false;
410       }
411       isl_map *CurrentAccessMap = MA->getAccessRelation().release();
412 
413       // Check if the number of parameter change
414       if (isl_map_dim(NewAccessMap, isl_dim_param) !=
415           isl_map_dim(CurrentAccessMap, isl_dim_param)) {
416         errs() << "JScop file changes the number of parameter dimensions.\n";
417         isl_map_free(CurrentAccessMap);
418         isl_map_free(NewAccessMap);
419         return false;
420       }
421 
422       isl_id *NewOutId;
423 
424       // If the NewAccessMap has zero dimensions, it is the scalar access; it
425       // must be the same as before.
426       // If it has at least one dimension, it's an array access; search for
427       // its ScopArrayInfo.
428       if (isl_map_dim(NewAccessMap, isl_dim_out) >= 1) {
429         NewOutId = isl_map_get_tuple_id(NewAccessMap, isl_dim_out);
430         auto *SAI = S.getArrayInfoByName(isl_id_get_name(NewOutId));
431         isl_id *OutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
432         auto *OutSAI = ScopArrayInfo::getFromId(isl::manage(OutId));
433         if (!SAI || SAI->getElementType() != OutSAI->getElementType()) {
434           errs() << "JScop file contains access function with undeclared "
435                     "ScopArrayInfo\n";
436           isl_map_free(CurrentAccessMap);
437           isl_map_free(NewAccessMap);
438           isl_id_free(NewOutId);
439           return false;
440         }
441         isl_id_free(NewOutId);
442         NewOutId = SAI->getBasePtrId().release();
443       } else {
444         NewOutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
445       }
446 
447       NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_out, NewOutId);
448 
449       if (MA->isArrayKind()) {
450         // We keep the old alignment, thus we cannot allow accesses to memory
451         // locations that were not accessed before if the alignment of the
452         // access is not the default alignment.
453         bool SpecialAlignment = true;
454         if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
455           SpecialAlignment =
456               LoadI->getAlignment() &&
457               DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
458         } else if (StoreInst *StoreI =
459                        dyn_cast<StoreInst>(MA->getAccessInstruction())) {
460           SpecialAlignment =
461               StoreI->getAlignment() &&
462               DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
463                   StoreI->getAlignment();
464         }
465 
466         if (SpecialAlignment) {
467           isl_set *NewAccessSet = isl_map_range(isl_map_copy(NewAccessMap));
468           isl_set *CurrentAccessSet =
469               isl_map_range(isl_map_copy(CurrentAccessMap));
470           bool IsSubset = isl_set_is_subset(NewAccessSet, CurrentAccessSet);
471           isl_set_free(NewAccessSet);
472           isl_set_free(CurrentAccessSet);
473 
474           // Check if the JScop file changes the accessed memory.
475           if (!IsSubset) {
476             errs() << "JScop file changes the accessed memory\n";
477             isl_map_free(CurrentAccessMap);
478             isl_map_free(NewAccessMap);
479             return false;
480           }
481         }
482       }
483 
484       // We need to copy the isl_ids for the parameter dimensions to the new
485       // map. Without doing this the current map would have different
486       // ids then the new one, even though both are named identically.
487       for (unsigned i = 0; i < isl_map_dim(CurrentAccessMap, isl_dim_param);
488            i++) {
489         isl_id *Id = isl_map_get_dim_id(CurrentAccessMap, isl_dim_param, i);
490         NewAccessMap = isl_map_set_dim_id(NewAccessMap, isl_dim_param, i, Id);
491       }
492 
493       // Copy the old tuple id. This is necessary to retain the user pointer,
494       // that stores the reference to the ScopStmt this access belongs to.
495       isl_id *Id = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_in);
496       NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_in, Id);
497 
498       auto NewAccessDomain = isl_map_domain(isl_map_copy(NewAccessMap));
499       auto CurrentAccessDomain = isl_map_domain(isl_map_copy(CurrentAccessMap));
500 
501       if (!isl_set_has_equal_space(NewAccessDomain, CurrentAccessDomain)) {
502         errs() << "JScop file contains access function with incompatible "
503                << "dimensions\n";
504         isl_map_free(CurrentAccessMap);
505         isl_map_free(NewAccessMap);
506         isl_set_free(NewAccessDomain);
507         isl_set_free(CurrentAccessDomain);
508         return false;
509       }
510 
511       NewAccessDomain =
512           isl_set_intersect_params(NewAccessDomain, S.getContext().release());
513       CurrentAccessDomain = isl_set_intersect_params(CurrentAccessDomain,
514                                                      S.getContext().release());
515       CurrentAccessDomain =
516           isl_set_intersect(CurrentAccessDomain, Stmt.getDomain().release());
517 
518       if (MA->isRead() &&
519           isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
520               isl_bool_false) {
521         errs() << "Mapping not defined for all iteration domain elements\n";
522         isl_set_free(CurrentAccessDomain);
523         isl_set_free(NewAccessDomain);
524         isl_map_free(CurrentAccessMap);
525         isl_map_free(NewAccessMap);
526         return false;
527       }
528 
529       isl_set_free(CurrentAccessDomain);
530       isl_set_free(NewAccessDomain);
531 
532       if (!isl_map_is_equal(NewAccessMap, CurrentAccessMap)) {
533         // Statistics.
534         ++NewAccessMapFound;
535         if (NewAccessStrings)
536           NewAccessStrings->push_back(Accesses.asCString());
537         MA->setNewAccessRelation(isl::manage(NewAccessMap));
538       } else {
539         isl_map_free(NewAccessMap);
540       }
541       isl_map_free(CurrentAccessMap);
542       MemoryAccessIdx++;
543     }
544     StatementIdx++;
545   }
546 
547   return true;
548 }
549 
550 /// Check whether @p SAI and @p Array represent the same array.
551 static bool areArraysEqual(ScopArrayInfo *SAI, Json::Value Array) {
552   std::string Buffer;
553   llvm::raw_string_ostream RawStringOstream(Buffer);
554 
555   // Check if key 'type' is present.
556   if (!Array.isMember("type")) {
557     errs() << "Array has no key 'type'.\n";
558     return false;
559   }
560 
561   // Check if key 'sizes' is present.
562   if (!Array.isMember("sizes")) {
563     errs() << "Array has no key 'sizes'.\n";
564     return false;
565   }
566 
567   // Check if key 'name' is present.
568   if (!Array.isMember("name")) {
569     errs() << "Array has no key 'name'.\n";
570     return false;
571   }
572 
573   if (SAI->getName() != Array["name"].asCString())
574     return false;
575 
576   if (SAI->getNumberOfDimensions() != Array["sizes"].size())
577     return false;
578 
579   for (unsigned i = 1; i < Array["sizes"].size(); i++) {
580     SAI->getDimensionSize(i)->print(RawStringOstream);
581     if (RawStringOstream.str() != Array["sizes"][i].asCString())
582       return false;
583     Buffer.clear();
584   }
585 
586   // Check if key 'type' differs from the current one or is not valid.
587   SAI->getElementType()->print(RawStringOstream);
588   if (RawStringOstream.str() != Array["type"].asCString()) {
589     errs() << "Array has not a valid type.\n";
590     return false;
591   }
592 
593   return true;
594 }
595 
596 /// Get the accepted primitive type from its textual representation
597 ///        @p TypeTextRepresentation.
598 ///
599 /// @param TypeTextRepresentation The textual representation of the type.
600 /// @return The pointer to the primitive type, if this type is accepted
601 ///         or nullptr otherwise.
602 static Type *parseTextType(const std::string &TypeTextRepresentation,
603                            LLVMContext &LLVMContext) {
604   std::map<std::string, Type *> MapStrToType = {
605       {"void", Type::getVoidTy(LLVMContext)},
606       {"half", Type::getHalfTy(LLVMContext)},
607       {"float", Type::getFloatTy(LLVMContext)},
608       {"double", Type::getDoubleTy(LLVMContext)},
609       {"x86_fp80", Type::getX86_FP80Ty(LLVMContext)},
610       {"fp128", Type::getFP128Ty(LLVMContext)},
611       {"ppc_fp128", Type::getPPC_FP128Ty(LLVMContext)},
612       {"i1", Type::getInt1Ty(LLVMContext)},
613       {"i8", Type::getInt8Ty(LLVMContext)},
614       {"i16", Type::getInt16Ty(LLVMContext)},
615       {"i32", Type::getInt32Ty(LLVMContext)},
616       {"i64", Type::getInt64Ty(LLVMContext)},
617       {"i128", Type::getInt128Ty(LLVMContext)}};
618 
619   auto It = MapStrToType.find(TypeTextRepresentation);
620   if (It != MapStrToType.end())
621     return It->second;
622 
623   errs() << "Textual representation can not be parsed: "
624          << TypeTextRepresentation << "\n";
625   return nullptr;
626 }
627 
628 /// Import new arrays from JScop.
629 ///
630 /// @param S The scop to update.
631 /// @param JScop The JScop file describing new arrays.
632 ///
633 /// @returns True if the import succeeded, otherwise False.
634 static bool importArrays(Scop &S, Json::Value &JScop) {
635   Json::Value Arrays = JScop["arrays"];
636   if (Arrays.size() == 0)
637     return true;
638 
639   unsigned ArrayIdx = 0;
640   for (auto &SAI : S.arrays()) {
641     if (!SAI->isArrayKind())
642       continue;
643     if (ArrayIdx + 1 > Arrays.size()) {
644       errs() << "Not enough array entries in JScop file.\n";
645       return false;
646     }
647     if (!areArraysEqual(SAI, Arrays[ArrayIdx])) {
648       errs() << "No match for array '" << SAI->getName() << "' in JScop.\n";
649       return false;
650     }
651     ArrayIdx++;
652   }
653 
654   for (; ArrayIdx < Arrays.size(); ArrayIdx++) {
655     auto &Array = Arrays[ArrayIdx];
656     auto *ElementType =
657         parseTextType(Array["type"].asCString(), S.getSE()->getContext());
658     if (!ElementType) {
659       errs() << "Error while parsing element type for new array.\n";
660       return false;
661     }
662     std::vector<unsigned> DimSizes;
663     for (unsigned i = 0; i < Array["sizes"].size(); i++) {
664       auto Size = std::stoi(Array["sizes"][i].asCString());
665 
666       // Check if the size if positive.
667       if (Size <= 0) {
668         errs() << "The size at index " << i << " is =< 0.\n";
669         return false;
670       }
671 
672       DimSizes.push_back(Size);
673     }
674 
675     auto NewSAI =
676         S.createScopArrayInfo(ElementType, Array["name"].asCString(), DimSizes);
677 
678     if (Array.isMember("allocation")) {
679       NewSAI->setIsOnHeap(Array["allocation"].asString() == "heap");
680     }
681   }
682 
683   return true;
684 }
685 
686 /// Import a Scop from a JSCOP file
687 /// @param S The scop to be modified
688 /// @param D Dependence Info
689 /// @param DL The DataLayout of the function
690 /// @param NewAccessStrings Optionally record the imported access strings
691 ///
692 /// @returns true on success, false otherwise. Beware that if this returns
693 /// false, the Scop may still have been modified. In this case the Scop contains
694 /// invalid information.
695 static bool importScop(Scop &S, const Dependences &D, const DataLayout &DL,
696                        std::vector<std::string> *NewAccessStrings = nullptr) {
697   std::string FileName = ImportDir + "/" + getFileName(S, ImportPostfix);
698 
699   std::string FunctionName = S.getFunction().getName();
700   errs() << "Reading JScop '" << S.getNameStr() << "' in function '"
701          << FunctionName << "' from '" << FileName << "'.\n";
702   ErrorOr<std::unique_ptr<MemoryBuffer>> result =
703       MemoryBuffer::getFile(FileName);
704   std::error_code ec = result.getError();
705 
706   if (ec) {
707     errs() << "File could not be read: " << ec.message() << "\n";
708     return false;
709   }
710 
711   Json::Reader reader;
712   Json::Value jscop;
713 
714   bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop);
715 
716   if (!parsingSuccessful) {
717     errs() << "JSCoP file could not be parsed\n";
718     return false;
719   }
720 
721   bool Success = importContext(S, jscop);
722 
723   if (!Success)
724     return false;
725 
726   Success = importSchedule(S, jscop, D);
727 
728   if (!Success)
729     return false;
730 
731   Success = importArrays(S, jscop);
732 
733   if (!Success)
734     return false;
735 
736   Success = importAccesses(S, jscop, DL, NewAccessStrings);
737 
738   if (!Success)
739     return false;
740   return true;
741 }
742 
743 char JSONExporter::ID = 0;
744 void JSONExporter::printScop(raw_ostream &OS, Scop &S) const { OS << S; }
745 
746 bool JSONExporter::runOnScop(Scop &S) {
747   exportScop(S);
748   return false;
749 }
750 
751 void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const {
752   AU.setPreservesAll();
753   AU.addRequired<ScopInfoRegionPass>();
754 }
755 
756 Pass *polly::createJSONExporterPass() { return new JSONExporter(); }
757 
758 PreservedAnalyses JSONExportPass::run(Scop &S, ScopAnalysisManager &SAM,
759                                       ScopStandardAnalysisResults &SAR,
760                                       SPMUpdater &) {
761   exportScop(S);
762   return PreservedAnalyses::all();
763 }
764 
765 char JSONImporter::ID = 0;
766 
767 void JSONImporter::printScop(raw_ostream &OS, Scop &S) const {
768   OS << S;
769   for (std::vector<std::string>::const_iterator I = NewAccessStrings.begin(),
770                                                 E = NewAccessStrings.end();
771        I != E; I++)
772     OS << "New access function '" << *I << "' detected in JSCOP file\n";
773 }
774 
775 bool JSONImporter::runOnScop(Scop &S) {
776   const Dependences &D =
777       getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
778   const DataLayout &DL = S.getFunction().getParent()->getDataLayout();
779 
780   if (!importScop(S, D, DL, &NewAccessStrings))
781     report_fatal_error("Tried to import a malformed jscop file.");
782 
783   return false;
784 }
785 
786 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
787   ScopPass::getAnalysisUsage(AU);
788   AU.addRequired<DependenceInfo>();
789 
790   // TODO: JSONImporter should throw away DependenceInfo.
791   AU.addPreserved<DependenceInfo>();
792 }
793 
794 Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
795 
796 PreservedAnalyses JSONImportPass::run(Scop &S, ScopAnalysisManager &SAM,
797                                       ScopStandardAnalysisResults &SAR,
798                                       SPMUpdater &) {
799   const Dependences &D =
800       SAM.getResult<DependenceAnalysis>(S, SAR).getDependences(
801           Dependences::AL_Statement);
802   const DataLayout &DL = S.getFunction().getParent()->getDataLayout();
803 
804   if (!importScop(S, D, DL))
805     report_fatal_error("Tried to import a malformed jscop file.");
806 
807   // This invalidates all analyses on Scop.
808   PreservedAnalyses PA;
809   PA.preserveSet<AllAnalysesOn<Module>>();
810   PA.preserveSet<AllAnalysesOn<Function>>();
811   PA.preserveSet<AllAnalysesOn<Loop>>();
812   return PA;
813 }
814 
815 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop",
816                       "Polly - Export Scops as JSON"
817                       " (Writes a .jscop file for each Scop)",
818                       false, false);
819 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
820 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
821                     "Polly - Export Scops as JSON"
822                     " (Writes a .jscop file for each Scop)",
823                     false, false)
824 
825 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop",
826                       "Polly - Import Scops from JSON"
827                       " (Reads a .jscop file for each Scop)",
828                       false, false);
829 INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
830 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
831                     "Polly - Import Scops from JSON"
832                     " (Reads a .jscop file for each Scop)",
833                     false, false)
834