| 1 | //===- FunctionCallUtils.h - Utilities for C function calls -----*- 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 | // This file declares helper functions to call common simple C functions in |
| 10 | // LLVMIR (e.g. among others to support printing and debugging). |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ |
| 15 | #define MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ |
| 16 | |
| 17 | #include "mlir/IR/Operation.h" |
| 18 | #include "mlir/Support/LLVM.h" |
| 19 | |
| 20 | namespace mlir { |
| 21 | class Location; |
| 22 | class ModuleOp; |
| 23 | class OpBuilder; |
| 24 | class Operation; |
| 25 | class Type; |
| 26 | class ValueRange; |
| 27 | class SymbolTableCollection; |
| 28 | |
| 29 | namespace LLVM { |
| 30 | class LLVMFuncOp; |
| 31 | |
| 32 | /// Helper functions to look up or create the declaration for commonly used |
| 33 | /// external C function calls. The list of functions provided here must be |
| 34 | /// implemented separately (e.g. as part of a support runtime library or as part |
| 35 | /// of the libc). |
| 36 | /// Failure if an unexpected version of function is found. |
| 37 | FailureOr<LLVM::LLVMFuncOp> |
| 38 | lookupOrCreatePrintI64Fn(OpBuilder &b, Operation *moduleOp, |
| 39 | SymbolTableCollection *symbolTables = nullptr); |
| 40 | FailureOr<LLVM::LLVMFuncOp> |
| 41 | lookupOrCreatePrintU64Fn(OpBuilder &b, Operation *moduleOp, |
| 42 | SymbolTableCollection *symbolTables = nullptr); |
| 43 | FailureOr<LLVM::LLVMFuncOp> |
| 44 | lookupOrCreatePrintF16Fn(OpBuilder &b, Operation *moduleOp, |
| 45 | SymbolTableCollection *symbolTables = nullptr); |
| 46 | FailureOr<LLVM::LLVMFuncOp> |
| 47 | lookupOrCreatePrintBF16Fn(OpBuilder &b, Operation *moduleOp, |
| 48 | SymbolTableCollection *symbolTables = nullptr); |
| 49 | FailureOr<LLVM::LLVMFuncOp> |
| 50 | lookupOrCreatePrintF32Fn(OpBuilder &b, Operation *moduleOp, |
| 51 | SymbolTableCollection *symbolTables = nullptr); |
| 52 | FailureOr<LLVM::LLVMFuncOp> |
| 53 | lookupOrCreatePrintF64Fn(OpBuilder &b, Operation *moduleOp, |
| 54 | SymbolTableCollection *symbolTables = nullptr); |
| 55 | /// Declares a function to print a C-string. |
| 56 | /// If a custom runtime function is defined via `runtimeFunctionName`, it must |
| 57 | /// have the signature void(char const*). The default function is `printString`. |
| 58 | FailureOr<LLVM::LLVMFuncOp> |
| 59 | lookupOrCreatePrintStringFn(OpBuilder &b, Operation *moduleOp, |
| 60 | std::optional<StringRef> runtimeFunctionName = {}, |
| 61 | SymbolTableCollection *symbolTables = nullptr); |
| 62 | FailureOr<LLVM::LLVMFuncOp> |
| 63 | lookupOrCreatePrintOpenFn(OpBuilder &b, Operation *moduleOp, |
| 64 | SymbolTableCollection *symbolTables = nullptr); |
| 65 | FailureOr<LLVM::LLVMFuncOp> |
| 66 | lookupOrCreatePrintCloseFn(OpBuilder &b, Operation *moduleOp, |
| 67 | SymbolTableCollection *symbolTables = nullptr); |
| 68 | FailureOr<LLVM::LLVMFuncOp> |
| 69 | lookupOrCreatePrintCommaFn(OpBuilder &b, Operation *moduleOp, |
| 70 | SymbolTableCollection *symbolTables = nullptr); |
| 71 | FailureOr<LLVM::LLVMFuncOp> |
| 72 | lookupOrCreatePrintNewlineFn(OpBuilder &b, Operation *moduleOp, |
| 73 | SymbolTableCollection *symbolTables = nullptr); |
| 74 | FailureOr<LLVM::LLVMFuncOp> |
| 75 | lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType, |
| 76 | SymbolTableCollection *symbolTables = nullptr); |
| 77 | FailureOr<LLVM::LLVMFuncOp> |
| 78 | lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType, |
| 79 | SymbolTableCollection *symbolTables = nullptr); |
| 80 | FailureOr<LLVM::LLVMFuncOp> |
| 81 | lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp, |
| 82 | SymbolTableCollection *symbolTables = nullptr); |
| 83 | FailureOr<LLVM::LLVMFuncOp> |
| 84 | lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType, |
| 85 | SymbolTableCollection *symbolTables = nullptr); |
| 86 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericAlignedAllocFn( |
| 87 | OpBuilder &b, Operation *moduleOp, Type indexType, |
| 88 | SymbolTableCollection *symbolTables = nullptr); |
| 89 | FailureOr<LLVM::LLVMFuncOp> |
| 90 | lookupOrCreateGenericFreeFn(OpBuilder &b, Operation *moduleOp, |
| 91 | SymbolTableCollection *symbolTables = nullptr); |
| 92 | FailureOr<LLVM::LLVMFuncOp> |
| 93 | lookupOrCreateMemRefCopyFn(OpBuilder &b, Operation *moduleOp, Type indexType, |
| 94 | Type unrankedDescriptorType, |
| 95 | SymbolTableCollection *symbolTables = nullptr); |
| 96 | |
| 97 | /// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`. |
| 98 | /// Return a failure if the FuncOp found has unexpected signature. |
| 99 | FailureOr<LLVM::LLVMFuncOp> |
| 100 | lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name, |
| 101 | ArrayRef<Type> paramTypes = {}, Type resultType = {}, |
| 102 | bool isVarArg = false, bool isReserved = false, |
| 103 | SymbolTableCollection *symbolTables = nullptr); |
| 104 | |
| 105 | } // namespace LLVM |
| 106 | } // namespace mlir |
| 107 | |
| 108 | #endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ |
| 109 | |