[Mlir-commits] [mlir] c4769ef - [mlir][Conversion] Re-submit: Implement ConvertToLLVMPatternInterface (1)
Matthias Springer
llvmlistbot at llvm.org
Wed Aug 9 01:00:28 PDT 2023
Author: Matthias Springer
Date: 2023-08-09T09:57:37+02:00
New Revision: c4769ef59c07bcb8c1ab0d9c0fe6e9ef319ffa6a
URL: https://github.com/llvm/llvm-project/commit/c4769ef59c07bcb8c1ab0d9c0fe6e9ef319ffa6a
DIFF: https://github.com/llvm/llvm-project/commit/c4769ef59c07bcb8c1ab0d9c0fe6e9ef319ffa6a.diff
LOG: [mlir][Conversion] Re-submit: Implement ConvertToLLVMPatternInterface (1)
This is a resubmit of the original D157391 change, which was reverted
because it needed special handling for the async dialect. (I removed it
from this change.)
Implement ConvertToLLVMPatternInterface for more dialects: arith,
complex, cf.
Differential Revision: https://reviews.llvm.org/D157391
Added:
Modified:
mlir/include/mlir/Conversion/ArithToLLVM/ArithToLLVM.h
mlir/include/mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h
mlir/include/mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h
mlir/include/mlir/InitAllExtensions.h
mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp
mlir/lib/Conversion/ComplexToLLVM/ComplexToLLVM.cpp
mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
mlir/test/Conversion/ControlFlowToLLVM/assert.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Conversion/ArithToLLVM/ArithToLLVM.h b/mlir/include/mlir/Conversion/ArithToLLVM/ArithToLLVM.h
index b9f57cc8beb0b1..25b82aed9c968a 100644
--- a/mlir/include/mlir/Conversion/ArithToLLVM/ArithToLLVM.h
+++ b/mlir/include/mlir/Conversion/ArithToLLVM/ArithToLLVM.h
@@ -13,6 +13,7 @@
namespace mlir {
+class DialectRegistry;
class LLVMTypeConverter;
class RewritePatternSet;
class Pass;
@@ -23,6 +24,8 @@ class Pass;
namespace arith {
void populateArithToLLVMConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns);
+
+void registerConvertArithToLLVMInterface(DialectRegistry ®istry);
} // namespace arith
} // namespace mlir
diff --git a/mlir/include/mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h b/mlir/include/mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h
index 1385618c098001..69b01b471dd984 100644
--- a/mlir/include/mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h
+++ b/mlir/include/mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h
@@ -11,6 +11,7 @@
#include "mlir/Conversion/LLVMCommon/StructBuilder.h"
namespace mlir {
+class DialectRegistry;
class LLVMTypeConverter;
class Pass;
class RewritePatternSet;
@@ -40,6 +41,9 @@ class ComplexStructBuilder : public StructBuilder {
/// Populate the given list with patterns that convert from Complex to LLVM.
void populateComplexToLLVMConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns);
+
+void registerConvertComplexToLLVMInterface(DialectRegistry ®istry);
+
} // namespace mlir
#endif // MLIR_CONVERSION_COMPLEXTOLLVM_COMPLEXTOLLVM_H_
diff --git a/mlir/include/mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h b/mlir/include/mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h
index 9cc867238a00dc..35b691b8bc44b5 100644
--- a/mlir/include/mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h
+++ b/mlir/include/mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h
@@ -16,6 +16,7 @@
#include <memory>
namespace mlir {
+class DialectRegistry;
class LLVMTypeConverter;
class RewritePatternSet;
class Pass;
@@ -37,6 +38,9 @@ void populateControlFlowToLLVMConversionPatterns(LLVMTypeConverter &converter,
void populateAssertToLLVMConversionPattern(LLVMTypeConverter &converter,
RewritePatternSet &patterns,
bool abortOnFailure = true);
+
+void registerConvertControlFlowToLLVMInterface(DialectRegistry ®istry);
+
} // namespace cf
} // namespace mlir
diff --git a/mlir/include/mlir/InitAllExtensions.h b/mlir/include/mlir/InitAllExtensions.h
index ee51bee9ced1d8..9d2057009d719c 100644
--- a/mlir/include/mlir/InitAllExtensions.h
+++ b/mlir/include/mlir/InitAllExtensions.h
@@ -14,6 +14,9 @@
#ifndef MLIR_INITALLEXTENSIONS_H_
#define MLIR_INITALLEXTENSIONS_H_
+#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
+#include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h"
+#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
#include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
@@ -29,6 +32,9 @@ namespace mlir {
/// individually register the specific extensions that are useful for the
/// pipelines and transformations you are using.
inline void registerAllExtensions(DialectRegistry ®istry) {
+ arith::registerConvertArithToLLVMInterface(registry);
+ registerConvertComplexToLLVMInterface(registry);
+ cf::registerConvertControlFlowToLLVMInterface(registry);
func::registerAllExtensions(registry);
registerConvertMemRefToLLVMInterface(registry);
registerConvertNVVMToLLVMInterface(registry);
diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index d12fee3552dcf2..d90154612a55d1 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -9,6 +9,7 @@
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ArithCommon/AttrToLLVMConverter.h"
+#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
@@ -451,6 +452,35 @@ struct ArithToLLVMConversionPass
};
} // namespace
+//===----------------------------------------------------------------------===//
+// ConvertToLLVMPatternInterface implementation
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// Implement the interface to convert MemRef to LLVM.
+struct ArithToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
+ using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
+ void loadDependentDialects(MLIRContext *context) const final {
+ context->loadDialect<LLVM::LLVMDialect>();
+ }
+
+ /// Hook for derived dialect interface to provide conversion patterns
+ /// and mark dialect legal for the conversion target.
+ void populateConvertToLLVMConversionPatterns(
+ ConversionTarget &target, LLVMTypeConverter &typeConverter,
+ RewritePatternSet &patterns) const final {
+ arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
+ }
+};
+} // namespace
+
+void mlir::arith::registerConvertArithToLLVMInterface(
+ DialectRegistry ®istry) {
+ registry.addExtension(+[](MLIRContext *ctx, arith::ArithDialect *dialect) {
+ dialect->addInterfaces<ArithToLLVMDialectInterface>();
+ });
+}
+
//===----------------------------------------------------------------------===//
// Pattern Population
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp
index d1998cfb4b642d..7c8baee1448575 100644
--- a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp
+++ b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp
@@ -8,6 +8,7 @@
#include "mlir/Conversion/AsyncToLLVM/AsyncToLLVM.h"
+#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
diff --git a/mlir/lib/Conversion/ComplexToLLVM/ComplexToLLVM.cpp b/mlir/lib/Conversion/ComplexToLLVM/ComplexToLLVM.cpp
index 7185b01afdc00a..b219de7fef8f8c 100644
--- a/mlir/lib/Conversion/ComplexToLLVM/ComplexToLLVM.cpp
+++ b/mlir/lib/Conversion/ComplexToLLVM/ComplexToLLVM.cpp
@@ -8,6 +8,7 @@
#include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h"
+#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
@@ -342,3 +343,32 @@ void ConvertComplexToLLVMPass::runOnOperation() {
applyPartialConversion(getOperation(), target, std::move(patterns))))
signalPassFailure();
}
+
+//===----------------------------------------------------------------------===//
+// ConvertToLLVMPatternInterface implementation
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// Implement the interface to convert MemRef to LLVM.
+struct ComplexToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
+ using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
+ void loadDependentDialects(MLIRContext *context) const final {
+ context->loadDialect<LLVM::LLVMDialect>();
+ }
+
+ /// Hook for derived dialect interface to provide conversion patterns
+ /// and mark dialect legal for the conversion target.
+ void populateConvertToLLVMConversionPatterns(
+ ConversionTarget &target, LLVMTypeConverter &typeConverter,
+ RewritePatternSet &patterns) const final {
+ populateComplexToLLVMConversionPatterns(typeConverter, patterns);
+ }
+};
+} // namespace
+
+void mlir::registerConvertComplexToLLVMInterface(DialectRegistry ®istry) {
+ registry.addExtension(
+ +[](MLIRContext *ctx, complex::ComplexDialect *dialect) {
+ dialect->addInterfaces<ComplexToLLVMDialectInterface>();
+ });
+}
diff --git a/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp b/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
index f58f9b4761ad8a..d99968d78d248c 100644
--- a/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
+++ b/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
@@ -13,6 +13,7 @@
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
+#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
@@ -287,3 +288,34 @@ struct ConvertControlFlowToLLVM
}
};
} // namespace
+
+//===----------------------------------------------------------------------===//
+// ConvertToLLVMPatternInterface implementation
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// Implement the interface to convert MemRef to LLVM.
+struct ControlFlowToLLVMDialectInterface
+ : public ConvertToLLVMPatternInterface {
+ using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
+ void loadDependentDialects(MLIRContext *context) const final {
+ context->loadDialect<LLVM::LLVMDialect>();
+ }
+
+ /// Hook for derived dialect interface to provide conversion patterns
+ /// and mark dialect legal for the conversion target.
+ void populateConvertToLLVMConversionPatterns(
+ ConversionTarget &target, LLVMTypeConverter &typeConverter,
+ RewritePatternSet &patterns) const final {
+ mlir::cf::populateControlFlowToLLVMConversionPatterns(typeConverter,
+ patterns);
+ }
+};
+} // namespace
+
+void mlir::cf::registerConvertControlFlowToLLVMInterface(
+ DialectRegistry ®istry) {
+ registry.addExtension(+[](MLIRContext *ctx, cf::ControlFlowDialect *dialect) {
+ dialect->addInterfaces<ControlFlowToLLVMDialectInterface>();
+ });
+}
diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index db9df579820d69..edb3de9cf68338 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -1,5 +1,9 @@
// RUN: mlir-opt -pass-pipeline="builtin.module(func.func(convert-arith-to-llvm))" %s -split-input-file | FileCheck %s
+// Same below, but using the `ConvertToLLVMPatternInterface` entry point
+// and the generic `convert-to-llvm` pass.
+// RUN: mlir-opt --convert-to-llvm --split-input-file %s | FileCheck %s
+
// CHECK-LABEL: @vector_ops
func.func @vector_ops(%arg0: vector<4xf32>, %arg1: vector<4xi1>, %arg2: vector<4xi64>, %arg3: vector<4xi64>) -> vector<4xf32> {
// CHECK-NEXT: %0 = llvm.mlir.constant(dense<4.200000e+01> : vector<4xf32>) : vector<4xf32>
diff --git a/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir b/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
index be46c22155a6c6..32c1a38af5a8fc 100644
--- a/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
+++ b/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
@@ -1,5 +1,9 @@
// RUN: mlir-opt %s -convert-complex-to-llvm | FileCheck %s
+// Same below, but using the `ConvertToLLVMPatternInterface` entry point
+// and the generic `convert-to-llvm` pass.
+// RUN: mlir-opt --convert-to-llvm --split-input-file %s | FileCheck %s
+
// CHECK-LABEL: func @complex_create
// CHECK-SAME: (%[[REAL0:.*]]: f32, %[[IMAG0:.*]]: f32)
// CHECK-NEXT: %[[CPLX0:.*]] = llvm.mlir.undef : !llvm.struct<(f32, f32)>
diff --git a/mlir/test/Conversion/ControlFlowToLLVM/assert.mlir b/mlir/test/Conversion/ControlFlowToLLVM/assert.mlir
index 67804b60f26f4a..0f5b7bc273212d 100644
--- a/mlir/test/Conversion/ControlFlowToLLVM/assert.mlir
+++ b/mlir/test/Conversion/ControlFlowToLLVM/assert.mlir
@@ -1,5 +1,9 @@
// RUN: mlir-opt %s -convert-cf-to-llvm='use-opaque-pointers=1' | FileCheck %s
+// Same below, but using the `ConvertToLLVMPatternInterface` entry point
+// and the generic `convert-to-llvm` pass.
+// RUN: mlir-opt --convert-to-llvm --split-input-file %s | FileCheck %s
+
func.func @main() {
%a = arith.constant 0 : i1
cf.assert %a, "assertion foo"
More information about the Mlir-commits
mailing list