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