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

View File

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