Skip to content

Commit 3d10c6b

Browse files
committed
PackedFunc returns result
1 parent 5ed0c42 commit 3d10c6b

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

rust/common/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ pub mod ffi {
1919

2020
pub type BackendPackedCFunc =
2121
extern "C" fn(args: *const TVMValue, type_codes: *const c_int, num_args: c_int) -> c_int;
22+
23+
pub fn get_last_error() -> String {
24+
unsafe { std::ffi::CStr::from_ptr(TVMGetLastError()) }
25+
.to_str()
26+
.expect("double fault")
27+
.to_owned()
28+
}
2229
}
2330

2431
pub mod array;

rust/common/src/packed_func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{any::Any, convert::TryFrom, marker::PhantomData, os::raw::c_void, str:
33
pub use crate::ffi::TVMValue;
44
use crate::{errors::*, ffi::*, value::*};
55

6-
pub type PackedFunc = Box<Fn(&[TVMArgValue]) -> TVMRetValue + Send + Sync>;
6+
pub type PackedFunc = Box<Fn(&[TVMArgValue]) -> Result<TVMRetValue>>;
77

88
/// Calls a packed function and returns a `TVMRetValue`.
99
///

rust/runtime/src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
279279
.iter()
280280
.map(|t| t.into())
281281
.collect::<Vec<TVMArgValue>>();
282-
func(args.as_slice());
282+
func(args.as_slice()).unwrap();
283283
};
284284
op_execs.push(op);
285285
}

rust/runtime/src/module.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Default for SystemLibModule {
3636
// @see `WrapPackedFunc` in `llvm_module.cc`.
3737
pub(super) fn wrap_backend_packed_func(func: BackendPackedCFunc) -> PackedFunc {
3838
box move |args: &[TVMArgValue]| {
39-
func(
39+
let exit_code = func(
4040
args.iter()
4141
.map(|ref arg| arg.value)
4242
.collect::<Vec<TVMValue>>()
@@ -47,7 +47,11 @@ pub(super) fn wrap_backend_packed_func(func: BackendPackedCFunc) -> PackedFunc {
4747
.as_ptr() as *const i32,
4848
args.len() as i32,
4949
);
50-
TVMRetValue::default()
50+
if exit_code == 0 {
51+
Ok(TVMRetValue::default())
52+
} else {
53+
Err(tvm_common::ffi::get_last_error().into())
54+
}
5155
}
5256
}
5357

0 commit comments

Comments
 (0)