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 lookup or create the declaration for commonly used
32 /// external C function calls. Such ops can then be invoked by creating a CallOp
33 /// with the proper arguments via `createLLVMCall`.
34 /// The list of functions provided here must be implemented separately (e.g. as
35 /// part of a support runtime library or as part of the libc).
36 LLVM::LLVMFuncOp lookupOrCreatePrintI64Fn(ModuleOp moduleOp);
37 LLVM::LLVMFuncOp lookupOrCreatePrintU64Fn(ModuleOp moduleOp);
38 LLVM::LLVMFuncOp lookupOrCreatePrintF32Fn(ModuleOp moduleOp);
39 LLVM::LLVMFuncOp lookupOrCreatePrintF64Fn(ModuleOp moduleOp);
40 LLVM::LLVMFuncOp lookupOrCreatePrintOpenFn(ModuleOp moduleOp);
41 LLVM::LLVMFuncOp lookupOrCreatePrintCloseFn(ModuleOp moduleOp);
42 LLVM::LLVMFuncOp lookupOrCreatePrintCommaFn(ModuleOp moduleOp);
43 LLVM::LLVMFuncOp lookupOrCreatePrintNewlineFn(ModuleOp moduleOp);
44 LLVM::LLVMFuncOp lookupOrCreateMallocFn(ModuleOp moduleOp, Type indexType);
45 LLVM::LLVMFuncOp lookupOrCreateAlignedAllocFn(ModuleOp moduleOp,
46                                               Type indexType);
47 LLVM::LLVMFuncOp lookupOrCreateFreeFn(ModuleOp moduleOp);
48 LLVM::LLVMFuncOp lookupOrCreateGenericAllocFn(ModuleOp moduleOp,
49                                               Type indexType);
50 LLVM::LLVMFuncOp lookupOrCreateGenericAlignedAllocFn(ModuleOp moduleOp,
51                                                      Type indexType);
52 LLVM::LLVMFuncOp lookupOrCreateGenericFreeFn(ModuleOp moduleOp);
53 LLVM::LLVMFuncOp lookupOrCreateMemRefCopyFn(ModuleOp moduleOp, Type indexType,
54                                             Type unrankedDescriptorType);
55 
56 /// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`.
57 LLVM::LLVMFuncOp lookupOrCreateFn(ModuleOp moduleOp, StringRef name,
58                                   ArrayRef<Type> paramTypes = {},
59                                   Type resultType = {});
60 
61 /// Helper wrapper to create a call to `fn` with `args` and `resultTypes`.
62 Operation::result_range createLLVMCall(OpBuilder &b, Location loc,
63                                        LLVM::LLVMFuncOp fn,
64                                        ValueRange args = {},
65                                        ArrayRef<Type> resultTypes = {});
66 
67 } // namespace LLVM
68 } // namespace mlir
69 
70 #endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_
71