Split evaluation up into one function per instruction kind (#5008)

Replace the large and growing `TryEvalInstInContext` function with one
function per kind. While we still have special-case handling for a small
number of instruction kinds, most instructions are now handled either
fully automatically or use a common codepath that evaluates the
instruction operands and then performs an eval-context-independent
evaluation of the instruction.

To support this, `InstConstantKind` is expanded to describe more
fine-grained details about how each kind of instruction interacts with
constant evaluation. Also, the operand kinds of instructions become
slightly more fine-grained: we now distinguish between operands that
describe the destination of an initializing expression (`DestInstId`)
from other `InstId` operands, because `DestInstId` operands need
different treatment during constant evaluation. In particular, an
initializing expression can have a constant value even if its
destination is non-constant or has not yet been set, because evaluation
of an initializing expression doesn't include the store to the
destination.

Some minor test changes:

- We now more consistently propagate errors into the results of constant
evaluation, so more instructions that depend on errors have a constant
value of `<error>`.
- Diagnostic location for invalid array types now point at the whole
array type rather than the array index expression, because
`EvalConstantinst` doesn't have access to the original expression.
- Diagnostic for failed `RequireCompleteType` doesn't print the original
type any more because `EvalConstantInst` doesn't have access to the
original expression.

As a follow-up, some of this -- in particular, the `EvalConstantInst`
overloads -- will be moved to a separate file, in an effort to split the
overall constant evaluation machinery apart from the logic to evaluate
each individual kind of instruction.
This commit is contained in:
Richard Smith
2025-02-27 01:31:26 +00:00
committed by GitHub
parent 90b6f5a22c
commit 0d2f364f39
50 changed files with 1050 additions and 929 deletions
+3 -1
View File
@@ -72,7 +72,9 @@ static auto LowerInstHelper(FunctionContext& context, SemIR::InstId inst_id,
"instruction in lowered contexts. Instruction: {0}",
inst);
} else if constexpr (InstT::Kind.constant_kind() ==
SemIR::InstConstantKind::Always) {
SemIR::InstConstantKind::Always ||
InstT::Kind.constant_kind() ==
SemIR::InstConstantKind::Unique) {
CARBON_FATAL("Missing constant value for constant instruction {0}", inst);
} else if constexpr (InstT::Kind.is_type() == SemIR::InstIsType::Always) {
// For instructions that are always of type `type`, produce the trivial