MLIR 22.0.0git
LLVMIRToNVVMTranslation.cpp
Go to the documentation of this file.
1//===- LLVMIRToNVVMTranslation.cpp - Translate LLVM IR to NVVM dialect ----===//
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 a translation between LLVM IR and the MLIR NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/IR/ConstantRange.h"
17
18using namespace mlir;
19using namespace mlir::NVVM;
20
21/// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect
22/// intrinsic. Returns false otherwise.
23static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
24 static const DenseSet<unsigned> convertibleIntrinsics = {
25#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
26 };
27 return convertibleIntrinsics.contains(id);
28}
29
30/// Returns the list of LLVM IR intrinsic identifiers that are convertible to
31/// MLIR NVVM dialect intrinsics.
33 static const SmallVector<unsigned> convertibleIntrinsics = {
34#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
35 };
36 return convertibleIntrinsics;
37}
38
39/// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
40/// conversion exits. Returns failure otherwise.
41static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
42 llvm::CallInst *inst,
43 LLVM::ModuleImport &moduleImport) {
44 llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
45
46 // Check if the intrinsic is convertible to an MLIR dialect counterpart and
47 // copy the arguments to an an LLVM operands array reference for conversion.
48 if (isConvertibleIntrinsic(intrinsicID)) {
49 SmallVector<llvm::Value *> args(inst->args());
50 ArrayRef<llvm::Value *> llvmOperands(args);
51
53 llvmOpBundles.reserve(inst->getNumOperandBundles());
54 for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
55 llvmOpBundles.push_back(inst->getOperandBundleAt(i));
56
57#include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
58 }
59
60 return failure();
61}
62
63namespace {
64
65/// Implementation of the dialect interface that converts operations belonging
66/// to the NVVM dialect.
67class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
68public:
70
71 /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
72 /// conversion exits. Returns failure otherwise.
73 LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
74 LLVM::ModuleImport &moduleImport) const final {
75 return convertIntrinsicImpl(builder, inst, moduleImport);
76 }
77
78 /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
79 /// MLIR NVVM dialect intrinsics.
80 ArrayRef<unsigned> getSupportedIntrinsics() const final {
82 }
83};
84
85} // namespace
86
88 registry.insert<NVVM::NVVMDialect>();
89 registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
90 dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();
91 });
92}
93
95 DialectRegistry registry;
97 context.appendDialectRegistry(registry);
98}
static ArrayRef< unsigned > getSupportedIntrinsicsImpl()
Returns the list of LLVM IR intrinsic identifiers that are convertible to MLIR NVVM dialect intrinsic...
static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id)
Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect intrinsic.
static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport)
Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a conversion exits.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Base class for dialect interfaces used to import LLVM IR.
Module import implementation class that provides methods to import globals and functions from an LLVM...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
This class helps build Operations.
Definition Builders.h:207
Include the generated interface declarations.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:128
void registerNVVMDialectImport(DialectRegistry &registry)
Registers the NVVM dialect and its import from LLVM IR in the given registry.