@@ -3309,9 +3309,12 @@ LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
3309
3309
// / Try to reconcile all given UnrealizedConversionCastOps and store the
3310
3310
// / left-over ops in `remainingCastOps` (if provided). See documentation in
3311
3311
// / DialectConversion.h for more details.
3312
+ // / The `isCastOpOfInterestFn` is used to filter the cast ops to proceed: the algorithm may
3313
+ // / visit an operand (or user) which is a cast op, but will not try to reconcile it if not in the
3314
+ // / filtered set.
3312
3315
template <typename RangeT>
3313
3316
static void reconcileUnrealizedCastsImpl (
3314
- RangeT castOps, function_ref<bool (UnrealizedConversionCastOp)> isCastOpFn ,
3317
+ RangeT castOps, function_ref<bool (UnrealizedConversionCastOp)> isCastOpOfInterestFn ,
3315
3318
SmallVectorImpl<UnrealizedConversionCastOp> *remainingCastOps) {
3316
3319
// A worklist of cast ops to process.
3317
3320
SetVector<UnrealizedConversionCastOp> worklist (llvm::from_range, castOps);
@@ -3363,9 +3366,9 @@ static void reconcileUnrealizedCastsImpl(
3363
3366
3364
3367
// Helper function that marks the given op and transitively reachable input
3365
3368
// cast ops as alive.
3366
- auto markOpLive = [&](Operation *op ) {
3369
+ auto markOpLive = [&](Operation *rootOp ) {
3367
3370
SmallVector<Operation *> worklist;
3368
- worklist.push_back (op );
3371
+ worklist.push_back (rootOp );
3369
3372
while (!worklist.empty ()) {
3370
3373
Operation *op = worklist.pop_back_val ();
3371
3374
if (liveOps.insert (op).second ) {
@@ -3380,6 +3383,8 @@ static void reconcileUnrealizedCastsImpl(
3380
3383
3381
3384
// Find all alive cast ops.
3382
3385
for (UnrealizedConversionCastOp op : castOps) {
3386
+ // The op may have been marked live already as being an operand of another live cast op.
3387
+ if (liveOps.contains (op.getOperation ()) continue ;
3383
3388
// If any of the users is not a cast op, mark the current op (and its
3384
3389
// input ops) as live.
3385
3390
if (llvm::any_of (op->getUsers (), [&](Operation *user) {
0 commit comments