2021-06-17 13:19:19 +01:00
|
|
|
/* -*- C++ -*-
|
2021-10-12 20:51:03 +01:00
|
|
|
* Serene Programming Language
|
2021-06-17 13:19:19 +01:00
|
|
|
*
|
2022-01-27 11:44:44 +00:00
|
|
|
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
2021-06-17 13:19:19 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
2021-06-17 13:19:19 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2021-06-17 13:19:19 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-06-17 13:19:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serene/passes.h"
|
|
|
|
#include "serene/slir/dialect.h"
|
|
|
|
|
|
|
|
#include <mlir/Conversion/AffineToStandard/AffineToStandard.h>
|
2021-12-30 13:52:33 +00:00
|
|
|
#include <mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h>
|
2021-08-28 18:01:48 +01:00
|
|
|
#include <mlir/Conversion/LLVMCommon/ConversionTarget.h>
|
|
|
|
#include <mlir/Conversion/LLVMCommon/TypeConverter.h>
|
2021-06-17 13:19:19 +01:00
|
|
|
#include <mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h>
|
|
|
|
#include <mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h>
|
|
|
|
#include <mlir/Dialect/LLVMIR/LLVMDialect.h>
|
|
|
|
#include <mlir/Dialect/SCF/SCF.h>
|
|
|
|
#include <mlir/IR/BuiltinOps.h>
|
|
|
|
#include <mlir/Pass/Pass.h>
|
|
|
|
#include <mlir/Transforms/DialectConversion.h>
|
|
|
|
|
2021-12-30 13:52:33 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2021-06-17 13:19:19 +01:00
|
|
|
namespace serene::passes {
|
|
|
|
struct SLIRToLLVMDialect
|
|
|
|
: public mlir::PassWrapper<SLIRToLLVMDialect,
|
|
|
|
mlir::OperationPass<mlir::ModuleOp>> {
|
|
|
|
void getDependentDialects(mlir::DialectRegistry ®istry) const override {
|
2021-10-16 16:15:56 +01:00
|
|
|
registry.insert<mlir::LLVM::LLVMDialect>();
|
2021-06-17 13:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void runOnOperation() final;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SLIRToLLVMDialect::runOnOperation() {
|
|
|
|
// The first thing to define is the conversion target. This will define the
|
|
|
|
// final target for this lowering. For this lowering, we are only targeting
|
|
|
|
// the LLVM dialect.
|
|
|
|
mlir::LLVMConversionTarget target(getContext());
|
|
|
|
target.addLegalOp<mlir::ModuleOp>();
|
|
|
|
|
|
|
|
// During this lowering, we will also be lowering the MemRef types, that are
|
|
|
|
// currently being operated on, to a representation in LLVM. To perform this
|
|
|
|
// conversion we use a TypeConverter as part of the lowering. This converter
|
|
|
|
// details how one type maps to another. This is necessary now that we will be
|
|
|
|
// doing more complicated lowerings, involving loop region arguments.
|
|
|
|
mlir::LLVMTypeConverter typeConverter(&getContext());
|
|
|
|
|
|
|
|
// Now that the conversion target has been defined, we need to provide the
|
|
|
|
// patterns used for lowering. At this point of the compilation process, we
|
2021-09-28 17:11:50 +01:00
|
|
|
// have a combination of `serene`, `affine`, and `std` operations. Luckily,
|
|
|
|
// there are already exists a set of patterns to transform `affine` and `std`
|
2021-06-17 13:19:19 +01:00
|
|
|
// dialects. These patterns lowering in multiple stages, relying on transitive
|
|
|
|
// lowerings. Transitive lowering, or A->B->C lowering, is when multiple
|
|
|
|
// patterns must be applied to fully transform an illegal operation into a
|
|
|
|
// set of legal ones.
|
|
|
|
mlir::RewritePatternSet patterns(&getContext());
|
2021-09-28 17:11:50 +01:00
|
|
|
|
2021-10-06 21:07:56 +01:00
|
|
|
populateStdToLLVMConversionPatterns(typeConverter, patterns);
|
2021-12-30 13:52:33 +00:00
|
|
|
mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
|
|
|
|
patterns);
|
2021-06-17 13:19:19 +01:00
|
|
|
// patterns.add<PrintOpLowering>(&getContext());
|
|
|
|
|
|
|
|
// We want to completely lower to LLVM, so we use a `FullConversion`. This
|
|
|
|
// ensures that only legal operations will remain after the conversion.
|
|
|
|
auto module = getOperation();
|
2021-10-16 20:48:14 +01:00
|
|
|
if (failed(applyFullConversion(module, target, std::move(patterns)))) {
|
2021-06-17 13:19:19 +01:00
|
|
|
signalPassFailure();
|
2021-10-16 20:48:14 +01:00
|
|
|
}
|
2021-06-17 13:19:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<mlir::Pass> createSLIRLowerToLLVMDialectPass() {
|
|
|
|
return std::make_unique<SLIRToLLVMDialect>();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace serene::passes
|