Polly 22.0.0git
JSONExporter.cpp
Go to the documentation of this file.
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"
15#include "polly/Options.h"
16#include "polly/ScopInfo.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/JSON.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/ToolOutputFile.h"
25#include "llvm/Support/raw_ostream.h"
26#include "isl/map.h"
27#include "isl/set.h"
28#include <memory>
29#include <string>
30#include <system_error>
31
32using namespace llvm;
33using namespace polly;
34
35#define DEBUG_TYPE "polly-import-jscop"
36
37static cl::opt<bool>
38 PollyPrintImportJscop("polly-print-import-jscop",
39 cl::desc("Polly - Print Scop import result"),
40 cl::cat(PollyCategory));
41
42STATISTIC(NewAccessMapFound, "Number of updated access functions");
43
44namespace {
45static 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
51static 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} // namespace
57
58static std::string getFileName(Scop &S, StringRef Suffix = "") {
59 std::string FunctionName = S.getFunction().getName().str();
60 std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
61
62 if (Suffix != "")
63 FileName += "." + Suffix.str();
64
65 return FileName;
66}
67
68/// Export all arrays from the Scop.
69///
70/// @param S The Scop containing the arrays.
71///
72/// @returns Json::Value containing the arrays.
73static json::Array exportArrays(const Scop &S) {
74 json::Array Arrays;
75 std::string Buffer;
76 llvm::raw_string_ostream RawStringOstream(Buffer);
77
78 for (auto &SAI : S.arrays()) {
79 if (!SAI->isArrayKind())
80 continue;
81
82 json::Object Array;
83 json::Array Sizes;
84 Array["name"] = SAI->getName();
85 unsigned i = 0;
86 if (!SAI->getDimensionSize(i)) {
87 Sizes.push_back("*");
88 i++;
89 }
90 for (; i < SAI->getNumberOfDimensions(); i++) {
91 SAI->getDimensionSize(i)->print(RawStringOstream);
92 Sizes.push_back(Buffer);
93 Buffer.clear();
94 }
95 Array["sizes"] = std::move(Sizes);
96 SAI->getElementType()->print(RawStringOstream);
97 Array["type"] = Buffer;
98 Buffer.clear();
99 Arrays.push_back(std::move(Array));
100 }
101 return Arrays;
102}
103
104static json::Value getJSON(Scop &S) {
105 json::Object root;
106 unsigned LineBegin, LineEnd;
107 std::string FileName;
108
109 getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName);
110 std::string Location;
111 if (LineBegin != (unsigned)-1)
112 Location = FileName + ":" + std::to_string(LineBegin) + "-" +
113 std::to_string(LineEnd);
114
115 root["name"] = S.getNameStr();
116 root["context"] = S.getContextStr();
117 if (LineBegin != (unsigned)-1)
118 root["location"] = Location;
119
120 root["arrays"] = exportArrays(S);
121
122 root["statements"];
123
124 json::Array Statements;
125 for (ScopStmt &Stmt : S) {
126 json::Object statement;
127
128 statement["name"] = Stmt.getBaseName();
129 statement["domain"] = Stmt.getDomainStr();
130 statement["schedule"] = Stmt.getScheduleStr();
131
132 json::Array Accesses;
133 for (MemoryAccess *MA : Stmt) {
134 json::Object access;
135
136 access["kind"] = MA->isRead() ? "read" : "write";
137 access["relation"] = MA->getAccessRelationStr();
138
139 Accesses.push_back(std::move(access));
140 }
141 statement["accesses"] = std::move(Accesses);
142
143 Statements.push_back(std::move(statement));
144 }
145
146 root["statements"] = std::move(Statements);
147 return json::Value(std::move(root));
148}
149
150static void exportScop(Scop &S) {
151 std::string FileName = ImportDir + "/" + getFileName(S);
152
153 json::Value jscop = getJSON(S);
154
155 // Write to file.
156 std::error_code EC;
157 ToolOutputFile F(FileName, EC, llvm::sys::fs::OF_TextWithCRLF);
158
159 std::string FunctionName = S.getFunction().getName().str();
160 errs() << "Writing JScop '" << S.getNameStr() << "' in function '"
161 << FunctionName << "' to '" << FileName << "'.\n";
162
163 if (!EC) {
164 F.os() << formatv("{0:3}", jscop);
165 F.os().close();
166 if (!F.os().has_error()) {
167 errs() << "\n";
168 F.keep();
169 return;
170 }
171 }
172
173 errs() << " error opening file for writing!\n";
174 F.os().clear_error();
175}
176
178
179/// Import a new context from JScop.
180///
181/// @param S The scop to update.
182/// @param JScop The JScop file describing the new schedule.
183///
184/// @returns True if the import succeeded, otherwise False.
185static bool importContext(Scop &S, const json::Object &JScop) {
186 isl::set OldContext = S.getContext();
187
188 // Check if key 'context' is present.
189 if (!JScop.get("context")) {
190 errs() << "JScop file has no key named 'context'.\n";
191 return false;
192 }
193
194 isl::set NewContext =
195 isl::set{S.getIslCtx().get(), JScop.getString("context").value().str()};
196
197 // Check whether the context was parsed successfully.
198 if (NewContext.is_null()) {
199 errs() << "The context was not parsed successfully by ISL.\n";
200 return false;
201 }
202
203 // Check if the isl_set is a parameter set.
204 if (!NewContext.is_params()) {
205 errs() << "The isl_set is not a parameter set.\n";
206 return false;
207 }
208
209 unsigned OldContextDim = unsignedFromIslSize(OldContext.dim(isl::dim::param));
210 unsigned NewContextDim = unsignedFromIslSize(NewContext.dim(isl::dim::param));
211
212 // Check if the imported context has the right number of parameters.
213 if (OldContextDim != NewContextDim) {
214 errs() << "Imported context has the wrong number of parameters : "
215 << "Found " << NewContextDim << " Expected " << OldContextDim
216 << "\n";
217 return false;
218 }
219
220 for (unsigned i = 0; i < OldContextDim; i++) {
221 isl::id Id = OldContext.get_dim_id(isl::dim::param, i);
222 NewContext = NewContext.set_dim_id(isl::dim::param, i, Id);
223 }
224
225 S.setContext(NewContext);
226 return true;
227}
228
229/// Import a new schedule from JScop.
230///
231/// ... and verify that the new schedule does preserve existing data
232/// dependences.
233///
234/// @param S The scop to update.
235/// @param JScop The JScop file describing the new schedule.
236/// @param D The data dependences of the @p S.
237///
238/// @returns True if the import succeeded, otherwise False.
239static bool importSchedule(Scop &S, const json::Object &JScop,
240 const Dependences &D) {
241 StatementToIslMapTy NewSchedule;
242
243 // Check if key 'statements' is present.
244 if (!JScop.get("statements")) {
245 errs() << "JScop file has no key name 'statements'.\n";
246 return false;
247 }
248
249 const json::Array &statements = *JScop.getArray("statements");
250
251 // Check whether the number of indices equals the number of statements
252 if (statements.size() != S.getSize()) {
253 errs() << "The number of indices and the number of statements differ.\n";
254 return false;
255 }
256
257 int Index = 0;
258 for (ScopStmt &Stmt : S) {
259 // Check if key 'schedule' is present.
260 if (!statements[Index].getAsObject()->get("schedule")) {
261 errs() << "Statement " << Index << " has no 'schedule' key.\n";
262 return false;
263 }
264 std::optional<StringRef> Schedule =
265 statements[Index].getAsObject()->getString("schedule");
266 assert(Schedule.has_value() &&
267 "Schedules that contain extension nodes require special handling.");
268 isl_map *Map = isl_map_read_from_str(S.getIslCtx().get(),
269 Schedule.value().str().c_str());
270
271 // Check whether the schedule was parsed successfully
272 if (!Map) {
273 errs() << "The schedule was not parsed successfully (index = " << Index
274 << ").\n";
275 return false;
276 }
277
278 isl_space *Space = Stmt.getDomainSpace().release();
279
280 // Copy the old tuple id. This is necessary to retain the user pointer,
281 // that stores the reference to the ScopStmt this schedule belongs to.
284 for (isl_size i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
286 Map = isl_map_set_dim_id(Map, isl_dim_param, i, Id);
287 }
288 isl_space_free(Space);
289 NewSchedule[&Stmt] = isl::manage(Map);
290 Index++;
291 }
292
293 // Check whether the new schedule is valid or not.
294 if (!D.isValidSchedule(S, NewSchedule)) {
295 errs() << "JScop file contains a schedule that changes the "
296 << "dependences. Use -disable-polly-legality to continue anyways\n";
297 return false;
298 }
299
300 auto ScheduleMap = isl::union_map::empty(S.getIslCtx());
301 for (ScopStmt &Stmt : S) {
302 if (NewSchedule.contains(&Stmt))
303 ScheduleMap = ScheduleMap.unite(NewSchedule[&Stmt]);
304 else
305 ScheduleMap = ScheduleMap.unite(Stmt.getSchedule());
306 }
307
308 S.setSchedule(ScheduleMap);
309
310 return true;
311}
312
313/// Import new memory accesses from JScop.
314///
315/// @param S The scop to update.
316/// @param JScop The JScop file describing the new schedule.
317/// @param DL The data layout to assume.
318/// @param NewAccessStrings optionally record the imported access strings
319///
320/// @returns True if the import succeeded, otherwise False.
321static bool
322importAccesses(Scop &S, const json::Object &JScop, const DataLayout &DL,
323 std::vector<std::string> *NewAccessStrings = nullptr) {
324 int StatementIdx = 0;
325
326 // Check if key 'statements' is present.
327 if (!JScop.get("statements")) {
328 errs() << "JScop file has no key name 'statements'.\n";
329 return false;
330 }
331 const json::Array &statements = *JScop.getArray("statements");
332
333 // Check whether the number of indices equals the number of statements
334 if (statements.size() != S.getSize()) {
335 errs() << "The number of indices and the number of statements differ.\n";
336 return false;
337 }
338
339 for (ScopStmt &Stmt : S) {
340 int MemoryAccessIdx = 0;
341 const json::Object *Statement = statements[StatementIdx].getAsObject();
342 assert(Statement);
343
344 // Check if key 'accesses' is present.
345 if (!Statement->get("accesses")) {
346 errs()
347 << "Statement from JScop file has no key name 'accesses' for index "
348 << StatementIdx << ".\n";
349 return false;
350 }
351 const json::Array &JsonAccesses = *Statement->getArray("accesses");
352
353 // Check whether the number of indices equals the number of memory
354 // accesses
355 if (Stmt.size() != JsonAccesses.size()) {
356 errs() << "The number of memory accesses in the JSop file and the number "
357 "of memory accesses differ for index "
358 << StatementIdx << ".\n";
359 return false;
360 }
361
362 for (MemoryAccess *MA : Stmt) {
363 // Check if key 'relation' is present.
364 const json::Object *JsonMemoryAccess =
365 JsonAccesses[MemoryAccessIdx].getAsObject();
366 assert(JsonMemoryAccess);
367 if (!JsonMemoryAccess->get("relation")) {
368 errs() << "Memory access number " << MemoryAccessIdx
369 << " has no key name 'relation' for statement number "
370 << StatementIdx << ".\n";
371 return false;
372 }
373 StringRef Accesses = *JsonMemoryAccess->getString("relation");
374 isl_map *NewAccessMap =
375 isl_map_read_from_str(S.getIslCtx().get(), Accesses.str().c_str());
376
377 // Check whether the access was parsed successfully
378 if (!NewAccessMap) {
379 errs() << "The access was not parsed successfully by ISL.\n";
380 return false;
381 }
382 isl_map *CurrentAccessMap = MA->getAccessRelation().release();
383
384 // Check if the number of parameter change
385 if (isl_map_dim(NewAccessMap, isl_dim_param) !=
386 isl_map_dim(CurrentAccessMap, isl_dim_param)) {
387 errs() << "JScop file changes the number of parameter dimensions.\n";
388 isl_map_free(CurrentAccessMap);
389 isl_map_free(NewAccessMap);
390 return false;
391 }
392
393 isl_id *NewOutId;
394
395 // If the NewAccessMap has zero dimensions, it is the scalar access; it
396 // must be the same as before.
397 // If it has at least one dimension, it's an array access; search for
398 // its ScopArrayInfo.
399 if (isl_map_dim(NewAccessMap, isl_dim_out) >= 1) {
400 NewOutId = isl_map_get_tuple_id(NewAccessMap, isl_dim_out);
401 auto *SAI = S.getArrayInfoByName(isl_id_get_name(NewOutId));
402 isl_id *OutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
403 auto *OutSAI = ScopArrayInfo::getFromId(isl::manage(OutId));
404 if (!SAI || SAI->getElementType() != OutSAI->getElementType()) {
405 errs() << "JScop file contains access function with undeclared "
406 "ScopArrayInfo\n";
407 isl_map_free(CurrentAccessMap);
408 isl_map_free(NewAccessMap);
409 isl_id_free(NewOutId);
410 return false;
411 }
412 isl_id_free(NewOutId);
413 NewOutId = SAI->getBasePtrId().release();
414 } else {
415 NewOutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
416 }
417
418 NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_out, NewOutId);
419
420 if (MA->isArrayKind()) {
421 // We keep the old alignment, thus we cannot allow accesses to memory
422 // locations that were not accessed before if the alignment of the
423 // access is not the default alignment.
424 bool SpecialAlignment = true;
425 if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) {
426 SpecialAlignment =
427 DL.getABITypeAlign(LoadI->getType()) != LoadI->getAlign();
428 } else if (StoreInst *StoreI =
429 dyn_cast<StoreInst>(MA->getAccessInstruction())) {
430 SpecialAlignment =
431 DL.getABITypeAlign(StoreI->getValueOperand()->getType()) !=
432 StoreI->getAlign();
433 }
434
435 if (SpecialAlignment) {
436 isl_set *NewAccessSet = isl_map_range(isl_map_copy(NewAccessMap));
437 isl_set *CurrentAccessSet =
438 isl_map_range(isl_map_copy(CurrentAccessMap));
439 bool IsSubset = isl_set_is_subset(NewAccessSet, CurrentAccessSet);
440 isl_set_free(NewAccessSet);
441 isl_set_free(CurrentAccessSet);
442
443 // Check if the JScop file changes the accessed memory.
444 if (!IsSubset) {
445 errs() << "JScop file changes the accessed memory\n";
446 isl_map_free(CurrentAccessMap);
447 isl_map_free(NewAccessMap);
448 return false;
449 }
450 }
451 }
452
453 // We need to copy the isl_ids for the parameter dimensions to the new
454 // map. Without doing this the current map would have different
455 // ids then the new one, even though both are named identically.
456 for (isl_size i = 0; i < isl_map_dim(CurrentAccessMap, isl_dim_param);
457 i++) {
458 isl_id *Id = isl_map_get_dim_id(CurrentAccessMap, isl_dim_param, i);
459 NewAccessMap = isl_map_set_dim_id(NewAccessMap, isl_dim_param, i, Id);
460 }
461
462 // Copy the old tuple id. This is necessary to retain the user pointer,
463 // that stores the reference to the ScopStmt this access belongs to.
464 isl_id *Id = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_in);
465 NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_in, Id);
466
467 auto NewAccessDomain = isl_map_domain(isl_map_copy(NewAccessMap));
468 auto CurrentAccessDomain = isl_map_domain(isl_map_copy(CurrentAccessMap));
469
470 if (!isl_set_has_equal_space(NewAccessDomain, CurrentAccessDomain)) {
471 errs() << "JScop file contains access function with incompatible "
472 << "dimensions\n";
473 isl_map_free(CurrentAccessMap);
474 isl_map_free(NewAccessMap);
475 isl_set_free(NewAccessDomain);
476 isl_set_free(CurrentAccessDomain);
477 return false;
478 }
479
480 NewAccessDomain =
481 isl_set_intersect_params(NewAccessDomain, S.getContext().release());
482 CurrentAccessDomain = isl_set_intersect_params(CurrentAccessDomain,
483 S.getContext().release());
484 CurrentAccessDomain =
485 isl_set_intersect(CurrentAccessDomain, Stmt.getDomain().release());
486
487 if (MA->isRead() &&
488 isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
490 errs() << "Mapping not defined for all iteration domain elements\n";
491 isl_set_free(CurrentAccessDomain);
492 isl_set_free(NewAccessDomain);
493 isl_map_free(CurrentAccessMap);
494 isl_map_free(NewAccessMap);
495 return false;
496 }
497
498 isl_set_free(CurrentAccessDomain);
499 isl_set_free(NewAccessDomain);
500
501 if (!isl_map_is_equal(NewAccessMap, CurrentAccessMap)) {
502 // Statistics.
503 ++NewAccessMapFound;
504 if (NewAccessStrings)
505 NewAccessStrings->push_back(Accesses.str());
506 MA->setNewAccessRelation(isl::manage(NewAccessMap));
507 } else {
508 isl_map_free(NewAccessMap);
509 }
510 isl_map_free(CurrentAccessMap);
511 MemoryAccessIdx++;
512 }
513 StatementIdx++;
514 }
515
516 return true;
517}
518
519/// Check whether @p SAI and @p Array represent the same array.
520static bool areArraysEqual(ScopArrayInfo *SAI, const json::Object &Array) {
521 std::string Buffer;
522 llvm::raw_string_ostream RawStringOstream(Buffer);
523
524 // Check if key 'type' is present.
525 if (!Array.get("type")) {
526 errs() << "Array has no key 'type'.\n";
527 return false;
528 }
529
530 // Check if key 'sizes' is present.
531 if (!Array.get("sizes")) {
532 errs() << "Array has no key 'sizes'.\n";
533 return false;
534 }
535
536 // Check if key 'name' is present.
537 if (!Array.get("name")) {
538 errs() << "Array has no key 'name'.\n";
539 return false;
540 }
541
542 if (SAI->getName() != *Array.getString("name"))
543 return false;
544
545 if (SAI->getNumberOfDimensions() != Array.getArray("sizes")->size())
546 return false;
547
548 for (unsigned i = 1; i < Array.getArray("sizes")->size(); i++) {
549 SAI->getDimensionSize(i)->print(RawStringOstream);
550 const json::Array &SizesArray = *Array.getArray("sizes");
551 if (Buffer != SizesArray[i].getAsString().value())
552 return false;
553 Buffer.clear();
554 }
555
556 // Check if key 'type' differs from the current one or is not valid.
557 SAI->getElementType()->print(RawStringOstream);
558 if (Buffer != Array.getString("type").value()) {
559 errs() << "Array has not a valid type.\n";
560 return false;
561 }
562
563 return true;
564}
565
566/// Get the accepted primitive type from its textual representation
567/// @p TypeTextRepresentation.
568///
569/// @param TypeTextRepresentation The textual representation of the type.
570/// @return The pointer to the primitive type, if this type is accepted
571/// or nullptr otherwise.
572static Type *parseTextType(const std::string &TypeTextRepresentation,
573 LLVMContext &LLVMContext) {
574 std::map<std::string, Type *> MapStrToType = {
575 {"void", Type::getVoidTy(LLVMContext)},
576 {"half", Type::getHalfTy(LLVMContext)},
577 {"float", Type::getFloatTy(LLVMContext)},
578 {"double", Type::getDoubleTy(LLVMContext)},
579 {"x86_fp80", Type::getX86_FP80Ty(LLVMContext)},
580 {"fp128", Type::getFP128Ty(LLVMContext)},
581 {"ppc_fp128", Type::getPPC_FP128Ty(LLVMContext)},
582 {"i1", Type::getInt1Ty(LLVMContext)},
583 {"i8", Type::getInt8Ty(LLVMContext)},
584 {"i16", Type::getInt16Ty(LLVMContext)},
585 {"i32", Type::getInt32Ty(LLVMContext)},
586 {"i64", Type::getInt64Ty(LLVMContext)},
587 {"i128", Type::getInt128Ty(LLVMContext)}};
588
589 auto It = MapStrToType.find(TypeTextRepresentation);
590 if (It != MapStrToType.end())
591 return It->second;
592
593 errs() << "Textual representation can not be parsed: "
594 << TypeTextRepresentation << "\n";
595 return nullptr;
596}
597
598/// Import new arrays from JScop.
599///
600/// @param S The scop to update.
601/// @param JScop The JScop file describing new arrays.
602///
603/// @returns True if the import succeeded, otherwise False.
604static bool importArrays(Scop &S, const json::Object &JScop) {
605 if (!JScop.get("arrays"))
606 return true;
607 const json::Array &Arrays = *JScop.getArray("arrays");
608 if (Arrays.size() == 0)
609 return true;
610
611 unsigned ArrayIdx = 0;
612 for (auto &SAI : S.arrays()) {
613 if (!SAI->isArrayKind())
614 continue;
615 if (ArrayIdx + 1 > Arrays.size()) {
616 errs() << "Not enough array entries in JScop file.\n";
617 return false;
618 }
619 if (!areArraysEqual(SAI, *Arrays[ArrayIdx].getAsObject())) {
620 errs() << "No match for array '" << SAI->getName() << "' in JScop.\n";
621 return false;
622 }
623 ArrayIdx++;
624 }
625
626 for (; ArrayIdx < Arrays.size(); ArrayIdx++) {
627 const json::Object &Array = *Arrays[ArrayIdx].getAsObject();
628 auto *ElementType =
629 parseTextType(Array.get("type")->getAsString().value().str(),
630 S.getSE()->getContext());
631 if (!ElementType) {
632 errs() << "Error while parsing element type for new array.\n";
633 return false;
634 }
635 const json::Array &SizesArray = *Array.getArray("sizes");
636 std::vector<unsigned> DimSizes;
637 for (unsigned i = 0; i < SizesArray.size(); i++) {
638 auto Size = std::stoi(SizesArray[i].getAsString()->str());
639
640 // Check if the size if positive.
641 if (Size <= 0) {
642 errs() << "The size at index " << i << " is =< 0.\n";
643 return false;
644 }
645
646 DimSizes.push_back(Size);
647 }
648
649 auto NewSAI = S.createScopArrayInfo(
650 ElementType, Array.getString("name").value().str(), DimSizes);
651
652 if (Array.get("allocation")) {
653 NewSAI->setIsOnHeap(Array.getString("allocation").value() == "heap");
654 }
655 }
656
657 return true;
658}
659
660/// Import a Scop from a JSCOP file
661/// @param S The scop to be modified
662/// @param D Dependence Info
663/// @param DL The DataLayout of the function
664/// @param NewAccessStrings Optionally record the imported access strings
665///
666/// @returns true on success, false otherwise. Beware that if this returns
667/// false, the Scop may still have been modified. In this case the Scop contains
668/// invalid information.
669static bool importScop(Scop &S, const Dependences &D, const DataLayout &DL,
670 std::vector<std::string> *NewAccessStrings = nullptr) {
671 std::string FileName = ImportDir + "/" + getFileName(S, ImportPostfix);
672
673 std::string FunctionName = S.getFunction().getName().str();
674 errs() << "Reading JScop '" << S.getNameStr() << "' in function '"
675 << FunctionName << "' from '" << FileName << "'.\n";
676 ErrorOr<std::unique_ptr<MemoryBuffer>> result =
677 MemoryBuffer::getFile(FileName);
678 std::error_code ec = result.getError();
679
680 if (ec) {
681 errs() << "File could not be read: " << ec.message() << "\n";
682 return false;
683 }
684
685 Expected<json::Value> ParseResult =
686 json::parse(result.get().get()->getBuffer());
687
688 if (Error E = ParseResult.takeError()) {
689 errs() << "JSCoP file could not be parsed\n";
690 errs() << E << "\n";
691 consumeError(std::move(E));
692 return false;
693 }
694 json::Object &jscop = *ParseResult.get().getAsObject();
695
696 bool Success = importContext(S, jscop);
697
698 if (!Success)
699 return false;
700
701 Success = importSchedule(S, jscop, D);
702
703 if (!Success)
704 return false;
705
706 Success = importArrays(S, jscop);
707
708 if (!Success)
709 return false;
710
711 Success = importAccesses(S, jscop, DL, NewAccessStrings);
712
713 if (!Success)
714 return false;
715 return true;
716}
717
720 const DataLayout &DL = S.getFunction().getParent()->getDataLayout();
721 std::vector<std::string> NewAccessStrings;
722 if (!importScop(S, D, DL, &NewAccessStrings))
723 report_fatal_error("Tried to import a malformed jscop file.");
724
726 outs()
727 << "Printing analysis 'Polly - Print Scop import result' for region: '"
728 << S.getRegion().getNameStr() << "' in function '"
729 << S.getFunction().getName() << "':\n";
730 outs() << S;
731 for (std::vector<std::string>::const_iterator I = NewAccessStrings.begin(),
732 E = NewAccessStrings.end();
733 I != E; I++)
734 outs() << "New access function '" << *I << "' detected in JSCOP file\n";
735 }
736}
737
unsigned unsignedFromIslSize(const isl::size &Size)
Check that Size is valid (only on debug builds) and cast it to unsigned.
Definition ISLTools.h:40
static bool importAccesses(Scop &S, const json::Object &JScop, const DataLayout &DL, std::vector< std::string > *NewAccessStrings=nullptr)
Import new memory accesses from JScop.
static Type * parseTextType(const std::string &TypeTextRepresentation, LLVMContext &LLVMContext)
Get the accepted primitive type from its textual representation TypeTextRepresentation.
static bool importContext(Scop &S, const json::Object &JScop)
Import a new context from JScop.
static std::string getFileName(Scop &S, StringRef Suffix="")
static cl::opt< bool > PollyPrintImportJscop("polly-print-import-jscop", cl::desc("Polly - Print Scop import result"), cl::cat(PollyCategory))
Dependences::StatementToIslMapTy StatementToIslMapTy
static json::Array exportArrays(const Scop &S)
Export all arrays from the Scop.
STATISTIC(NewAccessMapFound, "Number of updated access functions")
static json::Value getJSON(Scop &S)
static void exportScop(Scop &S)
static bool importScop(Scop &S, const Dependences &D, const DataLayout &DL, std::vector< std::string > *NewAccessStrings=nullptr)
Import a Scop from a JSCOP file.
static bool importSchedule(Scop &S, const json::Object &JScop, const Dependences &D)
Import a new schedule from JScop.
static bool importArrays(Scop &S, const json::Object &JScop)
Import new arrays from JScop.
static bool areArraysEqual(ScopArrayInfo *SAI, const json::Object &Array)
Check whether SAI and Array represent the same array.
llvm::cl::OptionCategory PollyCategory
isl::set set_dim_id(isl::dim type, unsigned int pos, isl::id id) const
isl::id get_dim_id(isl::dim type, unsigned int pos) const
bool is_null() const
boolean is_params() const
class size dim(isl::dim type) const
static isl::union_map empty(isl::ctx ctx)
The accumulated dependence information for a SCoP.
DenseMap< ScopStmt *, isl::map > StatementToIslMapTy
Map type to associate statements with schedules.
bool isValidSchedule(Scop &S, const StatementToIslMapTy &NewSchedules) const
Check if a new schedule is valid.
Represent memory accesses in statements.
Definition ScopInfo.h:426
A class to store information about arrays in the SCoP.
Definition ScopInfo.h:214
const SCEV * getDimensionSize(unsigned Dim) const
Return the size of dimension dim as SCEV*.
Definition ScopInfo.h:287
static const ScopArrayInfo * getFromId(isl::id Id)
Access the ScopArrayInfo associated with an isl Id.
Definition ScopInfo.cpp:381
std::string getName() const
Get the name of this memory reference.
Definition ScopInfo.cpp:333
unsigned getNumberOfDimensions() const
Return the number of dimensions.
Definition ScopInfo.h:275
Type * getElementType() const
Get the canonical element type of this array.
Definition ScopInfo.h:305
Statement of the Scop.
Definition ScopInfo.h:1135
Static Control Part.
Definition ScopInfo.h:1625
int isl_size
Definition ctx.h:96
@ isl_bool_false
Definition ctx.h:91
__isl_export __isl_keep const char * isl_id_get_name(__isl_keep isl_id *id)
Definition isl_id.c:41
__isl_null isl_id * isl_id_free(__isl_take isl_id *id)
Definition isl_id.c:207
#define S(TYPE, NAME)
#define isl_set
const char * size
Definition isl_test.c:1570
const char * str
Definition isl_test.c:2095
#define assert(exp)
__isl_export __isl_give isl_set * isl_map_domain(__isl_take isl_map *bmap)
Definition isl_map.c:8129
__isl_give isl_map * isl_map_copy(__isl_keep isl_map *map)
Definition isl_map.c:1494
__isl_give isl_map * isl_map_set_dim_id(__isl_take isl_map *map, enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
Definition isl_map.c:1009
__isl_export isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Definition isl_map.c:9237
__isl_give isl_id * isl_map_get_dim_id(__isl_keep isl_map *map, enum isl_dim_type type, unsigned pos)
Definition isl_map.c:991
__isl_constructor __isl_give isl_map * isl_map_read_from_str(isl_ctx *ctx, const char *str)
isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
Definition isl_map.c:110
__isl_give isl_map * isl_map_set_tuple_id(__isl_take isl_map *map, enum isl_dim_type type, __isl_take isl_id *id)
Definition isl_map.c:754
__isl_null isl_map * isl_map_free(__isl_take isl_map *map)
Definition isl_map.c:6421
__isl_export __isl_give isl_set * isl_map_range(__isl_take isl_map *map)
Definition isl_map.c:6109
__isl_give isl_id * isl_map_get_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
Definition isl_map.c:824
boolean manage(isl_bool val)
void runExportJSON(Scop &S)
This pass exports a scop to a jscop file.
void runImportJSON(Scop &S, DependenceAnalysis::Result &DA)
This pass imports a scop from a jscop file.
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
Definition ScopInfo.h:110
void getDebugLocation(const llvm::Region *R, unsigned &LineBegin, unsigned &LineEnd, std::string &FileName)
Get the location of a region from the debug info.
isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
Definition isl_map.c:9192
__isl_export __isl_give isl_set * isl_set_intersect_params(__isl_take isl_set *set, __isl_take isl_set *params)
Definition isl_map.c:3982
__isl_null isl_set * isl_set_free(__isl_take isl_set *set)
Definition isl_map.c:3513
__isl_export isl_bool isl_set_is_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
__isl_export __isl_give isl_set * isl_set_intersect(__isl_take isl_set *set1, __isl_take isl_set *set2)
Definition isl_map.c:3965
__isl_null isl_space * isl_space_free(__isl_take isl_space *space)
Definition isl_space.c:445
__isl_give isl_id * isl_space_get_tuple_id(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:598
__isl_give isl_id * isl_space_get_dim_id(__isl_keep isl_space *space, enum isl_dim_type type, unsigned pos)
Definition isl_space.c:774
isl_size isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:340
@ isl_dim_param
Definition space_type.h:15
@ isl_dim_in
Definition space_type.h:16
@ isl_dim_set
Definition space_type.h:18
@ isl_dim_out
Definition space_type.h:17
const Dependences & getDependences(Dependences::AnalysisLevel Level)
Return the dependence information for the current SCoP.