| 1 |
package ssa |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 7 |
) |
| 8 |
|
| 9 |
func TestBuilder_passes(t *testing.T) { |
| 10 |
for _, tc := range []struct { |
| 11 |
name string |
| 12 |
// pass is the optimization pass to run. |
| 13 |
pass, |
| 14 |
// postPass is run after the pass is executed, and can be used to test a pass that depends on another pass. |
| 15 |
postPass func(b *builder) |
| 16 |
// setup creates the SSA function in the given *builder. |
| 17 |
// TODO: when we have the text SSA IR parser, we can eliminate this `setup`, |
| 18 |
// we could directly decode the *builder from the `before` string. I am still |
| 19 |
// constantly changing the format, so let's keep setup for now. |
| 20 |
// `verifier` is executed after executing pass, and can be used to |
| 21 |
// do the additional verification of the state of SSA function in addition to `after` text result. |
| 22 |
setup func(*builder) (verifier func(t *testing.T)) |
| 23 |
// before is the expected SSA function after `setup` is executed. |
| 24 |
before, |
| 25 |
// after is the expected output after optimization pass. |
| 26 |
after string |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "dead block", |
| 30 |
pass: passDeadBlockEliminationOpt, |
| 31 |
setup: func(b *builder) func(*testing.T) { |
| 32 |
entry := b.AllocateBasicBlock() |
| 33 |
value := entry.AddParam(b, TypeI32) |
| 34 |
|
| 35 |
middle1, middle2 := b.AllocateBasicBlock(), b.AllocateBasicBlock() |
| 36 |
end := b.AllocateBasicBlock() |
| 37 |
|
| 38 |
b.SetCurrentBlock(entry) |
| 39 |
{ |
| 40 |
brz := b.AllocateInstruction() |
| 41 |
brz.AsBrz(value, ValuesNil, middle1) |
| 42 |
b.InsertInstruction(brz) |
| 43 |
|
| 44 |
jmp := b.AllocateInstruction() |
| 45 |
jmp.AsJump(ValuesNil, middle2) |
| 46 |
b.InsertInstruction(jmp) |
| 47 |
} |
| 48 |
|
| 49 |
b.SetCurrentBlock(middle1) |
| 50 |
{ |
| 51 |
jmp := b.AllocateInstruction() |
| 52 |
jmp.AsJump(ValuesNil, end) |
| 53 |
b.InsertInstruction(jmp) |
| 54 |
} |
| 55 |
|
| 56 |
b.SetCurrentBlock(middle2) |
| 57 |
{ |
| 58 |
jmp := b.AllocateInstruction() |
| 59 |
jmp.AsJump(ValuesNil, end) |
| 60 |
b.InsertInstruction(jmp) |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
unreachable := b.AllocateBasicBlock() |
| 65 |
b.SetCurrentBlock(unreachable) |
| 66 |
jmp := b.AllocateInstruction() |
| 67 |
jmp.AsJump(ValuesNil, end) |
| 68 |
b.InsertInstruction(jmp) |
| 69 |
} |
| 70 |
|
| 71 |
b.SetCurrentBlock(end) |
| 72 |
{ |
| 73 |
jmp := b.AllocateInstruction() |
| 74 |
jmp.AsJump(ValuesNil, middle1) |
| 75 |
b.InsertInstruction(jmp) |
| 76 |
} |
| 77 |
|
| 78 |
b.Seal(entry) |
| 79 |
b.Seal(middle1) |
| 80 |
b.Seal(middle2) |
| 81 |
b.Seal(end) |
| 82 |
return nil |
| 83 |
}, |
| 84 |
before: ` |
| 85 |
blk0: (v0:i32) |
| 86 |
Brz v0, blk1 |
| 87 |
Jump blk2 |
| 88 |
|
| 89 |
blk1: () <-- (blk0,blk3) |
| 90 |
Jump blk3 |
| 91 |
|
| 92 |
blk2: () <-- (blk0) |
| 93 |
Jump blk3 |
| 94 |
|
| 95 |
blk3: () <-- (blk1,blk2,blk4) |
| 96 |
Jump blk1 |
| 97 |
|
| 98 |
blk4: () |
| 99 |
Jump blk3 |
| 100 |
`, |
| 101 |
after: ` |
| 102 |
blk0: (v0:i32) |
| 103 |
Brz v0, blk1 |
| 104 |
Jump blk2 |
| 105 |
|
| 106 |
blk1: () <-- (blk0,blk3) |
| 107 |
Jump blk3 |
| 108 |
|
| 109 |
blk2: () <-- (blk0) |
| 110 |
Jump blk3 |
| 111 |
|
| 112 |
blk3: () <-- (blk1,blk2) |
| 113 |
Jump blk1 |
| 114 |
`, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "redundant phis", |
| 118 |
pass: passRedundantPhiEliminationOpt, |
| 119 |
setup: func(b *builder) func(*testing.T) { |
| 120 |
entry, loopHeader, end := b.AllocateBasicBlock(), b.AllocateBasicBlock(), b.AllocateBasicBlock() |
| 121 |
|
| 122 |
loopHeader.AddParam(b, TypeI32) |
| 123 |
var1 := b.DeclareVariable(TypeI32) |
| 124 |
|
| 125 |
b.SetCurrentBlock(entry) |
| 126 |
{ |
| 127 |
constInst := b.AllocateInstruction() |
| 128 |
constInst.AsIconst32(0xff) |
| 129 |
b.InsertInstruction(constInst) |
| 130 |
iConst := constInst.Return() |
| 131 |
b.DefineVariable(var1, iConst, entry) |
| 132 |
|
| 133 |
jmp := b.AllocateInstruction() |
| 134 |
args := b.varLengthPool.Allocate(1) |
| 135 |
args = args.Append(&b.varLengthPool, iConst) |
| 136 |
jmp.AsJump(args, loopHeader) |
| 137 |
b.InsertInstruction(jmp) |
| 138 |
} |
| 139 |
b.Seal(entry) |
| 140 |
|
| 141 |
b.SetCurrentBlock(loopHeader) |
| 142 |
{ |
| 143 |
// At this point, loop is not sealed, so PHI will be added to this header. However, the only |
| 144 |
// input to the PHI is iConst above, so there must be an alias to iConst from the PHI value. |
| 145 |
value := b.MustFindValue(var1) |
| 146 |
|
| 147 |
tmpInst := b.AllocateInstruction() |
| 148 |
tmpInst.AsIconst32(0xff) |
| 149 |
b.InsertInstruction(tmpInst) |
| 150 |
tmp := tmpInst.Return() |
| 151 |
|
| 152 |
args := b.varLengthPool.Allocate(0) |
| 153 |
args = args.Append(&b.varLengthPool, tmp) |
| 154 |
brz := b.AllocateInstruction() |
| 155 |
brz.AsBrz(value, args, loopHeader) // Loop to itself. |
| 156 |
b.InsertInstruction(brz) |
| 157 |
|
| 158 |
jmp := b.AllocateInstruction() |
| 159 |
jmp.AsJump(ValuesNil, end) |
| 160 |
b.InsertInstruction(jmp) |
| 161 |
} |
| 162 |
b.Seal(loopHeader) |
| 163 |
|
| 164 |
b.SetCurrentBlock(end) |
| 165 |
{ |
| 166 |
ret := b.AllocateInstruction() |
| 167 |
ret.AsReturn(ValuesNil) |
| 168 |
b.InsertInstruction(ret) |
| 169 |
} |
| 170 |
|
| 171 |
// passRedundantPhiEliminationOpt requires the reverse post-order traversal to be calculated. |
| 172 |
passCalculateImmediateDominators(b) |
| 173 |
return nil |
| 174 |
}, |
| 175 |
before: ` |
| 176 |
blk0: () |
| 177 |
v1:i32 = Iconst_32 0xff |
| 178 |
Jump blk1, v1, v1 |
| 179 |
|
| 180 |
blk1: (v0:i32,v2:i32) <-- (blk0,blk1) |
| 181 |
v3:i32 = Iconst_32 0xff |
| 182 |
Brz v2, blk1, v3, v2 |
| 183 |
Jump blk2 |
| 184 |
|
| 185 |
blk2: () <-- (blk1) |
| 186 |
Return |
| 187 |
`, |
| 188 |
after: ` |
| 189 |
blk0: () |
| 190 |
v1:i32 = Iconst_32 0xff |
| 191 |
Jump blk1, v1 |
| 192 |
|
| 193 |
blk1: (v0:i32) <-- (blk0,blk1) |
| 194 |
v3:i32 = Iconst_32 0xff |
| 195 |
Brz v1, blk1, v3 |
| 196 |
Jump blk2 |
| 197 |
|
| 198 |
blk2: () <-- (blk1) |
| 199 |
Return |
| 200 |
`, |
| 201 |
}, |
| 202 |
{ |
| 203 |
name: "dead code", |
| 204 |
pass: passDeadCodeEliminationOpt, |
| 205 |
setup: func(b *builder) func(*testing.T) { |
| 206 |
entry, end := b.AllocateBasicBlock(), b.AllocateBasicBlock() |
| 207 |
|
| 208 |
b.SetCurrentBlock(entry) |
| 209 |
iconstRefThriceInst := b.AllocateInstruction() |
| 210 |
iconstRefThriceInst.AsIconst32(3) |
| 211 |
b.InsertInstruction(iconstRefThriceInst) |
| 212 |
refThriceVal := iconstRefThriceInst.Return() |
| 213 |
|
| 214 |
// This has side effect. |
| 215 |
store := b.AllocateInstruction() |
| 216 |
store.AsStore(OpcodeStore, refThriceVal, refThriceVal, 0) |
| 217 |
b.InsertInstruction(store) |
| 218 |
|
| 219 |
iconstDeadInst := b.AllocateInstruction() |
| 220 |
iconstDeadInst.AsIconst32(0) |
| 221 |
b.InsertInstruction(iconstDeadInst) |
| 222 |
|
| 223 |
iconstRefOnceInst := b.AllocateInstruction() |
| 224 |
iconstRefOnceInst.AsIconst32(1) |
| 225 |
b.InsertInstruction(iconstRefOnceInst) |
| 226 |
refOnceVal := iconstRefOnceInst.Return() |
| 227 |
|
| 228 |
jmp := b.AllocateInstruction() |
| 229 |
jmp.AsJump(ValuesNil, end) |
| 230 |
b.InsertInstruction(jmp) |
| 231 |
|
| 232 |
b.SetCurrentBlock(end) |
| 233 |
aliasedRefOnceVal := b.allocateValue(refOnceVal.Type()) |
| 234 |
b.alias(aliasedRefOnceVal, refOnceVal) |
| 235 |
|
| 236 |
add := b.AllocateInstruction() |
| 237 |
add.AsIadd(aliasedRefOnceVal, refThriceVal) |
| 238 |
b.InsertInstruction(add) |
| 239 |
|
| 240 |
addRes := add.Return() |
| 241 |
|
| 242 |
ret := b.AllocateInstruction() |
| 243 |
args := b.varLengthPool.Allocate(1) |
| 244 |
args = args.Append(&b.varLengthPool, addRes) |
| 245 |
ret.AsReturn(args) |
| 246 |
b.InsertInstruction(ret) |
| 247 |
return func(t *testing.T) { |
| 248 |
// Group IDs. |
| 249 |
const gid0, gid1, gid2 InstructionGroupID = 0, 1, 2 |
| 250 |
require.Equal(t, gid0, iconstRefThriceInst.gid) |
| 251 |
require.Equal(t, gid0, store.gid) |
| 252 |
require.Equal(t, gid1, iconstDeadInst.gid) |
| 253 |
require.Equal(t, gid1, iconstRefOnceInst.gid) |
| 254 |
require.Equal(t, gid1, jmp.gid) |
| 255 |
// Different blocks have different gids. |
| 256 |
require.Equal(t, gid2, add.gid) |
| 257 |
require.Equal(t, gid2, ret.gid) |
| 258 |
|
| 259 |
// Dead or Alive... |
| 260 |
require.False(t, iconstDeadInst.live) |
| 261 |
require.True(t, iconstRefOnceInst.live) |
| 262 |
require.True(t, iconstRefThriceInst.live) |
| 263 |
require.True(t, add.live) |
| 264 |
require.True(t, jmp.live) |
| 265 |
require.True(t, ret.live) |
| 266 |
|
| 267 |
require.Equal(t, uint32(1), b.valuesInfo[refOnceVal.ID()].RefCount) |
| 268 |
require.Equal(t, uint32(1), b.valuesInfo[addRes.ID()].RefCount) |
| 269 |
require.Equal(t, uint32(3), b.valuesInfo[refThriceVal.ID()].RefCount) |
| 270 |
} |
| 271 |
}, |
| 272 |
before: ` |
| 273 |
blk0: () |
| 274 |
v0:i32 = Iconst_32 0x3 |
| 275 |
Store v0, v0, 0x0 |
| 276 |
v1:i32 = Iconst_32 0x0 |
| 277 |
v2:i32 = Iconst_32 0x1 |
| 278 |
Jump blk1 |
| 279 |
|
| 280 |
blk1: () <-- (blk0) |
| 281 |
v4:i32 = Iadd v3, v0 |
| 282 |
Return v4 |
| 283 |
`, |
| 284 |
after: ` |
| 285 |
blk0: () |
| 286 |
v0:i32 = Iconst_32 0x3 |
| 287 |
Store v0, v0, 0x0 |
| 288 |
v2:i32 = Iconst_32 0x1 |
| 289 |
Jump blk1 |
| 290 |
|
| 291 |
blk1: () <-- (blk0) |
| 292 |
v4:i32 = Iadd v2, v0 |
| 293 |
Return v4 |
| 294 |
`, |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "nop elimination", |
| 298 |
pass: passNopInstElimination, |
| 299 |
postPass: passDeadCodeEliminationOpt, |
| 300 |
setup: func(b *builder) (verifier func(t *testing.T)) { |
| 301 |
entry := b.AllocateBasicBlock() |
| 302 |
b.SetCurrentBlock(entry) |
| 303 |
|
| 304 |
i32Param := entry.AddParam(b, TypeI32) |
| 305 |
i64Param := entry.AddParam(b, TypeI64) |
| 306 |
|
| 307 |
// 32-bit shift. |
| 308 |
moduleZeroI32 := b.AllocateInstruction().AsIconst32(32 * 245).Insert(b).Return() |
| 309 |
nopIshl := b.AllocateInstruction().AsIshl(i32Param, moduleZeroI32).Insert(b).Return() |
| 310 |
|
| 311 |
// 64-bit shift. |
| 312 |
moduleZeroI64 := b.AllocateInstruction().AsIconst64(64 * 245).Insert(b).Return() |
| 313 |
nopUshr := b.AllocateInstruction().AsUshr(i64Param, moduleZeroI64).Insert(b).Return() |
| 314 |
|
| 315 |
// Non zero shift amount should not be eliminated. |
| 316 |
nonZeroI32 := b.AllocateInstruction().AsIconst32(32*245 + 1).Insert(b).Return() |
| 317 |
nonZeroIshl := b.AllocateInstruction().AsIshl(i32Param, nonZeroI32).Insert(b).Return() |
| 318 |
|
| 319 |
nonZeroI64 := b.AllocateInstruction().AsIconst64(64*245 + 1).Insert(b).Return() |
| 320 |
nonZeroSshr := b.AllocateInstruction().AsSshr(i64Param, nonZeroI64).Insert(b).Return() |
| 321 |
|
| 322 |
ret := b.AllocateInstruction() |
| 323 |
args := b.varLengthPool.Allocate(4) |
| 324 |
args = args.Append(&b.varLengthPool, nopIshl) |
| 325 |
args = args.Append(&b.varLengthPool, nopUshr) |
| 326 |
args = args.Append(&b.varLengthPool, nonZeroIshl) |
| 327 |
args = args.Append(&b.varLengthPool, nonZeroSshr) |
| 328 |
ret.AsReturn(args) |
| 329 |
b.InsertInstruction(ret) |
| 330 |
return nil |
| 331 |
}, |
| 332 |
before: ` |
| 333 |
blk0: (v0:i32, v1:i64) |
| 334 |
v2:i32 = Iconst_32 0x1ea0 |
| 335 |
v3:i32 = Ishl v0, v2 |
| 336 |
v4:i64 = Iconst_64 0x3d40 |
| 337 |
v5:i64 = Ushr v1, v4 |
| 338 |
v6:i32 = Iconst_32 0x1ea1 |
| 339 |
v7:i32 = Ishl v0, v6 |
| 340 |
v8:i64 = Iconst_64 0x3d41 |
| 341 |
v9:i64 = Sshr v1, v8 |
| 342 |
Return v3, v5, v7, v9 |
| 343 |
`, |
| 344 |
after: ` |
| 345 |
blk0: (v0:i32, v1:i64) |
| 346 |
v6:i32 = Iconst_32 0x1ea1 |
| 347 |
v7:i32 = Ishl v0, v6 |
| 348 |
v8:i64 = Iconst_64 0x3d41 |
| 349 |
v9:i64 = Sshr v1, v8 |
| 350 |
Return v0, v1, v7, v9 |
| 351 |
`, |
| 352 |
}, |
| 353 |
} { |
| 354 |
tc := tc |
| 355 |
t.Run(tc.name, func(t *testing.T) { |
| 356 |
b := NewBuilder().(*builder) |
| 357 |
verifier := tc.setup(b) |
| 358 |
require.Equal(t, tc.before, b.Format()) |
| 359 |
tc.pass(b) |
| 360 |
if verifier != nil { |
| 361 |
verifier(t) |
| 362 |
} |
| 363 |
if tc.postPass != nil { |
| 364 |
tc.postPass(b) |
| 365 |
} |
| 366 |
require.Equal(t, tc.after, b.Format()) |
| 367 |
}) |
| 368 |
} |
| 369 |
} |