Skip to content

Commit c6dea5f

Browse files
committed
copy elision - fn returning result of other fn
```zig fn bar() Foo { return foo(); } ``` ```llvm define internal fastcc void @bar(%Foo* nonnull sret) unnamed_addr #2 !dbg !55 { Entry: call fastcc void @foo(%Foo* sret %0), !dbg !60 ret void, !dbg !62 } ```
1 parent c7c3cfc commit c6dea5f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/codegen.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,8 +2242,12 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns
22422242
ZigType *return_type = return_instruction->value->value.type;
22432243

22442244
if (want_first_arg_sret(g, &g->cur_fn->type_entry->data.fn.fn_type_id)) {
2245-
assert(g->cur_ret_ptr);
2246-
gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
2245+
// Assume that the result location mechanism populated the value,
2246+
// unless the value is a comptime const.
2247+
if (return_instruction->value->value.special == ConstValSpecialStatic) {
2248+
assert(g->cur_ret_ptr);
2249+
gen_assign_raw(g, g->cur_ret_ptr, get_pointer_to_type(g, return_type, false), value);
2250+
}
22472251
LLVMBuildRetVoid(g->builder);
22482252
} else if (handle_is_ptr(return_type)) {
22492253
LLVMValueRef by_val_value = gen_load_untyped(g, value, 0, false, "");
@@ -5087,6 +5091,13 @@ static LLVMValueRef ir_render_sqrt(CodeGen *g, IrExecutable *executable, IrInstr
50875091
return LLVMBuildCall(g->builder, fn_val, &op, 1, "");
50885092
}
50895093

5094+
static LLVMValueRef ir_render_result_return(CodeGen *g, IrExecutable *executable,
5095+
IrInstructionResultReturn *instruction)
5096+
{
5097+
assert(g->cur_ret_ptr != nullptr);
5098+
return g->cur_ret_ptr;
5099+
}
5100+
50905101
static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
50915102
AstNode *source_node = instruction->source_node;
50925103
Scope *scope = instruction->scope;
@@ -5326,9 +5337,9 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
53265337
return ir_render_mark_err_ret_trace_ptr(g, executable, (IrInstructionMarkErrRetTracePtr *)instruction);
53275338
case IrInstructionIdSqrt:
53285339
return ir_render_sqrt(g, executable, (IrInstructionSqrt *)instruction);
5329-
case IrInstructionIdResultErrorUnionPayload:
5330-
zig_panic("TODO");
53315340
case IrInstructionIdResultReturn:
5341+
return ir_render_result_return(g, executable, (IrInstructionResultReturn *)instruction);
5342+
case IrInstructionIdResultErrorUnionPayload:
53325343
zig_panic("TODO");
53335344
case IrInstructionIdResultSliceToBytes:
53345345
zig_panic("TODO");

0 commit comments

Comments
 (0)