From 6b1b0775fecff69af25b0f176795e3367f640fcc Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 16 Jan 2024 22:12:08 -0500 Subject: [PATCH 1/4] [mono][jit] Reenable branch optimizations when running with llvm. --- src/mono/mono/mini/branch-opts.c | 10 +++++----- src/mono/mono/mini/mini.c | 4 +--- src/mono/mono/mini/mini.h | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/mono/mono/mini/branch-opts.c b/src/mono/mono/mini/branch-opts.c index d8e1c3abc80d0..6891022f51916 100644 --- a/src/mono/mono/mini/branch-opts.c +++ b/src/mono/mono/mini/branch-opts.c @@ -776,7 +776,7 @@ mono_if_conversion (MonoCompile *cfg) } void -mono_nullify_basic_block (MonoBasicBlock *bb) +mono_nullify_basic_block (MonoCompile *cfg, MonoBasicBlock *bb) { bb->in_count = 0; bb->out_count = 0; @@ -960,7 +960,7 @@ remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *p } previous_bb->next_bb = bb->next_bb; - mono_nullify_basic_block (bb); + mono_nullify_basic_block (cfg, bb); return TRUE; } else { @@ -1046,7 +1046,7 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b if (bb->next_bb == bbn) bb->next_bb = bbn->next_bb; } - mono_nullify_basic_block (bbn); + mono_nullify_basic_block (cfg, bbn); /* * If bbn fell through to its next bblock, have to add a branch, since bb @@ -1291,7 +1291,7 @@ mono_optimize_branches (MonoCompile *cfg) for (i = 0; i < bbn->out_count; i++) replace_in_block (bbn->out_bb [i], bbn, NULL); - mono_nullify_basic_block (bbn); + mono_nullify_basic_block (cfg, bbn); changed = TRUE; } @@ -1341,7 +1341,7 @@ mono_optimize_branches (MonoCompile *cfg) for (i = 0; i < bbn->out_count; i++) replace_in_block (bbn->out_bb [i], bbn, NULL); - mono_nullify_basic_block (bbn); + mono_nullify_basic_block (cfg, bbn); changed = TRUE; continue; } diff --git a/src/mono/mono/mini/mini.c b/src/mono/mono/mini/mini.c index b6935d312c6ee..66320482421ab 100644 --- a/src/mono/mono/mini/mini.c +++ b/src/mono/mono/mini/mini.c @@ -3549,8 +3549,6 @@ mini_method_compile (MonoMethod *method, guint32 opts, JitFlags flags, int parts } cfg->opt &= ~MONO_OPT_LINEARS; - - cfg->opt &= ~MONO_OPT_BRANCH; } cfg->after_method_to_ir = TRUE; @@ -3975,7 +3973,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, JitFlags flags, int parts if (!cfg->compile_aot) mono_lldb_save_method_info (cfg); - if (cfg->verbose_level >= 2) { + if (cfg->verbose_level >= 2 && !cfg->llvm_only) { char *id = mono_method_full_name (cfg->method, TRUE); g_print ("\n*** ASM for %s ***\n", id); mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3); diff --git a/src/mono/mono/mini/mini.h b/src/mono/mono/mini/mini.h index cd2cad8e7dcec..3c35b60035f87 100644 --- a/src/mono/mono/mini/mini.h +++ b/src/mono/mono/mini/mini.h @@ -2184,7 +2184,7 @@ void mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *f void mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to); gboolean mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2); void mono_remove_bblock (MonoCompile *cfg, MonoBasicBlock *bb); -void mono_nullify_basic_block (MonoBasicBlock *bb); +void mono_nullify_basic_block (MonoCompile *cfg, MonoBasicBlock *bb); void mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *bbn); void mono_optimize_branches (MonoCompile *cfg); From c927f3f9d71843d34d9a8e11e29d3004262badb7 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Mon, 22 Jan 2024 15:43:46 -0500 Subject: [PATCH 2/4] [mono][llvm] Fix the return type of OP_SSE2_PACKUS. This is supposed to return a 16xi8 so there is no need to cast it. This only worked by accident because the phi handling code casted it to the right type most of the time. --- src/mono/mono/mini/mini-llvm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/mono/mono/mini/mini-llvm.c b/src/mono/mono/mini/mini-llvm.c index 77e389a51cf9a..1fced1040b304 100644 --- a/src/mono/mono/mini/mini-llvm.c +++ b/src/mono/mono/mini/mini-llvm.c @@ -9579,9 +9579,7 @@ MONO_RESTORE_WARNING LLVMValueRef args [2]; args [0] = convert (ctx, lhs, sse_i2_t); args [1] = convert (ctx, rhs, sse_i2_t); - values [ins->dreg] = convert (ctx, - call_intrins (ctx, INTRINS_SSE_PACKUSWB, args, dname), - type_to_sse_type (GTMREG_TO_INT (ins->inst_c1))); + values [ins->dreg] = call_intrins (ctx, INTRINS_SSE_PACKUSWB, args, dname); break; } From 813974be4754d1183e67a77aab6aac1c6c5e3755 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Wed, 24 Jan 2024 13:04:40 -0500 Subject: [PATCH 3/4] Disable moving out of line bblocks to the end of code when using llvm. --- src/mono/mono/mini/branch-opts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/mini/branch-opts.c b/src/mono/mono/mini/branch-opts.c index 6891022f51916..a55ae6583057a 100644 --- a/src/mono/mono/mini/branch-opts.c +++ b/src/mono/mono/mini/branch-opts.c @@ -1463,7 +1463,7 @@ mono_optimize_branches (MonoCompile *cfg) } if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) { - if (bb->last_ins->inst_false_bb && bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region) && !cfg->disable_out_of_line_bblocks) { + if (!COMPILE_LLVM (cfg) && bb->last_ins->inst_false_bb && bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region) && !cfg->disable_out_of_line_bblocks) { /* Reverse the branch */ bb->last_ins->opcode = GUINT32_TO_OPCODE (mono_reverse_branch_op (bb->last_ins->opcode)); bbn = bb->last_ins->inst_false_bb; From 2e57282ecc905d5322bb70b48b1ef05dc0ff53ce Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Thu, 25 Jan 2024 19:21:57 -0500 Subject: [PATCH 4/4] Fix a crash in OP_SETRET. --- src/mono/mono/mini/mini-llvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/mini/mini-llvm.c b/src/mono/mono/mini/mini-llvm.c index 1fced1040b304..e2c68179bf7c9 100644 --- a/src/mono/mono/mini/mini-llvm.c +++ b/src/mono/mono/mini/mini-llvm.c @@ -6147,7 +6147,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb) break; case LLVMArgVtypeAsScalar: if (is_simd) { - retval = LLVMBuildBitCast (builder, values [ins->sreg1], ret_type, "setret_simd_vtype_as_scalar"); + retval = LLVMBuildBitCast (builder, lhs, ret_type, "setret_simd_vtype_as_scalar"); } else { g_assert (addresses [ins->sreg1]); retval = LLVMBuildLoad2 (builder, ret_type, build_ptr_cast (builder, addresses [ins->sreg1]->value, pointer_type (ret_type)), "");