MLIR  21.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 
17 using namespace mlir;
18 using namespace mlir::LLVM;
19 using namespace mlir::LLVM::detail;
20 
21 LogicalResult 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 
30  llvmOpBundles.reserve(inst->getNumOperandBundles());
31  for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
32  llvmOpBundles.push_back(inst->getOperandBundleAt(i));
33 
34  SmallVector<Value> mlirOperands;
36  if (failed(moduleImport.convertIntrinsicArguments(
37  llvmOperands, llvmOpBundles, false, {}, {}, mlirOperands, mlirAttrs)))
38  return failure();
39 
40  Type results = moduleImport.convertType(inst->getType());
41  auto op = builder.create<::mlir::LLVM::CallIntrinsicOp>(
42  moduleImport.translateLoc(inst->getDebugLoc()), results,
43  StringAttr::get(builder.getContext(), intrinName),
44  ValueRange{mlirOperands}, FastmathFlagsAttr{});
45 
46  moduleImport.setFastmathFlagsAttr(inst, op);
47 
48  ArrayAttr argsAttr, resAttr;
49  moduleImport.convertParameterAttributes(inst, argsAttr, resAttr, builder);
50  op.setArgAttrsAttr(argsAttr);
51  op.setResAttrsAttr(resAttr);
52 
53  // Update importer tracking of results.
54  unsigned numRes = op.getNumResults();
55  if (numRes == 1)
56  moduleImport.mapValue(inst) = op.getResult(0);
57  else if (numRes == 0)
58  moduleImport.mapNoResultOp(inst);
59  else
60  return op.emitError(
61  "expected at most one result from target intrinsic call");
62 
63  return success();
64 }
65 
66 /// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
67 /// Returns failure otherwise.
69  OpBuilder &builder, llvm::CallInst *inst,
70  LLVM::ModuleImport &moduleImport) const {
71  // Lookup the dialect interface for the given intrinsic.
72  // Verify the intrinsic identifier maps to an actual intrinsic.
73  llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
74  assert(intrinId != llvm::Intrinsic::not_intrinsic);
75 
76  // First lookup the intrinsic across different dialects for known
77  // supported conversions, examples include arm-neon, nvm-sve, etc.
78  Dialect *dialect = nullptr;
79 
80  if (!moduleImport.useUnregisteredIntrinsicsOnly())
81  dialect = intrinsicToDialect.lookup(intrinId);
82 
83  // No specialized (supported) intrinsics, attempt to generate a generic
84  // version via llvm.call_intrinsic (if available).
85  if (!dialect)
86  return convertUnregisteredIntrinsic(builder, inst, moduleImport);
87 
88  // Dispatch the conversion to the dialect interface.
89  const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
90  assert(iface && "expected to find a dialect interface");
91  return iface->convertIntrinsic(builder, inst, moduleImport);
92 }
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:47
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...
Definition: ModuleImport.h:297
void convertParameterAttributes(llvm::CallBase *call, ArrayAttr &argsAttr, ArrayAttr &resAttr, OpBuilder &builder)
Converts the parameter and result attributes in argsAttr and resAttr and add them to the callOp.
void mapNoResultOp(llvm::Instruction *llvm, Operation *mlir)
Stores a mapping between an LLVM instruction and the imported MLIR operation if the operation returns...
Definition: ModuleImport.h:100
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
Definition: ModuleImport.h:182
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const
Sets the fastmath flags attribute for the imported operation op given the original instruction inst.
void mapValue(llvm::Value *llvm, Value mlir)
Stores the mapping between an LLVM value and its MLIR counterpart.
Definition: ModuleImport.h:83
This class helps build Operations.
Definition: Builders.h:205
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
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
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...