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 /// @brief Export the SCoP @p S to a JSON file. 66 bool runOnScop(Scop &S) override; 67 68 /// @brief Print the SCoP @p S as it is exported. 69 void printScop(raw_ostream &OS, Scop &S) const override; 70 71 /// @brief 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 datalayout 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 /// @brief Import new access functions for SCoP @p S from a JSON file. 120 bool runOnScop(Scop &S) override; 121 122 /// @brief Print the SCoP @p S and the imported access functions. 123 void printScop(raw_ostream &OS, Scop &S) const override; 124 125 /// @brief 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 for (unsigned i = 1; i < SAI->getNumberOfDimensions(); i++) { 156 SAI->getDimensionSize(i)->print(RawStringOstream); 157 Array["sizes"].append(RawStringOstream.str()); 158 Buffer.clear(); 159 } 160 SAI->getElementType()->print(RawStringOstream); 161 Array["type"] = RawStringOstream.str(); 162 Buffer.clear(); 163 Arrays.append(Array); 164 } 165 return Arrays; 166 } 167 168 Json::Value JSONExporter::getJSON(Scop &S) const { 169 Json::Value root; 170 unsigned LineBegin, LineEnd; 171 std::string FileName; 172 173 getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName); 174 std::string Location; 175 if (LineBegin != (unsigned)-1) 176 Location = FileName + ":" + std::to_string(LineBegin) + "-" + 177 std::to_string(LineEnd); 178 179 root["name"] = S.getNameStr(); 180 root["context"] = S.getContextStr(); 181 if (LineBegin != (unsigned)-1) 182 root["location"] = Location; 183 184 root["arrays"] = exportArrays(S); 185 186 root["statements"]; 187 188 for (ScopStmt &Stmt : S) { 189 Json::Value statement; 190 191 statement["name"] = Stmt.getBaseName(); 192 statement["domain"] = Stmt.getDomainStr(); 193 statement["schedule"] = Stmt.getScheduleStr(); 194 statement["accesses"]; 195 196 for (MemoryAccess *MA : Stmt) { 197 Json::Value access; 198 199 access["kind"] = MA->isRead() ? "read" : "write"; 200 access["relation"] = MA->getOriginalAccessRelationStr(); 201 202 statement["accesses"].append(access); 203 } 204 205 root["statements"].append(statement); 206 } 207 208 return root; 209 } 210 211 bool JSONExporter::runOnScop(Scop &S) { 212 std::string FileName = ImportDir + "/" + getFileName(S); 213 214 Json::Value jscop = getJSON(S); 215 Json::StyledWriter writer; 216 std::string fileContent = writer.write(jscop); 217 218 // Write to file. 219 std::error_code EC; 220 tool_output_file F(FileName, EC, llvm::sys::fs::F_Text); 221 222 std::string FunctionName = S.getFunction().getName(); 223 errs() << "Writing JScop '" << S.getNameStr() << "' in function '" 224 << FunctionName << "' to '" << FileName << "'.\n"; 225 226 if (!EC) { 227 F.os() << fileContent; 228 F.os().close(); 229 if (!F.os().has_error()) { 230 errs() << "\n"; 231 F.keep(); 232 return false; 233 } 234 } 235 236 errs() << " error opening file for writing!\n"; 237 F.os().clear_error(); 238 239 return false; 240 } 241 242 void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const { 243 AU.setPreservesAll(); 244 AU.addRequired<ScopInfoRegionPass>(); 245 } 246 247 Pass *polly::createJSONExporterPass() { return new JSONExporter(); } 248 249 char JSONImporter::ID = 0; 250 std::string JSONImporter::getFileName(Scop &S) const { 251 std::string FunctionName = S.getFunction().getName(); 252 std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop"; 253 254 if (ImportPostfix != "") 255 FileName += "." + ImportPostfix; 256 257 return FileName; 258 } 259 260 void JSONImporter::printScop(raw_ostream &OS, Scop &S) const { 261 S.print(OS); 262 for (std::vector<std::string>::const_iterator I = NewAccessStrings.begin(), 263 E = NewAccessStrings.end(); 264 I != E; I++) 265 OS << "New access function '" << *I << "'detected in JSCOP file\n"; 266 } 267 268 typedef Dependences::StatementToIslMapTy StatementToIslMapTy; 269 270 bool JSONImporter::importContext(Scop &S, Json::Value &JScop) { 271 isl_set *OldContext = S.getContext(); 272 isl_set *NewContext = 273 isl_set_read_from_str(S.getIslCtx(), JScop["context"].asCString()); 274 275 for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) { 276 isl_id *Id = isl_set_get_dim_id(OldContext, isl_dim_param, i); 277 NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, Id); 278 } 279 280 isl_set_free(OldContext); 281 S.setContext(NewContext); 282 return true; 283 } 284 285 bool JSONImporter::importSchedule(Scop &S, Json::Value &JScop, 286 const Dependences &D) { 287 StatementToIslMapTy NewSchedule; 288 289 int Index = 0; 290 for (ScopStmt &Stmt : S) { 291 Json::Value Schedule = JScop["statements"][Index]["schedule"]; 292 isl_map *Map = isl_map_read_from_str(S.getIslCtx(), Schedule.asCString()); 293 isl_space *Space = Stmt.getDomainSpace(); 294 295 // Copy the old tuple id. This is necessary to retain the user pointer, 296 // that stores the reference to the ScopStmt this schedule belongs to. 297 Map = isl_map_set_tuple_id(Map, isl_dim_in, 298 isl_space_get_tuple_id(Space, isl_dim_set)); 299 for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { 300 isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i); 301 Map = isl_map_set_dim_id(Map, isl_dim_param, i, Id); 302 } 303 isl_space_free(Space); 304 NewSchedule[&Stmt] = Map; 305 Index++; 306 } 307 308 if (!D.isValidSchedule(S, &NewSchedule)) { 309 errs() << "JScop file contains a schedule that changes the " 310 << "dependences. Use -disable-polly-legality to continue anyways\n"; 311 for (auto Element : NewSchedule) 312 isl_map_free(Element.second); 313 return false; 314 } 315 316 auto ScheduleMap = isl_union_map_empty(S.getParamSpace()); 317 for (ScopStmt &Stmt : S) { 318 if (NewSchedule.find(&Stmt) != NewSchedule.end()) 319 ScheduleMap = isl_union_map_add_map(ScheduleMap, NewSchedule[&Stmt]); 320 else 321 ScheduleMap = isl_union_map_add_map(ScheduleMap, Stmt.getSchedule()); 322 } 323 324 S.setSchedule(ScheduleMap); 325 326 return true; 327 } 328 329 bool JSONImporter::importAccesses(Scop &S, Json::Value &JScop, 330 const DataLayout &DL) { 331 int StatementIdx = 0; 332 for (ScopStmt &Stmt : S) { 333 int MemoryAccessIdx = 0; 334 for (MemoryAccess *MA : Stmt) { 335 Json::Value Accesses = JScop["statements"][StatementIdx]["accesses"] 336 [MemoryAccessIdx]["relation"]; 337 isl_map *NewAccessMap = 338 isl_map_read_from_str(S.getIslCtx(), Accesses.asCString()); 339 isl_map *CurrentAccessMap = MA->getAccessRelation(); 340 341 if (isl_map_dim(NewAccessMap, isl_dim_param) != 342 isl_map_dim(CurrentAccessMap, isl_dim_param)) { 343 errs() << "JScop file changes the number of parameter dimensions\n"; 344 isl_map_free(CurrentAccessMap); 345 isl_map_free(NewAccessMap); 346 return false; 347 } 348 349 isl_id *NewOutId; 350 351 if (MA->isArrayKind()) { 352 NewOutId = isl_map_get_tuple_id(NewAccessMap, isl_dim_out); 353 auto *SAI = S.getArrayInfoByName(isl_id_get_name(NewOutId)); 354 isl_id *OutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out); 355 auto *OutSAI = ScopArrayInfo::getFromId(OutId); 356 if (!SAI || SAI->getElementType() != OutSAI->getElementType()) { 357 errs() << "JScop file contains access function with undeclared " 358 "ScopArrayInfo\n"; 359 isl_map_free(CurrentAccessMap); 360 isl_map_free(NewAccessMap); 361 isl_id_free(NewOutId); 362 return false; 363 } 364 isl_id_free(NewOutId); 365 NewOutId = SAI->getBasePtrId(); 366 } else { 367 NewOutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out); 368 } 369 370 NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_out, NewOutId); 371 372 if (MA->isArrayKind()) { 373 // We keep the old alignment, thus we cannot allow accesses to memory 374 // locations that were not accessed before if the alignment of the 375 // access is not the default alignment. 376 bool SpecialAlignment = true; 377 if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) { 378 SpecialAlignment = 379 DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment(); 380 } else if (StoreInst *StoreI = 381 dyn_cast<StoreInst>(MA->getAccessInstruction())) { 382 SpecialAlignment = 383 DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) != 384 StoreI->getAlignment(); 385 } 386 387 if (SpecialAlignment) { 388 isl_set *NewAccessSet = isl_map_range(isl_map_copy(NewAccessMap)); 389 isl_set *CurrentAccessSet = 390 isl_map_range(isl_map_copy(CurrentAccessMap)); 391 bool IsSubset = isl_set_is_subset(NewAccessSet, CurrentAccessSet); 392 isl_set_free(NewAccessSet); 393 isl_set_free(CurrentAccessSet); 394 395 if (!IsSubset) { 396 errs() << "JScop file changes the accessed memory\n"; 397 isl_map_free(CurrentAccessMap); 398 isl_map_free(NewAccessMap); 399 return false; 400 } 401 } 402 } 403 404 // We need to copy the isl_ids for the parameter dimensions to the new 405 // map. Without doing this the current map would have different 406 // ids then the new one, even though both are named identically. 407 for (unsigned i = 0; i < isl_map_dim(CurrentAccessMap, isl_dim_param); 408 i++) { 409 isl_id *Id = isl_map_get_dim_id(CurrentAccessMap, isl_dim_param, i); 410 NewAccessMap = isl_map_set_dim_id(NewAccessMap, isl_dim_param, i, Id); 411 } 412 413 // Copy the old tuple id. This is necessary to retain the user pointer, 414 // that stores the reference to the ScopStmt this access belongs to. 415 isl_id *Id = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_in); 416 NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_in, Id); 417 418 auto NewAccessDomain = isl_map_domain(isl_map_copy(NewAccessMap)); 419 auto CurrentAccessDomain = isl_map_domain(isl_map_copy(CurrentAccessMap)); 420 421 if (!isl_set_has_equal_space(NewAccessDomain, CurrentAccessDomain)) { 422 errs() << "JScop file contains access function with incompatible " 423 << "dimensions\n"; 424 isl_map_free(CurrentAccessMap); 425 isl_map_free(NewAccessMap); 426 isl_set_free(NewAccessDomain); 427 isl_set_free(CurrentAccessDomain); 428 return false; 429 } 430 431 NewAccessDomain = 432 isl_set_intersect_params(NewAccessDomain, S.getContext()); 433 CurrentAccessDomain = 434 isl_set_intersect_params(CurrentAccessDomain, S.getContext()); 435 436 if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) == 437 isl_bool_false) { 438 errs() << "Mapping not defined for all iteration domain elements\n"; 439 isl_set_free(CurrentAccessDomain); 440 isl_set_free(NewAccessDomain); 441 isl_map_free(CurrentAccessMap); 442 isl_map_free(NewAccessMap); 443 return false; 444 } 445 446 isl_set_free(CurrentAccessDomain); 447 isl_set_free(NewAccessDomain); 448 449 if (!isl_map_is_equal(NewAccessMap, CurrentAccessMap)) { 450 // Statistics. 451 ++NewAccessMapFound; 452 NewAccessStrings.push_back(Accesses.asCString()); 453 MA->setNewAccessRelation(NewAccessMap); 454 } else { 455 isl_map_free(NewAccessMap); 456 } 457 isl_map_free(CurrentAccessMap); 458 MemoryAccessIdx++; 459 } 460 StatementIdx++; 461 } 462 463 return true; 464 } 465 466 /// @brief Check whether @p SAI and @p Array represent the same array. 467 bool areArraysEqual(ScopArrayInfo *SAI, Json::Value Array) { 468 std::string Buffer; 469 llvm::raw_string_ostream RawStringOstream(Buffer); 470 471 if (SAI->getName() != Array["name"].asCString()) 472 return false; 473 474 if (SAI->getNumberOfDimensions() != Array["sizes"].size() + 1) 475 return false; 476 477 for (unsigned i = 0; i < Array["sizes"].size(); i++) { 478 SAI->getDimensionSize(i + 1)->print(RawStringOstream); 479 if (RawStringOstream.str() != Array["sizes"][i].asCString()) 480 return false; 481 Buffer.clear(); 482 } 483 484 SAI->getElementType()->print(RawStringOstream); 485 if (RawStringOstream.str() != Array["type"].asCString()) 486 return false; 487 488 return true; 489 } 490 491 /// @brief Get the accepted primitive type from its textual representation 492 /// @p TypeTextRepresentation. 493 /// 494 /// @param TypeTextRepresentation The textual representation of the type. 495 /// @return The pointer to the primitive type, if this type is accepted 496 /// or nullptr otherwise. 497 Type *parseTextType(const std::string &TypeTextRepresentation, 498 LLVMContext &LLVMContext) { 499 std::map<std::string, Type *> MapStrToType = { 500 {"void", Type::getVoidTy(LLVMContext)}, 501 {"half", Type::getHalfTy(LLVMContext)}, 502 {"float", Type::getFloatTy(LLVMContext)}, 503 {"double", Type::getDoubleTy(LLVMContext)}, 504 {"x86_fp80", Type::getX86_FP80Ty(LLVMContext)}, 505 {"fp128", Type::getFP128Ty(LLVMContext)}, 506 {"ppc_fp128", Type::getPPC_FP128Ty(LLVMContext)}, 507 {"i1", Type::getInt1Ty(LLVMContext)}, 508 {"i8", Type::getInt8Ty(LLVMContext)}, 509 {"i16", Type::getInt16Ty(LLVMContext)}, 510 {"i32", Type::getInt32Ty(LLVMContext)}, 511 {"i64", Type::getInt64Ty(LLVMContext)}, 512 {"i128", Type::getInt128Ty(LLVMContext)}}; 513 514 auto It = MapStrToType.find(TypeTextRepresentation); 515 if (It != MapStrToType.end()) 516 return It->second; 517 518 errs() << "Textual representation can not be parsed: " 519 << TypeTextRepresentation << "\n"; 520 return nullptr; 521 } 522 523 bool JSONImporter::importArrays(Scop &S, Json::Value &JScop) { 524 Json::Value Arrays = JScop["arrays"]; 525 526 if (Arrays.size() == 0) 527 return true; 528 529 unsigned ArrayIdx = 0; 530 for (auto &SAI : S.arrays()) { 531 if (!SAI->isArrayKind()) 532 continue; 533 if (ArrayIdx + 1 > Arrays.size()) 534 return false; 535 if (!areArraysEqual(SAI, Arrays[ArrayIdx])) 536 return false; 537 ArrayIdx++; 538 } 539 540 for (; ArrayIdx < Arrays.size(); ArrayIdx++) { 541 auto *ElementType = parseTextType(Arrays[ArrayIdx]["type"].asCString(), 542 S.getSE()->getContext()); 543 if (!ElementType) 544 return false; 545 std::vector<unsigned> DimSizes; 546 for (unsigned i = 0; i < Arrays[ArrayIdx]["sizes"].size(); i++) 547 DimSizes.push_back(std::stoi(Arrays[ArrayIdx]["sizes"][i].asCString())); 548 S.createScopArrayInfo(ElementType, Arrays[ArrayIdx]["name"].asCString(), 549 DimSizes); 550 } 551 552 return true; 553 } 554 555 bool JSONImporter::runOnScop(Scop &S) { 556 const Dependences &D = 557 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); 558 const DataLayout &DL = S.getFunction().getParent()->getDataLayout(); 559 560 std::string FileName = ImportDir + "/" + getFileName(S); 561 562 std::string FunctionName = S.getFunction().getName(); 563 errs() << "Reading JScop '" << S.getNameStr() << "' in function '" 564 << FunctionName << "' from '" << FileName << "'.\n"; 565 ErrorOr<std::unique_ptr<MemoryBuffer>> result = 566 MemoryBuffer::getFile(FileName); 567 std::error_code ec = result.getError(); 568 569 if (ec) { 570 errs() << "File could not be read: " << ec.message() << "\n"; 571 return false; 572 } 573 574 Json::Reader reader; 575 Json::Value jscop; 576 577 bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop); 578 579 if (!parsingSuccessful) { 580 errs() << "JSCoP file could not be parsed\n"; 581 return false; 582 } 583 584 bool Success = importContext(S, jscop); 585 586 if (!Success) 587 return false; 588 589 Success = importSchedule(S, jscop, D); 590 591 if (!Success) 592 return false; 593 594 Success = importArrays(S, jscop); 595 596 if (!Success) 597 return false; 598 599 Success = importAccesses(S, jscop, DL); 600 601 if (!Success) 602 return false; 603 604 return false; 605 } 606 607 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const { 608 ScopPass::getAnalysisUsage(AU); 609 AU.addRequired<DependenceInfo>(); 610 } 611 612 Pass *polly::createJSONImporterPass() { return new JSONImporter(); } 613 614 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop", 615 "Polly - Export Scops as JSON" 616 " (Writes a .jscop file for each Scop)", 617 false, false); 618 INITIALIZE_PASS_DEPENDENCY(DependenceInfo) 619 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop", 620 "Polly - Export Scops as JSON" 621 " (Writes a .jscop file for each Scop)", 622 false, false) 623 624 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop", 625 "Polly - Import Scops from JSON" 626 " (Reads a .jscop file for each Scop)", 627 false, false); 628 INITIALIZE_PASS_DEPENDENCY(DependenceInfo) 629 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop", 630 "Polly - Import Scops from JSON" 631 " (Reads a .jscop file for each Scop)", 632 false, false) 633