diff options
author | Michael Kruse <llvm@meinersbur.de> | 2018-08-01 00:15:16 +0000 |
---|---|---|
committer | Michael Kruse <llvm@meinersbur.de> | 2018-08-01 00:15:16 +0000 |
commit | 23655020a271d83870163905905a13a26cfceb94 (patch) | |
tree | 00ca485d7d4f70df2c1723a91020362a9f59c2bb /polly/lib/Exchange | |
parent | 83c49e8ed407ae60e1d82f389780a4fc5e9b430f (diff) | |
download | bcm5719-llvm-23655020a271d83870163905905a13a26cfceb94.tar.gz bcm5719-llvm-23655020a271d83870163905905a13a26cfceb94.zip |
[JSONExporter] Replace bundled Jsoncpp with llvm/Support/JSON.h. NFC.
Differential Revision: https://reviews.llvm.org/D49950
llvm-svn: 338461
Diffstat (limited to 'polly/lib/Exchange')
-rw-r--r-- | polly/lib/Exchange/JSONExporter.cpp | 146 |
1 files changed, 79 insertions, 67 deletions
diff --git a/polly/lib/Exchange/JSONExporter.cpp b/polly/lib/Exchange/JSONExporter.cpp index 943d1b2d983..7e7728003e0 100644 --- a/polly/lib/Exchange/JSONExporter.cpp +++ b/polly/lib/Exchange/JSONExporter.cpp @@ -22,6 +22,7 @@ #include "llvm/Analysis/RegionInfo.h" #include "llvm/IR/Module.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/JSON.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/raw_ostream.h" @@ -30,8 +31,6 @@ #include "isl/printer.h" #include "isl/set.h" #include "isl/union_map.h" -#include "json/reader.h" -#include "json/writer.h" #include <memory> #include <string> #include <system_error> @@ -100,8 +99,8 @@ static std::string getFileName(Scop &S, StringRef Suffix = "") { /// @param S The Scop containing the arrays. /// /// @returns Json::Value containing the arrays. -static Json::Value exportArrays(const Scop &S) { - Json::Value Arrays; +static json::Array exportArrays(const Scop &S) { + json::Array Arrays; std::string Buffer; llvm::raw_string_ostream RawStringOstream(Buffer); @@ -109,28 +108,30 @@ static Json::Value exportArrays(const Scop &S) { if (!SAI->isArrayKind()) continue; - Json::Value Array; + json::Object Array; + json::Array Sizes; Array["name"] = SAI->getName(); unsigned i = 0; if (!SAI->getDimensionSize(i)) { - Array["sizes"].append("*"); + Sizes.push_back("*"); i++; } for (; i < SAI->getNumberOfDimensions(); i++) { SAI->getDimensionSize(i)->print(RawStringOstream); - Array["sizes"].append(RawStringOstream.str()); + Sizes.push_back(RawStringOstream.str()); Buffer.clear(); } + Array["sizes"] = std::move(Sizes); SAI->getElementType()->print(RawStringOstream); Array["type"] = RawStringOstream.str(); Buffer.clear(); - Arrays.append(Array); + Arrays.push_back(std::move(Array)); } return Arrays; } -static Json::Value getJSON(Scop &S) { - Json::Value root; +static json::Value getJSON(Scop &S) { + json::Object root; unsigned LineBegin, LineEnd; std::string FileName; @@ -149,35 +150,36 @@ static Json::Value getJSON(Scop &S) { root["statements"]; + json::Array Statements; for (ScopStmt &Stmt : S) { - Json::Value statement; + json::Object statement; statement["name"] = Stmt.getBaseName(); statement["domain"] = Stmt.getDomainStr(); statement["schedule"] = Stmt.getScheduleStr(); - statement["accesses"]; + json::Array Accesses; for (MemoryAccess *MA : Stmt) { - Json::Value access; + json::Object access; access["kind"] = MA->isRead() ? "read" : "write"; access["relation"] = MA->getAccessRelationStr(); - statement["accesses"].append(access); + Accesses.push_back(std::move(access)); } + statement["accesses"] = std::move(Accesses); - root["statements"].append(statement); + Statements.push_back(std::move(statement)); } + root["statements"] = std::move(Statements); return root; } static void exportScop(Scop &S) { std::string FileName = ImportDir + "/" + getFileName(S); - Json::Value jscop = getJSON(S); - Json::StyledWriter writer; - std::string fileContent = writer.write(jscop); + json::Value jscop = getJSON(S); // Write to file. std::error_code EC; @@ -188,7 +190,7 @@ static void exportScop(Scop &S) { << FunctionName << "' to '" << FileName << "'.\n"; if (!EC) { - F.os() << fileContent; + F.os() << formatv("{0:3}", jscop); F.os().close(); if (!F.os().has_error()) { errs() << "\n"; @@ -209,17 +211,17 @@ typedef Dependences::StatementToIslMapTy StatementToIslMapTy; /// @param JScop The JScop file describing the new schedule. /// /// @returns True if the import succeeded, otherwise False. -static bool importContext(Scop &S, Json::Value &JScop) { +static bool importContext(Scop &S, const json::Object &JScop) { isl::set OldContext = S.getContext(); // Check if key 'context' is present. - if (!JScop.isMember("context")) { + if (!JScop.get("context")) { errs() << "JScop file has no key named 'context'.\n"; return false; } isl::set NewContext = - isl::set{S.getIslCtx().get(), JScop["context"].asString()}; + isl::set{S.getIslCtx().get(), JScop.getString("context").getValue()}; // Check whether the context was parsed successfully. if (!NewContext) { @@ -263,16 +265,17 @@ static bool importContext(Scop &S, Json::Value &JScop) { /// @param D The data dependences of the @p S. /// /// @returns True if the import succeeded, otherwise False. -static bool importSchedule(Scop &S, Json::Value &JScop, const Dependences &D) { +static bool importSchedule(Scop &S, const json::Object &JScop, + const Dependences &D) { StatementToIslMapTy NewSchedule; // Check if key 'statements' is present. - if (!JScop.isMember("statements")) { + if (!JScop.get("statements")) { errs() << "JScop file has no key name 'statements'.\n"; return false; } - Json::Value statements = JScop["statements"]; + const json::Array &statements = *JScop.getArray("statements"); // Check whether the number of indices equals the number of statements if (statements.size() != S.getSize()) { @@ -283,18 +286,19 @@ static bool importSchedule(Scop &S, Json::Value &JScop, const Dependences &D) { int Index = 0; for (ScopStmt &Stmt : S) { // Check if key 'schedule' is present. - if (!statements[Index].isMember("schedule")) { + if (!statements[Index].getAsObject()->get("schedule")) { errs() << "Statement " << Index << " has no 'schedule' key.\n"; for (auto Element : NewSchedule) { isl_map_free(Element.second); } return false; } - Json::Value Schedule = statements[Index]["schedule"]; - assert(!Schedule.asString().empty() && + Optional<StringRef> Schedule = + statements[Index].getAsObject()->getString("schedule"); + assert(Schedule.hasValue() && "Schedules that contain extension nodes require special handling."); - isl_map *Map = - isl_map_read_from_str(S.getIslCtx().get(), Schedule.asCString()); + isl_map *Map = isl_map_read_from_str(S.getIslCtx().get(), + Schedule.getValue().str().c_str()); // Check whether the schedule was parsed successfully if (!Map) { @@ -352,16 +356,16 @@ static bool importSchedule(Scop &S, Json::Value &JScop, const Dependences &D) { /// /// @returns True if the import succeeded, otherwise False. static bool -importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL, +importAccesses(Scop &S, const json::Object &JScop, const DataLayout &DL, std::vector<std::string> *NewAccessStrings = nullptr) { int StatementIdx = 0; // Check if key 'statements' is present. - if (!JScop.isMember("statements")) { + if (!JScop.get("statements")) { errs() << "JScop file has no key name 'statements'.\n"; return false; } - Json::Value statements = JScop["statements"]; + const json::Array &statements = *JScop.getArray("statements"); // Check whether the number of indices equals the number of statements if (statements.size() != S.getSize()) { @@ -371,18 +375,21 @@ importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL, for (ScopStmt &Stmt : S) { int MemoryAccessIdx = 0; + const json::Object *Statement = statements[StatementIdx].getAsObject(); + assert(Statement); // Check if key 'accesses' is present. - if (!statements[StatementIdx].isMember("accesses")) { + if (!Statement->get("accesses")) { errs() << "Statement from JScop file has no key name 'accesses' for index " << StatementIdx << ".\n"; return false; } + const json::Array &JsonAccesses = *Statement->getArray("accesses"); // Check whether the number of indices equals the number of memory // accesses - if (Stmt.size() != statements[StatementIdx]["accesses"].size()) { + if (Stmt.size() != JsonAccesses.size()) { errs() << "The number of memory accesses in the JSop file and the number " "of memory accesses differ for index " << StatementIdx << ".\n"; @@ -391,17 +398,18 @@ importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL, for (MemoryAccess *MA : Stmt) { // Check if key 'relation' is present. - Json::Value JsonMemoryAccess = - statements[StatementIdx]["accesses"][MemoryAccessIdx]; - if (!JsonMemoryAccess.isMember("relation")) { + const json::Object *JsonMemoryAccess = + JsonAccesses[MemoryAccessIdx].getAsObject(); + assert(JsonMemoryAccess); + if (!JsonMemoryAccess->get("relation")) { errs() << "Memory access number " << MemoryAccessIdx << " has no key name 'relation' for statement number " << StatementIdx << ".\n"; return false; } - Json::Value Accesses = JsonMemoryAccess["relation"]; + StringRef Accesses = JsonMemoryAccess->getString("relation").getValue(); isl_map *NewAccessMap = - isl_map_read_from_str(S.getIslCtx().get(), Accesses.asCString()); + isl_map_read_from_str(S.getIslCtx().get(), Accesses.str().c_str()); // Check whether the access was parsed successfully if (!NewAccessMap) { @@ -533,7 +541,7 @@ importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL, // Statistics. ++NewAccessMapFound; if (NewAccessStrings) - NewAccessStrings->push_back(Accesses.asCString()); + NewAccessStrings->push_back(Accesses); MA->setNewAccessRelation(isl::manage(NewAccessMap)); } else { isl_map_free(NewAccessMap); @@ -548,44 +556,45 @@ importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL, } /// Check whether @p SAI and @p Array represent the same array. -static bool areArraysEqual(ScopArrayInfo *SAI, Json::Value Array) { +static bool areArraysEqual(ScopArrayInfo *SAI, const json::Object &Array) { std::string Buffer; llvm::raw_string_ostream RawStringOstream(Buffer); // Check if key 'type' is present. - if (!Array.isMember("type")) { + if (!Array.get("type")) { errs() << "Array has no key 'type'.\n"; return false; } // Check if key 'sizes' is present. - if (!Array.isMember("sizes")) { + if (!Array.get("sizes")) { errs() << "Array has no key 'sizes'.\n"; return false; } // Check if key 'name' is present. - if (!Array.isMember("name")) { + if (!Array.get("name")) { errs() << "Array has no key 'name'.\n"; return false; } - if (SAI->getName() != Array["name"].asCString()) + if (SAI->getName() != Array.getString("name").getValue()) return false; - if (SAI->getNumberOfDimensions() != Array["sizes"].size()) + if (SAI->getNumberOfDimensions() != Array.getArray("sizes")->size()) return false; - for (unsigned i = 1; i < Array["sizes"].size(); i++) { + for (unsigned i = 1; i < Array.getArray("sizes")->size(); i++) { SAI->getDimensionSize(i)->print(RawStringOstream); - if (RawStringOstream.str() != Array["sizes"][i].asCString()) + const json::Array &SizesArray = *Array.getArray("sizes"); + if (RawStringOstream.str() != SizesArray[i].getAsString().getValue()) return false; Buffer.clear(); } // Check if key 'type' differs from the current one or is not valid. SAI->getElementType()->print(RawStringOstream); - if (RawStringOstream.str() != Array["type"].asCString()) { + if (RawStringOstream.str() != Array.getString("type").getValue()) { errs() << "Array has not a valid type.\n"; return false; } @@ -631,8 +640,10 @@ static Type *parseTextType(const std::string &TypeTextRepresentation, /// @param JScop The JScop file describing new arrays. /// /// @returns True if the import succeeded, otherwise False. -static bool importArrays(Scop &S, Json::Value &JScop) { - Json::Value Arrays = JScop["arrays"]; +static bool importArrays(Scop &S, const json::Object &JScop) { + if (!JScop.get("arrays")) + return true; + const json::Array &Arrays = *JScop.getArray("arrays"); if (Arrays.size() == 0) return true; @@ -644,7 +655,7 @@ static bool importArrays(Scop &S, Json::Value &JScop) { errs() << "Not enough array entries in JScop file.\n"; return false; } - if (!areArraysEqual(SAI, Arrays[ArrayIdx])) { + if (!areArraysEqual(SAI, *Arrays[ArrayIdx].getAsObject())) { errs() << "No match for array '" << SAI->getName() << "' in JScop.\n"; return false; } @@ -652,16 +663,17 @@ static bool importArrays(Scop &S, Json::Value &JScop) { } for (; ArrayIdx < Arrays.size(); ArrayIdx++) { - auto &Array = Arrays[ArrayIdx]; - auto *ElementType = - parseTextType(Array["type"].asCString(), S.getSE()->getContext()); + const json::Object &Array = *Arrays[ArrayIdx].getAsObject(); + auto *ElementType = parseTextType( + Array.get("type")->getAsString().getValue(), S.getSE()->getContext()); if (!ElementType) { errs() << "Error while parsing element type for new array.\n"; return false; } + const json::Array &SizesArray = *Array.getArray("sizes"); std::vector<unsigned> DimSizes; - for (unsigned i = 0; i < Array["sizes"].size(); i++) { - auto Size = std::stoi(Array["sizes"][i].asCString()); + for (unsigned i = 0; i < SizesArray.size(); i++) { + auto Size = std::stoi(SizesArray[i].getAsString().getValue()); // Check if the size if positive. if (Size <= 0) { @@ -672,11 +684,11 @@ static bool importArrays(Scop &S, Json::Value &JScop) { DimSizes.push_back(Size); } - auto NewSAI = - S.createScopArrayInfo(ElementType, Array["name"].asCString(), DimSizes); + auto NewSAI = S.createScopArrayInfo( + ElementType, Array.getString("name").getValue(), DimSizes); - if (Array.isMember("allocation")) { - NewSAI->setIsOnHeap(Array["allocation"].asString() == "heap"); + if (Array.get("allocation")) { + NewSAI->setIsOnHeap(Array.getString("allocation").getValue() == "heap"); } } @@ -708,15 +720,15 @@ static bool importScop(Scop &S, const Dependences &D, const DataLayout &DL, return false; } - Json::Reader reader; - Json::Value jscop; - - bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop); + Expected<json::Value> ParseResult = + json::parse(result.get().get()->getBuffer()); - if (!parsingSuccessful) { + if (!ParseResult) { + ParseResult.takeError(); errs() << "JSCoP file could not be parsed\n"; return false; } + json::Object &jscop = *ParseResult.get().getAsObject(); bool Success = importContext(S, jscop); |