Skip to content

Commit

Permalink
Merge pull request #512 from nevalang/fix_array_inport_iteration_order
Browse files Browse the repository at this point in the history
fix(irgen): sort port addrs
  • Loading branch information
emil14 authored Mar 11, 2024
2 parents 2c7ee29 + 487a951 commit 596802d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@
"cwd": "${workspaceFolder}/examples",
"args": ["run", "11_99_bottles"]
},
{
"name": "Neva CLI 113",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/neva",
"cwd": "${workspaceFolder}/examples",
"args": ["run", "13_subtract"]
},
// run (e2e tests)
{
"name": "E2E struct_selector_on_port_addr",
Expand Down
26 changes: 22 additions & 4 deletions internal/compiler/irgen/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package irgen

import (
"fmt"
"sort"
"strings"

"github.com/nevalang/neva/internal/runtime/ir"
Expand Down Expand Up @@ -153,7 +154,6 @@ func (g Generator) processSenderSide(

return irSenderSide, nil
}

func (Generator) insertAndReturnInports(
nodeCtx nodeContext,
result *ir.Program,
Expand All @@ -175,14 +175,30 @@ func (Generator) insertAndReturnInports(
inports = append(inports, *addr)
}

sortPortAddrs(inports)

return inports
}

// sortPortAddrs sorts port addresses by path, port and idx,
// this is very important because runtime function calls depends on this order.
func sortPortAddrs(addrs []ir.PortAddr) {
sort.Slice(addrs, func(i, j int) bool {
if addrs[i].Path != addrs[j].Path {
return addrs[i].Path < addrs[j].Path
}
if addrs[i].Port != addrs[j].Port {
return addrs[i].Port < addrs[j].Port
}
return addrs[i].Idx < addrs[j].Idx
})
}

func (Generator) insertAndReturnOutports(
nodeCtx nodeContext,
result *ir.Program,
) []ir.PortAddr {
runtimeFuncOutportAddrs := make([]ir.PortAddr, 0, len(nodeCtx.portsUsage.out))
outports := make([]ir.PortAddr, 0, len(nodeCtx.portsUsage.out))

// In a valid (desugared) program all outports are used so it's safe to depend on nodeCtx and not use component's IO.
// Actually we can't use IO because we need to know how many slots are used.
Expand All @@ -196,10 +212,12 @@ func (Generator) insertAndReturnOutports(
PortAddr: *irAddr,
BufSize: 0,
})
runtimeFuncOutportAddrs = append(runtimeFuncOutportAddrs, *irAddr)
outports = append(outports, *irAddr)
}

return runtimeFuncOutportAddrs
sortPortAddrs(outports)

return outports
}

// mapReceiverSide maps compiler connection side to ir connection side 1-1 just making the port addr's path absolute
Expand Down
43 changes: 42 additions & 1 deletion internal/compiler/irgen/network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package irgen

import "testing"
import (
"testing"

"github.com/nevalang/neva/internal/runtime/ir"
"github.com/stretchr/testify/require"
)

func Test_joinNodePath(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -29,3 +34,39 @@ func Test_joinNodePath(t *testing.T) {
})
}
}

func Test_sortPortAddrs(t *testing.T) {
tests := []struct {
name string
addrs []ir.PortAddr
want []ir.PortAddr
}{
{
name: "messed up order",
addrs: []ir.PortAddr{
{Path: "b", Port: "A", Idx: 1},
{Path: "b", Port: "A", Idx: 0},
{Path: "a", Port: "B", Idx: 0},
{Path: "a", Port: "B", Idx: 1},
{Path: "a", Port: "A", Idx: 2},
{Path: "a", Port: "A", Idx: 1},
{Path: "a", Port: "A", Idx: 0},
},
want: []ir.PortAddr{
{Path: "a", Port: "A", Idx: 0},
{Path: "a", Port: "A", Idx: 1},
{Path: "a", Port: "A", Idx: 2},
{Path: "a", Port: "B", Idx: 0},
{Path: "a", Port: "B", Idx: 1},
{Path: "b", Port: "A", Idx: 0},
{Path: "b", Port: "A", Idx: 1},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sortPortAddrs(tt.addrs)
require.Equal(t, tt.want, tt.addrs)
})
}
}
4 changes: 2 additions & 2 deletions internal/runtime/ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type FuncCall struct {

// FuncIO represents the input/output ports of a function.
type FuncIO struct {
In []PortAddr
Out []PortAddr
In []PortAddr // Must be ordered by path -> port -> idx
Out []PortAddr // Must be ordered by path -> port -> idx
}

// Msg represents a message.
Expand Down

0 comments on commit 596802d

Please sign in to comment.