19 #include "llvm/ADT/PostOrderIterator.h"
20 #include "llvm/ADT/ScopeExit.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/ADT/TypeSwitch.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/Support/ModRef.h"
33 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc"
39 #include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
41 return convertibleIntrinsics.contains(
id);
48 #include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
50 return convertibleIntrinsics;
65 #include "mlir/Dialect/LLVMIR/LLVMIntrinsicFromLLVMIRConversions.inc"
75 llvm::LLVMContext::MD_prof, llvm::LLVMContext::MD_tbaa,
76 llvm::LLVMContext::MD_access_group, llvm::LLVMContext::MD_loop,
77 llvm::LLVMContext::MD_noalias, llvm::LLVMContext::MD_alias_scope};
78 return convertibleMetadata;
88 if (!node->getNumOperands())
91 auto *name = dyn_cast<llvm::MDString>(node->getOperand(0));
96 if (name->getString().equals(
"function_entry_count")) {
99 if (node->getNumOperands() != 2)
102 llvm::ConstantInt *entryCount =
103 llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(1));
106 if (
auto funcOp = dyn_cast<LLVMFuncOp>(op)) {
107 funcOp.setFunctionEntryCount(entryCount->getZExtValue());
111 <<
"expected function_entry_count to be attached to a function";
114 if (!name->getString().equals(
"branch_weights"))
119 branchWeights.reserve(node->getNumOperands() - 1);
120 for (
unsigned i = 1, e = node->getNumOperands(); i != e; ++i) {
121 llvm::ConstantInt *branchWeight =
122 llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(i));
125 branchWeights.push_back(branchWeight->getZExtValue());
128 if (
auto iface = dyn_cast<BranchWeightOpInterface>(op)) {
144 auto iface = dyn_cast<AliasAnalysisOpInterface>(op);
148 iface.setTBAATags(
ArrayAttr::get(iface.getContext(), tbaaTagSym));
163 auto iface = dyn_cast<AccessGroupOpInterface>(op);
168 iface.getContext(), llvm::to_vector_of<Attribute>(*accessGroups)));
177 LoopAnnotationAttr attr =
183 .Case<LLVM::BrOp, LLVM::CondBrOp>([&](
auto branchOp) {
184 branchOp.setLoopAnnotationAttr(attr);
187 .Default([](
auto) {
return failure(); });
200 auto iface = dyn_cast<AliasAnalysisOpInterface>(op);
205 iface.getContext(), llvm::to_vector_of<Attribute>(*aliasScopes)));
217 if (
failed(noAliasScopes))
220 auto iface = dyn_cast<AliasAnalysisOpInterface>(op);
225 iface.getContext(), llvm::to_vector_of<Attribute>(*noAliasScopes)));
251 if (kind == llvm::LLVMContext::MD_prof)
253 if (kind == llvm::LLVMContext::MD_tbaa)
255 if (kind == llvm::LLVMContext::MD_access_group)
257 if (kind == llvm::LLVMContext::MD_loop)
259 if (kind == llvm::LLVMContext::MD_alias_scope)
261 if (kind == llvm::LLVMContext::MD_noalias)
265 llvm_unreachable(
"unknown metadata type");
283 registry.
insert<LLVM::LLVMDialect>();
285 dialect->addInterfaces<LLVMDialectLLVMIRImportInterface>();
static ArrayRef< unsigned > getSupportedMetadataImpl()
Returns the list of LLVM IR metadata kinds that are convertible to MLIR LLVM dialect attributes.
static LogicalResult setLoopAttr(const llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Converts the given loop metadata node to an MLIR loop annotation attribute and attaches it to the imp...
static ArrayRef< unsigned > getSupportedIntrinsicsImpl()
Returns the list of LLVM IR intrinsic identifiers that are convertible to MLIR LLVM dialect intrinsic...
static LogicalResult setTBAAAttr(const llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Searches for the attribute that maps to the given TBAA metadata node and attaches it to the imported ...
static LogicalResult setProfilingAttr(OpBuilder &builder, llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Converts the given profiling metadata node to an MLIR profiling attribute and attaches it to the impo...
static LogicalResult setAliasScopesAttr(const llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Looks up all the alias scope attributes that map to the alias scope nodes starting from the alias sco...
static LogicalResult setNoaliasScopesAttr(const llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Looks up all the alias scope attributes that map to the alias scope nodes starting from the noalias m...
static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id)
Returns true if the LLVM IR intrinsic is convertible to an MLIR LLVM dialect intrinsic.
static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport)
Converts the LLVM intrinsic to an MLIR LLVM dialect operation if a conversion exits.
static LogicalResult setAccessGroupsAttr(const llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport)
Looks up all the access group attributes that map to the access group nodes starting from the access ...
Attributes are known-constant values of operations.
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
This class provides support for representing a failure result, or a valid value of type T.
Base class for dialect interfaces used to import LLVM IR.
LLVMImportDialectInterface(Dialect *dialect)
Module import implementation class that provides methods to import globals and functions from an LLVM...
Attribute lookupTBAAAttr(const llvm::MDNode *node) const
Returns the MLIR attribute mapped to the given LLVM TBAA metadata node.
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.
LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node, Location loc) const
Returns the loop annotation attribute that corresponds to the given LLVM loop metadata node.
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...
MLIRContext is the top-level object for a collection of MLIR operations.
void appendDialectRegistry(const DialectRegistry ®istry)
Append the contents of the given dialect registry to the registry associated with this context.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
InFlightDiagnostic emitWarning(const Twine &message={})
Emit a warning about this operation, reporting up to any diagnostic handlers that may be listening.
Location getLoc()
The source location the operation was defined or derived from.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
void registerLLVMDialectImport(DialectRegistry ®istry)
Registers the LLVM dialect and its import from LLVM IR in the given registry.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.