Skip to content

Commit a8aa03c

Browse files
committed
Sync with main
1 parent a1bb7a5 commit a8aa03c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1197
-1125
lines changed

.github/workflows/release.yml

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,19 @@ on:
66

77
env:
88
CIBW_BUILD_VERBOSITY: 3
9-
CIBW_TEST_REQUIRES: "pytest"
10-
CIBW_TEST_COMMAND: "pytest -svv {project}/tests/python/"
9+
CIBW_TEST_COMMAND: "python -c \"import mlc\""
1110
CIBW_SKIP: "cp313-win_amd64" # Python 3.13 is not quite ready yet
1211
MLC_CIBW_VERSION: "2.20.0"
1312
MLC_PYTHON_VERSION: "3.9"
1413
MLC_CIBW_WIN_BUILD: "cp3*-win_amd64"
1514
MLC_CIBW_MAC_BUILD: "cp3*-macosx_arm64"
15+
MLC_CIBW_MAC_X86_BUILD: "cp3*-macosx_x86_64"
1616
MLC_CIBW_LINUX_BUILD: "cp3*-manylinux_x86_64"
1717

1818
jobs:
19-
pre-commit:
20-
runs-on: ubuntu-latest
21-
steps:
22-
- uses: actions/checkout@v4
23-
- uses: actions/setup-python@v5
24-
with:
25-
python-version: ${{ env.MLC_PYTHON_VERSION }}
26-
- uses: pre-commit/[email protected]
2719
windows:
2820
name: Windows
2921
runs-on: windows-latest
30-
needs: pre-commit
3122
steps:
3223
- uses: actions/checkout@v4
3324
with:
@@ -36,7 +27,7 @@ jobs:
3627
with:
3728
python-version: ${{ env.MLC_PYTHON_VERSION }}
3829
- name: Install cibuildwheel
39-
run: python -m pip install twine cibuildwheel=="${{ env.MLC_CIBW_VERSION }}"
30+
run: python -m pip install cibuildwheel=="${{ env.MLC_CIBW_VERSION }}"
4031
- name: Build wheels
4132
run: python -m cibuildwheel --output-dir wheelhouse
4233
env:
@@ -51,7 +42,6 @@ jobs:
5142
macos:
5243
name: MacOS
5344
runs-on: macos-latest
54-
needs: pre-commit
5545
steps:
5646
- uses: actions/checkout@v4
5747
with:
@@ -60,7 +50,7 @@ jobs:
6050
with:
6151
python-version: ${{ env.MLC_PYTHON_VERSION }}
6252
- name: Install cibuildwheel
63-
run: python -m pip install twine cibuildwheel==${{ env.MLC_CIBW_VERSION }}
53+
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
6454
- name: Build wheels
6555
run: python -m cibuildwheel --output-dir wheelhouse
6656
env:
@@ -72,10 +62,32 @@ jobs:
7262
with:
7363
name: wheels-macos
7464
path: ./wheelhouse/*.whl
65+
macos-x86:
66+
name: MacOS-x86
67+
runs-on: macos-13
68+
steps:
69+
- uses: actions/checkout@v4
70+
with:
71+
submodules: "recursive"
72+
- uses: actions/setup-python@v5
73+
with:
74+
python-version: ${{ env.MLC_PYTHON_VERSION }}
75+
- name: Install cibuildwheel
76+
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
77+
- name: Build wheels
78+
run: python -m cibuildwheel --output-dir wheelhouse
79+
env:
80+
CIBW_BUILD: ${{ env.MLC_CIBW_MAC_X86_BUILD }}
81+
- name: Show package contents
82+
run: python scripts/show_wheel_content.py wheelhouse
83+
- name: Upload wheels
84+
uses: actions/upload-artifact@v3
85+
with:
86+
name: wheels-macos-x86
87+
path: ./wheelhouse/*.whl
7588
linux:
7689
name: Linux
7790
runs-on: ubuntu-latest
78-
needs: pre-commit
7991
steps:
8092
- uses: actions/checkout@v4
8193
with:
@@ -84,7 +96,7 @@ jobs:
8496
with:
8597
python-version: ${{ env.MLC_PYTHON_VERSION }}
8698
- name: Install cibuildwheel
87-
run: python -m pip install twine cibuildwheel==${{ env.MLC_CIBW_VERSION }}
99+
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
88100
- name: Build wheels
89101
run: python -m cibuildwheel --output-dir wheelhouse
90102
env:
@@ -99,7 +111,7 @@ jobs:
99111
publish:
100112
name: Publish
101113
runs-on: ubuntu-latest
102-
needs: [windows, macos, linux]
114+
needs: [windows, macos, linux, macos-x86]
103115
environment:
104116
name: pypi
105117
url: https://pypi.org/p/mlc-python
@@ -114,6 +126,7 @@ jobs:
114126
- name: Prepare distribution files
115127
run: |
116128
mkdir -p dist
129+
mv wheelhouse/wheels-macos-x86/*.whl dist/
117130
mv wheelhouse/wheels-macos/*.whl dist/
118131
mv wheelhouse/wheels-linux/*.whl dist/
119132
mv wheelhouse/wheels-windows/*.whl dist/

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(
44
mlc
5-
VERSION 1.0
5+
VERSION 0.0.2
66
DESCRIPTION "MLC's FFI system"
77
LANGUAGES C CXX
88
)

cpp/c_api.cc

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
#include "./registry.h"
2-
#include "mlc/ffi/ext/func.h"
3-
#include <mlc/ffi/ffi.hpp>
42
#if defined(__APPLE__)
53
#include <iostream>
64
#endif
75

86
namespace mlc {
9-
namespace ffi {
107
namespace registry {
118
TypeTable *TypeTable::Global() {
129
static TypeTable *instance = TypeTable::New();
1310
return instance;
1411
}
1512
} // namespace registry
16-
} // namespace ffi
1713
} // namespace mlc
1814

19-
using ::mlc::ffi::Any;
20-
using ::mlc::ffi::AnyView;
21-
using ::mlc::ffi::ErrorObj;
22-
using ::mlc::ffi::FuncObj;
23-
using ::mlc::ffi::Ref;
24-
using ::mlc::ffi::registry::TypeTable;
15+
using ::mlc::Any;
16+
using ::mlc::AnyView;
17+
using ::mlc::ErrorObj;
18+
using ::mlc::FuncObj;
19+
using ::mlc::Ref;
20+
using ::mlc::registry::TypeTable;
2521

2622
namespace {
2723
thread_local Any last_error;
@@ -35,9 +31,10 @@ MLC_API MLCAny MLCGetLastError() {
3531
}
3632

3733
MLC_API int32_t MLCTypeRegister(MLCTypeTableHandle _self, int32_t parent_type_index, const char *type_key,
38-
int32_t type_index, MLCTypeInfo **out_type_info) {
34+
int32_t type_index, MLCAttrGetterSetter getter, MLCAttrGetterSetter setter,
35+
MLCTypeInfo **out_type_info) {
3936
MLC_SAFE_CALL_BEGIN();
40-
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key);
37+
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key, getter, setter);
4138
MLC_SAFE_CALL_END(&last_error);
4239
}
4340

@@ -86,16 +83,16 @@ MLC_API int32_t MLCDynTypeTypeTableDestroy(MLCTypeTableHandle handle) {
8683

8784
MLC_API int32_t MLCAnyIncRef(MLCAny *any) {
8885
MLC_SAFE_CALL_BEGIN();
89-
if (!::mlc::ffi::details::IsTypeIndexPOD(any->type_index)) {
90-
::mlc::ffi::details::IncRef(any->v_obj);
86+
if (!::mlc::base::IsTypeIndexPOD(any->type_index)) {
87+
::mlc::base::IncRef(any->v_obj);
9188
}
9289
MLC_SAFE_CALL_END(&last_error);
9390
}
9491

9592
MLC_API int32_t MLCAnyDecRef(MLCAny *any) {
9693
MLC_SAFE_CALL_BEGIN();
97-
if (!::mlc::ffi::details::IsTypeIndexPOD(any->type_index)) {
98-
::mlc::ffi::details::DecRef(any->v_obj);
94+
if (!::mlc::base::IsTypeIndexPOD(any->type_index)) {
95+
::mlc::base::DecRef(any->v_obj);
9996
}
10097
MLC_SAFE_CALL_END(&last_error);
10198
}

cpp/c_api_tests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include <mlc/ffi/ffi.hpp>
1+
#include <mlc/all.h>
22

33
namespace mlc {
4-
namespace ffi {
4+
namespace {
55

66
/**************** FFI ****************/
77

