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/LinkAllPasses.h" 15 #include "polly/Dependences.h" 16 #include "polly/ScopInfo.h" 17 #include "polly/ScopPass.h" 18 19 #include "llvm/Support/CommandLine.h" 20 #include "llvm/Support/ToolOutputFile.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/system_error.h" 23 #include "llvm/ADT/OwningPtr.h" 24 #include "llvm/Assembly/Writer.h" 25 #include "llvm/ADT/Statistic.h" 26 27 #define DEBUG_TYPE "polly-import-jscop" 28 29 #include "json/reader.h" 30 #include "json/writer.h" 31 32 #include "isl/set.h" 33 #include "isl/map.h" 34 #include "isl/constraint.h" 35 #include "isl/printer.h" 36 37 #include <string> 38 39 using namespace llvm; 40 using namespace polly; 41 42 STATISTIC(NewAccessMapFound, "Number of updated access functions"); 43 44 namespace { 45 static cl::opt<std::string> 46 ImportDir("polly-import-jscop-dir", 47 cl::desc("The directory to import the .jscop files from."), 48 cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired, 49 cl::init(".")); 50 51 static cl::opt<std::string> 52 ImportPostfix("polly-import-jscop-postfix", 53 cl::desc("Postfix to append to the import .jsop files."), 54 cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired, 55 cl::init("")); 56 57 struct JSONExporter : public ScopPass { 58 static char ID; 59 Scop *S; 60 explicit JSONExporter() : ScopPass(ID) {} 61 62 std::string getFileName(Scop *S) const; 63 Json::Value getJSON(Scop &scop) const; 64 virtual bool runOnScop(Scop &S); 65 void printScop(raw_ostream &OS) const; 66 void getAnalysisUsage(AnalysisUsage &AU) const; 67 }; 68 69 struct JSONImporter : public ScopPass { 70 static char ID; 71 Scop *S; 72 std::vector<std::string> newAccessStrings; 73 explicit JSONImporter() : ScopPass(ID) {} 74 75 std::string getFileName(Scop *S) const; 76 virtual bool runOnScop(Scop &S); 77 void printScop(raw_ostream &OS) const; 78 void getAnalysisUsage(AnalysisUsage &AU) const; 79 }; 80 81 } 82 83 char JSONExporter::ID = 0; 84 std::string JSONExporter::getFileName(Scop *S) const { 85 std::string FunctionName = 86 S->getRegion().getEntry()->getParent()->getName(); 87 std::string FileName = FunctionName + "___" + S->getNameStr() + ".jscop"; 88 return FileName; 89 } 90 91 void JSONExporter::printScop(raw_ostream &OS) const { 92 S->print(OS); 93 } 94 95 Json::Value JSONExporter::getJSON(Scop &scop) const { 96 Json::Value root; 97 98 root["name"] = S->getRegion().getNameStr(); 99 root["context"] = S->getContextStr(); 100 root["statements"]; 101 102 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 103 ScopStmt *Stmt = *SI; 104 105 if (Stmt->isFinalRead()) 106 continue; 107 108 Json::Value statement; 109 110 statement["name"] = Stmt->getBaseName(); 111 statement["domain"] = Stmt->getDomainStr(); 112 statement["schedule"] = Stmt->getScatteringStr(); 113 statement["accesses"]; 114 115 for (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(), 116 ME = Stmt->memacc_end(); MI != ME; ++MI) { 117 Json::Value access; 118 119 access["kind"] = (*MI)->isRead() ? "read" : "write"; 120 access["relation"] = (*MI)->getAccessRelationStr(); 121 122 statement["accesses"].append(access); 123 } 124 125 root["statements"].append(statement); 126 } 127 128 return root; 129 } 130 131 bool JSONExporter::runOnScop(Scop &scop) { 132 S = &scop; 133 Region &R = S->getRegion(); 134 135 std::string FileName = ImportDir + "/" + getFileName(S); 136 137 Json::Value jscop = getJSON(scop); 138 Json::StyledWriter writer; 139 std::string fileContent = writer.write(jscop); 140 141 // Write to file. 142 std::string ErrInfo; 143 tool_output_file F(FileName.c_str(), ErrInfo); 144 145 std::string FunctionName = R.getEntry()->getParent()->getName(); 146 errs() << "Writing JScop '" << R.getNameStr() << "' in function '" 147 << FunctionName << "' to '" << FileName << "'.\n"; 148 149 if (ErrInfo.empty()) { 150 F.os() << fileContent; 151 F.os().close(); 152 if (!F.os().has_error()) { 153 errs() << "\n"; 154 F.keep(); 155 return false; 156 } 157 } 158 159 errs() << " error opening file for writing!\n"; 160 F.os().clear_error(); 161 162 return false; 163 } 164 165 void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const { 166 AU.setPreservesAll(); 167 AU.addRequired<ScopInfo>(); 168 } 169 170 INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop", 171 "Polly - Export Scops as JSON" 172 " (Writes a .jscop file for each Scop)", false, false) 173 INITIALIZE_PASS_DEPENDENCY(Dependences) 174 INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop", 175 "Polly - Export Scops as JSON" 176 " (Writes a .jscop file for each Scop)", false, false) 177 178 Pass *polly::createJSONExporterPass() { 179 return new JSONExporter(); 180 } 181 182 char JSONImporter::ID = 0; 183 std::string JSONImporter::getFileName(Scop *S) const { 184 std::string FunctionName = 185 S->getRegion().getEntry()->getParent()->getName(); 186 std::string FileName = FunctionName + "___" + S->getNameStr() + ".jscop"; 187 188 if (ImportPostfix != "") 189 FileName += "." + ImportPostfix; 190 191 return FileName; 192 } 193 194 void JSONImporter::printScop(raw_ostream &OS) const { 195 S->print(OS); 196 for (std::vector<std::string>::const_iterator I = newAccessStrings.begin(), 197 E = newAccessStrings.end(); I != E; I++) 198 OS << "New access function '" << *I << "'detected in JSCOP file\n"; 199 } 200 201 typedef Dependences::StatementToIslMapTy StatementToIslMapTy; 202 203 bool JSONImporter::runOnScop(Scop &scop) { 204 S = &scop; 205 Region &R = S->getRegion(); 206 Dependences *D = &getAnalysis<Dependences>(); 207 208 std::string FileName = ImportDir + "/" + getFileName(S); 209 210 std::string FunctionName = R.getEntry()->getParent()->getName(); 211 errs() << "Reading JScop '" << R.getNameStr() << "' in function '" 212 << FunctionName << "' from '" << FileName << "'.\n"; 213 OwningPtr<MemoryBuffer> result; 214 error_code ec = MemoryBuffer::getFile(FileName, result); 215 216 if (ec) { 217 errs() << "File could not be read: " << ec.message() << "\n"; 218 return false; 219 } 220 221 Json::Reader reader; 222 Json::Value jscop; 223 224 bool parsingSuccessful = reader.parse(result->getBufferStart(), jscop); 225 226 if (!parsingSuccessful) { 227 errs() << "JSCoP file could not be parsed\n"; 228 return false; 229 } 230 231 isl_set *OldContext = S->getContext(); 232 isl_set *NewContext = isl_set_read_from_str(S->getIslCtx(), 233 jscop["context"].asCString()); 234 235 for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) { 236 isl_id *id = isl_set_get_dim_id(OldContext, isl_dim_param, i); 237 NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, id); 238 239 } 240 241 isl_set_free(OldContext); 242 S->setContext(NewContext); 243 244 StatementToIslMapTy &NewScattering = *(new StatementToIslMapTy()); 245 246 int index = 0; 247 248 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 249 ScopStmt *Stmt = *SI; 250 251 if (Stmt->isFinalRead()) 252 continue; 253 Json::Value schedule = jscop["statements"][index]["schedule"]; 254 255 isl_map *m = isl_map_read_from_str(S->getIslCtx(), schedule.asCString()); 256 NewScattering[*SI] = m; 257 index++; 258 } 259 260 if (!D->isValidScattering(&NewScattering)) { 261 errs() << "JScop file contains a scattering that changes the " 262 << "dependences. Use -disable-polly-legality to continue anyways\n"; 263 return false; 264 } 265 266 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 267 ScopStmt *Stmt = *SI; 268 269 if (NewScattering.find(Stmt) != NewScattering.end()) 270 Stmt->setScattering(NewScattering[Stmt]); 271 } 272 273 int statementIdx = 0; 274 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 275 ScopStmt *Stmt = *SI; 276 277 if (Stmt->isFinalRead()) 278 continue; 279 280 int memoryAccessIdx = 0; 281 for (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(), 282 ME = Stmt->memacc_end(); MI != ME; ++MI) { 283 Json::Value accesses = jscop["statements"][statementIdx] 284 ["accesses"][memoryAccessIdx]["relation"]; 285 isl_map *newAccessMap = isl_map_read_from_str(S->getIslCtx(), 286 accesses.asCString()); 287 isl_map *currentAccessMap = (*MI)->getAccessRelation(); 288 289 if (isl_map_dim(newAccessMap, isl_dim_param) != 290 isl_map_dim(currentAccessMap, isl_dim_param)) { 291 errs() << "JScop file changes the number of parameter dimensions\n"; 292 isl_map_free(currentAccessMap); 293 isl_map_free(newAccessMap); 294 return false; 295 296 } 297 298 // We need to copy the isl_ids for the parameter dimensions to the new 299 // map. Without doing this the current map would have different 300 // ids then the new one, even though both are named identically. 301 for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param); 302 i++) { 303 isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i); 304 newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id); 305 } 306 307 if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) { 308 errs() << "JScop file contains access function with incompatible " 309 << "dimensions\n"; 310 isl_map_free(currentAccessMap); 311 isl_map_free(newAccessMap); 312 return false; 313 } 314 if (isl_map_dim(newAccessMap, isl_dim_out) != 1) { 315 errs() << "New access map in JScop file should be single dimensional\n"; 316 isl_map_free(currentAccessMap); 317 isl_map_free(newAccessMap); 318 return false; 319 } 320 if (!isl_map_is_equal(newAccessMap, currentAccessMap)) { 321 // Statistics. 322 ++NewAccessMapFound; 323 newAccessStrings.push_back(accesses.asCString()); 324 (*MI)->setNewAccessRelation(newAccessMap); 325 } else { 326 isl_map_free(newAccessMap); 327 } 328 isl_map_free(currentAccessMap); 329 memoryAccessIdx++; 330 } 331 statementIdx++; 332 } 333 334 return false; 335 } 336 337 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const { 338 ScopPass::getAnalysisUsage(AU); 339 AU.addRequired<Dependences>(); 340 } 341 342 INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop", 343 "Polly - Import Scops from JSON" 344 " (Reads a .jscop file for each Scop)", false, false) 345 INITIALIZE_PASS_DEPENDENCY(Dependences) 346 INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop", 347 "Polly - Import Scops from JSON" 348 " (Reads a .jscop file for each Scop)", false, false) 349 350 Pass *polly::createJSONImporterPass() { 351 return new JSONImporter(); 352 } 353