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
20namespace mlir {
21class Location;
22class ModuleOp;
23class OpBuilder;
24class Operation;
25class Type;
26class ValueRange;
27class SymbolTableCollection;
28
29namespace LLVM {
30class 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.
37FailureOr<LLVM::LLVMFuncOp>
38lookupOrCreatePrintI64Fn(OpBuilder &b, Operation *moduleOp,
39 SymbolTableCollection *symbolTables = nullptr);
40FailureOr<LLVM::LLVMFuncOp>
41lookupOrCreatePrintU64Fn(OpBuilder &b, Operation *moduleOp,
42 SymbolTableCollection *symbolTables = nullptr);
43FailureOr<LLVM::LLVMFuncOp>
44lookupOrCreatePrintF16Fn(OpBuilder &b, Operation *moduleOp,
45 SymbolTableCollection *symbolTables = nullptr);
46FailureOr<LLVM::LLVMFuncOp>
47lookupOrCreatePrintBF16Fn(OpBuilder &b, Operation *moduleOp,
48 SymbolTableCollection *symbolTables = nullptr);
49FailureOr<LLVM::LLVMFuncOp>
50lookupOrCreatePrintF32Fn(OpBuilder &b, Operation *moduleOp,
51 SymbolTableCollection *symbolTables = nullptr);
52FailureOr<LLVM::LLVMFuncOp>
53lookupOrCreatePrintF64Fn(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`.
58FailureOr<LLVM::LLVMFuncOp>
59lookupOrCreatePrintStringFn(OpBuilder &b, Operation *moduleOp,
60 std::optional<StringRef> runtimeFunctionName = {},
61 SymbolTableCollection *symbolTables = nullptr);
62FailureOr<LLVM::LLVMFuncOp>
63lookupOrCreatePrintOpenFn(OpBuilder &b, Operation *moduleOp,
64 SymbolTableCollection *symbolTables = nullptr);
65FailureOr<LLVM::LLVMFuncOp>
66lookupOrCreatePrintCloseFn(OpBuilder &b, Operation *moduleOp,
67 SymbolTableCollection *symbolTables = nullptr);
68FailureOr<LLVM::LLVMFuncOp>
69lookupOrCreatePrintCommaFn(OpBuilder &b, Operation *moduleOp,
70 SymbolTableCollection *symbolTables = nullptr);
71FailureOr<LLVM::LLVMFuncOp>
72lookupOrCreatePrintNewlineFn(OpBuilder &b, Operation *moduleOp,
73 SymbolTableCollection *symbolTables = nullptr);
74FailureOr<LLVM::LLVMFuncOp>
75lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
76 SymbolTableCollection *symbolTables = nullptr);
77FailureOr<LLVM::LLVMFuncOp>
78lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
79 SymbolTableCollection *symbolTables = nullptr);
80FailureOr<LLVM::LLVMFuncOp>
81lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp,
82 SymbolTableCollection *symbolTables = nullptr);
83FailureOr<LLVM::LLVMFuncOp>
84lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
85 SymbolTableCollection *symbolTables = nullptr);
86FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericAlignedAllocFn(
87 OpBuilder &b, Operation *moduleOp, Type indexType,
88 SymbolTableCollection *symbolTables = nullptr);
89FailureOr<LLVM::LLVMFuncOp>
90lookupOrCreateGenericFreeFn(OpBuilder &b, Operation *moduleOp,
91 SymbolTableCollection *symbolTables = nullptr);
92FailureOr<LLVM::LLVMFuncOp>
93lookupOrCreateMemRefCopyFn(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.
99FailureOr<LLVM::LLVMFuncOp>
100lookupOrCreateFn(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

source code of mlir/include/mlir/Dialect/LLVMIR/FunctionCallUtils.h