* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "ir/transforms/base/mutator.h"
#include <any>
#include <cstddef>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "core/dtype.h"
#include "core/logging.h"
#include "ir/core.h"
#include "ir/expr.h"
#include "ir/function.h"
#include "ir/kind_traits.h"
#include "ir/memref.h"
#include "ir/program.h"
#include "ir/scalar_expr.h"
#include "ir/span.h"
#include "ir/stmt.h"
#include "ir/transforms/base/functor.h"
#include "ir/type.h"
#include "interface/tensor/ir_tensor_op_rebuild.h"
namespace pypto {
namespace ir {
namespace {
ExprPtr ReconstructBinaryExpr(ObjectKind kind, ExprPtr left, ExprPtr right, DataType dtype, const Span& span)
{
switch (kind) {
case ObjectKind::Add:
return std::make_shared<const Add>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Sub:
return std::make_shared<const Sub>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Mul:
return std::make_shared<const Mul>(std::move(left), std::move(right), dtype, span);
case ObjectKind::FloorDiv:
return std::make_shared<const FloorDiv>(std::move(left), std::move(right), dtype, span);
case ObjectKind::FloorMod:
return std::make_shared<const FloorMod>(std::move(left), std::move(right), dtype, span);
case ObjectKind::FloatDiv:
return std::make_shared<const FloatDiv>(std::move(left), std::move(right), dtype, span);
case ObjectKind::CeilDiv:
return std::make_shared<const CeilDiv>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Min:
return std::make_shared<const Min>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Max:
return std::make_shared<const Max>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Pow:
return std::make_shared<const Pow>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Eq:
return std::make_shared<const Eq>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Ne:
return std::make_shared<const Ne>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Lt:
return std::make_shared<const Lt>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Le:
return std::make_shared<const Le>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Gt:
return std::make_shared<const Gt>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Ge:
return std::make_shared<const Ge>(std::move(left), std::move(right), dtype, span);
case ObjectKind::And:
return std::make_shared<const And>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Or:
return std::make_shared<const Or>(std::move(left), std::move(right), dtype, span);
case ObjectKind::Xor:
return std::make_shared<const Xor>(std::move(left), std::move(right), dtype, span);
case ObjectKind::BitAnd:
return std::make_shared<const BitAnd>(std::move(left), std::move(right), dtype, span);
case ObjectKind::BitOr:
return std::make_shared<const BitOr>(std::move(left), std::move(right), dtype, span);
case ObjectKind::BitXor:
return std::make_shared<const BitXor>(std::move(left), std::move(right), dtype, span);
case ObjectKind::BitShiftLeft:
return std::make_shared<const BitShiftLeft>(std::move(left), std::move(right), dtype, span);
case ObjectKind::BitShiftRight:
return std::make_shared<const BitShiftRight>(std::move(left), std::move(right), dtype, span);
default:
INTERNAL_CHECK_SPAN(false, span) << "Unknown binary expression kind in ReconstructBinaryExpr";
}
}
ExprPtr ReconstructUnaryExpr(ObjectKind kind, ExprPtr operand, DataType dtype, const Span& span)
{
switch (kind) {
case ObjectKind::Abs:
return std::make_shared<const Abs>(std::move(operand), dtype, span);
case ObjectKind::Neg:
return std::make_shared<const Neg>(std::move(operand), dtype, span);
case ObjectKind::Not:
return std::make_shared<const Not>(std::move(operand), dtype, span);
case ObjectKind::BitNot:
return std::make_shared<const BitNot>(std::move(operand), dtype, span);
case ObjectKind::Cast:
return std::make_shared<const Cast>(std::move(operand), dtype, span);
case ObjectKind::Ceil:
return std::make_shared<const Ceil>(std::move(operand), dtype, span);
case ObjectKind::Floor:
return std::make_shared<const Floor>(std::move(operand), dtype, span);
default:
INTERNAL_CHECK_SPAN(false, span) << "Unknown unary expression kind in ReconstructUnaryExpr";
}
}
VarPtr AsVar(const ExprPtr& expr, const Span& span)
{
if (auto var = As<Var>(expr)) {
return var;
}
if (auto memref = As<MemRef>(expr)) {
return std::static_pointer_cast<const Var>(memref);
}
INTERNAL_CHECK_SPAN(false, span) << "invalid var expression";
return nullptr;
}
ExprPtr ReconstructCallWithKwargs(const std::string& name, std::vector<ExprPtr> args,
const std::vector<std::pair<std::string, std::any>>& kwargs, const TypePtr& type,
const Span& span)
{
return std::make_shared<Call>(name, std::move(args), kwargs, type, span);
}
}
ProgramPtr IRMutator::VisitProgram(const ProgramPtr& program)
{
std::vector<FunctionPtr> newFunctions;
bool changed = false;
for (const auto& entry : program->functions_) {
auto newFunc = VisitFunction(entry.second);
newFunctions.emplace_back(newFunc);
changed = changed || (newFunc != entry.second);
}
if (!changed) {
return program;
}
return std::make_shared<const Program>(std::move(newFunctions), program->name_, program->span_,
program->debugInfo_);
}
FunctionPtr IRMutator::VisitFunction(const FunctionPtr& func)
{
auto newBody = VisitStmt(func->body_);
if (newBody.get() == func->body_.get()) {
return func;
}
return std::make_shared<const Function>(func->name_, func->params_, func->returnTypes_, std::move(newBody),
func->span_, func->funcType_, func->entry_, func->attrs_);
}
ExprPtr IRMutator::VisitExpr(const ExprPtr& expr) { return ExprFunctor<ExprPtr>::VisitExpr(expr); }
StmtPtr IRMutator::VisitStmt(const StmtPtr& stmt) { return StmtFunctor<StmtPtr>::VisitStmt(stmt); }
std::pair<std::vector<ExprPtr>, bool> IRMutator::VisitExprList(const std::vector<ExprPtr>& exprs)
{
std::vector<ExprPtr> out;
bool changed = false;
out.reserve(exprs.size());
for (size_t i = 0; i < exprs.size(); ++i) {
auto ne = ExprFunctor<ExprPtr>::VisitExpr(exprs[i]);
out.push_back(ne);
changed = changed || (ne.get() != exprs[i].get());
}
return {std::move(out), changed};
}
std::pair<std::vector<VarPtr>, bool> IRMutator::VisitVarList(const std::vector<VarPtr>& vars)
{
std::vector<VarPtr> out;
bool changed = false;
out.reserve(vars.size());
for (size_t i = 0; i < vars.size(); ++i) {
auto nv = ExprFunctor<ExprPtr>::VisitExpr(vars[i]);
out.push_back(AsVar(nv, vars[i]->span_));
changed = changed || (nv.get() != vars[i].get());
}
return {std::move(out), changed};
}
std::pair<std::vector<IterArgPtr>, bool> IRMutator::VisitIterArgList(const std::vector<IterArgPtr>& iterArgs)
{
std::vector<IterArgPtr> out;
bool changed = false;
out.reserve(iterArgs.size());
for (size_t i = 0; i < iterArgs.size(); ++i) {
auto newInit = ExprFunctor<ExprPtr>::VisitExpr(iterArgs[i]->initValue_);
if (newInit.get() != iterArgs[i]->initValue_.get()) {
out.push_back(std::make_shared<const IterArg>(iterArgs[i]->iterVar_, std::move(newInit)));
changed = true;
} else {
out.push_back(iterArgs[i]);
}
}
for (size_t i = 0; i < iterArgs.size(); ++i) {
if (out[i].get() != iterArgs[i].get()) {
var_remap_[iterArgs[i]->iterVar_.get()] = out[i]->iterVar_;
}
}
return {std::move(out), changed};
}
ExprPtr IRMutator::VisitExpr_(const VarPtr& op)
{
auto it = var_remap_.find(op.get());
if (it != var_remap_.end()) {
return it->second;
}
return op;
}
ExprPtr IRMutator::VisitExpr_(const MemRefPtr& op) { return op; }
ExprPtr IRMutator::VisitExpr_(const ConstIntPtr& op) { return op; }
ExprPtr IRMutator::VisitExpr_(const ConstFloatPtr& op) { return op; }
ExprPtr IRMutator::VisitExpr_(const ConstBoolPtr& op) { return op; }
ExprPtr IRMutator::VisitExpr_(const CallPtr& op)
{
auto [newArgs, changed] = VisitExprList(op->args_);
if (changed) {
return ReconstructCallWithKwargs(op->name_, std::move(newArgs), op->kwargs_, op->GetType(), op->span_);
}
return op;
}
ExprPtr IRMutator::VisitExpr_(const MakeTuplePtr& op)
{
auto [newElements, changed] = VisitExprList(op->elements_);
if (changed) {
return std::make_shared<const MakeTuple>(std::move(newElements), op->span_);
}
return op;
}
ExprPtr IRMutator::VisitExpr_(const GetItemExprPtr& op)
{
INTERNAL_CHECK_SPAN(op->value_, op->span_) << "GetItemExpr has null value";
INTERNAL_CHECK_SPAN(op->slice_, op->span_) << "GetItemExpr has null slice";
auto newValue = ExprFunctor<ExprPtr>::VisitExpr(op->value_);
auto newSlice = ExprFunctor<ExprPtr>::VisitExpr(op->slice_);
if (newValue.get() != op->value_.get() || newSlice.get() != op->slice_.get()) {
return std::make_shared<const GetItemExpr>(std::move(newValue), std::move(newSlice), op->span_);
}
return op;
}
ExprPtr IRMutator::VisitExpr_(const ScalarExprPtr& op) { return op; }
ExprPtr IRMutator::VisitBinaryExpr_(const BinaryExprPtr& op)
{
INTERNAL_CHECK_SPAN(op->left_, op->span_) << "BinaryExpr has null left operand";
INTERNAL_CHECK_SPAN(op->right_, op->span_) << "BinaryExpr has null right operand";
auto newLeft = ExprFunctor<ExprPtr>::VisitExpr(op->left_);
auto newRight = ExprFunctor<ExprPtr>::VisitExpr(op->right_);
if (newLeft.get() != op->left_.get() || newRight.get() != op->right_.get()) {
auto scalarType = As<ScalarType>(op->GetType());
INTERNAL_CHECK_SPAN(scalarType, op->span_) << "BinaryExpr has null type";
return ReconstructBinaryExpr(op->GetKind(), std::move(newLeft), std::move(newRight), scalarType->dtype_,
op->span_);
}
return op;
}
ExprPtr IRMutator::VisitUnaryExpr_(const UnaryExprPtr& op)
{
INTERNAL_CHECK_SPAN(op->operand_, op->span_) << "UnaryExpr has null operand";
auto newOperand = ExprFunctor<ExprPtr>::VisitExpr(op->operand_);
if (newOperand.get() != op->operand_.get()) {
auto scalarType = As<ScalarType>(op->GetType());
INTERNAL_CHECK_SPAN(scalarType, op->span_) << "UnaryExpr has null type";
return ReconstructUnaryExpr(op->GetKind(), std::move(newOperand), scalarType->dtype_, op->span_);
}
return op;
}
#define DEFINE_BINARY_MUTATOR(OpType) \
ExprPtr IRMutator::VisitExpr_(const OpType##Ptr& op) { return VisitBinaryExpr_(op); }
DEFINE_BINARY_MUTATOR(Add)
DEFINE_BINARY_MUTATOR(Sub)
DEFINE_BINARY_MUTATOR(Mul)
DEFINE_BINARY_MUTATOR(FloorDiv)
DEFINE_BINARY_MUTATOR(FloorMod)
DEFINE_BINARY_MUTATOR(FloatDiv)
DEFINE_BINARY_MUTATOR(CeilDiv)
DEFINE_BINARY_MUTATOR(Min)
DEFINE_BINARY_MUTATOR(Max)
DEFINE_BINARY_MUTATOR(Pow)
DEFINE_BINARY_MUTATOR(Eq)
DEFINE_BINARY_MUTATOR(Ne)
DEFINE_BINARY_MUTATOR(Lt)
DEFINE_BINARY_MUTATOR(Le)
DEFINE_BINARY_MUTATOR(Gt)
DEFINE_BINARY_MUTATOR(Ge)
DEFINE_BINARY_MUTATOR(And)
DEFINE_BINARY_MUTATOR(Or)
DEFINE_BINARY_MUTATOR(Xor)
DEFINE_BINARY_MUTATOR(BitAnd)
DEFINE_BINARY_MUTATOR(BitOr)
DEFINE_BINARY_MUTATOR(BitXor)
DEFINE_BINARY_MUTATOR(BitShiftLeft)
DEFINE_BINARY_MUTATOR(BitShiftRight)
#undef DEFINE_BINARY_MUTATOR
#define DEFINE_UNARY_MUTATOR(OpType) \
ExprPtr IRMutator::VisitExpr_(const OpType##Ptr& op) { return VisitUnaryExpr_(op); }
DEFINE_UNARY_MUTATOR(Abs)
DEFINE_UNARY_MUTATOR(Neg)
DEFINE_UNARY_MUTATOR(Not)
DEFINE_UNARY_MUTATOR(BitNot)
DEFINE_UNARY_MUTATOR(Cast)
DEFINE_UNARY_MUTATOR(Ceil)
DEFINE_UNARY_MUTATOR(Floor)
#undef DEFINE_UNARY_MUTATOR
StmtPtr IRMutator::VisitStmt_(const AssignStmtPtr& op)
{
INTERNAL_CHECK_SPAN(op->value_, op->span_) << "AssignStmt has null value";
for (const auto& target : op->targets_) {
INTERNAL_CHECK_SPAN(target, op->span_) << "AssignStmt has null target";
}
auto [newTargets, targetsChanged] = VisitVarList(op->targets_);
auto newValue = ExprFunctor<ExprPtr>::VisitExpr(op->value_);
if (targetsChanged || newValue.get() != op->value_.get()) {
return std::make_shared<const AssignStmt>(std::move(newTargets), std::move(newValue), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const IfStmtPtr& op)
{
INTERNAL_CHECK_SPAN(op->condition_, op->span_) << "IfStmt has null condition";
auto newCondition = ExprFunctor<ExprPtr>::VisitExpr(op->condition_);
auto changed = (newCondition.get() != op->condition_.get());
INTERNAL_CHECK_SPAN(op->thenBody_, op->span_) << "IfStmt has null then_body";
auto newThenBody = StmtFunctor<StmtPtr>::VisitStmt(op->thenBody_);
changed = changed || (newThenBody.get() != op->thenBody_.get());
std::optional<StmtPtr> newElseBody;
if (op->elseBody_.has_value()) {
INTERNAL_CHECK_SPAN(*op->elseBody_, op->span_) << "IfStmt has null else_body";
auto newStmt = StmtFunctor<StmtPtr>::VisitStmt(*op->elseBody_);
newElseBody = newStmt;
changed = changed || (newStmt.get() != op->elseBody_->get());
}
std::vector<VarPtr> newReturnVars;
newReturnVars.reserve(op->returnVars_.size());
for (size_t i = 0; i < op->returnVars_.size(); ++i) {
INTERNAL_CHECK_SPAN(op->returnVars_[i], op->span_) << "IfStmt has null return_vars at index " << i;
auto newVarExpr = ExprFunctor<ExprPtr>::VisitExpr(op->returnVars_[i]);
auto newVar = AsVar(newVarExpr, op->span_);
newReturnVars.push_back(newVar);
changed = changed || (newVar.get() != op->returnVars_[i].get());
}
if (changed) {
if (newElseBody.has_value()) {
return std::make_shared<const IfStmt>(std::move(newCondition), std::move(newThenBody), *newElseBody,
std::move(newReturnVars), op->span_);
} else {
return std::make_shared<const IfStmt>(std::move(newCondition), std::move(newThenBody), std::nullopt,
std::move(newReturnVars), op->span_);
}
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const YieldStmtPtr& op)
{
auto [newValue, changed] = VisitExprList(op->value_);
if (changed) {
return std::make_shared<const YieldStmt>(std::move(newValue), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const ReturnStmtPtr& op)
{
auto [newValue, changed] = VisitExprList(op->value_);
if (changed) {
return std::make_shared<const ReturnStmt>(std::move(newValue), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const ForStmtPtr& op)
{
INTERNAL_CHECK_SPAN(op->loopVar_, op->span_) << "ForStmt has null loop_var";
INTERNAL_CHECK_SPAN(op->start_, op->span_) << "ForStmt has null start";
INTERNAL_CHECK_SPAN(op->stop_, op->span_) << "ForStmt has null stop";
INTERNAL_CHECK_SPAN(op->step_, op->span_) << "ForStmt has null step";
auto newLoopVar = AsVar(ExprFunctor<ExprPtr>::VisitExpr(op->loopVar_), op->span_);
auto changed = (newLoopVar.get() != op->loopVar_.get());
auto newStart = ExprFunctor<ExprPtr>::VisitExpr(op->start_);
auto newStop = ExprFunctor<ExprPtr>::VisitExpr(op->stop_);
auto newStep = ExprFunctor<ExprPtr>::VisitExpr(op->step_);
changed = changed || (newStart.get() != op->start_.get() || newStop.get() != op->stop_.get() ||
newStep.get() != op->step_.get());
auto [newIterArgs, iterChanged] = VisitIterArgList(op->iterArgs_);
changed = changed || iterChanged;
INTERNAL_CHECK_SPAN(op->body_, op->span_) << "ForStmt has null body";
auto newBody = StmtFunctor<StmtPtr>::VisitStmt(op->body_);
changed = changed || (newBody.get() != op->body_.get());
for (const auto& oldIterArg : op->iterArgs_) {
var_remap_.erase(oldIterArg->iterVar_.get());
}
auto [newReturns, returnChanged] = VisitVarList(op->returnVars_);
changed = changed || returnChanged;
if (changed) {
return std::make_shared<const ForStmt>(std::move(newLoopVar), std::move(newStart), std::move(newStop),
std::move(newStep), std::move(newIterArgs), std::move(newBody),
std::move(newReturns), op->span_, op->attrs_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const WhileStmtPtr& op)
{
auto [newIterArgs, changed] = VisitIterArgList(op->iterArgs_);
INTERNAL_CHECK_SPAN(op->condition_, op->span_) << "WhileStmt has null condition";
auto newCondition = ExprFunctor<ExprPtr>::VisitExpr(op->condition_);
changed = changed || (newCondition.get() != op->condition_.get());
INTERNAL_CHECK_SPAN(op->body_, op->span_) << "WhileStmt has null body";
auto newBody = StmtFunctor<StmtPtr>::VisitStmt(op->body_);
changed = changed || (newBody.get() != op->body_.get());
for (const auto& oldIterArg : op->iterArgs_) {
var_remap_.erase(oldIterArg->iterVar_.get());
}
auto [newReturns, returnChanged] = VisitVarList(op->returnVars_);
changed = changed || returnChanged;
if (changed) {
return std::make_shared<const WhileStmt>(std::move(newCondition), std::move(newIterArgs), std::move(newBody),
std::move(newReturns), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const SeqStmtsPtr& op)
{
std::vector<StmtPtr> newStmts;
bool changed = false;
newStmts.reserve(op->stmts_.size());
for (size_t i = 0; i < op->stmts_.size(); ++i) {
INTERNAL_CHECK_SPAN(op->stmts_[i], op->span_) << "SeqStmts has null statement at index " << i;
auto newStmt = StmtFunctor<StmtPtr>::VisitStmt(op->stmts_[i]);
newStmts.push_back(newStmt);
changed = changed || (newStmt.get() != op->stmts_[i].get());
}
if (changed) {
return SeqStmts::Flatten(std::move(newStmts), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const SectionStmtPtr& op)
{
INTERNAL_CHECK_SPAN(op->body_, op->span_) << "SectionStmt has null body";
auto newBody = StmtFunctor<StmtPtr>::VisitStmt(op->body_);
if (newBody.get() != op->body_.get()) {
return std::make_shared<const SectionStmt>(op->sectionKind_, std::move(newBody), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const BreakStmtPtr& op)
{
auto [newValues, changed] = VisitExprList(op->value_);
if (changed) {
return std::make_shared<const BreakStmt>(std::move(newValues), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const ContinueStmtPtr& op)
{
auto [newValues, changed] = VisitExprList(op->value_);
if (changed) {
return std::make_shared<const ContinueStmt>(std::move(newValues), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const ScalarOpStmtPtr& op)
{
INTERNAL_CHECK_SPAN(op->result_, op->span_) << "ScalarOpStmt has null result";
auto newResult = As<Var>(ExprFunctor<ExprPtr>::VisitExpr(op->result_));
auto changed = (newResult.get() != op->result_.get());
INTERNAL_CHECK_SPAN(op->result_token_, op->span_) << "ScalarOpStmt has null result_token";
auto newToken = As<Var>(ExprFunctor<ExprPtr>::VisitExpr(op->result_token_));
changed = changed || (newToken.get() != op->result_token_.get());
auto [newArgs, argsChanged] = VisitExprList(op->args_);
changed = changed || argsChanged;
if (changed) {
return std::make_shared<const ScalarOpStmt>(std::move(newResult), std::move(newToken), std::move(op->opcode_),
std::move(newArgs), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const TensorOpStmtPtr& op)
{
auto [newResults, changed] = VisitVarList(op->result_);
auto [newTokensResult, resultTokensChanged] = VisitVarList(op->result_token_);
changed = changed || resultTokensChanged;
auto [newArgs, argsChanged] = VisitExprList(op->args_);
changed = changed || argsChanged;
auto [newTokens, tokensChanged] = VisitVarList(op->tokens_);
changed = changed || tokensChanged;
if (changed) {
npu::tile_fwk::RemapTensorAccessTokens(op, newResults, newTokensResult, newArgs);
return npu::tile_fwk::RebuildTensorOpStmt(op, std::move(newResults), std::move(newTokensResult),
std::move(newArgs), std::move(newTokens), op->span_);
}
return op;
}
StmtPtr IRMutator::VisitStmt_(const StmtPtr& op) { return op; }
}
}