[VM] Preserve 64-bit values in internal calls (#24946) Internal calls marshaled one 32-bit register per argument and result, so the high word of a 64-bit value was dropped or overwritten by the next argument. When that value feeds a buffer bound it surfaces in HAL command buffer validation as an out-of-range binding, as seen in #24929. Marshal each argument and result at its full width from the callee's calling convention, and align argument registers to match how external calls and imports are already marshaled. Adds call_ops.vmasm cases for consecutive i64 arguments, i64 results, and mixed primitive/ref calls. The full VM bytecode test suite passes, and the repro from #24929, which aborted with OUT_OF_RANGE before the fix, now returns the expected output. Fixes #24929 Signed-off-by: zhangpan <98025960+zhangpan2001@users.noreply.github.com> Co-authored-by: OpenAI <noreply@openai.com>
diff --git a/runtime/src/iree/vm/bytecode/dispatch.c b/runtime/src/iree/vm/bytecode/dispatch.c index 4d0a53c..0579955 100644 --- a/runtime/src/iree/vm/bytecode/dispatch.c +++ b/runtime/src/iree/vm/bytecode/dispatch.c
@@ -362,9 +362,16 @@ function.module = module; function.linkage = IREE_VM_FUNCTION_LINKAGE_INTERNAL; function.ordinal = function_ordinal; - IREE_RETURN_IF_ERROR( - iree_vm_bytecode_function_enter(stack, function, iree_string_view_empty(), - out_callee_frame, out_callee_registers)); + // Call register lists contain one entry per value, including 64-bit values. + // Use the callee signature to determine each value's width and ABI alignment. + iree_vm_function_signature_t signature = + iree_vm_function_signature(&function); + iree_string_view_t cconv_arguments; + iree_string_view_t cconv_results; + IREE_RETURN_IF_ERROR(iree_vm_function_call_get_cconv_fragments( + &signature, &cconv_arguments, &cconv_results)); + IREE_RETURN_IF_ERROR(iree_vm_bytecode_function_enter( + stack, function, cconv_results, out_callee_frame, out_callee_registers)); // Remaps argument/result registers from a source list in the caller/callee // frame to the 0-N ABI registers in the callee/caller frame. @@ -382,17 +389,27 @@ // TODO(benvanik): change encoding to avoid this branching. // Could write two arrays: one for prims and one for refs. uint16_t src_reg = src_reg_list->registers[i]; - if (src_reg & IREE_VM_ISA_REF_REGISTER_TYPE_BIT) { - uint16_t dst_reg = ref_reg_offset++; - memset(&dst_regs->ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK], 0, - sizeof(iree_vm_ref_t)); - iree_vm_ref_retain_or_move( - src_reg & IREE_VM_ISA_REF_REGISTER_MOVE_BIT, - &src_regs.ref[src_reg & IREE_VM_ISA_REF_REGISTER_MASK], - &dst_regs->ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK]); - } else { - uint16_t dst_reg = i32_reg_offset++; - dst_regs->i32[dst_reg] = src_regs.i32[src_reg]; + switch (cconv_arguments.data[i]) { + case IREE_VM_CCONV_TYPE_I32: + case IREE_VM_CCONV_TYPE_F32: + dst_regs->i32[i32_reg_offset++] = src_regs.i32[src_reg]; + break; + case IREE_VM_CCONV_TYPE_I64: + case IREE_VM_CCONV_TYPE_F64: + i32_reg_offset = iree_host_align(i32_reg_offset, 2); + memcpy(&dst_regs->i32[i32_reg_offset], &src_regs.i32[src_reg], + sizeof(int64_t)); + i32_reg_offset += 2; + break; + case IREE_VM_CCONV_TYPE_REF: { + uint16_t dst_reg = ref_reg_offset++; + memset(&dst_regs->ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK], 0, + sizeof(iree_vm_ref_t)); + iree_vm_ref_retain_or_move( + src_reg & IREE_VM_ISA_REF_REGISTER_MOVE_BIT, + &src_regs.ref[src_reg & IREE_VM_ISA_REF_REGISTER_MASK], + &dst_regs->ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK]); + } break; } } @@ -431,18 +448,31 @@ } iree_vm_registers_t caller_registers = iree_vm_bytecode_get_register_storage(caller_frame); + iree_vm_bytecode_frame_storage_t* callee_storage = + (iree_vm_bytecode_frame_storage_t*)iree_vm_stack_frame_storage( + callee_frame); + iree_string_view_t cconv_results = callee_storage->cconv_results; for (int i = 0; i < src_reg_list->size; ++i) { // TODO(benvanik): change encoding to avoid this branching. // Could write two arrays: one for prims and one for refs. uint16_t src_reg = src_reg_list->registers[i]; uint16_t dst_reg = dst_reg_list->registers[i]; - if (src_reg & IREE_VM_ISA_REF_REGISTER_TYPE_BIT) { - iree_vm_ref_retain_or_move( - src_reg & IREE_VM_ISA_REF_REGISTER_MOVE_BIT, - &callee_registers.ref[src_reg & IREE_VM_ISA_REF_REGISTER_MASK], - &caller_registers.ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK]); - } else { - caller_registers.i32[dst_reg] = callee_registers.i32[src_reg]; + switch (cconv_results.data[i]) { + case IREE_VM_CCONV_TYPE_I32: + case IREE_VM_CCONV_TYPE_F32: + caller_registers.i32[dst_reg] = callee_registers.i32[src_reg]; + break; + case IREE_VM_CCONV_TYPE_I64: + case IREE_VM_CCONV_TYPE_F64: + memcpy(&caller_registers.i32[dst_reg], &callee_registers.i32[src_reg], + sizeof(int64_t)); + break; + case IREE_VM_CCONV_TYPE_REF: + iree_vm_ref_retain_or_move( + src_reg & IREE_VM_ISA_REF_REGISTER_MOVE_BIT, + &callee_registers.ref[src_reg & IREE_VM_ISA_REF_REGISTER_MASK], + &caller_registers.ref[dst_reg & IREE_VM_ISA_REF_REGISTER_MASK]); + break; } } // Clear return_registers so it's not misinterpreted as resume state by @@ -450,9 +480,6 @@ caller_storage->return_registers = NULL; // Mark successful completion - compiler guarantees all refs are released. - iree_vm_bytecode_frame_storage_t* callee_storage = - (iree_vm_bytecode_frame_storage_t*)iree_vm_stack_frame_storage( - callee_frame); callee_storage->result_code = IREE_STATUS_OK; // Leave and deallocate bytecode stack frame.
diff --git a/runtime/src/iree/vm/test/call_ops.vmasm b/runtime/src/iree/vm/test/call_ops.vmasm index 824c9b8..1fb771e 100644 --- a/runtime/src/iree/vm/test/call_ops.vmasm +++ b/runtime/src/iree/vm/test/call_ops.vmasm
@@ -1,5 +1,8 @@ vm.module @call_ops version 0 vm.type i32 +vm.type i64 +vm.type f32 +vm.type f64 vm.type !vm.buffer vm.type !vm.opaque vm.rodata[0] = 010203 @@ -213,3 +216,97 @@ %i0 = vm.const.i32 9 // 0x00000009 vm.fail %i0, "" } + +// Consecutive i64 arguments must each occupy two registers in the callee. +vm.export @test_call_consecutive_i64_arguments +vm.func @test_call_consecutive_i64_arguments() -> () { +^bb0: + %i0:1 = vm.const.i64 4294967298 + %i2:3 = vm.const.i64 12884901892 + vm.call @call_ops:19(%i0, %i2) + vm.return +} + +vm.func @check_consecutive_i64(%i0:1: i64, %i2:3: i64) -> () { +^bb0: + %i4:5 = vm.const.i64 4294967298 + %i6:7 = vm.const.i64 12884901892 + %i8 = vm.cmp.ne.i64 %i0:1, %i4:5 + %i9 = vm.cmp.ne.i64 %i2:3, %i6:7 + %i8 = vm.or.i32 %i8, %i9 + vm.cond_br %i8, ^bb2(), ^bb1() +^bb1: + vm.return +^bb2: + %i10 = vm.const.i32 9 + vm.fail %i10, "consecutive i64 arguments must preserve both words" +} + +// Initialize destination high words so a partial return copy fails reliably. +vm.export @test_call_i64_results_preserve_high_words +vm.func @test_call_i64_results_preserve_high_words() -> () { +^bb0: + %i0:1 = vm.const.i64.zero + %i2:3 = vm.const.i64.zero + %i0, %i2 = vm.call @call_ops:21() + %i4:5 = vm.const.i64 4294967298 + %i6:7 = vm.const.i64 12884901892 + %i8 = vm.cmp.ne.i64 %i0:1, %i4:5 + %i9 = vm.cmp.ne.i64 %i2:3, %i6:7 + %i8 = vm.or.i32 %i8, %i9 + vm.cond_br %i8, ^bb2(), ^bb1() +^bb1: + vm.return +^bb2: + %i10 = vm.const.i32 9 + vm.fail %i10, "i64 results must preserve both words" +} + +vm.func @return_i64_pair() -> (i64, i64) { +^bb0: + %i0:1 = vm.const.i64 4294967298 + %i2:3 = vm.const.i64 12884901892 + vm.return %i0, %i2 +} + +// Mixed-width values share the integer bank; references use a separate bank +// and must retain their ownership semantics. +vm.export @test_call_mixed_width_arguments_and_results +vm.func @test_call_mixed_width_arguments_and_results() -> () { +^bb0: + %i0 = vm.const.i32 7 + %i2:3 = vm.const.i64 4294967298 + %r0 = vm.const.ref.rodata 0 + %i4 = vm.const.f32 3.5 + %i6:7 = vm.const.f64 5.5 + %i8 = vm.const.i32 11 + %i10 = vm.const.i32.zero + %i12:13 = vm.const.i64.zero + %i14 = vm.const.i32.zero + %i16:17 = vm.const.i64.zero + %i18 = vm.const.i32.zero + %i10, %i12, %r1, %i14, %i16, %i18 = vm.call @call_ops:23(%i2, %i0, %r0, %i4, %i6, %i8) + %i19 = vm.cmp.ne.i32 %i10, %i0 + %i20 = vm.cmp.ne.i64 %i12:13, %i2:3 + %i19 = vm.or.i32 %i19, %i20 + %i20 = vm.cmp.ne.f32.o %i14, %i4 + %i19 = vm.or.i32 %i19, %i20 + %i20 = vm.cmp.ne.f64.o %i16:17, %i6:7 + %i19 = vm.or.i32 %i19, %i20 + %i20 = vm.cmp.ne.i32 %i18, %i8 + %i19 = vm.or.i32 %i19, %i20 + %i20 = vm.cmp.ne.ref %r0, %r1 + %i19 = vm.or.i32 %i19, %i20 + vm.discard.refs %r0, %r1 + vm.cond_br %i19, ^bb2(), ^bb1() +^bb1: + vm.return +^bb2: + %i21 = vm.const.i32 9 + vm.fail %i21, "mixed-width call must preserve values and alignment" +} + +vm.func @return_mixed_width(%i0:1: i64, %i2: i32, %r0: !vm.ref<?>, %i3: f32, %i4:5: f64, %i6: i32) -> (i32, i64, !vm.ref<?>, f32, f64, i32) { +^bb0: + vm.return %i2, %i0, %R0, %i3, %i4, %i6 +}