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 resultType = moduleImport.convertType(inst->getType());
41  auto op = builder.create<::mlir::LLVM::CallIntrinsicOp>(
42  moduleImport.translateLoc(inst->getDebugLoc()),
43  isa<LLVMVoidType>(resultType) ? TypeRange{} : TypeRange{resultType},
44  StringAttr::get(builder.getContext(), intrinName),
45  ValueRange{mlirOperands}, FastmathFlagsAttr{});
46 
47  moduleImport.setFastmathFlagsAttr(inst, op);
48 
49  ArrayAttr argsAttr, resAttr;
50  moduleImport.convertParameterAttributes(inst, argsAttr, resAttr, builder);
51  op.setArgAttrsAttr(argsAttr);
52  op.setResAttrsAttr(resAttr);
53 
54  // Update importer tracking of results.
55  unsigned numRes = op.getNumResults();
56  if (numRes == 1)
57  moduleImport.mapValue(inst) = op.getResult(0);
58  else if (numRes == 0)
59  moduleImport.mapNoResultOp(inst);
60  else
61  return op.emitError(
62  "expected at most one result from target intrinsic call");
63 
64  return success();
65 }
66 
67 /// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
68 /// Returns failure otherwise.
70  OpBuilder &builder, llvm::CallInst *inst,
71  LLVM::ModuleImport &moduleImport) const {
72  // Lookup the dialect interface for the given intrinsic.
73  // Verify the intrinsic identifier maps to an actual intrinsic.
74  llvm::Intrinsic::ID intrinId = inst->getIntrinsicID();
75  assert(intrinId != llvm::Intrinsic::not_intrinsic);
76 
77  // First lookup the intrinsic across different dialects for known
78  // supported conversions, examples include arm-neon, nvm-sve, etc.
79  Dialect *dialect = nullptr;
80 
81  if (!moduleImport.useUnregisteredIntrinsicsOnly())
82  dialect = intrinsicToDialect.lookup(intrinId);
83 
84  // No specialized (supported) intrinsics, attempt to generate a generic
85  // version via llvm.call_intrinsic (if available).
86  if (!dialect)
87  return convertUnregisteredIntrinsic(builder, inst, moduleImport);
88 
89  // Dispatch the conversion to the dialect interface.
90  const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
91  assert(iface && "expected to find a dialect interface");
92  return iface->convertIntrinsic(builder, inst, moduleImport);
93 }
MLIRContext * getContext() const
Definition: Builders.h:55
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:204
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
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
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...