-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Computed gotos #19459
Computed gotos #19459
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've not reviewed this properly, but for a first time contribution, this looks like impressive work! I've left a few preliminary comments, but will wait until this is a little more finished before doing a full review.
This pull request makes it possible to write code like this const bit: u1 = 0;
label: switch (bit) {
0 => continue :label 1,
1 => continue :label 0,
} It's obvious that such code results in infinite loop. So far I'm not quite sure if it will possible to compiler to infer this statically. But if so, I'm sure that compiler should produce diagnostics with a note something like this: |
|
@wrongnull it does catch it if the switch is run at compile time |
Are you completely sure about that? At least the compiler is able to detect reaching unreachable code at compile time. As far as I'm concerned there is a control flow graph for thing like that.
good point. Maybe the error message is redundant here. That's why I asked a question about proposal |
Having the switch never result in a value explicitly is allowed: in fact, it's probably the main or only way we'll utilize the syntax in the compiler itself, since the termination case would just |
@EzequielRamis genuinely great work on this PR -- it needs some tinkering, but it's incredibly high-quality for a first-time PR. Would you like to continue working on this? If you're interested in solving the TODOs and issues yourself, I'm very happy for you to do so, and I will help you where I can. However, if you'd prefer to fast-track the actual feature, or if you're too busy to dedicate much more time to it, I can take over this branch once I'm done with my current branch. Your call! |
@mlugg thank you for your kind words! It would be great if you could continue with the todos and the other comments I've left, since I need time for my studies. Before that I will push some fixes of the reviews you've written. |
@mlugg there you have it |
Okay, no worries -- I'll pick this up within the next few days. Thanks! |
You can detect some infinite loops, but in general it's undecidable; see "The Halting Problem" from the Theory of Computer Science. |
I understand it well. I told about detecting loops in control flow graph statically when it's possible |
draft status for > 30 days |
(Succeeded by #19812, which I will finish off soon!) |
Closes #8220.
I took the risk of contributing because it's my first time working with something complex like the zig source code, but I'm currently doing a college project with it and I want to boost the performance with this feature. About the bounty, it's ok if the deadline is missed, as it's personally a nice incentive but secondary.
Below you'd find a list of what I was able to do, what needs to be corrected, what is missing and doubts/things to consider, expecting many items to have phrases like "I have no idea".
TODO
zig fmt
: I think the parsing works, but I couldn't found why this command does not and I don't know how to debug it properly.phi
instruction must be created in Air?switch_dispatch
instruction with its labeledswitch_br
without having type or liveness resolution errors.switch
. Adding thisAir.SwitchBr
field solves it but I don't think it's nice/good enough.airSwitchIndirectBr
orindirectBr
yet.Doubts
... catch(err) switch(err)
andif (...) |p| ... else |err| switch(err)
#11957 should be considered.else
prong is also handled when dispatching, it would be reasonable to warn about using types which could generate megabytes-sized dispatch tables like a littleu16
.