Skip to content

Commit c6bca28

Browse files
committed
feat: add actual cpp bridge
1 parent ac7cb73 commit c6bca28

File tree

9 files changed

+104
-26
lines changed

9 files changed

+104
-26
lines changed

cmake/src/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class IREETensor {
7474

7575
iree_vm_instance_t* create_instance();
7676
iree_hal_driver_registry_t* get_driver_registry();
77-
iree_hal_device_t* create_device(const std::string& device_uri);
77+
iree_hal_device_t* create_device(iree_hal_driver_registry_t* registry, const std::string& device_uri);
7878

7979
std::pair<iree_status_t, std::optional<std::vector<iree::runtime::IREETensor*>>>
8080
call(iree_vm_instance_t* i, iree_hal_device_t*, std::string, unsigned char*, size_t, std::vector<iree::runtime::IREETensor*>);

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE.xcodeproj/project.pbxproj

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@
16571657
buildPhases = (
16581658
D43BFCA900A98D1510808FD6 /* Sources */,
16591659
7288EE5E619E83B96AB819F8 /* Resources */,
1660+
373496942C78272A00FD2E6F /* Copy libnx_ireee_runtime.so */,
16601661
22F70DB26730B53329332C83 /* Frameworks */,
16611662
);
16621663
buildRules = (
@@ -1719,6 +1720,35 @@
17191720
};
17201721
/* End PBXResourcesBuildPhase section */
17211722

1723+
/* Begin PBXShellScriptBuildPhase section */
1724+
373496942C78272A00FD2E6F /* Copy libnx_ireee_runtime.so */ = {
1725+
isa = PBXShellScriptBuildPhase;
1726+
buildActionMask = 2147483647;
1727+
files = (
1728+
);
1729+
inputFileListPaths = (
1730+
);
1731+
inputPaths = (
1732+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/host/libnx_iree_runtime.so",
1733+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/ios/libnx_iree_runtime.so",
1734+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/ios_simulator/libnx_iree_runtime.so",
1735+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/visionos/libnx_iree_runtime.so",
1736+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/visionos_simulator/libnx_iree_runtime.so",
1737+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/tvos/libnx_iree_runtime.so",
1738+
"$(SRCROOT)/LiveNxIREE/nx_iree/lib/tvos_simulator/libnx_iree_runtime.so",
1739+
);
1740+
name = "Copy libnx_ireee_runtime.so";
1741+
outputFileListPaths = (
1742+
);
1743+
outputPaths = (
1744+
"$(BUILT_PRODUCTS_DIR)/libnx_iree_runtime.so",
1745+
);
1746+
runOnlyForDeploymentPostprocessing = 0;
1747+
shellPath = /bin/sh;
1748+
shellScript = "# Define the source directories for the different platforms\nIOS_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/ios\"\nIOS_SIMULATOR_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/ios_simulator\"\nTVOS_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/tvos\"\nTVOS_SIMULATOR_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/tvos_simulator\"\nVISIONOS_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/visionos\"\nVISIONOS_SIMULATOR_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/visionos_simulator\"\nHOST_DIR=\"${SRCROOT}/${PROJECT_NAME}/nx_iree/lib/host\"\n\n# Define the destination directory inside the app bundle's Frameworks folder\nDESTINATION=\"${BUILT_PRODUCTS_DIR}\"\n\n# Create the destination directory if it doesn't exist\nmkdir -p \"${DESTINATION}\"\n\n# Check the SDK and copy the appropriate library\nif [[ \"${SDK_NAME}\" == *\"iphoneos\"* ]]; then\n cp \"${IOS_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"iphonesimulator\"* ]]; then\n cp \"${IOS_SIMULATOR_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"appletvos\"* ]]; then\n cp \"${TVOS_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"appletvsimulator\"* ]]; then\n cp \"${TVOS_SIMULATOR_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"xros\"* ]]; then\n cp \"${VISIONOS_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"xrsimulator\"* ]]; then\n cp \"${VISIONOS_SIMULATOR_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelif [[ \"${SDK_NAME}\" == *\"macosx\"* ]]; then\n cp \"${HOST_DIR}/libnx_iree_runtime.so\" \"${DESTINATION}/\"\nelse\n echo \"Unsupported SDK: ${SDK_NAME}\"\n exit 1\nfi\n\n# Ensure the copied file has the correct permissions\nchmod 755 \"${DESTINATION}/libnx_iree_runtime.so\"\n";
1749+
};
1750+
/* End PBXShellScriptBuildPhase section */
1751+
17221752
/* Begin PBXSourcesBuildPhase section */
17231753
D43BFCA900A98D1510808FD6 /* Sources */ = {
17241754
isa = PBXSourcesBuildPhase;
@@ -1793,7 +1823,7 @@
17931823
MARKETING_VERSION = 1.0;
17941824
ONLY_ACTIVE_ARCH = YES;
17951825
OTHER_LDFLAGS = "-lnx_iree_runtime";
1796-
"OTHER_LDFLAGS[arch=*]" = "";
1826+
"OTHER_LDFLAGS[arch=*]" = "-lnx_iree_runtime";
17971827
SWIFT_OBJC_BRIDGING_HEADER = "$(PROJECT_NAME)/$(PROJECT_NAME)-Bridging-Header.h";
17981828
SYSTEM_HEADER_SEARCH_PATHS = "";
17991829
TVOS_DEPLOYMENT_TARGET = 16.0;

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/LiveNxIREE.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import SwiftUI
77

88
@main
99
struct LiveNxIREE: App {
10+
init() {
11+
// Allocate memory for the pointers
12+
let vmInstance = UnsafeMutablePointer<iree_vm_instance_t>.allocate(capacity: 1)
13+
let driverRegistry = UnsafeMutablePointer<iree_hal_driver_registry_t>.allocate(capacity: 1)
14+
let errorMessage = UnsafeMutablePointer<CChar>.allocate(capacity: 256)
15+
16+
// Call the initialization function
17+
let result = nx_iree_initialize(vmInstance, driverRegistry, errorMessage)
18+
19+
if result != 0 {
20+
// Handle the error
21+
let errorString = String(cString: errorMessage)
22+
print("Error initializing nx_iree: \(errorString)")
23+
} else {
24+
globalVmInstance = vmInstance
25+
globalDriverRegistry = driverRegistry
26+
print("nx_iree initialized successfully.")
27+
}
28+
}
29+
1030
var body: some Scene {
1131
WindowGroup {
1232
ContentView()

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/NxAddon.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@
88
import LiveViewNative
99
import SwiftUI
1010

11+
class iree_vm_instance_t {}
12+
class iree_hal_driver_registry_t {}
13+
class iree_hal_device_t {}
14+
15+
var globalVmInstance: UnsafeMutablePointer<iree_vm_instance_t>?
16+
var globalDriverRegistry: UnsafeMutablePointer<iree_hal_driver_registry_t>?
17+
18+
@_silgen_name("nx_iree_initialize")
19+
func nx_iree_initialize(
20+
_ vm_instance: UnsafeMutablePointer<iree_vm_instance_t>,
21+
_ driver_registry: UnsafeMutablePointer<iree_hal_driver_registry_t>,
22+
_ error_message: UnsafeMutablePointer<CChar>) -> Int
23+
24+
@_silgen_name("nx_iree_create_device")
25+
func nx_iree_create_device(
26+
_ driver_registry: UnsafeMutablePointer<iree_hal_driver_registry_t>,
27+
_ name: UnsafePointer<Int8>) -> UnsafeMutablePointer<iree_hal_device_t>
28+
29+
@_silgen_name("nx_iree_call")
30+
func nx_iree_call(
31+
_ vm_instance: UnsafeMutablePointer<iree_vm_instance_t>,
32+
_ device: UnsafeMutablePointer<iree_hal_device_t>,
33+
_ bytecode_size: UInt64,
34+
_ bytecode: UnsafePointer<CUnsignedChar>,
35+
_ num_inputs: UInt64,
36+
_ serialized_inputs: UnsafePointer<UnsafePointer<CChar>>,
37+
_ num_outputs: UInt64,
38+
_ serialized_outputs: UnsafePointer<UnsafePointer<CChar>>,
39+
_ error_message: UnsafeMutablePointer<CChar>) -> Int
40+
41+
42+
1143
public extension Addons {
1244
@Addon
1345
struct NxAddon<Root: RootRegistry> {

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/NxAddon/NxFunctionView.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct NxFunctionView<Root: RootRegistry>: View {
1313
@_documentation(visibility: public)
1414
@LiveAttribute("bytecode") private var bytecode: String? = nil
1515
@LiveAttribute("signature") private var signature: String? = nil
16-
@LiveAttribute("device") private var device: String? = nil
16+
@LiveAttribute("device") private var deviceURI: String? = nil
1717
@LiveAttribute("trigger") private var trigger: Bool = false
1818
@Event("on-execution", type: "change") private var change
1919

@@ -36,9 +36,15 @@ struct NxFunctionView<Root: RootRegistry>: View {
3636
if bytecode == nil {
3737
return
3838
}
39-
// Custom logic that should run when execute is set to true
40-
print("Executing function \(signature ?? "None") on device: \(device ?? "None")")
4139

42-
change(value: "Sending something back")
40+
if let vmInstance = globalVmInstance,
41+
let driverRegistry = globalDriverRegistry {
42+
print("Executing function \(signature ?? "None") on device: \(deviceURI ?? "None")")
43+
change(value: "Sending something back")
44+
} else {
45+
print("vm instance: \(globalVmInstance)")
46+
print("driver registry: \(globalDriverRegistry)")
47+
print("IREE components are not initialized.")
48+
}
4349
}
4450
}

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/c_src/nx_iree.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@
66
//
77

88
#include "nx_iree.h"
9+
#include <nx_iree/runtime.h>
910

1011
#include <string>
1112
#include <vector>
1213

13-
int func(int x) {
14-
return x;
15-
}
16-
17-
/*
18-
int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry_t* driver_registry, iree_hal_device_t* device, char* device_uri, char* error_message) {
14+
int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry_t* driver_registry, char* error_message) {
1915
vm_instance = create_instance();
2016
driver_registry = get_driver_registry();
2117
auto status = register_all_drivers(driver_registry);
2218

23-
if (!is_ok(status)) {
19+
if (!is_ok(status) && !iree_status_is_already_exists(status)) {
2420
if (error_message) {
2521
std::string msg = get_status_message(status);
2622
strncpy(error_message, msg.c_str(), msg.length());
@@ -29,18 +25,13 @@ int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry
2925
return 1;
3026
}
3127

32-
33-
device = create_device(std::string(device_uri));
34-
35-
if (!device) {
36-
const char* msg = "Unable to initialize device\0";
37-
strncpy(error_message, msg, strlen(msg));
38-
return 1;
39-
}
40-
4128
return 0;
4229
}
4330

31+
iree_hal_device_t* nx_iree_create_device(iree_hal_driver_registry_t* registry, char* device_uri) {
32+
return create_device(registry, std::string(device_uri));
33+
}
34+
4435
int nx_iree_call(iree_vm_instance_t* vm_instance, iree_hal_device_t* device, uint64_t bytecode_size, unsigned char* bytecode, uint64_t num_inputs, char** serialized_inputs, uint64_t num_outputs, char** serialized_outputs, char* error_message) {
4536
std::vector<iree::runtime::IREETensor*> inputs;
4637

@@ -72,5 +63,3 @@ int nx_iree_call(iree_vm_instance_t* vm_instance, iree_hal_device_t* device, uin
7263
return 0;
7364
}
7465

75-
}
76-
*/

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/c_src/nx_iree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
extern "C" {
1616
#endif
1717

18-
int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry_t* driver_registry, iree_hal_device_t* device, char* device_uri, char* error_message);
18+
int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry_t* driver_registry, char* error_message);
19+
iree_hal_device_t* nx_iree_create_device(char* device_uri);
1920
int nx_iree_call(iree_vm_instance_t* vm_instance, iree_hal_device_t* device, uint64_t bytecode_size, unsigned char* bytecode, uint64_t num_inputs, char** serialized_inputs, uint64_t num_outputs, char** serialized_outputs, char* error_message);
2021

2122

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/nx_iree/include/nx_iree/runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class IREETensor {
7474

7575
iree_vm_instance_t* create_instance();
7676
iree_hal_driver_registry_t* get_driver_registry();
77-
iree_hal_device_t* create_device(const std::string& device_uri);
77+
iree_hal_device_t* create_device(iree_hal_driver_registry_t *registry, const std::string& device_uri);
7878

7979
std::pair<iree_status_t, std::optional<std::vector<iree::runtime::IREETensor*>>>
8080
call(iree_vm_instance_t* i, iree_hal_device_t*, std::string, unsigned char*, size_t, std::vector<iree::runtime::IREETensor*>);

0 commit comments

Comments
 (0)