@@ -95,5 +95,5 @@ MLC_REGISTER_FUNC("mlc.testing.nested_type_checking_list").set_body([](Str name)
9595
MLC_UNREACHABLE();
9696
});
9797

98-
} // namespace ffi
98+
} // namespace
9999
} // namespace mlc

cpp/dso_loader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#else
77
#include <dlfcn.h>
88
#endif
9-
#include <mlc/ffi/core/core.h>
9+
#include <mlc/all.h>
10+
#include <string>
1011

1112
namespace mlc {
12-
namespace ffi {
1313

1414
struct DSOLibrary {
1515
~DSOLibrary() { Unload(); }
@@ -56,7 +56,6 @@ struct DSOLibrary {
5656
#endif
5757
};
5858

59-
} // namespace ffi
6059
} // namespace mlc
6160

6261
#endif // MLC_DSO_LIBRARY_H_

cpp/registry.h

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#include <cstring>
77
#include <iostream>
88
#include <memory>
9-
#include <mlc/ffi/ffi.hpp>
109
#include <string>
1110
#include <unordered_map>
1211
#include <vector>
1312

1413
namespace mlc {
15-
namespace ffi {
1614
namespace registry {
1715

1816
struct TypeTable;
@@ -31,6 +29,32 @@ struct TypeInfoWrapper {
3129
~TypeInfoWrapper() { this->Reset(); }
3230
};
3331

32+
template <typename T> struct PODGetterSetter {
33+
static int32_t Getter(void *addr, MLCAny *ret) {
34+
MLC_SAFE_CALL_BEGIN();
35+
*static_cast<Any *>(ret) = *static_cast<T *>(addr);
36+
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
37+
}
38+
static int32_t Setter(void *addr, MLCAny *src) {
39+
MLC_SAFE_CALL_BEGIN();
40+
*static_cast<T *>(addr) = (static_cast<Any *>(src))->operator T();
41+
MLC_SAFE_CALL_END(static_cast<Any *>(src));
42+
}
43+
};
44+
45+
template <> struct PODGetterSetter<std::nullptr_t> {
46+
static int32_t Getter(void *, MLCAny *ret) {
47+
MLC_SAFE_CALL_BEGIN();
48+
*static_cast<Any *>(ret) = nullptr;
49+
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
50+
}
51+
static int32_t Setter(void *addr, MLCAny *src) {
52+
MLC_SAFE_CALL_BEGIN();
53+
*static_cast<void **>(addr) = nullptr;
54+
MLC_SAFE_CALL_END(static_cast<Any *>(src));
55+
}
56+
};
57+
3458
struct TypeTable {
3559
using ObjPtr = std::unique_ptr<MLCObject, void (*)(MLCObject *)>;
3660

@@ -39,15 +63,15 @@ struct TypeTable {
3963
std::unordered_map<std::string, MLCTypeInfo *> type_key_to_info;
4064
std::unordered_map<std::string, std::unordered_map<int32_t, FuncObj *>> vtable;
4165
std::unordered_map<std::string, FuncObj *> global_funcs;
42-
std::unordered_map<const void *, details::PODArray> pool_pod_array;
66+
std::unordered_map<const void *, ::mlc::base::PODArray> pool_pod_array;
4367
std::unordered_map<const void *, ObjPtr> pool_obj_ptr;
4468
std::unordered_map<std::string, std::unique_ptr<DSOLibrary>> dso_library;
4569

4670
template <typename PODType> inline PODType *NewArray(int64_t size) {
4771
if (size == 0) {
4872
return nullptr;
4973
}
50-
details::PODArray owned(static_cast<void *>(std::malloc(size * sizeof(PODType))), std::free);
74+
::mlc::base::PODArray owned(static_cast<void *>(std::malloc(size * sizeof(PODType))), std::free);
5175
PODType *ptr = reinterpret_cast<PODType *>(owned.get());
5276
auto [it, success] = this->pool_pod_array.emplace(ptr, std::move(owned));
5377
if (!success) {
@@ -80,8 +104,8 @@ struct TypeTable {
80104
std::abort();
81105
}
82106
MLCObject *source_casted = reinterpret_cast<MLCObject *>(source);
83-
::mlc::ffi::details::IncRef(source_casted);
84-
it->second = ObjPtr(source_casted, ::mlc::ffi::details::DecRef);
107+
::mlc::base::IncRef(source_casted);
108+
it->second = ObjPtr(source_casted, ::mlc::base::DecRef);
85109
}
86110
}
87111

@@ -131,7 +155,8 @@ struct TypeTable {
131155
return (self == nullptr) ? TypeTable::Global() : static_cast<TypeTable *>(self);
132156
}
133157

134-
MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key) {
158+
MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key,
159+
MLCAttrGetterSetter getter, MLCAttrGetterSetter setter) {
135160
// Step 1.Check if the type is already registered
136161
if (auto it = this->type_key_to_info.find(type_key); it != this->type_key_to_info.end()) {
137162
MLCTypeInfo *ret = it->second;
@@ -156,6 +181,8 @@ struct TypeTable {
156181
info->type_index = type_index;
157182
info->type_key = this->NewArray(type_key);
158183
info->type_depth = (parent == nullptr) ? 0 : (parent->type_depth + 1);
184+
info->getter = getter;
185+
info->setter = setter;
159186
info->fields = nullptr;
160187
info->methods = nullptr;
161188
info->type_ancestors = this->NewArray<int32_t>(info->type_depth);
@@ -209,24 +236,24 @@ struct TypeTable {
209236
};
210237

211238
struct _POD_REG {
212-
inline static const int32_t _none = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCNone))
213-
.Method("__str__", &PODTraits<std::nullptr_t>::__str__);
214-
inline static const int32_t _int = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCInt))
215-
.Method("__str__", &PODTraits<int64_t>::__str__);
216-
inline static const int32_t _float = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCFloat))
217-
.Method("__str__", &PODTraits<double>::__str__);
218-
inline static const int32_t _ptr = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCPtr))
219-
.Method("__str__", &PODTraits<void *>::__str__);
239+
inline static const int32_t _none = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCNone))
240+
.Method("__str__", &base::PODTraits<std::nullptr_t>::__str__);
241+
inline static const int32_t _int = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCInt))
242+
.Method("__str__", &base::PODTraits<int64_t>::__str__);
243+
inline static const int32_t _float = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCFloat))
244+
.Method("__str__", &base::PODTraits<double>::__str__);
245+
inline static const int32_t _ptr = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCPtr))
246+
.Method("__str__", &base::PODTraits<void *>::__str__);
220247
inline static const int32_t _device =
221-
details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDevice))
222-
.Method("__str__", &PODTraits<DLDevice>::__str__)
248+
base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDevice))
249+
.Method("__str__", &base::PODTraits<DLDevice>::__str__)
223250
.Method("__init__", [](AnyView device) { return device.operator DLDevice(); });
224251
inline static const int32_t _dtype =
225-
details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDataType))
226-
.Method("__str__", &PODTraits<DLDataType>::__str__)
252+
base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDataType))
253+
.Method("__str__", &base::PODTraits<DLDataType>::__str__)
227254
.Method("__init__", [](AnyView dtype) { return dtype.operator DLDataType(); });
228-
inline static const int32_t _str = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCRawStr))
229-
.Method("__str__", &PODTraits<const char *>::__str__);
255+
inline static const int32_t _str = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCRawStr))
256+
.Method("__str__", &base::PODTraits<const char *>::__str__);
230257
};
231258

