@@ -1291,59 +1291,45 @@ Operation *transform::TrackingListener::getCommonDefiningOp(ValueRange values) {
1291
1291
return defOp;
1292
1292
}
1293
1293
1294
- DiagnosedSilenceableFailure transform::TrackingListener::findReplacementOp (
1295
- Operation *&result, Operation *op, ValueRange newValues) const {
1294
+ FailureOr<Operation *>
1295
+ transform::TrackingListener::findReplacementOp (Operation *op,
1296
+ ValueRange newValues) const {
1296
1297
assert (op->getNumResults () == newValues.size () &&
1297
1298
" invalid number of replacement values" );
1298
1299
SmallVector<Value> values (newValues.begin (), newValues.end ());
1299
1300
1300
- DiagnosedSilenceableFailure diag = emitSilenceableFailure (
1301
- getTransformOp (), " tracking listener failed to find replacement op "
1302
- " during application of this transform op" );
1303
-
1304
1301
do {
1305
1302
// If the replacement values belong to different ops, drop the mapping.
1306
1303
Operation *defOp = getCommonDefiningOp (values);
1307
- if (!defOp) {
1308
- diag.attachNote () << " replacement values belong to different ops" ;
1309
- return diag;
1310
- }
1304
+ if (!defOp)
1305
+ return failure ();
1311
1306
1312
1307
// If the defining op has the same type, we take it as a replacement.
1313
- if (op->getName () == defOp->getName ()) {
1314
- result = defOp;
1315
- return DiagnosedSilenceableFailure::success ();
1316
- }
1308
+ if (op->getName () == defOp->getName ())
1309
+ return defOp;
1317
1310
1318
1311
// Replacing an op with a constant-like equivalent is a common
1319
1312
// canonicalization.
1320
- if (defOp->hasTrait <OpTrait::ConstantLike>()) {
1321
- result = defOp;
1322
- return DiagnosedSilenceableFailure::success ();
1323
- }
1313
+ if (defOp->hasTrait <OpTrait::ConstantLike>())
1314
+ return defOp;
1324
1315
1325
1316
values.clear ();
1326
1317
1327
1318
// Skip through ops that implement FindPayloadReplacementOpInterface.
1328
1319
if (auto findReplacementOpInterface =
1329
1320
dyn_cast<FindPayloadReplacementOpInterface>(defOp)) {
1330
1321
values.assign (findReplacementOpInterface.getNextOperands ());
1331
- diag.attachNote (defOp->getLoc ()) << " using operands provided by "
1332
- " 'FindPayloadReplacementOpInterface'" ;
1333
1322
continue ;
1334
1323
}
1335
1324
1336
1325
// Skip through ops that implement CastOpInterface.
1337
1326
if (isa<CastOpInterface>(defOp)) {
1338
1327
values.assign (defOp->getOperands ().begin (), defOp->getOperands ().end ());
1339
- diag.attachNote (defOp->getLoc ())
1340
- << " using output of 'CastOpInterface' op" ;
1341
1328
continue ;
1342
1329
}
1343
1330
} while (!values.empty ());
1344
1331
1345
- diag.attachNote () << " ran out of suitable replacement values" ;
1346
- return diag;
1332
+ return failure ();
1347
1333
}
1348
1334
1349
1335
LogicalResult transform::TrackingListener::notifyMatchFailure (
@@ -1412,39 +1398,32 @@ void transform::TrackingListener::notifyOperationReplaced(
1412
1398
};
1413
1399
1414
1400
// Helper function to check if the handle is alive.
1415
- auto firstAliveUser = [&]() -> std::optional<OpOperand *> {
1401
+ auto hasAliveUser = [&]() {
1416
1402
for (Value v : opHandles) {
1417
- for (OpOperand &use : v.getUses ())
1418
- if (use.getOwner () != transformOp &&
1419
- !happensBefore (use.getOwner (), transformOp))
1420
- return &use;
1403
+ for (Operation *user : v.getUsers ())
1404
+ if (user != transformOp && !happensBefore (user, transformOp))
1405
+ return true ;
1421
1406
}
1422
- return std::nullopt ;
1423
- }() ;
1407
+ return false ;
1408
+ };
1424
1409
1425
- if (!firstAliveUser. has_value () || handleWasConsumed ()) {
1410
+ if (!hasAliveUser () || handleWasConsumed ()) {
1426
1411
// The op is tracked but the corresponding handles are dead or were
1427
1412
// consumed. Drop the op form the mapping.
1428
1413
(void )replacePayloadOp (op, nullptr );
1429
1414
return ;
1430
1415
}
1431
1416
1432
- Operation *replacement;
1433
- DiagnosedSilenceableFailure diag =
1434
- findReplacementOp (replacement, op, newValues);
1417
+ FailureOr<Operation *> replacement = findReplacementOp (op, newValues);
1435
1418
// If the op is tracked but no replacement op was found, send a
1436
1419
// notification.
1437
- if (!diag.succeeded ()) {
1438
- diag.attachNote ((*firstAliveUser)->getOwner ()->getLoc ())
1439
- << " replacement is required because alive handle(s) exist "
1440
- << " (first use in this op as operand number "
1441
- << (*firstAliveUser)->getOperandNumber () << " )" ;
1442
- notifyPayloadReplacementNotFound (op, newValues, std::move (diag));
1420
+ if (failed (replacement)) {
1421
+ notifyPayloadReplacementNotFound (op, newValues);
1443
1422
(void )replacePayloadOp (op, nullptr );
1444
1423
return ;
1445
1424
}
1446
1425
1447
- (void )replacePayloadOp (op, replacement);
1426
+ (void )replacePayloadOp (op, * replacement);
1448
1427
}
1449
1428
1450
1429
transform::ErrorCheckingTrackingListener::~ErrorCheckingTrackingListener () {
@@ -1467,20 +1446,17 @@ bool transform::ErrorCheckingTrackingListener::failed() const {
1467
1446
}
1468
1447
1469
1448
void transform::ErrorCheckingTrackingListener::notifyPayloadReplacementNotFound (
1470
- Operation *op, ValueRange values, DiagnosedSilenceableFailure &&diag) {
1471
-
1472
- // Merge potentially existing diags and store the result in the listener.
1473
- SmallVector<Diagnostic> diags;
1474
- diag.takeDiagnostics (diags);
1475
- if (!status.succeeded ())
1476
- status.takeDiagnostics (diags);
1477
- status = DiagnosedSilenceableFailure::silenceableFailure (std::move (diags));
1449
+ Operation *op, ValueRange values) {
1450
+ if (status.succeeded ()) {
1451
+ status = emitSilenceableFailure (
1452
+ getTransformOp (), " tracking listener failed to find replacement op" );
1453
+ }
1478
1454
1479
- // Report more details.
1480
1455
status.attachNote (op->getLoc ()) << " [" << errorCounter << " ] replaced op" ;
1481
1456
for (auto &&[index, value] : llvm::enumerate (values))
1482
1457
status.attachNote (value.getLoc ())
1483
1458
<< " [" << errorCounter << " ] replacement value " << index;
1459
+
1484
1460
++errorCounter;
1485
1461
}
1486
1462
0 commit comments