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 | #include <optional> |
20 | |
21 | namespace mlir { |
22 | class Location; |
23 | class ModuleOp; |
24 | class OpBuilder; |
25 | class Operation; |
26 | class Type; |
27 | class ValueRange; |
28 | |
29 | namespace LLVM { |
30 | class LLVMFuncOp; |
31 | |
32 | /// Helper functions to lookup 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 | LLVM::LLVMFuncOp lookupOrCreatePrintI64Fn(ModuleOp moduleOp); |
37 | LLVM::LLVMFuncOp lookupOrCreatePrintU64Fn(ModuleOp moduleOp); |
38 | LLVM::LLVMFuncOp lookupOrCreatePrintF16Fn(ModuleOp moduleOp); |
39 | LLVM::LLVMFuncOp lookupOrCreatePrintBF16Fn(ModuleOp moduleOp); |
40 | LLVM::LLVMFuncOp lookupOrCreatePrintF32Fn(ModuleOp moduleOp); |
41 | LLVM::LLVMFuncOp lookupOrCreatePrintF64Fn(ModuleOp moduleOp); |
42 | /// Declares a function to print a C-string. |
43 | /// If a custom runtime function is defined via `runtimeFunctionName`, it must |
44 | /// have the signature void(char const*). The default function is `printString`. |
45 | LLVM::LLVMFuncOp |
46 | lookupOrCreatePrintStringFn(ModuleOp moduleOp, |
47 | std::optional<StringRef> runtimeFunctionName = {}); |
48 | LLVM::LLVMFuncOp lookupOrCreatePrintOpenFn(ModuleOp moduleOp); |
49 | LLVM::LLVMFuncOp lookupOrCreatePrintCloseFn(ModuleOp moduleOp); |
50 | LLVM::LLVMFuncOp lookupOrCreatePrintCommaFn(ModuleOp moduleOp); |
51 | LLVM::LLVMFuncOp lookupOrCreatePrintNewlineFn(ModuleOp moduleOp); |
52 | LLVM::LLVMFuncOp lookupOrCreateMallocFn(ModuleOp moduleOp, Type indexType); |
53 | LLVM::LLVMFuncOp lookupOrCreateAlignedAllocFn(ModuleOp moduleOp, |
54 | Type indexType); |
55 | LLVM::LLVMFuncOp lookupOrCreateFreeFn(ModuleOp moduleOp); |
56 | LLVM::LLVMFuncOp lookupOrCreateGenericAllocFn(ModuleOp moduleOp, |
57 | Type indexType); |
58 | LLVM::LLVMFuncOp lookupOrCreateGenericAlignedAllocFn(ModuleOp moduleOp, |
59 | Type indexType); |
60 | LLVM::LLVMFuncOp lookupOrCreateGenericFreeFn(ModuleOp moduleOp); |
61 | LLVM::LLVMFuncOp lookupOrCreateMemRefCopyFn(ModuleOp moduleOp, Type indexType, |
62 | Type unrankedDescriptorType); |
63 | |
64 | /// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`. |
65 | LLVM::LLVMFuncOp lookupOrCreateFn(ModuleOp moduleOp, StringRef name, |
66 | ArrayRef<Type> paramTypes = {}, |
67 | Type resultType = {}, bool isVarArg = false); |
68 | |
69 | } // namespace LLVM |
70 | } // namespace mlir |
71 | |
72 | #endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ |
73 | |