[Mlir-commits] [mlir] cc6b8d8 - [mlir][conversion] NFC - Relax convert-cf-to-llvm and finalize-memref-to-llvm to also work on non-builtin module ops

Nicolas Vasilache llvmlistbot at llvm.org
Fri Aug 4 04:25:17 PDT 2023


Author: Nicolas Vasilache
Date: 2023-08-04T11:06:17Z
New Revision: cc6b8d8077699916728053dfa760e69e146271cb

URL: https://github.com/llvm/llvm-project/commit/cc6b8d8077699916728053dfa760e69e146271cb
DIFF: https://github.com/llvm/llvm-project/commit/cc6b8d8077699916728053dfa760e69e146271cb.diff

LOG: [mlir][conversion] NFC - Relax convert-cf-to-llvm and finalize-memref-to-llvm to also work on non-builtin module ops

This is a prerequisite for unentangling LowerGpuOpsToNVVMOps which explicitly populates its conversion with
`populateControlFlowToLLVMConversionPatterns` and `populateFinalizeMemRefToLLVMConversionPatterns`.

However those patterns cannot be generally added to a pass anchored on a non-ModuleOp.

Instead, LowerGpuOpsToNVVMOps should stop including the world, in a future commit.

Differential Revision: https://reviews.llvm.org/D156860

Added: 
    

