Skip to content

Commit d92d35c

Browse files
peppsacMarge Bot
authored andcommitted
ac/llvm: add a return value to ac_nir_translate
Return a boolean instead of aborting when the NIR->LLVM translation fails. This way, we can deal with the error at the calling site instead of crashing the whole application. Reviewed-by: Timur Kristóf <[email protected]> Reviewed-by: Marek Olšák <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18147>
1 parent 07b3ade commit d92d35c

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

src/amd/llvm/ac_nir_to_llvm.c

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ static LLVMValueRef exit_waterfall(struct ac_nir_context *ctx, struct waterfall_
560560
return ret;
561561
}
562562

563-
static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
563+
static bool visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
564564
{
565565
LLVMValueRef src[16], result = NULL;
566566
unsigned num_components = instr->dest.dest.ssa.num_components;
@@ -1361,17 +1361,18 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
13611361
fprintf(stderr, "Unknown NIR alu instr: ");
13621362
nir_print_instr(&instr->instr, stderr);
13631363
fprintf(stderr, "\n");
1364-
abort();
1364+
return false;
13651365
}
13661366

13671367
if (result) {
13681368
assert(instr->dest.dest.is_ssa);
13691369
result = ac_to_integer_or_pointer(&ctx->ac, result);
13701370
ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
13711371
}
1372+
return true;
13721373
}
13731374

