Files
zeekay 674da0bb9e Name the licence the repository actually carries
The headers said 'BSD3 License with Lux Industries Exceptions', which is not
a licence that exists — the sweep that removed the upstream vendor's name
substituted ours into the licence title instead of correcting it. LICENSE.txt
is the BSD 3-Clause Clear License and the headers now say so.
2026-08-27 16:00:52 -07:00

82 lines
2.7 KiB
C++

// Part of the Torus Compiler Project, under the BSD 3-Clause Clear
// License. See
// https://github.com/luxfhe.com/torus-compiler-internal/blob/main/LICENSE.txt
// for license information.
#include "toruslang/Transforms/Passes.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Transforms/DialectConversion.h"
namespace {
struct LinalgFillToLinalgGenericPattern
: public mlir::OpRewritePattern<mlir::linalg::FillOp> {
LinalgFillToLinalgGenericPattern(::mlir::MLIRContext *context,
mlir::PatternBenefit benefit = 1)
: ::mlir::OpRewritePattern<mlir::linalg::FillOp>(context, benefit) {}
::mlir::LogicalResult
matchAndRewrite(mlir::linalg::FillOp fillOp,
::mlir::PatternRewriter &rewriter) const override {
if (fillOp.getOutputs().size() != 1)
return ::mlir::failure();
mlir::RankedTensorType outputTensorType =
fillOp.getOutputs()[0].getType().cast<mlir::RankedTensorType>();
llvm::SmallVector<mlir::utils::IteratorType> iteratorTypes(
outputTensorType.getRank(), mlir::utils::IteratorType::parallel);
mlir::AffineMap map = mlir::AffineMap::getMultiDimIdentityMap(
outputTensorType.getRank(), this->getContext());
mlir::SmallVector<mlir::AffineMap> maps(1, map);
auto bodyBuilder = [&](mlir::OpBuilder &nestedBuilder,
mlir::Location nestedLoc,
mlir::ValueRange blockArgs) {
nestedBuilder.create<mlir::linalg::YieldOp>(nestedLoc,
fillOp.getInputs()[0]);
};
rewriter.replaceOpWithNewOp<mlir::linalg::GenericOp>(
fillOp, fillOp.getOutputs().getTypes(), mlir::ValueRange{},
fillOp.getOutputs(), maps, iteratorTypes, bodyBuilder);
return ::mlir::success();
};
};
struct LinalgFillToLinalgGenericPass
: public LinalgFillToLinalgGenericBase<LinalgFillToLinalgGenericPass> {
LinalgFillToLinalgGenericPass() {}
void runOnOperation() override {
auto op = this->getOperation();
mlir::ConversionTarget target(getContext());
mlir::RewritePatternSet patterns(&getContext());
patterns.add<LinalgFillToLinalgGenericPattern>(&getContext());
target.addIllegalOp<mlir::linalg::FillOp>();
target.markUnknownOpDynamicallyLegal(
[](mlir::Operation *) { return true; });
// Apply conversion
if (mlir::applyPartialConversion(op, target, std::move(patterns))
.failed()) {
this->signalPassFailure();
}
}
};
} // namespace
namespace mlir {
namespace toruslang {
std::unique_ptr<OperationPass<ModuleOp>> createLinalgFillToLinalgGenericPass() {
return std::make_unique<LinalgFillToLinalgGenericPass>();
}
} // namespace toruslang
} // namespace mlir