summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/tail-calls/return_call.js
blob: ddaa933aafa4e463ef7cdd55713432ceb5554193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var ins = wasmEvalText(`(module
    (func $fac-acc (export "fac-acc") (param i64 i64) (result i64)
        (if (result i64) (i64.eqz (local.get 0))
        (then (local.get 1))
        (else
            (return_call $fac-acc
                (i64.sub (local.get 0) (i64.const 1))
                (i64.mul (local.get 0) (local.get 1))
            )
        )
        )
    )
    (func (export "main") (param i64) (result i64)
        (call $fac-acc (local.get 0) (i64.const 1))
    )
)`);

// Check return call via wasm function
assertEq(ins.exports.main(5n), 120n);

// Check return call directly via interpreter stub
const fac = ins.exports["fac-acc"];
assertEq(fac(4n, 1n), 24n);

// Check return call directly via jit stub
check_stub1: {
    let options = getJitCompilerOptions();
    if (!options["baseline.enable"]) break check_stub1;
    const check = function() {
        fac(4n, 1n);
    };
    for (let i = options["baseline.warmup.trigger"] + 1; i--;)
        check();
}

// Return call of an import
var ins0 = wasmEvalText(`(module
    (func $fac-acc (export "fac-acc") (param i64 i64) (result i64)
      (if (result i64) (i64.eqz (local.get 0))
        (then (local.get 1))
        (else
          (return_call $fac-acc
            (i64.sub (local.get 0) (i64.const 1))
            (i64.mul (local.get 0) (local.get 1))
          )
        )
      )
    )
)`);

var ins = wasmEvalText(`(module
    (import "" "fac-acc" (func $fac-acc (param i64 i64) (result i64)))
    (func (export "fac") (param i64) (result i64)
      local.get 0
      i64.const 1
      return_call $fac-acc
    )
    (func (export "main") (result i64)
      i64.const 4
      call 1
    )
)`, {"": {"fac-acc": ins0.exports["fac-acc"],}});

assertEq(ins.exports.main(), 24n);
assertEq(ins.exports.fac(3n, 1n), 6n);
check_stub2: {
    let options = getJitCompilerOptions();
    if (!options["baseline.enable"]) break check_stub2;
    const check = function() {
        ins.exports.fac(3n, 1n)
    };
    for (let i = options["baseline.warmup.trigger"] + 1; i--;)
        check();
}

// Check with parameters area growth
var ins0 = wasmEvalText(`(module
  (func $fac-acc (export "fac-acc") (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)
    (if (result i64) (i64.eqz (local.get 0))
      (then (local.get 1))
      (else
        (return_call $fac-acc
          (i64.sub (local.get 0) (i64.const 1))
          (i64.mul (local.get 0) (local.get 1))
          i64.const 1 i64.const 2 i64.const 3 i64.const 4 i64.const 5 i64.const 6
        )
      )
    )
  )
)`);

var ins = wasmEvalText(`(module
  (import "" "fac-acc" (func $fac-acc (param i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))
  (func (export "fac") (param i64) (result i64)
    local.get 0
    i64.const 1
    i64.const 1 i64.const 2 i64.const 3 i64.const 4 i64.const 5 i64.const 6
    return_call $fac-acc
  )
  (func (export "main") (result i64)
    i64.const 5
    call 1
  )
)`, {"": {"fac-acc": ins0.exports["fac-acc"],}});

assertEq(ins.exports.main(), 120n);
assertEq(ins.exports.fac(3n, 1n), 6n);
check_stub3: {
    let options = getJitCompilerOptions();
    if (!options["baseline.enable"]) break check_stub3;
    const check = function() {
        ins.exports.fac(4n, 1n)
    };
    for (let i = options["baseline.warmup.trigger"] + 1; i--;)
        check();
}

// Test multi-value returns.
var ins = wasmEvalText(`(module
  (memory (export "memory") 1 1)
  (func $rec (export "rec") (param i32 i32 i32 i32 i32 i32 i32) (result i32 i32 f32 f32)
    (local f32 i32)
    (if (result i32 i32 f32 f32) (i32.ge_u (local.get 0) (local.get 1))
      (then
        (local.get 5)
        (local.get 6)
        (local.tee 7 (f32.div (f32.convert_i32_u (local.get 3)) (f32.convert_i32_u (local.get 2))))
        (f32.sqrt
          (f32.sub
            (f32.div (f32.convert_i32_u (local.get 4)) (f32.convert_i32_u (local.get 2)))
            (f32.mul (local.get 7) (local.get 7))
          )
        )
      )
      (else
        (return_call $rec
          (i32.add (local.get 0) (i32.const 1))
          (local.get 1)
          (i32.add (local.get 2) (i32.const 1))
          (i32.add (local.get 3) (local.tee 8 (i32.load8_u (local.get 0))))
          (i32.add (local.get 4) (i32.mul (local.get 8) (local.get 8)))
          (if (result i32) (i32.gt_s (local.get 5) (local.get 8))
            (then (local.get 8)) (else (local.get 5))
          )
          (if (result i32) (i32.lt_s (local.get 6) (local.get 8))
            (then (local.get 8)) (else (local.get 6))
          )
        )
      )
    )
  )
  (func $main (export "main") (result i32 i32 f32 f32)
    (call $rec
      (i32.const 0)
      (i32.const 6)
      (i32.const 0)
      (i32.const 0)
      (i32.const 0)
      (i32.const 1000)
      (i32.const -1000)
    )
  )
  (data (i32.const 0) "\\02\\13\\22\\04\\08\\30")
)`);

