/* -*- C++ -*- * Serene Programming Language * * Copyright (c) 2019-2021 Sameer Rahmani * * 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. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "serene/passes.h" #include "serene/slir/dialect.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace serene::passes { struct SLIRToLLVMDialect : public mlir::PassWrapper> { void getDependentDialects(mlir::DialectRegistry ®istry) const override { registry.insert(); } 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(); // 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 `serene`, `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(&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 createSLIRLowerToLLVMDialectPass() { return std::make_unique(); }; } // namespace serene::passes