Linearisation failure in Array.mapMUnsafe
#4699
Labels
bug
Something isn't working
depends on new code generator
We are currently working on a new compiler (code generator) for Lean. This issue/PR is blocked by it
P-medium
We may work on this issue if we find the time
Prerequisites
Please put an X between the brackets as you perform the following steps:
https://github.com/leanprover/lean4/issues
Avoid dependencies to Mathlib or Batteries.
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
Array.mapMUnsafe
operates on an array in-place. Under some circumstances, the generated IR fails to use the array linearly, which causes many copies and manifests as a quadratic runtime:To prove this is what's going on, let's take a look.
The output contains
Clearly the array is being copied. I guess this corresponds to the lines
let v :=
andlet r :=
in the source ofmapMUnsafe
, reproduced below. (But transposed for some reason?)In the Zulip thread (linked below)
fonqL
observed that swapping the linelet r :=
with the line after it prevents the copy from happening, and so we get linear runtime again. (And naturally the same result is obtained by removing that line altogether.)Context
I first discovered this issue when I noticed that
took an unusually long time to run.
See the associated thread on Zulip, where user
fonqL
reduced the problem to the form presented in this issue.Versions
"4.11.0-nightly-2024-07-07"
onlive.lean-lang.org
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
The text was updated successfully, but these errors were encountered: