MLIR  20.0.0git
FunctionCallUtils.cpp
Go to the documentation of this file.
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 
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 kPrintString = "printString";
34 static constexpr llvm::StringRef kPrintOpen = "printOpen";
35 static constexpr llvm::StringRef kPrintClose = "printClose";
36 static constexpr llvm::StringRef kPrintComma = "printComma";
37 static constexpr llvm::StringRef kPrintNewline = "printNewline";
38 static constexpr llvm::StringRef kMalloc = "malloc";
39 static constexpr llvm::StringRef kAlignedAlloc = "aligned_alloc";
40 static constexpr llvm::StringRef kFree = "free";
41 static constexpr llvm::StringRef kGenericAlloc = "_mlir_memref_to_llvm_alloc";
42 static constexpr llvm::StringRef kGenericAlignedAlloc =
43  "_mlir_memref_to_llvm_aligned_alloc";
44 static constexpr llvm::StringRef kGenericFree = "_mlir_memref_to_llvm_free";
45 static constexpr llvm::StringRef kMemRefCopy = "memrefCopy";
46 
47 /// Generic print function lookupOrCreate helper.
48 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateFn(Operation *moduleOp,
49  StringRef name,
50  ArrayRef<Type> paramTypes,
51  Type resultType, bool isVarArg) {
52  assert(moduleOp->hasTrait<OpTrait::SymbolTable>() &&
53  "expected SymbolTable operation");
54  auto func = llvm::dyn_cast_or_null<LLVM::LLVMFuncOp>(
55  SymbolTable::lookupSymbolIn(moduleOp, name));
56  if (func)
57  return func;
58  OpBuilder b(moduleOp->getRegion(0));
59  return b.create<LLVM::LLVMFuncOp>(
60  moduleOp->getLoc(), name,
61  LLVM::LLVMFunctionType::get(resultType, paramTypes, isVarArg));
62 }
63 
64 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintI64Fn(Operation *moduleOp) {
65  return lookupOrCreateFn(moduleOp, kPrintI64,
66  IntegerType::get(moduleOp->getContext(), 64),
67  LLVM::LLVMVoidType::get(moduleOp->getContext()));
68 }
69 
70 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintU64Fn(Operation *moduleOp) {
71  return lookupOrCreateFn(moduleOp, kPrintU64,
72  IntegerType::get(moduleOp->getContext(), 64),
73  LLVM::LLVMVoidType::get(moduleOp->getContext()));
74 }
75 
76 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintF16Fn(Operation *moduleOp) {
77  return lookupOrCreateFn(moduleOp, kPrintF16,
78  IntegerType::get(moduleOp->getContext(), 16), // bits!
79  LLVM::LLVMVoidType::get(moduleOp->getContext()));
80 }
81 
82 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintBF16Fn(Operation *moduleOp) {
83  return lookupOrCreateFn(moduleOp, kPrintBF16,
84  IntegerType::get(moduleOp->getContext(), 16), // bits!
85  LLVM::LLVMVoidType::get(moduleOp->getContext()));
86 }
87 
88 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintF32Fn(Operation *moduleOp) {
89  return lookupOrCreateFn(moduleOp, kPrintF32,
90  Float32Type::get(moduleOp->getContext()),
91  LLVM::LLVMVoidType::get(moduleOp->getContext()));
92 }
93 
94 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintF64Fn(Operation *moduleOp) {
95  return lookupOrCreateFn(moduleOp, kPrintF64,
96  Float64Type::get(moduleOp->getContext()),
97  LLVM::LLVMVoidType::get(moduleOp->getContext()));
98 }
99 
100 static LLVM::LLVMPointerType getCharPtr(MLIRContext *context) {
101  return LLVM::LLVMPointerType::get(context);
102 }
103 
104 static LLVM::LLVMPointerType getVoidPtr(MLIRContext *context) {
105  // A char pointer and void ptr are the same in LLVM IR.
106  return getCharPtr(context);
107 }
108 
110  Operation *moduleOp, std::optional<StringRef> runtimeFunctionName) {
111  return lookupOrCreateFn(moduleOp, runtimeFunctionName.value_or(kPrintString),
112  getCharPtr(moduleOp->getContext()),
113  LLVM::LLVMVoidType::get(moduleOp->getContext()));
114 }
115 
117  return lookupOrCreateFn(moduleOp, kPrintOpen, {},
118  LLVM::LLVMVoidType::get(moduleOp->getContext()));
119 }
120 
122  return lookupOrCreateFn(moduleOp, kPrintClose, {},
123  LLVM::LLVMVoidType::get(moduleOp->getContext()));
124 }
125 
127  return lookupOrCreateFn(moduleOp, kPrintComma, {},
128  LLVM::LLVMVoidType::get(moduleOp->getContext()));
129 }
130 
132  return lookupOrCreateFn(moduleOp, kPrintNewline, {},
133  LLVM::LLVMVoidType::get(moduleOp->getContext()));
134 }
135 
137  Type indexType) {
138  return LLVM::lookupOrCreateFn(moduleOp, kMalloc, indexType,
139  getVoidPtr(moduleOp->getContext()));
140 }
141 
143  Type indexType) {
144  return LLVM::lookupOrCreateFn(moduleOp, kAlignedAlloc, {indexType, indexType},
145  getVoidPtr(moduleOp->getContext()));
146 }
147 
148 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateFreeFn(Operation *moduleOp) {
149  return LLVM::lookupOrCreateFn(
150  moduleOp, kFree, getVoidPtr(moduleOp->getContext()),
151  LLVM::LLVMVoidType::get(moduleOp->getContext()));
152 }
153 
155  Type indexType) {
156  return LLVM::lookupOrCreateFn(moduleOp, kGenericAlloc, indexType,
157  getVoidPtr(moduleOp->getContext()));
158 }
159 
160 LLVM::LLVMFuncOp
162  Type indexType) {
164  {indexType, indexType},
165  getVoidPtr(moduleOp->getContext()));
166 }
167 
169  return LLVM::lookupOrCreateFn(
170  moduleOp, kGenericFree, getVoidPtr(moduleOp->getContext()),
171  LLVM::LLVMVoidType::get(moduleOp->getContext()));
172 }
173 
174 LLVM::LLVMFuncOp
176  Type unrankedDescriptorType) {
177  return LLVM::lookupOrCreateFn(
178  moduleOp, kMemRefCopy,
179  ArrayRef<Type>{indexType, unrankedDescriptorType, unrankedDescriptorType},
180  LLVM::LLVMVoidType::get(moduleOp->getContext()));
181 }
static LLVM::LLVMPointerType getVoidPtr(MLIRContext *context)
static constexpr llvm::StringRef kPrintI64
Helper functions to lookup or create the declaration for commonly used external C function calls.
static constexpr llvm::StringRef kFree
static constexpr llvm::StringRef kPrintU64
static constexpr llvm::StringRef kPrintBF16
static constexpr llvm::StringRef kPrintString
static constexpr llvm::StringRef kGenericAlignedAlloc
static constexpr llvm::StringRef kAlignedAlloc
static constexpr llvm::StringRef kPrintClose
static constexpr llvm::StringRef kMalloc
static constexpr llvm::StringRef kMemRefCopy
static constexpr llvm::StringRef kPrintComma
static constexpr llvm::StringRef kGenericAlloc
static constexpr llvm::StringRef kPrintNewline
static LLVM::LLVMPointerType getCharPtr(MLIRContext *context)
static constexpr llvm::StringRef kPrintOpen
static constexpr llvm::StringRef kGenericFree
static constexpr llvm::StringRef kPrintF16
static constexpr llvm::StringRef kPrintF32
static constexpr llvm::StringRef kPrintF64
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:215
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:497
A trait used to provide symbol table functionalities to a region operation.
Definition: SymbolTable.h:435
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:745
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition: Operation.h:682
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
LLVM::LLVMFuncOp lookupOrCreateMemRefCopyFn(Operation *moduleOp, Type indexType, Type unrankedDescriptorType)
LLVM::LLVMFuncOp lookupOrCreatePrintNewlineFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreateFreeFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreateGenericFreeFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintI64Fn(Operation *moduleOp)
Helper functions to lookup or create the declaration for commonly used external C function calls.
LLVM::LLVMFuncOp lookupOrCreatePrintOpenFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreateAlignedAllocFn(Operation *moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateGenericAllocFn(Operation *moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateGenericAlignedAllocFn(Operation *moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreatePrintStringFn(Operation *moduleOp, std::optional< StringRef > runtimeFunctionName={})
Declares a function to print a C-string.
LLVM::LLVMFuncOp lookupOrCreateFn(Operation *moduleOp, StringRef name, ArrayRef< Type > paramTypes={}, Type resultType={}, bool isVarArg=false)
Create a FuncOp with signature resultType(paramTypes)and namename`.
LLVM::LLVMFuncOp lookupOrCreatePrintBF16Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF16Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF32Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintCloseFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreateMallocFn(Operation *moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreatePrintF64Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintCommaFn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintU64Fn(Operation *moduleOp)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...