| 1 | //===- FunctionCallUtils.cpp - Utilities for C function calls -------------===// |
| 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 implements helper functions to call common simple C functions in |
| 10 | // LLVMIR (e.g. amon others to support printing and debugging). |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/LLVMIR/FunctionCallUtils.h" |
| 15 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 16 | #include "mlir/IR/Builders.h" |
| 17 | #include "mlir/IR/OpDefinition.h" |
| 18 | #include "mlir/Support/LLVM.h" |
| 19 | |
| 20 | using namespace mlir; |
| 21 | using namespace mlir::LLVM; |
| 22 | |
| 23 | /// Helper functions to lookup or create the declaration for commonly used |
| 24 | /// external C function calls. The list of functions provided here must be |
| 25 | /// implemented separately (e.g. as part of a support runtime library or as |
| 26 | /// part of the libc). |
| 27 | static constexpr llvm::StringRef kPrintI64 = "printI64" ; |
| 28 | static constexpr llvm::StringRef kPrintU64 = "printU64" ; |
| 29 | static constexpr llvm::StringRef kPrintF16 = "printF16" ; |
| 30 | static constexpr llvm::StringRef kPrintBF16 = "printBF16" ; |
| 31 | static constexpr llvm::StringRef kPrintF32 = "printF32" ; |
| 32 | static constexpr llvm::StringRef kPrintF64 = "printF64" ; |
| 33 | static constexpr llvm::StringRef kPrintApFloat = "printApFloat" ; |
| 34 | static constexpr llvm::StringRef kPrintString = "printString" ; |
| 35 | static constexpr llvm::StringRef kPrintOpen = "printOpen" ; |
| 36 | static constexpr llvm::StringRef kPrintClose = "printClose" ; |
| 37 | static constexpr llvm::StringRef kPrintComma = "printComma" ; |
| 38 | static constexpr llvm::StringRef kPrintNewline = "printNewline" ; |
| 39 | static constexpr llvm::StringRef kMalloc = "malloc" ; |
| 40 | static constexpr llvm::StringRef kAlignedAlloc = "aligned_alloc" ; |
| 41 | static constexpr llvm::StringRef kFree = "free" ; |
| 42 | static constexpr llvm::StringRef kGenericAlloc = "_mlir_memref_to_llvm_alloc" ; |
| 43 | static constexpr llvm::StringRef kGenericAlignedAlloc = |
| 44 | "_mlir_memref_to_llvm_aligned_alloc" ; |
| 45 | static constexpr llvm::StringRef kGenericFree = "_mlir_memref_to_llvm_free" ; |
| 46 | static constexpr llvm::StringRef kMemRefCopy = "memrefCopy" ; |
| 47 | |
| 48 | namespace { |
| 49 | /// Search for an LLVMFuncOp with a given name within an operation with the |
| 50 | /// SymbolTable trait. An optional collection of cached symbol tables can be |
| 51 | /// given to avoid a linear scan of the symbol table operation. |
| 52 | LLVM::LLVMFuncOp lookupFuncOp(StringRef name, Operation *symbolTableOp, |
| 53 | SymbolTableCollection *symbolTables = nullptr) { |
| 54 | if (symbolTables) { |
| 55 | return symbolTables->lookupSymbolIn<LLVM::LLVMFuncOp>( |
| 56 | symbolTableOp, name: StringAttr::get(context: symbolTableOp->getContext(), bytes: name)); |
| 57 | } |
| 58 | |
| 59 | return llvm::dyn_cast_or_null<LLVM::LLVMFuncOp>( |
| 60 | Val: SymbolTable::lookupSymbolIn(op: symbolTableOp, symbol: name)); |
| 61 | } |
| 62 | } // namespace |
| 63 | |
| 64 | /// Generic print function lookupOrCreate helper. |
| 65 | FailureOr<LLVM::LLVMFuncOp> |
| 66 | mlir::LLVM::lookupOrCreateFn(OpBuilder &b, Operation *moduleOp, StringRef name, |
| 67 | ArrayRef<Type> paramTypes, Type resultType, |
| 68 | bool isVarArg, bool isReserved, |
| 69 | SymbolTableCollection *symbolTables) { |
| 70 | assert(moduleOp->hasTrait<OpTrait::SymbolTable>() && |
| 71 | "expected SymbolTable operation" ); |
| 72 | auto func = lookupFuncOp(name, symbolTableOp: moduleOp, symbolTables); |
| 73 | auto funcT = LLVMFunctionType::get(result: resultType, arguments: paramTypes, isVarArg); |
| 74 | // Assert the signature of the found function is same as expected |
| 75 | if (func) { |
| 76 | if (funcT != func.getFunctionType()) { |
| 77 | if (isReserved) { |
| 78 | func.emitError(message: "redefinition of reserved function '" ) |
| 79 | << name << "' of different type " << func.getFunctionType() |
| 80 | << " is prohibited" ; |
| 81 | } else { |
| 82 | func.emitError(message: "redefinition of function '" ) |
| 83 | << name << "' of different type " << funcT << " is prohibited" ; |
| 84 | } |
| 85 | return failure(); |
| 86 | } |
| 87 | return func; |
| 88 | } |
| 89 | |
| 90 | // A symbol with this name may already exist as a non-LLVM function (e.g., |
| 91 | // func::FuncOp from user code that hasn't been converted to LLVM dialect |
| 92 | // yet). Creating a new LLVMFuncOp with the same name would cause a symbol |
| 93 | // redefinition error. Return failure so the calling pattern can retry after |
| 94 | // the existing symbol is converted. |
| 95 | if (symbolTables |
| 96 | ? symbolTables->lookupSymbolIn( |
| 97 | symbolTableOp: moduleOp, symbol: StringAttr::get(context: moduleOp->getContext(), bytes: name)) |
| 98 | : SymbolTable::lookupSymbolIn(op: moduleOp, symbol: name)) |
| 99 | return failure(); |
| 100 | |
| 101 | OpBuilder::InsertionGuard g(b); |
| 102 | assert(!moduleOp->getRegion(0).empty() && "expected non-empty region" ); |
| 103 | b.setInsertionPointToStart(&moduleOp->getRegion(index: 0).front()); |
| 104 | auto funcOp = LLVM::LLVMFuncOp::create( |
| 105 | builder&: b, location: moduleOp->getLoc(), name, |
| 106 | type: LLVM::LLVMFunctionType::get(result: resultType, arguments: paramTypes, isVarArg)); |
| 107 | |
| 108 | if (symbolTables) { |
| 109 | SymbolTable &symbolTable = symbolTables->getSymbolTable(op: moduleOp); |
| 110 | symbolTable.insert(symbol: funcOp, insertPt: moduleOp->getRegion(index: 0).front().begin()); |
| 111 | } |
| 112 | |
| 113 | return funcOp; |
| 114 | } |
| 115 | |
| 116 | static FailureOr<LLVM::LLVMFuncOp> |
| 117 | lookupOrCreateReservedFn(OpBuilder &b, Operation *moduleOp, StringRef name, |
| 118 | ArrayRef<Type> paramTypes, Type resultType, |
| 119 | SymbolTableCollection *symbolTables) { |
| 120 | return lookupOrCreateFn(b, moduleOp, name, paramTypes, resultType, |
| 121 | /*isVarArg=*/false, /*isReserved=*/true, |
| 122 | symbolTables); |
| 123 | } |
| 124 | |
| 125 | FailureOr<LLVM::LLVMFuncOp> |
| 126 | mlir::LLVM::lookupOrCreatePrintI64Fn(OpBuilder &b, Operation *moduleOp, |
| 127 | SymbolTableCollection *symbolTables) { |
| 128 | return lookupOrCreateReservedFn( |
| 129 | b, moduleOp, name: kPrintI64, paramTypes: IntegerType::get(context: moduleOp->getContext(), width: 64), |
| 130 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 131 | } |
| 132 | |
| 133 | FailureOr<LLVM::LLVMFuncOp> |
| 134 | mlir::LLVM::lookupOrCreatePrintU64Fn(OpBuilder &b, Operation *moduleOp, |
| 135 | SymbolTableCollection *symbolTables) { |
| 136 | return lookupOrCreateReservedFn( |
| 137 | b, moduleOp, name: kPrintU64, paramTypes: IntegerType::get(context: moduleOp->getContext(), width: 64), |
| 138 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 139 | } |
| 140 | |
| 141 | FailureOr<LLVM::LLVMFuncOp> |
| 142 | mlir::LLVM::lookupOrCreatePrintF16Fn(OpBuilder &b, Operation *moduleOp, |
| 143 | SymbolTableCollection *symbolTables) { |
| 144 | return lookupOrCreateReservedFn( |
| 145 | b, moduleOp, name: kPrintF16, |
| 146 | paramTypes: IntegerType::get(context: moduleOp->getContext(), width: 16), // bits! |
| 147 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 148 | } |
| 149 | |
| 150 | FailureOr<LLVM::LLVMFuncOp> |
| 151 | mlir::LLVM::lookupOrCreatePrintBF16Fn(OpBuilder &b, Operation *moduleOp, |
| 152 | SymbolTableCollection *symbolTables) { |
| 153 | return lookupOrCreateReservedFn( |
| 154 | b, moduleOp, name: kPrintBF16, |
| 155 | paramTypes: IntegerType::get(context: moduleOp->getContext(), width: 16), // bits! |
| 156 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 157 | } |
| 158 | |
| 159 | FailureOr<LLVM::LLVMFuncOp> |
| 160 | mlir::LLVM::lookupOrCreatePrintF32Fn(OpBuilder &b, Operation *moduleOp, |
| 161 | SymbolTableCollection *symbolTables) { |
| 162 | return lookupOrCreateReservedFn( |
| 163 | b, moduleOp, name: kPrintF32, paramTypes: Float32Type::get(context: moduleOp->getContext()), |
| 164 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 165 | } |
| 166 | |
| 167 | FailureOr<LLVM::LLVMFuncOp> |
| 168 | mlir::LLVM::lookupOrCreatePrintF64Fn(OpBuilder &b, Operation *moduleOp, |
| 169 | SymbolTableCollection *symbolTables) { |
| 170 | return lookupOrCreateReservedFn( |
| 171 | b, moduleOp, name: kPrintF64, paramTypes: Float64Type::get(context: moduleOp->getContext()), |
| 172 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 173 | } |
| 174 | |
| 175 | FailureOr<LLVM::LLVMFuncOp> |
| 176 | mlir::LLVM::lookupOrCreateApFloatPrintFn(OpBuilder &b, Operation *moduleOp, |
| 177 | SymbolTableCollection *symbolTables) { |
| 178 | return lookupOrCreateReservedFn( |
| 179 | b, moduleOp, name: kPrintApFloat, |
| 180 | paramTypes: {IntegerType::get(context: moduleOp->getContext(), width: 32), |
| 181 | IntegerType::get(context: moduleOp->getContext(), width: 64)}, |
| 182 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 183 | } |
| 184 | |
| 185 | static LLVM::LLVMPointerType getCharPtr(MLIRContext *context) { |
| 186 | return LLVM::LLVMPointerType::get(context); |
| 187 | } |
| 188 | |
| 189 | static LLVM::LLVMPointerType getVoidPtr(MLIRContext *context) { |
| 190 | // A char pointer and void ptr are the same in LLVM IR. |
| 191 | return getCharPtr(context); |
| 192 | } |
| 193 | |
| 194 | FailureOr<LLVM::LLVMFuncOp> mlir::LLVM::lookupOrCreatePrintStringFn( |
| 195 | OpBuilder &b, Operation *moduleOp, |
| 196 | std::optional<StringRef> runtimeFunctionName, |
| 197 | SymbolTableCollection *symbolTables) { |
| 198 | return lookupOrCreateReservedFn( |
| 199 | b, moduleOp, name: runtimeFunctionName.value_or(u: kPrintString), |
| 200 | paramTypes: getCharPtr(context: moduleOp->getContext()), |
| 201 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 202 | } |
| 203 | |
| 204 | FailureOr<LLVM::LLVMFuncOp> |
| 205 | mlir::LLVM::lookupOrCreatePrintOpenFn(OpBuilder &b, Operation *moduleOp, |
| 206 | SymbolTableCollection *symbolTables) { |
| 207 | return lookupOrCreateReservedFn( |
| 208 | b, moduleOp, name: kPrintOpen, paramTypes: {}, |
| 209 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 210 | } |
| 211 | |
| 212 | FailureOr<LLVM::LLVMFuncOp> |
| 213 | mlir::LLVM::lookupOrCreatePrintCloseFn(OpBuilder &b, Operation *moduleOp, |
| 214 | SymbolTableCollection *symbolTables) { |
| 215 | return lookupOrCreateReservedFn( |
| 216 | b, moduleOp, name: kPrintClose, paramTypes: {}, |
| 217 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 218 | } |
| 219 | |
| 220 | FailureOr<LLVM::LLVMFuncOp> |
| 221 | mlir::LLVM::lookupOrCreatePrintCommaFn(OpBuilder &b, Operation *moduleOp, |
| 222 | SymbolTableCollection *symbolTables) { |
| 223 | return lookupOrCreateReservedFn( |
| 224 | b, moduleOp, name: kPrintComma, paramTypes: {}, |
| 225 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 226 | } |
| 227 | |
| 228 | FailureOr<LLVM::LLVMFuncOp> |
| 229 | mlir::LLVM::lookupOrCreatePrintNewlineFn(OpBuilder &b, Operation *moduleOp, |
| 230 | SymbolTableCollection *symbolTables) { |
| 231 | return lookupOrCreateReservedFn( |
| 232 | b, moduleOp, name: kPrintNewline, paramTypes: {}, |
| 233 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 234 | } |
| 235 | |
| 236 | FailureOr<LLVM::LLVMFuncOp> |
| 237 | mlir::LLVM::lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, |
| 238 | Type indexType, |
| 239 | SymbolTableCollection *symbolTables) { |
| 240 | return lookupOrCreateReservedFn(b, moduleOp, name: kMalloc, paramTypes: indexType, |
| 241 | resultType: getVoidPtr(context: moduleOp->getContext()), |
| 242 | symbolTables); |
| 243 | } |
| 244 | |
| 245 | FailureOr<LLVM::LLVMFuncOp> |
| 246 | mlir::LLVM::lookupOrCreateAlignedAllocFn(OpBuilder &b, Operation *moduleOp, |
| 247 | Type indexType, |
| 248 | SymbolTableCollection *symbolTables) { |
| 249 | return lookupOrCreateReservedFn( |
| 250 | b, moduleOp, name: kAlignedAlloc, paramTypes: {indexType, indexType}, |
| 251 | resultType: getVoidPtr(context: moduleOp->getContext()), symbolTables); |
| 252 | } |
| 253 | |
| 254 | FailureOr<LLVM::LLVMFuncOp> |
| 255 | mlir::LLVM::lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp, |
| 256 | SymbolTableCollection *symbolTables) { |
| 257 | return lookupOrCreateReservedFn( |
| 258 | b, moduleOp, name: kFree, paramTypes: getVoidPtr(context: moduleOp->getContext()), |
| 259 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 260 | } |
| 261 | |
| 262 | FailureOr<LLVM::LLVMFuncOp> |
| 263 | mlir::LLVM::lookupOrCreateGenericAllocFn(OpBuilder &b, Operation *moduleOp, |
| 264 | Type indexType, |
| 265 | SymbolTableCollection *symbolTables) { |
| 266 | return lookupOrCreateReservedFn(b, moduleOp, name: kGenericAlloc, paramTypes: indexType, |
| 267 | resultType: getVoidPtr(context: moduleOp->getContext()), |
| 268 | symbolTables); |
| 269 | } |
| 270 | |
| 271 | FailureOr<LLVM::LLVMFuncOp> mlir::LLVM::lookupOrCreateGenericAlignedAllocFn( |
| 272 | OpBuilder &b, Operation *moduleOp, Type indexType, |
| 273 | SymbolTableCollection *symbolTables) { |
| 274 | return lookupOrCreateReservedFn( |
| 275 | b, moduleOp, name: kGenericAlignedAlloc, paramTypes: {indexType, indexType}, |
| 276 | resultType: getVoidPtr(context: moduleOp->getContext()), symbolTables); |
| 277 | } |
| 278 | |
| 279 | FailureOr<LLVM::LLVMFuncOp> |
| 280 | mlir::LLVM::lookupOrCreateGenericFreeFn(OpBuilder &b, Operation *moduleOp, |
| 281 | SymbolTableCollection *symbolTables) { |
| 282 | return lookupOrCreateReservedFn( |
| 283 | b, moduleOp, name: kGenericFree, paramTypes: getVoidPtr(context: moduleOp->getContext()), |
| 284 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 285 | } |
| 286 | |
| 287 | FailureOr<LLVM::LLVMFuncOp> mlir::LLVM::lookupOrCreateMemRefCopyFn( |
| 288 | OpBuilder &b, Operation *moduleOp, Type indexType, |
| 289 | Type unrankedDescriptorType, SymbolTableCollection *symbolTables) { |
| 290 | return lookupOrCreateReservedFn( |
| 291 | b, moduleOp, name: kMemRefCopy, |
| 292 | paramTypes: ArrayRef<Type>{indexType, unrankedDescriptorType, unrankedDescriptorType}, |
| 293 | resultType: LLVM::LLVMVoidType::get(ctx: moduleOp->getContext()), symbolTables); |
| 294 | } |
| 295 | |