Skip to content

Fix local confusion in builtin min/max/rotl/rotr #1540

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
Nov 9, 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
30 changes: 29 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
} from "./program";

import {
findUsedLocals,
FlowFlags,
LocalFlags
} from "./flow";
Expand Down Expand Up @@ -1185,7 +1186,7 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
case TypeKind.U16: {
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
let flow = compiler.currentFlow;
let temp1 = flow.getTempLocal(type);
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
Expand Down Expand Up @@ -1266,7 +1267,7 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
case TypeKind.U16: {
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
let flow = compiler.currentFlow;
let temp1 = flow.getTempLocal(type);
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
Expand Down Expand Up @@ -1484,7 +1485,7 @@ function builtin_max(ctx: BuiltinContext): ExpressionRef {
if (op != -1) {
let flow = compiler.currentFlow;
let nativeType = type.toNativeType();
let temp1 = flow.getTempLocal(type);
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
Copy link
Member

@MaxGraey MaxGraey Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about make getUnusedLocal which try to find unused local in already allocated locals or allocate new one otherwise? And use flow.getUnusedLocal(type) here and in other places?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findUsedLocals is what other code typically uses to keep the number of necessary locals low. Otherwise we'd always get a new local, never a reused one, I guess?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also builtin_abs, builtin_rotl / builtin_rotr I guess also required this fix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, at some point I expect us to move away from free'ing temporary locals, but we'll have to get rid of any kind of recompiling expressions first. There are still a few places doing this, and getUnusedLocal would return a new local upon recompile, yielding different code, while findUsedLocals doesn't.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also builtin_abs, builtin_rotl / builtin_rotr I guess also required this fix

Abs is a unary expression not executing input code in between assignment and usage of the temporary locals. Should be safe. Going to take a look at rotl/rotr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findUsedLocals is what other code typically uses to keep the number of necessary locals low. Otherwise we'd always get a new local, never a reused one, I guess?

I guess better always alloc new loacal and let binaryen optimize all of this afterwards

Copy link
Member Author

@dcodeIO dcodeIO Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect us to move into that direction, yeah, but we cannot do that as long as we are recompiling code (and not resetting the local pool with a new mechanism) because it would confuse local flags (adding new locals on each recompile, leaving earlier ones unused), then for example leading to infinite loops when attempting to unify local flags in loops.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that rotl for some reason does not exhibit the behavior on rotl(a, rotl(b, c)), but should be susceptible just as rotr is, which trivially breaks without the fix. Probably coincidence. Applied the same fix for both.

Copy link
Member

@MaxGraey MaxGraey Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then for example leading to infinite loops when attempting to unify local flags in loops.

I see. Ok, in this case PR LGTM

flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
Expand Down Expand Up @@ -1563,7 +1564,7 @@ function builtin_min(ctx: BuiltinContext): ExpressionRef {
if (op != -1) {
let flow = compiler.currentFlow;
let nativeType = type.toNativeType();
let temp1 = flow.getTempLocal(type);
let temp1 = flow.getTempLocal(type, findUsedLocals(arg1));
flow.setLocalFlag(temp1.index, LocalFlags.WRAPPED);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.WRAPPED);
Expand Down
22 changes: 22 additions & 0 deletions tests/compiler/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,25 @@ assert(isInteger<ReturnType<() => i32>>());
assert(isInteger<returnof<() => i32>>());
assert(isManaged<returnof<() => C>>());
assert(isManaged<ReturnType<() => C>>());

// Issue #1537: Make sure temp. locals are not confused

function max3(a: i32, b: i32, c: i32): i32 {
return max(a, max(b, c));
}
assert(max3(3, 2, 1) == 3);

function min3(a: i32, b: i32, c: i32): i32 {
return min(a, min(b, c));
}
assert(min3(1, 2, 3) == 1);

function rotl3(a: i8, b: i8, c: i8): i32 {
return rotl(a, rotl(b, c));
}
assert(rotl3(3, 2, 1) == 48);

function rotr3(a: i8, b: i8, c: i8): i32 {
return rotr(a, rotr(b, c));
}
assert(rotr3(48, 8, 1) == 3);
Loading