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 
17 using namespace mlir;
18 using namespace mlir::NVVM;
19 
20 /// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect
21 /// intrinsic. Returns false otherwise.
23  static const DenseSet<unsigned> convertibleIntrinsics = {
24 #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
25  };
26  return convertibleIntrinsics.contains(id);
27 }
28 
29 /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
30 /// MLIR NVVM dialect intrinsics.
32  static const SmallVector<unsigned> convertibleIntrinsics = {
33 #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
34  };
35  return convertibleIntrinsics;
36 }
37 
38 /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
39 /// conversion exits. Returns failure otherwise.
40 static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
41  llvm::CallInst *inst,
42  LLVM::ModuleImport &moduleImport) {
43  llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
44 
45  // Check if the intrinsic is convertible to an MLIR dialect counterpart and
46  // copy the arguments to an an LLVM operands array reference for conversion.
47  if (isConvertibleIntrinsic(intrinsicID)) {
48  SmallVector<llvm::Value *> args(inst->args());
49  ArrayRef<llvm::Value *> llvmOperands(args);
50 
52  llvmOpBundles.reserve(inst->getNumOperandBundles());
53  for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
54  llvmOpBundles.push_back(inst->getOperandBundleAt(i));
55 
56 #include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
57  }
58 
59  return failure();
60 }
61 
62 namespace {
63 
64 /// Implementation of the dialect interface that converts operations belonging
65 /// to the NVVM dialect.
66 class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
67 public:
69 
70  /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
71  /// conversion exits. Returns failure otherwise.
72  LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
73  LLVM::ModuleImport &moduleImport) const final {
74  return convertIntrinsicImpl(builder, inst, moduleImport);
75  }
76 
77  /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
78  /// MLIR NVVM dialect intrinsics.
79  ArrayRef<unsigned> getSupportedIntrinsics() const final {
81  }
82 };
83 
84 } // namespace
85 
87  registry.insert<NVVM::NVVMDialect>();
88  registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
89  dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();
90  });
91 }
92 
94  DialectRegistry registry;
95  registerNVVMDialectImport(registry);
96  context.appendDialectRegistry(registry);
97 }
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.
LLVMImportDialectInterface(Dialect *dialect)
Module import implementation class that provides methods to import globals and functions from an LLVM...
Definition: ModuleImport.h:48
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
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:205
Include the generated interface declarations.
void registerNVVMDialectImport(DialectRegistry &registry)
Registers the NVVM dialect and its import from LLVM IR in the given registry.