8 #include <circt/Conversion/ExportVerilog.h>
9 #include <circt/Dialect/FIRRTL/FIRParser.h>
10 #include <circt/Dialect/HW/HWOps.h>
11 #include <circt/Dialect/SV/SVPasses.h>
12 #include <circt/Firtool/Firtool.h>
13 #include <llvm/Support/SourceMgr.h>
14 #include <mlir/IR/BuiltinOps.h>
15 #include <mlir/Support/FileUtilities.h>
20 using namespace circt;
28 mlir::MLIRContext context;
32 std::string errorMessage;
33 auto input = mlir::openInputFile(inputFirrtlFile.
to_str(), &errorMessage);
36 std::cerr << errorMessage << std::endl;
39 llvm::SourceMgr sourceMgr;
40 sourceMgr.AddNewSourceBuffer(std::move(input), llvm::SMLoc());
41 mlir::SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
43 firrtl::FIRParserOptions options;
44 options.infoLocatorHandling = firrtl::FIRParserOptions::InfoLocHandling::IgnoreInfo;
45 options.numAnnotationFiles = 0;
46 options.scalarizePublicModules =
true;
47 options.scalarizeExtModules =
true;
48 auto module = importFIRFile(sourceMgr, &context, ts, options);
51 std::cerr <<
"Failed to parse FIRRTL input" << std::endl;
56 firtool::FirtoolOptions firtoolOptions;
57 firtoolOptions.setOutputFilename(outputVerilogFile.
to_str());
58 firtoolOptions.setPreserveAggregate(firrtl::PreserveAggregate::PreserveMode::None);
59 firtoolOptions.setPreserveValues(firrtl::PreserveValues::PreserveMode::Named);
60 firtoolOptions.setBuildMode(firtool::FirtoolOptions::BuildModeDefault);
61 firtoolOptions.setChiselInterfaceOutDirectory(
"");
62 firtoolOptions.setDisableHoistingHWPassthrough(
true);
63 firtoolOptions.setOmirOutFile(
"");
64 firtoolOptions.setBlackBoxRootPath(
"");
65 firtoolOptions.setReplSeqMemFile(
"");
66 firtoolOptions.setOutputAnnotationFilename(
"");
69 mlir::PassManager pm(&context);
70 if (failed(firtool::populatePreprocessTransforms(pm, firtoolOptions)))
72 std::cerr <<
"Failed to populate preprocess transforms" << std::endl;
78 if (failed(firtool::populateCHIRRTLToLowFIRRTL(pm, firtoolOptions,
"")))
80 std::cerr <<
"Failed to populate CHIRRTL to LowFIRRTL" << std::endl;
83 if (failed(firtool::populateLowFIRRTLToHW(pm, firtoolOptions)))
85 std::cerr <<
"Failed to populate LowFIRRTL to HW" << std::endl;
88 if (failed(firtool::populateHWToSV(pm, firtoolOptions)))
90 std::cerr <<
"Failed to populate HW to SV" << std::endl;
93 std::error_code errorCode;
94 llvm::raw_fd_ostream os(outputVerilogFile.
to_str(), errorCode);
95 if (failed(firtool::populateExportVerilog(pm, firtoolOptions, os)))
97 std::cerr <<
"Failed to populate Export Verilog" << std::endl;
101 if (failed(pm.run(module.get())))
103 std::cerr <<
"Failed to run pass manager" << std::endl;
107 (void)module.release();
static bool Convert(const util::FilePath inputFirrtlFile, const util::FilePath outputVerilogFile)
const std::string & to_str() const noexcept