Skip to content

fix(compiler): Implement proper left / right cyclic builtin rotation for small integers #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 42 additions & 16 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,8 @@ function builtin_popcnt(ctx: BuiltinContext): ExpressionRef {
var type = compiler.currentType;
if (type.isValue) {
switch (compiler.currentType.kind) {
case TypeKind.BOOL: // not wrapped
case TypeKind.I8:
case TypeKind.BOOL: return arg0;
case TypeKind.I8: // not wrapped
case TypeKind.U8:
case TypeKind.I16:
case TypeKind.U16:
Expand Down Expand Up @@ -1171,14 +1171,27 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
if (type.isValue) {
let arg1 = compiler.compileExpression(operands[1], type, Constraints.CONV_IMPLICIT);
switch (type.kind) {
case TypeKind.BOOL: return arg0;
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
return compiler.ensureSmallIntegerWrap(
module.binary(BinaryOp.RotlI32, arg0, arg1),
type
case TypeKind.U16: {
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
return module.binary(BinaryOp.OrI32,
module.binary(
BinaryOp.ShlI32,
arg0,
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
),
module.binary(
BinaryOp.ShrU32,
arg0,
module.binary(
BinaryOp.AndI32,
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
module.i32(type.size - 1)
)
)
);
}
case TypeKind.I32:
Expand Down Expand Up @@ -1221,14 +1234,27 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
if (type.isValue) {
let arg1 = compiler.compileExpression(operands[1], type, Constraints.CONV_IMPLICIT);
switch (type.kind) {
case TypeKind.BOOL: return arg0;
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.BOOL: {
return compiler.ensureSmallIntegerWrap(
module.binary(BinaryOp.RotrI32, arg0, arg1),
type
case TypeKind.U16: {
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
return module.binary(BinaryOp.OrI32,
module.binary(
BinaryOp.ShrU32,
arg0,
module.binary(BinaryOp.AndI32, arg1, module.i32(type.size - 1))
),
module.binary(
BinaryOp.ShlI32,
arg0,
module.binary(
BinaryOp.AndI32,
module.binary(BinaryOp.SubI32, module.i32(0), arg1),
module.i32(type.size - 1)
)
)
);
}
case TypeKind.I32:
Expand Down Expand Up @@ -2129,7 +2155,7 @@ function builtin_add(ctx: BuiltinContext): ExpressionRef {
{
op = BinaryOp.AddI32;
break;
}
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.AddI64;
Expand Down Expand Up @@ -2220,7 +2246,7 @@ function builtin_sub(ctx: BuiltinContext): ExpressionRef {
{
op = BinaryOp.SubI32;
break;
}
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.SubI64;
Expand Down Expand Up @@ -2311,7 +2337,7 @@ function builtin_mul(ctx: BuiltinContext): ExpressionRef {
{
op = BinaryOp.MulI32;
break;
}
}
case TypeKind.I64:
case TypeKind.U64: {
op = BinaryOp.MulI64;
Expand Down Expand Up @@ -3041,7 +3067,7 @@ function builtin_assert(ctx: BuiltinContext): ExpressionRef {
case TypeKind.EXTERNREF:
case TypeKind.EXNREF:
case TypeKind.ANYREF: return module.if(module.ref_is_null(arg0), abort);

}
} else {
compiler.currentType = type.nonNullableType;
Expand Down
62 changes: 31 additions & 31 deletions tests/compiler/builtins.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@
if
i32.const 0
i32.const 1088
i32.const 294
i32.const 298
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -518,7 +518,7 @@
if
i32.const 0
i32.const 1088
i32.const 295
i32.const 299
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -530,7 +530,7 @@
if
i32.const 0
i32.const 1088
i32.const 296
i32.const 300
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -542,7 +542,7 @@
if
i32.const 0
i32.const 1088
i32.const 299
i32.const 303
i32.const 1
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -621,7 +621,7 @@
if
i32.const 0
i32.const 1088
i32.const 475
i32.const 479
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -633,7 +633,7 @@
if
i32.const 0
i32.const 1088
i32.const 476
i32.const 480
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -645,7 +645,7 @@
if
i32.const 0
i32.const 1088
i32.const 477
i32.const 481
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -655,7 +655,7 @@
if
i32.const 0
i32.const 1088
i32.const 478
i32.const 482
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -667,7 +667,7 @@
if
i32.const 0
i32.const 1088
i32.const 479
i32.const 483
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -677,7 +677,7 @@
if
i32.const 0
i32.const 1088
i32.const 480
i32.const 484
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -687,7 +687,7 @@
if
i32.const 0
i32.const 1088
i32.const 481
i32.const 485
i32.const 1
call $~lib/builtins/abort
unreachable
Expand All @@ -707,7 +707,7 @@
if
i32.const 0
i32.const 1088
i32.const 498
i32.const 502
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -719,7 +719,7 @@
if
i32.const 0
i32.const 1088
i32.const 499
i32.const 503
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -731,7 +731,7 @@
if
i32.const 0
i32.const 1088
i32.const 500
i32.const 504
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -743,7 +743,7 @@
if
i32.const 0
i32.const 1088
i32.const 501
i32.const 505
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -755,7 +755,7 @@
if
i32.const 0
i32.const 1088
i32.const 502
i32.const 506
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -767,7 +767,7 @@
if
i32.const 0
i32.const 1088
i32.const 503
i32.const 507
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -779,7 +779,7 @@
if
i32.const 0
i32.const 1088
i32.const 504
i32.const 508
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -791,7 +791,7 @@
if
i32.const 0
i32.const 1088
i32.const 505
i32.const 509
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -803,7 +803,7 @@
if
i32.const 0
i32.const 1088
i32.const 506
i32.const 510
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -815,7 +815,7 @@
if
i32.const 0
i32.const 1088
i32.const 507
i32.const 511
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -827,7 +827,7 @@
if
i32.const 0
i32.const 1088
i32.const 508
i32.const 512
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -839,7 +839,7 @@
if
i32.const 0
i32.const 1088
i32.const 509
i32.const 513
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -851,7 +851,7 @@
if
i32.const 0
i32.const 1088
i32.const 510
i32.const 514
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -863,7 +863,7 @@
if
i32.const 0
i32.const 1088
i32.const 511
i32.const 515
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -875,7 +875,7 @@
if
i32.const 0
i32.const 1088
i32.const 512
i32.const 516
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -887,7 +887,7 @@
if
i32.const 0
i32.const 1088
i32.const 513
i32.const 517
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -899,7 +899,7 @@
if
i32.const 0
i32.const 1088
i32.const 514
i32.const 518
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -911,7 +911,7 @@
if
i32.const 0
i32.const 1088
i32.const 515
i32.const 519
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -923,7 +923,7 @@
if
i32.const 0
i32.const 1088
i32.const 516
i32.const 520
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -935,7 +935,7 @@
if
i32.const 0
i32.const 1088
i32.const 517
i32.const 521
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down
Loading