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