Skip to content

Commit

Permalink
replace nlohmann json with llvm json
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Nov 17, 2024
1 parent e16313b commit 3e2d218
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
36 changes: 28 additions & 8 deletions include/circt/Dialect/Calyx/CalyxLoweringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/JSON.h"

#include <variant>

namespace circt {
namespace calyx {

using json = nlohmann::ordered_json;

void appendPortsForExternalMemref(PatternRewriter &rewriter, StringRef memName,
Value memref, unsigned memoryID,
SmallVectorImpl<calyx::PortInfo> &inPorts,
Expand Down Expand Up @@ -429,18 +428,36 @@ class ComponentLoweringStateInterface {
return builder.create<TLibraryOp>(loc, getUniqueName(name), resTypes);
}

json &getExtMemData() { return extMemData; }
llvm::json::Value &getExtMemData() { return extMemData; }

const llvm::json::Value &getExtMemData() const { return extMemData; }

const json &getExtMemData() const { return extMemData; }
void setDataField(StringRef name, llvm::json::Array data) {
auto *extMemDataObj = extMemData.getAsObject();
assert(extMemDataObj && "extMemData should be an object");

void setDataField(StringRef name, const json::array_t &data) {
extMemData[name]["data"] = data;
auto &value = (*extMemDataObj)[name.str()];
llvm::json::Object *obj = value.getAsObject();
if (!obj) {
value = llvm::json::Object{};
obj = value.getAsObject();
}
(*obj)["data"] = llvm::json::Value(std::move(data));
}

void setFormat(StringRef name, std::string numType, bool isSigned,
unsigned width) {
extMemData[name]["format"] = {
{"numeric_type", numType}, {"is_signed", true}, {"width", width}};
auto *extMemDataObj = extMemData.getAsObject();
assert(extMemDataObj && "extMemData should be an object");

auto &value = (*extMemDataObj)[name.str()];
llvm::json::Object *obj = value.getAsObject();
if (!obj) {
value = llvm::json::Object{};
obj = value.getAsObject();
}
(*obj)["format"] = llvm::json::Object{
{"numeric_type", numType}, {"is_signed", isSigned}, {"width", width}};
}

private:
Expand Down Expand Up @@ -479,6 +496,9 @@ class ComponentLoweringStateInterface {

/// A mapping between the callee and the instance.
llvm::StringMap<calyx::InstanceOp> instanceMap;

/// A json file to store external global memory data
llvm::json::Value extMemData;
};

/// An interface for conversion passes that lower Calyx programs. This handles
Expand Down
63 changes: 48 additions & 15 deletions lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include "mlir/Support/LogicalResult.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/LogicalResult.h"
#include "llvm/Support/raw_os_ostream.h"
#include <filesystem>
#include <fstream>

#include <variant>

Expand All @@ -47,8 +49,6 @@ namespace circt {
class ComponentLoweringStateInterface;
namespace scftocalyx {

using json = nlohmann::ordered_json;

//===----------------------------------------------------------------------===//
// Utility types
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -302,6 +302,22 @@ class BuildOpGroups : public calyx::FuncOpPartialLoweringPattern {
: WalkResult::interrupt();
});

if (writeJson) {
if (auto fileLoc = dyn_cast<mlir::FileLineColLoc>(funcOp->getLoc())) {
std::filesystem::path path(fileLoc.getFilename().str());
auto outFileName = path.parent_path().append("data.json");
std::ofstream outFile(outFileName);

if (outFile.is_open()) {
llvm::raw_os_ostream llvmOut(outFile);
llvm::json::OStream JOS(llvmOut, 2);
JOS.value(getState<ComponentLoweringState>().getExtMemData());
outFile.close();
} else
llvm::errs() << "Unable to open file for writing\n";
}
}

return success(opBuiltSuccessfully);
}

Expand Down Expand Up @@ -704,6 +720,29 @@ LogicalResult BuildOpGroups::buildOp(PatternRewriter &rewriter,
addFN.getOut());
}

void insertNestedArrayValue(llvm::json::Array &array,
const std::vector<int> &indices, size_t index,
llvm::json::Value value) {
if (index == indices.size() - 1) {
// ensure the array is big enough
while (array.size() <= static_cast<size_t>(indices[index])) {
array.emplace_back(nullptr);
}
array[indices[index]] = std::move(value);
return;
}
// ensure the array is big enough
while (array.size() <= static_cast<size_t>(indices[index])) {
array.emplace_back(llvm::json::Array{});
}
llvm::json::Value &val = array[indices[index]];
if (!val.getAsArray()) {
val = llvm::json::Array{};
}
insertNestedArrayValue(*val.getAsArray(), indices, index + 1,
std::move(value));
}

template <typename TAllocOp>
static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
PatternRewriter &rewriter, TAllocOp allocOp) {
Expand Down Expand Up @@ -755,7 +794,7 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
return indices;
};

json result = json::array();
llvm::json::Array result;
if (isa<memref::GetGlobalOp>(allocOp)) {
auto getGlobalOp = cast<memref::GetGlobalOp>(allocOp);
auto *symbolTableOp =
Expand All @@ -779,17 +818,11 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
// Put the flattened values in the multi-dimensional structure
for (size_t i = 0; i < flattenedVals.size(); ++i) {
std::vector<int> indices = getIndices(i);
json *nested = &result;
for (size_t j = 0; j < indices.size() - 1; ++j) {
while (nested->size() <= static_cast<json::size_type>(indices[j])) {
nested->push_back(json::array());
}
nested = &(*nested)[indices[j]];
}
if (isFloat)
nested->push_back(flattenedVals[i]);
else
nested->push_back(static_cast<int64_t>(flattenedVals[i]));
llvm::json::Value value =
isFloat ? llvm::json::Value(flattenedVals[i])
: llvm::json::Value(static_cast<int64_t>(flattenedVals[i]));

insertNestedArrayValue(result, indices, 0, std::move(value));
}

componentState.setDataField(memoryOp.getName(), result);
Expand Down
2 changes: 1 addition & 1 deletion lib/Dialect/Calyx/Transforms/CalyxLoweringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ BasicLoopInterface::~BasicLoopInterface() = default;

ComponentLoweringStateInterface::ComponentLoweringStateInterface(
calyx::ComponentOp component)
: component(component) {}
: component(component), extMemData(llvm::json::Object{}) {}

ComponentLoweringStateInterface::~ComponentLoweringStateInterface() = default;

Expand Down

0 comments on commit 3e2d218

Please sign in to comment.