Skip to content

Commit d5fb4c0

Browse files
ivanradanovgysitDinistro
authored
[MLIR][NVVM] Enable nvvm intrinsics import to LLVMIR (#68843)
Co-authored-by: Tobias Gysi <[email protected]> Co-authored-by: Christian Ulmann <[email protected]>
1 parent d11e54f commit d5fb4c0

File tree

6 files changed

+501
-0
lines changed

6 files changed

+501
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ add_mlir_dialect(NVVMOps nvvm)
5656
add_mlir_doc(NVVMOps NVVMDialect Dialects/ -gen-dialect-doc -dialect=nvvm)
5757
set(LLVM_TARGET_DEFINITIONS NVVMOps.td)
5858
mlir_tablegen(NVVMConversions.inc -gen-llvmir-conversions)
59+
mlir_tablegen(NVVMFromLLVMIRConversions.inc -gen-intr-from-llvmir-conversions)
60+
mlir_tablegen(NVVMConvertibleLLVMIRIntrinsics.inc -gen-convertible-llvmir-intrinsics)
5961
mlir_tablegen(NVVMOpsEnums.h.inc -gen-enum-decls)
6062
mlir_tablegen(NVVMOpsEnums.cpp.inc -gen-enum-defs)
6163
mlir_tablegen(NVVMOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=nvvm)

mlir/include/mlir/Target/LLVMIR/Dialect/All.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
2323
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h"
2424
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
25+
#include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
2526
#include "mlir/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.h"
2627
#include "mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h"
2728
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
@@ -74,6 +75,7 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry &registry) {
7475
static inline void
7576
registerAllFromLLVMIRTranslations(DialectRegistry &registry) {
7677
registerLLVMDialectImport(registry);
78+
registerNVVMDialectImport(registry);
7779
}
7880
} // namespace mlir
7981

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- LLVMIRToNVVMTranslation.h - LLVM IR to NVVM Dialect ------*- 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 provides registration calls for LLVM IR to NVVM dialect translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_LLVMIR_DIALECT_NVVM_LLVMIRTONVVMTRANSLATION_H
14+
#define MLIR_TARGET_LLVMIR_DIALECT_NVVM_LLVMIRTONVVMTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Registers the NVVM dialect and its import from LLVM IR in the given
22+
/// registry.
23+
void registerNVVMDialectImport(DialectRegistry &registry);
24+
25+
/// Registers the NVVM dialect and its import from LLVM IR with the given
26+
/// context.
27+
void registerNVVMDialectImport(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_LLVMIR_DIALECT_NVVM_LLVMIRTONVVMTRANSLATION_H

mlir/lib/Target/LLVMIR/Dialect/NVVM/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
set(LLVM_OPTIONAL_SOURCES
2+
LLVMIRToNVVMTranslation.cpp
3+
NVVMToLLVMIRTranslation.cpp
4+
)
5+
6+
add_mlir_translation_library(MLIRLLVMIRToNVVMTranslation
7+
LLVMIRToNVVMTranslation.cpp
8+
9+
LINK_COMPONENTS
10+
Core
11+
12+
LINK_LIBS PUBLIC
13+
MLIRIR
14+
MLIRNVVMDialect
15+
MLIRSupport
16+
MLIRTargetLLVMIRImport
17+
)
18+
119
add_mlir_translation_library(MLIRNVVMToLLVMIRTranslation
220
NVVMToLLVMIRTranslation.cpp
321

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
13+
#include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
14+
#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
15+
#include "mlir/Target/LLVMIR/ModuleImport.h"
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.
24+
static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
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.
33+
static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {
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.
42+
static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
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:
64+
using LLVMImportDialectInterface::LLVMImportDialectInterface;
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 {
76+
return getSupportedIntrinsicsImpl();
77+
}
78+
};
79+
80+
} // namespace
81+
82+
void mlir::registerNVVMDialectImport(DialectRegistry &registry) {
83+
registry.insert<NVVM::NVVMDialect>();
84+
registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
85+
dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();
86+
});
87+
}
88+
89+
void mlir::registerNVVMDialectImport(MLIRContext &context) {
90+
DialectRegistry registry;
91+
registerNVVMDialectImport(registry);
92+
context.appendDialectRegistry(registry);
93+
}

0 commit comments

Comments
 (0)