93 lines
4.1 KiB
C++
93 lines
4.1 KiB
C++
/* -*- C++ -*-
|
|
* Serene programming language.
|
|
*
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#include "serene/passes.h"
|
|
#include "serene/slir/dialect.h"
|
|
|
|
#include <memory>
|
|
#include <mlir/Conversion/AffineToStandard/AffineToStandard.h>
|
|
#include <mlir/Conversion/LLVMCommon/ConversionTarget.h>
|
|
#include <mlir/Conversion/LLVMCommon/TypeConverter.h>
|
|
#include <mlir/Conversion/SCFToStandard/SCFToStandard.h>
|
|
#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>
|
|
|
|
namespace serene::passes {
|
|
struct SLIRToLLVMDialect
|
|
: public mlir::PassWrapper<SLIRToLLVMDialect,
|
|
mlir::OperationPass<mlir::ModuleOp>> {
|
|
void getDependentDialects(mlir::DialectRegistry ®istry) const override {
|
|
registry.insert<mlir::LLVM::LLVMDialect, mlir::scf::SCFDialect>();
|
|
}
|
|
|
|
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
|
|
// have a combination of `toy`, `affine`, and `std` operations. Luckily, there
|
|
// are already exists a set of patterns to transform `affine` and `std`
|
|
// 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());
|
|
mlir::populateAffineToStdConversionPatterns(patterns);
|
|
populateLoopToStdConversionPatterns(patterns);
|
|
populateStdToLLVMConversionPatterns(typeConverter, patterns);
|
|
|
|
// 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();
|
|
if (failed(applyFullConversion(module, target, std::move(patterns))))
|
|
signalPassFailure();
|
|
};
|
|
|
|
std::unique_ptr<mlir::Pass> createSLIRLowerToLLVMDialectPass() {
|
|
return std::make_unique<SLIRToLLVMDialect>();
|
|
};
|
|
|
|
} // namespace serene::passes
|