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

Replace C NULL for iteration stop with StopIteration #2023

Closed
wants to merge 5 commits into from

Conversation

plajjan
Copy link
Contributor

@plajjan plajjan commented Dec 16, 2024

No description provided.

sydow and others added 2 commits December 16, 2024 10:40
Instead of returning a C NULL when we want to stop iteration, we now
raise the StopIteration exception. This is a prerequisite for doing
unboxed values since we need the NULL value for unboxed values.

Longer term, we might go back to using NULL but packed in a tuple, like
a tagged union basically, which is likely faster than raising an
exception (that uses longjmp) but that requires some other support.
@plajjan plajjan mentioned this pull request Dec 16, 2024
@plajjan plajjan marked this pull request as draft December 16, 2024 09:49
@plajjan
Copy link
Contributor Author

plajjan commented Dec 16, 2024

@sydow I have found a bug which is reproducible just in the Acton repository. I'm not sure if this is because of the same issue as in the respnet repo or not, but it's at least a difference in behavior that seems to come with this PR!

commands to reproduce:

cd test/compiler/hello
acton build

output:

kll@Boxy:~/dt/acton/test/compiler/hello$ acton build
INFO: Acton global cache is empty: rebuilding common libraries (e.g. libc) which will take some time...
INFO: Acton local cache is empty: rebuilding Acton base which will take some time...
Building project in /home/kll/dt/acton/test/compiler/hello
  Compiling hello.act for release
   Finished compilation in   0.010 s
  Final compilation step
   Finished final compilation step in  21.741 s
kll@Boxy:~/dt/acton/test/compiler/hello$

The messages INFO: Acton global cache is empty: rebuilding common libraries (e.g. libc) which will take some time... etc are always printed out. That's not supposed to happen. It should only happen when the cache dir is actually empty. This logic is in cli/src/acton.act and / or base/src/file.act I suppose...

@nordlander
Copy link
Contributor

I've located the bug, it's the CPS pass that contains a leak. Fix is coming up.

@sydow
Copy link
Collaborator

sydow commented Dec 17, 2024 via email

@nordlander
Copy link
Contributor

I believe we have identified two separate bugs! The extended search for volatile variables looks obvious once it's explained, although it hadn't occurred to me...

But the example of building test/compiler/hello also reveals a different problem. It is stems from the fact that the iteration involves a call to the CPS-coverted function fs.walk, which currently causes the whole StopIteration exception handler to be CPS-converted. On the one hand, this doesn't seem to be strictly necessary, but on the other hand it does reveal the truth about the scopings we (well, I) produce for some very special CPS'ed exception handlers.

The core obstacle is that our language doesn't allow non-local reassignment, so we can't even represent the outcome of certain CPS-conversions in a straightforward way (and the lambda-lifter is consequently not prepared to handle them). As a remedy, we add reassigned variables as extra parameters to the generated continuations already in the CPS pass, as a kind of "specialized pre-mature lambda-lifting". It's just that this trick doesn't blend well with the irregular transfer of control of CPS'ed exception handlers.

In fact, I suspect this might be exactly the same problem that renders regular register allocation in the C compiler unsuitable for variables that are reassigned right before a longjmp. But this time a volatile keyword won't help, since it would just be a reminder to ourselves that here's a variable that doesn't respect the normal continuation control flow. Meaning that we cannot treat it as an ordinary continuation parameter, like we currently do.

Need to ponder how to resolve this. Perhaps the non-local reassignments we've talked about introducing when we generalize the var keyword is an avenue, although it merely moves the challenge to the lambda-lifter (where it perhaps more rightly belongs).

In any case, fixing the CPS analysis so that it doesn't convert more exception handlers than strictly necessary might fix the present issue. But we mustn't fool ourselves into believeing this causes the core problem to go away.

@nordlander
Copy link
Contributor

And fixing the overly eager CPS converter immediately exposes the problem Björn described. Intricate!

@nordlander
Copy link
Contributor

I've pushed my small fix that avoid unneeded CPS transation. It doesn't resolve the issue by itself, but will most likely do so in combination with Björn's pending commit. But the fundamental problem with CPS/lambda-lifting of exception handlers remains unsolved for now :-(

@sydow
Copy link
Collaborator

sydow commented Dec 17, 2024 via email

@plajjan
Copy link
Contributor Author

plajjan commented Dec 17, 2024

@sydow @nordlander FYI, it is still broken. CI testing shows almost all test are passing, but it's actually not. If we look in test-macos or test-linux, we can see that acton test doesn't actually run the tests properly. I think they immediately return an OK result instead of reporting actual progress - again, indicating improper control flow.

To reproduce, run cd test/stdlib_tests && acton test - compare on main with results on this branch - it just prints yellow stars for each test and exits instead of actually running the test.

@plajjan
Copy link
Contributor Author

plajjan commented Dec 17, 2024

I'd really like to have some test cases for the various failing cases that we are seeing now, so we don't just rely on whatever the acton cli happens to look like right now

@plajjan
Copy link
Contributor Author

plajjan commented Dec 17, 2024

Okay, so I added a new test program that includes @sydow example from above as well as what I believe is the failing patter we saw in the acton program, that @nordlander and I looked at. This new cps1 test is still failing, which is good. It shows we have a problem :)

Feel free to rename the test program or whatnot, I don't know what this is called.

@plajjan
Copy link
Contributor Author

plajjan commented Dec 17, 2024

Whops, I just noticed I wasn't using the latest version of this branch. The cps1 test program I just wrote does work with the latest fixes 😝

But okay, so there's got to be some other interesting interaction of features going on...

@plajjan
Copy link
Contributor Author

plajjan commented Jan 2, 2025

I have rebased this work on the latest main, pushed to a new branch and opened PR #2054 for the new branch, which supersedes this one and thus I'm closing this PR.

@plajjan plajjan closed this Jan 2, 2025
@plajjan plajjan deleted the iterator_exceptions2 branch January 2, 2025 22:47
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.

3 participants