From 059fbfc2523e8f64e4613d38de681e2365ed76fb Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:17:54 +0800 Subject: [PATCH] Fix potential issue in aot compiler when translating block opcodes (#2622) The LLVM zext IR may be inserted after the terminator of a basic block when popping the arguments of a wasm block. Change to insert the zext IR before the terminator of the basic block to resolve the issue. Reported in #2620. --- core/iwasm/compilation/aot_emit_control.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_control.c b/core/iwasm/compilation/aot_emit_control.c index 7e710f91..446ca5ea 100644 --- a/core/iwasm/compilation/aot_emit_control.c +++ b/core/iwasm/compilation/aot_emit_control.c @@ -281,7 +281,7 @@ push_aot_block_to_stack_and_pass_params(AOTCompContext *comp_ctx, AOTBlock *block) { uint32 i, param_index; - LLVMValueRef value; + LLVMValueRef value, br_inst; uint64 size; char name[32]; LLVMBasicBlockRef block_curr = CURR_BLOCK(); @@ -329,7 +329,14 @@ push_aot_block_to_stack_and_pass_params(AOTCompContext *comp_ctx, } } } - SET_BUILDER_POS(block_curr); + + /* At this point, the branch instruction was already built to jump to + * the new BB, to avoid generating zext instruction from the popped + * operand that would come after branch instruction, we should position + * the builder before the last branch instruction */ + br_inst = LLVMGetLastInstruction(block_curr); + bh_assert(LLVMGetInstructionOpcode(br_inst) == LLVMBr); + LLVMPositionBuilderBefore(comp_ctx->builder, br_inst); /* Pop param values from current block's * value stack and add to param phis.