MLIR  17.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 kPrintF32 = "printF32";
30 static constexpr llvm::StringRef kPrintF64 = "printF64";
31 static constexpr llvm::StringRef kPrintStr = "puts";
32 static constexpr llvm::StringRef kPrintOpen = "printOpen";
33 static constexpr llvm::StringRef kPrintClose = "printClose";
34 static constexpr llvm::StringRef kPrintComma = "printComma";
35 static constexpr llvm::StringRef kPrintNewline = "printNewline";
36 static constexpr llvm::StringRef kMalloc = "malloc";
37 static constexpr llvm::StringRef kAlignedAlloc = "aligned_alloc";
38 static constexpr llvm::StringRef kFree = "free";
39 static constexpr llvm::StringRef kGenericAlloc = "_mlir_memref_to_llvm_alloc";
40 static constexpr llvm::StringRef kGenericAlignedAlloc =
41  "_mlir_memref_to_llvm_aligned_alloc";
42 static constexpr llvm::StringRef kGenericFree = "_mlir_memref_to_llvm_free";
43 static constexpr llvm::StringRef kMemRefCopy = "memrefCopy";
44 
45 /// Generic print function lookupOrCreate helper.
46 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateFn(ModuleOp moduleOp, StringRef name,
47  ArrayRef<Type> paramTypes,
48  Type resultType) {
49  auto func = moduleOp.lookupSymbol<LLVM::LLVMFuncOp>(name);
50  if (func)
51  return func;
52  OpBuilder b(moduleOp.getBodyRegion());
53  return b.create<LLVM::LLVMFuncOp>(
54  moduleOp->getLoc(), name,
55  LLVM::LLVMFunctionType::get(resultType, paramTypes));
56 }
57 
58 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintI64Fn(ModuleOp moduleOp) {
59  return lookupOrCreateFn(moduleOp, kPrintI64,
60  IntegerType::get(moduleOp->getContext(), 64),
61  LLVM::LLVMVoidType::get(moduleOp->getContext()));
62 }
63 
64 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintU64Fn(ModuleOp moduleOp) {
65  return lookupOrCreateFn(moduleOp, kPrintU64,
66  IntegerType::get(moduleOp->getContext(), 64),
67  LLVM::LLVMVoidType::get(moduleOp->getContext()));
68 }
69 
70 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintF32Fn(ModuleOp moduleOp) {
71  return lookupOrCreateFn(moduleOp, kPrintF32,
72  Float32Type::get(moduleOp->getContext()),
73  LLVM::LLVMVoidType::get(moduleOp->getContext()));
74 }
75 
76 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintF64Fn(ModuleOp moduleOp) {
77  return lookupOrCreateFn(moduleOp, kPrintF64,
78  Float64Type::get(moduleOp->getContext()),
79  LLVM::LLVMVoidType::get(moduleOp->getContext()));
80 }
81 
82 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintStrFn(ModuleOp moduleOp) {
83  return lookupOrCreateFn(
84  moduleOp, kPrintStr,
85  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)),
86  LLVM::LLVMVoidType::get(moduleOp->getContext()));
87 }
88 
89 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintOpenFn(ModuleOp moduleOp) {
90  return lookupOrCreateFn(moduleOp, kPrintOpen, {},
91  LLVM::LLVMVoidType::get(moduleOp->getContext()));
92 }
93 
94 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintCloseFn(ModuleOp moduleOp) {
95  return lookupOrCreateFn(moduleOp, kPrintClose, {},
96  LLVM::LLVMVoidType::get(moduleOp->getContext()));
97 }
98 
99 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintCommaFn(ModuleOp moduleOp) {
100  return lookupOrCreateFn(moduleOp, kPrintComma, {},
101  LLVM::LLVMVoidType::get(moduleOp->getContext()));
102 }
103 
104 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreatePrintNewlineFn(ModuleOp moduleOp) {
105  return lookupOrCreateFn(moduleOp, kPrintNewline, {},
106  LLVM::LLVMVoidType::get(moduleOp->getContext()));
107 }
108 
109 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateMallocFn(ModuleOp moduleOp,
110  Type indexType) {
111  return LLVM::lookupOrCreateFn(
112  moduleOp, kMalloc, indexType,
113  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)));
114 }
115 
116 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateAlignedAllocFn(ModuleOp moduleOp,
117  Type indexType) {
118  return LLVM::lookupOrCreateFn(
119  moduleOp, kAlignedAlloc, {indexType, indexType},
120  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)));
121 }
122 
123 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateFreeFn(ModuleOp moduleOp) {
124  return LLVM::lookupOrCreateFn(
125  moduleOp, kFree,
126  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)),
127  LLVM::LLVMVoidType::get(moduleOp->getContext()));
128 }
129 
130 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateGenericAllocFn(ModuleOp moduleOp,
131  Type indexType) {
132  return LLVM::lookupOrCreateFn(
133  moduleOp, kGenericAlloc, indexType,
134  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)));
135 }
136 
137 LLVM::LLVMFuncOp
139  Type indexType) {
140  return LLVM::lookupOrCreateFn(
141  moduleOp, kGenericAlignedAlloc, {indexType, indexType},
142  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)));
143 }
144 
145 LLVM::LLVMFuncOp mlir::LLVM::lookupOrCreateGenericFreeFn(ModuleOp moduleOp) {
146  return LLVM::lookupOrCreateFn(
147  moduleOp, kGenericFree,
148  LLVM::LLVMPointerType::get(IntegerType::get(moduleOp->getContext(), 8)),
149  LLVM::LLVMVoidType::get(moduleOp->getContext()));
150 }
151 
152 LLVM::LLVMFuncOp
153 mlir::LLVM::lookupOrCreateMemRefCopyFn(ModuleOp moduleOp, Type indexType,
154  Type unrankedDescriptorType) {
155  return LLVM::lookupOrCreateFn(
156  moduleOp, kMemRefCopy,
157  ArrayRef<Type>{indexType, unrankedDescriptorType, unrankedDescriptorType},
158  LLVM::LLVMVoidType::get(moduleOp->getContext()));
159 }
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 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 constexpr llvm::StringRef kPrintOpen
static constexpr llvm::StringRef kGenericFree
static constexpr llvm::StringRef kPrintF32
static constexpr llvm::StringRef kPrintF64
static constexpr llvm::StringRef kPrintStr
This class helps build Operations.
Definition: Builders.h:199
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:422
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
LLVM::LLVMFuncOp lookupOrCreateGenericFreeFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreateAlignedAllocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateMemRefCopyFn(ModuleOp moduleOp, Type indexType, Type unrankedDescriptorType)
LLVM::LLVMFuncOp lookupOrCreateGenericAlignedAllocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateFreeFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreateFn(ModuleOp moduleOp, StringRef name, ArrayRef< Type > paramTypes={}, Type resultType={})
Create a FuncOp with signature resultType(paramTypes)and namename`.
LLVM::LLVMFuncOp lookupOrCreateMallocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreatePrintU64Fn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF64Fn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreateGenericAllocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreatePrintNewlineFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintCommaFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintCloseFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF32Fn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintOpenFn(ModuleOp moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintI64Fn(ModuleOp moduleOp)
Helper functions to lookup or create the declaration for commonly used external C function calls.
LLVM::LLVMFuncOp lookupOrCreatePrintStrFn(ModuleOp moduleOp)
Include the generated interface declarations.