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

Fixes #733 by bringing the rewritten definitions into scope, instead of old ones #754

Merged
merged 2 commits into from
Dec 21, 2024

Conversation

b-studios
Copy link
Collaborator

@b-studios b-studios commented Dec 21, 2024

See discussion in #733 for how I traced down the bug. Summary:

At the point where we dealias the binding l = m, the core tree looks like:

let l = m;
def b_k_1(r, xs) =
  // note the l here:
  let v_3 = run {link(k, v, l, r)}
  go(bitwiseShl225(level, 1), v_3, xs);
...

As it can be clearly seen, l is used in the join points b_k_....
The bindings, after rewriting look like this.

let l = m;
def b_k_1(r, xs) =
  // note the m here:
  let v_3 = run {link(k, v, m, r)}
  go(bitwiseShl225(level, 1), v_3, xs);
...

It appears the renaming was successful. However, the rewritten defs are updated in the InlineContext, which can be seen by inspecting rewrite(List[Definition])

https://github.com/effekt-lang/effekt/blob/9007b059291153c7d9279ec729d62803a7b47275/effekt/shared/src/main/scala/effekt/core/Inline.scala#L88C7-L97

and its callsite:

case Stmt.Scope(definitions, body) =>
val (defs, ctx) = rewrite(definitions)
scope(defs, rewrite(body)(using ctx))

So, the next time a def is inlined, the old def is inlined. It still refers to the old, aliased but now removed, binding... Hence causing the error.

In this PR, I update the defs in the context to now contain the rewritten (dealiased) right hand sides.

@b-studios b-studios requested a review from jiribenes December 21, 2024 18:46
@b-studios
Copy link
Collaborator Author

Looks like a few tests still fail, but I can't look into it any further today.

Copy link
Contributor

@jiribenes jiribenes 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. The tests are only failing because of genericX, so I'll just exclude them on non-JS backends and I'll test it with the other repro. Thanks again! 🎄 🎅 🎁

EDIT: Other repro tested, seems to work!

@jiribenes jiribenes linked an issue Dec 21, 2024 that may be closed by this pull request
@jiribenes jiribenes merged commit 69471e5 into master Dec 21, 2024
2 checks passed
@jiribenes jiribenes deleted the fix/issue733 branch December 21, 2024 20:10
b-studios added a commit that referenced this pull request Dec 24, 2024
…of old ones (#754)

See discussion in #733 for how I traced down the bug. Summary:


At the point where we dealias the binding `l = m`, the core tree looks
like:

```
let l = m;
def b_k_1(r, xs) =
  // note the l here:
  let v_3 = run {link(k, v, l, r)}
  go(bitwiseShl225(level, 1), v_3, xs);
...
```
As it can be clearly seen, `l` is used in the join points `b_k_...`.
The bindings, after rewriting look like this. 
```
let l = m;
def b_k_1(r, xs) =
  // note the m here:
  let v_3 = run {link(k, v, m, r)}
  go(bitwiseShl225(level, 1), v_3, xs);
...
```
It appears the renaming was successful. However, the rewritten defs are
updated in the `InlineContext`, which can be seen by inspecting
`rewrite(List[Definition])`


https://github.com/effekt-lang/effekt/blob/9007b059291153c7d9279ec729d62803a7b47275/effekt/shared/src/main/scala/effekt/core/Inline.scala#L88C7-L97

and its callsite:


https://github.com/effekt-lang/effekt/blob/9007b059291153c7d9279ec729d62803a7b47275/effekt/shared/src/main/scala/effekt/core/Inline.scala#L124-L126

So, the next time a `def` is inlined, the old `def` is inlined. It still
refers to the old, aliased but now removed, binding... Hence causing the
error.

In this PR, I update the defs in the context to now contain the
rewritten (dealiased) right hand sides.

---------

Co-authored-by: Jiří Beneš <mail@jiribenes.com>
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.

Optimiser (inliner) forgets a binding, crashing the resulting program
2 participants