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

shallow fold prevention for addr, nkHiddenAddr #24322

Merged
merged 2 commits into from
Oct 18, 2024

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Oct 17, 2024

fixes #24305, refs #23807

Since #23014 nkHiddenAddr is produced to fast assign array elements in iterators. However the array access inside this nkHiddenAddr can get folded at compile time, generating invalid code. In #23807, compile time folding of regular addr expressions was changed to be prevented in transf but nkHiddenAddr was not updated alongside it.

The method for preventing folding in addr in #23807 was also faulty, it should only trigger on the immediate child node of the address rather than all nodes nested inside it. This caused a regression as outlined in this comment.

To fix both issues, addr and nkHiddenAddr now both shallowly prevent constant folding for their immediate children.

@metagn
Copy link
Collaborator Author

metagn commented Oct 17, 2024

The regression from #23807 fixed in this is:

proc main =
  let a = [0, 1, 2]
  let x = addr a[low(a)]
main()
Error: internal error: genMagicExpr: mLow

When the compiler deeply prevents folding, this causes issues for low which depends on constant folding and doesn't codegen at all. Fixing this by itself would have also passed CI but the folding prevention being shallow is a more general fix.

@metagn metagn changed the title don't fold inside nkHiddenAddr same as nkAddr shallow fold prevention for addr, nkHiddenAddr Oct 17, 2024
@Araq Araq merged commit 52cf7df into nim-lang:devel Oct 18, 2024
19 checks passed
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 52cf7df

Hint: mm: orc; opt: speed; options: -d:release
175263 lines; 9.044s; 655.316MiB peakmem

narimiran pushed a commit that referenced this pull request Oct 28, 2024
fixes #24305, refs #23807

Since #23014 `nkHiddenAddr` is produced to fast assign array elements in
iterators. However the array access inside this `nkHiddenAddr` can get
folded at compile time, generating invalid code. In #23807, compile time
folding of regular `addr` expressions was changed to be prevented in
`transf` but `nkHiddenAddr` was not updated alongside it.

The method for preventing folding in `addr` in #23807 was also faulty,
it should only trigger on the immediate child node of the address rather
than all nodes nested inside it. This caused a regression as outlined in
[this
comment](#24322 (comment)).

To fix both issues, `addr` and `nkHiddenAddr` now both shallowly prevent
constant folding for their immediate children.

(cherry picked from commit 52cf7df)
narimiran pushed a commit that referenced this pull request Oct 28, 2024
fixes #24305, refs #23807

Since #23014 `nkHiddenAddr` is produced to fast assign array elements in
iterators. However the array access inside this `nkHiddenAddr` can get
folded at compile time, generating invalid code. In #23807, compile time
folding of regular `addr` expressions was changed to be prevented in
`transf` but `nkHiddenAddr` was not updated alongside it.

The method for preventing folding in `addr` in #23807 was also faulty,
it should only trigger on the immediate child node of the address rather
than all nodes nested inside it. This caused a regression as outlined in
[this
comment](#24322 (comment)).

To fix both issues, `addr` and `nkHiddenAddr` now both shallowly prevent
constant folding for their immediate children.

(cherry picked from commit 52cf7df)
(cherry picked from commit 7ad7ee03e5c0adb6832cbae10a62de7b68ef6fa5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Constant sequence can not be passed as iterator openArray parameter to countup()
2 participants