MLIR 24.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
21#include "mlir/IR/Operation.h"
22#include "mlir/IR/SymbolTable.h"
23#include "mlir/IR/Value.h"
25#include "mlir/Support/LLVM.h"
27#include "mlir/Support/TypeID.h"
31
32#include "llvm/ADT/MapVector.h"
33#include "llvm/ADT/SetVector.h"
34#include "llvm/IR/FPEnv.h"
35#include "llvm/IR/Module.h"
36#include "llvm/Support/VirtualFileSystemFwd.h"
37
38namespace llvm {
39class BasicBlock;
40class CallBase;
41class Function;
42class IRBuilderBase;
43class Metadata;
44class Value;
45} // namespace llvm
46
47namespace mlir {
48class Attribute;
49class Block;
50class Location;
51
52namespace LLVM {
53
54namespace detail {
57} // namespace detail
58
59class AliasScopeAttr;
60class AliasScopeDomainAttr;
61class DINodeAttr;
62class LLVMFuncOp;
63class ComdatSelectorOp;
64
65/// Implementation class for module translation. Holds a reference to the module
66/// being translated, and the mappings between the original and the translated
67/// functions, basic blocks and values. It is practically easier to hold these
68/// mappings in one class since the conversion of control flow operations
69/// needs to look up block and function mappings.
70class ModuleTranslation {
71 friend std::unique_ptr<llvm::Module>
72 mlir::translateModuleToLLVMIR(Operation *, llvm::LLVMContext &, StringRef,
73 bool, llvm::vfs::FileSystem *);
74
75public:
76 /// Stores the mapping between a function name and its LLVM IR representation.
77 void mapFunction(StringRef name, llvm::Function *func) {
78 auto result = functionMapping.try_emplace(name, func);
79 (void)result;
80 assert(result.second &&
81 "attempting to map a function that is already mapped");
82 }
83
84 /// Finds an LLVM IR function by its name.
85 llvm::Function *lookupFunction(StringRef name) const {
86 return functionMapping.lookup(name);
87 }
88
89 /// Stores the mapping between an MLIR value and its LLVM IR counterpart.
90 void mapValue(Value mlir, llvm::Value *llvm) { mapValue(mlir) = llvm; }
91
92 /// Provides write-once access to store the LLVM IR value corresponding to the
93 /// given MLIR value.
94 llvm::Value *&mapValue(Value value) {
95 llvm::Value *&llvm = valueMapping[value];
96 assert(llvm == nullptr &&
97 "attempting to map a value that is already mapped");
98 return llvm;
99 }
100
101 /// Finds an LLVM IR value corresponding to the given MLIR value.
102 llvm::Value *lookupValue(Value value) const {
103 return valueMapping.lookup(value);
104 }
105
106 /// Remap old value with new value in the MLIR-to-LLVM value map so later
107 /// translations use the replacement. Existing LLVM instructions are not
108 /// rewritten.
109 void remapAllValuesWith(llvm::Value *oldValue, llvm::Value *newValue);
110
111 /// Looks up remapped a list of remapped values.
113
114 /// Stores the mapping between an MLIR block and LLVM IR basic block.
115 void mapBlock(Block *mlir, llvm::BasicBlock *llvm) {
116 auto result = blockMapping.try_emplace(mlir, llvm);
117 (void)result;
118 assert(result.second && "attempting to map a block that is already mapped");
119 }
120
121 /// Finds an LLVM IR basic block that corresponds to the given MLIR block.
122 llvm::BasicBlock *lookupBlock(Block *block) const {
123 return blockMapping.lookup(block);
124 }
125
126 /// Stores the mapping between an MLIR operation with successors and a
127 /// corresponding LLVM IR instruction.
128 void mapBranch(Operation *mlir, llvm::Instruction *llvm) {
129 auto result = branchMapping.try_emplace(mlir, llvm);
130 (void)result;
131 assert(result.second &&
132 "attempting to map a branch that is already mapped");
133 }
134
135 /// Finds an LLVM IR instruction that corresponds to the given MLIR operation
136 /// with successors.
137 llvm::Instruction *lookupBranch(Operation *op) const {
138 return branchMapping.lookup(op);
139 }
140
141 /// Stores a mapping between an MLIR call operation and a corresponding LLVM
142 /// call instruction.
143 void mapCall(Operation *mlir, llvm::CallInst *llvm) {
144 auto result = callMapping.try_emplace(mlir, llvm);
145 (void)result;
146 assert(result.second && "attempting to map a call that is already mapped");
147 }
148
149 /// Finds an LLVM call instruction that corresponds to the given MLIR call
150 /// operation.
151 llvm::CallInst *lookupCall(Operation *op) const {
152 return callMapping.lookup(op);
153 }
154
155 /// Maps a blockaddress operation to its corresponding placeholder LLVM
156 /// value.
157 void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst) {
158 auto result = unresolvedBlockAddressMapping.try_emplace(op, cst);
159 (void)result;
160 assert(result.second &&
161 "attempting to map a blockaddress operation that is already mapped");
162 }
163
164 /// Maps a BlockAddressAttr to its corresponding LLVM basic block.
165 void mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block) {
166 auto result = blockAddressToLLVMMapping.try_emplace(attr, block);
167 (void)result;
168 assert(result.second &&
169 "attempting to map a blockaddress attribute that is already mapped");
170 }
171
172 /// Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
173 llvm::BasicBlock *lookupBlockAddress(BlockAddressAttr attr) const {
174 return blockAddressToLLVMMapping.lookup(attr);
175 }
176
177 /// Removes the mapping for blocks contained in the region and values defined
178 /// in these blocks.
179 void forgetMapping(Region &region);
180
181 /// Removes the mapping for the given value.
182 void forgetMapping(Value value) { valueMapping.erase(value); }
183
184 /// Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope
185 /// attribute. Creates the metadata node if it has not been converted before.
186 llvm::MDNode *getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr);
187
188 /// Returns the LLVM metadata corresponding to an array of mlir LLVM dialect
189 /// alias scope attributes. Creates the metadata nodes if they have not been
190 /// converted before.
191 llvm::MDNode *
193
194 // Sets LLVM metadata for memory operations that are in a parallel loop.
195 void setAccessGroupsMetadata(AccessGroupOpInterface op,
196 llvm::Instruction *inst);
197
198 // Sets LLVM metadata for memory operations that have alias scope information.
199 void setAliasScopeMetadata(AliasAnalysisOpInterface op,
200 llvm::Instruction *inst);
201
202 /// Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
203 void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst);
204
205 /// Sets LLVM tbaa.struct metadata on an aggregate copy, describing which
206 /// fields of the aggregate the copy touches.
207 void setTBAAStructMetadata(ArrayAttr fields, llvm::Instruction *inst);
208
209 /// Sets LLVM dereferenceable metadata for operations that have
210 /// dereferenceable attributes.
211 void setDereferenceableMetadata(DereferenceableOpInterface op,
212 llvm::Instruction *inst);
213
214 /// Sets LLVM profiling metadata for operations that have branch weights.
215 void setBranchWeightsMetadata(WeightedBranchOpInterface op);
216
217 /// Sets LLVM loop metadata for branch operations that have a loop annotation
218 /// attribute.
219 void setLoopMetadata(Operation *op, llvm::Instruction *inst);
220
221 /// Sets the disjoint flag attribute for the exported instruction `value`
222 /// given the original operation `op`. Asserts if the operation does
223 /// not implement the disjoint flag interface, and asserts if the value
224 /// is an instruction that implements the disjoint flag.
225 void setDisjointFlag(Operation *op, llvm::Value *value);
226
227 /// Converts the type from MLIR LLVM dialect to LLVM.
228 llvm::Type *convertType(Type type);
229
230 /// Returns the MLIR context of the module being translated.
231 MLIRContext &getContext() { return *mlirModule->getContext(); }
232
233 /// Returns the LLVM context in which the IR is being constructed.
234 llvm::LLVMContext &getLLVMContext() const { return llvmModule->getContext(); }
235
236 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
237 /// defining a global value.
238 llvm::GlobalValue *lookupGlobal(Operation *op) {
239 return globalsMapping.lookup(op);
240 }
241
242 /// Finds an LLVM IR global value by the mlir.global symbol name.
243 llvm::GlobalValue *lookupGlobal(StringRef name) const {
244 return globalsByNameMapping.lookup(name);
245 }
246
247 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
248 /// defining a global alias value.
249 llvm::GlobalValue *lookupAlias(Operation *op) {
250 return aliasesMapping.lookup(op);
251 }
252
253 /// Finds an LLVM IR global value that corresponds to the given MLIR operation
254 /// defining an IFunc.
255 llvm::GlobalValue *lookupIFunc(Operation *op) {
256 return ifuncMapping.lookup(op);
257 }
258
259 /// Returns the virtual filesystem to use for file operations. Falls back to
260 /// the real filesystem if none was provided.
261 llvm::vfs::FileSystem &getFileSystem();
262
263 /// Returns the LLVM module in which the IR is being constructed.
264 llvm::Module *getLLVMModule() { return llvmModule.get(); }
265
266 /// Translates the given location.
267 llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
268
269 /// Translates the given LLVM DWARF expression metadata.
270 llvm::DIExpression *translateExpression(LLVM::DIExpressionAttr attr);
271
272 /// Translates the given LLVM global variable expression metadata.
273 llvm::DIGlobalVariableExpression *
274 translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr);
275
276 /// Translates the given LLVM debug info metadata.
277 llvm::Metadata *translateDebugInfo(LLVM::DINodeAttr attr);
278
279 /// Translates the given LLVM rounding mode metadata.
280 llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding);
281
282 /// Translates the given LLVM FP exception behavior metadata.
283 llvm::fp::ExceptionBehavior
284 translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior);
285
286 /// Translates the contents of the given block to LLVM IR using this
287 /// translator. The LLVM IR basic block corresponding to the given block is
288 /// expected to exist in the mapping of this translator. Uses `builder` to
289 /// translate the IR, leaving it at the end of the block. If `ignoreArguments`
290 /// is set, does not produce PHI nodes for the block arguments. Otherwise, the
291 /// PHI nodes are constructed for block arguments but are _not_ connected to
292 /// the predecessors that may not exist yet.
293 LogicalResult convertBlock(Block &bb, bool ignoreArguments,
294 llvm::IRBuilderBase &builder) {
295 return convertBlockImpl(bb, ignoreArguments, builder,
296 /*recordInsertions=*/false);
297 }
298
299 /// Converts the given MLIR operation into LLVM IR using this translator. It
300 /// is up to the caller to ensure that all operands have been mapped before
301 /// calling this function.
302 LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder) {
303 return convertOperationImpl(op, builder, /*recordInsertions=*/false);
304 }
305
306 /// Converts argument and result attributes from `attrsOp` to LLVM IR
307 /// attributes on the `call` instruction. Returns failure if conversion fails.
308 /// The `immArgPositions` parameter is only relevant for intrinsics. It
309 /// specifies the positions of immediate arguments, which do not have
310 /// associated argument attributes in MLIR and should be skipped during
311 /// attribute mapping.
312 LogicalResult
313 convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp,
314 llvm::CallBase *call,
315 ArrayRef<unsigned> immArgPositions = {});
316
317 /// Gets the named metadata in the LLVM IR module being constructed, creating
318 /// it if it does not exist.
319 llvm::NamedMDNode *getOrInsertNamedModuleMetadata(StringRef name);
320
321 /// Converts an LLVM dialect metadata attribute to LLVM IR metadata.
322 /// Returns failure and emits a diagnostic using `emitError` if the attribute
323 /// cannot be converted.
324 FailureOr<llvm::Metadata *>
327
328 /// Creates a stack frame of type `T` on ModuleTranslation stack. `T` must
329 /// be derived from `StackFrameBase<T>` and constructible from the provided
330 /// arguments. Doing this before entering the region of the op being
331 /// translated makes the frame available when translating ops within that
332 /// region.
333 template <typename T, typename... Args>
334 void stackPush(Args &&...args) {
335 stack.stackPush<T>(std::forward<Args>(args)...);
336 }
337
338 /// Pops the last element from the ModuleTranslation stack.
339 void stackPop() { stack.stackPop(); }
340
341 /// Calls `callback` for every ModuleTranslation stack frame of type `T`
342 /// starting from the top of the stack.
343 template <typename T>
345 return stack.stackWalk(callback);
346 }
347
348 /// RAII object calling stackPush/stackPop on construction/destruction.
349 template <typename T>
351
352 /// Base class for state that lives as long as the module translation.
353 /// Dialect translations use extensions to keep state shared across the
354 /// translation of several operations (e.g. IR builders or handle mappings)
355 /// without the core translation depending on those dialects. Derived classes
356 /// must provide an explicit TypeID.
357 class Extension {
358 public:
359 virtual ~Extension();
360
361 /// Called once after all operations have been translated and before the
362 /// LLVM module is verified. Extensions are finalized in creation order.
363 virtual LogicalResult finalize() { return success(); }
364 };
365
366 /// Returns the extension of type `T`, constructing it from `args` on first
367 /// use. `T` must derive from `Extension`.
368 template <typename T, typename... Args>
369 T &getOrCreateExtension(Args &&...args) {
370 static_assert(std::is_base_of<Extension, T>::value,
371 "expected T derived from ModuleTranslation::Extension");
372 TypeID id = TypeID::get<T>();
373 auto it = extensions.find(id);
374 if (it != extensions.end())
375 return static_cast<T &>(*it->second);
376 // Construct before inserting: the constructor may create other extensions.
377 auto extension = std::make_unique<T>(std::forward<Args>(args)...);
378 T &result = *extension;
379 extensions.insert({id, std::move(extension)});
380 return result;
381 }
382
383 SymbolTableCollection &symbolTable() { return symbolTableCollection; }
384
385 // A helper callback that takes an attribute, and if it is a StringAttr,
386 // properly converts it to the 'no-builtin-VALUE' form.
387 static std::optional<llvm::Attribute> convertNoBuiltin(llvm::LLVMContext &ctx,
389
390 static std::optional<llvm::Attribute>
391 convertDefaultFuncAttr(llvm::LLVMContext &ctx,
392 mlir::NamedAttribute namedAttr);
393
394 /// A template that takes a collection-like attribute, and converts it via a
395 /// user provided callback, then adds each element as function attributes to
396 /// the provided operation.
397 template <typename AttrsTy, typename Operation, typename Converter>
399 const Converter &conv) {
400 if (!attrs)
401 return;
402 for (auto elt : attrs) {
403 std::optional<llvm::Attribute> result = conv(getLLVMContext(), elt);
404 if (result)
405 op->addFnAttr(*result);
406 }
407 }
408
409 llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr);
410
411private:
412 ModuleTranslation(Operation *module, std::unique_ptr<llvm::Module> llvmModule,
413 llvm::vfs::FileSystem *fs = nullptr);
415
416 /// Converts individual components.
417 LogicalResult convertOperationImpl(Operation &op,
418 llvm::IRBuilderBase &builder,
419 bool recordInsertions = false);
420 LogicalResult convertFunctionSignatures();
421 LogicalResult convertFunctionMetadata();
422 LogicalResult convertFunctions();
423 LogicalResult convertIFuncs();
424 LogicalResult convertComdats();
425
426 LogicalResult convertUnresolvedBlockAddress();
427
428 /// Handle conversion for both globals and global aliases.
429 ///
430 /// - Create named global variables that correspond to llvm.mlir.global
431 /// definitions, similarly Convert llvm.global_ctors and global_dtors ops.
432 /// - Create global alias that correspond to llvm.mlir.alias.
433 /// Global metadata that can reference other global objects (including
434 /// ifuncs) is converted later by `convertGlobalMetadata`.
435 LogicalResult convertGlobalsAndAliases();
436
437 /// Attach metadata on LLVM globals after all global objects exist so that
438 /// symbol references (globals, aliases, functions, and ifuncs) can be
439 /// resolved.
440 LogicalResult convertGlobalMetadata();
441 /// Converts a symbol ref to LLVM IR metadata, or fails if unresolved.
442 FailureOr<llvm::Metadata *>
443 convertSymbolRefToMetadata(FlatSymbolRefAttr name,
445 LogicalResult convertOneFunction(LLVMFuncOp func);
446 LogicalResult convertBlockImpl(Block &bb, bool ignoreArguments,
447 llvm::IRBuilderBase &builder,
448 bool recordInsertions);
449
450 /// Returns the LLVM metadata corresponding to the given mlir LLVM dialect
451 /// TBAATagAttr.
452 llvm::MDNode *getTBAANode(TBAATagAttr tbaaAttr) const;
453
454 /// Process tbaa LLVM Metadata operations and create LLVM
455 /// metadata nodes for them.
456 LogicalResult createTBAAMetadata();
457
458 /// Process the ident LLVM Metadata, if it exists.
459 LogicalResult createIdentMetadata();
460
461 /// Process the llvm.commandline LLVM Metadata, if it exists.
462 LogicalResult createCommandlineMetadata();
463
464 /// Process the llvm.dependent_libraries LLVM Metadata, if it exists.
465 LogicalResult createDependentLibrariesMetadata();
466
467 /// Translates dialect attributes attached to the given operation.
468 LogicalResult
469 convertDialectAttributes(Operation *op,
470 ArrayRef<llvm::Instruction *> instructions);
471
472 /// Translates parameter attributes of a call and adds them to the returned
473 /// AttrBuilder. Returns failure if any of the translations failed.
474 FailureOr<llvm::AttrBuilder> convertParameterAttrs(mlir::Location loc,
475 DictionaryAttr paramAttrs);
476
477 /// Translates parameter attributes of a function and adds them to the
478 /// returned AttrBuilder. Returns failure if any of the translations failed.
479 FailureOr<llvm::AttrBuilder>
480 convertParameterAttrs(LLVMFuncOp func, int argIdx, DictionaryAttr paramAttrs);
481
482 /// Original and translated module.
483 Operation *mlirModule;
484 std::unique_ptr<llvm::Module> llvmModule;
485 /// A converter for translating debug information.
486 std::unique_ptr<detail::DebugTranslation> debugTranslation;
487
488 /// A converter for translating loop annotations.
489 std::unique_ptr<detail::LoopAnnotationTranslation> loopAnnotationTranslation;
490
491 /// Translation-scoped state owned by dialect translations.
493
494 /// Optional virtual filesystem for file operations. When null, the real
495 /// filesystem is used (via getFileSystem()). Not owned.
496 llvm::vfs::FileSystem *fileSystem = nullptr;
497
498 /// Mappings between llvm.mlir.global definitions and corresponding globals.
500
501 /// Name-keyed mirror of `globalsMapping`, populated alongside it during
502 /// `convertGlobalsAndAliases`. Lets `getLLVMConstant` resolve
503 /// `FlatSymbolRefAttr` leaves of aggregate constants that name a global
504 /// (mirroring how `functionMapping` resolves function names).
505 llvm::StringMap<llvm::GlobalValue *> globalsByNameMapping;
506
507 /// Mappings between llvm.mlir.alias definitions and corresponding global
508 /// aliases.
510
511 /// Mappings between llvm.mlir.ifunc definitions and corresponding global
512 /// ifuncs.
514
515 /// A stateful object used to translate types.
516 TypeToLLVMIRTranslator typeTranslator;
517
518 /// A dialect interface collection used for dispatching the translation to
519 /// specific dialects.
521
522 /// Mappings between original and translated values, used for lookups.
523 llvm::StringMap<llvm::Function *> functionMapping;
526
527 /// A mapping between MLIR LLVM dialect terminators and LLVM IR terminators
528 /// they are converted to. This allows for connecting PHI nodes to the source
529 /// values after all operations are converted.
531
532 /// A mapping between MLIR LLVM dialect call operations and LLVM IR call
533 /// instructions. This allows for adding branch weights after the operations
534 /// have been converted.
536
537 /// Mapping from an alias scope attribute to its LLVM metadata.
538 /// This map is populated lazily.
539 DenseMap<AliasScopeAttr, llvm::MDNode *> aliasScopeMetadataMapping;
540
541 /// Mapping from an alias scope domain attribute to its LLVM metadata.
542 /// This map is populated lazily.
543 DenseMap<AliasScopeDomainAttr, llvm::MDNode *> aliasDomainMetadataMapping;
544
545 /// Mapping from a tbaa attribute to its LLVM metadata.
546 /// This map is populated on module entry.
547 DenseMap<Attribute, llvm::MDNode *> tbaaMetadataMapping;
548
549 /// Mapping from a comdat selector operation to its LLVM comdat struct.
550 /// This map is populated on module entry.
552
553 /// Mapping from llvm.blockaddress operations to their corresponding LLVM
554 /// constant placeholders. After all basic blocks are translated, this
555 /// mapping is used to replace the placeholders with the LLVM block addresses.
556 DenseMap<BlockAddressOp, llvm::Value *> unresolvedBlockAddressMapping;
557
558 /// Mapping from a BlockAddressAttr attribute to it's matching LLVM basic
559 /// block.
560 DenseMap<BlockAddressAttr, llvm::BasicBlock *> blockAddressToLLVMMapping;
561
562 /// Stack of user-specified state elements, useful when translating operations
563 /// with regions.
564 StateStack stack;
565
566 /// A cache for the symbol tables constructed during symbols lookup.
567 SymbolTableCollection symbolTableCollection;
568};
569
570namespace detail {
571/// For all blocks in the region that were converted to LLVM IR using the given
572/// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
573/// to the results of preceding blocks.
574void connectPHINodes(Region &region, const ModuleTranslation &state);
575
576/// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
577/// This currently supports integer, floating point, splat and dense element
578/// attributes and combinations thereof. Also, an array attribute with two
579/// elements is supported to represent a complex constant. In case of error,
580/// report it to `loc` and return nullptr.
581llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
582 Location loc,
583 const ModuleTranslation &moduleTranslation);
584
585/// Creates a call to an LLVM IR intrinsic function with the given arguments.
586llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
587 llvm::Intrinsic::ID intrinsic,
588 ArrayRef<llvm::Value *> args = {},
589 ArrayRef<llvm::Type *> tys = {});
590
591/// Creates a call to an LLVM IR intrinsic function with the given return type
592/// and arguments. If the intrinsic is overloaded, the function signature will
593/// be automatically resolved based on the provided return type and argument
594/// types.
595llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &builder,
596 llvm::Intrinsic::ID intrinsic,
597 llvm::Type *retTy,
598 ArrayRef<llvm::Value *> args);
599
600/// Creates a call to a LLVM IR intrinsic defined by LLVM_IntrOpBase. This
601/// resolves the overloads, and maps mixed MLIR value and attribute arguments to
602/// LLVM values.
603llvm::CallInst *createIntrinsicCall(
604 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
605 Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
606 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
607 ArrayRef<unsigned> immArgPositions,
608 ArrayRef<StringLiteral> immArgAttrNames);
609
610} // namespace detail
611
612} // namespace LLVM
613} // namespace mlir
614
615#endif // MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
return success()
static LogicalResult convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
ArrayAttr()
Attributes are known-constant values of operations.
Definition Attributes.h:25
Block represents an ordered list of Operations.
Definition Block.h:34
A symbol reference with a reference path containing a single element.
This class represents a diagnostic that is inflight and set to be reported.
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
Base class for state that lives as long as the module translation.
virtual LogicalResult finalize()
Called once after all operations have been translated and before the LLVM module is verified.
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.
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.
T & getOrCreateExtension(Args &&...args)
Returns the extension of type T, constructing it from args on first use.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
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.
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::vfs::FileSystem & getFileSystem()
Returns the virtual filesystem to use for file operations.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
llvm::GlobalValue * lookupGlobal(StringRef name) const
Finds an LLVM IR global value by the mlir.global symbol name.
void forgetMapping(Value value)
Removes the mapping for the given value.
FailureOr< llvm::Metadata * > convertMetadataAttr(Attribute attr, function_ref< InFlightDiagnostic()> emitError)
Converts an LLVM dialect metadata attribute to LLVM IR metadata.
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.
void remapAllValuesWith(llvm::Value *oldValue, llvm::Value *newValue)
Remap old value with new value in the MLIR-to-LLVM value map so later translations use the replacemen...
void setTBAAStructMetadata(ArrayAttr fields, llvm::Instruction *inst)
Sets LLVM tbaa.struct metadata on an aggregate copy, describing which fields of the aggregate the cop...
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...
LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder)
Converts the given MLIR operation into LLVM IR using this translator.
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)
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::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:71
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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.
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
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:392
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.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false, llvm::vfs::FileSystem *fs=nullptr)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
RAII object calling stackPush/stackPop on construction/destruction.
Definition StateStack.h:106