Modified: 
    mlir/include/mlir/Conversion/Passes.td
    mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
    mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index 9608d771a5dd52..382565b59a45ae 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -249,7 +249,7 @@ def ConvertComplexToStandard : Pass<"convert-complex-to-standard"> {
 // ControlFlowToLLVM
 //===----------------------------------------------------------------------===//
 
-def ConvertControlFlowToLLVMPass : Pass<"convert-cf-to-llvm", "ModuleOp"> {
+def ConvertControlFlowToLLVMPass : Pass<"convert-cf-to-llvm"> {
   let summary = "Convert ControlFlow operations to the LLVM dialect";
   let description = [{
     Convert ControlFlow operations into LLVM IR dialect operations.
@@ -257,6 +257,16 @@ def ConvertControlFlowToLLVMPass : Pass<"convert-cf-to-llvm", "ModuleOp"> {
     If other operations are present and their results are required by the LLVM
     IR dialect operations, the pass will fail.  Any LLVM IR operations or types
     already present in the IR will be kept as is.
+
+    This pass is not restricted to run to any op but certain conversions are
+    omitted when the pass is run on a non-builtin ModuleOp. The omitted 
+    conversions are the ones that assume a ModuleOp is accessible, for 
+    manipulating its symbol table (for the purpose of calling into external
+    libraries), namely:
+      1. cf.assert lowering
+
+    Such decoupling of partial lowerings is particularly useful in the context
+    of accelerators that may depend on custom module operations.
   }];
   let dependentDialects = ["LLVM::LLVMDialect"];
   let options = [
@@ -645,15 +655,25 @@ def ConvertMathToFuncs : Pass<"convert-math-to-funcs", "ModuleOp"> {
 // MemRefToLLVM
 //===----------------------------------------------------------------------===//
 
-def FinalizeMemRefToLLVMConversionPass :
-    Pass<"finalize-memref-to-llvm", "ModuleOp"> {
+def FinalizeMemRefToLLVMConversionPass : Pass<"finalize-memref-to-llvm"> {
   let summary = "Finalize MemRef dialect to LLVM dialect conversion";
   let description = [{
-    Finalize the conversion of the operations from the MemRef
-    dialect to the LLVM dialect.
-    This conversion will not convert some complex MemRef
-    operations. Make sure to run `expand-strided-metadata`
-    beforehand for these.
+    Finalize the conversion of the operations from the MemRef dialect to the
+    LLVM dialect.
+    This conversion will not convert some complex MemRef operations. Make sure
+    to run `expand-strided-metadata` beforehand for these.
+
+    This pass is not restricted to run to any op but certain conversions are
+    omitted when the pass is run on a non-builtin ModuleOp. The omitted 
+    conversions are the ones that assume a ModuleOp is accessible, for 
+    manipulating its symbol table (for the purpose of calling into external
+    libraries), namely:
+      1. memref.copy lowering
+      2. memref.alloc lowering
+      3. memref.dealloc lowering
+
+    Such decoupling of partial lowerings is particularly useful in the context
+    of accelerators that may depend on custom module operations.
   }];
   let dependentDialects = ["LLVM::LLVMDialect"];
   let options = [

diff  --git a/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp b/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
index f58f9b4761ad8a..ee221c16d246c9 100644
--- a/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
+++ b/mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
@@ -240,17 +240,25 @@ struct SwitchOpLowering : public ConvertOpToLLVMPattern<cf::SwitchOp> {
 
 } // namespace
 
-void mlir::cf::populateControlFlowToLLVMConversionPatterns(
+static void populateModuleIndependentControlFlowToLLVMConversionPatterns(
     LLVMTypeConverter &converter, RewritePatternSet &patterns) {
   // clang-format off
   patterns.add<
-      AssertOpLowering,
       BranchOpLowering,
       CondBranchOpLowering,
       SwitchOpLowering>(converter);
   // clang-format on
 }
 
+void mlir::cf::populateControlFlowToLLVMConversionPatterns(
+    LLVMTypeConverter &converter, RewritePatternSet &patterns) {
+  // clang-format off
+  patterns.add<AssertOpLowering>(converter);
+  // clang-format on
+  populateModuleIndependentControlFlowToLLVMConversionPatterns(converter,
+                                                               patterns);
+}
+
 void mlir::cf::populateAssertToLLVMConversionPattern(
     LLVMTypeConverter &converter, RewritePatternSet &patterns,
     bool abortOnFailure) {
@@ -279,8 +287,13 @@ struct ConvertControlFlowToLLVM
     options.useOpaquePointers = useOpaquePointers;
 
     LLVMTypeConverter converter(&getContext(), options);
-    mlir::cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
-
+    if (isa<ModuleOp>(getOperation())) {
+      mlir::cf::populateControlFlowToLLVMConversionPatterns(converter,
+                                                            patterns);
+    } else {
+      populateModuleIndependentControlFlowToLLVMConversionPatterns(converter,
+                                                                   patterns);
+    }
     if (failed(applyPartialConversion(getOperation(), target,
                                       std::move(patterns))))
       signalPassFailure();

diff  --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index d69ee3ff82220e..55372b9c9c1248 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -1865,7 +1865,7 @@ class ExtractStridedMetadataOpLowering
 
 } // namespace
 
-void mlir::populateFinalizeMemRefToLLVMConversionPatterns(
+static void populateModuleIndependentFinalizeMemRefToLLVMConversionPatterns(
     LLVMTypeConverter &converter, RewritePatternSet &patterns) {
   // clang-format off
   patterns.add<
@@ -1881,7 +1881,6 @@ void mlir::populateFinalizeMemRefToLLVMConversionPatterns(
       GetGlobalMemrefOpLowering,
       LoadOpLowering,
       MemRefCastOpLowering,
-      MemRefCopyOpLowering,
       MemorySpaceCastOpLowering,
       MemRefReinterpretCastOpLowering,
       MemRefReshapeOpLowering,
@@ -1894,6 +1893,15 @@ void mlir::populateFinalizeMemRefToLLVMConversionPatterns(
       TransposeOpLowering,
       ViewOpLowering>(converter);
   // clang-format on
+}
+
+void mlir::populateFinalizeMemRefToLLVMConversionPatterns(
+    LLVMTypeConverter &converter, RewritePatternSet &patterns) {
+  // clang-format off
+  patterns.add<
+      MemRefCopyOpLowering>(converter);
+  // clang-format on
+
   auto allocLowering = converter.getOptions().allocLowering;
   if (allocLowering == LowerToLLVMOptions::AllocLowering::AlignedAlloc)
     patterns.add<AlignedAllocOpLowering, AlignedReallocOpLowering,
@@ -1901,6 +1909,9 @@ void mlir::populateFinalizeMemRefToLLVMConversionPatterns(
   else if (allocLowering == LowerToLLVMOptions::AllocLowering::Malloc)
     patterns.add<AllocOpLowering, ReallocOpLowering, DeallocOpLowering>(
         converter);
+
+  populateModuleIndependentFinalizeMemRefToLLVMConversionPatterns(converter,
+                                                                  patterns);
 }
 
 namespace {
@@ -1929,6 +1940,12 @@ struct FinalizeMemRefToLLVMConversionPass
                                     &dataLayoutAnalysis);
     RewritePatternSet patterns(&getContext());
     populateFinalizeMemRefToLLVMConversionPatterns(typeConverter, patterns);
+    if (isa<ModuleOp>(getOperation())) {
+      populateFinalizeMemRefToLLVMConversionPatterns(typeConverter, patterns);
+    } else {
+      populateModuleIndependentFinalizeMemRefToLLVMConversionPatterns(
+          typeConverter, patterns);
+    }
     LLVMConversionTarget target(getContext());
     target.addLegalOp<func::FuncOp>();
     if (failed(applyPartialConversion(op, target, std::move(patterns))))


        


More information about the Mlir-commits mailing list