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
13#include "mlir/Target/LLVMIR/LLVMImportInterface.h"
14#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
15#include "mlir/Target/LLVMIR/ModuleImport.h"
16
17using namespace mlir;
18using namespace mlir::LLVM;
19using namespace mlir::LLVM::detail;
20
21LogicalResult mlir::LLVMImportInterface::convertUnregisteredIntrinsic(
22 OpBuilder &builder, llvm::CallInst *inst,
23 LLVM::ModuleImport &moduleImport) {
24 StringRef intrinName = inst->getCalledFunction()->getName();
25
26 SmallVector<llvm::Value *> args(inst->args());
27 ArrayRef<llvm::Value *> llvmOperands(args);
28
29 SmallVector<llvm::OperandBundleUse> llvmOpBundles;
30 llvmOpBundles.reserve(N: inst->getNumOperandBundles());
31 for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
32 llvmOpBundles.push_back(Elt: inst->getOperandBundleAt(Index: i));
33
34 SmallVector<Value> mlirOperands;
35 SmallVector<NamedAttribute> mlirAttrs;
36 if (failed(Result: moduleImport.convertIntrinsicArguments(
37 values: llvmOperands, opBundles: llvmOpBundles, /*requiresOpBundles=*/false,
38 /*immArgPositions=*/{}, /*immArgAttrNames=*/{}, valuesOut&: mlirOperands,
39 attrsOut&: mlirAttrs)))
40 return failure();
41
42 Type resultType = moduleImport.convertType(type: inst->getType());
43 auto op = CallIntrinsicOp::create(
44 builder, location: moduleImport.translateLoc(loc: inst->getDebugLoc()),
45 resultTypes: isa<LLVMVoidType>(Val: resultType) ? TypeRange{} : TypeRange{resultType},
46 intrin: StringAttr::get(context: builder.getContext(), bytes: intrinName),
47 args: ValueRange{mlirOperands}, fastMathFlags: FastmathFlagsAttr{});
48
49 moduleImport.setFastmathFlagsAttr(inst, op);
50 moduleImport.convertArgAndResultAttrs(call: inst, attrsOp: op);
51
52 // Update importer tracking of results.
53 unsigned numRes = op.getNumResults();
54 if (numRes == 1)
55 moduleImport.mapValue(value: inst) = op.getResult(i: 0);
56 else if (numRes == 0)
57 moduleImport.mapNoResultOp(inst);
58 else
59 return op.emitError(
60 message: "expected at most one result from target intrinsic call");
61
62 return success();
63}
64
65/// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
66/// Returns failure otherwise.
67LogicalResult mlir::LLVMImportInterface::convertIntrinsic(
68 OpBuilder &builder, llvm::CallInst *inst,
69 LLVM::ModuleImport &moduleImport) const {
70 // Lookup the dialect interface for the given intrinsic.
71 // Verify the intrinsic identifier maps to an actual intrinsic.
72 llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
73 assert(intrinId != llvm::Intrinsic::not_intrinsic);
74
75 // First lookup the intrinsic across different dialects for known
76 // supported conversions, examples include arm-neon, nvm-sve, etc.
77 Dialect *dialect = nullptr;
78
79 if (!moduleImport.useUnregisteredIntrinsicsOnly())
80 dialect = intrinsicToDialect.lookup(Val: intrinId);
81
82 // No specialized (supported) intrinsics, attempt to generate a generic
83 // version via llvm.call_intrinsic (if available).
84 if (!dialect)
85 return convertUnregisteredIntrinsic(builder, inst, moduleImport);
86
87 // Dispatch the conversion to the dialect interface.
88 const LLVMImportDialectInterface *iface = getInterfaceFor(obj: dialect);
89 assert(iface && "expected to find a dialect interface");
90 return iface->convertIntrinsic(builder, inst, moduleImport);
91}
92