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(Operation *moduleOp); 37 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintU64Fn(Operation *moduleOp); 38 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF16Fn(Operation *moduleOp); 39 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintBF16Fn(Operation *moduleOp); 40 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF32Fn(Operation *moduleOp); 41 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintF64Fn(Operation *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 FailureOr<LLVM::LLVMFuncOp> 46 lookupOrCreatePrintStringFn(Operation *moduleOp, 47 std::optional<StringRef> runtimeFunctionName = {}); 48 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintOpenFn(Operation *moduleOp); 49 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintCloseFn(Operation *moduleOp); 50 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintCommaFn(Operation *moduleOp); 51 FailureOr<LLVM::LLVMFuncOp> lookupOrCreatePrintNewlineFn(Operation *moduleOp); 52 FailureOr<LLVM::LLVMFuncOp> lookupOrCreateMallocFn(Operation *moduleOp, 53 Type indexType); 54 FailureOr<LLVM::LLVMFuncOp> lookupOrCreateAlignedAllocFn(Operation *moduleOp, 55 Type indexType); 56 FailureOr<LLVM::LLVMFuncOp> lookupOrCreateFreeFn(Operation *moduleOp); 57 FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericAllocFn(Operation *moduleOp, 58 Type indexType); 59 FailureOr<LLVM::LLVMFuncOp> 60 lookupOrCreateGenericAlignedAllocFn(Operation *moduleOp, Type indexType); 61 FailureOr<LLVM::LLVMFuncOp> lookupOrCreateGenericFreeFn(Operation *moduleOp); 62 FailureOr<LLVM::LLVMFuncOp> 63 lookupOrCreateMemRefCopyFn(Operation *moduleOp, Type indexType, 64 Type unrankedDescriptorType); 65 66 /// Create a FuncOp with signature `resultType`(`paramTypes`)` and name `name`. 67 /// Return a failure if the FuncOp found has unexpected signature. 68 FailureOr<LLVM::LLVMFuncOp> 69 lookupOrCreateFn(Operation *moduleOp, StringRef name, 70 ArrayRef<Type> paramTypes = {}, Type resultType = {}, 71 bool isVarArg = false, bool isReserved = false); 72 73 } // namespace LLVM 74 } // namespace mlir 75 76 #endif // MLIR_DIALECT_LLVMIR_FUNCTIONCALLUTILS_H_ 77