From 77e84aba5c1a166009b498d577066e7b7d9ffb63 Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Sat, 6 Apr 2024 10:24:59 +0200 Subject: [PATCH] gallivm: Handle nir_instr_debug_info Emits llvm source locations using the nir line numbers provided by nir_instr_debug_info. Reviewed-By: Mike Blumenkrantz Part-of: --- src/gallium/auxiliary/draw/draw_llvm.c | 28 ++++++++++++++++++- .../auxiliary/gallivm/lp_bld_nir_soa.c | 23 ++++++++++++++- src/gallium/drivers/llvmpipe/lp_state_cs.c | 10 +++++++ src/gallium/drivers/llvmpipe/lp_state_fs.c | 5 ++++ .../llvmpipe/lp_state_fs_linear_llvm.c | 5 ++++ 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/gallium/auxiliary/draw/draw_llvm.c b/src/gallium/auxiliary/draw/draw_llvm.c index b1357d30536..00756292376 100644 --- a/src/gallium/auxiliary/draw/draw_llvm.c +++ b/src/gallium/auxiliary/draw/draw_llvm.c @@ -1683,6 +1683,11 @@ draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant) builder = gallivm->builder; LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + memset(&vs_type, 0, sizeof vs_type); vs_type.floating = true; /* floating point values */ vs_type.sign = true; /* values are signed */ @@ -2413,6 +2418,11 @@ draw_gs_llvm_generate(struct draw_llvm *llvm, builder = gallivm->builder; LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + lp_build_context_init(&bld, gallivm, lp_type_int(32)); memset(&gs_type, 0, sizeof gs_type); @@ -2963,7 +2973,6 @@ draw_tcs_llvm_generate(struct draw_llvm *llvm, } lp_function_add_debug_info(gallivm, variant_func, func_type); - lp_function_add_debug_info(gallivm, variant_coro, coro_func_type); if (gallivm->cache && gallivm->cache->data_size) { gallivm_stub_func(gallivm, variant_func); @@ -2989,6 +2998,11 @@ draw_tcs_llvm_generate(struct draw_llvm *llvm, builder = gallivm->builder; LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + lp_build_context_init(&bld, gallivm, lp_type_int(32)); memset(&tcs_type, 0, sizeof tcs_type); @@ -3056,9 +3070,16 @@ draw_tcs_llvm_generate(struct draw_llvm *llvm, NULL, LLVMIntEQ); LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32))); + lp_function_add_debug_info(gallivm, variant_coro, coro_func_type); + block = LLVMAppendBasicBlockInContext(gallivm->context, variant_coro, "entry"); LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + resources_ptr = LLVMGetParam(variant_coro, 0); input_array = LLVMGetParam(variant_coro, 1); output_array = LLVMGetParam(variant_coro, 2); @@ -3568,6 +3589,11 @@ draw_tes_llvm_generate(struct draw_llvm *llvm, builder = gallivm->builder; LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + lp_build_context_init(&bld, gallivm, lp_type_int(32)); memset(&tes_type, 0, sizeof tes_type); diff --git a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c index 8c6f762c0e3..1ceaed1955a 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c @@ -5547,10 +5547,19 @@ visit_call(struct lp_build_nir_soa_context *bld, static void visit_block(struct lp_build_nir_soa_context *bld, nir_block *block) { + struct gallivm_state *gallivm = bld->base.gallivm; + nir_foreach_instr(instr, block) { bld->instr = instr; + if (gallivm->di_builder && gallivm->file_name && instr->has_debug_info) { + nir_instr_debug_info *debug_info = nir_instr_get_debug_info(instr); + LLVMMetadataRef di_loc = LLVMDIBuilderCreateDebugLocation( + gallivm->context, debug_info->nir_line, 1, gallivm->di_function, NULL); + LLVMSetCurrentDebugLocation2(gallivm->builder, di_loc); + } + switch (instr->type) { case nir_instr_type_alu: visit_alu(bld, nir_instr_as_alu(instr)); @@ -5936,6 +5945,19 @@ void lp_build_nir_soa_func(struct gallivm_state *gallivm, _mesa_key_pointer_equal); bld.range_ht = _mesa_pointer_hash_table_create(NULL); + nir_index_ssa_defs(impl); + + if (bld.base.gallivm->di_builder && bld.base.gallivm->file_name && shader->has_debug_info) { + char *shader_src = nir_shader_gather_debug_info(shader, bld.base.gallivm->file_name, 1); + if (shader_src) { + FILE *f = fopen(bld.base.gallivm->file_name, "w"); + fprintf(f, "%s\n", shader_src); + fclose(f); + + ralloc_free(shader_src); + } + } + nir_foreach_reg_decl(reg, impl) { LLVMTypeRef type = get_register_type(&bld, reg); LLVMValueRef reg_alloc = lp_build_alloca(bld.base.gallivm, @@ -5943,7 +5965,6 @@ void lp_build_nir_soa_func(struct gallivm_state *gallivm, _mesa_hash_table_insert(bld.regs, reg, reg_alloc); } - nir_index_ssa_defs(impl); nir_divergence_analysis_impl(impl, impl->function->shader->options->divergence_analysis_options); bld.ssa_defs = calloc(impl->ssa_alloc * NIR_MAX_VEC_COMPONENTS * 2, sizeof(LLVMValueRef)); visit_cf_list(&bld, &impl->body); diff --git a/src/gallium/drivers/llvmpipe/lp_state_cs.c b/src/gallium/drivers/llvmpipe/lp_state_cs.c index 01bf9e626fe..b935c788bf9 100644 --- a/src/gallium/drivers/llvmpipe/lp_state_cs.c +++ b/src/gallium/drivers/llvmpipe/lp_state_cs.c @@ -580,6 +580,11 @@ generate_compute(struct llvmpipe_context *lp, assert(builder); LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + if (is_mesh) { LLVMTypeRef output_type = create_mesh_jit_output_type_deref(gallivm); output_array = lp_build_array_alloca(gallivm, output_type, lp_build_const_int32(gallivm, align(MAX2(nir->info.mesh.max_primitives_out, nir->info.mesh.max_vertices_out), 8)), "outputs"); @@ -712,6 +717,11 @@ generate_compute(struct llvmpipe_context *lp, builder = gallivm->builder; LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + struct lp_build_loop_state loop_state; if (!use_coro) { diff --git a/src/gallium/drivers/llvmpipe/lp_state_fs.c b/src/gallium/drivers/llvmpipe/lp_state_fs.c index ee5ecefd8f5..d65694a0b3a 100644 --- a/src/gallium/drivers/llvmpipe/lp_state_fs.c +++ b/src/gallium/drivers/llvmpipe/lp_state_fs.c @@ -3320,6 +3320,11 @@ generate_fragment(struct llvmpipe_context *lp, assert(builder); LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + /* code generated texture sampling */ struct lp_build_sampler_soa *sampler = lp_llvm_sampler_soa_create(lp_fs_variant_key_samplers(key), diff --git a/src/gallium/drivers/llvmpipe/lp_state_fs_linear_llvm.c b/src/gallium/drivers/llvmpipe/lp_state_fs_linear_llvm.c index 85353de01ba..d0636000a92 100644 --- a/src/gallium/drivers/llvmpipe/lp_state_fs_linear_llvm.c +++ b/src/gallium/drivers/llvmpipe/lp_state_fs_linear_llvm.c @@ -328,6 +328,11 @@ llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp, LLVMPositionBuilderAtEnd(builder, block); + if (gallivm->di_function) { + LLVMSetCurrentDebugLocation2( + gallivm->builder, LLVMDIBuilderCreateDebugLocation(gallivm->context, 0, 0, gallivm->di_function, NULL)); + } + struct lp_build_context bld; lp_build_context_init(&bld, gallivm, fs_type);