Skip to content

Commit f7686bd

Browse files
[mlir][Transforms] Fix crash in reconcile-unrealized-casts
1 parent 23302a2 commit f7686bd

File tree

6 files changed

+163
-42
lines changed

6 files changed

+163
-42
lines changed

mlir/include/mlir/Transforms/DialectConversion.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,9 @@ struct ConversionConfig {
14211421
///
14221422
/// In the above example, %0 can be used instead of %3 and all cast ops are
14231423
/// folded away.
1424+
void reconcileUnrealizedCasts(
1425+
const DenseSet<UnrealizedConversionCastOp> &castOps,
1426+
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps = nullptr);
14241427
void reconcileUnrealizedCasts(
14251428
ArrayRef<UnrealizedConversionCastOp> castOps,
14261429
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps = nullptr);

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,7 @@ unsigned OperationLegalizer::applyCostModelToPatterns(
31003100
//===----------------------------------------------------------------------===//
31013101
// OperationConverter
31023102
//===----------------------------------------------------------------------===//
3103+
31033104
namespace {
31043105
enum OpConversionMode {
31053106
/// In this mode, the conversion will ignore failed conversions to allow
@@ -3117,6 +3118,13 @@ enum OpConversionMode {
31173118
} // namespace
31183119

31193120
namespace mlir {
3121+
3122+
// Predeclaration only.
3123+
static void reconcileUnrealizedCasts(
3124+
const DenseMap<UnrealizedConversionCastOp, UnresolvedMaterializationInfo>
3125+
&castOps,
3126+
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps);
3127+
31203128
// This class converts operations to a given conversion target via a set of
31213129
// rewrite patterns. The conversion behaves differently depending on the
31223130
// conversion mode.
@@ -3264,18 +3272,13 @@ LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
32643272
// After a successful conversion, apply rewrites.
32653273
rewriterImpl.applyRewrites();
32663274

3267-
// Gather all unresolved materializations.
3268-
SmallVector<UnrealizedConversionCastOp> allCastOps;
3269-
const DenseMap<UnrealizedConversionCastOp, UnresolvedMaterializationInfo>
3270-
&materializations = rewriterImpl.unresolvedMaterializations;
3271-
for (auto it : materializations)
3272-
allCastOps.push_back(it.first);
3273-
32743275
// Reconcile all UnrealizedConversionCastOps that were inserted by the
3275-
// dialect conversion frameworks. (Not the one that were inserted by
3276+
// dialect conversion frameworks. (Not the ones that were inserted by
32763277
// patterns.)
3278+
const DenseMap<UnrealizedConversionCastOp, UnresolvedMaterializationInfo>
3279+
&materializations = rewriterImpl.unresolvedMaterializations;
32773280
SmallVector<UnrealizedConversionCastOp> remainingCastOps;
3278-
reconcileUnrealizedCasts(allCastOps, &remainingCastOps);
3281+
reconcileUnrealizedCasts(materializations, &remainingCastOps);
32793282

32803283
// Drop markers.
32813284
for (UnrealizedConversionCastOp castOp : remainingCastOps)
@@ -3303,20 +3306,15 @@ LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
33033306
// Reconcile Unrealized Casts
33043307
//===----------------------------------------------------------------------===//
33053308

3306-
void mlir::reconcileUnrealizedCasts(
3307-
ArrayRef<UnrealizedConversionCastOp> castOps,
3309+
/// Try to reconcile all given UnrealizedConversionCastOps and store the
3310+
/// left-over ops in `remainingCastOps` (if provided). See documentation in
3311+
/// DialectConversion.h for more details.
3312+
template <typename RangeT>
3313+
static void reconcileUnrealizedCastsImpl(
3314+
RangeT castOps, function_ref<bool(UnrealizedConversionCastOp)> isCastOpFn,
33083315
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps) {
3316+
// A worklist of cast ops to process.
33093317
SetVector<UnrealizedConversionCastOp> worklist(llvm::from_range, castOps);
3310-
// This set is maintained only if `remainingCastOps` is provided.
3311-
DenseSet<Operation *> erasedOps;
3312-
3313-
// Helper function that adds all operands to the worklist that are an
3314-
// unrealized_conversion_cast op result.
3315-
auto enqueueOperands = [&](UnrealizedConversionCastOp castOp) {
3316-
for (Value v : castOp.getInputs())
3317-
if (auto inputCastOp = v.getDefiningOp<UnrealizedConversionCastOp>())
3318-
worklist.insert(inputCastOp);
3319-
};
33203318

33213319
// Helper function that return the unrealized_conversion_cast op that
33223320
// defines all inputs of the given op (in the same order). Return "nullptr"
@@ -3337,39 +3335,106 @@ void mlir::reconcileUnrealizedCasts(
33373335
// Process ops in the worklist bottom-to-top.
33383336
while (!worklist.empty()) {
33393337
UnrealizedConversionCastOp castOp = worklist.pop_back_val();
3340-
if (castOp->use_empty()) {
3341-
// DCE: If the op has no users, erase it. Add the operands to the
3342-
// worklist to find additional DCE opportunities.
3343-
enqueueOperands(castOp);
3344-
if (remainingCastOps)
3345-
erasedOps.insert(castOp.getOperation());
3346-
castOp->erase();
3347-
continue;
3348-
}
33493338

33503339
// Traverse the chain of input cast ops to see if an op with the same
33513340
// input types can be found.
33523341
UnrealizedConversionCastOp nextCast = castOp;
33533342
while (nextCast) {
33543343
if (nextCast.getInputs().getTypes() == castOp.getResultTypes()) {
3344+
if (llvm::any_of(nextCast.getInputs(), [&](Value v) {
3345+
return v.getDefiningOp() == castOp;
3346+
})) {
3347+
// Ran into a cycle.
3348+
break;
3349+
}
3350+
33553351
// Found a cast where the input types match the output types of the
3356-
// matched op. We can directly use those inputs and the matched op can
3357-
// be removed.
3358-
enqueueOperands(castOp);
3352+
// matched op. We can directly use those inputs.
33593353
castOp.replaceAllUsesWith(nextCast.getInputs());
3360-
if (remainingCastOps)
3361-
erasedOps.insert(castOp.getOperation());
3362-
castOp->erase();
33633354
break;
33643355
}
33653356
nextCast = getInputCast(nextCast);
33663357
}
33673358
}
33683359

3369-
if (remainingCastOps)
3370-
for (UnrealizedConversionCastOp op : castOps)
3371-
if (!erasedOps.contains(op.getOperation()))
3360+
// A set of all alive cast ops. I.e., ops whose results are (transitively)
3361+
// used by an op that is not a cast op.
3362+
DenseSet<Operation *> liveOps;
3363+
3364+
// Helper function that marks the given op and transitively reachable input
3365+
// cast ops as alive.
3366+
auto markOpLive = [&](Operation *op) {
3367+
SmallVector<Operation *> worklist;
3368+
worklist.push_back(op);
3369+
while (!worklist.empty()) {
3370+
Operation *op = worklist.pop_back_val();
3371+
if (liveOps.insert(op).second) {
3372+
// Successfully inserted: process reachable input cast ops.
3373+
for (Value v : op->getOperands())
3374+
if (auto castOp = v.getDefiningOp<UnrealizedConversionCastOp>())
3375+
if (isCastOpFn(castOp))
3376+
worklist.push_back(castOp);
3377+
}
3378+
}
3379+
};
3380+
3381+
// Find all alive cast ops.
3382+
for (UnrealizedConversionCastOp op : castOps) {
3383+
// If any of the users is not a cast op, mark the current op (and its
3384+
// input ops) as live.
3385+
if (llvm::any_of(op->getUsers(), [&](Operation *user) {
3386+
auto castOp = dyn_cast<UnrealizedConversionCastOp>(user);
3387+
return !castOp || !isCastOpFn(castOp);
3388+
}))
3389+
markOpLive(op);
3390+
}
3391+
3392+
// Erase all dead cast ops.
3393+
for (UnrealizedConversionCastOp op : castOps) {
3394+
if (liveOps.contains(op)) {
3395+
// Op is alive and was not erased. Add it to the remaining cast ops.
3396+
if (remainingCastOps)
33723397
remainingCastOps->push_back(op);
3398+
continue;
3399+
}
3400+
3401+
// Op is dead. Erase it.
3402+
op->dropAllUses();
3403+
op->erase();
3404+
}
3405+
}
3406+
3407+
void mlir::reconcileUnrealizedCasts(
3408+
ArrayRef<UnrealizedConversionCastOp> castOps,
3409+
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps) {
3410+
// Set of all cast ops for faster lookups.
3411+
DenseSet<UnrealizedConversionCastOp> castOpSet;
3412+
for (UnrealizedConversionCastOp op : castOps)
3413+
castOpSet.insert(op);
3414+
reconcileUnrealizedCasts(castOpSet, remainingCastOps);
3415+
}
3416+
3417+
void mlir::reconcileUnrealizedCasts(
3418+
const DenseSet<UnrealizedConversionCastOp> &castOps,
3419+
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps) {
3420+
reconcileUnrealizedCastsImpl(
3421+
llvm::make_range(castOps.begin(), castOps.end()),
3422+
[&](UnrealizedConversionCastOp castOp) {
3423+
return castOps.contains(castOp);
3424+
},
3425+
remainingCastOps);
3426+
}
3427+
3428+
static void mlir::reconcileUnrealizedCasts(
3429+
const DenseMap<UnrealizedConversionCastOp, UnresolvedMaterializationInfo>
3430+
&castOps,
3431+
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps) {
3432+
reconcileUnrealizedCastsImpl(
3433+
castOps.keys(),
3434+
[&](UnrealizedConversionCastOp castOp) {
3435+
return castOps.contains(castOp);
3436+
},
3437+
remainingCastOps);
33733438
}
33743439

33753440
//===----------------------------------------------------------------------===//

mlir/test/Conversion/ReconcileUnrealizedCasts/reconcile-unrealized-casts.mlir

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,53 @@ func.func @emptyCast() -> index {
194194
%0 = builtin.unrealized_conversion_cast to index
195195
return %0 : index
196196
}
197+
198+
// -----
199+
200+
// CHECK-LABEL: test.graph_region
201+
// CHECK-NEXT: "test.return"() : () -> ()
202+
test.graph_region {
203+
%0 = builtin.unrealized_conversion_cast %2 : i32 to i64
204+
%1 = builtin.unrealized_conversion_cast %0 : i64 to i16
205+
%2 = builtin.unrealized_conversion_cast %1 : i16 to i32
206+
"test.return"() : () -> ()
207+
}
208+
209+
// -----
210+
211+
// CHECK-LABEL: test.graph_region
212+
// CHECK-NEXT: %[[cast0:.*]] = builtin.unrealized_conversion_cast %[[cast2:.*]] : i32 to i64
213+
// CHECK-NEXT: %[[cast1:.*]] = builtin.unrealized_conversion_cast %[[cast0]] : i64 to i16
214+
// CHECK-NEXT: %[[cast2]] = builtin.unrealized_conversion_cast %[[cast1]] : i16 to i32
215+
// CHECK-NEXT: "test.user"(%[[cast2]]) : (i32) -> ()
216+
// CHECK-NEXT: "test.return"() : () -> ()
217+
test.graph_region {
218+
%0 = builtin.unrealized_conversion_cast %2 : i32 to i64
219+
%1 = builtin.unrealized_conversion_cast %0 : i64 to i16
220+
%2 = builtin.unrealized_conversion_cast %1 : i16 to i32
221+
"test.user"(%2) : (i32) -> ()
222+
"test.return"() : () -> ()
223+
}
224+
225+
// -----
226+
227+
// CHECK-LABEL: test.graph_region
228+
// CHECK-NEXT: "test.return"() : () -> ()
229+
test.graph_region {
230+
%0 = builtin.unrealized_conversion_cast %0 : i32 to i32
231+
"test.return"() : () -> ()
232+
}
233+
234+
// -----
235+
236+
// CHECK-LABEL: test.graph_region
237+
// CHECK-NEXT: %[[c0:.*]] = arith.constant
238+
// CHECK-NEXT: %[[cast:.*]]:2 = builtin.unrealized_conversion_cast %[[c0]], %[[cast]]#1 : i32, i32 to i32, i32
239+
// CHECK-NEXT: "test.user"(%[[cast]]#0) : (i32) -> ()
240+
// CHECK-NEXT: "test.return"() : () -> ()
241+
test.graph_region {
242+
%cst = arith.constant 0 : i32
243+
%0, %1 = builtin.unrealized_conversion_cast %cst, %1 : i32, i32 to i32, i32
244+
"test.user"(%0) : (i32) -> ()
245+
"test.return"() : () -> ()
246+
}

mlir/test/Integration/Dialect/MemRef/assume-alignment-runtime-verification.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: mlir-opt %s -generate-runtime-verification \
22
// RUN: -expand-strided-metadata \
33
// RUN: -test-cf-assert \
4-
// RUN: -convert-to-llvm | \
4+
// RUN: -convert-to-llvm \
5+
// RUN: -reconcile-unrealized-casts | \
56
// RUN: mlir-runner -e main -entry-point-result=void \
67
// RUN: -shared-libs=%mlir_runner_utils 2>&1 | \
78
// RUN: FileCheck %s

mlir/test/Integration/Dialect/MemRef/atomic-rmw-runtime-verification.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: mlir-opt %s -generate-runtime-verification \
22
// RUN: -test-cf-assert \
3-
// RUN: -convert-to-llvm | \
3+
// RUN: -convert-to-llvm \
4+
// RUN: -reconcile-unrealized-casts | \
45
// RUN: mlir-runner -e main -entry-point-result=void \
56
// RUN: -shared-libs=%mlir_runner_utils 2>&1 | \
67
// RUN: FileCheck %s

mlir/test/Integration/Dialect/MemRef/store-runtime-verification.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: mlir-opt %s -generate-runtime-verification \
22
// RUN: -test-cf-assert \
3-
// RUN: -convert-to-llvm | \
3+
// RUN: -convert-to-llvm \
4+
// RUN: -reconcile-unrealized-casts | \
45
// RUN: mlir-runner -e main -entry-point-result=void \
56
// RUN: -shared-libs=%mlir_runner_utils 2>&1 | \
67
// RUN: FileCheck %s

0 commit comments

Comments
 (0)