/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <tvm/ffi/cast.h>
#include <tvm/ffi/function.h>
#include <tvm/ffi/reflection/registry.h>
#include <tvm/ir/transform.h>
#include <tvm/s_tir/stmt.h>
#include <tvm/s_tir/transform.h>
#include <tvm/tirx/builtin.h>
#include <tvm/tirx/expr.h>
#include <tvm/tirx/stmt_functor.h>

namespace tvm {
namespace s_tir {
using namespace tvm::tirx;

class IrregularLoopAnnotator : public StmtMutator {
 public:
  static Stmt Annotate(const Stmt& body) { return IrregularLoopAnnotator().VisitStmt(body); }

 private:
  IrregularLoopAnnotator() = default;

  Stmt VisitStmt_(const ForNode* op) final {
    bool cur_has_jump = has_jump_;
    has_jump_ = false;
    For res = StmtMutator::VisitStmt_(op).as_or_throw<For>();
    if (has_jump_) {
      TVM_FFI_ICHECK(op->kind == ForKind::kSerial)
          << "Loop kind " << op->kind << " is invalid for irregular loop " << op->loop_var;
      for (const char* key :
           {tirx::attr::pragma_auto_unroll_max_step, tirx::attr::pragma_unroll_explicit,
            s_tir::attr::pragma_loop_partition_hint, s_tir::attr::software_pipeline_stage}) {
        TVM_FFI_ICHECK(!res->annotations.count(key))
            << "Annotation `" << key << "` is invalid for irregular loop " << op->loop_var;
      }
      res.CopyOnWrite()->annotations.Set(s_tir::attr::irregular_loop_mark, 1);
    }
    std::swap(cur_has_jump, has_jump_);
    return res;
  }

  Stmt VisitStmt_(const WhileNode* op) final {
    bool cur_has_jump = has_jump_;
    has_jump_ = false;
    Stmt res = StmtMutator::VisitStmt_(op);
    std::swap(cur_has_jump, has_jump_);
    return res;
  }

  Stmt VisitStmt_(const EvaluateNode* op) final {
    if (const CallNode* call = op->value.as<CallNode>()) {
      if (call->op.same_as(builtin::continue_loop()) || call->op.same_as(builtin::break_loop())) {
        has_jump_ = true;
      }
    }
    return ffi::GetRef<Evaluate>(op);
  }

  bool has_jump_{false};
};

namespace transform {

Pass AnnotateIrregularLoop() {
  auto pass_func = [](PrimFunc func, IRModule mod, PassContext ctx) -> PrimFunc {
    func.CopyOnWrite()->body = IrregularLoopAnnotator::Annotate(func->body);
    return func;
  };

  return CreatePrimFuncPass(pass_func, 0, "s_tir.AnnotateIrregularLoop", {});
}

TVM_FFI_STATIC_INIT_BLOCK() {
  namespace refl = tvm::ffi::reflection;
  refl::GlobalDef().def("s_tir.transform.AnnotateIrregularLoop", AnnotateIrregularLoop);
}

}  // namespace transform
}  // namespace s_tir
}  // namespace tvm
