Skip to content

Commit

Permalink
change writeJson from bool to json file name; add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahanxie353 committed Dec 3, 2024
1 parent af62f75 commit 8b314cd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def SCFToCalyx : Pass<"lower-scf-to-calyx", "mlir::ModuleOp"> {
" of the Calyx program.">,
Option<"ciderSourceLocationMetadata", "cider-source-location-metadata", "bool", "",
"Whether to track source location for the Cider debugger.">,
Option<"writeJsonOpt", "write-json", "bool", "",
Option<"writeJsonOpt", "write-json", "std::string", "",
"Whether to write memory contents to the json file.">
];
}
Expand Down
15 changes: 10 additions & 5 deletions lib/Conversion/SCFToCalyx/SCFToCalyx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class BuildOpGroups : public calyx::FuncOpPartialLoweringPattern {
calyx::PatternApplicationState &patternState,
DenseMap<mlir::func::FuncOp, calyx::ComponentOp> &map,
calyx::CalyxLoweringState &state,
mlir::Pass::Option<bool> &writeJsonOpt)
mlir::Pass::Option<std::string> &writeJsonOpt)
: FuncOpPartialLoweringPattern(context, resRef, patternState, map, state),
writeJson(writeJsonOpt) {}
using FuncOpPartialLoweringPattern::FuncOpPartialLoweringPattern;
Expand Down Expand Up @@ -316,10 +316,15 @@ class BuildOpGroups : public calyx::FuncOpPartialLoweringPattern {
: WalkResult::interrupt();
});

if (writeJson) {
if (!writeJson.empty()) {
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::string filename = fileLoc.getFilename().str();
// Hack: remove unwanted prefixes during testing
if (filename.rfind("within split at", 0) == 0)
filename = filename.substr(strlen("within split at "));
std::filesystem::path path(filename);
std::string jsonFileName = writeJson.append(".json");
auto outFileName = path.parent_path().append(jsonFileName);
std::ofstream outFile(outFileName);

if (outFile.is_open()) {
Expand All @@ -339,7 +344,7 @@ class BuildOpGroups : public calyx::FuncOpPartialLoweringPattern {
}

private:
mlir::Pass::Option<bool> &writeJson;
mlir::Pass::Option<std::string> &writeJson;
/// Op builder specializations.
LogicalResult buildOp(PatternRewriter &rewriter, scf::YieldOp yieldOp) const;
LogicalResult buildOp(PatternRewriter &rewriter,
Expand Down
65 changes: 65 additions & 0 deletions test/Conversion/SCFToCalyx/write_memory.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// RUN: circt-opt %s --lower-scf-to-calyx="write-json=data" -canonicalize -split-input-file>/dev/null && cat ./data.json | FileCheck %s

// CHECK-LABEL: "mem_0": {
// CHECK: "data": [
// CHECK: 0,
// CHECK: 0,
// CHECK: 0,
// CHECK: 0
// CHECK: ],
// CHECK: "format": {
// CHECK: "is_signed": true,
// CHECK: "numeric_type": "ieee754_float",
// CHECK: "width": 32
// CHECK: }
// CHECK: },

// CHECK-LABEL: "mem_1": {
// CHECK: "data": [
// CHECK: 43,
// CHECK: 8,
// CHECK: -39,
// CHECK: -19,
// CHECK: 70,
// CHECK: -64,
// CHECK: -7,
// CHECK: -27,
// CHECK: -57,
// CHECK: 5
// CHECK: ],
// CHECK: "format": {
// CHECK: "is_signed": false,
// CHECK: "numeric_type": "bitnum",
// CHECK: "width": 32
// CHECK: }
// CHECK: },

// CHECK-LABEL: "mem_2": {
// CHECK: "data": [
// CHECK: 0.69999998807907104,
// CHECK: -4.1999998092651367,
// CHECK: 0
// CHECK: ],
// CHECK: "format": {
// CHECK: "is_signed": true,
// CHECK: "numeric_type": "ieee754_float",
// CHECK: "width": 32
// CHECK: }
// CHECK: }

module {
memref.global "private" constant @constant_10xi32_0 : memref<10xi32> = dense<[43, 8, -39, -19, 70, -64, -7, -27, -57, 5]>
memref.global "private" constant @constant_3xf32 : memref<3xf32> = dense<[0.7, -4.2, 0.0]>
func.func @main(%arg_idx : index) -> i32 {
%alloc = memref.alloc() : memref<4xf32>
%c2 = arith.constant 2 : index
%c1 = arith.constant 1 : index
%0 = memref.get_global @constant_10xi32_0 : memref<10xi32>
%ret = memref.load %0[%arg_idx] : memref<10xi32>
%1 = memref.get_global @constant_3xf32 : memref<3xf32>
%2 = memref.load %1[%c1] : memref<3xf32>
memref.store %2, %alloc[%c2] : memref<4xf32>
return %ret : i32
}
}

0 comments on commit 8b314cd

Please sign in to comment.