brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · b60d711 Raw
51 lines · cpp
1//===- ReconcileUnrealizedCasts.cpp - Eliminate noop unrealized casts -----===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"10 11#include "mlir/IR/BuiltinOps.h"12#include "mlir/Pass/Pass.h"13#include "mlir/Transforms/DialectConversion.h"14 15namespace mlir {16#define GEN_PASS_DEF_RECONCILEUNREALIZEDCASTSPASS17#include "mlir/Conversion/Passes.h.inc"18} // namespace mlir19 20using namespace mlir;21 22namespace {23 24/// Pass to simplify and eliminate unrealized conversion casts.25///26/// This pass processes unrealized_conversion_cast ops in a worklist-driven27/// fashion. For each matched cast op, if the chain of input casts eventually28/// reaches a cast op where the input types match the output types of the29/// matched op, replace the matched op with the inputs.30///31/// Example:32/// %1 = unrealized_conversion_cast %0 : !A to !B33/// %2 = unrealized_conversion_cast %1 : !B to !C34/// %3 = unrealized_conversion_cast %2 : !C to !A35///36/// In the above example, %0 can be used instead of %3 and all cast ops are37/// folded away.38struct ReconcileUnrealizedCasts39    : public impl::ReconcileUnrealizedCastsPassBase<ReconcileUnrealizedCasts> {40  ReconcileUnrealizedCasts() = default;41 42  void runOnOperation() override {43    SmallVector<UnrealizedConversionCastOp> ops;44    getOperation()->walk(45        [&](UnrealizedConversionCastOp castOp) { ops.push_back(castOp); });46    reconcileUnrealizedCasts(ops);47  }48};49 50} // namespace51