Skip to content

Commit

Permalink
fix: Fix assertion when declaring a const v128 (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored Jul 25, 2020
1 parent c8a150c commit 0472909
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ import {
writeF32,
writeF64,
uniqueMap,
isPowerOf2
isPowerOf2,
v128_zero
} from "./util";

/** Compiler options. */
Expand Down Expand Up @@ -3058,10 +3059,11 @@ export class Compiler extends DiagnosticEmitter {
if (initExpr) {
let precomp = module.runExpression(initExpr, ExpressionRunnerFlags.PreserveSideeffects);
if (precomp) {
initExpr = precomp;
let local = new Local(name, -1, type, flow.parentFunction);
initExpr = precomp; // always use precomputed initExpr
let local: Local | null = null;
switch (<u32>getExpressionType(initExpr)) {
case <u32>NativeType.I32: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantIntegerValue(
i64_new(
getConstValueI32(initExpr),
Expand All @@ -3072,6 +3074,7 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case <u32>NativeType.I64: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantIntegerValue(
i64_new(
getConstValueI64Low(initExpr),
Expand All @@ -3082,33 +3085,33 @@ export class Compiler extends DiagnosticEmitter {
break;
}
case <u32>NativeType.F32: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantFloatValue(<f64>getConstValueF32(initExpr), type);
break;
}
case <u32>NativeType.F64: {
local = new Local(name, -1, type, flow.parentFunction);
local.setConstantFloatValue(getConstValueF64(initExpr), type);
break;
}
default: {
assert(false);
return module.unreachable();
}
}
// Create a virtual local that doesn't actually exist in WebAssembly
let scopedLocals = flow.scopedLocals;
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
else if (scopedLocals.has(name)) {
let existing = assert(scopedLocals.get(name));
this.errorRelated(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range,
existing.declaration.name.range,
name
);
return this.module.unreachable();
if (local) {
// Add as a virtual local that doesn't actually exist in WebAssembly
let scopedLocals = flow.scopedLocals;
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
else if (scopedLocals.has(name)) {
let existing = assert(scopedLocals.get(name));
this.errorRelated(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range,
existing.declaration.name.range,
name
);
return this.module.unreachable();
}
scopedLocals.set(name, local);
isStatic = true;
}
scopedLocals.set(name, local);
isStatic = true;
}
} else {
this.error(
Expand Down Expand Up @@ -11035,8 +11038,6 @@ export class Compiler extends DiagnosticEmitter {

// helpers

const v128_zero = new Uint8Array(16);

function mangleImportName(
element: Element,
declaration: DeclarationStatement
Expand Down
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./collections";
export * from "./math";
export * from "./path";
export * from "./text";
export * from "./vector";
7 changes: 7 additions & 0 deletions src/util/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @fileoverview Various vector utility.
* @license Apache-2.0
*/

/** v128 zero constant. */
export const v128_zero = new Uint8Array(16);
6 changes: 6 additions & 0 deletions tests/compiler/features/simd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,11 @@ function test_v64x2(): void {
// }
}

function test_const(): v128 {
const one = i32x4.splat(1); // should precompute
return one; // should not inline
}

if (ASC_FEATURE_SIMD) {
test_v128();
test_i8x16();
Expand All @@ -695,4 +700,5 @@ if (ASC_FEATURE_SIMD) {
test_v16x8();
test_v32x4();
test_v64x2();
test_const();
}
9 changes: 9 additions & 0 deletions tests/compiler/features/simd.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(type $i32_=>_none (func (param i32)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_v128 (func (result v128)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 16) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00s\00t\00u\00b\00.\00t\00s\00")
Expand Down Expand Up @@ -2989,6 +2990,12 @@
(func $features/simd/test_v64x2
nop
)
(func $features/simd/test_const (result v128)
(local $0 v128)
v128.const i32x4 0x00000001 0x00000001 0x00000001 0x00000001
local.set $0
local.get $0
)
(func $start:features/simd
global.get $~lib/heap/__heap_base
i32.const 15
Expand All @@ -3013,6 +3020,8 @@
call $features/simd/test_v16x8
call $features/simd/test_v32x4
call $features/simd/test_v64x2
call $features/simd/test_const
drop
)
(func $~start
call $start:features/simd
Expand Down

0 comments on commit 0472909

Please sign in to comment.