Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Fix flang build after #83132 #83253

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

matthias-springer
Copy link
Member

This fix is a temporary workaround. LowerHLFIRIntrinsics.cpp should be using the greedy pattern rewriter or a manual IR traversal. All patterns in this file are rewrite patterns. The test failure was caused by replaceAllUsesWith, which is not supported by the dialect conversion; additional asserts were added recently to prevent incorrect API usage. These trigger now.

Alternatively, turning the patterns into conversion patterns and specifying a type converter may work.

Failing test case: Fortran/gfortran/regression/gfortran-regression-compile-regression__inline_matmul_14_f90.test

This fix is a temporary workaround. `LowerHLFIRIntrinsics.cpp` should be
using the greedy pattern rewriter or a manual IR traversal. All patterns
in this file are rewrite patterns. The test failure was caused by
`replaceAllUsesWith`, which is not supported by the dialect conversion;
additional asserts were added recently to prevent incorrect API usage.
These trigger now.

Alternatively, turning the patterns into conversion patterns and
specifying a type converter may work.

Failing test case: `Fortran/gfortran/regression/gfortran-regression-compile-regression__inline_matmul_14_f90.test`
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 28, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 28, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Matthias Springer (matthias-springer)

Changes

This fix is a temporary workaround. LowerHLFIRIntrinsics.cpp should be using the greedy pattern rewriter or a manual IR traversal. All patterns in this file are rewrite patterns. The test failure was caused by replaceAllUsesWith, which is not supported by the dialect conversion; additional asserts were added recently to prevent incorrect API usage. These trigger now.

Alternatively, turning the patterns into conversion patterns and specifying a type converter may work.

Failing test case: Fortran/gfortran/regression/gfortran-regression-compile-regression__inline_matmul_14_f90.test


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

1 Files Affected:

  • (modified) flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp (+8-1)
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp b/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
index 314e4264c17e83..377cc44392028f 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
@@ -176,7 +176,14 @@ class HlfirIntrinsicConversion : public mlir::OpRewritePattern<OP> {
           rewriter.eraseOp(use);
       }
     }
-    rewriter.replaceAllUsesWith(op->getResults(), {base});
+    // TODO: This entire pass should be a greedy pattern rewrite or a manual
+    // IR traversal. A dialect conversion cannot be used here because
+    // `replaceAllUsesWith` is not supported. Similarly, `replaceOp` is not
+    // suitable because "op->getResult(0)" and "base" can have different types.
+    // In such a case, the dialect conversion will attempt to convert the type,
+    // but no type converter is specified in this pass. Also note that all
+    // patterns in this pass are actually rewrite patterns.
+    op->getResult(0).replaceAllUsesWith(base);
     rewriter.replaceOp(op, base);
   }
 };

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix and advice. I'll work on a more permanent fix asap.

@matthias-springer
Copy link
Member Author

Btw, how do I run the above mentioned test case? I think this commit should fix it, but I haven't actually verified it locally. There is no file with that name (gfortran-regression-compile-regression__inline_matmul_14_f90) in my LLVM checkout.

@tblah
Copy link
Contributor

tblah commented Feb 28, 2024

The gfortran regression tests are in the LLVM testsuite repo: https://github.com/llvm/llvm-test-suite/tree/main/Fortran/gfortran

I ran the test suite successfully before approving.

You could test manually by building and running https://github.com/llvm/llvm-test-suite/blob/main/Fortran/gfortran/regression/inline_matmul_14.f90 with flang. Alternatively you could run the whole test suite. The documentation should be up to date. Please get in touch if any of it is unclear or incorrect.

@matthias-springer matthias-springer merged commit 1f74f5f into main Feb 28, 2024
6 of 7 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/fix-flang branch February 28, 2024 13:05
tblah added a commit to tblah/llvm-project that referenced this pull request Feb 28, 2024
In llvm#83253 @matthias-springer pointed out that LowerHLFIRIntrinsics.cpp
should not be using rewrite patterns with the dialect conversion driver.

The intention of this pass is to lower HLFIR intrinsic operations into
FIR and so I think this best fits dialect conversion and so I have
changed all of these into conversion patterns. Taking this approach
also avoids test suite churn because GreedyPatternRewriter also performs
canonicalization.

One remaining misuse of the MLIR API is that we replace values of one
type with a different (although safe) type e.g.
!hlfir.expr<2xi32> -> !hlfir.expr<?xi32>. There isn't a convenient way
to perform this conversion in IR at the moment because fir.convert does
not accept !hlfir.expr.
tblah added a commit to tblah/llvm-project that referenced this pull request Feb 29, 2024
In llvm#83253 @matthias-springer pointed out that LowerHLFIRIntrinsics.cpp
should not be using rewrite patterns with the dialect conversion driver.

The intention of this pass is to lower HLFIR intrinsic operations into
FIR so it conceptually fits dialect conversion. However, dialect
conversion is much stricter about changing types when replacing
operations. This pass sometimes looses track of array bounds, resulting
in replacements with operations with different but compatible types
(expressions of the same rank and element types but with or without
compile time known array bounds). This is difficult to accommodate with
the dialect conversion driver and so I have changed to use the greedy
pattern rewriter.

There is a lot of test churn because the greedy pattern rewriter
also performs canonicalization.
tblah added a commit that referenced this pull request Mar 1, 2024
)

In #83253 @matthias-springer pointed out that LowerHLFIRIntrinsics.cpp
should not be using rewrite patterns with the dialect conversion driver.

The intention of this pass is to lower HLFIR intrinsic operations into
FIR so it conceptually fits dialect conversion. However, dialect
conversion is much stricter about changing types when replacing
operations. This pass sometimes looses track of array bounds, resulting
in replacements with operations with different but compatible types
(expressions of the same rank and element types but with or without
compile time known array bounds). This is difficult to accommodate with
the dialect conversion driver and so I have changed to use the greedy
pattern rewriter.

There is a lot of test churn because the greedy pattern rewriter also
performs canonicalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants