MLIR 22.0.0git
LLVMImportInterface.cpp
Go to the documentation of this file.
1//===------------------------------------------------------------*- 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 methods from LLVMImportInterface.
10//
11//===----------------------------------------------------------------------===//
12
15
16using namespace mlir;
17using namespace mlir::LLVM;
18using namespace mlir::LLVM::detail;
19
20LogicalResult mlir::LLVMImportInterface::convertUnregisteredIntrinsic(
21 OpBuilder &builder, llvm::CallInst *inst,
22 LLVM::ModuleImport &moduleImport) {
23 StringRef intrinName = inst->getCalledFunction()->getName();
24
25 SmallVector<llvm::Value *> args(inst->args());
26 ArrayRef<llvm::Value *> llvmOperands(args);
27
28 SmallVector<llvm::OperandBundleUse> llvmOpBundles;
29 llvmOpBundles.reserve(inst->getNumOperandBundles());
30 for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
31 llvmOpBundles.push_back(inst->getOperandBundleAt(i));
32
33 SmallVector<Value> mlirOperands;
34 SmallVector<NamedAttribute> mlirAttrs;
35 if (failed(moduleImport.convertIntrinsicArguments(
36 llvmOperands, llvmOpBundles, /*requiresOpBundles=*/false,
37 /*immArgPositions=*/{}, /*immArgAttrNames=*/{}, mlirOperands,
38 mlirAttrs)))
39 return failure();
40
41 Type resultType = moduleImport.convertType(inst->getType());
42 auto op = CallIntrinsicOp::create(
43 builder, moduleImport.translateLoc(inst->getDebugLoc()),
44 isa<LLVMVoidType>(resultType) ? TypeRange{} : TypeRange{resultType},
45 StringAttr::get(builder.getContext(), intrinName),
46 ValueRange{mlirOperands}, FastmathFlagsAttr{});
47
48 moduleImport.setFastmathFlagsAttr(inst, op);
49 moduleImport.convertArgAndResultAttrs(inst, op);
50
51 // Update importer tracking of results.
52 unsigned numRes = op.getNumResults();
53 if (numRes == 1)
54 moduleImport.mapValue(inst) = op.getResult(0);
55 else if (numRes == 0)
56 moduleImport.mapNoResultOp(inst);
57 else
58 return op.emitError(
59 "expected at most one result from target intrinsic call");
60
61 return success();
62}
63
64/// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
65/// Returns failure otherwise.
67 OpBuilder &builder, llvm::CallInst *inst,
68 LLVM::ModuleImport &moduleImport) const {
69 // Lookup the dialect interface for the given intrinsic.
70 // Verify the intrinsic identifier maps to an actual intrinsic.
71 llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
72 assert(intrinId != llvm::Intrinsic::not_intrinsic);
73
74 // First lookup the intrinsic across different dialects for known
75 // supported conversions, examples include arm-neon, nvm-sve, etc.
76 Dialect *dialect = nullptr;
77
78 if (!moduleImport.useUnregisteredIntrinsicsOnly())
79 dialect = intrinsicToDialect.lookup(intrinId);
80
81 // No specialized (supported) intrinsics, attempt to generate a generic
82 // version via llvm.call_intrinsic (if available).
83 if (!dialect)
84 return convertUnregisteredIntrinsic(builder, inst, moduleImport);
85
86 // Dispatch the conversion to the dialect interface.
87 const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
88 assert(iface && "expected to find a dialect interface");
89 return iface->convertIntrinsic(builder, inst, moduleImport);
90}
return success()
MLIRContext * getContext() const
Definition Builders.h:56
const LLVMImportDialectInterface * getInterfaceFor(Object *obj) const
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
Base class for dialect interfaces used to import LLVM IR.
virtual LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport) const
Hook for derived dialect interfaces to implement the import of intrinsics into MLIR.
LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport) const
Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
Module import implementation class that provides methods to import globals and functions from an LLVM...
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.
bool useUnregisteredIntrinsicsOnly() const
Whether the importer should try to convert all intrinsics to llvm.call_intrinsic instead of dialect s...
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.
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const
Sets the fastmath flags attribute for the imported operation op given the original instruction inst.
void convertArgAndResultAttrs(llvm::CallBase *call, ArgAndResultAttrsOpInterface attrsOp, ArrayRef< unsigned > immArgPositions={})
Converts the argument and result attributes attached to call and adds them to attrsOp.
void mapValue(llvm::Value *llvm, Value mlir)
Stores the mapping between an LLVM value and its MLIR counterpart.
This class helps build Operations.
Definition Builders.h:207
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
Include the generated interface declarations.