-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Further possibilities for mir::Constant cleanup #115877
Comments
I tried some refactoring in I think if we do the refactor proposed above, and we have a new There is an interesting interaction of two aspects I hadn't considered yet: we might want to guarantee that valtrees can only be constructed for StructuralEq types. This means under this proposal, non-structural-eq types cannot get the optimized representation. This loses the optimized representation for (some) scalar newtypes. But I think we long as we keep the optimized representations for literals (which have primitive types and hence are structural eq), this should be fine. |
There is one footgun I'd like to treat around constants too: cloning a The same question can be asked for a valtree that represents a reference. |
Under my proposal And for a valtree I would say this is entirely intentional: if such a valtree is used multiple times, we don't guarantee that you get the same address on each use at runtime. It will be a new AllocId each time. Codegen might be able to deduplicate identical |
So, regarding the MIR opt concern discussed here -- to me that sounds like you'd want constants that are valtrees only for non-pointer scalar types (to keep an efficient representation for integers). So inside the optimization you'd want a normalization pass that turns all constants that contain pointers into I don't think we want general clonability of all We also need a better name for |
Our
mir::Constant
/ty::Const
story has come a long way. I like that we have two distinct types that each have aneval
function that returns an appropriate "evaluated" form (mir::interpret::ConstValue
/ty::Valtree
). Generally I think this is fairly clean now. :)However, there are still some subtle invariants and sharp corners:
eval_to_allocation_raw
) to aConstValue
and back to an interpreterOpTy
should ideally not alter the value (failing to do that leads to surprises likeslice::from_raw_parts
returns a different address in const context for u8 #105536).ConstValue
and inValtree
) and how to convert between them.const_to_pat
is only ever called with aValtree
orConstValue
, and furthermore it relies on the caller "normalizing" to a valtree if possible. The logic of "try valtree first, fall back to general ConstValue" exists multiple times (here and here).str
patterns must be turned into valtrees which is a very inefficient representation, despite them having a nice efficient representation asConstValue
.So... I am wondering if we can have a world where there is only one efficient representation, and it's
Valtree
. I want to killConstValue
entirely. The things that need to be represented efficiently are:ConstValue
is additionally efficient for newtype wrappers around primitive types. I don't know if these matter, but if they do we can always declare that for any type with Scalar ABI, the valtree is a leaf -- we can optimize away all the branches. Or we declare that for anyrepr(transparent)
type, valtrees skip the branch and just represent the one non-1-ZST field (and use an empty branch for 1-ZST types). We have options, and we can hide this behind general functions that are given a type and know how to construct/destruct a valtree of that type.[(), 1024]
).&str
and&[u8]
/&[u8; N]
: currently not very efficient as valtrees. For patterns I think we already turn all strings into valtrees, but we don't currently do that for all literals, and that would be silly to do. So we'd definitely want a more efficient representation. And we have a lot of constraints here -- I think we want this to be both "not any more expensive to construct than the currentConstValue
" (since otherwise we will regress MIR building), and we want it to be reasonably efficient to turn into an interpreterOpTy
(so that uses of the constants in const-eval are efficient). Codegen also needs to be able to efficiently consume them.Efficient representation of str and byte literals
So for that last point about slices, currently "efficient consumption in const-eval and codegen" is achieved by sharing the codepath with handling arbitrary results of const-evaluation (both are represented as a
ConstAllocation
). const-eval needs to generate a freshAllocId
but at least it doesn't have to copy the data. MIR building right now isn't actually that efficient since a newConstAllocation
needs to be created.It's hard to be efficient on all sides. I see two options here:
[u8]
, suitably interned. (Maybe even push this all the way toLitKind::ByteStr
/CStr
? And maybe evenLitKind::Str
, which currently stores aSymbol
but that's actually not that great for us since we can't get an&'tcx [u8]
out of that.) Codegen backends should learn how to directly turn that into a suitable constant in their target representation. The const-eval interpreter needs to either make a full copy (boooh) or we alterinterpret::Allocation
to store the bytes in something like aCow<'tcx, [u8]>
-- that is still a bit less efficient than today's literal-to-OpTy
path but at least it doesn't copy the string contents. (It needs to create a newAllocation
though which we can currently avoid.)ConstAllocation
is a type that the interpreter can handle very efficiently, and also codegen backends know very well how to turn this into their appropriate backend types. So we could useConstAllocation
as "an efficiently storedstr
/[u8]
/[u8; N]
". It's kind of dicey to put aConstAllocation
into a valtree though, since the entire point of valtrees is to not be able to represent arbitrary interpreter data. We'd have tight constraints: only for valtrees of typestr
/[u8]
/[u8; N]
; no provenance; everything initialized (inInitMaskBlocks::Lazy
mode); alignment 1; not mutable. I think this is coherent (equal data implies equal representation), and it's a lot less churn for the rest of the compiler than the previous alternative, but I can see how it could make someone feel uneasy.Valtrees as the only efficient representation
On the other hand, I think we could get a nice payoff from making valtrees the only efficient representation:
ConstValue
disappears!mir::ConstantKind::Value
becomes just aConstAlloc
(we might have to add anoffset
if we still want to support projection fortry_destructure_mir_constant_for_diagnostics
).EvalToConstValueResult
would have anEither<ValTree, AllocId>
value, and it would pickLeft
exactly and only for scalar types, to make these easily accessible in the rest of the compiler without accessingAllocation
s. (It might as well beEither<Scalar, AllocId>
.) Turning that into amir::ConstantKind
will usemir::ConstantKind::Ty
when encountering aLeft
-- basically, everything that today ismir::ConstantKind::Value(_)
exceptConstValue::Indirect
becomesmir::ConstantKind::Ty(ty::ConstKind::Value(_))
. We remap the "efficiently represented"mir::ConstantKind::Value
to be valtrees instead.We'd avoid the ambiguity of representing something as a
mir::ConstantKind::Value(ConstValue::Scalar(s))
vsmir::ConstantKind::Ty(ty::ConstKind::Value(ValTree::Leaf(s)))
. We'd reduce the number of possible representations of a compile-time known value down to two: "efficient and abstract" vs "low-level". Generally values of any type can appear in either representation (if they can be valtree'd), except pretty much everyone will assume that scalars are represented efficiently. Pattern matching could much more directly useEither<ValTree, AllocId>
, and its use of valtrees forstr
wouldn't be a performance hazard any more.Maybe we'll want to give
Either<ValTree, AllocId>
a name, and maybe that name ismir::ConstantValue
-- but that would still be quite different from the currentConstValue
type. If the type exists it can have methods like "normalize to valtree if possible" (that's what pattern matching would want), "normalize to valtree if cheap" (i.e., only for scalars; that's what most consumers would want), "normalize to AllocId unless valtree is trivial" (that's what the interpreter / codegen would want -- a "trivial" valtree is a leaf, empty branch, or the efficient str/byte representation; crucially for "valtree if cheap"-normalized constants this is a NOP).@rust-lang/wg-const-eval please let me know if this makes any sense. :)
The text was updated successfully, but these errors were encountered: