xref: /aosp_15_r20/art/compiler/optimizing/nodes_vector_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "base/arena_allocator.h"
18 #include "base/macros.h"
19 #include "nodes.h"
20 #include "optimizing_unit_test.h"
21 
22 namespace art HIDDEN {
23 
24 /**
25  * Fixture class for testing vector nodes.
26  */
27 class NodesVectorTest : public OptimizingUnitTest {
28  public:
NodesVectorTest()29   NodesVectorTest()
30       : graph_(CreateGraph()) {
31     BuildGraph();
32   }
33 
~NodesVectorTest()34   ~NodesVectorTest() { }
35 
BuildGraph()36   void BuildGraph() {
37     graph_->SetNumberOfVRegs(1);
38     entry_block_ = new (GetAllocator()) HBasicBlock(graph_);
39     exit_block_ = new (GetAllocator()) HBasicBlock(graph_);
40     graph_->AddBlock(entry_block_);
41     graph_->AddBlock(exit_block_);
42     graph_->SetEntryBlock(entry_block_);
43     graph_->SetExitBlock(exit_block_);
44     int8_parameter_ = MakeParam(DataType::Type::kInt8);
45     int16_parameter_ = MakeParam(DataType::Type::kInt16);
46     int32_parameter_ = MakeParam(DataType::Type::kInt32);
47   }
48 
49   // General building fields.
50   HGraph* graph_;
51 
52   HBasicBlock* entry_block_;
53   HBasicBlock* exit_block_;
54 
55   HInstruction* int8_parameter_;
56   HInstruction* int16_parameter_;
57   HInstruction* int32_parameter_;
58 };
59 
60 //
61 // The actual vector nodes tests.
62 //
63 
TEST(NodesVector,Alignment)64 TEST(NodesVector, Alignment) {
65   EXPECT_TRUE(Alignment(1, 0).IsAlignedAt(1));
66   EXPECT_FALSE(Alignment(1, 0).IsAlignedAt(2));
67 
68   EXPECT_TRUE(Alignment(2, 0).IsAlignedAt(1));
69   EXPECT_TRUE(Alignment(2, 1).IsAlignedAt(1));
70   EXPECT_TRUE(Alignment(2, 0).IsAlignedAt(2));
71   EXPECT_FALSE(Alignment(2, 1).IsAlignedAt(2));
72   EXPECT_FALSE(Alignment(2, 0).IsAlignedAt(4));
73   EXPECT_FALSE(Alignment(2, 1).IsAlignedAt(4));
74 
75   EXPECT_TRUE(Alignment(4, 0).IsAlignedAt(1));
76   EXPECT_TRUE(Alignment(4, 2).IsAlignedAt(1));
77   EXPECT_TRUE(Alignment(4, 0).IsAlignedAt(2));
78   EXPECT_TRUE(Alignment(4, 2).IsAlignedAt(2));
79   EXPECT_TRUE(Alignment(4, 0).IsAlignedAt(4));
80   EXPECT_FALSE(Alignment(4, 2).IsAlignedAt(4));
81   EXPECT_FALSE(Alignment(4, 0).IsAlignedAt(8));
82   EXPECT_FALSE(Alignment(4, 2).IsAlignedAt(8));
83 
84   EXPECT_TRUE(Alignment(16, 0).IsAlignedAt(1));
85   EXPECT_TRUE(Alignment(16, 0).IsAlignedAt(2));
86   EXPECT_TRUE(Alignment(16, 0).IsAlignedAt(4));
87   EXPECT_TRUE(Alignment(16, 8).IsAlignedAt(8));
88   EXPECT_TRUE(Alignment(16, 0).IsAlignedAt(16));
89   EXPECT_FALSE(Alignment(16, 1).IsAlignedAt(16));
90   EXPECT_FALSE(Alignment(16, 7).IsAlignedAt(16));
91   EXPECT_FALSE(Alignment(16, 0).IsAlignedAt(32));
92 
93   EXPECT_EQ(16u, Alignment(16, 0).Base());
94   EXPECT_EQ(0u, Alignment(16, 0).Offset());
95   EXPECT_EQ(4u, Alignment(16, 4).Offset());
96 }
97 
TEST(NodesVector,AlignmentEQ)98 TEST(NodesVector, AlignmentEQ) {
99   EXPECT_TRUE(Alignment(2, 0) == Alignment(2, 0));
100   EXPECT_TRUE(Alignment(2, 1) == Alignment(2, 1));
101   EXPECT_TRUE(Alignment(4, 0) == Alignment(4, 0));
102   EXPECT_TRUE(Alignment(4, 2) == Alignment(4, 2));
103 
104   EXPECT_FALSE(Alignment(4, 0) == Alignment(2, 0));
105   EXPECT_FALSE(Alignment(4, 0) == Alignment(4, 1));
106   EXPECT_FALSE(Alignment(4, 0) == Alignment(8, 0));
107 }
108 
TEST(NodesVector,AlignmentString)109 TEST(NodesVector, AlignmentString) {
110   EXPECT_STREQ("ALIGN(1,0)", Alignment(1, 0).ToString().c_str());
111 
112   EXPECT_STREQ("ALIGN(2,0)", Alignment(2, 0).ToString().c_str());
113   EXPECT_STREQ("ALIGN(2,1)", Alignment(2, 1).ToString().c_str());
114 
115   EXPECT_STREQ("ALIGN(16,0)", Alignment(16, 0).ToString().c_str());
116   EXPECT_STREQ("ALIGN(16,1)", Alignment(16, 1).ToString().c_str());
117   EXPECT_STREQ("ALIGN(16,8)", Alignment(16, 8).ToString().c_str());
118   EXPECT_STREQ("ALIGN(16,9)", Alignment(16, 9).ToString().c_str());
119 }
120 
TEST_F(NodesVectorTest,VectorOperationProperties)121 TEST_F(NodesVectorTest, VectorOperationProperties) {
122   HVecOperation* v0 = new (GetAllocator())
123       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
124   HVecOperation* v1 = new (GetAllocator())
125       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
126   HVecOperation* v2 = new (GetAllocator())
127       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 2, kNoDexPc);
128   HVecOperation* v3 = new (GetAllocator())
129       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt16, 4, kNoDexPc);
130   HVecOperation* v4 = new (GetAllocator()) HVecStore(
131       GetAllocator(),
132       int32_parameter_,
133       int32_parameter_,
134       v0,
135       DataType::Type::kInt32,
136       SideEffects::ArrayWriteOfType(DataType::Type::kInt32),
137       4,
138       kNoDexPc);
139 
140   EXPECT_TRUE(v0->Equals(v0));
141   EXPECT_TRUE(v1->Equals(v1));
142   EXPECT_TRUE(v2->Equals(v2));
143   EXPECT_TRUE(v3->Equals(v3));
144   EXPECT_TRUE(v4->Equals(v4));
145 
146   EXPECT_TRUE(v0->Equals(v1));
147   EXPECT_FALSE(v0->Equals(v2));  // different vector lengths
148   EXPECT_FALSE(v0->Equals(v3));  // different packed types
149   EXPECT_FALSE(v0->Equals(v4));  // different kinds
150 
151   EXPECT_TRUE(v1->Equals(v0));  // switch operands
152   EXPECT_FALSE(v4->Equals(v0));
153 
154   EXPECT_EQ(4u, v0->GetVectorLength());
155   EXPECT_EQ(4u, v1->GetVectorLength());
156   EXPECT_EQ(2u, v2->GetVectorLength());
157   EXPECT_EQ(4u, v3->GetVectorLength());
158   EXPECT_EQ(4u, v4->GetVectorLength());
159 
160   EXPECT_EQ(DataType::Type::kFloat64, v0->GetType());
161   EXPECT_EQ(DataType::Type::kFloat64, v1->GetType());
162   EXPECT_EQ(DataType::Type::kFloat64, v2->GetType());
163   EXPECT_EQ(DataType::Type::kFloat64, v3->GetType());
164   EXPECT_EQ(DataType::Type::kFloat64, v4->GetType());
165 
166   EXPECT_EQ(DataType::Type::kInt32, v0->GetPackedType());
167   EXPECT_EQ(DataType::Type::kInt32, v1->GetPackedType());
168   EXPECT_EQ(DataType::Type::kInt32, v2->GetPackedType());
169   EXPECT_EQ(DataType::Type::kInt16, v3->GetPackedType());
170   EXPECT_EQ(DataType::Type::kInt32, v4->GetPackedType());
171 
172   EXPECT_EQ(16u, v0->GetVectorNumberOfBytes());
173   EXPECT_EQ(16u, v1->GetVectorNumberOfBytes());
174   EXPECT_EQ(8u, v2->GetVectorNumberOfBytes());
175   EXPECT_EQ(8u, v3->GetVectorNumberOfBytes());
176   EXPECT_EQ(16u, v4->GetVectorNumberOfBytes());
177 
178   EXPECT_FALSE(v0->CanBeMoved());
179   EXPECT_FALSE(v1->CanBeMoved());
180   EXPECT_FALSE(v2->CanBeMoved());
181   EXPECT_FALSE(v3->CanBeMoved());
182   EXPECT_FALSE(v4->CanBeMoved());
183 }
184 
TEST_F(NodesVectorTest,VectorAlignmentAndStringCharAtMatterOnLoad)185 TEST_F(NodesVectorTest, VectorAlignmentAndStringCharAtMatterOnLoad) {
186   HVecLoad* v0 = new (GetAllocator()) HVecLoad(GetAllocator(),
187                                                int32_parameter_,
188                                                int32_parameter_,
189                                                DataType::Type::kInt32,
190                                                SideEffects::ArrayReadOfType(DataType::Type::kInt32),
191                                                4,
192                                                /*is_string_char_at*/ false,
193                                                kNoDexPc);
194   HVecLoad* v1 = new (GetAllocator()) HVecLoad(GetAllocator(),
195                                                int32_parameter_,
196                                                int32_parameter_,
197                                                DataType::Type::kInt32,
198                                                SideEffects::ArrayReadOfType(DataType::Type::kInt32),
199                                                4,
200                                                /*is_string_char_at*/ false,
201                                                kNoDexPc);
202   HVecLoad* v2 = new (GetAllocator()) HVecLoad(GetAllocator(),
203                                                int32_parameter_,
204                                                int32_parameter_,
205                                                DataType::Type::kInt32,
206                                                SideEffects::ArrayReadOfType(DataType::Type::kInt32),
207                                                 4,
208                                                /*is_string_char_at*/ true,
209                                                kNoDexPc);
210 
211   EXPECT_TRUE(v0->CanBeMoved());
212   EXPECT_TRUE(v1->CanBeMoved());
213   EXPECT_TRUE(v2->CanBeMoved());
214 
215   EXPECT_FALSE(v0->IsStringCharAt());
216   EXPECT_FALSE(v1->IsStringCharAt());
217   EXPECT_TRUE(v2->IsStringCharAt());
218 
219   EXPECT_TRUE(v0->Equals(v0));
220   EXPECT_TRUE(v1->Equals(v1));
221   EXPECT_TRUE(v2->Equals(v2));
222 
223   EXPECT_TRUE(v0->Equals(v1));
224   EXPECT_FALSE(v0->Equals(v2));  // different is_string_char_at
225 
226   EXPECT_TRUE(v0->GetAlignment() == Alignment(4, 0));
227   EXPECT_TRUE(v1->GetAlignment() == Alignment(4, 0));
228   EXPECT_TRUE(v2->GetAlignment() == Alignment(4, 0));
229 
230   v1->SetAlignment(Alignment(8, 0));
231 
232   EXPECT_TRUE(v1->GetAlignment() == Alignment(8, 0));
233 
234   EXPECT_FALSE(v0->Equals(v1));  // no longer equal
235 }
236 
TEST_F(NodesVectorTest,VectorAlignmentMattersOnStore)237 TEST_F(NodesVectorTest, VectorAlignmentMattersOnStore) {
238   HVecOperation* p0 = new (GetAllocator())
239       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
240   HVecStore* v0 = new (GetAllocator()) HVecStore(
241       GetAllocator(),
242       int32_parameter_,
243       int32_parameter_,
244       p0,
245       DataType::Type::kInt32,
246       SideEffects::ArrayWriteOfType(DataType::Type::kInt32),
247       4,
248       kNoDexPc);
249   HVecStore* v1 = new (GetAllocator()) HVecStore(
250       GetAllocator(),
251       int32_parameter_,
252       int32_parameter_,
253       p0,
254       DataType::Type::kInt32,
255       SideEffects::ArrayWriteOfType(DataType::Type::kInt32),
256       4,
257       kNoDexPc);
258 
259   EXPECT_FALSE(v0->CanBeMoved());
260   EXPECT_FALSE(v1->CanBeMoved());
261 
262   EXPECT_TRUE(v0->Equals(v1));
263 
264   EXPECT_TRUE(v0->GetAlignment() == Alignment(4, 0));
265   EXPECT_TRUE(v1->GetAlignment() == Alignment(4, 0));
266 
267   v1->SetAlignment(Alignment(8, 0));
268 
269   EXPECT_TRUE(v1->GetAlignment() == Alignment(8, 0));
270 
271   EXPECT_FALSE(v0->Equals(v1));  // no longer equal
272 }
273 
TEST_F(NodesVectorTest,VectorAttributesMatterOnHalvingAdd)274 TEST_F(NodesVectorTest, VectorAttributesMatterOnHalvingAdd) {
275   HVecOperation* u0 = new (GetAllocator())
276       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kUint32, 4, kNoDexPc);
277   HVecOperation* u1 = new (GetAllocator())
278       HVecReplicateScalar(GetAllocator(), int16_parameter_, DataType::Type::kUint16, 8, kNoDexPc);
279   HVecOperation* u2 = new (GetAllocator())
280       HVecReplicateScalar(GetAllocator(), int8_parameter_, DataType::Type::kUint8, 16, kNoDexPc);
281 
282   HVecOperation* p0 = new (GetAllocator())
283       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
284   HVecOperation* p1 = new (GetAllocator())
285       HVecReplicateScalar(GetAllocator(), int16_parameter_, DataType::Type::kInt16, 8, kNoDexPc);
286   HVecOperation* p2 = new (GetAllocator())
287       HVecReplicateScalar(GetAllocator(), int8_parameter_, DataType::Type::kInt8, 16, kNoDexPc);
288 
289   HVecHalvingAdd* v0 = new (GetAllocator()) HVecHalvingAdd(
290       GetAllocator(), u0, u0, DataType::Type::kUint32, 4, /*is_rounded*/ true, kNoDexPc);
291   HVecHalvingAdd* v1 = new (GetAllocator()) HVecHalvingAdd(
292       GetAllocator(), u0, u0, DataType::Type::kUint32, 4, /*is_rounded*/ false, kNoDexPc);
293   HVecHalvingAdd* v2 = new (GetAllocator()) HVecHalvingAdd(
294       GetAllocator(), p0, p0, DataType::Type::kInt32, 4, /*is_rounded*/ true, kNoDexPc);
295   HVecHalvingAdd* v3 = new (GetAllocator()) HVecHalvingAdd(
296       GetAllocator(), p0, p0, DataType::Type::kInt32, 4, /*is_rounded*/ false, kNoDexPc);
297 
298   HVecHalvingAdd* v4 = new (GetAllocator()) HVecHalvingAdd(
299       GetAllocator(), u1, u1, DataType::Type::kUint16, 8, /*is_rounded*/ true, kNoDexPc);
300   HVecHalvingAdd* v5 = new (GetAllocator()) HVecHalvingAdd(
301       GetAllocator(), u1, u1, DataType::Type::kUint16, 8, /*is_rounded*/ false, kNoDexPc);
302   HVecHalvingAdd* v6 = new (GetAllocator()) HVecHalvingAdd(
303       GetAllocator(), p1, p1, DataType::Type::kInt16, 8, /*is_rounded*/ true, kNoDexPc);
304   HVecHalvingAdd* v7 = new (GetAllocator()) HVecHalvingAdd(
305       GetAllocator(), p1, p1, DataType::Type::kInt16, 8, /*is_rounded*/ false, kNoDexPc);
306 
307   HVecHalvingAdd* v8 = new (GetAllocator()) HVecHalvingAdd(
308       GetAllocator(), u2, u2, DataType::Type::kUint8, 16, /*is_rounded*/ true, kNoDexPc);
309   HVecHalvingAdd* v9 = new (GetAllocator()) HVecHalvingAdd(
310       GetAllocator(), u2, u2, DataType::Type::kUint8, 16, /*is_rounded*/ false, kNoDexPc);
311   HVecHalvingAdd* v10 = new (GetAllocator()) HVecHalvingAdd(
312       GetAllocator(), p2, p2, DataType::Type::kInt8, 16, /*is_rounded*/ true, kNoDexPc);
313   HVecHalvingAdd* v11 = new (GetAllocator()) HVecHalvingAdd(
314       GetAllocator(), p2, p2, DataType::Type::kInt8, 16, /*is_rounded*/ false, kNoDexPc);
315 
316   HVecHalvingAdd* hadd_insns[] = { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 };
317 
318   EXPECT_FALSE(u0->CanBeMoved());
319   EXPECT_FALSE(u1->CanBeMoved());
320   EXPECT_FALSE(u2->CanBeMoved());
321   EXPECT_FALSE(p0->CanBeMoved());
322   EXPECT_FALSE(p1->CanBeMoved());
323   EXPECT_FALSE(p2->CanBeMoved());
324 
325   for (HVecHalvingAdd* hadd_insn : hadd_insns) {
326     EXPECT_TRUE(hadd_insn->CanBeMoved());
327   }
328 
329   EXPECT_TRUE(v0->IsRounded());
330   EXPECT_TRUE(!v1->IsRounded());
331   EXPECT_TRUE(v2->IsRounded());
332   EXPECT_TRUE(!v3->IsRounded());
333   EXPECT_TRUE(v4->IsRounded());
334   EXPECT_TRUE(!v5->IsRounded());
335   EXPECT_TRUE(v6->IsRounded());
336   EXPECT_TRUE(!v7->IsRounded());
337   EXPECT_TRUE(v8->IsRounded());
338   EXPECT_TRUE(!v9->IsRounded());
339   EXPECT_TRUE(v10->IsRounded());
340   EXPECT_TRUE(!v11->IsRounded());
341 
342   for (HVecHalvingAdd* hadd_insn1 : hadd_insns) {
343     for (HVecHalvingAdd* hadd_insn2 : hadd_insns) {
344       EXPECT_EQ(hadd_insn1 == hadd_insn2, hadd_insn1->Equals(hadd_insn2));
345     }
346   }
347 }
348 
TEST_F(NodesVectorTest,VectorOperationMattersOnMultiplyAccumulate)349 TEST_F(NodesVectorTest, VectorOperationMattersOnMultiplyAccumulate) {
350   HVecOperation* v0 = new (GetAllocator())
351       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
352 
353   HVecMultiplyAccumulate* v1 = new (GetAllocator()) HVecMultiplyAccumulate(
354       GetAllocator(), HInstruction::kAdd, v0, v0, v0, DataType::Type::kInt32, 4, kNoDexPc);
355   HVecMultiplyAccumulate* v2 = new (GetAllocator()) HVecMultiplyAccumulate(
356       GetAllocator(), HInstruction::kSub, v0, v0, v0, DataType::Type::kInt32, 4, kNoDexPc);
357   HVecMultiplyAccumulate* v3 = new (GetAllocator()) HVecMultiplyAccumulate(
358       GetAllocator(), HInstruction::kAdd, v0, v0, v0, DataType::Type::kInt32, 2, kNoDexPc);
359 
360   EXPECT_FALSE(v0->CanBeMoved());
361   EXPECT_TRUE(v1->CanBeMoved());
362   EXPECT_TRUE(v2->CanBeMoved());
363   EXPECT_TRUE(v3->CanBeMoved());
364 
365   EXPECT_EQ(HInstruction::kAdd, v1->GetOpKind());
366   EXPECT_EQ(HInstruction::kSub, v2->GetOpKind());
367   EXPECT_EQ(HInstruction::kAdd, v3->GetOpKind());
368 
369   EXPECT_TRUE(v1->Equals(v1));
370   EXPECT_TRUE(v2->Equals(v2));
371   EXPECT_TRUE(v3->Equals(v3));
372 
373   EXPECT_FALSE(v1->Equals(v2));  // different operators
374   EXPECT_FALSE(v1->Equals(v3));  // different vector lengths
375 }
376 
TEST_F(NodesVectorTest,VectorKindMattersOnReduce)377 TEST_F(NodesVectorTest, VectorKindMattersOnReduce) {
378   HVecOperation* v0 = new (GetAllocator())
379       HVecReplicateScalar(GetAllocator(), int32_parameter_, DataType::Type::kInt32, 4, kNoDexPc);
380 
381   HVecReduce* v1 = new (GetAllocator()) HVecReduce(
382       GetAllocator(), v0, DataType::Type::kInt32, 4, HVecReduce::kSum, kNoDexPc);
383   HVecReduce* v2 = new (GetAllocator()) HVecReduce(
384       GetAllocator(), v0, DataType::Type::kInt32, 4, HVecReduce::kMin, kNoDexPc);
385   HVecReduce* v3 = new (GetAllocator()) HVecReduce(
386       GetAllocator(), v0, DataType::Type::kInt32, 4, HVecReduce::kMax, kNoDexPc);
387 
388   EXPECT_FALSE(v0->CanBeMoved());
389   EXPECT_TRUE(v1->CanBeMoved());
390   EXPECT_TRUE(v2->CanBeMoved());
391   EXPECT_TRUE(v3->CanBeMoved());
392 
393   EXPECT_EQ(HVecReduce::kSum, v1->GetReductionKind());
394   EXPECT_EQ(HVecReduce::kMin, v2->GetReductionKind());
395   EXPECT_EQ(HVecReduce::kMax, v3->GetReductionKind());
396 
397   EXPECT_TRUE(v1->Equals(v1));
398   EXPECT_TRUE(v2->Equals(v2));
399   EXPECT_TRUE(v3->Equals(v3));
400 
401   EXPECT_FALSE(v1->Equals(v2));  // different kinds
402   EXPECT_FALSE(v1->Equals(v3));
403 }
404 
405 }  // namespace art
406