1 //===- ConvertStandardToLLVMPass.h - Pass entrypoint ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVMPASS_H_ 10 #define MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVMPASS_H_ 11 12 #include "llvm/IR/DataLayout.h" 13 14 #include <memory> 15 16 namespace mlir { 17 class LLVMTypeConverter; 18 class ModuleOp; 19 template <typename T> 20 class OperationPass; 21 class OwningRewritePatternList; 22 23 /// Value to pass as bitwidth for the index type when the converter is expected 24 /// to derive the bitwidth from the LLVM data layout. 25 static constexpr unsigned kDeriveIndexBitwidthFromDataLayout = 0; 26 27 /// Options to control the Standard dialect to LLVM lowering. The struct is used 28 /// to share lowering options between passes, patterns, and type converter. 29 struct LowerToLLVMOptions { 30 bool useBarePtrCallConv = false; 31 bool emitCWrappers = false; 32 unsigned indexBitwidth = kDeriveIndexBitwidthFromDataLayout; 33 /// Use aligned_alloc for heap allocations. 34 bool useAlignedAlloc = false; 35 36 /// The data layout of the module to produce. This must be consistent with the 37 /// data layout used in the upper levels of the lowering pipeline. 38 // TODO: this should be replaced by MLIR data layout when one exists. 39 llvm::DataLayout dataLayout = llvm::DataLayout(""); 40 41 /// Get a statically allocated copy of the default LowerToLLVMOptions. getDefaultOptionsLowerToLLVMOptions42 static const LowerToLLVMOptions &getDefaultOptions() { 43 static LowerToLLVMOptions options; 44 return options; 45 } 46 }; 47 48 /// Collect a set of patterns to convert memory-related operations from the 49 /// Standard dialect to the LLVM dialect, excluding non-memory-related 50 /// operations and FuncOp. 51 void populateStdToLLVMMemoryConversionPatterns( 52 LLVMTypeConverter &converter, OwningRewritePatternList &patterns); 53 54 /// Collect a set of patterns to convert from the Standard dialect to the LLVM 55 /// dialect, excluding the memory-related operations. 56 void populateStdToLLVMNonMemoryConversionPatterns( 57 LLVMTypeConverter &converter, OwningRewritePatternList &patterns); 58 59 /// Collect the default pattern to convert a FuncOp to the LLVM dialect. If 60 /// `emitCWrappers` is set, the pattern will also produce functions 61 /// that pass memref descriptors by pointer-to-structure in addition to the 62 /// default unpacked form. 63 void populateStdToLLVMFuncOpConversionPattern( 64 LLVMTypeConverter &converter, OwningRewritePatternList &patterns); 65 66 /// Collect the patterns to convert from the Standard dialect to LLVM. The 67 /// conversion patterns capture the LLVMTypeConverter and the LowerToLLVMOptions 68 /// by reference meaning the references have to remain alive during the entire 69 /// pattern lifetime. 70 void populateStdToLLVMConversionPatterns(LLVMTypeConverter &converter, 71 OwningRewritePatternList &patterns); 72 73 /// Creates a pass to convert the Standard dialect into the LLVMIR dialect. 74 /// stdlib malloc/free is used by default for allocating memrefs allocated with 75 /// std.alloc, while LLVM's alloca is used for those allocated with std.alloca. 76 std::unique_ptr<OperationPass<ModuleOp>> 77 createLowerToLLVMPass(const LowerToLLVMOptions &options = 78 LowerToLLVMOptions::getDefaultOptions()); 79 80 } // namespace mlir 81 82 #endif // MLIR_CONVERSION_STANDARDTOLLVM_CONVERTSTANDARDTOLLVMPASS_H_ 83