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 
16 using namespace mlir;
17 using namespace mlir::LLVM;
18 using namespace mlir::LLVM::detail;
19 
20 LogicalResult 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 
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;
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 }
MLIRContext * getContext() const
Definition: Builders.h:56
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...
Definition: ModuleImport.h:48
bool useUnregisteredIntrinsicsOnly() const
Whether the importer should try to convert all intrinsics to llvm.call_intrinsic instead of dialect s...
Definition: ModuleImport.h:307
This class helps build Operations.
Definition: Builders.h:207
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...