const main = ins.exports["main"];
assertEq(""+ main(), "2,48,19.16666603088379,16.836633682250977");
assertEq("" + ins.exports.rec(1, 5, 0, 0, 0, 1000, -1000), "4,34,16.25,11.627016067504883");
check_stub3: {
    let options = getJitCompilerOptions();
    if (!options["baseline.enable"]) break check_stub3;
    const check = function() {
        ins.exports.rec(1, 5, 0, 0, 0, 1000, -1000);
    };
    for (let i = options["baseline.warmup.trigger"] + 1; i--;)
        check();
}

// Handling trap.
var ins = wasmEvalText(`(module
  (func $fac-acc (export "fac") (param i64 i64) (result i64)
    (if (result i64) (i64.eqz (local.get 0))
    (then (unreachable))
    (else
        (return_call $fac-acc
            (i64.sub (local.get 0) (i64.const 1))
            (i64.mul (local.get 0) (local.get 1))
        )
    )
    )
  )
  (func (export "main") (param i64) (result i64)
    (call $fac-acc (local.get 0) (i64.const 1))
  )
)`);

assertErrorMessage(() => ins.exports.main(4n), WebAssembly.RuntimeError, /unreachable executed/);
assertErrorMessage(() => ins.exports.fac(3n, 1n), WebAssembly.RuntimeError, /unreachable executed/);

// Performance and stack growth: calculating sum of numbers 1..40000000
var ins = wasmEvalText(`(module
  (func $sum (param i32 i64) (result i64)
    local.get 0
    i32.eqz
    if
      local.get 1
      return
    else
      local.get 0
      i32.const 1
      i32.sub
      local.get 1
      local.get 0
      i64.extend_i32_s
      i64.add
      return_call $sum
    end
    unreachable
  )

  (func (export "main") (param i32) (result i64)
    local.get 0
    i64.const 0
    call $sum
  )
)`);

if (getBuildConfiguration("simulator")) {
  assertEq(ins.exports.main(400000), 80000200000n);
} else {
  assertEq(ins.exports.main(40000000), 800000020000000n);
}

// GC/externref shall not cling to the trampoline frame.
// The `return_call` caller will create a trampoline because the callee is
// an import. The caller will create a GC object and will hold in its frame
// and a WeakMap.
// Test if the created object is in the WeakMap even after gc().
var wm = new WeakMap();
var ins = wasmEvalText(`(module
  (import "" "test" (func $test))
  (func $sum (param i32 i64) (result i64)
    local.get 0
    i32.eqz
    if
      call $test
      local.get 1
      return
    else
      local.get 0
      i32.const 1
      i32.sub
      local.get 1
      local.get 0
      i64.extend_i32_s
      i64.add
      return_call $sum
    end
    unreachable
  )
  (export "sum" (func $sum))
)`, {"": {
  test() {
    gc();
    assertEq(nondeterministicGetWeakMapKeys(wm).length, 0);
  }
}});

var ins2 = wasmEvalText(`(module
  (import "" "add_ref" (func $add_ref (result externref)))
  (import "" "use_ref" (func $use_ref (param externref)))
  (import "" "sum" (func $sum (param i32 i64) (result i64)))
  (global $g1 (mut i32) (i32.const 0))
  (func (export "main_gc") (param i32) (result i64)
    (local $ref externref)
    call $add_ref
    local.set $ref

    local.get $ref
    call $use_ref

    block
      global.get $g1
      br_if 0
      local.get 0
      i64.const 0
      return_call $sum
    end

    local.get $ref
    call $use_ref
    i64.const -1
  )
)`, {"": {
  sum: ins.exports.sum,
  add_ref() {
    const obj = {}; wm.set(obj, 'foo'); return obj;
  },
  use_ref(obj) {
    assertEq(nondeterministicGetWeakMapKeys(wm).length, 1);
  },
}});

assertEq(ins2.exports.main_gc(400000), 80000200000n);
assertEq(nondeterministicGetWeakMapKeys(wm).length, 0);