232259
inline TypeTable *TypeTable::New() {
@@ -235,7 +262,8 @@ inline TypeTable *TypeTable::New() {
235262
self->type_key_to_info.reserve(1024);
236263
self->num_types = static_cast<int32_t>(MLCTypeIndex::kMLCDynObjectBegin);
237264
#define MLC_TYPE_TABLE_INIT_TYPE(TypeIndex, UnderlyingType, Self) \
238-
Self->TypeRegister(-1, static_cast<int32_t>(TypeIndex), PODTraits<UnderlyingType>::Type2Str());
265+
Self->TypeRegister(-1, static_cast<int32_t>(TypeIndex), ::mlc::base::PODTraits<UnderlyingType>::Type2Str(), \
266+
PODGetterSetter<UnderlyingType>::Getter, PODGetterSetter<UnderlyingType>::Setter);
239267

240268
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCNone, std::nullptr_t, self);
241269
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCInt, int64_t, self);
@@ -315,7 +343,6 @@ inline void TypeInfoWrapper::SetMethods(int64_t new_num_methods, MLCTypeMethod *
315343
}
316344

317345
} // namespace registry
318-
} // namespace ffi
319346
} // namespace mlc
320347

321348
#endif // MLC_REGISTRY_H_

cpp/traceback.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <backtrace.h>
44
#include <cxxabi.h>
55
#include <iostream>
6-
#include <mlc/ffi/c_api.h>
6+
#include <mlc/c_api.h>
77

88
namespace mlc {
99
namespace ffi {

0 commit comments

Comments
 (0)