Skip to content

[flang] Fix runtime error messages for the MATMUL intrinsic #96928

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

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

psteinfeld
Copy link
Contributor

There are three forms of MATMUL -- where the first argument is a rank 1 array, where the second argument is a rank 1 array, and where both arguments are rank 2 arrays. There's code in the runtime that detects when the array shapes are incorrect. But the code that emits an error message assumes that both arguments are rank 2 arrays.

This change contains code for the other two cases.

There are three forms of MATMUL -- where the first argument is a rank 1
array, where the second argument is a rank 1 array, and where both
arguments are rank 2 arrays.  There's code in the runtime that detects
when the array shapes are incorrect.  But the code that emits an error
message assumes that both arguments are rank 2 arrays.

This change contains code for the other two cases.
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jun 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-flang-runtime

Author: Pete Steinfeld (psteinfeld)

Changes

There are three forms of MATMUL -- where the first argument is a rank 1 array, where the second argument is a rank 1 array, and where both arguments are rank 2 arrays. There's code in the runtime that detects when the array shapes are incorrect. But the code that emits an error message assumes that both arguments are rank 2 arrays.

This change contains code for the other two cases.


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

1 Files Affected:

  • (modified) flang/runtime/matmul.cpp (+19-5)
diff --git a/flang/runtime/matmul.cpp b/flang/runtime/matmul.cpp
index 543284cb5c363..8f9b50a549e1f 100644
--- a/flang/runtime/matmul.cpp
+++ b/flang/runtime/matmul.cpp
@@ -288,11 +288,25 @@ static inline RT_API_ATTRS void DoMatmul(
   }
   SubscriptValue n{x.GetDimension(xRank - 1).Extent()};
   if (n != y.GetDimension(0).Extent()) {
-    terminator.Crash("MATMUL: unacceptable operand shapes (%jdx%jd, %jdx%jd)",
-        static_cast<std::intmax_t>(x.GetDimension(0).Extent()),
-        static_cast<std::intmax_t>(n),
-        static_cast<std::intmax_t>(y.GetDimension(0).Extent()),
-        static_cast<std::intmax_t>(y.GetDimension(1).Extent()));
+    // At this point, we know that there's a shape error.  There are three
+    // possibilities, x is rank 1, y is rank 1, or both are rank 2.
+    if (xRank == 1) {
+      terminator.Crash("MATMUL: unacceptable operand shapes (%jd, %jdx%jd)",
+          static_cast<std::intmax_t>(n),
+          static_cast<std::intmax_t>(y.GetDimension(0).Extent()),
+          static_cast<std::intmax_t>(y.GetDimension(1).Extent()));
+    } else if (yRank == 1) {
+      terminator.Crash("MATMUL: unacceptable operand shapes (%jdx%jd, %jd)",
+          static_cast<std::intmax_t>(x.GetDimension(0).Extent()),
+          static_cast<std::intmax_t>(n),
+          static_cast<std::intmax_t>(y.GetDimension(0).Extent()));
+    } else {
+      terminator.Crash("MATMUL: unacceptable operand shapes (%jdx%jd, %jdx%jd)",
+          static_cast<std::intmax_t>(x.GetDimension(0).Extent()),
+          static_cast<std::intmax_t>(n),
+          static_cast<std::intmax_t>(y.GetDimension(0).Extent()),
+          static_cast<std::intmax_t>(y.GetDimension(1).Extent()));
+    }
   }
   using WriteResult =
       CppTypeFor<RCAT == TypeCategory::Logical ? TypeCategory::Integer : RCAT,

@psteinfeld psteinfeld requested a review from klausler June 27, 2024 21:32
@psteinfeld psteinfeld merged commit e55aa02 into llvm:main Jun 27, 2024
8 of 9 checks passed
@psteinfeld psteinfeld deleted the bug1398 branch June 27, 2024 21:54
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
There are three forms of MATMUL -- where the first argument is a rank 1
array, where the second argument is a rank 1 array, and where both
arguments are rank 2 arrays. There's code in the runtime that detects
when the array shapes are incorrect. But the code that emits an error
message assumes that both arguments are rank 2 arrays.

This change contains code for the other two cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants