array-iterator-turbo.js 6.36 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --turbo-escape --allow-natives-syntax --no-always-opt
6
// Flags: --opt --turbo-filter=* --no-force-slow-path
7 8 9 10 11 12 13

"use strict";

let global = this;
let tests = {
  FastElementsKind() {
    let runners = {
14
      PACKED_SMI_ELEMENTS(array) {
15 16 17 18 19
        let sum = 0;
        for (let x of array) sum += x;
        return sum;
      },

20
      HOLEY_SMI_ELEMENTS(array) {
21 22 23 24 25 26 27
        let sum = 0;
        for (let x of array) {
          if (x) sum += x;
        }
        return sum;
      },

28
      PACKED_ELEMENTS(array) {
29 30 31 32 33
        let ret = "";
        for (let str of array) ret += `> ${str}`;
        return ret;
      },

34
      HOLEY_ELEMENTS(array) {
35 36 37 38 39
        let ret = "";
        for (let str of array) ret += `> ${str}`;
        return ret;
      },

40
      PACKED_DOUBLE_ELEMENTS(array) {
41 42 43 44 45
        let sum = 0.0;
        for (let x of array) sum += x;
          return sum;
      },

46 47 48
      // TODO(6587): Re-enable the below test case once we no longer deopt due
      // to non-truncating uses of {CheckFloat64Hole} nodes.
      /*HOLEY_DOUBLE_ELEMENTS(array) {
49 50 51 52 53
        let sum = 0.0;
        for (let x of array) {
          if (x) sum += x;
        }
        return sum;
54
      }*/
55 56 57
    };

    let tests = {
58
      PACKED_SMI_ELEMENTS: {
59 60 61 62 63
        array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        expected: 55,
        array2: [1, 2, 3],
        expected2: 6
      },
64
      HOLEY_SMI_ELEMENTS: {
65 66 67 68 69
        array: [1, , 3, , 5, , 7, , 9, ,],
        expected: 25,
        array2: [1, , 3],
        expected2: 4
      },
70
      PACKED_ELEMENTS: {
71 72 73 74 75
        array: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
        expected: "> a> b> c> d> e> f> g> h> i> j",
        array2: ["a", "b", "c"],
        expected2: "> a> b> c"
      },
76
      HOLEY_ELEMENTS: {
77 78 79 80 81 82
        array: ["a", , "c", , "e", , "g", , "i", ,],
        expected: "> a> undefined> c> undefined> e> undefined> g" +
                  "> undefined> i> undefined",
        array2: ["a", , "c"],
        expected2: "> a> undefined> c"
      },
83
      PACKED_DOUBLE_ELEMENTS: {
84 85 86 87 88
        array: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
        expected: 5.5,
        array2: [0.6, 0.4, 0.2],
        expected2: 1.2
      },
89 90 91
      // TODO(6587): Re-enable the below test case once we no longer deopt due
      // to non-truncating uses of {CheckFloat64Hole} nodes.
      /*HOLEY_DOUBLE_ELEMENTS: {
92 93 94 95
        array: [0.1, , 0.3, , 0.5, , 0.7, , 0.9, ,],
        expected: 2.5,
        array2: [0.1, , 0.3],
        expected2: 0.4
96
      }*/
97 98 99 100 101 102 103
    };

    for (let key of Object.keys(runners)) {
      let fn = runners[key];
      let { array, expected, array2, expected2 } = tests[key];

      // Warmup:
104
      %PrepareFunctionForOptimization(fn);
105 106 107 108 109
      fn(array);
      fn(array);
      %OptimizeFunctionOnNextCall(fn);
      fn(array);

110
      assertOptimized(fn, '', key);
111
      assertEquals(expected, fn(array), key);
112
      assertOptimized(fn, '', key);
113

114
      // Check no deopt when another array with the same map is used
115
      assertTrue(%HaveSameMap(array, array2), key);
116
      assertOptimized(fn, '', key);
117 118 119 120 121 122 123
      assertEquals(expected2, fn(array2), key);

      // CheckMaps bailout
      let newArray = Object.defineProperty(
          [1, 2, 3], 2, { enumerable: false, configurable: false,
                          get() { return 7; } });
      fn(newArray);
124
      assertUnoptimized(fn, '', key);
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
    }
  },

  TypedArrays() {
    let tests = {
      Uint8Array: {
        array: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, -1, 256]),
        expected: 291,
        array2: new Uint8Array([1, 2, 3]),
        expected2: 6
      },

      Int8Array: {
        array: new Int8Array([1, 2, 3, 4, 5, 6, 7, 8, -129, 128]),
        expected: 35,
        array2: new Int8Array([1, 2, 3]),
        expected2: 6
      },

      Uint16Array: {
        array: new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8, -1, 0x10000]),
        expected: 65571,
        array2: new Uint16Array([1, 2, 3]),
        expected2: 6
      },

      Int16Array: {
        array: new Int16Array([1, 2, 3, 4, 5, 6, 7, 8, -32769, 0x7FFF]),
        expected: 65570,
        array2: new Int16Array([1, 2, 3]),
        expected2: 6
      },

      Uint32Array: {
        array: new Uint32Array([1, 2, 3, 4, 5, 6, 7, 8, -1, 0x100000000]),
        expected: 4294967331,
        array2: new Uint32Array([1, 2, 3]),
        expected2: 6
      },

      Int32Array: {
        array: new Int32Array([1, 2, 3, 4, 5, 6, 7, 8,
                               -2147483649, 0x7FFFFFFF]),
        expected: 4294967330,
        array2: new Int32Array([1, 2, 3]),
        expected2: 6
      },

      Float32Array: {
        array: new Float32Array([9.5, 8.0, 7.0, 7.0, 5.0, 4.0, 3.0, 2.0]),
        expected: 45.5,
        array2: new Float32Array([10.5, 5.5, 1.5]),
        expected2: 17.5
      },

      Float64Array: {
        array: new Float64Array([9.5, 8.0, 7.0, 7.0, 5.0, 4.0, 3.0, 2.0]),
        expected: 45.5,
        array2: new Float64Array([10.5, 5.5, 1.5]),
        expected2: 17.5
      },

      Uint8ClampedArray: {
        array: new Uint8ClampedArray([4.3, 7.45632, 3.14, 4.61, 5.0004, 6.493,
                                      7.12, 8, 1.7, 3.6]),
        expected: 51,
        array2: new Uint8ClampedArray([1, 2, 3]),
        expected2: 6
      }
    };

    for (let key of Object.keys(tests)) {
      let test = tests[key];
      let { array, expected, array2, expected2 } = test;

      let sum = function(array) {
        let ret = 0;
        for (let x of array) ret += x;
        return ret;
      };

      // Warmup
207
      %PrepareFunctionForOptimization(sum);
208 209 210 211 212
      sum(array);
      sum(array);
      %OptimizeFunctionOnNextCall(sum);
      assertEquals(expected, sum(array), key);

213
      assertOptimized(sum, '', key);
214 215 216 217

      // Not deoptimized when called on typed array of same type / map
      assertTrue(%HaveSameMap(array, array2));
      assertEquals(expected2, sum(array2), key);
218
      assertOptimized(sum, '', key);
219 220 221

      // Throw when detached
      let clone = new array.constructor(array);
222
      %ArrayBufferDetach(clone.buffer);
223
      assertThrows(() => sum(clone), TypeError);
224 225 226 227

      // Clear the slate for the next iteration.
      %DeoptimizeFunction(sum);
      %ClearFunctionFeedback(sum);
228 229 230 231 232 233 234 235
    }
  }
};

for (let name of Object.keys(tests)) {
  let test = tests[name];
  test();
}