Skip to content

Commit b5c1f4d

Browse files
cyx-6claude
andcommitted
[TEST] Add comprehensive ORC JIT tests for error handling, type variety, and cross-library linking
- Group 1 (pure Python): test empty library, invalid object file path/content, independent sessions, library name collision - Group 2 (type variety): zero-arg, four-arg, float multiply, void return — parametrized over C and C++ with new test_types.c/cc source files - Group 3 (advanced): cross-library symbol resolution via set_link_order with new link_order_base/caller source files, error propagation via TVMFFIErrorSetRaisedFromCStr (C) and std::runtime_error (C++) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a981ffd commit b5c1f4d

10 files changed

Lines changed: 430 additions & 0 deletions

File tree

addons/tvm-ffi-orcjit/tests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ if(NOT WIN32)
6969
add_test_object(sources/cc/test_funcs_conflict.cc)
7070
add_test_object(sources/cc/test_ctor_dtor.cc)
7171
add_test_object(sources/cc/test_call_global.cc)
72+
add_test_object(sources/cc/test_types.cc)
73+
add_test_object(sources/cc/test_link_order_base.cc)
74+
add_test_object(sources/cc/test_link_order_caller.cc)
75+
add_test_object(sources/cc/test_error.cc)
7276
endif()
7377

7478
# Pure C object files — built on all platforms (no C++ runtime deps)
@@ -77,6 +81,10 @@ add_test_object(sources/c/test_funcs.c)
7781
add_test_object(sources/c/test_funcs2.c)
7882
add_test_object(sources/c/test_funcs_conflict.c)
7983
add_test_object(sources/c/test_call_global.c)
84+
add_test_object(sources/c/test_types.c)
85+
add_test_object(sources/c/test_link_order_base.c)
86+
add_test_object(sources/c/test_link_order_caller.c)
87+
add_test_object(sources/c/test_error.c)
8088

8189
# CUDA object files — optional
8290
find_package(CUDAToolkit)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*
21+
* Error propagation test: JIT function signals error via
22+
* TVMFFIErrorSetRaisedFromCStr, which should surface as a Python exception.
23+
*/
24+
#include <tvm/ffi/c_api.h>
25+
26+
/* test_error: always raises a ValueError */
27+
TVM_FFI_DLL_EXPORT int __tvm_ffi_test_error(
28+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
29+
TVMFFIErrorSetRaisedFromCStr("ValueError", "test error");
30+
return -1;
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*
21+
* Base library for cross-library linking test.
22+
* Exports helper_add which is called by test_link_order_caller.c.
23+
*/
24+
#include <tvm/ffi/c_api.h>
25+
26+
/* helper_add: add two integers — used as a dependency by the caller library */
27+
TVM_FFI_DLL_EXPORT int __tvm_ffi_helper_add(
28+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
29+
result->type_index = kTVMFFIInt;
30+
result->zero_padding = 0;
31+
result->v_int64 = args[0].v_int64 + args[1].v_int64;
32+
return 0;
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*
21+
* Caller library for cross-library linking test.
22+
* References __tvm_ffi_helper_add from test_link_order_base.c via extern
23+
* declaration, and exports cross_lib_add which forwards to it.
24+
*/
25+
#include <tvm/ffi/c_api.h>
26+
27+
/* Declare external symbol from the base library */
28+
extern int __tvm_ffi_helper_add(
29+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result);
30+
31+
/* cross_lib_add: forwards to helper_add in the base library */
32+
TVM_FFI_DLL_EXPORT int __tvm_ffi_cross_lib_add(
33+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
34+
return __tvm_ffi_helper_add(self, args, num_args, result);
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*
21+
* Pure C test functions exercising type variety: zero-arg, four-arg,
22+
* float, and void return types via the TVMFFISafeCallType ABI.
23+
*/
24+
#include <tvm/ffi/c_api.h>
25+
26+
/* test_zero_arg: ignores arguments, returns integer 42 */
27+
TVM_FFI_DLL_EXPORT int __tvm_ffi_test_zero_arg(
28+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
29+
result->type_index = kTVMFFIInt;
30+
result->zero_padding = 0;
31+
result->v_int64 = 42;
32+
return 0;
33+
}
34+
35+
/* test_four_args: sum of four integer arguments */
36+
TVM_FFI_DLL_EXPORT int __tvm_ffi_test_four_args(
37+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
38+
result->type_index = kTVMFFIInt;
39+
result->zero_padding = 0;
40+
result->v_int64 = args[0].v_int64 + args[1].v_int64 + args[2].v_int64 + args[3].v_int64;
41+
return 0;
42+
}
43+
44+
/* test_float_multiply: multiply two doubles */
45+
TVM_FFI_DLL_EXPORT int __tvm_ffi_test_float_multiply(
46+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
47+
result->type_index = kTVMFFIFloat;
48+
result->zero_padding = 0;
49+
result->v_float64 = args[0].v_float64 * args[1].v_float64;
50+
return 0;
51+
}
52+
53+
/* test_void_function: does nothing, returns None */
54+
TVM_FFI_DLL_EXPORT int __tvm_ffi_test_void_function(
55+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result) {
56+
result->type_index = kTVMFFINone;
57+
result->zero_padding = 0;
58+
result->v_int64 = 0;
59+
return 0;
60+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Error propagation test (C++ version): throws std::runtime_error
19+
// which TVM_FFI_SAFE_CALL_END catches and converts to an InternalError.
20+
21+
#include <tvm/ffi/function.h>
22+
23+
#include <stdexcept>
24+
25+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(test_error, []() -> int {
26+
throw std::runtime_error("test error");
27+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Base library for cross-library linking test (C++ version).
19+
// Exports helper_add which is called by test_link_order_caller.cc.
20+
21+
#include <tvm/ffi/function.h>
22+
23+
int helper_add_impl(int a, int b) { return a + b; }
24+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(helper_add, helper_add_impl);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Caller library for cross-library linking test (C++ version).
19+
// References __tvm_ffi_helper_add from test_link_order_base.cc and
20+
// exports cross_lib_add which forwards to it.
21+
22+
#include <tvm/ffi/c_api.h>
23+
#include <tvm/ffi/function.h>
24+
25+
extern "C" int __tvm_ffi_helper_add(
26+
void* self, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* result);
27+
28+
int cross_lib_add_impl(int a, int b) {
29+
TVMFFIAny call_args[2];
30+
TVMFFIAny call_result;
31+
call_args[0].type_index = kTVMFFIInt;
32+
call_args[0].zero_padding = 0;
33+
call_args[0].v_int64 = a;
34+
call_args[1].type_index = kTVMFFIInt;
35+
call_args[1].zero_padding = 0;
36+
call_args[1].v_int64 = b;
37+
call_result.type_index = kTVMFFINone;
38+
call_result.zero_padding = 0;
39+
call_result.v_int64 = 0;
40+
__tvm_ffi_helper_add(nullptr, call_args, 2, &call_result);
41+
return static_cast<int>(call_result.v_int64);
42+
}
43+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(cross_lib_add, cross_lib_add_impl);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// C++ test functions exercising type variety: zero-arg, four-arg,
19+
// float, and void return types.
20+
21+
#include <tvm/ffi/function.h>
22+
23+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(test_zero_arg, []() { return 42; });
24+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(test_four_args, [](int a, int b, int c, int d) { return a + b + c + d; });
25+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(test_float_multiply, [](double a, double b) { return a * b; });
26+
TVM_FFI_DLL_EXPORT_TYPED_FUNC(test_void_function, []() {});

0 commit comments

Comments
 (0)