#include "mlir/Pass/Pass.h"

#include "Common/PimCommon.hpp"
#include "Compiler/PimCodeGen.hpp"

using namespace mlir;

namespace onnx_mlir {

namespace {

struct EmitPimJsonPass : PassWrapper<EmitPimJsonPass, OperationPass<ModuleOp>> {
  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(EmitPimJsonPass);
  StringRef getArgument() const override { return "emit-pim-json-pass"; }
  StringRef getDescription() const override { return "Emit json code for the pim simulators"; }

  EmitPimJsonPass() {}
  EmitPimJsonPass(const EmitPimJsonPass& pass) {}

  void runOnOperation() override {
    ModuleOp moduleOp = getOperation();

    std::string pimDir = getOutputDir() + "/pim";
    createDirectory(pimDir);

    int compiler_error_code = compileToPimJson(moduleOp, pimDir);
    if (compiler_error_code != CompilerSuccess)
      signalPassFailure();
  }
};

} // namespace

std::unique_ptr<Pass> createEmitPimJsonPass() { return std::make_unique<EmitPimJsonPass>(); }

} // namespace onnx_mlir
