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