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()->getNameStr(); 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()->getNameStr(); 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 static RegisterPass<JSONExporter> A("polly-export-jscop", 171 "Polly - Export Scops as JSON" 172 " (Writes a .jscop file for each Scop)" 173 ); 174 175 Pass *polly::createJSONExporterPass() { 176 return new JSONExporter(); 177 } 178 179 char JSONImporter::ID = 0; 180 std::string JSONImporter::getFileName(Scop *S) const { 181 std::string FunctionName = 182 S->getRegion().getEntry()->getParent()->getNameStr(); 183 std::string FileName = FunctionName + "___" + S->getNameStr() + ".jscop"; 184 185 if (ImportPostfix != "") 186 FileName += "." + ImportPostfix; 187 188 return FileName; 189 } 190 191 void JSONImporter::printScop(raw_ostream &OS) const { 192 S->print(OS); 193 for (std::vector<std::string>::const_iterator I = newAccessStrings.begin(), 194 E = newAccessStrings.end(); I != E; I++) 195 OS << "New access function '" << *I << "'detected in JSCOP file\n"; 196 } 197 198 typedef Dependences::StatementToIslMapTy StatementToIslMapTy; 199 200 bool JSONImporter::runOnScop(Scop &scop) { 201 S = &scop; 202 Region &R = S->getRegion(); 203 Dependences *D = &getAnalysis<Dependences>(); 204 205 std::string FileName = ImportDir + "/" + getFileName(S); 206 207 std::string FunctionName = R.getEntry()->getParent()->getNameStr(); 208 errs() << "Reading JScop '" << R.getNameStr() << "' in function '" 209 << FunctionName << "' from '" << FileName << "'.\n"; 210 OwningPtr<MemoryBuffer> result; 211 error_code ec = MemoryBuffer::getFile(FileName, result); 212 213 if (ec) { 214 errs() << "File could not be read: " << ec.message() << "\n"; 215 return false; 216 } 217 218 Json::Reader reader; 219 Json::Value jscop; 220 221 bool parsingSuccessful = reader.parse(result->getBufferStart(), jscop); 222 223 if (!parsingSuccessful) { 224 errs() << "JSCoP file could not be parsed\n"; 225 return false; 226 } 227 228 StatementToIslMapTy &NewScattering = *(new StatementToIslMapTy()); 229 230 int index = 0; 231 232 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 233 ScopStmt *Stmt = *SI; 234 235 if (Stmt->isFinalRead()) 236 continue; 237 Json::Value schedule = jscop["statements"][index]["schedule"]; 238 239 isl_map *m = isl_map_read_from_str(S->getIslCtx(), schedule.asCString()); 240 NewScattering[*SI] = m; 241 index++; 242 } 243 244 if (!D->isValidScattering(&NewScattering)) { 245 errs() << "JScop file contains a scattering that changes the " 246 << "dependences. Use -disable-polly-legality to continue anyways\n"; 247 return false; 248 } 249 250 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 251 ScopStmt *Stmt = *SI; 252 253 if (NewScattering.find(Stmt) != NewScattering.end()) 254 Stmt->setScattering(NewScattering[Stmt]); 255 } 256 257 int statementIdx = 0; 258 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { 259 ScopStmt *Stmt = *SI; 260 261 if (Stmt->isFinalRead()) 262 continue; 263 264 int memoryAccessIdx = 0; 265 for (ScopStmt::memacc_iterator MI = Stmt->memacc_begin(), 266 ME = Stmt->memacc_end(); MI != ME; ++MI) { 267 Json::Value accesses = jscop["statements"][statementIdx] 268 ["accesses"][memoryAccessIdx]["relation"]; 269 isl_map *newAccessMap = isl_map_read_from_str(S->getIslCtx(), 270 accesses.asCString()); 271 isl_map *currentAccessMap = (*MI)->getAccessRelation(); 272 if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) { 273 errs() << "JScop file contains access function with incompatible " 274 << "dimensions\n"; 275 isl_map_free(currentAccessMap); 276 isl_map_free(newAccessMap); 277 return false; 278 } 279 if (isl_map_dim(newAccessMap, isl_dim_out) != 1) { 280 errs() << "New access map in JScop file should be single dimensional\n"; 281 isl_map_free(currentAccessMap); 282 isl_map_free(newAccessMap); 283 return false; 284 } 285 if (!isl_map_is_equal(newAccessMap, currentAccessMap)) { 286 // Statistics. 287 ++NewAccessMapFound; 288 newAccessStrings.push_back(accesses.asCString()); 289 (*MI)->setNewAccessRelation(newAccessMap); 290 } else { 291 isl_map_free(newAccessMap); 292 } 293 isl_map_free(currentAccessMap); 294 memoryAccessIdx++; 295 } 296 statementIdx++; 297 } 298 299 return false; 300 } 301 302 void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const { 303 ScopPass::getAnalysisUsage(AU); 304 AU.addRequired<Dependences>(); 305 } 306 307 static RegisterPass<JSONImporter> B("polly-import-jscop", 308 "Polly - Import Scops from JSON" 309 " (Reads a .jscop file for each Scop)" 310 ); 311 312 Pass *polly::createJSONImporterPass() { 313 return new JSONImporter(); 314 } 315