Files
concrete/compiler/lib/Conversion/LinalgExtras/LinalgExtras.cpp
Andi Drebes 45577fb79e Rebase onto llvm-project f69328049e9e with local changes
This commit rebases the compiler onto commit f69328049e9e from
llvm-project.

Changes:

* Use of the one-shot bufferizer for improved memory management

* A new pass `OneShotBufferizeDPSWrapper` that converts functions
  returning tensors to destination-passing-style as required by the
  one-shot bufferizer

* A new pass `LinalgGenericOpWithTensorsToLoopsPass` that converts
  `linalg.generic` operations with value semantics to loop nests

* Rebase onto a fork of llvm-project at f69328049e9e with local
  modifications to enable bufferization of `linalg.generic` operations
  with value semantics

* Workaround for the absence of type propagation after type conversion
  via extra patterns in all dialect conversion passes

* Printer, parser and verifier definitions moved from inline
  declarations in ODS to the respective source files as required by
  upstream changes

* New tests for functions with a large number of inputs

* Increase the number of allowed task inputs as required by new tests

* Use upstream function `mlir_configure_python_dev_packages()` to
  locate Python development files for compatibility with various CMake
  versions

Co-authored-by: Quentin Bourgerie <quentin.bourgerie@zama.ai>
Co-authored-by: Ayoub Benaissa <ayoub.benaissa@zama.ai>
Co-authored-by: Antoniu Pop <antoniu.pop@zama.ai>
2022-06-14 14:35:25 +02:00

73 lines
2.4 KiB
C++

// Part of the Concrete Compiler Project, under the BSD3 License with Zama
// Exceptions. See
// https://github.com/zama-ai/concrete-compiler-internal/blob/main/LICENSE.txt
// for license information.
#include <concretelang/Conversion/Passes.h>
#include <concretelang/Support/LinalgExtras.h>
#include <mlir/Dialect/Linalg/IR/Linalg.h>
#include <mlir/Transforms/GreedyPatternRewriteDriver.h>
namespace {
struct LinalgGenericOpWithTensorsToLoopsPass
: public LinalgGenericOpWithTensorsToLoopsBase<
LinalgGenericOpWithTensorsToLoopsPass> {
LinalgGenericOpWithTensorsToLoopsPass() = delete;
LinalgGenericOpWithTensorsToLoopsPass(bool parallelizeLoops)
: parallelizeLoops(parallelizeLoops){};
void runOnOperation() final;
private:
bool parallelizeLoops;
};
} // namespace
template <typename LoopType>
class LinalgRewritePattern
: public mlir::OpRewritePattern<mlir::linalg::GenericOp> {
public:
using OpRewritePattern<mlir::linalg::GenericOp>::OpRewritePattern;
LinalgRewritePattern(::mlir::MLIRContext *context, bool parallelizeLoops,
mlir::PatternBenefit benefit = 0)
: parallelizeLoops(parallelizeLoops),
::mlir::OpRewritePattern<mlir::linalg::GenericOp>(context, benefit) {}
mlir::LogicalResult
matchAndRewrite(mlir::linalg::GenericOp linalgOp,
mlir::PatternRewriter &rewriter) const override {
mlir::FailureOr<mlir::linalg::LinalgLoops> loops =
mlir::concretelang::linalgextras::linalgTensorOpToLoopsImpl<LoopType>(
rewriter, linalgOp, parallelizeLoops);
if (((mlir::LogicalResult)loops).failed() || loops->size() == 0)
return mlir::failure();
rewriter.replaceOp(linalgOp, loops.getValue()[0]->getResult(0));
return mlir::success();
};
private:
bool parallelizeLoops;
};
void LinalgGenericOpWithTensorsToLoopsPass::runOnOperation() {
auto op = this->getOperation();
mlir::RewritePatternSet patterns(&getContext());
patterns.insert<LinalgRewritePattern<mlir::scf::ForOp>>(&getContext(),
parallelizeLoops);
(void)applyPatternsAndFoldGreedily(op, std::move(patterns));
}
namespace mlir {
namespace concretelang {
std::unique_ptr<OperationPass<ModuleOp>>
createLinalgGenericOpWithTensorsToLoopsPass(bool parallelizeLoops) {
return std::make_unique<LinalgGenericOpWithTensorsToLoopsPass>(
parallelizeLoops);
}
} // namespace concretelang
} // namespace mlir