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

add const_eval_select macro to reduce redundancy #132571

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Nov 3, 2024

I played around a bit with a macro to make const_eval_select invocations look a bit nicer and avoid repeating the argument lists. Here's what I got. What do you think?

I didn't apply this everywhere yet because I wanted to gather feedback first.

The second commit moves the macros from #132542 into a more sensible place. It didn't seem worth its own PR and would conflict with this PR if done separately.

Cc @oli-obk @saethlin @tgross35

try-job: dist-aarch64-msvc

@rustbot
Copy link
Collaborator

rustbot commented Nov 3, 2024

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 3, 2024
@RalfJung
Copy link
Member Author

RalfJung commented Nov 3, 2024

In many cases, const_eval_select is actually the entire body of the surrounding function. The current macro generates an expression as that seemed most natural, but an alternative worth considering is having a macro that basically generates a function with two bodies -- a const-body and a runtime-body.

Conceptually I prefer the expression-based version, but the function version would be easier to make "look good" in the macro.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 4, 2024

The function based version does seem less redundant, but is a bit annoying when there is a shared section of the function, as you'd then have to use a nested function again.

Maybe closure syntax can be reused here for the args and return type?

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

Sure that is easy to do... it's an immediately invoked closure though, so it'll still be syntactically strange.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

It may make more sense to do it a bit like the Miri callback macro, with something like

                    @capture {
                        arg1: i32 = arg1_val,
                        arg2: T = arg2_val,
                    } -> ReturnType:
                    if const {
                        // ...
                    } else {
                       // ...
                    }  

@oli-obk
Copy link
Contributor

oli-obk commented Nov 4, 2024

Hmm right. Would be neat if we could use closures in general (before this PR) and just have them not capture anything like when converting them to function pointers. Then we wouldn't need to specify types at all.

So this is an improvement over the status, so let's go with it and separately think about other things

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

Does const_eval_select support captureless closures?

So this is an improvement over the status, so let's go with it and separately think about other things

With the @captures syntax or what the PR does?

@RalfJung RalfJung force-pushed the const_eval_select_macro branch 2 times, most recently from 71ddbc4 to d731552 Compare November 4, 2024 09:49
@oli-obk
Copy link
Contributor

oli-obk commented Nov 4, 2024

Does const_eval_select support captureless closures?

I do not believe so

With the @captures syntax or what the PR does?

Without, I think that style only makes sense if there are multiple such things like in miri callbacks

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

Without, I think that style only makes sense if there are multiple such things like in miri callbacks

There are multiple things though, namely the two arms of the if?

I think I like the capture more than the previous syntax since that looks like a function declaration which is confusing -- this is about defining which part of the surrounding environment can be used inside the if const, which IMO is better captured by @capture. Please take a look.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 4, 2024

The return type on capture logic seems odd... maybe have a separate @return line?

Since the #[inline] will be repeated at every site, is there a use for specifying it?

The const/runtime code blocks could also be specified as @const and @runtime respectively, not sure what I like more

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

maybe have a separate @return line?

Or maybe if const -> T { ... } else { ... }?

Since the #[inline] will be repeated at every site, is there a use for specifying it?

I originally was going for perfectly matching the existing code, including the exact attributes.

But I can also add #[inline] automatically and only require annotations for further attributes.

The const/runtime code blocks could also be specified as @const and @runtime respectively, not sure what I like more

I considered that, but I like if const too much to give up on it so easily. ;)

@RalfJung
Copy link
Member Author

RalfJung commented Nov 4, 2024

I did the inline thing. I couldn't find a notation for the return type that I liked more than the current one. if const -> T doesn't work due to follow set restrictions, it'd have to be if const ->T:... which I guess is fine? But it's not really less weird than @capture {} -> T:.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

@bors try

@bors

This comment was marked as outdated.

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 5, 2024
…<try>

add const_eval_select macro to reduce redundancy

I played around a bit with a macro to make const_eval_select invocations look a bit nicer and avoid repeating the argument lists. Here's what I got. What do you think?

I didn't apply this everywhere yet because I wanted to gather feedback first.

The second commit moves the macros from rust-lang#132542 into a more sensible place. It didn't seem worth its own PR and would conflict with this PR if done separately.

Cc `@oli-obk` `@saethlin` `@tgross35`

try-job: dist-aarch64-msvc
@tgross35
Copy link
Contributor

tgross35 commented Nov 5, 2024

@saethlin was suggesting the compiler could just automatically #[inline] anything with f128 or f16 as arguments. I'm not exactly sure how that would have to happen but it sounds nice...

@workingjubilee
Copy link
Member

On ARM64EC, a lot of stuff has to generate thunks, @dpaoliello explained it here: #131781 (comment)

@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

Yeah, but it's a pretty bad bug in that target if that means we have to add #[inline] everywhere a new LLVM type is used. These kind of externalities are not generally acceptable.

