[llvm-branch-commits] [compiler-rt] [flang] [llvm] release/19.x: [Flang][Docs] Update information about AArch64 trampolines (#100391) (PR #100471)

2024-08-17 Thread Carlos Seo via llvm-branch-commits

ceseo wrote:

> @ceseo (or anyone else). If you would like to add a note about this fix in 
> the release notes (completely optional). Please reply to this comment with a 
> one or two sentence description of the fix.  When you are done, please add 
> the release:note label to this PR. 

Add support for llvm.init.trampoline and llvm.adjust.trampoline intrinsics for 
AArch64/Linux.

https://github.com/llvm/llvm-project/pull/100471
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms][WIP] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms][WIP] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread via llvm-branch-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff aad27bf534b59645f47a92f072af798687b1dd0d 
d20fdc5ea1821ae409e1ac57e51cc6932b8130ba --extensions h,cpp -- 
mlir/include/mlir/Transforms/DialectConversion.h 
mlir/lib/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.cpp 
mlir/lib/Transforms/Utils/DialectConversion.cpp
``





View the diff from clang-format here.


``diff
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index bc58d7889b..77cb83efee 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2424,8 +2424,8 @@ LogicalResult 
OperationConverter::convert(ConversionPatternRewriter &rewriter,
 
 static LogicalResult
 legalizeUnresolvedMaterialization(RewriterBase &rewriter,
- UnrealizedConversionCastOp op,
- UnresolvedMaterializationMetadata metadata) {
+  UnrealizedConversionCastOp op,
+  UnresolvedMaterializationMetadata metadata) {
   assert(!op.use_empty() &&
  "expected that dead materializations have already been DCE'd");
   Operation::operand_range inputOperands = op.getOperands();
@@ -2516,7 +2516,8 @@ LogicalResult 
OperationConverter::convertOperations(ArrayRef ops) {
 
   IRRewriter re2(rewriterImpl.context);
   SmallVector remainingCastOps;
-  reconcileUnrealizedCasts(rewriterImpl.getUnresolvedMaterializations(), 
&remainingCastOps);
+  reconcileUnrealizedCasts(rewriterImpl.getUnresolvedMaterializations(),
+   &remainingCastOps);
   for (UnrealizedConversionCastOp op : remainingCastOps)
 if (failed(legalizeUnresolvedMaterialization(
 re2, op,

``




https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms][WIP] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/104668

>From ca2db3fa01051a6e9b74e22efc3adbbff585f8f4 Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 17 Aug 2024 11:38:40 +0200
Subject: [PATCH] [mlir][Transforms][WIP] Dialect conversion: Make
 materializations optional

Build all source/target/argument materializations after the conversion has 
succeeded. Provide a new configuration option for users to opt out of all 
automatic materializations. In that case, the resulting IR will have 
`builtin.unrealized_conversion_cast` ops.
---
 .../Transforms/Utils/DialectConversion.cpp| 382 --
 .../Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir |   5 +-
 .../Transforms/finalizing-bufferize.mlir  |   1 +
 .../test-legalize-type-conversion.mlir|   6 +-
 4 files changed, 94 insertions(+), 300 deletions(-)

diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..1f9b78963403a8 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx), config(config) {}
 
   
//======//
   // State Management
@@ -834,6 +828,7 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   
//======//
   // Materializations
   
//======//
+
   /// Build an unresolved materialization operation given an output type and 
set
   /// of input operands.
   Value buildUnresolvedMaterialization(MaterializationKind kind,
@@ -877,8 +872,8 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// no new IR is created between calls to `eraseOp`/`eraseBlock`.
   struct SingleEraseRewriter : public RewriterBase, RewriterBase::Listener {
   public:
-SingleEraseRewriter(MLIRContext *context)
-: RewriterBase(context, /*listener=*/this) {}
+SingleEraseRewriter(MLIRContext *ctx)
+: RewriterBase(ctx, /*listener=*/this) {}
 
 /// Erase the given op (unless it was already erased).
 void eraseOp(Operation *op) override {
@@ -912,6 +907,8 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// MLIR context.
   MLIRContext *context;
 
+  SingleEraseRewriter eraseRewriter;
+
   // Mapping between replaced values that differ in type. This happens when
   // replacing a value with one of a different type.
   ConversionValueMapping mapping;
@@ -1058,10 +1055,6 @@ void UnresolvedMaterializationRewrite::rollback() {
   op->erase();
 }
 
-void UnresolvedMaterializationRewrite::cleanup(RewriterBase &rewriter) {
-  rewriter.eraseOp(op);
-}
-
 void ConversionPatternRewriterImpl::applyRewrites() {
   // Commit all rewrites.
   IRRewriter rewriter(context, config.listener);
@@ -1069,7 +1062,6 @@ void ConversionPatternRewriterImpl::applyRewrites() {
 rewrite->commit(rewriter);
 
   // Clean up all rewrites.
-  SingleEraseRewriter eraseRewriter(context);
   for (auto &rewrite : rewrites)
 rewrite->cleanup(eraseRewriter);
 }
@@ -2354,12 +2346,6 @@ struct OperationConverter {
   ConversionPatternRewriterImpl &rewriterImpl,
   DenseMap> &inverseMapping);
 
-  /// Legalize any unresolved type materializations.
-  LogicalResult legalizeUnresolvedMaterializations(
-  ConversionPatternRewriter &rewriter,
-  ConversionPatternRewriterImpl &rewriterImpl,
-  DenseMap> &inverseMapping);
-
   /// Legalize an operation result that was marked as "erased".
   LogicalResult
   legalizeErasedResult(Operation *op, OpResult result,
@@ -2406,6 +2392,57 @@ LogicalResult 
OperationConverter::convert(ConversionPatternRewriter &rewriter,
   return success();
 }
 
+static LogicalResult
+legalizeUnresolvedMaterialization(RewriterBase &rewriter,
+  UnresolvedMaterializationRewrite *rewrite) {
+  UnrealizedConversionCastOp op =
+  cast(rewrite->getOperation());
+  assert(!op.use_empty() &&
+ "expected that dead materiali

[llvm-branch-commits] [mlir] [mlir][Transforms][WIP] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/104668

>From 26c00f29b320e95f314372cae557cc0294796863 Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 17 Aug 2024 11:38:40 +0200
Subject: [PATCH] [mlir][Transforms][WIP] Dialect conversion: Make
 materializations optional

Build all source/target/argument materializations after the conversion has 
succeeded. Provide a new configuration option for users to opt out of all 
automatic materializations. In that case, the resulting IR will have 
`builtin.unrealized_conversion_cast` ops.
---
 .../Transforms/Utils/DialectConversion.cpp| 382 --
 .../Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir |   5 +-
 .../Transforms/finalizing-bufferize.mlir  |   1 +
 .../test-legalize-type-conversion.mlir|   6 +-
 4 files changed, 94 insertions(+), 300 deletions(-)

diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..1f9b78963403a8 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx), config(config) {}
 
   
//======//
   // State Management
@@ -834,6 +828,7 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   
//======//
   // Materializations
   
//======//
+
   /// Build an unresolved materialization operation given an output type and 
set
   /// of input operands.
   Value buildUnresolvedMaterialization(MaterializationKind kind,
@@ -877,8 +872,8 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// no new IR is created between calls to `eraseOp`/`eraseBlock`.
   struct SingleEraseRewriter : public RewriterBase, RewriterBase::Listener {
   public:
-SingleEraseRewriter(MLIRContext *context)
-: RewriterBase(context, /*listener=*/this) {}
+SingleEraseRewriter(MLIRContext *ctx)
+: RewriterBase(ctx, /*listener=*/this) {}
 
 /// Erase the given op (unless it was already erased).
 void eraseOp(Operation *op) override {
@@ -912,6 +907,8 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// MLIR context.
   MLIRContext *context;
 
+  SingleEraseRewriter eraseRewriter;
+
   // Mapping between replaced values that differ in type. This happens when
   // replacing a value with one of a different type.
   ConversionValueMapping mapping;
@@ -1058,10 +1055,6 @@ void UnresolvedMaterializationRewrite::rollback() {
   op->erase();
 }
 
-void UnresolvedMaterializationRewrite::cleanup(RewriterBase &rewriter) {
-  rewriter.eraseOp(op);
-}
-
 void ConversionPatternRewriterImpl::applyRewrites() {
   // Commit all rewrites.
   IRRewriter rewriter(context, config.listener);
@@ -1069,7 +1062,6 @@ void ConversionPatternRewriterImpl::applyRewrites() {
 rewrite->commit(rewriter);
 
   // Clean up all rewrites.
-  SingleEraseRewriter eraseRewriter(context);
   for (auto &rewrite : rewrites)
 rewrite->cleanup(eraseRewriter);
 }
@@ -2354,12 +2346,6 @@ struct OperationConverter {
   ConversionPatternRewriterImpl &rewriterImpl,
   DenseMap> &inverseMapping);
 
-  /// Legalize any unresolved type materializations.
-  LogicalResult legalizeUnresolvedMaterializations(
-  ConversionPatternRewriter &rewriter,
-  ConversionPatternRewriterImpl &rewriterImpl,
-  DenseMap> &inverseMapping);
-
   /// Legalize an operation result that was marked as "erased".
   LogicalResult
   legalizeErasedResult(Operation *op, OpResult result,
@@ -2406,6 +2392,57 @@ LogicalResult 
OperationConverter::convert(ConversionPatternRewriter &rewriter,
   return success();
 }
 
+static LogicalResult
+legalizeUnresolvedMaterialization(RewriterBase &rewriter,
+  UnresolvedMaterializationRewrite *rewrite) {
+  UnrealizedConversionCastOp op =
+  cast(rewrite->getOperation());
+  assert(!op.use_empty() &&
+ "expected that dead materiali

[llvm-branch-commits] [mlir] [mlir][Transforms][WIP] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/104668

>From 2658d44d5c8aab38f39c34dc99a8e874c6794b75 Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 17 Aug 2024 11:38:40 +0200
Subject: [PATCH] [mlir][Transforms][WIP] Dialect conversion: Make
 materializations optional

Build all source/target/argument materializations after the conversion has 
succeeded. Provide a new configuration option for users to opt out of all 
automatic materializations. In that case, the resulting IR will have 
`builtin.unrealized_conversion_cast` ops.
---
 .../mlir/Transforms/DialectConversion.h   |  11 +
 .../Transforms/Utils/DialectConversion.cpp| 380 --
 .../Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir |   5 +-
 .../Transforms/finalizing-bufferize.mlir  |   1 +
 .../test-legalize-type-conversion.mlir|   6 +-
 5 files changed, 105 insertions(+), 298 deletions(-)

diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 60113bdef16a23..5f680e8eca7559 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -1124,6 +1124,17 @@ struct ConversionConfig {
   // already been modified) and iterators into past IR state cannot be
   // represented at the moment.
   RewriterBase::Listener *listener = nullptr;
+
+  /// If set to "true", the dialect conversion attempts to build source/target/
+  /// argument materializations through the type converter API in lieu of
+  /// builtin.unrealized_conversion_cast ops. The conversion process fails if
+  /// at least one materialization could not be built.
+  ///
+  /// If set to "false", the dialect conversion does not does not build any
+  /// custom materializations and instead inserts
+  /// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
+  /// is valid.
+  bool buildMaterializations = true;
 };
 
 
//===--===//
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..dd6109c9fe6cfc 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx), config(config) {}
 
   
//======//
   // State Management
@@ -834,6 +828,7 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   
//======//
   // Materializations
   
//======//
+
   /// Build an unresolved materialization operation given an output type and 
set
   /// of input operands.
   Value buildUnresolvedMaterialization(MaterializationKind kind,
@@ -912,6 +907,8 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// MLIR context.
   MLIRContext *context;
 
+  SingleEraseRewriter eraseRewriter;
+
   // Mapping between replaced values that differ in type. This happens when
   // replacing a value with one of a different type.
   ConversionValueMapping mapping;
@@ -1058,10 +1055,6 @@ void UnresolvedMaterializationRewrite::rollback() {
   op->erase();
 }
 
-void UnresolvedMaterializationRewrite::cleanup(RewriterBase &rewriter) {
-  rewriter.eraseOp(op);
-}
-
 void ConversionPatternRewriterImpl::applyRewrites() {
   // Commit all rewrites.
   IRRewriter rewriter(context, config.listener);
@@ -1069,7 +1062,6 @@ void ConversionPatternRewriterImpl::applyRewrites() {
 rewrite->commit(rewriter);
 
   // Clean up all rewrites.
-  SingleEraseRewriter eraseRewriter(context);
   for (auto &rewrite : rewrites)
 rewrite->cleanup(eraseRewriter);
 }
@@ -2354,12 +2346,6 @@ struct OperationConverter {
   ConversionPatternRewriterImpl &rewriterImpl,
   DenseMap> &inverseMapping);
 
-  /// Legalize any unresolved type materializations.
-  LogicalResult legalizeUnresolvedMaterializations(
-  ConversionPatternRewriter &rewriter,
-  ConversionPatternRewriterImp

[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/104668

>From f72f6427871531a135e6621c520b05570dfa3bcb Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 17 Aug 2024 11:38:40 +0200
Subject: [PATCH] [mlir][Transforms][WIP] Dialect conversion: Make
 materializations optional

Build all source/target/argument materializations after the conversion has 
succeeded. Provide a new configuration option for users to opt out of all 
automatic materializations. In that case, the resulting IR will have 
`builtin.unrealized_conversion_cast` ops.
---
 .../mlir/Transforms/DialectConversion.h   |  11 +
 .../Transforms/Utils/DialectConversion.cpp| 383 --
 .../Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir |   5 +-
 .../Transforms/finalizing-bufferize.mlir  |   1 +
 .../test-legalize-type-conversion.mlir|   6 +-
 5 files changed, 108 insertions(+), 298 deletions(-)

diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 60113bdef16a23..5f680e8eca7559 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -1124,6 +1124,17 @@ struct ConversionConfig {
   // already been modified) and iterators into past IR state cannot be
   // represented at the moment.
   RewriterBase::Listener *listener = nullptr;
+
+  /// If set to "true", the dialect conversion attempts to build source/target/
+  /// argument materializations through the type converter API in lieu of
+  /// builtin.unrealized_conversion_cast ops. The conversion process fails if
+  /// at least one materialization could not be built.
+  ///
+  /// If set to "false", the dialect conversion does not does not build any
+  /// custom materializations and instead inserts
+  /// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
+  /// is valid.
+  bool buildMaterializations = true;
 };
 
 
//===--===//
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..f710116186e741 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx), config(config) {}
 
   
//======//
   // State Management
@@ -834,6 +828,7 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   
//======//
   // Materializations
   
//======//
+
   /// Build an unresolved materialization operation given an output type and 
set
   /// of input operands.
   Value buildUnresolvedMaterialization(MaterializationKind kind,
@@ -912,6 +907,11 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// MLIR context.
   MLIRContext *context;
 
+  /// A rewriter that keeps track of ops/block that were already erased and
+  /// skips duplicate op/block erasures. This rewriter is used during the
+  /// "cleanup" phase.
+  SingleEraseRewriter eraseRewriter;
+
   // Mapping between replaced values that differ in type. This happens when
   // replacing a value with one of a different type.
   ConversionValueMapping mapping;
@@ -1058,10 +1058,6 @@ void UnresolvedMaterializationRewrite::rollback() {
   op->erase();
 }
 
-void UnresolvedMaterializationRewrite::cleanup(RewriterBase &rewriter) {
-  rewriter.eraseOp(op);
-}
-
 void ConversionPatternRewriterImpl::applyRewrites() {
   // Commit all rewrites.
   IRRewriter rewriter(context, config.listener);
@@ -1069,7 +1065,6 @@ void ConversionPatternRewriterImpl::applyRewrites() {
 rewrite->commit(rewriter);
 
   // Clean up all rewrites.
-  SingleEraseRewriter eraseRewriter(context);
   for (auto &rewrite : rewrites)
 rewrite->cleanup(eraseRewriter);
 }
@@ -2354,12 +2349,6 @@ struct OperationConverter {
   ConversionPatternRewriterImpl &rewriterImpl,
   DenseMap> &inverseMapping);
 
-  /// Le

[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer ready_for_review 
https://github.com/llvm/llvm-project/pull/104668
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-gpu

Author: Matthias Springer (matthias-springer)


Changes

This commit makes source/target/argument materializations (via the 
`TypeConverter` API) optional.

By default (`ConversionConfig::buildMaterializations = true`), the dialect 
conversion infrastructure tries to legalize all unresolved materializations 
right after the main transformation process has succeeded. If at least one 
unresolved materialization fails to resolve, the dialect conversion fails. 
(With an error message such as `failed to legalize unresolved materialization 
...`.)

Automatic materializations through the `TypeConverter` API can now be 
deactivated. In that case, every unresolved materialization will show up as a 
`builtin.unrealized_conversion_cast` op in the output IR.

There used to be a complex and error-prone analysis in the dialect conversion 
that predicted the future uses of unresolved materializations. Based on that 
logic, some casts (that were deemed to unnecessary) were folded. This analysis 
was needed because folding happened at a point of time when some IR changes 
(e.g., op replacements) had not materialized yet.

This commit removes that analysis. Any folding of cast ops now happens after 
all other IR changes have been materialized and the uses can directly be 
queried from the IR. This simplifies the analysis significantly. And certain 
helper data structures such as `inverseMapping` are no longer needed for the 
analysis. The folding itself is done by `reconcileUnrealizedCasts` (which also 
exists as a standalone pass).

After casts have been folded, the remaining casts are materialized through the 
`TypeConverter`, as usual. This last step can be deactivated in the 
`ConversionConfig`.

`ConversionConfig::buildMaterializations = false` can be used to debug error 
messages such as `failed to legalize unresolved materialization ...`. (It is 
also useful in case automatic materializations are not needed.) The 
materializations that failed to resolve can then be seen as 
`builtin.unrealized_conversion_cast` ops in the resulting IR. (This is better 
than running with `-debug`, because `-debug` shows IR where some IR changes 
have not been materialized yet.)


---

Patch is 30.78 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/104668.diff


5 Files Affected:

- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+11) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+89-294) 
- (modified) mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir (+2-3) 
- (modified) 
mlir/test/Dialect/Bufferization/Transforms/finalizing-bufferize.mlir (+1) 
- (modified) mlir/test/Transforms/test-legalize-type-conversion.mlir (+5-1) 


``diff
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 60113bdef16a23..5f680e8eca7559 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -1124,6 +1124,17 @@ struct ConversionConfig {
   // already been modified) and iterators into past IR state cannot be
   // represented at the moment.
   RewriterBase::Listener *listener = nullptr;
+
+  /// If set to "true", the dialect conversion attempts to build source/target/
+  /// argument materializations through the type converter API in lieu of
+  /// builtin.unrealized_conversion_cast ops. The conversion process fails if
+  /// at least one materialization could not be built.
+  ///
+  /// If set to "false", the dialect conversion does not does not build any
+  /// custom materializations and instead inserts
+  /// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
+  /// is valid.
+  bool buildMaterializations = true;
 };
 
 
//===--===//
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..f710116186e741 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx), config(co

[llvm-branch-commits] [mlir] [mlir][Transforms] Dialect conversion: Make materializations optional (PR #104668)

2024-08-17 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-bufferization

Author: Matthias Springer (matthias-springer)


Changes

This commit makes source/target/argument materializations (via the 
`TypeConverter` API) optional.

By default (`ConversionConfig::buildMaterializations = true`), the dialect 
conversion infrastructure tries to legalize all unresolved materializations 
right after the main transformation process has succeeded. If at least one 
unresolved materialization fails to resolve, the dialect conversion fails. 
(With an error message such as `failed to legalize unresolved materialization 
...`.)

Automatic materializations through the `TypeConverter` API can now be 
deactivated. In that case, every unresolved materialization will show up as a 
`builtin.unrealized_conversion_cast` op in the output IR.

There used to be a complex and error-prone analysis in the dialect conversion 
that predicted the future uses of unresolved materializations. Based on that 
logic, some casts (that were deemed to unnecessary) were folded. This analysis 
was needed because folding happened at a point of time when some IR changes 
(e.g., op replacements) had not materialized yet.

This commit removes that analysis. Any folding of cast ops now happens after 
all other IR changes have been materialized and the uses can directly be 
queried from the IR. This simplifies the analysis significantly. And certain 
helper data structures such as `inverseMapping` are no longer needed for the 
analysis. The folding itself is done by `reconcileUnrealizedCasts` (which also 
exists as a standalone pass).

After casts have been folded, the remaining casts are materialized through the 
`TypeConverter`, as usual. This last step can be deactivated in the 
`ConversionConfig`.

`ConversionConfig::buildMaterializations = false` can be used to debug error 
messages such as `failed to legalize unresolved materialization ...`. (It is 
also useful in case automatic materializations are not needed.) The 
materializations that failed to resolve can then be seen as 
`builtin.unrealized_conversion_cast` ops in the resulting IR. (This is better 
than running with `-debug`, because `-debug` shows IR where some IR changes 
have not been materialized yet.)


---

Patch is 30.78 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/104668.diff


5 Files Affected:

- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+11) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+89-294) 
- (modified) mlir/test/Conversion/NVGPUToNVVM/nvgpu-to-nvvm.mlir (+2-3) 
- (modified) 
mlir/test/Dialect/Bufferization/Transforms/finalizing-bufferize.mlir (+1) 
- (modified) mlir/test/Transforms/test-legalize-type-conversion.mlir (+5-1) 


``diff
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 60113bdef16a23..5f680e8eca7559 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -1124,6 +1124,17 @@ struct ConversionConfig {
   // already been modified) and iterators into past IR state cannot be
   // represented at the moment.
   RewriterBase::Listener *listener = nullptr;
+
+  /// If set to "true", the dialect conversion attempts to build source/target/
+  /// argument materializations through the type converter API in lieu of
+  /// builtin.unrealized_conversion_cast ops. The conversion process fails if
+  /// at least one materialization could not be built.
+  ///
+  /// If set to "false", the dialect conversion does not does not build any
+  /// custom materializations and instead inserts
+  /// builtin.unrealized_conversion_cast ops to ensure that the resulting IR
+  /// is valid.
+  bool buildMaterializations = true;
 };
 
 
//===--===//
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 6238a257b2ffda..f710116186e741 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -702,14 +702,8 @@ class UnresolvedMaterializationRewrite : public 
OperationRewrite {
 return rewrite->getKind() == Kind::UnresolvedMaterialization;
   }
 
-  UnrealizedConversionCastOp getOperation() const {
-return cast(op);
-  }
-
   void rollback() override;
 
-  void cleanup(RewriterBase &rewriter) override;
-
   /// Return the type converter of this materialization (which may be null).
   const TypeConverter *getConverter() const {
 return converterAndKind.getPointer();
@@ -766,7 +760,7 @@ namespace detail {
 struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
  const ConversionConfig &config)
-  : context(ctx), config(config) {}
+  : context(ctx), eraseRewriter(ctx),