MLIR  19.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 #include "llvm/IR/IntrinsicsNVPTX.h"
18 
19 using namespace mlir;
20 using namespace mlir::NVVM;
21 
22 /// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect
23 /// intrinsic. Returns false otherwise.
25  static const DenseSet<unsigned> convertibleIntrinsics = {
26 #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
27  };
28  return convertibleIntrinsics.contains(id);
29 }
30 
31 /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
32 /// MLIR NVVM dialect intrinsics.
34  static const SmallVector<unsigned> convertibleIntrinsics = {
35 #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
36  };
37  return convertibleIntrinsics;
38 }
39 
40 /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
41 /// conversion exits. Returns failure otherwise.
43  llvm::CallInst *inst,
44  LLVM::ModuleImport &moduleImport) {
45  llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
46 
47  // Check if the intrinsic is convertible to an MLIR dialect counterpart and
48  // copy the arguments to an an LLVM operands array reference for conversion.
49  if (isConvertibleIntrinsic(intrinsicID)) {
50  SmallVector<llvm::Value *> args(inst->args());
51  ArrayRef<llvm::Value *> llvmOperands(args);
52 #include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
53  }
54 
55  return failure();
56 }
57 
58 namespace {
59 
60 /// Implementation of the dialect interface that converts operations belonging
61 /// to the NVVM dialect.
62 class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
63 public:
65 
66  /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
67  /// conversion exits. Returns failure otherwise.
68  LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
69  LLVM::ModuleImport &moduleImport) const final {
70  return convertIntrinsicImpl(builder, inst, moduleImport);
71  }
72 
73  /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
74  /// MLIR NVVM dialect intrinsics.
75  ArrayRef<unsigned> getSupportedIntrinsics() const final {
77  }
78 };
79 
80 } // namespace
81 
83  registry.insert<NVVM::NVVMDialect>();
84  registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
85  dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();
86  });
87 }
88 
90  DialectRegistry registry;
91  registerNVVMDialectImport(registry);
92  context.appendDialectRegistry(registry);
93 }
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.
void addExtension(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:47
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:209
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
void registerNVVMDialectImport(DialectRegistry &registry)
Registers the NVVM dialect and its import from LLVM IR in the given registry.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26