From 965879a9a94b1d552f22b2ca5741bd6fd24f83fd Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 25 Mar 2026 09:03:44 -0700 Subject: [PATCH] Support for in-place return in `eval fn`. (#6954) Ignore storage arguments when evaluating a call, like we do for other kinds of instruction. Create a placeholder constant to represent each out parameter so that it can be used in the function body to form more storage arguments. Assisted-by: Gemini 3.1 Pro via Antigravity --- toolchain/check/eval.cpp | 87 +++++- toolchain/check/testdata/eval/call.carbon | 321 ++++++++++++++++++++++ toolchain/lower/constant.cpp | 13 +- toolchain/lower/testdata/var/local.carbon | 99 ++++++- 4 files changed, 499 insertions(+), 21 deletions(-) diff --git a/toolchain/check/eval.cpp b/toolchain/check/eval.cpp index 46aa02fd9dac..706747fca221 100644 --- a/toolchain/check/eval.cpp +++ b/toolchain/check/eval.cpp @@ -588,24 +588,29 @@ static auto GetConstantValue(EvalContext& eval_context, Phase* phase) -> SemIR::InstBlockId = delete; // If the given instruction block contains only constants, returns a -// corresponding block of those values. -static auto GetConstantValue(EvalContext& eval_context, - SemIR::InstBlockId inst_block_id, Phase* phase) - -> SemIR::InstBlockId { +// corresponding block of those values. Ignores the instructions in the +// specified range of indexes, replacing those elements with `None`. +static auto GetConstantBlockValueIgnoringIndexRange( + EvalContext& eval_context, SemIR::InstBlockId inst_block_id, Phase* phase, + std::pair ignored_range) -> SemIR::InstBlockId { if (!inst_block_id.has_value()) { return SemIR::InstBlockId::None; } auto insts = eval_context.inst_blocks().Get(inst_block_id); llvm::SmallVector const_insts; for (auto inst_id : insts) { - auto const_inst_id = GetConstantValue(eval_context, inst_id, phase); - if (!const_inst_id.has_value()) { - return SemIR::InstBlockId::None; + auto const_inst_id = SemIR::InstId::None; + if (static_cast(const_insts.size()) < ignored_range.first || + static_cast(const_insts.size()) >= ignored_range.second) { + const_inst_id = GetConstantValue(eval_context, inst_id, phase); + if (!const_inst_id.has_value()) { + return SemIR::InstBlockId::None; + } } // Once we leave the small buffer, we know the first few elements are all - // constant, so it's likely that the entire block is constant. Resize to the - // target size given that we're going to allocate memory now anyway. + // constant, so it's likely that the entire block is constant. Resize to + // the target size given that we're going to allocate memory now anyway. if (const_insts.size() == const_insts.capacity()) { const_insts.reserve(insts.size()); } @@ -617,6 +622,15 @@ static auto GetConstantValue(EvalContext& eval_context, return eval_context.inst_blocks().AddCanonical(const_insts); } +// If the given instruction block contains only constants, returns a +// corresponding block of those values. +static auto GetConstantValue(EvalContext& eval_context, + SemIR::InstBlockId inst_block_id, Phase* phase) + -> SemIR::InstBlockId { + return GetConstantBlockValueIgnoringIndexRange(eval_context, inst_block_id, + phase, {0, 0}); +} + // Compute the constant value of a type block. This may be different from the // input type block if we have known generic arguments. static auto GetConstantValue(EvalContext& eval_context, @@ -2088,6 +2102,39 @@ static auto TryEvalCall(EvalContext& outer_eval_context, SemIR::LocId loc_id, SemIR::SpecificId specific_id, SemIR::InstBlockId args_id) -> SemIR::ConstantId; +// Returns the range of parameter indexes that contain the return storage for +// this function call. +static auto GetReturnStorageParamIndexRange(EvalContext& eval_context, + const SemIR::Callee& callee) + -> std::pair { + if (const auto* callee_function = + std::get_if(&callee)) { + const auto& function = + eval_context.functions().Get(callee_function->function_id); + return {function.call_param_ranges.return_begin().index, + function.call_param_ranges.return_end().index}; + } + + return {0, 0}; +} + +// Replace the `args_id` field of a call with its constant value. The return +// storage argument, if any, is instead replaced with `None`. +static auto ReplaceCallArgsFieldWithConstantValue(EvalContext& eval_context, + const SemIR::Callee& callee, + SemIR::Call* call, + Phase* phase) -> bool { + auto return_storage_param_index_range = + GetReturnStorageParamIndexRange(eval_context, callee); + auto args_id = GetConstantBlockValueIgnoringIndexRange( + eval_context, call->args_id, phase, return_storage_param_index_range); + if (!args_id.has_value() && call->args_id.has_value()) { + return false; + } + call->args_id = args_id; + return IsConstantOrError(*phase); +} + // Makes a constant for a call instruction. static auto MakeConstantForCall(EvalContext& eval_context, SemIR::InstId inst_id, SemIR::Call call) @@ -2129,8 +2176,8 @@ static auto MakeConstantForCall(EvalContext& eval_context, bool has_constant_operands = has_constant_callee && ReplaceTypeWithConstantValue(eval_context, inst_id, &call, &phase) && - ReplaceFieldWithConstantValue(eval_context, &call, &SemIR::Call::args_id, - &phase); + ReplaceCallArgsFieldWithConstantValue(eval_context, callee, &call, + &phase); if (phase == Phase::UnknownDueToError) { return SemIR::ErrorInst::ConstantId; } @@ -2837,6 +2884,24 @@ auto TryExecTypedInst(FunctionExecContext& eval_context, return SemIR::ConstantId::None; } + if (!eval_context.args()[param.index.index].has_value()) { + // The argument will be `None` for an index corresponding to a return + // storage argument for return values that have an in-place initializing + // representation. Produce an opaque "out parameter" variable for now, so + // that references to it can still successfully evaluate. + // + // TODO: Create and track mutable storage for the return value here. This is + // necessary to support things like `returned var`. + eval_context.locals().Insert( + inst_id, + MakeConstantResult( + eval_context.context(), + SemIR::VarStorage{.type_id = inst.type_id(), + .pattern_id = SemIR::AbsoluteInstId::None}, + Phase::Concrete)); + return SemIR::ConstantId::None; + } + return TryExecTypedParam(eval_context, inst_id, inst); } diff --git a/toolchain/check/testdata/eval/call.carbon b/toolchain/check/testdata/eval/call.carbon index 73d2d9e388de..ee06ae42e985 100644 --- a/toolchain/check/testdata/eval/call.carbon +++ b/toolchain/check/testdata/eval/call.carbon @@ -109,3 +109,324 @@ fn UseFGenerically(X:! i32) { fn UseFSpecifically() { UseFGenerically(3); } + +// --- return_in_place_discarded.carbon + +library "[[@TEST_NAME]]"; + +eval fn F() -> (i32, i32, i32) { return (1, 2, 3); } + +fn G() { + //@dump-sem-ir-begin + F(); + //@dump-sem-ir-end +} + +// --- return_in_place_init.carbon + +library "[[@TEST_NAME]]"; + +eval fn F() -> (i32, i32, i32) { return (1, 2, 3); } + +fn G() { + //@dump-sem-ir-begin + var unused a: (i32, i32, i32) = F(); + //@dump-sem-ir-end +} + +// --- fail_todo_return_in_place_tuple.carbon + +library "[[@TEST_NAME]]"; + +//@dump-sem-ir-begin +eval fn F() -> (i32, i32, i32) { return (1, 2, 3); } +//@dump-sem-ir-end + +eval fn G(v: (i32, i32, i32)) -> i32 { return v.1; } + +fn H() { + // TODO: The call to the function is a constant, but the `temporary` instruction wrapping it is not. + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_return_in_place_tuple.carbon:[[@LINE+4]]:28: error: array bound is not a constant [InvalidArrayExpr] + // CHECK:STDERR: var unused a: array(i32, G(F())) = (1, 2); + // CHECK:STDERR: ^~~~~~ + // CHECK:STDERR: + var unused a: array(i32, G(F())) = (1, 2); + //@dump-sem-ir-end +} + +// --- fail_todo_return_in_place_class.carbon + +library "[[@TEST_NAME]]"; + +class C {} +//@dump-sem-ir-begin +eval fn F() -> C { return {}; } +//@dump-sem-ir-end + +fn G(_:! C) {} + +fn H() { + // TODO: The call to the function is a constant, but the `temporary` instruction wrapping it is not. + //@dump-sem-ir-begin + // CHECK:STDERR: fail_todo_return_in_place_class.carbon:[[@LINE+7]]:3: error: argument for generic parameter is not a compile-time constant [CompTimeArgumentNotConstant] + // CHECK:STDERR: G(F()); + // CHECK:STDERR: ^~~~~~ + // CHECK:STDERR: fail_todo_return_in_place_class.carbon:[[@LINE-8]]:6: note: initializing generic parameter `_` declared here [InitializingGenericParam] + // CHECK:STDERR: fn G(_:! C) {} + // CHECK:STDERR: ^ + // CHECK:STDERR: + G(F()); + //@dump-sem-ir-end +} + +// CHECK:STDOUT: --- return_in_place_discarded.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] +// CHECK:STDOUT: %tuple.type.189: type = tuple_type (%i32, %i32, %i32) [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete] +// CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete] +// CHECK:STDOUT: %int_3.822: %i32 = int_value 3 [concrete] +// CHECK:STDOUT: %tuple.ee6: %tuple.type.189 = tuple_value (%int_1.5d2, %int_2.ef8, %int_3.822) [concrete] +// CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete] +// CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @G() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] +// CHECK:STDOUT: %.loc8_5.1: ref %tuple.type.189 = temporary_storage +// CHECK:STDOUT: %F.call: init %tuple.type.189 to %.loc8_5.1 = call %F.ref() [concrete = constants.%tuple.ee6] +// CHECK:STDOUT: %.loc8_5.2: ref %tuple.type.189 = temporary %.loc8_5.1, %F.call +// CHECK:STDOUT: %Destroy.Op.bound: = bound_method %.loc8_5.2, constants.%Destroy.Op +// CHECK:STDOUT: %Destroy.Op.call: init %empty_tuple.type = call %Destroy.Op.bound(%.loc8_5.2) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %tuple.type.189) = "no_op"; +// CHECK:STDOUT: +// CHECK:STDOUT: --- return_in_place_init.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] +// CHECK:STDOUT: %tuple.type.ff9: type = tuple_type (type, type, type) [concrete] +// CHECK:STDOUT: %tuple.e64: %tuple.type.ff9 = tuple_value (%i32, %i32, %i32) [concrete] +// CHECK:STDOUT: %tuple.type.189: type = tuple_type (%i32, %i32, %i32) [concrete] +// CHECK:STDOUT: %pattern_type.b5a: type = pattern_type %tuple.type.189 [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete] +// CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete] +// CHECK:STDOUT: %int_3.822: %i32 = int_value 3 [concrete] +// CHECK:STDOUT: %tuple.ee6: %tuple.type.189 = tuple_value (%int_1.5d2, %int_2.ef8, %int_3.822) [concrete] +// CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete] +// CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @G() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %a.patt: %pattern_type.b5a = ref_binding_pattern a [concrete] +// CHECK:STDOUT: %a.var_patt: %pattern_type.b5a = var_pattern %a.patt [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %a.var: ref %tuple.type.189 = var %a.var_patt +// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] +// CHECK:STDOUT: %.loc8_3: ref %tuple.type.189 = splice_block %a.var {} +// CHECK:STDOUT: %F.call: init %tuple.type.189 to %.loc8_3 = call %F.ref() [concrete = constants.%tuple.ee6] +// CHECK:STDOUT: assign %a.var, %F.call +// CHECK:STDOUT: %.loc8_31.1: type = splice_block %.loc8_31.3 [concrete = constants.%tuple.type.189] { +// CHECK:STDOUT: %i32.loc8_18: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %i32.loc8_23: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %i32.loc8_28: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %.loc8_31.2: %tuple.type.ff9 = tuple_literal (%i32.loc8_18, %i32.loc8_23, %i32.loc8_28) [concrete = constants.%tuple.e64] +// CHECK:STDOUT: %.loc8_31.3: type = converted %.loc8_31.2, constants.%tuple.type.189 [concrete = constants.%tuple.type.189] +// CHECK:STDOUT: } +// CHECK:STDOUT: %a: ref %tuple.type.189 = ref_binding a, %a.var +// CHECK:STDOUT: %Destroy.Op.bound: = bound_method %a.var, constants.%Destroy.Op +// CHECK:STDOUT: %Destroy.Op.call: init %empty_tuple.type = call %Destroy.Op.bound(%a.var) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %tuple.type.189) = "no_op"; +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_return_in_place_tuple.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] +// CHECK:STDOUT: %tuple.type.ff9: type = tuple_type (type, type, type) [concrete] +// CHECK:STDOUT: %tuple.e64: %tuple.type.ff9 = tuple_value (%i32, %i32, %i32) [concrete] +// CHECK:STDOUT: %tuple.type.189: type = tuple_type (%i32, %i32, %i32) [concrete] +// CHECK:STDOUT: %.074: Core.Form = init_form %tuple.type.189 [concrete] +// CHECK:STDOUT: %pattern_type.b5a: type = pattern_type %tuple.type.189 [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete] +// CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete] +// CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete] +// CHECK:STDOUT: %tuple.type.37f: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [concrete] +// CHECK:STDOUT: %tuple.2d5: %tuple.type.37f = tuple_value (%int_1.5b8, %int_2.ecc, %int_3.1ba) [concrete] +// CHECK:STDOUT: %ImplicitAs.type.e8c: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete] +// CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6 = struct_value () [symbolic] +// CHECK:STDOUT: %ImplicitAs.impl_witness.6bc: = impl_witness imports.%ImplicitAs.impl_witness_table.74f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d = struct_value () [concrete] +// CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.6bc) [concrete] +// CHECK:STDOUT: %ImplicitAs.WithSelf.Convert.type.b37: type = fn_type @ImplicitAs.WithSelf.Convert, @ImplicitAs.WithSelf(%i32, %ImplicitAs.facet) [concrete] +// CHECK:STDOUT: %.545: type = fn_type_with_self_type %ImplicitAs.WithSelf.Convert.type.b37, %ImplicitAs.facet [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.215: = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete] +// CHECK:STDOUT: %bound_method.38b: = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete] +// CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.4e5: = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete] +// CHECK:STDOUT: %bound_method.646: = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete] +// CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.061: = bound_method %int_3.1ba, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete] +// CHECK:STDOUT: %bound_method.fa7: = bound_method %int_3.1ba, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete] +// CHECK:STDOUT: %int_3.822: %i32 = int_value 3 [concrete] +// CHECK:STDOUT: %tuple.ee6: %tuple.type.189 = tuple_value (%int_1.5d2, %int_2.ef8, %int_3.822) [concrete] +// CHECK:STDOUT: %tuple.type.f94: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [concrete] +// CHECK:STDOUT: %tuple.ad8: %tuple.type.f94 = tuple_value (%int_1.5b8, %int_2.ecc) [concrete] +// CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete] +// CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Core.import_ref.42d: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2)] +// CHECK:STDOUT: %ImplicitAs.impl_witness_table.74f = impl_witness_table (%Core.import_ref.42d), @Core.IntLiteral.as.ImplicitAs.impl [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { +// CHECK:STDOUT: %return.param_patt: %pattern_type.b5a = out_param_pattern [concrete] +// CHECK:STDOUT: %return.patt: %pattern_type.b5a = return_slot_pattern %return.param_patt, %.loc5_30.2 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: %i32.loc5_17: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %i32.loc5_22: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %i32.loc5_27: type = type_literal constants.%i32 [concrete = constants.%i32] +// CHECK:STDOUT: %.loc5_30.1: %tuple.type.ff9 = tuple_literal (%i32.loc5_17, %i32.loc5_22, %i32.loc5_27) [concrete = constants.%tuple.e64] +// CHECK:STDOUT: %.loc5_30.2: type = converted %.loc5_30.1, constants.%tuple.type.189 [concrete = constants.%tuple.type.189] +// CHECK:STDOUT: %.loc5_30.3: Core.Form = init_form %.loc5_30.2 [concrete = constants.%.074] +// CHECK:STDOUT: %return.param: ref %tuple.type.189 = out_param call_param0 +// CHECK:STDOUT: %return: ref %tuple.type.189 = return_slot %return.param +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F() -> out %return.param: %tuple.type.189 { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8] +// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc] +// CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba] +// CHECK:STDOUT: %.loc5_49.1: %tuple.type.37f = tuple_literal (%int_1, %int_2, %int_3) [concrete = constants.%tuple.2d5] +// CHECK:STDOUT: %impl.elem0.loc5_49.1: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5] +// CHECK:STDOUT: %bound_method.loc5_49.1: = bound_method %int_1, %impl.elem0.loc5_49.1 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.215] +// CHECK:STDOUT: %specific_fn.loc5_49.1: = specific_function %impl.elem0.loc5_49.1, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc5_49.2: = bound_method %int_1, %specific_fn.loc5_49.1 [concrete = constants.%bound_method.38b] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.1: init %i32 = call %bound_method.loc5_49.2(%int_1) [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %.loc5_49.2: init %i32 = converted %int_1, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.1 [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %tuple.elem0: ref %i32 = tuple_access %return.param, element0 +// CHECK:STDOUT: %.loc5_49.3: init %i32 to %tuple.elem0 = in_place_init %.loc5_49.2 [concrete = constants.%int_1.5d2] +// CHECK:STDOUT: %impl.elem0.loc5_49.2: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5] +// CHECK:STDOUT: %bound_method.loc5_49.3: = bound_method %int_2, %impl.elem0.loc5_49.2 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.4e5] +// CHECK:STDOUT: %specific_fn.loc5_49.2: = specific_function %impl.elem0.loc5_49.2, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc5_49.4: = bound_method %int_2, %specific_fn.loc5_49.2 [concrete = constants.%bound_method.646] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.2: init %i32 = call %bound_method.loc5_49.4(%int_2) [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %.loc5_49.4: init %i32 = converted %int_2, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.2 [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %tuple.elem1: ref %i32 = tuple_access %return.param, element1 +// CHECK:STDOUT: %.loc5_49.5: init %i32 to %tuple.elem1 = in_place_init %.loc5_49.4 [concrete = constants.%int_2.ef8] +// CHECK:STDOUT: %impl.elem0.loc5_49.3: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5] +// CHECK:STDOUT: %bound_method.loc5_49.5: = bound_method %int_3, %impl.elem0.loc5_49.3 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.061] +// CHECK:STDOUT: %specific_fn.loc5_49.3: = specific_function %impl.elem0.loc5_49.3, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn] +// CHECK:STDOUT: %bound_method.loc5_49.6: = bound_method %int_3, %specific_fn.loc5_49.3 [concrete = constants.%bound_method.fa7] +// CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.3: init %i32 = call %bound_method.loc5_49.6(%int_3) [concrete = constants.%int_3.822] +// CHECK:STDOUT: %.loc5_49.6: init %i32 = converted %int_3, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc5_49.3 [concrete = constants.%int_3.822] +// CHECK:STDOUT: %tuple.elem2: ref %i32 = tuple_access %return.param, element2 +// CHECK:STDOUT: %.loc5_49.7: init %i32 to %tuple.elem2 = in_place_init %.loc5_49.6 [concrete = constants.%int_3.822] +// CHECK:STDOUT: %.loc5_49.8: init %tuple.type.189 to %return.param = tuple_init (%.loc5_49.3, %.loc5_49.5, %.loc5_49.7) [concrete = constants.%tuple.ee6] +// CHECK:STDOUT: %.loc5_50: init %tuple.type.189 = converted %.loc5_49.1, %.loc5_49.8 [concrete = constants.%tuple.ee6] +// CHECK:STDOUT: return %.loc5_50 to %return.param +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @H() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %a.patt: = ref_binding_pattern a [concrete] +// CHECK:STDOUT: %a.var_patt: = var_pattern %a.patt [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %a.var: ref = var %a.var_patt [concrete = ] +// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8] +// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc] +// CHECK:STDOUT: %.loc17_43: %tuple.type.f94 = tuple_literal (%int_1, %int_2) [concrete = constants.%tuple.ad8] +// CHECK:STDOUT: assign %a.var, +// CHECK:STDOUT: +// CHECK:STDOUT: %a: ref = ref_binding a, [concrete = ] +// CHECK:STDOUT: %Destroy.Op.bound: = bound_method %.loc17_32.2, constants.%Destroy.Op +// CHECK:STDOUT: %Destroy.Op.call: init %empty_tuple.type = call %Destroy.Op.bound(%.loc17_32.2) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %tuple.type.189) = "no_op"; +// CHECK:STDOUT: +// CHECK:STDOUT: --- fail_todo_return_in_place_class.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %C: type = class_type @C [concrete] +// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete] +// CHECK:STDOUT: %.a69: Core.Form = init_form %C [concrete] +// CHECK:STDOUT: %pattern_type.7c7: type = pattern_type %C [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete] +// CHECK:STDOUT: %C.val: %C = struct_value () [concrete] +// CHECK:STDOUT: %G.type: type = fn_type @G [concrete] +// CHECK:STDOUT: %G: %G.type = struct_value () [concrete] +// CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete] +// CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { +// CHECK:STDOUT: %return.param_patt: %pattern_type.7c7 = out_param_pattern [concrete] +// CHECK:STDOUT: %return.patt: %pattern_type.7c7 = return_slot_pattern %return.param_patt, %C.ref [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C] +// CHECK:STDOUT: %.loc6_16: Core.Form = init_form %C.ref [concrete = constants.%.a69] +// CHECK:STDOUT: %return.param: ref %C = out_param call_param0 +// CHECK:STDOUT: %return: ref %C = return_slot %return.param +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F() -> out %return.param: %C { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc6_28.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct] +// CHECK:STDOUT: %.loc6_28.2: init %C to %return.param = class_init () [concrete = constants.%C.val] +// CHECK:STDOUT: %.loc6_29: init %C = converted %.loc6_28.1, %.loc6_28.2 [concrete = constants.%C.val] +// CHECK:STDOUT: return %.loc6_29 to %return.param +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @H() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G] +// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] +// CHECK:STDOUT: %.loc21_7.1: ref %C = temporary_storage +// CHECK:STDOUT: %F.call: init %C to %.loc21_7.1 = call %F.ref() [concrete = constants.%C.val] +// CHECK:STDOUT: %.loc21_7.2: ref %C = temporary %.loc21_7.1, %F.call +// CHECK:STDOUT: %.loc21_7.3: %C = acquire_value %.loc21_7.2 +// CHECK:STDOUT: %Destroy.Op.bound: = bound_method %.loc21_7.2, constants.%Destroy.Op +// CHECK:STDOUT: %Destroy.Op.call: init %empty_tuple.type = call %Destroy.Op.bound(%.loc21_7.2) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %C) = "no_op"; +// CHECK:STDOUT: diff --git a/toolchain/lower/constant.cpp b/toolchain/lower/constant.cpp index 524cf0476599..1752e2b6142d 100644 --- a/toolchain/lower/constant.cpp +++ b/toolchain/lower/constant.cpp @@ -117,7 +117,11 @@ static auto EmitAggregateConstant(ConstantContext& context, llvm::SmallVector elements; elements.reserve(refs.size()); for (auto ref : refs) { - elements.push_back(context.GetConstant(ref)); + if (auto* constant = context.GetConstant(ref)) { + elements.push_back(constant); + } else { + return nullptr; + } } return ConstantType::get(llvm_type, elements); @@ -172,6 +176,9 @@ static auto EmitAsConstant(ConstantContext& context, SemIR::VtablePtr inst) static auto EmitAsConstant(ConstantContext& context, SemIR::AnyAggregateAccess inst) -> llvm::Constant* { auto* aggr_addr = context.GetConstant(inst.aggregate_id); + if (!aggr_addr) { + return nullptr; + } auto* aggr_type = context.GetType( context.sem_ir().insts().Get(inst.aggregate_id).type_id()); @@ -297,6 +304,10 @@ static auto EmitAsConstant(ConstantContext& context, static auto EmitAsConstant(ConstantContext& context, SemIR::VarStorage inst) -> llvm::Constant* { + if (!inst.pattern_id.has_value()) { + // This constant is a placeholder and should not be used by lowering. + return nullptr; + } // Create the corresponding global variable declaration. return context.BuildGlobalVariableDecl(inst); } diff --git a/toolchain/lower/testdata/var/local.carbon b/toolchain/lower/testdata/var/local.carbon index 11f6d5f5250f..d8b4502c0973 100644 --- a/toolchain/lower/testdata/var/local.carbon +++ b/toolchain/lower/testdata/var/local.carbon @@ -10,13 +10,28 @@ // TIP: To dump output, run: // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/var/local.carbon +// --- simple.carbon + +library "[[@TEST_NAME]]"; + fn Run() -> i32 { var x: i32 = 1; return x; } -// CHECK:STDOUT: ; ModuleID = 'local.carbon' -// CHECK:STDOUT: source_filename = "local.carbon" +// --- eval_init_in_place.carbon + +library "[[@TEST_NAME]]"; + +eval fn F() -> (i32, i32, i32) { return (1, 2, 3); } + +fn G() -> (i32, i32, i32) { + var a: (i32, i32, i32) = F(); + return a; +} + +// CHECK:STDOUT: ; ModuleID = 'simple.carbon' +// CHECK:STDOUT: source_filename = "simple.carbon" // CHECK:STDOUT: // CHECK:STDOUT: ; Function Attrs: nounwind // CHECK:STDOUT: define i32 @main() #0 !dbg !4 { @@ -24,8 +39,8 @@ fn Run() -> i32 { // CHECK:STDOUT: %x.var = alloca i32, align 4, !dbg !8 // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %x.var), !dbg !8 // CHECK:STDOUT: store i32 1, ptr %x.var, align 4, !dbg !8 -// CHECK:STDOUT: %.loc15 = load i32, ptr %x.var, align 4, !dbg !9 -// CHECK:STDOUT: ret i32 %.loc15, !dbg !10 +// CHECK:STDOUT: %.loc6 = load i32, ptr %x.var, align 4, !dbg !9 +// CHECK:STDOUT: ret i32 %.loc6, !dbg !10 // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) @@ -40,11 +55,77 @@ fn Run() -> i32 { // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5} // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3} // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) -// CHECK:STDOUT: !3 = !DIFile(filename: "local.carbon", directory: "") -// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !3 = !DIFile(filename: "simple.carbon", directory: "") +// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 4, type: !5, spFlags: DISPFlagDefinition, unit: !2) // CHECK:STDOUT: !5 = !DISubroutineType(types: !6) // CHECK:STDOUT: !6 = !{!7} // CHECK:STDOUT: !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) -// CHECK:STDOUT: !8 = !DILocation(line: 14, column: 3, scope: !4) -// CHECK:STDOUT: !9 = !DILocation(line: 15, column: 10, scope: !4) -// CHECK:STDOUT: !10 = !DILocation(line: 15, column: 3, scope: !4) +// CHECK:STDOUT: !8 = !DILocation(line: 5, column: 3, scope: !4) +// CHECK:STDOUT: !9 = !DILocation(line: 6, column: 10, scope: !4) +// CHECK:STDOUT: !10 = !DILocation(line: 6, column: 3, scope: !4) +// CHECK:STDOUT: ; ModuleID = 'eval_init_in_place.carbon' +// CHECK:STDOUT: source_filename = "eval_init_in_place.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: @tuple.ee6.loc4_50 = internal constant { i32, i32, i32 } { i32 1, i32 2, i32 3 } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_CF.Main(ptr sret({ i32, i32, i32 }) %return) #0 !dbg !4 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %tuple.elem0.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 0, !dbg !8 +// CHECK:STDOUT: %tuple.elem1.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 1, !dbg !8 +// CHECK:STDOUT: %tuple.elem2.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 2, !dbg !8 +// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %return, ptr align 4 @tuple.ee6.loc4_50, i64 12, i1 false), !dbg !9 +// CHECK:STDOUT: ret void, !dbg !9 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_CG.Main(ptr sret({ i32, i32, i32 }) %return) #0 !dbg !10 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %a.var = alloca { i32, i32, i32 }, align 8, !dbg !11 +// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %a.var), !dbg !11 +// CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %a.var, ptr align 4 @tuple.ee6.loc4_50, i64 12, i1 false), !dbg !11 +// CHECK:STDOUT: %tuple.elem0.loc8_10.1.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %a.var, i32 0, i32 0, !dbg !12 +// CHECK:STDOUT: %.loc8_10.1 = load i32, ptr %tuple.elem0.loc8_10.1.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: %tuple.elem0.loc8_10.2.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 0, !dbg !12 +// CHECK:STDOUT: store i32 %.loc8_10.1, ptr %tuple.elem0.loc8_10.2.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: %tuple.elem1.loc8_10.1.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %a.var, i32 0, i32 1, !dbg !12 +// CHECK:STDOUT: %.loc8_10.3 = load i32, ptr %tuple.elem1.loc8_10.1.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: %tuple.elem1.loc8_10.2.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 1, !dbg !12 +// CHECK:STDOUT: store i32 %.loc8_10.3, ptr %tuple.elem1.loc8_10.2.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: %tuple.elem2.loc8_10.1.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %a.var, i32 0, i32 2, !dbg !12 +// CHECK:STDOUT: %.loc8_10.5 = load i32, ptr %tuple.elem2.loc8_10.1.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: %tuple.elem2.loc8_10.2.tuple.elem = getelementptr inbounds nuw { i32, i32, i32 }, ptr %return, i32 0, i32 2, !dbg !12 +// CHECK:STDOUT: store i32 %.loc8_10.5, ptr %tuple.elem2.loc8_10.2.tuple.elem, align 4, !dbg !12 +// CHECK:STDOUT: ret void, !dbg !13 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite) +// CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #1 +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) +// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #2 +// CHECK:STDOUT: +// CHECK:STDOUT: ; uselistorder directives +// CHECK:STDOUT: uselistorder ptr @llvm.memcpy.p0.p0.i64, { 1, 0 } +// CHECK:STDOUT: +// CHECK:STDOUT: attributes #0 = { nounwind } +// CHECK:STDOUT: attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) } +// CHECK:STDOUT: attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) } +// CHECK:STDOUT: +// CHECK:STDOUT: !llvm.module.flags = !{!0, !1} +// CHECK:STDOUT: !llvm.dbg.cu = !{!2} +// CHECK:STDOUT: +// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5} +// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3} +// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) +// CHECK:STDOUT: !3 = !DIFile(filename: "eval_init_in_place.carbon", directory: "") +// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main", scope: null, file: !3, line: 4, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !5 = !DISubroutineType(types: !6) +// CHECK:STDOUT: !6 = !{!7} +// CHECK:STDOUT: !7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 8) +// CHECK:STDOUT: !8 = !DILocation(line: 4, column: 41, scope: !4) +// CHECK:STDOUT: !9 = !DILocation(line: 4, column: 34, scope: !4) +// CHECK:STDOUT: !10 = distinct !DISubprogram(name: "G", linkageName: "_CG.Main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2) +// CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !10) +// CHECK:STDOUT: !12 = !DILocation(line: 8, column: 10, scope: !10) +// CHECK:STDOUT: !13 = !DILocation(line: 8, column: 3, scope: !10)