Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions arc/go/analyzer/expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,13 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "ox_pt_1",
Type: types.Chan(types.I32()),
ID: 20001,
},
"ox_pt_2": symbol.Symbol{
Kind: symbol.KindChannel,
Name: "ox_pt_2",
Type: types.Chan(types.I32()),
ID: 20002,
},
}
ctx := context.CreateRoot(bCtx, ast, resolver)
Expand All @@ -957,11 +959,13 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "ox_pt_1",
Type: types.Chan(types.I32()),
ID: 20003,
},
"ox_pt_2": symbol.Symbol{
Kind: symbol.KindChannel,
Name: "ox_pt_2",
Type: types.Chan(types.F32()),
ID: 20004,
},
}
ctx := context.CreateRoot(bCtx, ast, resolver)
Expand All @@ -982,6 +986,7 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "ox_pt_1",
Type: types.Chan(types.I32()),
ID: 20005,
},
}
ctx := context.CreateRoot(bCtx, ast, resolver)
Expand All @@ -1002,6 +1007,7 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "sensor",
Type: types.Chan(types.F32()),
ID: 20006,
},
}),
Entry("comparison", `
Expand All @@ -1013,16 +1019,17 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "sensor",
Type: types.Chan(types.F32()),
ID: 20007,
},
}),
Entry("multiple channels in expression", `
func testFunc() f64 {
return (temp1 + temp2 + temp3) / 3
}
`, symbol.MapResolver{
"temp1": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp1", Type: types.Chan(types.F64())},
"temp2": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp2", Type: types.Chan(types.F64())},
"temp3": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp3", Type: types.Chan(types.F64())},
"temp1": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp1", Type: types.Chan(types.F64()), ID: 20008},
"temp2": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp2", Type: types.Chan(types.F64()), ID: 20009},
"temp3": symbol.Symbol{Kind: symbol.KindChannel, Name: "temp3", Type: types.Chan(types.F64()), ID: 20010},
}),
)

Expand All @@ -1032,6 +1039,7 @@ var _ = Describe("Expressions", func() {
Kind: symbol.KindChannel,
Name: "sensor",
Type: types.Chan(types.F32()),
ID: 20011,
},
}
expectFailure(`
Expand Down
5 changes: 5 additions & 0 deletions arc/go/analyzer/statement/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ func analyzeStatefulVariable(ctx context.Context[parser.IStatefulVariableContext
})
return
}
// Stateful variables store VALUES, not channel references.
// If initialized from a channel, unwrap to get the value type.
if varType.Kind == types.KindChan {
varType = varType.Unwrap()
}
_, err := ctx.Scope.Add(ctx, symbol.Symbol{
Name: name,
Kind: symbol.KindStatefulVariable,
Expand Down
143 changes: 143 additions & 0 deletions arc/go/runtime/wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2542,5 +2542,148 @@ input_ch -> checker{} -> output_ch
result = h.Output("checker_0", 0)
Expect(telem.UnmarshalSeries[uint8](result)[0]).To(Equal(uint8(0)))
})

