This commit is contained in:
Nicolas Vasilache 2022-01-05 11:40:46 -05:00
parent 319971ca95
commit c05db63887
2 changed files with 20 additions and 17 deletions

View File

@ -29,39 +29,38 @@
using namespace mlir;
using namespace mlir::linalg;
static LogicalResult generalizeNamedOpPrecondition(Operation *op) {
LinalgOp namedOp = dyn_cast<LinalgOp>(op);
static LogicalResult generalizeNamedOpPrecondition(LinalgOp linalgOp) {
// Check if the operation is a LinalgOp but not a GenericOp.
if (!namedOp || isa<GenericOp>(op))
if (isa<GenericOp>(linalgOp))
return failure();
// Check if the operation has a region builder.
if (!namedOp.getRegionBuilder())
if (!linalgOp.getRegionBuilder())
return failure();
return success();
}
FailureOr<GenericOp> mlir::linalg::generalizeNamedOp(RewriterBase &rewriter,
LinalgOp namedOp) {
if (failed(generalizeNamedOpPrecondition(namedOp)))
return rewriter.notifyMatchFailure(namedOp, "preconditions not met");
LinalgOp linalgOp) {
if (failed(generalizeNamedOpPrecondition(linalgOp)))
return rewriter.notifyMatchFailure(linalgOp, "preconditions not met");
SmallVector<Value> inputOperands = namedOp.getInputOperands();
SmallVector<Value> outputOperands = namedOp.getOutputOperands();
SmallVector<AffineMap> indexingMaps = namedOp.getIndexingMaps();
SmallVector<Value> inputOperands = linalgOp.getInputOperands();
SmallVector<Value> outputOperands = linalgOp.getOutputOperands();
SmallVector<AffineMap> indexingMaps = linalgOp.getIndexingMaps();
SmallVector<StringRef> iterators = llvm::to_vector<4>(
namedOp.iterator_types().getAsValueRange<StringAttr>());
SmallVector<RankedTensorType> resultTypes = namedOp.getOutputTensorTypes();
linalgOp.iterator_types().getAsValueRange<StringAttr>());
SmallVector<RankedTensorType> resultTypes = linalgOp.getOutputTensorTypes();
SmallVector<Type> types(resultTypes.begin(), resultTypes.end());
// All named ops have a region attached that can be inlined.
assert(namedOp->getNumRegions() == 1 &&
assert(linalgOp->getNumRegions() == 1 &&
"expect named op to have one region attached");
GenericOp genericOp =
rewriter.create<GenericOp>(namedOp.getLoc(), types, inputOperands,
rewriter.create<GenericOp>(linalgOp.getLoc(), types, inputOperands,
outputOperands, indexingMaps, iterators);
rewriter.inlineRegionBefore(namedOp->getRegion(0), genericOp.region(),
rewriter.inlineRegionBefore(linalgOp->getRegion(0), genericOp.region(),
genericOp.region().begin());
rewriter.replaceOp(namedOp, genericOp->getResults());
rewriter.replaceOp(linalgOp, genericOp->getResults());
return genericOp;
}

View File

@ -648,9 +648,13 @@ mlir::linalg::LinalgGeneralizationPattern::LinalgGeneralizationPattern(
LogicalResult mlir::linalg::LinalgGeneralizationPattern::matchAndRewrite(
Operation *op, PatternRewriter &rewriter) const {
// TODO: Interface pattern.
LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
if (!linalgOp)
return failure();
if (failed(filter.checkAndNotify(rewriter, op)))
return failure();
FailureOr<GenericOp> genericOp = generalizeNamedOp(rewriter, op);
FailureOr<GenericOp> genericOp = generalizeNamedOp(rewriter, linalgOp);
if (failed(genericOp))
return failure();
filter.replaceLinalgTransformationFilter(rewriter, *genericOp);