Skip to content

Conversation

wsmoses
Copy link
Member

@wsmoses wsmoses commented Aug 18, 2025

This leads to catastrophic failures, especially when using conversion patterns inside the greedy driver that doesn't provide sufficient rewriter infrastructure.

As this is in conversion (and exported in populate functions), this should be a conversion rather than rewriter. And indeed I do see a segfault due to a mismatch of rewrite/conversion in a downstream project.

@wsmoses
Copy link
Member Author

wsmoses commented Aug 18, 2025

x/ref #154075 EnzymeAD/Enzyme-JAX#1278

@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2025

@llvm/pr-subscribers-mlir

Author: William Moses (wsmoses)

Changes

This leads to catastrophic failures, especially when using conversion patterns inside the greedy driver that doesn't provide sufficient rewriter infrastructure.

As this is in conversion (and exported in populate functions), this should be a conversion rather than rewriter. And indeed I do see a segfault due to a mismatch of rewrite/conversion in a downstream project.


Full diff: https://github.com/llvm/llvm-project/pull/154083.diff

1 Files Affected:

  • (modified) mlir/lib/Conversion/MathToLibm/MathToLibm.cpp (+13-10)
diff --git a/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp b/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
index f7c0d4fe3a799..70514658ac366 100644
--- a/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
+++ b/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
@@ -29,32 +29,35 @@ namespace {
 // Pattern to convert vector operations to scalar operations. This is needed as
 // libm calls require scalars.
 template <typename Op>
-struct VecOpToScalarOp : public OpRewritePattern<Op> {
+struct VecOpToScalarOp : public OpConversionPattern<Op> {
 public:
-  using OpRewritePattern<Op>::OpRewritePattern;
+  using OpConversionPattern<Op>::OpConversionPattern;
 
-  LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter) const final;
+  LogicalResult
+  matchAndRewrite(Op op, ConversionPatternRewriter &rewriter) const final;
 };
 // Pattern to promote an op of a smaller floating point type to F32.
 template <typename Op>
-struct PromoteOpToF32 : public OpRewritePattern<Op> {
+struct PromoteOpToF32 : public OpConversionPattern<Op> {
 public:
-  using OpRewritePattern<Op>::OpRewritePattern;
+  using OpConversionPattern<Op>::OpConversionPattern;
 
-  LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter) const final;
+  LogicalResult
+  matchAndRewrite(Op op, ConversionPatternRewriter &rewriter) const final;
 };
 // Pattern to convert scalar math operations to calls to libm functions.
 // Additionally the libm function signatures are declared.
 template <typename Op>
-struct ScalarOpToLibmCall : public OpRewritePattern<Op> {
+struct ScalarOpToLibmCall : public OpConversionPattern<Op> {
 public:
   using OpRewritePattern<Op>::OpRewritePattern;
   ScalarOpToLibmCall(MLIRContext *context, PatternBenefit benefit,
                      StringRef floatFunc, StringRef doubleFunc)
-      : OpRewritePattern<Op>(context, benefit), floatFunc(floatFunc),
-        doubleFunc(doubleFunc) {};
+      : OpConversionPattern<Op>(context, benefit), floatFunc(floatFunc),
+        doubleFunc(doubleFunc){};
 
-  LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter) const final;
+  LogicalResult
+  matchAndRewrite(Op op, ConversionPatternRewriter &rewriter) const final;
 
 private:
   std::string floatFunc, doubleFunc;

Copy link

github-actions bot commented Aug 18, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
View the diff from clang-format here.
diff --git a/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp b/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
index 3cbe0aa5f..21e35e119 100644
--- a/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
+++ b/mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
@@ -56,7 +56,7 @@ public:
   ScalarOpToLibmCall(MLIRContext *context, PatternBenefit benefit,
                      StringRef floatFunc, StringRef doubleFunc)
       : OpConversionPattern<Op>(context, benefit), floatFunc(floatFunc),
-        doubleFunc(doubleFunc){};
+        doubleFunc(doubleFunc) {};
 
   LogicalResult
   matchAndRewrite(Op op, typename OpConversionPattern<Op>::OpAdaptor adaptor,

@joker-eph
Copy link
Collaborator

Same request as the other PR: I'd like to see a description that refers to something principled into the design and constraint of the dialect conversion. Let's wait for @matthias-springer to help on this.

@wsmoses wsmoses force-pushed the users/wsmoses/math branch from c9e11d9 to c46c084 Compare August 18, 2025 09:30
@wsmoses wsmoses force-pushed the users/wsmoses/math branch 2 times, most recently from 2d13f6c to b8311a7 Compare August 18, 2025 10:08
@wsmoses wsmoses force-pushed the users/wsmoses/math branch from b8311a7 to c75561b Compare August 18, 2025 10:19
Copy link
Member

@matthias-springer matthias-springer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #154075.

@matthias-springer matthias-springer self-requested a review August 18, 2025 12:51
@wsmoses
Copy link
Member Author

wsmoses commented Aug 20, 2025

okay figured out and fixedthe source of the segfault from our end which is indeed unrelated to this PR (essentially blockargument being added during conversion, outside of pattern infra). This still might be useful to do (will leave that discussion to you guys being more familiar with this part of the code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants