MLIR 23.0.0git
ModuleTranslation.h
Go to the documentation of this file.
1//===- ModuleTranslation.h - MLIR to LLVM conversion ------------*- 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 implements the translation between an MLIR LLVM dialect module and
10// the corresponding LLVMIR module. It only handles core LLVM IR operations.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15#define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
16
19#include "mlir/IR/Operation.h"
20#include "mlir/IR/SymbolTable.h"
21#include "mlir/IR/Value.h"
26
27#include "llvm/ADT/SetVector.h"
28#include "llvm/IR/FPEnv.h"
29#include "llvm/IR/Module.h"
30
31namespace llvm {
32class BasicBlock;
33class CallBase;
34class CanonicalLoopInfo;
35class Function;
36class IRBuilderBase;
37class OpenMPIRBuilder;
38class Value;
39} // namespace llvm
40
41namespace mlir {
42class Attribute;
43class Block;
44class Location;
45
46namespace LLVM {
47
48namespace detail {
51} // namespace detail
52
53class AliasScopeAttr;
54class AliasScopeDomainAttr;
55class DINodeAttr;
56class LLVMFuncOp;
57class ComdatSelectorOp;
58
59/// Implementation class for module translation. Holds a reference to the module
60/// being translated, and the mappings between the original and the translated
61/// functions, basic blocks and values. It is practically easier to hold these
62/// mappings in one class since the conversion of control flow operations
63/// needs to look up block and function mappings.
64class ModuleTranslation {
65 friend std::unique_ptr<llvm::Module>
66 mlir::translateModuleToLLVMIR(Operation *, llvm::LLVMContext &, StringRef,
67 bool);
68
69public:
70 /// Stores the mapping between a function name and its LLVM IR representation.
71 void mapFunction(StringRef name, llvm::Function *func) {
72 auto result = functionMapping.try_emplace(name, func);
73 (void)result;
74 assert(result.second &&
75 "attempting to map a function that is already mapped");
76 }
77
78 /// Finds an LLVM IR function by its name.
79 llvm::Function *lookupFunction(StringRef name) const {
80 return functionMapping.lookup(name);
81 }
82
83 /// Stores the mapping between an MLIR value and its LLVM IR counterpart.
84 void mapValue(Value mlir, llvm::Value *llvm) { mapValue(mlir) = llvm; }
85
86 /// Provides write-once access to store the LLVM IR value corresponding to the
87 /// given MLIR value.
88 llvm::Value *&mapValue(Value value) {
89 llvm::Value *&llvm = valueMapping[value];
90 assert(llvm == nullptr &&
91 "attempting to map a value that is already mapped");
92 return llvm;
93 }
94
95 /// Finds an LLVM IR value corresponding to the given MLIR value.
96 llvm::Value *lookupValue(Value value) const {
97 return valueMapping.lookup(value);
98 }
99
100 /// Looks up remapped a list of remapped values.
102
103 /// Stores the mapping between an MLIR block and LLVM IR basic block.
104 void mapBlock(Block *mlir, llvm::BasicBlock *llvm) {
105 auto result = blockMapping.try_emplace(mlir, llvm);
106 (void)result;
107 assert(result.second && "attempting to map a block that is already mapped");
108 }
109
110 /// Finds an LLVM IR basic block that corresponds to the given MLIR block.
111 llvm::BasicBlock *lookupBlock(Block *block) const {
112 return blockMapping.lookup(block);
113 }
114
115 /// Find the LLVM-IR loop that represents an MLIR loop.
116 llvm::CanonicalLoopInfo *lookupOMPLoop(omp::NewCliOp mlir) const {
117 llvm::CanonicalLoopInfo *result = loopMapping.lookup(mlir);
118 assert(result && "attempt to get non-existing loop");
119 return result;
120 }
121
122 /// Find the LLVM-IR loop that represents an MLIR loop.
123 llvm::CanonicalLoopInfo *lookupOMPLoop(Value mlir) const {
124 return lookupOMPLoop(mlir.getDefiningOp<omp::NewCliOp>());
125 }
126
127 /// Mark an OpenMP loop as having been consumed.
128 void invalidateOmpLoop(omp::NewCliOp mlir) { loopMapping.erase(mlir); }
129
130 /// Mark an OpenMP loop as having been consumed.
132 invalidateOmpLoop(mlir.getDefiningOp<omp::NewCliOp>());
133 }
134
135 /// Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR
136 /// OpenMPIRBuilder CanonicalLoopInfo
137 void mapOmpLoop(omp::NewCliOp mlir, llvm::CanonicalLoopInfo *llvm) {
138 assert(llvm && "argument must be non-null");
139 llvm::CanonicalLoopInfo *&cur = loopMapping[mlir];
140 assert(cur == nullptr && "attempting to map a loop that is already mapped");
141 cur = llvm;
142 }
143
144 /// Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR
145 /// OpenMPIRBuilder CanonicalLoopInfo
146 void mapOmpLoop(Value mlir, llvm::CanonicalLoopInfo *llvm) {
147 mapOmpLoop(mlir.getDefiningOp<omp::NewCliOp>(), llvm);
148 }
149
150 /// Stores the mapping between an MLIR operation with successors and a
151 /// corresponding LLVM IR instruction.
152 void mapBranch(Operation *mlir, llvm::Instruction *llvm) {
153 auto result = branchMapping.try_emplace(mlir, llvm);
154 (void)result;
155 assert(result.second &&
156 "attempting to map a branch that is already mapped");
157 }
158
159 /// Finds an LLVM IR instruction that corresponds to the given MLIR operation
160 /// with successors.
161 llvm::Instruction *lookupBranch(Operation *op) const {
162 return branchMapping.lookup(op);
163 }
164
165 /// Stores a mapping between an MLIR call operation and a corresponding LLVM
166 /// call instruction.
167 void mapCall(Operation *mlir, llvm::CallInst *llvm) {
168 auto result = callMapping.try_emplace(mlir, llvm);
169 (void)result;
170 assert(result.second && "attempting to map a call that is already mapped");
171 }
172
173 /// Finds an LLVM call instruction that corresponds to the given MLIR call
174 /// operation.
175 llvm::CallInst *lookupCall(Operation *op) const {
176 return callMapping.lookup(op);
177 }
178
179 /// Maps a blockaddress operation to its corresponding placeholder LLVM
180 /// value.
181 void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst) {
182 auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);
183 (void)result;
184 assert(result.second &&
185 "attempting to map a blockaddress operation that is already mapped");
186 }
187
188 /// Maps a BlockAddressAttr to its corresponding LLVM basic block.
189 void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block) {
190 auto result = blockAddressToLLVMMapping.try_emplace(attr, block);
191 (void)result;
192 assert(result.second &&
193 "attempting to map a blockaddress attribute that is already mapped");
194 }
195
196 /// Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
197 llvm::BasicBlock *lookupBlockAddress(BlockAddressAttr attr) const {
198 return blockAddressToLLVMMapping.lookup(attr);
199 }
200
201 /// Removes the mapping for blocks contained in the region and values defined
202 /// in these blocks.
203 void forgetMapping(Region &region);
204
205 /// Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope
206 /// attribute. Creates the metadata node if it has not been converted before.
207 llvm::MDNode *getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr);
208
209 /// Returns the LLVM metadata corresponding to an array of mlir LLVM dialect
210 /// alias scope attributes. Creates the metadata nodes if they have not been
211 /// converted before.
212 llvm::MDNode *
214
215 // Sets LLVM metadata for memory operations that are in a parallel loop.
216 void setAccessGroupsMetadata(AccessGroupOpInterface op,
217 llvm::Instruction *inst);
218
219 // Sets LLVM metadata for memory operations that have alias scope information.
220 void setAliasScopeMetadata(AliasAnalysisOpInterface op,
221 llvm::Instruction *inst);
222
223 /// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
224 void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
225
226 /// Sets LLVM dereferenceable metadata for operations that have
227 /// dereferenceable attributes.
228 void setDereferenceableMetadata(DereferenceableOpInterface op,
229 llvm::Instruction *inst);
230
231 /// Sets LLVM profiling metadata for operations that have branch weights.
232 void setBranchWeightsMetadata(WeightedBranchOpInterface op);
233
234 /// Sets LLVM loop metadata for branch operations that have a loop annotation
235 /// attribute.
236 void setLoopMetadata(Operation *op, llvm::Instruction *inst);
237
238 /// Sets the disjoint flag attribute for the exported instruction `value`
239 /// given the original operation `op`. Asserts if the operation does
240 /// not implement the disjoint flag interface, and asserts if the value
241 /// is an instruction that implements the disjoint flag.
242 void setDisjointFlag(Operation *op, llvm::Value *value);
243
244 /// Converts the type from MLIR LLVM dialect to LLVM.
245 llvm::Type *convertType(Type type);
246
247 /// Returns the MLIR context of the module being translated.
248 MLIRContext &getContext() { return *mlirModule->getContext(); }
249
250 /// Returns the LLVM context in which the IR is being constructed.
251 llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }
252
253 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
254 /// defining a global value.
255 llvm::GlobalValue *lookupGlobal(Operation *op) {
256 return globalsMapping.lookup(op);
257 }
258
259 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
260 /// defining a global alias value.
261 llvm::GlobalValue *lookupAlias(Operation *op) {
262 return aliasesMapping.lookup(op);
263 }
264
265 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
266 /// defining an IFunc.
267 llvm::GlobalValue *lookupIFunc(Operation *op) {
268 return ifuncMapping.lookup(op);
269 }
270
271 /// Returns the OpenMP IR builder associated with the LLVM IR module being
272 /// constructed.
273 llvm::OpenMPIRBuilder *getOpenMPBuilder();
274
275 /// Returns the LLVM module in which the IR is being constructed.
276 llvm::Module *getLLVMModule() { return llvmModule.get(); }
277
278 /// Translates the given location.
279 llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
280
281 /// Translates the given LLVM DWARF expression metadata.
282 llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
283
284 /// Translates the given LLVM global variable expression metadata.
285 llvm::DIGlobalVariableExpression *
286 translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
287
288 /// Translates the given LLVM debug info metadata.
289 llvm::Metadata *translateDebugInfo(LLVM::DINodeAttr attr);
290
291 /// Translates the given LLVM rounding mode metadata.
292 llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding);
293
294 /// Translates the given LLVM FP exception behavior metadata.
295 llvm::fp::ExceptionBehavior
296 translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior);
297
298 /// Translates the contents of the given block to LLVM IR using this
299 /// translator. The LLVM IR basic block corresponding to the given block is
300 /// expected to exist in the mapping of this translator. Uses `builder` to
301 /// translate the IR, leaving it at the end of the block. If `ignoreArguments`
302 /// is set, does not produce PHI nodes for the block arguments. Otherwise, the
303 /// PHI nodes are constructed for block arguments but are _not_ connected to
304 /// the predecessors that may not exist yet.
305 LogicalResult convertBlock(Block &bb, bool ignoreArguments,
306 llvm::IRBuilderBase &builder) {
307 return convertBlockImpl(bb, ignoreArguments, builder,
308 /*recordInsertions=*/false);
309 }
310
311 /// Converts argument and result attributes from `attrsOp` to LLVM IR
312 /// attributes on the `call` instruction. Returns failure if conversion fails.
313 /// The `immArgPositions` parameter is only relevant for intrinsics. It
314 /// specifies the positions of immediate arguments, which do not have
315 /// associated argument attributes in MLIR and should be skipped during
316 /// attribute mapping.
317 LogicalResult
318 convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp,
319 llvm::CallBase *call,
320 ArrayRef<unsigned> immArgPositions = {});
321
322 /// Gets the named metadata in the LLVM IR module being constructed, creating
323 /// it if it does not exist.
324 llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
325
326 /// Creates a stack frame of type `T` on ModuleTranslation stack. `T` must
327 /// be derived from `StackFrameBase<T>` and constructible from the provided
328 /// arguments. Doing this before entering the region of the op being
329 /// translated makes the frame available when translating ops within that
330 /// region.
331 template <typename T, typename... Args>
332 void stackPush(Args &&...args) {
333 stack.stackPush<T>(std::forward<Args>(args)...);
334 }
335
336 /// Pops the last element from the ModuleTranslation stack.
337 void stackPop() { stack.stackPop(); }
338
339 /// Calls `callback` for every ModuleTranslation stack frame of type `T`
340 /// starting from the top of the stack.
341 template <typename T>
343 return stack.stackWalk(callback);
344 }
345
346 /// RAII object calling stackPush/stackPop on construction/destruction.
347 template <typename T>
349
350 SymbolTableCollection &symbolTable() { return symbolTableCollection; }
351
352 // A helper callback that takes an attribute, and if it is a StringAttr,
353 // properly converts it to the 'no-builtin-VALUE' form.
354 static std::optional<llvm::Attribute> convertNoBuiltin(llvm::LLVMContext &ctx,
356
357 static std::optional<llvm::Attribute>
358 convertDefaultFuncAttr(llvm::LLVMContext &ctx,
359 mlir::NamedAttribute namedAttr);
360
361 /// A template that takes a collection-like attribute, and converts it via a
362 /// user provided callback, then adds each element as function attributes to
363 /// the provided operation.
364 template <typename AttrsTy, typename Operation, typename Converter>
366 const Converter &conv) {
367 if (!attrs)
368 return;
369 for (auto elt : attrs) {
370 std::optional<llvm::Attribute> result = conv(getLLVMContext(), elt);
371 if (result)
372 op->addFnAttr(*result);
373 }
374 }
375
376 llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr);
377
378private:
380 std::unique_ptr<llvm::Module> llvmModule);
382
383 /// Converts individual components.
384 LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder,
385 bool recordInsertions = false);
386 LogicalResult convertFunctionSignatures();
387 LogicalResult convertFunctions();
388 LogicalResult convertIFuncs();
389 LogicalResult convertComdats();
390
391 LogicalResult convertUnresolvedBlockAddress();
392
393 /// Handle conversion for both globals and global aliases.
394 ///
395 /// - Create named global variables that correspond to llvm.mlir.global
396 /// definitions, similarly Convert llvm.global_ctors and global_dtors ops.
397 /// - Create global alias that correspond to llvm.mlir.alias.
398 LogicalResult convertGlobalsAndAliases();
399 LogicalResult convertOneFunction(LLVMFuncOp func);
400 LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,
401 llvm::IRBuilderBase &builder,
402 bool recordInsertions);
403
404 /// Returns the LLVM metadata corresponding to the given mlir LLVM dialect
405 /// TBAATagAttr.
406 llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;
407
408 /// Process tbaa LLVM Metadata operations and create LLVM
409 /// metadata nodes for them.
410 LogicalResult createTBAAMetadata();
411
412 /// Process the ident LLVM Metadata, if it exists.
413 LogicalResult createIdentMetadata();
414
415 /// Process the llvm.commandline LLVM Metadata, if it exists.
416 LogicalResult createCommandlineMetadata();
417
418 /// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
419 LogicalResult createDependentLibrariesMetadata();
420
421 /// Translates dialect attributes attached to the given operation.
422 LogicalResult
423 convertDialectAttributes(Operation *op,
424 ArrayRef<llvm::Instruction *> instructions);
425
426 /// Translates parameter attributes of a call and adds them to the returned
427 /// AttrBuilder. Returns failure if any of the translations failed.
428 FailureOr<llvm::AttrBuilder> convertParameterAttrs(mlir::Location loc,
429 DictionaryAttr paramAttrs);
430
431 /// Translates parameter attributes of a function and adds them to the
432 /// returned AttrBuilder. Returns failure if any of the translations failed.
433 FailureOr<llvm::AttrBuilder>
434 convertParameterAttrs(LLVMFuncOp func, int argIdx, DictionaryAttr paramAttrs);
435
436 /// Original and translated module.
437 Operation *mlirModule;
438 std::unique_ptr<llvm::Module> llvmModule;
439 /// A converter for translating debug information.
440 std::unique_ptr<detail::DebugTranslation> debugTranslation;
441
442 /// A converter for translating loop annotations.
443 std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
444
445 /// Builder for LLVM IR generation of OpenMP constructs.
446 std::unique_ptr<llvm::OpenMPIRBuilder> ompBuilder;
447
448 /// Mappings between llvm.mlir.global definitions and corresponding globals.
450
451 /// Mappings between llvm.mlir.alias definitions and corresponding global
452 /// aliases.
454
455 /// Mappings between llvm.mlir.ifunc definitions and corresponding global
456 /// ifuncs.
458
459 /// A stateful object used to translate types.
460 TypeToLLVMIRTranslator typeTranslator;
461
462 /// A dialect interface collection used for dispatching the translation to
463 /// specific dialects.
465
466 /// Mappings between original and translated values, used for lookups.
467 llvm::StringMap<llvm::Function *> functionMapping;
470
471 /// List of not yet consumed MLIR loop handles (represented by an omp.new_cli
472 /// operation which creates a value of type CanonicalLoopInfoType) and their
473 /// LLVM-IR representation as CanonicalLoopInfo which is managed by the
474 /// OpenMPIRBuilder.
476
477 /// A mapping between MLIR LLVM dialect terminators and LLVM IR terminators
478 /// they are converted to. This allows for connecting PHI nodes to the source
479 /// values after all operations are converted.
481
482 /// A mapping between MLIR LLVM dialect call operations and LLVM IR call
483 /// instructions. This allows for adding branch weights after the operations
484 /// have been converted.
486
487 /// Mapping from an alias scope attribute to its LLVM metadata.
488 /// This map is populated lazily.
489 DenseMap<AliasScopeAttr, llvm::MDNode *> aliasScopeMetadataMapping;
490
491 /// Mapping from an alias scope domain attribute to its LLVM metadata.
492 /// This map is populated lazily.
493 DenseMap<AliasScopeDomainAttr, llvm::MDNode *> aliasDomainMetadataMapping;
494
495 /// Mapping from a tbaa attribute to its LLVM metadata.
496 /// This map is populated on module entry.
497 DenseMap<Attribute, llvm::MDNode *> tbaaMetadataMapping;
498
499 /// Mapping from a comdat selector operation to its LLVM comdat struct.
500 /// This map is populated on module entry.
502
503 /// Mapping from llvm.blockaddress operations to their corresponding LLVM
504 /// constant placeholders. After all basic blocks are translated, this
505 /// mapping is used to replace the placeholders with the LLVM block addresses.
506 DenseMap<BlockAddressOp, llvm::Value *> unresolvedBlockAddressMapping;
507
508 /// Mapping from a BlockAddressAttr attribute to it's matching LLVM basic
509 /// block.
510 DenseMap<BlockAddressAttr, llvm::BasicBlock *> blockAddressToLLVMMapping;
511
512 /// Stack of user-specified state elements, useful when translating operations
513 /// with regions.
514 StateStack stack;
515
516 /// A cache for the symbol tables constructed during symbols lookup.
517 SymbolTableCollection symbolTableCollection;
518};
519
520namespace detail {
521/// For all blocks in the region that were converted to LLVM IR using the given
522/// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
523/// to the results of preceding blocks.
524void connectPHINodes(Region &region, const ModuleTranslation &state);
525
526/// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
527/// This currently supports integer, floating point, splat and dense element
528/// attributes and combinations thereof. Also, an array attribute with two
529/// elements is supported to represent a complex constant. In case of error,
530/// report it to `loc` and return nullptr.
531llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
532 Location loc,
533 const ModuleTranslation &moduleTranslation);
534
535/// Creates a call to an LLVM IR intrinsic function with the given arguments.
536llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
537 llvm::Intrinsic::ID intrinsic,
538 ArrayRef<llvm::Value *> args = {},
539 ArrayRef<llvm::Type *> tys = {});
540
541/// Creates a call to an LLVM IR intrinsic function with the given return type
542/// and arguments. If the intrinsic is overloaded, the function signature will
543/// be automatically resolved based on the provided return type and argument
544/// types.
545llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
546 llvm::Intrinsic::ID intrinsic,
547 llvm::Type *retTy,
548 ArrayRef<llvm::Value *> args);
549
550/// Creates a call to a LLVM IR intrinsic defined by LLVM_IntrOpBase. This
551/// resolves the overloads, and maps mixed MLIR value and attribute arguments to
552/// LLVM values.
553llvm::CallInst *createIntrinsicCall(
554 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
555 Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
556 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
557 ArrayRef<unsigned> immArgPositions,
558 ArrayRef<StringLiteral> immArgAttrNames);
559
560} // namespace detail
561
562} // namespace LLVM
563} // namespace mlir
564
565#endif // MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
Attributes are known-constant values of operations.
Definition Attributes.h:25
Block represents an ordered list of Operations.
Definition Block.h:33
Interface collection for translation to LLVM IR, dispatches to a concrete interface implementation ba...
This class represents the base attribute for all debug info attributes.
Definition LLVMAttrs.h:29
Implementation class for module translation.
void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst)
Maps a blockaddress operation to its corresponding placeholder LLVM value.
llvm::fp::ExceptionBehavior translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior)
Translates the given LLVM FP exception behavior metadata.
llvm::CallInst * lookupCall(Operation *op) const
Finds an LLVM call instruction that corresponds to the given MLIR call operation.
void mapCall(Operation *mlir, llvm::CallInst *llvm)
Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
llvm::DIGlobalVariableExpression * translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr)
Translates the given LLVM global variable expression metadata.
llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr)
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
WalkResult stackWalk(llvm::function_ref< WalkResult(T &)> callback)
Calls callback for every ModuleTranslation stack frame of type T starting from the top of the stack.
void stackPush(Args &&...args)
Creates a stack frame of type T on ModuleTranslation stack.
llvm::NamedMDNode * getOrInsertNamedModuleMetadata(StringRef name)
Gets the named metadata in the LLVM IR module being constructed, creating it if it does not exist.
LogicalResult convertBlock(Block &bb, bool ignoreArguments, llvm::IRBuilderBase &builder)
Translates the contents of the given block to LLVM IR using this translator.
void mapBranch(Operation *mlir, llvm::Instruction *llvm)
Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction.
void mapOmpLoop(Value mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
SmallVector< llvm::Value * > lookupValues(ValueRange values)
Looks up remapped a list of remapped values.
void mapFunction(StringRef name, llvm::Function *func)
Stores the mapping between a function name and its LLVM IR representation.
void convertFunctionAttrCollection(AttrsTy attrs, Operation *op, const Converter &conv)
A template that takes a collection-like attribute, and converts it via a user provided callback,...
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translates the given location.
void setDereferenceableMetadata(DereferenceableOpInterface op, llvm::Instruction *inst)
Sets LLVM dereferenceable metadata for operations that have dereferenceable attributes.
void setBranchWeightsMetadata(WeightedBranchOpInterface op)
Sets LLVM profiling metadata for operations that have branch weights.
llvm::Instruction * lookupBranch(Operation *op) const
Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
void invalidateOmpLoop(omp::NewCliOp mlir)
Mark an OpenMP loop as having been consumed.
LogicalResult convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp, llvm::CallBase *call, ArrayRef< unsigned > immArgPositions={})
Converts argument and result attributes from attrsOp to LLVM IR attributes on the call instruction.
static std::optional< llvm::Attribute > convertNoBuiltin(llvm::LLVMContext &ctx, mlir::Attribute a)
SymbolTableCollection & symbolTable()
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
llvm::Value *& mapValue(Value value)
Provides write-once access to store the LLVM IR value corresponding to the given MLIR value.
void invalidateOmpLoop(Value mlir)
Mark an OpenMP loop as having been consumed.
llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding)
Translates the given LLVM rounding mode metadata.
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::DIExpression * translateExpression(LLVM::DIExpressionAttr attr)
Translates the given LLVM DWARF expression metadata.
llvm::OpenMPIRBuilder * getOpenMPBuilder()
Returns the OpenMP IR builder associated with the LLVM IR module being constructed.
void mapOmpLoop(omp::NewCliOp mlir, llvm::CanonicalLoopInfo *llvm)
Map an MLIR OpenMP dialect CanonicalLoopInfo to its lowered LLVM-IR OpenMPIRBuilder CanonicalLoopInfo...
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
SaveStateStack< T, ModuleTranslation > SaveStack
RAII object calling stackPush/stackPop on construction/destruction.
llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
llvm::GlobalValue * lookupIFunc(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining an IFunc.
llvm::Metadata * translateDebugInfo(LLVM::DINodeAttr attr)
Translates the given LLVM debug info metadata.
void setDisjointFlag(Operation *op, llvm::Value *value)
Sets the disjoint flag attribute for the exported instruction value given the original operation op.
llvm::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
llvm::MDNode * getOrCreateAliasScopes(ArrayRef< AliasScopeAttr > aliasScopeAttrs)
Returns the LLVM metadata corresponding to an array of mlir LLVM dialect alias scope attributes.
void mapBlock(Block *mlir, llvm::BasicBlock *llvm)
Stores the mapping between an MLIR block and LLVM IR basic block.
llvm::MDNode * getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr)
Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope attribute.
llvm::Module * getLLVMModule()
Returns the LLVM module in which the IR is being constructed.
static std::optional< llvm::Attribute > convertDefaultFuncAttr(llvm::LLVMContext &ctx, mlir::NamedAttribute namedAttr)
llvm::CanonicalLoopInfo * lookupOMPLoop(Value mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
void stackPop()
Pops the last element from the ModuleTranslation stack.
void forgetMapping(Region &region)
Removes the mapping for blocks contained in the region and values defined in these blocks.
void setAliasScopeMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
void setAccessGroupsMetadata(AccessGroupOpInterface op, llvm::Instruction *inst)
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
llvm::CanonicalLoopInfo * lookupOMPLoop(omp::NewCliOp mlir) const
Find the LLVM-IR loop that represents an MLIR loop.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block)
Maps a BlockAddressAttr to its corresponding LLVM basic block.
void setLoopMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM loop metadata for branch operations that have a loop annotation attribute.
Utility class to translate MLIR LLVM dialect types to LLVM IR.
Definition TypeToLLVM.h:39
A helper class that converts LoopAnnotationAttrs and AccessGroupAttrs into corresponding llvm::MDNode...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
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
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
A utility result that is used to signal how to proceed with an ongoing walk:
Definition WalkResult.h:29
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
void connectPHINodes(Region &region, const ModuleTranslation &state)
For all blocks in the region that were converted to LLVM IR using the given ModuleTranslation,...
llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)
Create an LLVM IR constant of llvmType from the MLIR attribute attr.
AttrTypeReplacer.
Include the generated interface declarations.
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:118
RAII object calling stackPush/stackPop on construction/destruction.
Definition StateStack.h:106