was suggesting the compiler could just automatically #[inline] anything with f128 or f16 as arguments. I'm not exactly sure how that would have to happen but it sounds nice...

We should only do that for this broken target though, since it could also have quite negative effects.

@tgross35
Copy link
Contributor

tgross35 commented Nov 5, 2024

was suggesting the compiler could just automatically #[inline] anything with f128 or f16 as arguments. I'm not exactly sure how that would have to happen but it sounds nice...

We should only do that for this broken target though, since it could also have quite negative effects.

Cranelift also needs it, and there are a handful of other targets that LLVM crashes on https://github.com/rust-lang/compiler-builtins/blob/e34429548a0d6a186264dd74fbfc24bd3dbef667/configure.rs#L63-L76. But yeah, it's not ideal.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

Okay so support for these types generally is still pretty poor in LLVM, not just for that one target.

In other words, it's f16/f128 causing externalities here. In that case I agree, we should add some hacks to the compiler to make this more smooth for unrelated rustc development.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

But also, why does this function even get codegen'd? It is const-only!

Maybe we should just always make the const arm of const_eval_select #[inline] to prevent it being fed to LLVM. (We already do that with the runtime arm to avoid the overheads of the extra fn call.)

also move internal const_panic helpers to a better location
@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 5, 2024
…<try>

add const_eval_select macro to reduce redundancy

I played around a bit with a macro to make const_eval_select invocations look a bit nicer and avoid repeating the argument lists. Here's what I got. What do you think?

I didn't apply this everywhere yet because I wanted to gather feedback first.

The second commit moves the macros from rust-lang#132542 into a more sensible place. It didn't seem worth its own PR and would conflict with this PR if done separately.

Cc `@oli-obk` `@saethlin` `@tgross35`

try-job: dist-aarch64-msvc
@bors
Copy link
Contributor

bors commented Nov 5, 2024

⌛ Trying commit 613f53e with merge fecb30e...

// so the end-to-end behavior is the same at compiletime and runtime.
const_eval_select((v,), compiletime, runtime)
};
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one makes me particularly happy. :) This is much nicer with the macro.

@bors
Copy link
Contributor

bors commented Nov 5, 2024

☀️ Try build successful - checks-actions
Build commit: fecb30e (fecb30e858fa70192efbdb7f7d464956222749d4)

@RalfJung
Copy link
Member Author

RalfJung commented Nov 5, 2024

All right, looks like this helped. :)

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Nov 5, 2024

📌 Commit 613f53e has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 5, 2024
@saethlin
Copy link
Member

saethlin commented Nov 5, 2024

@saethlin was suggesting the compiler could just automatically #[inline] anything with f128 or f16 as arguments. I'm not exactly sure how that would have to happen but it sounds nice...

I don't remember suggesting this, but all you'd need to do is add a check below this point:

// From this point on, it is valid to return true or false.
based on checking if any argument types are tcx.types.f16 or tcx.types.f128.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 5, 2024
…r=oli-obk

add const_eval_select macro to reduce redundancy

I played around a bit with a macro to make const_eval_select invocations look a bit nicer and avoid repeating the argument lists. Here's what I got. What do you think?

I didn't apply this everywhere yet because I wanted to gather feedback first.

The second commit moves the macros from rust-lang#132542 into a more sensible place. It didn't seem worth its own PR and would conflict with this PR if done separately.

Cc `@oli-obk` `@saethlin` `@tgross35`

try-job: dist-aarch64-msvc
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 5, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#132259 (rustc_codegen_llvm: Add a new 'pc' option to branch-protection)
 - rust-lang#132409 (CI: switch 7 linux jobs to free runners)
 - rust-lang#132498 (Suggest fixing typos and let bindings at the same time)
 - rust-lang#132524 (chore(style): sync submodule exclusion list between tidy and rustfmt)
 - rust-lang#132567 (Properly suggest `E::assoc` when we encounter `E::Variant::assoc`)
 - rust-lang#132571 (add const_eval_select macro to reduce redundancy)
 - rust-lang#132637 (Do not filter empty lint passes & re-do CTFE pass)
 - rust-lang#132642 (Add documentation on `ast::Attribute`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit aa4fe48 into rust-lang:master Nov 6, 2024
7 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Nov 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 6, 2024
Rollup merge of rust-lang#132571 - RalfJung:const_eval_select_macro, r=oli-obk

add const_eval_select macro to reduce redundancy

I played around a bit with a macro to make const_eval_select invocations look a bit nicer and avoid repeating the argument lists. Here's what I got. What do you think?

I didn't apply this everywhere yet because I wanted to gather feedback first.

The second commit moves the macros from rust-lang#132542 into a more sensible place. It didn't seem worth its own PR and would conflict with this PR if done separately.

Cc ``@oli-obk`` ``@saethlin`` ``@tgross35``

try-job: dist-aarch64-msvc
@RalfJung RalfJung deleted the const_eval_select_macro branch November 6, 2024 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants