14 #ifndef MLIR_TARGET_LLVMIR_MODULEIMPORT_H
15 #define MLIR_TARGET_LLVMIR_MODULEIMPORT_H
26 class DbgVariableIntrinsic;
36 class DataLayoutImporter;
38 class LoopAnnotationImporter;
49 ModuleImport(ModuleOp mlirModule, std::unique_ptr<llvm::Module> llvmModule,
50 bool emitExpensiveWarnings);
79 assert(
mlir ==
nullptr &&
80 "attempting to map a value that is already mapped");
100 assert(inst->getType()->isVoidTy() &&
101 "attempting to map an operation that returns a result");
102 assert(
mlir ==
nullptr &&
103 "attempting to map an operation that is already mapped");
112 return value.getDefiningOp();
113 return noResultOpMapping.lookup(inst);
118 auto result = blockMapping.try_emplace(
llvm,
mlir);
120 assert(result.second &&
"attempting to map a block that is already mapped");
125 return blockMapping.lookup(block);
206 return tbaaMapping.lookup(node);
244 void clearRegionState() {
245 valueMapping.clear();
246 noResultOpMapping.clear();
247 blockMapping.clear();
248 debugIntrinsics.clear();
251 void setConstantInsertionPointToStart(
Block *block) {
252 constantInsertionBlock = block;
253 constantInsertionOp =
nullptr;
258 LogicalResult convertGlobal(llvm::GlobalVariable *globalVar);
260 LogicalResult convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar);
262 FlatSymbolRefAttr getPersonalityAsAttr(llvm::Function *func);
264 LogicalResult processBasicBlock(llvm::BasicBlock *bb, Block *block);
267 LogicalResult processDebugIntrinsics();
269 LogicalResult processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
270 DominanceInfo &domInfo);
273 LogicalResult convertIntrinsic(llvm::CallInst *inst);
276 LogicalResult convertInstruction(llvm::Instruction *inst);
281 void setNonDebugMetadataAttrs(llvm::Instruction *inst, Operation *op);
285 LogicalResult processInstruction(llvm::Instruction *inst);
290 LogicalResult convertBranchArgs(llvm::Instruction *branch,
291 llvm::BasicBlock *target,
292 SmallVectorImpl<Value> &blockArguments);
296 LogicalResult convertCallTypeAndOperands(llvm::CallBase *callInst,
297 SmallVectorImpl<Type> &types,
298 SmallVectorImpl<Value> &operands);
301 void convertParameterAttributes(llvm::Function *func, LLVMFuncOp funcOp,
305 DictionaryAttr convertParameterAttribute(llvm::AttributeSet llvmParamAttrs,
310 Type getBuiltinTypeForAttr(Type type);
314 Attribute getConstantAsAttr(llvm::Constant *constant);
317 SetVector<llvm::Constant *> getConstantsToConvert(llvm::Constant *constant);
322 FailureOr<Value> convertConstant(llvm::Constant *constant);
328 FailureOr<Value> convertConstantExpr(llvm::Constant *constant);
332 ComdatOp getGlobalComdatOp();
338 LogicalResult processTBAAMetadata(
const llvm::MDNode *node);
343 LogicalResult processAccessGroupMetadata(
const llvm::MDNode *node);
348 LogicalResult processAliasScopeMetadata(
const llvm::MDNode *node);
352 void processComdat(
const llvm::Comdat *comdat);
357 Block *constantInsertionBlock =
nullptr;
359 Operation *constantInsertionOp =
nullptr;
361 Operation *globalInsertionOp =
nullptr;
363 ComdatOp globalComdatOp =
nullptr;
365 MLIRContext *context;
369 std::unique_ptr<llvm::Module> llvmModule;
373 LLVMImportInterface iface;
376 DenseMap<llvm::BasicBlock *, Block *> blockMapping;
378 DenseMap<llvm::Value *, Value> valueMapping;
382 DenseMap<llvm::Instruction *, Operation *> noResultOpMapping;
385 SetVector<llvm::Instruction *> debugIntrinsics;
388 DenseMap<const llvm::MDNode *, Attribute> aliasScopeMapping;
391 DenseMap<const llvm::MDNode *, Attribute> tbaaMapping;
394 DenseMap<const llvm::Comdat *, SymbolRefAttr> comdatMapping;
396 LLVM::TypeFromLLVMIRTranslator typeTranslator;
398 std::unique_ptr<detail::DebugImporter> debugImporter;
400 std::unique_ptr<detail::LoopAnnotationImporter> loopAnnotationImporter;
405 bool emitExpensiveWarnings;
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
This class provides support for representing a failure result, or a valid value of type T.
LogicalResult initializeImport()
Queries all registered dialect interfaces for the supported LLVM IR intrinsic and metadata kinds and ...
Module import implementation class that provides methods to import globals and functions from an LLVM...
Location translateLoc(llvm::DILocation *loc)
Translates the debug location.
LogicalResult convertComdats()
Converts all comdat selectors of the LLVM module to MLIR comdat operations.
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.
ModuleImport(ModuleOp mlirModule, std::unique_ptr< llvm::Module > llvmModule, bool emitExpensiveWarnings)
DILocalVariableAttr matchLocalVariableAttr(llvm::Value *value)
Converts value to a local variable attribute.
LogicalResult convertLinkerOptionsMetadata()
Converts !llvm.linker.options metadata to the llvm.linker.options LLVM dialect operation.
void mapBlock(llvm::BasicBlock *llvm, Block *mlir)
Stores the mapping between an LLVM block and its MLIR counterpart.
void processFunctionAttributes(llvm::Function *func, LLVMFuncOp funcOp)
Converts function attributes of LLVM Function func into LLVM dialect attributes of LLVMFuncOp funcOp.
LogicalResult convertMetadata()
Converts all LLVM metadata nodes that translate to attributes such as alias analysis or access group ...
LogicalResult convertIntrinsicArguments(ArrayRef< llvm::Value * > values, 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.
Value & mapValue(llvm::Value *value)
Provides write-once access to store the MLIR value corresponding to the given LLVM value.
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...
Block * lookupBlock(llvm::BasicBlock *block) const
Returns the MLIR block mapped to the given LLVM block.
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 mapNoResultOp(llvm::Instruction *llvm, Operation *mlir)
Stores a mapping between an LLVM instruction and the imported MLIR operation if the operation returns...
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
Operation * lookupOperation(llvm::Instruction *inst)
Returns the MLIR operation mapped to the given LLVM instruction.
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.
LogicalResult processFunction(llvm::Function *func)
Imports func into the current module.
RoundingModeAttr matchRoundingModeAttr(llvm::Value *value)
Converts value to a rounding mode attribute.
Operation *& mapNoResultOp(llvm::Instruction *inst)
Provides write-once access to store the MLIR operation corresponding to the given LLVM instruction if...
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 ...
FPExceptionBehaviorAttr matchFPExceptionBehaviorAttr(llvm::Value *value)
Converts value to a FP exception behavior attribute.
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.
Type translateType(llvm::Type *type)
Translates the given LLVM IR type to the MLIR LLVM dialect.
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...
Include the generated interface declarations.
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.