1 //===- ReconcileUnrealizedCasts.cpp - Eliminate noop unrealized casts -----===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
11 #include "mlir/IR/BuiltinOps.h"
12 #include "mlir/Pass/Pass.h"
13 #include "mlir/Transforms/DialectConversion.h"
16 #define GEN_PASS_DEF_RECONCILEUNREALIZEDCASTS
17 #include "mlir/Conversion/Passes.h.inc"
24 /// Pass to simplify and eliminate unrealized conversion casts.
26 /// This pass processes unrealized_conversion_cast ops in a worklist-driven
27 /// fashion. For each matched cast op, if the chain of input casts eventually
28 /// reaches a cast op where the input types match the output types of the
29 /// matched op, replace the matched op with the inputs.
32 /// %1 = unrealized_conversion_cast %0 : !A to !B
33 /// %2 = unrealized_conversion_cast %1 : !B to !C
34 /// %3 = unrealized_conversion_cast %2 : !C to !A
36 /// In the above example, %0 can be used instead of %3 and all cast ops are
38 struct ReconcileUnrealizedCasts
39 : public impl::ReconcileUnrealizedCastsBase
<ReconcileUnrealizedCasts
> {
40 ReconcileUnrealizedCasts() = default;
42 void runOnOperation() override
{
43 SmallVector
<UnrealizedConversionCastOp
> ops
;
45 [&](UnrealizedConversionCastOp castOp
) { ops
.push_back(castOp
); });
46 reconcileUnrealizedCasts(ops
);
52 std::unique_ptr
<Pass
> mlir::createReconcileUnrealizedCastsPass() {
53 return std::make_unique
<ReconcileUnrealizedCasts
>();