Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake committed Mar 5, 2024
1 parent 95e0eaa commit aa53987
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
7 changes: 6 additions & 1 deletion internal/engine/wazevo/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ type Compiler struct {
execCtxPtrValue, moduleCtxPtrValue ssa.Value
}

// knownSafeBound represents a known safe bound for a value.
type knownSafeBound struct {
bound uint64
// bound is a constant upper bound for the value.
bound uint64
// absoluteAddr is the absolute address of the value.
absoluteAddr ssa.Value
}

Expand Down Expand Up @@ -439,6 +442,8 @@ func (c *Compiler) recordKnownSafeBound(v ssa.ValueID, safeBound uint64, absolut

// clearSafeBounds clears the known safe bounds. This must be called
// after the compilation of each block.
// If `hard` is true, it clears the bounds. Otherwise, it clears only the absolute addresses,
// so that the constant bounds are still valid.
func (c *Compiler) clearSafeBounds(hard bool) {
if hard {
for _, v := range c.knownSafeBoundsSet {
Expand Down
36 changes: 30 additions & 6 deletions internal/engine/wazevo/frontend/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2996,12 +2996,36 @@ func TestCompiler_getKnownSafeBound(t *testing.T) {
}

func TestCompiler_clearSafeBounds(t *testing.T) {
c := &Compiler{}
c.knownSafeBounds = []knownSafeBound{{bound: 1}, {}, {bound: 2}, {}, {}, {bound: 3}}
c.knownSafeBoundsSet = []ssa.ValueID{0, 2, 5}
c.clearSafeBounds(true)
require.Equal(t, 0, len(c.knownSafeBoundsSet))
require.Equal(t, []knownSafeBound{{}, {}, {}, {}, {}, {}}, c.knownSafeBounds)
t.Run("hard", func(t *testing.T) {
c := &Compiler{}
c.knownSafeBounds = []knownSafeBound{{bound: 1}, {}, {bound: 2}, {}, {}, {bound: 3}}
c.knownSafeBoundsSet = []ssa.ValueID{0, 2, 5}
c.clearSafeBounds(true)
require.Equal(t, 0, len(c.knownSafeBoundsSet))
require.Equal(t, []knownSafeBound{{}, {}, {}, {}, {}, {}}, c.knownSafeBounds)
})
t.Run("not hard", func(t *testing.T) {
c := &Compiler{}
c.knownSafeBounds = []knownSafeBound{
{bound: 1, absoluteAddr: ssa.Value(1)},
{},
{bound: 2, absoluteAddr: ssa.Value(2)},
{},
{},
{bound: 3, absoluteAddr: ssa.Value(3)},
}
c.knownSafeBoundsSet = []ssa.ValueID{0, 2, 5}
c.clearSafeBounds(false)
require.Equal(t, 3, len(c.knownSafeBoundsSet))
require.Equal(t, []knownSafeBound{
{bound: 1, absoluteAddr: ssa.ValueInvalid},
{},
{bound: 2, absoluteAddr: ssa.ValueInvalid},
{},
{},
{bound: 3, absoluteAddr: ssa.ValueInvalid},
}, c.knownSafeBounds)
})
}

func TestKnownSafeBound_valid(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions internal/engine/wazevo/frontend/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,10 @@ func (c *Compiler) lowerCurrentOpcode() {
builder.Seal(thenBlk)
builder.Seal(elseBlk)
case wasm.OpcodeElse:
c.clearSafeBounds(true) // Reset the safe bounds since we are entering the Else block.
// Reset the safe bounds since we are entering the Else block.
// TODO: we should be able to inherit the safe bounds from the parent block. So, right now, this means that
// else block is a little bit more slow than the then block.
c.clearSafeBounds(true)

ifctrl := state.ctrlPeekAt(0)
if unreachable := state.unreachable; unreachable && state.unreachableDepth > 0 {
Expand Down Expand Up @@ -1480,7 +1483,10 @@ func (c *Compiler) lowerCurrentOpcode() {
builder.Seal(followingBlk)

if unreachable || followingBlk.Preds() != 1 {
c.clearSafeBounds(true) // Reset the safe bounds since we are exiting the block.
// If we can reach this block without being unreachable, and it has only one predecessor,
// this means that we get here from the unique block contiguously. Therefore, we can
// keep using the same safe bounds information. Otherwise, we need to reset it.
c.clearSafeBounds(true)
}

// Ready to start translating the following block.
Expand Down Expand Up @@ -3773,7 +3779,8 @@ func (c *Compiler) reloadMemoryBaseLen() {
_ = c.getMemoryLenValue(true)

// This function being called means that the memory base might have changed.
// Therefore, we need to clear the known safe bounds because we cache the absolute address of the memory access per each base offset.
// Therefore, we need to clear the absolute addresses recorded in the known safe bounds (passing false to clearSafeBounds)
// because we cache the absolute address of the memory access per each base offset.
c.clearSafeBounds(false)
}

Expand Down

0 comments on commit aa53987

Please sign in to comment.