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

Split ConstValue #54738

Closed
RalfJung opened this issue Oct 2, 2018 · 9 comments
Closed

Split ConstValue #54738

RalfJung opened this issue Oct 2, 2018 · 9 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Oct 2, 2018

The ConstValue type serves different purposes currently. As return value of const_eval for statics, consts and promoteds, we could instead always return an AllocId + offset (i.e., a Pointer) and avoid even thinking about whether we want the value to be immediate instead. Some places want an immediate (array lengths, pattern matching), they could get a different query returning a different type that "wraps" the main query.

So, I imagine something like: We have one type that serves the current role of ConstValue

enum Const<'tcx> {
   Unevaluated(DefId, &'tcx Substs<'tcx>),
   Evaluated(Pointer),
}

and a new one

enum ConstValue {
  Scalar(Scalar),
  ScalarPair(Scalar, Scalar),
}

(I called the latter ConstValue for symmetry with the Value type. But maybe it's better to not change too many names, and call the 2nd type ConstImmediate or so.)

Then we make the const_eval query return a Pointer, and we add a new query const_eval_value returning a ConstValue.

Overall, this uses the type system to encode that the queries never return Unevaluated, and it lets most callers handle everything uniformly (no need to deal with immediates unless needed).

Cc @oli-obk @eddyb -- does this make any sense?

@RalfJung
Copy link
Member Author

RalfJung commented Oct 2, 2018

Actually, the function producing an immediate doesn't even have to be a query. It could just be a convenience function provided by tcx or const_eval.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

does this make any sense?

yes. I think we talked about this before and I'm still very much in favour.

@eddyb
Copy link
Member

eddyb commented Oct 2, 2018

This seems potentially slow, I'd like to get const generics and lazy normalization first, then we can see if we can do this efficiently - I suspect so, but I'm not sure exactly what we'd need.

@RalfJung
Copy link
Member Author

RalfJung commented Oct 2, 2018

What do you think is slow about this?

@eddyb
Copy link
Member

eddyb commented Oct 2, 2018

@RalfJung I mean, getting the actual value would take an extra operation, but I suppose we can rely on the query being "fast enough" for this to not matter at all.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

Usually we only need that value once or twice. Extracting the value from the memory is just a few instructions.

At this point we don't even need an (AllocId, Offset) but can use an (&'tcx Allocation, Offset) directly.

@RalfJung
Copy link
Member Author

RalfJung commented Oct 2, 2018

At this point we don't even need an (AllocId, Offset) but can use an (&'tcx Allocation, Offset) directly.

No, miri engine needs the AllocId here. I added it because of that. IIRC you r+'d that PR. ;)

@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

Well, yes XD, then we can do (AllocId, &'tcx Allocation, Offset) like currently.

bors added a commit that referenced this issue Oct 22, 2018
Get rid of the `Scalar` and `ScalarPair` variants of `ConstValue`...

... by always using `ByRef` and reading the scalars from the backing `Allocation` where desired

Oh god this uncovered so many bugs and bugs-just-waiting-to-happen. I'm still not fully done, but I don't want to grow this PR any further. It is in a sane state except for the `const_field` function, which was hacky to begin with.

fixes #54738

r? @RalfJung
@jonas-schievink jonas-schievink added C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Jan 27, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Mar 15, 2019

closing in favour of #59210

@oli-obk oli-obk closed this as completed Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants