MLIR 22.0.0git
FunctionCallUtils.h
Go to the documentation of this file.
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;
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);
55FailureOr<LLVM::LLVMFuncOp>
56lookupOrCreateApFloatPrintFn(OpBuilder &b, Operation *moduleOp,
57 SymbolTableCollection *symbolTables = nullptr);
58
59/// Declares a function to print a C-string.
60/// If a custom runtime function is defined via `runtimeFunctionName`, it must
61/// have the signature void(char const*). The default function is `printString`.
62FailureOr<LLVM::LLVMFuncOp>
63lookupOrCreatePrintStringFn(OpBuilder &b, Operation *moduleOp,
64 std::optional<StringRef> runtimeFunctionName = {},
65 SymbolTableCollection *symbolTables = nullptr);
66FailureOr<LLVM::LLVMFuncOp>
67lookupOrCreatePrintOpenFn(OpBuilder &b, Operation *moduleOp,
68 SymbolTableCollection *symbolTables = nullptr);
69FailureOr<LLVM::LLVMFuncOp>
70lookupOrCreatePrintCloseFn(OpBuilder &b, Operation *moduleOp,
71 SymbolTableCollection *symbolTables = nullptr);
72FailureOr<LLVM::LLVMFuncOp>
73lookupOrCreatePrintCommaFn(OpBuilder &b, Operation *moduleOp,
74 SymbolTableCollection *symbolTables = nullptr);
75FailureOr<LLVM::LLVMFuncOp>
76lookupOrCreatePrintNewlineFn(OpBuilder &b, Operation *moduleOp,
77 SymbolTableCollection *symbolTables = nullptr);
78FailureOr<LLVM::LLVMFuncOp>
79lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
80 SymbolTableCollection *symbolTables = nullptr);
81FailureOr<LLVM::LLVMFuncOp>
82lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
83 SymbolTableCollection *symbolTables = nullptr);
84FailureOr<LLVM::LLVMFuncOp>
85lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp,
86 SymbolTableCollection *symbolTables = nullptr);
87FailureOr<LLVM::LLVMFuncOp>
88lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType,
89 SymbolTableCollection *symbolTables = nullptr);
90FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericAlignedAllocFn(
91 OpBuilder &b, Operation *moduleOp, Type indexType,
92 SymbolTableCollection *symbolTables = nullptr);
93FailureOr<LLVM::LLVMFuncOp>
94lookupOrCreateGenericFreeFn(OpBuilder &b, Operation *moduleOp,
95 SymbolTableCollection *symbolTables = nullptr);
96FailureOr<LLVM::LLVMFuncOp>
97lookupOrCreateMemRefCopyFn(OpBuilder &b, Operation *moduleOp, Type indexType,
98 Type unrankedDescriptorType,
99 SymbolTableCollection *symbolTables = nullptr);
100
101/// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`.
102/// Return a failure if the FuncOp found has unexpected signature.
103FailureOr<LLVM::LLVMFuncOp>
104lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name,
105 ArrayRef<Type> paramTypes = {}, Type resultType = {},
106 bool isVarArg = false, bool isReserved = false,
107 SymbolTableCollection *symbolTables = nullptr);
108
109} // namespace LLVM
110} // namespace mlir
111
112#endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class represents a collection of SymbolTables.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintBF16Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintOpenFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateMemRefCopyFn(OpBuilder &b, Operation *moduleOp, Type indexType, Type unrankedDescriptorType, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintCommaFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintI64Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
Helper functions to look up or create the declaration for commonly used external C function calls.
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintNewlineFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintCloseFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintU64Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateGenericAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintF32Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, Type indexType, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintStringFn(OpBuilder &b, Operation *moduleOp, std::optional< StringRef > runtimeFunctionName={}, SymbolTableCollection *symbolTables=nullptr)
Declares a function to print a C-string.
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateApFloatPrintFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintF16Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateGenericFreeFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreatePrintF64Fn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name, ArrayRef< Type > paramTypes={}, Type resultType={}, bool isVarArg=false, bool isReserved=false, SymbolTableCollection *symbolTables=nullptr)
Create a FuncOp with signature resultType(paramTypes) and name name`.
Include the generated interface declarations.