14#ifndef MLIR_TARGET_LLVMIR_MODULEIMPORT_H
15#define MLIR_TARGET_LLVMIR_MODULEIMPORT_H
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/IR/Module.h"
27class ModuleSlotTracker;
29class DbgVariableIntrinsic;
52 ModuleImport(ModuleOp mlirModule, std::unique_ptr<llvm::Module> llvmModule,
53 bool emitExpensiveWarnings,
bool importEmptyDICompositeTypes,
54 bool preferUnregisteredIntrinsics,
bool importStructsAsLiterals);
62 return iface.initializeImport(llvmModule->getContext());
100 assert(
mlir ==
nullptr &&
101 "attempting to map a value that is already mapped");
121 assert(inst->getType()->isVoidTy() &&
122 "attempting to map an operation that returns a result");
123 assert(
mlir ==
nullptr &&
124 "attempting to map an operation that is already mapped");
133 return value.getDefiningOp();
134 return noResultOpMapping.lookup(inst);
141 assert(
result.second &&
"attempting to map a block that is already mapped");
146 return blockMapping.lookup(block);
187 FailureOr<SmallVector<AliasScopeAttr>>
195 return typeTranslator.translateType(type);
257 return tbaaMapping.lookup(node);
263 return tbaaStructMapping.lookup(node);
269 FailureOr<SmallVector<AccessGroupAttr>>
281 FailureOr<DereferenceableAttr>
287 FailureOr<SmallVector<AliasScopeAttr>>
306 bool requiresOpBundles,
316 ArgAndResultAttrsOpInterface attrsOp,
322 return preferUnregisteredIntrinsics;
327 std::string renderValue(
const llvm::Value &value);
329 std::string renderMetadata(
const llvm::Metadata *node);
331 llvm::ModuleSlotTracker &getDiagSlotTracker();
334 void clearRegionState() {
335 valueMapping.clear();
336 noResultOpMapping.clear();
337 blockMapping.clear();
338 debugIntrinsics.clear();
341 void setConstantInsertionPointToStart(
Block *block) {
342 constantInsertionBlock = block;
343 constantInsertionOp =
nullptr;
348 LogicalResult convertGlobal(llvm::GlobalVariable *globalVar);
350 LogicalResult convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar);
353 LogicalResult convertAlias(llvm::GlobalAlias *alias);
355 LogicalResult convertIFunc(llvm::GlobalIFunc *ifunc);
357 FlatSymbolRefAttr getPersonalityAsAttr(llvm::Function *func);
359 LogicalResult processBasicBlock(llvm::BasicBlock *bb,
Block *block);
362 LogicalResult processDebugIntrinsics();
365 LogicalResult processDebugRecords();
367 LogicalResult processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
368 DominanceInfo &domInfo);
370 LogicalResult processDebugRecord(llvm::DbgVariableRecord &dbgRecord,
371 DominanceInfo &domInfo);
376 std::tuple<DILocalVariableAttr, DIExpressionAttr, Value>
377 processDebugOpArgumentsAndInsertionPt(
380 llvm::Value *address,
382 llvm::DIExpression *expression, DominanceInfo &domInfo);
385 ArrayAttr convertAsmInlineOperandAttrs(
const llvm::CallBase &llvmCall);
388 LogicalResult convertIntrinsic(llvm::CallInst *inst);
391 LogicalResult convertInstruction(llvm::Instruction *inst);
396 void setNonDebugMetadataAttrs(llvm::Instruction *inst, Operation *op);
399 FlatSymbolRefAttr getMetadataGlobalValueSymbolRef(llvm::GlobalValue *global);
401 FlatSymbolRefAttr getMetadataOperandSymbolRef(
const llvm::Metadata *md);
404 Attribute convertMetadataToAttr(
const llvm::Metadata *md);
407 Attribute convertMetadataToAttrImpl(
413 LogicalResult processInstruction(llvm::Instruction *inst);
418 LogicalResult convertBranchArgs(llvm::Instruction *branch,
420 SmallVectorImpl<Value> &blockArguments);
426 FailureOr<SmallVector<Value>>
427 convertCallOperands(llvm::CallBase *callInst,
bool allowInlineAsm =
false);
434 FailureOr<LLVMFunctionType> convertFunctionType(llvm::CallBase *callInst,
435 bool &isIncompatibleCall);
437 FlatSymbolRefAttr convertCalleeName(llvm::CallBase *callInst);
443 DictionaryAttr convertArgOrResultAttrSet(llvm::AttributeSet llvmAttrSet);
445 LogicalResult convertCallAttributes(llvm::CallInst *inst, CallOp op);
447 LogicalResult convertInvokeAttributes(llvm::InvokeInst *inst, InvokeOp op);
451 Type getBuiltinTypeForAttr(Type type);
455 Attribute getConstantAsAttr(llvm::Constant *constant);
463 FailureOr<Value> convertConstant(llvm::Constant *constant);
469 FailureOr<Value> convertConstantExpr(llvm::Constant *constant);
473 ComdatOp getGlobalComdatOp();
479 LogicalResult processTBAAMetadata(
const llvm::MDNode *node);
485 Attribute convertTBAAStructMetadata(
const llvm::MDNode *node);
490 LogicalResult processAccessGroupMetadata(
const llvm::MDNode *node);
495 LogicalResult processAliasScopeMetadata(
const llvm::MDNode *node);
499 void processComdat(
const llvm::Comdat *comdat);
503 getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
507 OpBuilder::InsertionGuard setGlobalInsertionPoint();
512 Block *constantInsertionBlock =
nullptr;
514 Operation *constantInsertionOp =
nullptr;
516 Operation *globalInsertionOp =
nullptr;
518 ComdatOp globalComdatOp =
nullptr;
520 MLIRContext *context;
524 std::unique_ptr<llvm::Module> llvmModule;
528 unsigned namelessGlobalId = 0;
532 LLVMImportInterface iface;
544 SetVector<llvm::Instruction *> debugIntrinsics;
547 SetVector<llvm::DbgVariableRecord *> dbgRecords;
562 DenseMap<const llvm::Comdat *, SymbolRefAttr> comdatMapping;
564 LLVM::TypeFromLLVMIRTranslator typeTranslator;
566 std::unique_ptr<detail::DebugImporter> debugImporter;
568 std::unique_ptr<detail::LoopAnnotationImporter> loopAnnotationImporter;
575 std::unique_ptr<llvm::ModuleSlotTracker> diagSlotTracker;
580 bool emitExpensiveWarnings;
584 bool preferUnregisteredIntrinsics;
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
LogicalResult convertIFuncs()
Converts all ifuncs of the LLVM module to MLIR variables.
LogicalResult convertIntrinsicArguments(ArrayRef< llvm::Value * > values, ArrayRef< llvm::OperandBundleUse > opBundles, bool requiresOpBundles, ArrayRef< unsigned > immArgPositions, ArrayRef< StringLiteral > immArgAttrNames, SmallVectorImpl< Value > &valuesOut, SmallVectorImpl< NamedAttribute > &attrsOut)
Converts the LLVM values for an intrinsic to mixed MLIR values and attributes for LLVM_IntrOpBase.
Location translateLoc(llvm::DILocation *loc)
Translates the debug location.
LogicalResult convertComdats()
Converts all comdat selectors of the LLVM module to MLIR comdat operations.
LogicalResult convertAliases()
Converts all aliases of the LLVM module to MLIR variables.
LogicalResult convertFunctions()
Converts all functions of the LLVM module to MLIR functions.
FailureOr< SmallVector< Value > > convertValues(ArrayRef< llvm::Value * > values)
Converts a range of LLVM values to a range of MLIR values using the convertValue method,...
Attribute lookupTBAAAttr(const llvm::MDNode *node) const
Returns the MLIR attribute mapped to the given LLVM TBAA metadata node.
bool useUnregisteredIntrinsicsOnly() const
Whether the importer should try to convert all intrinsics to llvm.call_intrinsic instead of dialect s...
LogicalResult convertLinkerOptionsMetadata()
Converts !llvm.linker.options metadata to the llvm.linker.options LLVM dialect operation.
Block * lookupBlock(llvm::BasicBlock *block) const
Returns the MLIR block mapped to the given LLVM block.
void mapBlock(llvm::BasicBlock *llvm, Block *mlir)
Stores the mapping between an LLVM block and its MLIR counterpart.
DILocalVariableAttr matchLocalVariableAttr(llvm::PointerUnion< llvm::Value *, llvm::DILocalVariable * > valOrVariable)
Converts valOrVariable to a local variable attribute.
void processFunctionAttributes(llvm::Function *func, LLVMFuncOp funcOp)
Converts function attributes of LLVM Function func into LLVM dialect attributes of LLVMFuncOp funcOp.
Attribute lookupTBAAStructAttr(const llvm::MDNode *node) const
Looks up a converted !tbaa.struct node.
LogicalResult convertMetadata()
Converts all LLVM metadata nodes that translate to attributes such as alias analysis or access group ...
FailureOr< Value > convertValue(llvm::Value *value)
Converts an LLVM value to an MLIR value, or returns failure if the conversion fails.
LogicalResult initializeImportInterface()
Calls the LLVMImportInterface initialization that queries the registered dialect interfaces for the s...
void addDebugIntrinsic(llvm::CallInst *intrinsic)
Adds a debug intrinsics to the list of intrinsics that should be converted after the function convers...
LogicalResult convertIdentMetadata()
Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
FailureOr< Value > convertMetadataValue(llvm::Value *value)
Converts an LLVM metadata value to an MLIR value, or returns failure if the conversion fails.
FailureOr< SmallVector< AliasScopeAttr > > lookupAliasScopeAttrs(const llvm::MDNode *node) const
Returns the alias scope attributes that map to the alias scope nodes starting from the metadata node.
void setDisjointFlag(llvm::Instruction *inst, Operation *op) const
Sets the disjoint flag attribute for the imported operation op given the original instruction inst.
void mapNoResultOp(llvm::Instruction *llvm, Operation *mlir)
Stores a mapping between an LLVM instruction and the imported MLIR operation if the operation returns...
void convertModuleLevelAsm()
Converts the module level asm of the LLVM module to an MLIR module level asm specification.
Value & mapValue(llvm::Value *value)
Provides write-once access to store the MLIR value corresponding to the given LLVM value.
void setExactFlag(llvm::Instruction *inst, Operation *op) const
Sets the exact flag attribute for the imported operation op given the original instruction inst.
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
ModuleImport(ModuleOp mlirModule, std::unique_ptr< llvm::Module > llvmModule, bool emitExpensiveWarnings, bool importEmptyDICompositeTypes, bool preferUnregisteredIntrinsics, bool importStructsAsLiterals)
DILabelAttr matchLabelAttr(llvm::Value *value)
Converts value to a label attribute. Asserts if the matching fails.
FloatAttr matchFloatAttr(llvm::Value *value)
Converts value to a float attribute. Asserts if the matching fails.
LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node, Location loc) const
Returns the loop annotation attribute that corresponds to the given LLVM loop metadata node.
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const
Sets the fastmath flags attribute for the imported operation op given the original instruction inst.
FailureOr< SmallVector< AliasScopeAttr > > matchAliasScopeAttrs(llvm::Value *value)
Converts value to an array of alias scopes or returns failure if the conversion fails.
Value lookupValue(llvm::Value *value)
Returns the MLIR value mapped to the given LLVM value.
Operation * lookupOperation(llvm::Instruction *inst)
Returns the MLIR operation mapped to the given LLVM instruction.
LogicalResult processFunction(llvm::Function *func)
Imports func into the current module.
LogicalResult convertDependentLibrariesMetadata()
Converts !llvm.dependent-libraries metadata to llvm.dependent_libraries LLVM ModuleOp attribute.
Operation *& mapNoResultOp(llvm::Instruction *inst)
Provides write-once access to store the MLIR operation corresponding to the given LLVM instruction if...
RoundingModeAttr matchRoundingModeAttr(llvm::Value *value)
Converts value to a rounding mode attribute.
void convertTargetTriple()
Converts target triple of the LLVM module to an MLIR target triple specification.
void addDebugRecord(llvm::DbgVariableRecord *dbgRecord)
Adds a debug record to the list of debug records that need to be imported after the function conversi...
void convertArgAndResultAttrs(llvm::CallBase *call, ArgAndResultAttrsOpInterface attrsOp, ArrayRef< unsigned > immArgPositions={})
Converts the argument and result attributes attached to call and adds them to attrsOp.
LogicalResult convertModuleFlagsMetadata()
Converts !llvm.module.flags metadata.
void mapValue(llvm::Value *llvm, Value mlir)
Stores the mapping between an LLVM value and its MLIR counterpart.
FailureOr< SmallVector< AccessGroupAttr > > lookupAccessGroupAttrs(const llvm::MDNode *node) const
Returns the access group attributes that map to the access group nodes starting from the access group...
LogicalResult convertGlobals()
Converts all global variables of the LLVM module to MLIR global variables.
void setIntegerOverflowFlags(llvm::Instruction *inst, Operation *op) const
Sets the integer overflow flags (nsw/nuw) attribute for the imported operation op given the original ...
LogicalResult convertCommandlineMetadata()
Converts !llvm.commandline metadata to the llvm.commandline LLVM ModuleOp attribute.
FPExceptionBehaviorAttr matchFPExceptionBehaviorAttr(llvm::Value *value)
Converts value to a FP exception behavior attribute.
void setNonNegFlag(llvm::Instruction *inst, Operation *op) const
Sets the nneg flag attribute for the imported operation op given the original instruction inst.
FailureOr< DereferenceableAttr > translateDereferenceableAttr(const llvm::MDNode *node, unsigned kindID)
Returns the dereferenceable attribute that corresponds to the given LLVM dereferenceable or dereferen...
LogicalResult convertDataLayout()
Converts the data layout of the LLVM module to an MLIR data layout specification.
IntegerAttr matchIntegerAttr(llvm::Value *value)
Converts value to an integer attribute. Asserts if the matching fails.
Helper class that translates an LLVM data layout string to an MLIR data layout specification.
A helper class that converts llvm.loop metadata nodes into corresponding LoopAnnotationAttrs and llvm...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Operation is the basic unit of execution within MLIR.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
llvm::SetVector< T, Vector, Set, N > SetVector
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap