|
| 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/Target/LLVMIR/Import.h" |
| 15 | +#include "mlir/Target/LLVMIR/ModuleImport.h" |
| 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 | + |
| 29 | + SmallVector<llvm::OperandBundleUse> llvmOpBundles; |
| 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; |
| 35 | + SmallVector<NamedAttribute> mlirAttrs; |
| 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 | + // Update importer tracking of results. |
| 49 | + unsigned numRes = op.getNumResults(); |
| 50 | + if (numRes == 1) |
| 51 | + moduleImport.mapValue(inst) = op.getResult(0); |
| 52 | + else if (numRes == 0) |
| 53 | + moduleImport.mapNoResultOp(inst); |
| 54 | + else |
| 55 | + return op.emitError( |
| 56 | + "expected at most one result from target intrinsic call"); |
| 57 | + |
| 58 | + return success(); |
| 59 | +} |
0 commit comments