Skip to content

Commit 54f69ca

Browse files
[mlir][Pass] Fix crash when applying a pass to an optional interface (#168499)
Interfaces can be optional: whether an op implements an interface or not can depend on the state of the operation. ``` // An optional code block for adding additional "classof" logic. This can // be used to better enable "optional" interfaces, where an entity only // implements the interface if some dynamic characteristic holds. // `$_attr`/`$_op`/`$_type` may be used to refer to an instance of the // interface instance being checked. code extraClassOf = ""; ``` The current `Pass::canScheduleOn(RegisteredOperationName)` is insufficient. This commit adds an additional overload to inspect `Operation *`. This commit fixes a crash when scheduling an `InterfacePass` for an optional interface on an operation that does not actually implement the interface.
1 parent 131cf7d commit 54f69ca

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

mlir/include/mlir/Pass/Pass.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ class Pass {
193193
/// This is useful for generic operation passes to add restrictions on the
194194
/// operations they operate on.
195195
virtual bool canScheduleOn(RegisteredOperationName opName) const = 0;
196+
virtual bool canScheduleOn(Operation *op) const {
197+
std::optional<RegisteredOperationName> registeredInfo =
198+
op->getName().getRegisteredInfo();
199+
if (!registeredInfo)
200+
return false;
201+
return canScheduleOn(*registeredInfo);
202+
}
196203

197204
/// Schedule an arbitrary pass pipeline on the provided operation.
198205
/// This can be invoke any time in a pass to dynamic schedule more passes.
@@ -436,6 +443,7 @@ class InterfacePass : public OperationPass<> {
436443
/// Indicate if the current pass can be scheduled on the given operation type.
437444
/// For an InterfacePass, this checks if the operation implements the given
438445
/// interface.
446+
bool canScheduleOn(Operation *op) const final { return isa<InterfaceT>(op); }
439447
bool canScheduleOn(RegisteredOperationName opName) const final {
440448
return opName.hasInterface<InterfaceT>();
441449
}

mlir/lib/Pass/Pass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ LogicalResult OpToOpPassAdaptor::run(Pass *pass, Operation *op,
559559
return op->emitOpError() << "trying to schedule a pass on an operation not "
560560
"marked as 'IsolatedFromAbove'";
561561
}
562-
if (!pass->canScheduleOn(*op->getName().getRegisteredInfo())) {
563-
return op->emitOpError()
564-
<< "trying to schedule a pass on an unsupported operation";
562+
if (!pass->canScheduleOn(op)) {
563+
return op->emitOpError() << "trying to schedule pass '" << pass->getName()
564+
<< "' on an unsupported operation";
565565
}
566566

567567
// Initialize the pass state with a callback for the pass to dynamically

mlir/test/Dialect/Transform/test-pass-application.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ module attributes {transform.with_named_sequence} {
386386
// -----
387387

388388
module attributes {transform.with_named_sequence} {
389-
// expected-error @below {{trying to schedule a pass on an unsupported operation}}
389+
// expected-error @below {{trying to schedule pass 'DuplicateFunctionEliminationPass' on an unsupported operation}}
390390
// expected-note @below {{target op}}
391391
func.func @invalid_target_op_type() {
392392
return
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: mlir-opt %s -test-print-liveness -split-input-file -verify-diagnostics
2+
3+
// Unnamed modules do not implement SymbolOpInterface.
4+
// expected-error @+1 {{trying to schedule pass '(anonymous namespace)::TestLivenessPass' on an unsupported operation}}
5+
module {}
6+
7+
// -----
8+
9+
// Named modules implement SymbolOpInterface.
10+
module @named_module {}

mlir/test/Pass/pipeline-invalid.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ arith.constant 0
1515

1616
// -----
1717

18-
// expected-error@below {{trying to schedule a pass on an unsupported operation}}
18+
// expected-error@below {{trying to schedule pass '(anonymous namespace)::TestFunctionPass' on an unsupported operation}}
1919
module {}

0 commit comments

Comments
 (0)