OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "test/cctest/wasm/test-signatures.h" | 9 #include "test/cctest/wasm/test-signatures.h" |
10 | 10 |
11 #include "src/bit-vector.h" | 11 #include "src/bit-vector.h" |
12 #include "src/objects.h" | 12 #include "src/objects.h" |
13 | 13 |
14 #include "src/wasm/ast-decoder.h" | 14 #include "src/wasm/ast-decoder.h" |
15 #include "src/wasm/wasm-macro-gen.h" | 15 #include "src/wasm/wasm-macro-gen.h" |
16 #include "src/wasm/wasm-module.h" | 16 #include "src/wasm/wasm-module.h" |
17 | 17 |
18 #define WASM_SET_ZERO(i) WASM_SET_LOCAL(i, WASM_ZERO) | 18 #define WASM_SET_ZERO(i) WASM_SET_LOCAL(i, WASM_ZERO) |
19 | 19 |
20 namespace v8 { | 20 namespace v8 { |
21 namespace internal { | 21 namespace internal { |
22 namespace wasm { | 22 namespace wasm { |
23 | 23 |
24 class WasmLoopAssignmentAnalyzerTest : public TestWithZone { | 24 class WasmLoopAssignmentAnalyzerTest : public TestWithZone { |
25 public: | 25 public: |
26 WasmLoopAssignmentAnalyzerTest() : TestWithZone(), sigs() { | 26 WasmLoopAssignmentAnalyzerTest() : num_locals(0) {} |
27 init_env(&env, sigs.v_v()); | |
28 } | |
29 | |
30 TestSignatures sigs; | 27 TestSignatures sigs; |
31 FunctionEnv env; | 28 uint32_t num_locals; |
32 | |
33 static void init_env(FunctionEnv* env, FunctionSig* sig) { | |
34 env->module = nullptr; | |
35 env->sig = sig; | |
36 env->local_i32_count = 0; | |
37 env->local_i64_count = 0; | |
38 env->local_f32_count = 0; | |
39 env->local_f64_count = 0; | |
40 env->SumLocals(); | |
41 } | |
42 | 29 |
43 BitVector* Analyze(const byte* start, const byte* end) { | 30 BitVector* Analyze(const byte* start, const byte* end) { |
44 return AnalyzeLoopAssignmentForTesting(zone(), &env, start, end); | 31 return AnalyzeLoopAssignmentForTesting(zone(), num_locals, start, end); |
45 } | 32 } |
46 }; | 33 }; |
47 | 34 |
48 | 35 |
49 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty0) { | 36 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty0) { |
50 byte code[] = { 0 }; | 37 byte code[] = { 0 }; |
51 BitVector* assigned = Analyze(code, code); | 38 BitVector* assigned = Analyze(code, code); |
52 CHECK_NULL(assigned); | 39 CHECK_NULL(assigned); |
53 } | 40 } |
54 | 41 |
55 | 42 |
56 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty1) { | 43 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty1) { |
57 byte code[] = {kExprLoop, 0}; | 44 byte code[] = {kExprLoop, 0}; |
58 for (int i = 0; i < 5; i++) { | 45 for (int i = 0; i < 5; i++) { |
59 BitVector* assigned = Analyze(code, code + arraysize(code)); | 46 BitVector* assigned = Analyze(code, code + arraysize(code)); |
60 for (int j = 0; j < assigned->length(); j++) { | 47 for (int j = 0; j < assigned->length(); j++) { |
61 CHECK_EQ(false, assigned->Contains(j)); | 48 CHECK_EQ(false, assigned->Contains(j)); |
62 } | 49 } |
63 env.AddLocals(kAstI32, 1); | 50 num_locals++; |
64 } | 51 } |
65 } | 52 } |
66 | 53 |
67 | 54 |
68 TEST_F(WasmLoopAssignmentAnalyzerTest, One) { | 55 TEST_F(WasmLoopAssignmentAnalyzerTest, One) { |
69 env.AddLocals(kAstI32, 5); | 56 num_locals = 5; |
70 for (int i = 0; i < 5; i++) { | 57 for (int i = 0; i < 5; i++) { |
71 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i))}; | 58 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i))}; |
72 BitVector* assigned = Analyze(code, code + arraysize(code)); | 59 BitVector* assigned = Analyze(code, code + arraysize(code)); |
73 for (int j = 0; j < assigned->length(); j++) { | 60 for (int j = 0; j < assigned->length(); j++) { |
74 CHECK_EQ(j == i, assigned->Contains(j)); | 61 CHECK_EQ(j == i, assigned->Contains(j)); |
75 } | 62 } |
76 } | 63 } |
77 } | 64 } |
78 | 65 |
79 | 66 |
80 TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { | 67 TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { |
81 env.AddLocals(kAstI32, 5); | 68 num_locals = 5; |
82 for (int i = 0; i < 5; i++) { | 69 for (int i = 0; i < 5; i++) { |
83 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i)), WASM_SET_ZERO(1)}; | 70 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i)), WASM_SET_ZERO(1)}; |
84 BitVector* assigned = Analyze(code, code + arraysize(code)); | 71 BitVector* assigned = Analyze(code, code + arraysize(code)); |
85 for (int j = 0; j < assigned->length(); j++) { | 72 for (int j = 0; j < assigned->length(); j++) { |
86 CHECK_EQ(j == i, assigned->Contains(j)); | 73 CHECK_EQ(j == i, assigned->Contains(j)); |
87 } | 74 } |
88 } | 75 } |
89 } | 76 } |
90 | 77 |
91 | 78 |
92 TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { | 79 TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { |
93 env.AddLocals(kAstI32, 5); | 80 num_locals = 5; |
94 for (int i = 0; i < 5; i++) { | 81 for (int i = 0; i < 5; i++) { |
95 for (int j = 0; j < 5; j++) { | 82 for (int j = 0; j < 5; j++) { |
96 byte code[] = {WASM_LOOP(2, WASM_SET_ZERO(i), WASM_SET_ZERO(j))}; | 83 byte code[] = {WASM_LOOP(2, WASM_SET_ZERO(i), WASM_SET_ZERO(j))}; |
97 BitVector* assigned = Analyze(code, code + arraysize(code)); | 84 BitVector* assigned = Analyze(code, code + arraysize(code)); |
98 for (int k = 0; k < assigned->length(); k++) { | 85 for (int k = 0; k < assigned->length(); k++) { |
99 bool expected = k == i || k == j; | 86 bool expected = k == i || k == j; |
100 CHECK_EQ(expected, assigned->Contains(k)); | 87 CHECK_EQ(expected, assigned->Contains(k)); |
101 } | 88 } |
102 } | 89 } |
103 } | 90 } |
104 } | 91 } |
105 | 92 |
106 | 93 |
107 TEST_F(WasmLoopAssignmentAnalyzerTest, NestedIf) { | 94 TEST_F(WasmLoopAssignmentAnalyzerTest, NestedIf) { |
108 env.AddLocals(kAstI32, 5); | 95 num_locals = 5; |
109 for (int i = 0; i < 5; i++) { | 96 for (int i = 0; i < 5; i++) { |
110 byte code[] = {WASM_LOOP( | 97 byte code[] = {WASM_LOOP( |
111 1, WASM_IF_ELSE(WASM_SET_ZERO(0), WASM_SET_ZERO(i), WASM_SET_ZERO(1)))}; | 98 1, WASM_IF_ELSE(WASM_SET_ZERO(0), WASM_SET_ZERO(i), WASM_SET_ZERO(1)))}; |
112 BitVector* assigned = Analyze(code, code + arraysize(code)); | 99 BitVector* assigned = Analyze(code, code + arraysize(code)); |
113 for (int j = 0; j < assigned->length(); j++) { | 100 for (int j = 0; j < assigned->length(); j++) { |
114 bool expected = i == j || j == 0 || j == 1; | 101 bool expected = i == j || j == 0 || j == 1; |
115 CHECK_EQ(expected, assigned->Contains(j)); | 102 CHECK_EQ(expected, assigned->Contains(j)); |
116 } | 103 } |
117 } | 104 } |
118 } | 105 } |
119 | 106 |
120 | 107 |
121 static byte LEBByte(uint32_t val, byte which) { | 108 static byte LEBByte(uint32_t val, byte which) { |
122 byte b = (val >> (which * 7)) & 0x7F; | 109 byte b = (val >> (which * 7)) & 0x7F; |
123 if (val >> ((which + 1) * 7)) b |= 0x80; | 110 if (val >> ((which + 1) * 7)) b |= 0x80; |
124 return b; | 111 return b; |
125 } | 112 } |
126 | 113 |
127 | 114 |
128 TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { | 115 TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { |
129 env.AddLocals(kAstI32, 65000); | 116 num_locals = 65000; |
130 for (int i = 13; i < 65000; i = static_cast<int>(i * 1.5)) { | 117 for (int i = 13; i < 65000; i = static_cast<int>(i * 1.5)) { |
131 byte code[] = {kExprLoop, | 118 byte code[] = {kExprLoop, |
132 1, | 119 1, |
133 kExprSetLocal, | 120 kExprSetLocal, |
134 LEBByte(i, 0), | 121 LEBByte(i, 0), |
135 LEBByte(i, 1), | 122 LEBByte(i, 1), |
136 LEBByte(i, 2), | 123 LEBByte(i, 2), |
137 11, | 124 11, |
138 12, | 125 12, |
139 13}; | 126 13}; |
140 | 127 |
141 BitVector* assigned = Analyze(code, code + arraysize(code)); | 128 BitVector* assigned = Analyze(code, code + arraysize(code)); |
142 for (int j = 0; j < assigned->length(); j++) { | 129 for (int j = 0; j < assigned->length(); j++) { |
143 bool expected = i == j; | 130 bool expected = i == j; |
144 CHECK_EQ(expected, assigned->Contains(j)); | 131 CHECK_EQ(expected, assigned->Contains(j)); |
145 } | 132 } |
146 } | 133 } |
147 } | 134 } |
148 | 135 |
149 | 136 |
150 TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { | 137 TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { |
151 env.AddLocals(kAstI32, 3); | 138 num_locals = 3; |
152 byte code[] = { | 139 byte code[] = { |
153 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_SET_ZERO(1)))), | 140 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_SET_ZERO(1)))), |
154 WASM_SET_ZERO(0)}; | 141 WASM_SET_ZERO(0)}; |
155 | 142 |
156 BitVector* assigned = Analyze(code, code + arraysize(code)); | 143 BitVector* assigned = Analyze(code, code + arraysize(code)); |
157 for (int j = 0; j < assigned->length(); j++) { | 144 for (int j = 0; j < assigned->length(); j++) { |
158 bool expected = j == 1; | 145 bool expected = j == 1; |
159 CHECK_EQ(expected, assigned->Contains(j)); | 146 CHECK_EQ(expected, assigned->Contains(j)); |
160 } | 147 } |
161 } | 148 } |
162 | 149 |
163 | 150 |
164 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { | 151 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { |
165 env.AddLocals(kAstI32, 5); | 152 num_locals = 5; |
166 byte code[] = { | 153 byte code[] = { |
167 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), | 154 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), |
168 WASM_BRV(0, WASM_SET_LOCAL( | 155 WASM_BRV(0, WASM_SET_LOCAL( |
169 3, WASM_I32_SUB(WASM_GET_LOCAL(0), | 156 3, WASM_I32_SUB(WASM_GET_LOCAL(0), |
170 WASM_I8(1)))))), | 157 WASM_I8(1)))))), |
171 WASM_GET_LOCAL(0)}; | 158 WASM_GET_LOCAL(0)}; |
172 | 159 |
173 BitVector* assigned = Analyze(code, code + arraysize(code)); | 160 BitVector* assigned = Analyze(code, code + arraysize(code)); |
174 for (int j = 0; j < assigned->length(); j++) { | 161 for (int j = 0; j < assigned->length(); j++) { |
175 bool expected = j == 3; | 162 bool expected = j == 3; |
176 CHECK_EQ(expected, assigned->Contains(j)); | 163 CHECK_EQ(expected, assigned->Contains(j)); |
177 } | 164 } |
178 } | 165 } |
179 | 166 |
180 | 167 |
181 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop2) { | 168 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop2) { |
182 env.AddLocals(kAstI32, 3); | 169 num_locals = 6; |
183 const byte kIter = 0; | 170 const byte kIter = 0; |
184 env.AddLocals(kAstF32, 3); | |
185 const byte kSum = 3; | 171 const byte kSum = 3; |
186 | 172 |
187 byte code[] = {WASM_BLOCK( | 173 byte code[] = {WASM_BLOCK( |
188 3, | 174 3, |
189 WASM_WHILE( | 175 WASM_WHILE( |
190 WASM_GET_LOCAL(kIter), | 176 WASM_GET_LOCAL(kIter), |
191 WASM_BLOCK(2, WASM_SET_LOCAL( | 177 WASM_BLOCK(2, WASM_SET_LOCAL( |
192 kSum, WASM_F32_ADD( | 178 kSum, WASM_F32_ADD( |
193 WASM_GET_LOCAL(kSum), | 179 WASM_GET_LOCAL(kSum), |
194 WASM_LOAD_MEM(MachineType::Float32(), | 180 WASM_LOAD_MEM(MachineType::Float32(), |
195 WASM_GET_LOCAL(kIter)))), | 181 WASM_GET_LOCAL(kIter)))), |
196 WASM_SET_LOCAL(kIter, WASM_I32_SUB(WASM_GET_LOCAL(kIter), | 182 WASM_SET_LOCAL(kIter, WASM_I32_SUB(WASM_GET_LOCAL(kIter), |
197 WASM_I8(4))))), | 183 WASM_I8(4))))), |
198 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), | 184 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), |
199 WASM_GET_LOCAL(kIter))}; | 185 WASM_GET_LOCAL(kIter))}; |
200 | 186 |
201 BitVector* assigned = Analyze(code + 2, code + arraysize(code)); | 187 BitVector* assigned = Analyze(code + 2, code + arraysize(code)); |
202 for (int j = 0; j < assigned->length(); j++) { | 188 for (int j = 0; j < assigned->length(); j++) { |
203 bool expected = j == kIter || j == kSum; | 189 bool expected = j == kIter || j == kSum; |
204 CHECK_EQ(expected, assigned->Contains(j)); | 190 CHECK_EQ(expected, assigned->Contains(j)); |
205 } | 191 } |
206 } | 192 } |
207 | 193 |
208 | 194 |
209 } // namespace wasm | 195 } // namespace wasm |
210 } // namespace internal | 196 } // namespace internal |
211 } // namespace v8 | 197 } // namespace v8 |
OLD | NEW |