Skip to content

Commit ac7cb73

Browse files
committed
feat: add msg/reply for sending an execution to a liveview
1 parent 8a5ee2c commit ac7cb73

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

liveview_native/live_nx_iree/lib/live_nx_iree_web/live/home_live/home_live.ex

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule LiveNxIREEWeb.HomeLive do
1515

1616
dbg(self())
1717

18-
socket = assign(socket, bytecode: nil, function_signature: nil)
18+
socket = assign(socket, bytecode: nil, function_signature: nil, device: nil)
1919

2020
{:ok, socket}
2121
end
@@ -43,7 +43,7 @@ defmodule LiveNxIREEWeb.HomeLive do
4343
# end
4444

4545
@impl true
46-
def handle_info({:nx, :execute, function, input_templates, target_device}, socket) do
46+
def handle_info({:nx, :execute, function, input_templates, target_device, reply_to_pid}, socket) do
4747
fun =
4848
case function do
4949
{m, f, a} ->
@@ -55,10 +55,13 @@ defmodule LiveNxIREEWeb.HomeLive do
5555
"""
5656
end
5757

58-
backend_flag =
58+
{backend_flag, runtime_device} =
5959
case target_device do
60-
:metal -> "--iree-hal-target-backends=metal-spirv"
61-
:cpu -> "--iree-hal-target-backends=llvm-cpu"
60+
:metal ->
61+
{"--iree-hal-target-backends=metal-spirv", "metal://default"}
62+
63+
:cpu ->
64+
{"--iree-hal-target-backends=llvm-cpu", "local-sync://default"}
6265
end
6366

6467
compiler_flags = [
@@ -75,10 +78,19 @@ defmodule LiveNxIREEWeb.HomeLive do
7578
|> assign(:bytecode, Base.encode64(bytecode))
7679
|> assign(:output_container, output_container)
7780
|> assign(:function_signature, get_signature(function, input_templates, output_container))
81+
|> assign(:device, runtime_device)
82+
|> assign(:reply_to_pid, reply_to_pid)
7883

7984
{:noreply, socket}
8085
end
8186

87+
@impl true
88+
def handle_event("nx-executed", params, socket) do
89+
send(socket.assigns.reply_to_pid, {:nx, :executed, params})
90+
91+
{:noreply, assign(socket, :reply_to_pid, nil)}
92+
end
93+
8294
defp get_signature({mod, fun, _a}, input_templates, output_container) do
8395
"#{inspect(mod)}.#{fun}(#{to_flat_type(input_templates)}) -> #{to_flat_type(output_container)}"
8496
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<NxFunction signature={@function_signature} bytecode={@bytecode}/>
1+
<NxFunction on-execution="nx-executed" signature={@function_signature} bytecode={@bytecode} device={@device} />

liveview_native/live_nx_iree/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule LiveNxIREE.MixProject do
1919
def application do
2020
[
2121
mod: {LiveNxIREE.Application, []},
22-
extra_applications: [:logger, :runtime_tools]
22+
extra_applications: [:logger, :runtime_tools, :observer]
2323
]
2424
end
2525

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
uuid = "B200013E-0754-4DEE-8498-6AF0A1807EEA"
4+
type = "1"
5+
version = "2.0">
6+
</Bucket>

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,32 @@ 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
17+
@LiveAttribute("trigger") private var trigger: Bool = false
18+
@Event("on-execution", type: "change") private var change
1619

1720
var body: some View {
18-
if signature != nil {
19-
Text(signature!)
20-
.padding()
21+
VStack {
22+
if signature != nil {
23+
Text(signature!)
24+
.padding()
25+
}
26+
}
27+
.onAppear() {
28+
run()
29+
}
30+
.onChange(of: bytecode) {
31+
run() // Run the function when bytecode changes
2132
}
2233
}
34+
35+
private func run() {
36+
if bytecode == nil {
37+
return
38+
}
39+
// Custom logic that should run when execute is set to true
40+
print("Executing function \(signature ?? "None") on device: \(device ?? "None")")
41+
42+
change(value: "Sending something back")
43+
}
2344
}

0 commit comments

Comments
 (0)