Skip to content

Conversation

@krystophny
Copy link
Contributor

Fixes #9086.

Follow-up to #9050: remove unconditional allocation from instantiate_MatMul. The allocation/reallocation of allocatable LHS should be handled by the assignment pass based on --realloc-lhs-arrays, not inside the intrinsic.

Behavior now matches regular array assignment and other intrinsics.

@krystophny krystophny marked this pull request as draft December 15, 2025 20:58
@krystophny
Copy link
Contributor Author

... still have to wait how this plays with #9039 , let's merge it after that one.

real, allocatable :: A(:,:), B(:,:), C(:,:)
real :: expected

allocate(A(3, 4), B(4, 2), C(3, 2))
Copy link
Contributor

@certik certik Dec 15, 2025

Choose a reason for hiding this comment

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

Let's also add a test where C is not allocated, and ensure it works with --realloc-lhs-arrays, and that it gives a runtime error without i.

@krystophny krystophny force-pushed the fix/9086-matmul-realloc-flag branch from baebadb to 72e8757 Compare December 17, 2025 11:26
@krystophny
Copy link
Contributor Author

Added matmul_04.f90 test where C is not pre-allocated:

  • With --realloc-lhs-arrays: works correctly, C is auto-allocated
  • Without the flag: runtime error as expected:
    Runtime Error: Array 'result' is indexed but not allocated.
    

The integration test runs with the flag. The error case is verified manually but not added as a reference test due to test framework complications with runtime errors.

@krystophny
Copy link
Contributor Author

krystophny commented Dec 17, 2025

Update: Both tests now added as requested:

  1. integration_tests/matmul_04.f90 - C not pre-allocated, works with --realloc-lhs-arrays
  2. tests/errors/matmul_unallocated_01.f90 - runtime error without the flag

Both tests pass locally.

Copy link
Contributor

@HarshitaKalani HarshitaKalani left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks!

Remove unconditional allocation from instantiate_MatMul. Previously,
MATMUL would allocate the result array inside the generated function
regardless of the --realloc-lhs-arrays flag setting. This violated
LFortran's stricter-by-default behavior where allocatable LHS arrays
must be pre-allocated unless --realloc-lhs-arrays is specified.

Now MATMUL respects the flag like regular array assignment:
- Without flag: error if LHS is not allocated
- With flag: auto-(re)allocate LHS as needed

Add matmul_03 integration test covering pre-allocated result case.

Fixes: lfortran#9086
Addresses review comment: test that matmul works when C is not
pre-allocated, using --realloc-lhs-arrays flag.

Without the flag, runtime error is raised as expected:
  Runtime Error: Array 'result' is indexed but not allocated.
Verifies that matmul to unallocated allocatable result gives:
  Runtime Error: Array 'result' is indexed but not allocated.
@krystophny krystophny force-pushed the fix/9086-matmul-realloc-flag branch from c1b3cb8 to 01e1adb Compare December 17, 2025 20:34
@krystophny krystophny marked this pull request as ready for review December 17, 2025 23:26
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

Looks great, let's see if tests pass.

@@ -0,0 +1,2 @@
At 10:5 of file tests/errors/matmul_unallocated_01.f90
Runtime Error: Array 'result' is indexed but not allocated.
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to think a while about what this error means: I think we transform matmul to loops, then we index A, and that's where this error comes from. That's fine for now.

@certik certik added the Tests::Run-Exhaustive Run exhaustive checks in a PR label Dec 18, 2025
@certik certik merged commit ea4a2a8 into lfortran:main Dec 18, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Tests::Run-Exhaustive Run exhaustive checks in a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bounds check: matmul to unallocated LHS (without --realloc-lhs-arrays)

3 participants