MLIR 24.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
16#include "llvm/ADT/SmallVector.h"
17
18using namespace mlir;
19using namespace mlir::LLVM;
20using namespace mlir::LLVM::detail;
21
22LogicalResult mlir::LLVMImportInterface::convertUnregisteredIntrinsic(
23 OpBuilder &builder, llvm::CallInst *inst,
24 LLVM::ModuleImport &moduleImport) {
25 Location loc = moduleImport.translateLoc(inst->getDebugLoc());
26 SmallVector<llvm::Value *> args(inst->args());
27 SmallVector<Value> mlirOperands;
28 SmallVector<NamedAttribute> mlirAttrs;
29 if (failed(moduleImport.convertIntrinsicArguments(
30 args, llvm::to_vector(inst->operand_bundles()),
31 /*requiresOpBundles=*/true,
32 /*immArgPositions=*/{}, /*immArgAttrNames=*/{}, mlirOperands,
33 mlirAttrs)))
34 return failure();
35
36 CallIntrinsicOp::Properties properties{};
37 properties.setIntrin(
38 builder.getStringAttr(inst->getCalledFunction()->getName()));
39 properties.setOperandSegmentSizes(
40 {static_cast<int32_t>(args.size()),
41 static_cast<int32_t>(mlirOperands.size() - args.size())});
42 if (failed(CallIntrinsicOp::setPropertiesFromAttr(
43 properties, builder.getDictionaryAttr(mlirAttrs),
44 [&] { return emitError(loc); })))
45 return failure();
46
47 Type resultType = moduleImport.convertType(inst->getType());
48 auto op = CallIntrinsicOp::create(
49 builder, loc,
50 isa<LLVMVoidType>(resultType) ? TypeRange{} : TypeRange{resultType},
51 mlirOperands, properties, /*discardableAttributes=*/{});
52
53 moduleImport.setFastmathFlagsAttr(inst, op);
54 moduleImport.convertArgAndResultAttrs(inst, op);
55 op.setStrictfp(inst->getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
56
57 // Update importer tracking of results.
58 unsigned numRes = op.getNumResults();
59 if (numRes == 1)
60 moduleImport.mapValue(inst) = op.getResult(0);
61 else if (numRes == 0)
62 moduleImport.mapNoResultOp(inst);
63 else
64 return op.emitError(
65 "expected at most one result from target intrinsic call");
66
67 return success();
68}
69
70/// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
71/// Returns failure otherwise.
73 OpBuilder &builder, llvm::CallInst *inst,
74 LLVM::ModuleImport &moduleImport) const {
75 // Lookup the dialect interface for the given intrinsic.
76 // Verify the intrinsic identifier maps to an actual intrinsic.
77 llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
78 assert(intrinId != llvm::Intrinsic::not_intrinsic);
79
80 // First lookup the intrinsic across different dialects for known
81 // supported conversions, examples include arm-neon, nvm-sve, etc.
82 Dialect *dialect = nullptr;
83
84 if (!moduleImport.useUnregisteredIntrinsicsOnly())
85 dialect = intrinsicToDialect.lookup(intrinId);
86
87 // No specialized (supported) intrinsics, attempt to generate a generic
88 // version via llvm.call_intrinsic (if available).
89 if (!dialect)
90 return convertUnregisteredIntrinsic(builder, inst, moduleImport);
91
92 // Dispatch the conversion to the dialect interface.
93 const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
94 assert(iface && "expected to find a dialect interface");
95 return iface->convertIntrinsic(builder, inst, moduleImport);
96}
return success()
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:271
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:112
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
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:210
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.