| 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 | |
| 28 | namespace LLVM { |
| 29 | class LLVMFuncOp; |
| 30 | |
| 31 | /// Helper functions to look up or create the declaration for commonly used |
| 32 | /// external C function calls. The list of functions provided here must be |
| 33 | /// implemented separately (e.g. as part of a support runtime library or as part |
| 34 | /// of the libc). |
| 35 | /// Failure if an unexpected version of function is found. |
| 36 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintI64Fn(OpBuilder &b, |
| 37 | Operation *moduleOp); |
| 38 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintU64Fn(OpBuilder &b, |
| 39 | Operation *moduleOp); |
| 40 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF16Fn(OpBuilder &b, |
| 41 | Operation *moduleOp); |
| 42 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintBF16Fn(OpBuilder &b, |
| 43 | Operation *moduleOp); |
| 44 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF32Fn(OpBuilder &b, |
| 45 | Operation *moduleOp); |
| 46 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF64Fn(OpBuilder &b, |
| 47 | Operation *moduleOp); |
| 48 | /// Declares a function to print a C-string. |
| 49 | /// If a custom runtime function is defined via `runtimeFunctionName`, it must |
| 50 | /// have the signature void(char const*). The default function is `printString`. |
| 51 | FailureOr<LLVM::LLVMFuncOp> |
| 52 | lookupOrCreatePrintStringFn(OpBuilder &b, Operation *moduleOp, |
| 53 | std::optional<StringRef> runtimeFunctionName = {}); |
| 54 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintOpenFn(OpBuilder &b, |
| 55 | Operation *moduleOp); |
| 56 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintCloseFn(OpBuilder &b, |
| 57 | Operation *moduleOp); |
| 58 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintCommaFn(OpBuilder &b, |
| 59 | Operation *moduleOp); |
| 60 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintNewlineFn(OpBuilder &b, |
| 61 | Operation *moduleOp); |
| 62 | FailureOr<LLVM::LLVMFuncOp> |
| 63 | lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType); |
| 64 | FailureOr<LLVM::LLVMFuncOp> |
| 65 | lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType); |
| 66 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreateFreeFn(OpBuilder &b, |
| 67 | Operation *moduleOp); |
| 68 | FailureOr<LLVM::LLVMFuncOp> |
| 69 | lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType); |
| 70 | FailureOr<LLVM::LLVMFuncOp> |
| 71 | lookupOrCreateGenericAlignedAllocFn(OpBuilder &b, Operation *moduleOp, |
| 72 | Type indexType); |
| 73 | FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericFreeFn(OpBuilder &b, |
| 74 | Operation *moduleOp); |
| 75 | FailureOr<LLVM::LLVMFuncOp> |
| 76 | lookupOrCreateMemRefCopyFn(OpBuilder &b, Operation *moduleOp, Type indexType, |
| 77 | Type unrankedDescriptorType); |
| 78 | |
| 79 | /// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`. |
| 80 | /// Return a failure if the FuncOp found has unexpected signature. |
| 81 | FailureOr<LLVM::LLVMFuncOp> |
| 82 | lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name, |
| 83 | ArrayRef<Type> paramTypes = {}, Type resultType = {}, |
| 84 | bool isVarArg = false, bool isReserved = false); |
| 85 | |
| 86 | } // namespace LLVM |
| 87 | } // namespace mlir |
| 88 | |
| 89 | #endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ |
| 90 | |