From 461382ae941e235ad75a6b3d00e05e5369baa98a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 31 May 2019 01:08:16 -0400 Subject: [PATCH] no-copy semantics for function call init var and literal ```zig export fn entry() void { var x = foo(); } const Foo = struct { x: i32, }; fn foo() Foo { return Foo{ .x = 1234, }; } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %x = alloca %Foo, align 4 call fastcc void @foo(%Foo* sret %x), !dbg !45 call void @llvm.dbg.declare(metadata %Foo* %x, metadata !39, metadata !DIExpression()), !dbg !46 ret void, !dbg !47 } define internal fastcc void @foo(%Foo* nonnull sret) unnamed_addr #2 !dbg !48 { Entry: %1 = bitcast %Foo* %0 to i8*, !dbg !52 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %1, i8* align 4 bitcast (%Foo* @0 to i8*), i64 4, i1 false), !dbg !52 ret void, !dbg !52 } ``` --- src/ir.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 9c45542b5b..f700404f29 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14365,11 +14365,11 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, Z assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc); IrInstructionAllocaSrc *alloca_src = reinterpret_cast(result_loc->source_instruction); - bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime && - result_loc_var->var->gen_is_const; if (alloca_src->base.child == nullptr) { uint32_t align = 0; // TODO bool force_comptime = false; // TODO + bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime && + result_loc_var->var->gen_is_const; IrInstruction *alloca_gen; if (is_comptime) { alloca_gen = ir_get_ref(ira, result_loc->source_instruction, value, true, false); @@ -14378,10 +14378,13 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, Z alloca_src->name_hint, force_comptime); } alloca_src->base.child = alloca_gen; + return is_comptime ? nullptr : alloca_src->base.child; } - return is_comptime ? nullptr : alloca_src->base.child; + return nullptr; } case ResultLocIdReturn: { + bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime; + if (is_comptime) return nullptr; ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false); return ir_build_return_ptr(ira, result_loc->source_instruction, ptr_return_type); }