// Copyright 2020 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <memory>
#include <utility>

#include "iree/compiler/Dialect/HAL/IR/HALDialect.h"
#include "iree/compiler/Dialect/HAL/IR/HALOps.h"
#include "iree/compiler/Dialect/HAL/Target/TargetBackend.h"
#include "iree/compiler/Dialect/HAL/Target/TargetRegistry.h"
#include "iree/compiler/Dialect/HAL/Transforms/Passes.h"
#include "iree/compiler/Utils/PassUtils.h"
#include "iree/compiler/Utils/TracingUtils.h"
#include "llvm/ADT/StringSet.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"

namespace mlir::iree_compiler::IREE::HAL {

#define GEN_PASS_DEF_TRANSLATEALLEXECUTABLESPASS
#define GEN_PASS_DEF_TRANSLATETARGETEXECUTABLEVARIANTSPASS
#include "iree/compiler/Dialect/HAL/Transforms/Passes.h.inc"

namespace {

//===----------------------------------------------------------------------===//
// --iree-hal-translate-target-executable-variants
//===----------------------------------------------------------------------===//

struct TranslateTargetExecutableVariantsPass
    : IREE::HAL::impl::TranslateTargetExecutableVariantsPassBase<
          TranslateTargetExecutableVariantsPass> {
  using IREE::HAL::impl::TranslateTargetExecutableVariantsPassBase<
      TranslateTargetExecutableVariantsPass>::
      TranslateTargetExecutableVariantsPassBase;

  // Constructor that also accepts a shared pipeline cache.
  TranslateTargetExecutableVariantsPass(
      TranslateTargetExecutableVariantsPassOptions options,
      std::shared_ptr<PipelineCache> cache)
      : TranslateTargetExecutableVariantsPassBase(std::move(options)),
        pipelineCache(std::move(cache)) {}

  // Shared across clones of this pass for thread-safe pipeline caching.
  std::shared_ptr<PipelineCache> pipelineCache;

  void getDependentDialects(DialectRegistry &registry) const override {
    registry.insert<IREE::HAL::HALDialect>();
    registry.insert<bufferization::BufferizationDialect>();
    auto targetBackend = targetRegistry->getTargetBackend(target);
    if (targetBackend) {
      targetBackend->getDependentDialects(registry);
    }
  }

  void runOnOperation() override {
    IREE::HAL::ExecutableVariantOp variantOp = getOperation();
    if (variantOp.getTarget().getBackend().getValue() != target) {
      return;
    }
    if (variantOp.isExternal()) {
      return;
    }

    auto targetBackend = targetRegistry->getTargetBackend(target);
    if (!targetBackend) {
      emitError(variantOp->getLoc())
          << "unregistered target backend '" << target << "'";
      return signalPassFailure();
    }

    // Build or retrieve the cached pass pipeline for this target attribute.
    // When many executables share the same target, this avoids redundantly
    // reconstructing the same pipeline for each one.
    IREE::HAL::ExecutableTargetAttr targetAttr = variantOp.getTargetAttr();
    OpPassManager passManager(variantOp.getOperationName());
    if (pipelineCache) {
      passManager = pipelineCache->getOrCreate(
          targetAttr, variantOp.getOperationName(), [&](OpPassManager &pm) {
            targetBackend->buildTranslationPassPipeline(targetAttr, pm);
          });
    } else {
      // Fallback for standalone pass usage (e.g., iree-opt).
      targetBackend->buildTranslationPassPipeline(targetAttr, passManager);
    }

    if (failed(runPipeline(passManager, variantOp))) {
      emitError(variantOp->getLoc())
          << "failed to run translation of source executable to target "
             "executable for backend "
          << variantOp.getTarget();
      return signalPassFailure();
    }
  }
};

//===----------------------------------------------------------------------===//
// --iree-hal-translate-all-executables
//===----------------------------------------------------------------------===//

struct TranslateAllExecutablesPass
    : IREE::HAL::impl::TranslateAllExecutablesPassBase<
          TranslateAllExecutablesPass> {
  using IREE::HAL::impl::TranslateAllExecutablesPassBase<
      TranslateAllExecutablesPass>::TranslateAllExecutablesPassBase;

  // Shared across all clones of this pass for thread-safe pipeline caching.
  // When MLIR clones this pass for parallel execution on different
  // ExecutableOps, the shared_ptr is copied so all clones share the same
  // cache.
  std::shared_ptr<PipelineCache> pipelineCache =
      std::make_shared<PipelineCache>();

  void getDependentDialects(DialectRegistry &registry) const override {
    registry.insert<IREE::HAL::HALDialect>();
    registry.insert<bufferization::BufferizationDialect>();
    auto targetBackends = targetRegistry->getTargetBackends(
        targetRegistry->getRegisteredTargetBackends());
    for (auto &targetBackend : targetBackends) {
      targetBackend->getDependentDialects(registry);
    }
  }

  void runOnOperation() override {
    IREE::HAL::ExecutableOp executableOp = getOperation();
    OpPassManager passManager(executableOp.getOperationName());
    for (const auto &targetName : gatherExecutableTargetNames(executableOp)) {
      passManager.addNestedPass<IREE::HAL::ExecutableVariantOp>(
          std::make_unique<TranslateTargetExecutableVariantsPass>(
              TranslateTargetExecutableVariantsPassOptions{targetRegistry,
                                                           targetName},
              pipelineCache));
    }

    IREE_COMPILER_TRACE_MESSAGE_DYNAMIC(INFO, executableOp.getSymName().str());

    if (failed(runPipeline(passManager, executableOp))) {
      return signalPassFailure();
    }
  }
};

} // namespace

} // namespace mlir::iree_compiler::IREE::HAL
