-
Notifications
You must be signed in to change notification settings - Fork 218
Add constexpr if #3268
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
base: main
Are you sure you want to change the base?
Add constexpr if #3268
Conversation
7eaf714 to
4000a7d
Compare
4000a7d to
cf460b6
Compare
richmckeever
left a comment
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.
Let's add tests in typecheck_module_v2_test to prove that for example it's OK to have disparate types in the branches of the const if in a function like:
fn f<N: u32>() -> uN[N] {
const if (N == 1) {
u1:0
} else {
zero!<uN[N]>();
}
}
259f952 to
b29930e
Compare
Signed-off-by: Robert Winkler <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Co-authored-by: Robert Winkler <[email protected]> Signed-off-by: Wojciech Sipak <[email protected]>
Signed-off-by: Wojciech Sipak <[email protected]>
Internal-tag: #[80074]
Internal-tag: #[80074]
b29930e to
10c3f0e
Compare
|
I've applied changes requested in the review. This previously failed due to wrong I've also included an example with conditional proc spawning. In order to spawn procs in FYI @richmckeever |
This PR adds support for the constexpr
ifexpression.Information about the
constmodifier is stored in theConditionalclass and used later during type checking. For constexprif, separate type variables are assigned to each branch of the condition. At this stage, the result is not directly bound to them. During theTypeInfogeneration, the constexpr condition is evaluated, and one branch of the if is selected. Its type is unified, and the entire if is annotated with that type. The bytecode emitter and IR converter have been updated to inline the selected branch, skipping the unchosen one.Proper tests for this feature are still needed and will be added soon.
One issue not yet handled correctly in this PR is the conditional spawning of procs, for which an example has been added that shows the problem. Currently, all spawns affect the type, which should not happen as only the selected branch of constexpr
ifshould be considered. One possible approach is to enable constexpr evaluator in thePopulateInferenceTableVisitorand skip traversing the branch that was not taken. However, thePopulateInferenceTableVisitordoes not have access to the parametric environment, and the condition relying on the parametrics seems to be the most imprtant use-case for using constexprifwhen spawning procs.