[mlir][DialectConversion] Add overload of addDynamicallyLegalDialect to support lambdas

This allows for passing a lambda to addDynamicallyLegalDialect without needing to explicit wrap with Optional<DynamicLegalityCallbackFn>.

Differential Revision: https://reviews.llvm.org/D81680
This commit is contained in:
River Riddle 2020-06-15 15:30:21 -07:00
parent 0e360744f3
commit 552ef9fc09
3 changed files with 10 additions and 7 deletions

View File

@ -607,6 +607,12 @@ public:
if (callback)
setLegalityCallback(dialectNames, *callback);
}
template <typename... Args>
void addDynamicallyLegalDialect(DynamicLegalityCallbackFn callback) {
SmallVector<StringRef, 2> dialectNames({Args::getDialectNamespace()...});
setDialectAction(dialectNames, LegalizationAction::Dynamic);
setLegalityCallback(dialectNames, callback);
}
/// Register unknown operations as dynamically legal. For operations(and
/// dialects) that do not have a set legalization action, treat them as

View File

@ -657,10 +657,9 @@ spirv::SPIRVConversionTarget::get(spirv::TargetEnvAttr targetAttr) {
new SPIRVConversionTarget(targetAttr));
SPIRVConversionTarget *targetPtr = target.get();
target->addDynamicallyLegalDialect<SPIRVDialect>(
Optional<ConversionTarget::DynamicLegalityCallbackFn>(
// We need to capture the raw pointer here because it is stable:
// target will be destroyed once this function is returned.
[targetPtr](Operation *op) { return targetPtr->isLegalOp(op); }));
[targetPtr](Operation *op) { return targetPtr->isLegalOp(op); });
return target;
}

View File

@ -127,9 +127,7 @@ struct TestBufferPlacementPreparationPass
auto isLegalOperation = [&](Operation *op) {
return converter.isLegal(op);
};
target.addDynamicallyLegalDialect<linalg::LinalgDialect>(
Optional<ConversionTarget::DynamicLegalityCallbackFn>(
isLegalOperation));
target.addDynamicallyLegalDialect<linalg::LinalgDialect>(isLegalOperation);
// Mark Standard Return operations illegal as long as one operand is tensor.
target.addDynamicallyLegalOp<mlir::ReturnOp>([&](mlir::ReturnOp returnOp) {