[Mlir-commits] [mlir] [MLIR][NVVM] Enable nvvm intrinsics import (PR #68843)
Ivan R. Ivanov
llvmlistbot at llvm.org
Mon Nov 6 21:45:18 PST 2023
https://github.com/ivanradanov updated https://github.com/llvm/llvm-project/pull/68843
>From cd8f9eb5a07c8d9939a3c1640614a627b9ee6a3b Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <ivanov.i.aa at m.titech.ac.jp>
Date: Thu, 12 Oct 2023 10:18:30 +0900
Subject: [PATCH 1/3] Enable LLVMIR module import with nvvm intrinsics
---
mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt | 2 ++
.../LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp | 6 ++++++
2 files changed, 8 insertions(+)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
index aaf64e63321f204..693d8441b2a117d 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
@@ -50,6 +50,8 @@ add_mlir_dialect(NVVMOps nvvm)
add_mlir_doc(NVVMOps NVVMDialect Dialects/ -gen-dialect-doc -dialect=nvvm)
set(LLVM_TARGET_DEFINITIONS NVVMOps.td)
mlir_tablegen(NVVMConversions.inc -gen-llvmir-conversions)
+mlir_tablegen(NVVMFromLLVMIRConversions.inc -gen-intr-from-llvmir-conversions)
+mlir_tablegen(NVVMConvertibleLLVMIRIntrinsics.inc -gen-convertible-llvmir-intrinsics)
mlir_tablegen(NVVMOpsEnums.h.inc -gen-enum-decls)
mlir_tablegen(NVVMOpsEnums.cpp.inc -gen-enum-defs)
mlir_tablegen(NVVMOpsInterface.h.inc -gen-op-interface-decls)
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index 40d8253d822f647..d20e95754fbf5cb 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -13,6 +13,7 @@
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
+#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/LLVMIR/ModuleImport.h"
@@ -24,6 +25,7 @@
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/Support/ModRef.h"
using namespace mlir;
@@ -37,6 +39,7 @@ using namespace mlir::LLVM::detail;
static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
static const DenseSet<unsigned> convertibleIntrinsics = {
#include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
+#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
};
return convertibleIntrinsics.contains(id);
}
@@ -46,6 +49,7 @@ static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {
static const SmallVector<unsigned> convertibleIntrinsics = {
#include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
+#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
};
return convertibleIntrinsics;
}
@@ -63,6 +67,7 @@ static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
SmallVector<llvm::Value *> args(inst->args());
ArrayRef<llvm::Value *> llvmOperands(args);
#include "mlir/Dialect/LLVMIR/LLVMIntrinsicFromLLVMIRConversions.inc"
+#include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
}
return failure();
@@ -281,6 +286,7 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
void mlir::registerLLVMDialectImport(DialectRegistry ®istry) {
registry.insert<LLVM::LLVMDialect>();
+ registry.insert<NVVM::NVVMDialect>();
registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
dialect->addInterfaces<LLVMDialectLLVMIRImportInterface>();
});
>From 547937bc2ebee91a7d4ed15b6ca10eb12d4f055a Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <ivanov.i.aa at m.titech.ac.jp>
Date: Thu, 12 Oct 2023 10:26:46 +0900
Subject: [PATCH 2/3] Add test
---
mlir/test/Target/LLVMIR/Import/intrinsic.ll | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mlir/test/Target/LLVMIR/Import/intrinsic.ll b/mlir/test/Target/LLVMIR/Import/intrinsic.ll
index 8ce16fe5705cb86..686e45bdce15cbe 100644
--- a/mlir/test/Target/LLVMIR/Import/intrinsic.ll
+++ b/mlir/test/Target/LLVMIR/Import/intrinsic.ll
@@ -865,6 +865,14 @@ define float @ssa_copy(float %0) {
ret float %2
}
+; CHECK-LABEL: llvm.func @nvvm
+define void @nvvm() {
+ ; CHECK: %{{.*}} = nvvm.read.ptx.sreg.ntid.x : i32
+ %1 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+ ret void
+}
+
+declare i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
declare float @llvm.fmuladd.f32(float, float, float)
declare <8 x float> @llvm.fmuladd.v8f32(<8 x float>, <8 x float>, <8 x float>)
declare float @llvm.fma.f32(float, float, float)
>From 35a212f8898fa57d02f046cdaa1720c86b46c4df Mon Sep 17 00:00:00 2001
From: Ivan Radanov Ivanov <ivanov.i.aa at m.titech.ac.jp>
Date: Tue, 7 Nov 2023 14:43:47 +0900
Subject: [PATCH 3/3] Add NVVM Translation Interface
---
mlir/include/mlir/Target/LLVMIR/Dialect/All.h | 2 +
.../Dialect/NVVM/LLVMIRToNVVMTranslation.h | 31 +++++++
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp | 1 +
.../LLVMIR/LLVMIRToLLVMTranslation.cpp | 6 --
.../Target/LLVMIR/Dialect/NVVM/CMakeLists.txt | 18 ++++
.../Dialect/NVVM/LLVMIRToNVVMTranslation.cpp | 93 +++++++++++++++++++
6 files changed, 145 insertions(+), 6 deletions(-)
create mode 100644 mlir/include/mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h
create mode 100644 mlir/lib/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.cpp
diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
index 0563b9bf3d475a4..ed3dbba7ba9c5ba 100644
--- a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
+++ b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h
@@ -22,6 +22,7 @@
#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
+#include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
@@ -71,6 +72,7 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry ®istry) {
static inline void
registerAllFromLLVMIRTranslations(DialectRegistry ®istry) {
registerLLVMDialectImport(registry);
+ registerNVVMDialectImport(registry);
}
} // namespace mlir
diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h b/mlir/include/mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h
new file mode 100644
index 000000000000000..ed17556fb77f3b1
--- /dev/null
+++ b/mlir/include/mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h
@@ -0,0 +1,31 @@
+//===- LLVMIRToNVVMTranslation.h - LLVM IR to NVVM Dialect ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This provides registration calls for LLVM IR to NVVM dialect translation.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TARGET_LLVMIR_DIALECT_LLVMIR_LLVMIRTONVVMTRANSLATION_H
+#define MLIR_TARGET_LLVMIR_DIALECT_LLVMIR_LLVMIRTONVVMTRANSLATION_H
+
+namespace mlir {
+
+class DialectRegistry;
+class MLIRContext;
+
+/// Registers the NVVM dialect and its import from LLVM IR in the given
+/// registry.
+void registerNVVMDialectImport(DialectRegistry ®istry);
+
+/// Registers the NVVM dialect and its import from LLVM IR with the given
+/// context.
+void registerNVVMDialectImport(MLIRContext &context);
+
+} // namespace mlir
+
+#endif
diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
index c521d76a429958d..dbd6985a829a0e8 100644
--- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
+++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/DLTI/DLTI.h"
+#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Target/LLVMIR/Dialect/All.h"
#include "mlir/Target/LLVMIR/Import.h"
diff --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
index d20e95754fbf5cb..40d8253d822f647 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp
@@ -13,7 +13,6 @@
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
-#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/LLVMIR/ModuleImport.h"
@@ -25,7 +24,6 @@
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
-#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/Support/ModRef.h"
using namespace mlir;
@@ -39,7 +37,6 @@ using namespace mlir::LLVM::detail;
static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
static const DenseSet<unsigned> convertibleIntrinsics = {
#include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
-#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
};
return convertibleIntrinsics.contains(id);
}
@@ -49,7 +46,6 @@ static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {
static const SmallVector<unsigned> convertibleIntrinsics = {
#include "mlir/Dialect/LLVMIR/LLVMConvertibleLLVMIRIntrinsics.inc"
-#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
};
return convertibleIntrinsics;
}
@@ -67,7 +63,6 @@ static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
SmallVector<llvm::Value *> args(inst->args());
ArrayRef<llvm::Value *> llvmOperands(args);
#include "mlir/Dialect/LLVMIR/LLVMIntrinsicFromLLVMIRConversions.inc"
-#include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
}
return failure();
@@ -286,7 +281,6 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
void mlir::registerLLVMDialectImport(DialectRegistry ®istry) {
registry.insert<LLVM::LLVMDialect>();
- registry.insert<NVVM::NVVMDialect>();
registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
dialect->addInterfaces<LLVMDialectLLVMIRImportInterface>();
});
diff --git a/mlir/lib/Target/LLVMIR/Dialect/NVVM/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/NVVM/CMakeLists.txt
index 9f3935b0c3f473f..a90de1579816125 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/NVVM/CMakeLists.txt
+++ b/mlir/lib/Target/LLVMIR/Dialect/NVVM/CMakeLists.txt
@@ -1,3 +1,21 @@
+set(LLVM_OPTIONAL_SOURCES
+ LLVMIRToNVVMTranslation.cpp
+ NVVMToLLVMIRTranslation.cpp
+ )
+
+add_mlir_translation_library(MLIRLLVMIRToNVVMTranslation
+ LLVMIRToNVVMTranslation.cpp
+
+ LINK_COMPONENTS
+ Core
+
+ LINK_LIBS PUBLIC
+ MLIRIR
+ MLIRNVVMDialect
+ MLIRSupport
+ MLIRTargetLLVMIRImport
+ )
+
add_mlir_translation_library(MLIRNVVMToLLVMIRTranslation
NVVMToLLVMIRTranslation.cpp
diff --git a/mlir/lib/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.cpp
new file mode 100644
index 000000000000000..855abc12a909efc
--- /dev/null
+++ b/mlir/lib/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.cpp
@@ -0,0 +1,93 @@
+//===- LLVMIRToNVVMTranslation.cpp - Translate LLVM IR to NVVM dialect ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements a translation between LLVM IR and the MLIR NVVM dialect.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
+#include "mlir/Dialect/LLVMIR/NVVMDialect.h"
+#include "mlir/Target/LLVMIR/ModuleImport.h"
+
+#include "llvm/IR/IntrinsicsNVPTX.h"
+
+using namespace mlir;
+using namespace mlir::NVVM;
+
+/// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect
+/// intrinsic. Returns false otherwise.
+static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
+ static const DenseSet<unsigned> convertibleIntrinsics = {
+#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
+ };
+ return convertibleIntrinsics.contains(id);
+}
+
+/// Returns the list of LLVM IR intrinsic identifiers that are convertible to
+/// MLIR NVVM dialect intrinsics.
+static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {
+ static const SmallVector<unsigned> convertibleIntrinsics = {
+#include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc"
+ };
+ return convertibleIntrinsics;
+}
+
+/// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
+/// conversion exits. Returns failure otherwise.
+static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
+ llvm::CallInst *inst,
+ LLVM::ModuleImport &moduleImport) {
+ llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
+
+ // Check if the intrinsic is convertible to an MLIR dialect counterpart and
+ // copy the arguments to an an LLVM operands array reference for conversion.
+ if (isConvertibleIntrinsic(intrinsicID)) {
+ SmallVector<llvm::Value *> args(inst->args());
+ ArrayRef<llvm::Value *> llvmOperands(args);
+#include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
+ }
+
+ return failure();
+}
+
+namespace {
+
+/// Implementation of the dialect interface that converts operations belonging
+/// to the NVVM dialect.
+class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
+public:
+ using LLVMImportDialectInterface::LLVMImportDialectInterface;
+
+ /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a
+ /// conversion exits. Returns failure otherwise.
+ LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
+ LLVM::ModuleImport &moduleImport) const final {
+ return convertIntrinsicImpl(builder, inst, moduleImport);
+ }
+
+ /// Returns the list of LLVM IR intrinsic identifiers that are convertible to
+ /// MLIR NVVM dialect intrinsics.
+ ArrayRef<unsigned> getSupportedIntrinsics() const final {
+ return getSupportedIntrinsicsImpl();
+ }
+};
+
+} // namespace
+
+void mlir::registerNVVMDialectImport(DialectRegistry ®istry) {
+ registry.insert<NVVM::NVVMDialect>();
+ registry.addExtension(+[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) {
+ dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>();
+ });
+}
+
+void mlir::registerNVVMDialectImport(MLIRContext &context) {
+ DialectRegistry registry;
+ registerNVVMDialectImport(registry);
+ context.appendDialectRegistry(registry);
+}
More information about the Mlir-commits
mailing list