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 }
MLIRContext * getContext() const
Definition: Builders.h:56
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.
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:99
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
Definition: ModuleImport.h:181
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:82
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:381
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...