// Copyright 2025 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 "iree/compiler/Transforms/Passes.h"
#include "mlir/Dialect/Affine/Transforms/Transforms.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

#define DEBUG_TYPE "iree-affine-expand-index-ops"

namespace mlir::iree_compiler {

#define GEN_PASS_DEF_IREEAFFINEEXPANDINDEXOPSPASS
#include "iree/compiler/Transforms/Passes.h.inc"

namespace {

struct IREEAffineExpandIndexOpsPass final
    : impl::IREEAffineExpandIndexOpsPassBase<IREEAffineExpandIndexOpsPass> {
public:
  using Base::Base;

  void runOnOperation() override {
    GreedyRewriteConfig config;
    config.setRegionSimplificationLevel(GreedySimplifyRegionLevel::Normal);

    RewritePatternSet patterns(&getContext());
    affine::populateAffineExpandIndexOpsPatterns(patterns);
    if (failed(applyPatternsGreedily(getOperation(), std::move(patterns),
                                     config))) {
      return signalPassFailure();
    }
  }
};

} // namespace
} // namespace mlir::iree_compiler
