//====- LowerToLLVM.cpp - Lowering from Toy+Affine+Std to LLVM ------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements full lowering of Toy operations to LLVM MLIR dialect. // 'toy.print' is lowered to a loop nest that calls `printf` on each element of // the input array. The file also sets up the ToyToLLVMLoweringPass. This pass // lowers the combination of Arithmetic + Affine + SCF + Func dialects to the // LLVM one: // // Affine -- // | // v // Arithmetic + Func --> LLVM (Dialect) // ^ // | // 'toy.print' --> Loop (SCF) -- // //===----------------------------------------------------------------------===// #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" #include "mlir/Dialect/LLVMIR/LLVMTypes.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/Support/LLVM.h" #include "mlir/Support/TypeID.h" #include "toy/Dialect.h" #include "toy/Passes.h" #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" #include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h" #include "mlir/Conversion/LLVMCommon/ConversionTarget.h" #include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" #include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/Dialect/SCF/IR/SCF.h" #include "mlir/Pass/Pass.h" #include "mlir/Transforms/DialectConversion.h" #include "llvm/Support/Casting.h" #include <memory> #include <utility> usingnamespacemlir; //===----------------------------------------------------------------------===// // ToyToLLVM RewritePatterns //===----------------------------------------------------------------------===// namespace { /// Lowers `toy.print` to a loop nest calling `printf` on each of the individual /// elements of the array. class PrintOpLowering : public ConversionPattern { … }; } // namespace //===----------------------------------------------------------------------===// // ToyToLLVMLoweringPass //===----------------------------------------------------------------------===// namespace { struct ToyToLLVMLoweringPass : public PassWrapper<ToyToLLVMLoweringPass, OperationPass<ModuleOp>> { … }; } // namespace void ToyToLLVMLoweringPass::runOnOperation() { … } /// Create a pass for lowering operations the remaining `Toy` operations, as /// well as `Affine` and `Std`, to the LLVM dialect for codegen. std::unique_ptr<mlir::Pass> mlir::toy::createLowerToLLVMPass() { … }