// Copyright (c) 2021 CINN Authors. All Rights Reserved. // // Licensed 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 "paddle/cinn/optim/cast_simplify.h" #include "paddle/cinn/ir/utils/ir_mutator.h" namespace cinn::optim { using cinn::common::bfloat16; using cinn::common::float16; namespace { template CastType NormCastValue(T value) { if (type_of().is_uint() || type_of().is_uint()) { // not support uint return static_cast(value); } if (std::isinf(value)) { return std::numeric_limits::infinity(); } else if (std::isnan(value)) { return std::numeric_limits::signaling_NaN(); } else if (value >= static_cast(std::numeric_limits::max())) { return std::numeric_limits::max(); } else if (value <= static_cast(std::numeric_limits::lowest())) { return std::numeric_limits::lowest(); } return static_cast(value); } struct Mutator : ir::IRMutator<> { using ir::IRMutator<>::Visit; void Visit(const ir::Cast* op, Expr* expr) { auto* node = expr->As(); Visit(&node->v(), &node->v()); if (op->type() == op->v().type()) { *expr = op->v(); return; } #define __CAST_TO_TYPE(type__) \ if (auto* i = op->v().As()) { \ *expr = Expr(static_cast(i->value)); \ } else if (auto* f = op->v().As()) { \ *expr = Expr(static_cast(NormCastValue(f->value))); \ } else if (auto* u = op->v().As()) { \ *expr = Expr(static_cast(u->value)); \ } else { \ CINN_NOT_IMPLEMENTED \ } if (op->v().is_constant()) { if (op->type() == type_of()) { __CAST_TO_TYPE(int8_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(int16_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(int32_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(int64_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint8_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint16_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint32_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint64_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(float) } else if (op->type() == type_of()) { __CAST_TO_TYPE(double) } else if (op->type() == type_of()) { __CAST_TO_TYPE(bool) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint32_t) } else if (op->type() == type_of()) { __CAST_TO_TYPE(uint64_t) } else if (op->type() == type_of()) { // Cannot simplify!!! pass __CAST_TO_TYPE(bfloat16) } else if (op->type() == type_of()) { // Cannot simplify!!! pass __CAST_TO_TYPE(float16) } else { CINN_NOT_IMPLEMENTED } } #undef __CAST_TO_TYPE } }; } // namespace void CastSimplify(Expr* e) { Mutator mutator; mutator.Visit(e, e); } } // namespace cinn::optim