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() { | |
27 init_env(&env, sigs.v_v()); | |
28 } | |
29 | |
30 TestSignatures sigs; | 26 TestSignatures sigs; |
31 FunctionEnv env; | 27 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 | 28 |
43 BitVector* Analyze(const byte* start, const byte* end) { | 29 BitVector* Analyze(const byte* start, const byte* end) { |
44 return AnalyzeLoopAssignmentForTesting(zone(), &env, start, end); | 30 return AnalyzeLoopAssignmentForTesting(zone(), num_locals, start, end); |
45 } | 31 } |
46 }; | 32 }; |
47 | 33 |
48 | 34 |
49 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty0) { | 35 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty0) { |
50 byte code[] = { 0 }; | 36 byte code[] = { 0 }; |
51 BitVector* assigned = Analyze(code, code); | 37 BitVector* assigned = Analyze(code, code); |
52 CHECK_NULL(assigned); | 38 CHECK_NULL(assigned); |
53 } | 39 } |
54 | 40 |
55 | 41 |
56 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty1) { | 42 TEST_F(WasmLoopAssignmentAnalyzerTest, Empty1) { |
57 byte code[] = {kExprLoop, 0}; | 43 byte code[] = {kExprLoop, 0}; |
58 for (int i = 0; i < 5; i++) { | 44 for (int i = 0; i < 5; i++) { |
59 BitVector* assigned = Analyze(code, code + arraysize(code)); | 45 BitVector* assigned = Analyze(code, code + arraysize(code)); |
60 for (int j = 0; j < assigned->length(); j++) { | 46 for (int j = 0; j < assigned->length(); j++) { |
61 CHECK_EQ(false, assigned->Contains(j)); | 47 CHECK_EQ(false, assigned->Contains(j)); |
62 } | 48 } |
63 env.AddLocals(kAstI32, 1); | 49 num_locals++; |
64 } | 50 } |
65 } | 51 } |
66 | 52 |
67 | 53 |
68 TEST_F(WasmLoopAssignmentAnalyzerTest, One) { | 54 TEST_F(WasmLoopAssignmentAnalyzerTest, One) { |
69 env.AddLocals(kAstI32, 5); | 55 num_locals = 5; |
70 for (int i = 0; i < 5; i++) { | 56 for (int i = 0; i < 5; i++) { |
71 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i))}; | 57 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i))}; |
72 BitVector* assigned = Analyze(code, code + arraysize(code)); | 58 BitVector* assigned = Analyze(code, code + arraysize(code)); |
73 for (int j = 0; j < assigned->length(); j++) { | 59 for (int j = 0; j < assigned->length(); j++) { |
74 CHECK_EQ(j == i, assigned->Contains(j)); | 60 CHECK_EQ(j == i, assigned->Contains(j)); |
75 } | 61 } |
76 } | 62 } |
77 } | 63 } |
78 | 64 |
79 | 65 |
80 TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { | 66 TEST_F(WasmLoopAssignmentAnalyzerTest, OneBeyond) { |
81 env.AddLocals(kAstI32, 5); | 67 num_locals = 5; |
82 for (int i = 0; i < 5; i++) { | 68 for (int i = 0; i < 5; i++) { |
83 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i)), WASM_SET_ZERO(1)}; | 69 byte code[] = {WASM_LOOP(1, WASM_SET_ZERO(i)), WASM_SET_ZERO(1)}; |
84 BitVector* assigned = Analyze(code, code + arraysize(code)); | 70 BitVector* assigned = Analyze(code, code + arraysize(code)); |
85 for (int j = 0; j < assigned->length(); j++) { | 71 for (int j = 0; j < assigned->length(); j++) { |
86 CHECK_EQ(j == i, assigned->Contains(j)); | 72 CHECK_EQ(j == i, assigned->Contains(j)); |
87 } | 73 } |
88 } | 74 } |
89 } | 75 } |
90 | 76 |
91 | 77 |
92 TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { | 78 TEST_F(WasmLoopAssignmentAnalyzerTest, Two) { |
93 env.AddLocals(kAstI32, 5); | 79 num_locals = 5; |
94 for (int i = 0; i < 5; i++) { | 80 for (int i = 0; i < 5; i++) { |
95 for (int j = 0; j < 5; j++) { | 81 for (int j = 0; j < 5; j++) { |
96 byte code[] = {WASM_LOOP(2, WASM_SET_ZERO(i), WASM_SET_ZERO(j))}; | 82 byte code[] = {WASM_LOOP(2, WASM_SET_ZERO(i), WASM_SET_ZERO(j))}; |
97 BitVector* assigned = Analyze(code, code + arraysize(code)); | 83 BitVector* assigned = Analyze(code, code + arraysize(code)); |
98 for (int k = 0; k < assigned->length(); k++) { | 84 for (int k = 0; k < assigned->length(); k++) { |
99 bool expected = k == i || k == j; | 85 bool expected = k == i || k == j; |
100 CHECK_EQ(expected, assigned->Contains(k)); | 86 CHECK_EQ(expected, assigned->Contains(k)); |
101 } | 87 } |
102 } | 88 } |
103 } | 89 } |
104 } | 90 } |
105 | 91 |
106 | 92 |
107 TEST_F(WasmLoopAssignmentAnalyzerTest, NestedIf) { | 93 TEST_F(WasmLoopAssignmentAnalyzerTest, NestedIf) { |
108 env.AddLocals(kAstI32, 5); | 94 num_locals = 5; |
109 for (int i = 0; i < 5; i++) { | 95 for (int i = 0; i < 5; i++) { |
110 byte code[] = {WASM_LOOP( | 96 byte code[] = {WASM_LOOP( |
111 1, WASM_IF_ELSE(WASM_SET_ZERO(0), WASM_SET_ZERO(i), WASM_SET_ZERO(1)))}; | 97 1, WASM_IF_ELSE(WASM_SET_ZERO(0), WASM_SET_ZERO(i), WASM_SET_ZERO(1)))}; |
112 BitVector* assigned = Analyze(code, code + arraysize(code)); | 98 BitVector* assigned = Analyze(code, code + arraysize(code)); |
113 for (int j = 0; j < assigned->length(); j++) { | 99 for (int j = 0; j < assigned->length(); j++) { |
114 bool expected = i == j || j == 0 || j == 1; | 100 bool expected = i == j || j == 0 || j == 1; |
115 CHECK_EQ(expected, assigned->Contains(j)); | 101 CHECK_EQ(expected, assigned->Contains(j)); |
116 } | 102 } |
117 } | 103 } |
118 } | 104 } |
119 | 105 |
120 | 106 |
121 static byte LEBByte(uint32_t val, byte which) { | 107 static byte LEBByte(uint32_t val, byte which) { |
122 byte b = (val >> (which * 7)) & 0x7F; | 108 byte b = (val >> (which * 7)) & 0x7F; |
123 if (val >> ((which + 1) * 7)) b |= 0x80; | 109 if (val >> ((which + 1) * 7)) b |= 0x80; |
124 return b; | 110 return b; |
125 } | 111 } |
126 | 112 |
127 | 113 |
128 TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { | 114 TEST_F(WasmLoopAssignmentAnalyzerTest, BigLocal) { |
129 env.AddLocals(kAstI32, 65000); | 115 num_locals = 65000; |
130 for (int i = 13; i < 65000; i = static_cast<int>(i * 1.5)) { | 116 for (int i = 13; i < 65000; i = static_cast<int>(i * 1.5)) { |
131 byte code[] = {kExprLoop, | 117 byte code[] = {kExprLoop, |
132 1, | 118 1, |
133 kExprSetLocal, | 119 kExprSetLocal, |
134 LEBByte(i, 0), | 120 LEBByte(i, 0), |
135 LEBByte(i, 1), | 121 LEBByte(i, 1), |
136 LEBByte(i, 2), | 122 LEBByte(i, 2), |
137 11, | 123 11, |
138 12, | 124 12, |
139 13}; | 125 13}; |
140 | 126 |
141 BitVector* assigned = Analyze(code, code + arraysize(code)); | 127 BitVector* assigned = Analyze(code, code + arraysize(code)); |
142 for (int j = 0; j < assigned->length(); j++) { | 128 for (int j = 0; j < assigned->length(); j++) { |
143 bool expected = i == j; | 129 bool expected = i == j; |
144 CHECK_EQ(expected, assigned->Contains(j)); | 130 CHECK_EQ(expected, assigned->Contains(j)); |
145 } | 131 } |
146 } | 132 } |
147 } | 133 } |
148 | 134 |
149 | 135 |
150 TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { | 136 TEST_F(WasmLoopAssignmentAnalyzerTest, Break) { |
151 env.AddLocals(kAstI32, 3); | 137 num_locals = 3; |
152 byte code[] = { | 138 byte code[] = { |
153 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_SET_ZERO(1)))), | 139 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(1, WASM_SET_ZERO(1)))), |
154 WASM_SET_ZERO(0)}; | 140 WASM_SET_ZERO(0)}; |
155 | 141 |
156 BitVector* assigned = Analyze(code, code + arraysize(code)); | 142 BitVector* assigned = Analyze(code, code + arraysize(code)); |
157 for (int j = 0; j < assigned->length(); j++) { | 143 for (int j = 0; j < assigned->length(); j++) { |
158 bool expected = j == 1; | 144 bool expected = j == 1; |
159 CHECK_EQ(expected, assigned->Contains(j)); | 145 CHECK_EQ(expected, assigned->Contains(j)); |
160 } | 146 } |
161 } | 147 } |
162 | 148 |
163 | 149 |
164 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { | 150 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop1) { |
165 env.AddLocals(kAstI32, 5); | 151 num_locals = 5; |
166 byte code[] = { | 152 byte code[] = { |
167 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), | 153 WASM_LOOP(1, WASM_IF(WASM_GET_LOCAL(0), |
168 WASM_BRV(0, WASM_SET_LOCAL( | 154 WASM_BRV(0, WASM_SET_LOCAL( |
169 3, WASM_I32_SUB(WASM_GET_LOCAL(0), | 155 3, WASM_I32_SUB(WASM_GET_LOCAL(0), |
170 WASM_I8(1)))))), | 156 WASM_I8(1)))))), |
171 WASM_GET_LOCAL(0)}; | 157 WASM_GET_LOCAL(0)}; |
172 | 158 |
173 BitVector* assigned = Analyze(code, code + arraysize(code)); | 159 BitVector* assigned = Analyze(code, code + arraysize(code)); |
174 for (int j = 0; j < assigned->length(); j++) { | 160 for (int j = 0; j < assigned->length(); j++) { |
175 bool expected = j == 3; | 161 bool expected = j == 3; |
176 CHECK_EQ(expected, assigned->Contains(j)); | 162 CHECK_EQ(expected, assigned->Contains(j)); |
177 } | 163 } |
178 } | 164 } |
179 | 165 |
180 | 166 |
181 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop2) { | 167 TEST_F(WasmLoopAssignmentAnalyzerTest, Loop2) { |
182 env.AddLocals(kAstI32, 3); | 168 num_locals = 6; |
183 const byte kIter = 0; | 169 const byte kIter = 0; |
184 env.AddLocals(kAstF32, 3); | |
185 const byte kSum = 3; | 170 const byte kSum = 3; |
186 | 171 |
187 byte code[] = {WASM_BLOCK( | 172 byte code[] = {WASM_BLOCK( |
188 3, | 173 3, |
189 WASM_WHILE( | 174 WASM_WHILE( |
190 WASM_GET_LOCAL(kIter), | 175 WASM_GET_LOCAL(kIter), |
191 WASM_BLOCK(2, WASM_SET_LOCAL( | 176 WASM_BLOCK(2, WASM_SET_LOCAL( |
192 kSum, WASM_F32_ADD( | 177 kSum, WASM_F32_ADD( |
193 WASM_GET_LOCAL(kSum), | 178 WASM_GET_LOCAL(kSum), |
194 WASM_LOAD_MEM(MachineType::Float32(), | 179 WASM_LOAD_MEM(MachineType::Float32(), |
195 WASM_GET_LOCAL(kIter)))), | 180 WASM_GET_LOCAL(kIter)))), |
196 WASM_SET_LOCAL(kIter, WASM_I32_SUB(WASM_GET_LOCAL(kIter), | 181 WASM_SET_LOCAL(kIter, WASM_I32_SUB(WASM_GET_LOCAL(kIter), |
197 WASM_I8(4))))), | 182 WASM_I8(4))))), |
198 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), | 183 WASM_STORE_MEM(MachineType::Float32(), WASM_ZERO, WASM_GET_LOCAL(kSum)), |
199 WASM_GET_LOCAL(kIter))}; | 184 WASM_GET_LOCAL(kIter))}; |
200 | 185 |
201 BitVector* assigned = Analyze(code + 2, code + arraysize(code)); | 186 BitVector* assigned = Analyze(code + 2, code + arraysize(code)); |
202 for (int j = 0; j < assigned->length(); j++) { | 187 for (int j = 0; j < assigned->length(); j++) { |
203 bool expected = j == kIter || j == kSum; | 188 bool expected = j == kIter || j == kSum; |
204 CHECK_EQ(expected, assigned->Contains(j)); | 189 CHECK_EQ(expected, assigned->Contains(j)); |
205 } | 190 } |
206 } | 191 } |
207 | 192 |
208 | 193 |
209 } // namespace wasm | 194 } // namespace wasm |
210 } // namespace internal | 195 } // namespace internal |
211 } // namespace v8 | 196 } // namespace v8 |
OLD | NEW |