• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 public class Main {
main(String[] args)18     public static void main(String[] args) {
19         $noinline$testVectorAndNonVector();
20     }
21 
22     // Before loop optimization we only had an array get. After it, we optimized to also have
23     // VecLoad operations. This happens consistently only for Arm64. Arm32 vectorizes consistently
24     // but it also removes the ArrayGet. X86/X86_64 doesn't vectorize consistently (other
25     // vectorization tests also ignore x86/x86_64).
26 
27     /// CHECK-START-ARM64: void Main.$noinline$testVectorAndNonVector() loop_optimization (before)
28     /// CHECK:     ArrayGet
29 
30     /// CHECK-START-ARM64: void Main.$noinline$testVectorAndNonVector() loop_optimization (after)
31     /// CHECK:     ArrayGet
32 
33     /// CHECK-START-ARM64: void Main.$noinline$testVectorAndNonVector() loop_optimization (before)
34     /// CHECK-NOT: VecLoad
35 
36     /// CHECK-START-ARM64: void Main.$noinline$testVectorAndNonVector() loop_optimization (after)
37     /// CHECK:     VecLoad
38 
39     // In LoadStoreElimination both ArrayGet and VecLoad have the same heap location. We will try to
40     // replace the ArrayGet with the constant 0. The crash happens when we want to do the same with
41     // the vector operation, changing the vector operation to a scalar.
42 
43     /// CHECK-START-ARM64: void Main.$noinline$testVectorAndNonVector() load_store_elimination (before)
44     /// CHECK-DAG:     VecLoad outer_loop:<<VecBlock:B\d+>>
45     /// CHECK-DAG:     ArrayGet outer_loop:<<ScalarBlock:B\d+>>
46     /// CHECK-EVAL:    "<<VecBlock>>" == "<<ScalarBlock>>"
47 
$noinline$testVectorAndNonVector()48     private static void $noinline$testVectorAndNonVector() {
49         int[] result = new int[2];
50         int[] source = new int[12];
51 
52         // This will get vectorized.
53         for (int i = 0; i < result.length; ++i) {
54             int value = 0;
55             // Always true but needed to repro a crash since we need Phis.
56             if (i + 10 < source.length) {
57                 for (int j = 0; j < 10; j++) {
58                     value += Math.abs(source[i + j]);
59                 }
60             }
61             result[i] = value;
62         }
63     }
64 }
65