1374-
static void visit_load_const(struct ac_nir_context *ctx, const nir_load_const_instr *instr)
1375+
static bool visit_load_const(struct ac_nir_context *ctx, const nir_load_const_instr *instr)
13751376
{
13761377
LLVMValueRef values[4], value = NULL;
13771378
LLVMTypeRef element_type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
@@ -1395,7 +1396,7 @@ static void visit_load_const(struct ac_nir_context *ctx, const nir_load_const_in
13951396
break;
13961397
default:
13971398
fprintf(stderr, "unsupported nir load_const bit_size: %d\n", instr->def.bit_size);
1398-
abort();
1399+
return false;
13991400
}
14001401
}
14011402
if (instr->def.num_components > 1) {
@@ -1404,6 +1405,7 @@ static void visit_load_const(struct ac_nir_context *ctx, const nir_load_const_in
14041405
value = values[0];
14051406

14061407
ctx->ssa_defs[instr->def.index] = value;
1408+
return true;
14071409
}
14081410

14091411
/* Gather4 should follow the same rules as bilinear filtering, but the hardware
@@ -3536,7 +3538,7 @@ emit_load_frag_coord(struct ac_nir_context *ctx)
35363538
return ac_to_integer(&ctx->ac, ac_build_gather_values(&ctx->ac, values, 4));
35373539
}
35383540

3539-
static void visit_intrinsic(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
3541+
static bool visit_intrinsic(struct ac_nir_context *ctx, nir_intrinsic_instr *instr)
35403542
{
35413543
LLVMValueRef result = NULL;
35423544

@@ -4308,12 +4310,12 @@ static void visit_intrinsic(struct ac_nir_context *ctx, nir_intrinsic_instr *ins
43084310
fprintf(stderr, "Unknown intrinsic: ");
43094311
nir_print_instr(&instr->instr, stderr);
43104312
fprintf(stderr, "\n");
4311-
abort();
4312-
break;
4313+
return false;
43134314
}
43144315
if (result) {
43154316
ctx->ssa_defs[instr->dest.ssa.index] = result;
43164317
}
4318+
return true;
43174319
}
43184320

43194321
static LLVMValueRef get_bindless_index_from_uniform(struct ac_nir_context *ctx, unsigned base_index,
@@ -4988,7 +4990,7 @@ static void visit_ssa_undef(struct ac_nir_context *ctx, const nir_ssa_undef_inst
49884990
}
49894991
}
49904992

4991-
static void visit_jump(struct ac_llvm_context *ctx, const nir_jump_instr *instr)
4993+
static bool visit_jump(struct ac_llvm_context *ctx, const nir_jump_instr *instr)
49924994
{
49934995
switch (instr->type) {
49944996
case nir_jump_break:
@@ -5001,8 +5003,9 @@ static void visit_jump(struct ac_llvm_context *ctx, const nir_jump_instr *instr)
50015003
fprintf(stderr, "Unknown NIR jump instr: ");
50025004
nir_print_instr(&instr->instr, stderr);
50035005
fprintf(stderr, "\n");
5004-
abort();
5006+
return false;
50055007
}
5008+
return true;
50065009
}
50075010

50085011
static LLVMTypeRef glsl_base_to_llvm_type(struct ac_llvm_context *ac, enum glsl_base_type type)
@@ -5065,10 +5068,10 @@ static LLVMTypeRef glsl_to_llvm_type(struct ac_llvm_context *ac, const struct gl
50655068
return LLVMStructTypeInContext(ac->context, member_types, glsl_get_length(type), false);
50665069
}
50675070

5068-
static void visit_deref(struct ac_nir_context *ctx, nir_deref_instr *instr)
5071+
static bool visit_deref(struct ac_nir_context *ctx, nir_deref_instr *instr)
50695072
{
50705073
if (!nir_deref_mode_is_one_of(instr, nir_var_mem_shared | nir_var_mem_global))
5071-
return;
5074+
return true;
50725075

50735076
LLVMValueRef result = NULL;
50745077
switch (instr->deref_type) {
@@ -5147,7 +5150,9 @@ static void visit_deref(struct ac_nir_context *ctx, nir_deref_instr *instr)
51475150
address_space = AC_ADDR_SPACE_GLOBAL;
51485151
break;
51495152
default:
5150-
unreachable("Unhandled address space");
5153+
nir_print_instr(&instr->instr, stderr);
5154+
fprintf(stderr, "Unhandled address space %x\n", instr->modes);
5155+
return false;
51515156
}
51525157

51535158
LLVMTypeRef type = LLVMPointerType(pointee_type, address_space);
@@ -5162,15 +5167,19 @@ static void visit_deref(struct ac_nir_context *ctx, nir_deref_instr *instr)
51625167
break;
51635168
}
51645169
default:
5165-
unreachable("Unhandled deref_instr deref type");
5170+
fprintf(stderr, "Unhandled deref_instr deref type: ");
5171+
nir_print_instr(&instr->instr, stderr);
5172+
fprintf(stderr, "\n");
5173+
return false;
51665174
}
51675175

51685176
ctx->ssa_defs[instr->dest.ssa.index] = result;
5177+
return true;
51695178
}
51705179

5171-
static void visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list);
5180+
static bool visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list);
51725181

5173-
static void visit_block(struct ac_nir_context *ctx, nir_block *block)
5182+
static bool visit_block(struct ac_nir_context *ctx, nir_block *block)
51745183
{
51755184
LLVMBasicBlockRef blockref = LLVMGetInsertBlock(ctx->ac.builder);
51765185
LLVMValueRef first = LLVMGetFirstInstruction(blockref);
@@ -5190,13 +5199,16 @@ static void visit_block(struct ac_nir_context *ctx, nir_block *block)
51905199
nir_foreach_instr (instr, block) {
51915200
switch (instr->type) {
51925201
case nir_instr_type_alu:
5193-
visit_alu(ctx, nir_instr_as_alu(instr));
5202+
if (!visit_alu(ctx, nir_instr_as_alu(instr)))
5203+
return false;
51945204
break;
51955205
case nir_instr_type_load_const:
5196-
visit_load_const(ctx, nir_instr_as_load_const(instr));
5206+
if (!visit_load_const(ctx, nir_instr_as_load_const(instr)))
5207+
return false;
51975208
break;
51985209
case nir_instr_type_intrinsic:
5199-
visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
5210+
if (!visit_intrinsic(ctx, nir_instr_as_intrinsic(instr)))
5211+
return false;
52005212
break;
52015213
case nir_instr_type_tex:
52025214
visit_tex(ctx, nir_instr_as_tex(instr));
@@ -5207,74 +5219,87 @@ static void visit_block(struct ac_nir_context *ctx, nir_block *block)
52075219
visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
52085220
break;
52095221
case nir_instr_type_jump:
5210-
visit_jump(&ctx->ac, nir_instr_as_jump(instr));
5222+
if (!visit_jump(&ctx->ac, nir_instr_as_jump(instr)))
5223+
return false;
52115224
break;
52125225
case nir_instr_type_deref:
5213-
visit_deref(ctx, nir_instr_as_deref(instr));
5226+
if (!visit_deref(ctx, nir_instr_as_deref(instr)))
5227+
return false;
52145228
break;
52155229
default:
52165230
fprintf(stderr, "Unknown NIR instr type: ");
52175231
nir_print_instr(instr, stderr);
52185232
fprintf(stderr, "\n");
5219-
abort();
5233+
return false;
52205234
}
52215235
}
52225236

52235237
_mesa_hash_table_insert(ctx->defs, block, LLVMGetInsertBlock(ctx->ac.builder));
5238+
5239+
return true;
52245240
}
52255241

5226-
static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
5242+
static bool visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
52275243
{
52285244
LLVMValueRef value = get_src(ctx, if_stmt->condition);
52295245

52305246
nir_block *then_block = (nir_block *)exec_list_get_head(&if_stmt->then_list);
52315247

52325248
ac_build_ifcc(&ctx->ac, value, then_block->index);
52335249

5234-
visit_cf_list(ctx, &if_stmt->then_list);
5250+
if (!visit_cf_list(ctx, &if_stmt->then_list))
5251+
return false;
52355252

52365253
if (!exec_list_is_empty(&if_stmt->else_list)) {
52375254
nir_block *else_block = (nir_block *)exec_list_get_head(&if_stmt->else_list);
52385255

52395256
ac_build_else(&ctx->ac, else_block->index);
5240-
visit_cf_list(ctx, &if_stmt->else_list);
5257+
if (!visit_cf_list(ctx, &if_stmt->else_list))
5258+
return false;
52415259
}
52425260

52435261
ac_build_endif(&ctx->ac, then_block->index);
5262+
return true;
52445263
}
52455264

5246-
static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
5265+
static bool visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
52475266
{
52485267
nir_block *first_loop_block = (nir_block *)exec_list_get_head(&loop->body);
52495268

52505269
ac_build_bgnloop(&ctx->ac, first_loop_block->index);
52515270

5252-
visit_cf_list(ctx, &loop->body);
5271+
if (!visit_cf_list(ctx, &loop->body))
5272+
return false;
52535273

52545274
ac_build_endloop(&ctx->ac, first_loop_block->index);
5275+
return true;
52555276
}
52565277

5257-
static void visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list)
5278+
static bool visit_cf_list(struct ac_nir_context *ctx, struct exec_list *list)
52585279
{
52595280
foreach_list_typed(nir_cf_node, node, node, list)
52605281
{
52615282
switch (node->type) {
52625283
case nir_cf_node_block:
5263-
visit_block(ctx, nir_cf_node_as_block(node));
5284+
if (!visit_block(ctx, nir_cf_node_as_block(node)))
5285+
return false;
52645286
break;
52655287

52665288
case nir_cf_node_if:
5267-
visit_if(ctx, nir_cf_node_as_if(node));
5289+
if (!visit_if(ctx, nir_cf_node_as_if(node)))
5290+
return false;
52685291
break;
52695292

52705293
case nir_cf_node_loop:
5271-
visit_loop(ctx, nir_cf_node_as_loop(node));
5294+
if (!visit_loop(ctx, nir_cf_node_as_loop(node)))
5295+
return false;
52725296
break;
52735297

52745298
default:
5275-
assert(0);
5299+
return false;
52765300
}
52775301
}
5302+
return true;
52785303
}
52795304

52805305
void ac_handle_shader_output_decl(struct ac_llvm_context *ctx, struct ac_shader_abi *abi,
@@ -5379,7 +5404,7 @@ static void setup_gds(struct ac_nir_context *ctx, nir_function_impl *impl)
53795404
ac_llvm_add_target_dep_function_attr(ctx->main_function, "amdgpu-gds-size", gds_size);
53805405
}
53815406

5382-
void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
5407+
bool ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
53835408
const struct ac_shader_args *args, struct nir_shader *nir)
53845409
{
53855410
struct ac_nir_context ctx = {0};
@@ -5428,7 +5453,9 @@ void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
54285453
ctx.ac.postponed_kill = ac_build_alloca_init(&ctx.ac, ctx.ac.i1true, "");
54295454
}
54305455

5431-
visit_cf_list(&ctx, &func->impl->body);
5456+
if (!visit_cf_list(&ctx, &func->impl->body))
5457+
return false;
5458+
54325459
phi_post_pass(&ctx);
54335460

54345461
if (ctx.ac.postponed_kill)
@@ -5440,4 +5467,6 @@ void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
54405467
ralloc_free(ctx.vars);
54415468
if (ctx.abi->kill_ps_if_inf_interp)
54425469
ralloc_free(ctx.verified_interp);
5470+
5471+
return true;
54435472
}

src/amd/llvm/ac_nir_to_llvm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static inline unsigned ac_llvm_reg_index_soa(unsigned index, unsigned chan)
4747
return (index * 4) + chan;
4848
}
4949

50-
void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
50+
bool ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
5151
const struct ac_shader_args *args, struct nir_shader *nir);
5252

5353
void ac_handle_shader_output_decl(struct ac_llvm_context *ctx, struct ac_shader_abi *abi,

0 commit comments

Comments
 (0)