Skip to content

Commit 3037be1

Browse files
committed
copy elision: test case: scalar variable declaration
```zig export fn entry() void { var x: i32 = 1234; } ``` ```llvm define void @entry() #2 !dbg !41 { Entry: %x = alloca i32, align 4 store i32 1234, i32* %x, align 4, !dbg !48 call void @llvm.dbg.declare(metadata i32* %x, metadata !45, metadata !DIExpression()), !dbg !48 ret void, !dbg !49 } ```
1 parent 1bf72c6 commit 3037be1

File tree

4 files changed

+125
-62
lines changed

4 files changed

+125
-62
lines changed

src/all_types.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,7 @@ struct IrInstructionDeclVarGen {
22152215
IrInstruction base;
22162216

22172217
ZigVar *var;
2218+
IrInstruction *var_ptr;
22182219
};
22192220

22202221
struct IrInstructionCondBr {
@@ -3324,7 +3325,8 @@ struct IrInstructionResultPtrCast {
33243325
struct IrInstructionAlloca {
33253326
IrInstruction base;
33263327

3327-
IrInstruction *ty;
3328+
IrInstruction *child_type;
3329+
const char *name_hint;
33283330
};
33293331

33303332
static const size_t slice_ptr_index = 0;

src/codegen.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -1844,12 +1844,12 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
18441844
return nullptr;
18451845
}
18461846

1847-
static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {
1847+
static void gen_var_debug_decl(CodeGen *g, ZigVar *var, LLVMValueRef value_ref) {
18481848
assert(var->di_loc_var != nullptr);
18491849
AstNode *source_node = var->decl_node;
18501850
ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc((unsigned)source_node->line + 1,
18511851
(unsigned)source_node->column + 1, get_di_scope(g, var->parent_scope));
1852-
ZigLLVMInsertDeclareAtEnd(g->dbuilder, var->value_ref, var->di_loc_var, debug_loc,
1852+
ZigLLVMInsertDeclareAtEnd(g->dbuilder, value_ref, var->di_loc_var, debug_loc,
18531853
LLVMGetInsertBlock(g->builder));
18541854
}
18551855

@@ -2000,7 +2000,7 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20002000
clear_debug_source_node(g);
20012001
gen_store_untyped(g, LLVMGetParam(llvm_fn, fn_walk->data.inits.gen_i), var->value_ref, var->align_bytes, false);
20022002
if (var->decl_node) {
2003-
gen_var_debug_decl(g, var);
2003+
gen_var_debug_decl(g, var, var->value_ref);
20042004
}
20052005
fn_walk->data.inits.gen_i += 1;
20062006
break;
@@ -2035,7 +2035,7 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20352035
}
20362036
case FnWalkIdInits:
20372037
if (var->decl_node) {
2038-
gen_var_debug_decl(g, var);
2038+
gen_var_debug_decl(g, var, var->value_ref);
20392039
}
20402040
fn_walk->data.inits.gen_i += 1;
20412041
break;
@@ -2073,7 +2073,7 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20732073
}
20742074
case FnWalkIdInits:
20752075
if (var->decl_node) {
2076-
gen_var_debug_decl(g, var);
2076+
gen_var_debug_decl(g, var, var->value_ref);
20772077
}
20782078
fn_walk->data.inits.gen_i += 1;
20792079
break;
@@ -2111,7 +2111,7 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
21112111
LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, var->value_ref, ptr_to_int_type_ref, "");
21122112
gen_store_untyped(g, arg, bitcasted, var->align_bytes, false);
21132113
if (var->decl_node) {
2114-
gen_var_debug_decl(g, var);
2114+
gen_var_debug_decl(g, var, var->value_ref);
21152115
}
21162116
fn_walk->data.inits.gen_i += 1;
21172117
break;
@@ -2206,7 +2206,7 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {
22062206
}
22072207

22082208
if (variable->decl_node) {
2209-
gen_var_debug_decl(g, variable);
2209+
gen_var_debug_decl(g, variable, variable->value_ref);
22102210
}
22112211
break;
22122212
}
@@ -3175,18 +3175,16 @@ static LLVMValueRef ir_render_bool_not(CodeGen *g, IrExecutable *executable, IrI
31753175
return LLVMBuildICmp(g->builder, LLVMIntEQ, value, zero, "");
31763176
}
31773177

3178-
static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
3179-
IrInstructionDeclVarGen *decl_var_instruction)
3180-
{
3181-
ZigVar *var = decl_var_instruction->var;
3178+
static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable, IrInstructionDeclVarGen *instruction) {
3179+
ZigVar *var = instruction->var;
31823180

31833181
if (!type_has_bits(var->value->type))
31843182
return nullptr;
31853183

31863184
if (var->ref_count == 0 && g->build_mode != BuildModeDebug)
31873185
return nullptr;
31883186

3189-
gen_var_debug_decl(g, var);
3187+
gen_var_debug_decl(g, var, ir_llvm_value(g, instruction->var_ptr));
31903188
return nullptr;
31913189
}
31923190

@@ -5304,7 +5302,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
53045302
case IrInstructionIdStoreResult:
53055303
zig_panic("TODO");
53065304
case IrInstructionIdAlloca:
5307-
zig_panic("TODO");
5305+
return instruction->llvm_value; // handled before function code generation
53085306
}
53095307
zig_unreachable();
53105308
}
@@ -6194,8 +6192,11 @@ static void do_code_gen(CodeGen *g) {
61946192
// allocate temporary stack data
61956193
for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_list.length; alloca_i += 1) {
61966194
IrInstructionAlloca *instruction = fn_table_entry->alloca_list.at(alloca_i);
6197-
ZigType *slot_type = instruction->base.value.type;
6198-
instruction->base.llvm_value = build_alloca(g, slot_type, "", get_abi_alignment(g, slot_type));
6195+
ZigType *ptr_type = instruction->base.value.type;
6196+
assert(ptr_type->id == ZigTypeIdPointer);
6197+
ZigType *child_type = ptr_type->data.pointer.child_type;
6198+
instruction->base.llvm_value = build_alloca(g, child_type, instruction->name_hint,
6199+
get_ptr_align(g, ptr_type));
61996200
}
62006201

62016202
ImportTableEntry *import = get_scope_import(&fn_table_entry->fndef_scope->base);
@@ -6221,8 +6222,6 @@ static void do_code_gen(CodeGen *g) {
62216222
continue;
62226223

62236224
if (var->src_arg_index == SIZE_MAX) {
6224-
var->value_ref = build_alloca(g, var->value->type, buf_ptr(&var->name), var->align_bytes);
6225-
62266225
var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
62276226
buf_ptr(&var->name), import->di_file, (unsigned)(var->decl_node->line + 1),
62286227
var->value->type->di_type, !g->strip_debug_symbols, 0);

0 commit comments

Comments
 (0)