Skip to content

Commit 0472909

Browse files
authored
fix: Fix assertion when declaring a const v128 (#1411)
1 parent c8a150c commit 0472909

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

Diff for: src/compiler.ts

+24-23
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ import {
193193
writeF32,
194194
writeF64,
195195
uniqueMap,
196-
isPowerOf2
196+
isPowerOf2,
197+
v128_zero
197198
} from "./util";
198199

199200
/** Compiler options. */
@@ -3058,10 +3059,11 @@ export class Compiler extends DiagnosticEmitter {
30583059
if (initExpr) {
30593060
let precomp = module.runExpression(initExpr, ExpressionRunnerFlags.PreserveSideeffects);
30603061
if (precomp) {
3061-
initExpr = precomp;
3062-
let local = new Local(name, -1, type, flow.parentFunction);
3062+
initExpr = precomp; // always use precomputed initExpr
3063+
let local: Local | null = null;
30633064
switch (<u32>getExpressionType(initExpr)) {
30643065
case <u32>NativeType.I32: {
3066+
local = new Local(name, -1, type, flow.parentFunction);
30653067
local.setConstantIntegerValue(
30663068
i64_new(
30673069
getConstValueI32(initExpr),
@@ -3072,6 +3074,7 @@ export class Compiler extends DiagnosticEmitter {
30723074
break;
30733075
}
30743076
case <u32>NativeType.I64: {
3077+
local = new Local(name, -1, type, flow.parentFunction);
30753078
local.setConstantIntegerValue(
30763079
i64_new(
30773080
getConstValueI64Low(initExpr),
@@ -3082,33 +3085,33 @@ export class Compiler extends DiagnosticEmitter {
30823085
break;
30833086
}
30843087
case <u32>NativeType.F32: {
3088+
local = new Local(name, -1, type, flow.parentFunction);
30853089
local.setConstantFloatValue(<f64>getConstValueF32(initExpr), type);
30863090
break;
30873091
}
30883092
case <u32>NativeType.F64: {
3093+
local = new Local(name, -1, type, flow.parentFunction);
30893094
local.setConstantFloatValue(getConstValueF64(initExpr), type);
30903095
break;
30913096
}
3092-
default: {
3093-
assert(false);
3094-
return module.unreachable();
3095-
}
30963097
}
3097-
// Create a virtual local that doesn't actually exist in WebAssembly
3098-
let scopedLocals = flow.scopedLocals;
3099-
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
3100-
else if (scopedLocals.has(name)) {
3101-
let existing = assert(scopedLocals.get(name));
3102-
this.errorRelated(
3103-
DiagnosticCode.Duplicate_identifier_0,
3104-
declaration.name.range,
3105-
existing.declaration.name.range,
3106-
name
3107-
);
3108-
return this.module.unreachable();
3098+
if (local) {
3099+
// Add as a virtual local that doesn't actually exist in WebAssembly
3100+
let scopedLocals = flow.scopedLocals;
3101+
if (!scopedLocals) flow.scopedLocals = scopedLocals = new Map();
3102+
else if (scopedLocals.has(name)) {
3103+
let existing = assert(scopedLocals.get(name));
3104+
this.errorRelated(
3105+
DiagnosticCode.Duplicate_identifier_0,
3106+
declaration.name.range,
3107+
existing.declaration.name.range,
3108+
name
3109+
);
3110+
return this.module.unreachable();
3111+
}
3112+
scopedLocals.set(name, local);
3113+
isStatic = true;
31093114
}
3110-
scopedLocals.set(name, local);
3111-
isStatic = true;
31123115
}
31133116
} else {
31143117
this.error(
@@ -11035,8 +11038,6 @@ export class Compiler extends DiagnosticEmitter {
1103511038

1103611039
// helpers
1103711040

11038-
const v128_zero = new Uint8Array(16);
11039-
1104011041
function mangleImportName(
1104111042
element: Element,
1104211043
declaration: DeclarationStatement

Diff for: src/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./collections";
88
export * from "./math";
99
export * from "./path";
1010
export * from "./text";
11+
export * from "./vector";

Diff for: src/util/vector.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @fileoverview Various vector utility.
3+
* @license Apache-2.0
4+
*/
5+
6+
/** v128 zero constant. */
7+
export const v128_zero = new Uint8Array(16);

Diff for: tests/compiler/features/simd.ts

+6
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ function test_v64x2(): void {
683683
// }
684684
}
685685

686+
function test_const(): v128 {
687+
const one = i32x4.splat(1); // should precompute
688+
return one; // should not inline
689+
}
690+
686691
if (ASC_FEATURE_SIMD) {
687692
test_v128();
688693
test_i8x16();
@@ -695,4 +700,5 @@ if (ASC_FEATURE_SIMD) {
695700
test_v16x8();
696701
test_v32x4();
697702
test_v64x2();
703+
test_const();
698704
}

Diff for: tests/compiler/features/simd.untouched.wat

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(type $i32_=>_none (func (param i32)))
44
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
55
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
6+
(type $none_=>_v128 (func (result v128)))
67
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
78
(memory $0 1)
89
(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")
@@ -2989,6 +2990,12 @@
29892990
(func $features/simd/test_v64x2
29902991
nop
29912992
)
2993+
(func $features/simd/test_const (result v128)
2994+
(local $0 v128)
2995+
v128.const i32x4 0x00000001 0x00000001 0x00000001 0x00000001
2996+
local.set $0
2997+
local.get $0
2998+
)
29922999
(func $start:features/simd
29933000
global.get $~lib/heap/__heap_base
29943001
i32.const 15
@@ -3013,6 +3020,8 @@
30133020
call $features/simd/test_v16x8
30143021
call $features/simd/test_v32x4
30153022
call $features/simd/test_v64x2
3023+
call $features/simd/test_const
3024+
drop
30163025
)
30173026
(func $~start
30183027
call $start:features/simd

0 commit comments

Comments
 (0)