It("Should write to separate channels when function with channel config is used multiple times", func() {
resolver := symbol.MapResolver{
"input_1": {
Name: "input_1",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 101,
},
"input_2": {
Name: "input_2",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 102,
},
"counter_1": {
Name: "counter_1",
Kind: symbol.KindChannel,
Type: types.Chan(types.F32()),
ID: 201,
},
"counter_2": {
Name: "counter_2",
Kind: symbol.KindChannel,
Type: types.Chan(types.F32()),
ID: 202,
},
"sink_1": {
Name: "sink_1",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 301,
},
"sink_2": {
Name: "sink_2",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 302,
},
}

source := `
func increment{
counter chan f32
} (trigger u8) u8 {
if trigger != 0 {
counter = counter + 1.0
}
return 0
}

input_1 -> increment{counter=counter_1} -> sink_1
input_2 -> increment{counter=counter_2} -> sink_2
`
h := newTextHarness(ctx, source, resolver, []state.ChannelDigest{
{Key: 101, DataType: telem.Uint8T},
{Key: 102, DataType: telem.Uint8T},
{Key: 201, DataType: telem.Float32T},
{Key: 202, DataType: telem.Float32T},
{Key: 301, DataType: telem.Uint8T},
{Key: 302, DataType: telem.Uint8T},
})
defer h.Close()

fr := telem.Frame[uint32]{}
fr = fr.Append(201, telem.NewSeriesV[float32](0.0))
fr = fr.Append(202, telem.NewSeriesV[float32](0.0))
h.state.Ingest(fr)

h.SetInput("on_input_1_0", 0, telem.NewSeriesV[uint8](1), telem.NewSeriesSecondsTSV(1))
h.SetInput("on_input_2_0", 0, telem.NewSeriesV[uint8](1), telem.NewSeriesSecondsTSV(1))
h.Execute(ctx, "increment_0")
h.Execute(ctx, "increment_1")

outFr, changed := h.state.Flush(telem.Frame[uint32]{})
Expect(changed).To(BeTrue())
Expect(outFr.Get(201).Series).To(HaveLen(1), "counter_1 should have been written")
Expect(telem.UnmarshalSeries[float32](outFr.Get(201).Series[0])[0]).To(Equal(float32(1.0)))
Expect(outFr.Get(202).Series).To(HaveLen(1), "counter_2 should have been written")
Expect(telem.UnmarshalSeries[float32](outFr.Get(202).Series[0])[0]).To(Equal(float32(1.0)))
})

It("Should not write to channel when stateful variable initialized from channel is modified", func() {
resolver := symbol.MapResolver{
"input_ch": {
Name: "input_ch",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 100,
},
"counter_ch": {
Name: "counter_ch",
Kind: symbol.KindChannel,
Type: types.Chan(types.F32()),
ID: 200,
},
"sink_ch": {
Name: "sink_ch",
Kind: symbol.KindChannel,
Type: types.Chan(types.U8()),
ID: 300,
},
}

source := `
func count_local (trigger u8) u8 {
counter $= counter_ch
prev $= trigger
if trigger != 0 and prev == 0 {
counter = counter + 1.0
}
prev = trigger
return 0
}

input_ch -> count_local{} -> sink_ch
`
h := newTextHarness(ctx, source, resolver, []state.ChannelDigest{
{Key: 100, DataType: telem.Uint8T},
{Key: 200, DataType: telem.Float32T},
{Key: 300, DataType: telem.Uint8T},
})
defer h.Close()

fr := telem.Frame[uint32]{}
fr = fr.Append(200, telem.NewSeriesV[float32](5.0))
h.state.Ingest(fr)

h.SetInput("on_input_ch_0", 0, telem.NewSeriesV[uint8](0), telem.NewSeriesSecondsTSV(1))
h.Execute(ctx, "count_local_0")

outFr, _ := h.state.Flush(telem.Frame[uint32]{})
Expect(outFr.Get(200).Series).To(BeEmpty())

fr = telem.Frame[uint32]{}
fr = fr.Append(200, telem.NewSeriesV[float32](5.0))
h.state.Ingest(fr)
h.SetInput("on_input_ch_0", 0, telem.NewSeriesV[uint8](1), telem.NewSeriesSecondsTSV(2))
h.Execute(ctx, "count_local_0")

outFr, _ = h.state.Flush(telem.Frame[uint32]{})
Expect(outFr.Get(200).Series).To(BeEmpty())
})
})
})
2 changes: 1 addition & 1 deletion arc/go/text/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func analyzeFunctionNode(
n := ir.Node{
Key: key,
Type: name,
Channels: sym.Channels,
Channels: sym.Channels.Copy(),
Config: slices.Clone(sym.Type.Config),
Outputs: slices.Clone(sym.Type.Outputs),
Inputs: slices.Clone(sym.Type.Inputs),
Expand Down
Loading
Loading