1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker // This test checks that values (e.g. induction variable) are properly set for SuspendCheck 18*795d594fSAndroid Build Coastguard Worker // environment in the SIMD loop; by causing asynchronous deoptimization in debuggable mode we 19*795d594fSAndroid Build Coastguard Worker // we observe the values. 20*795d594fSAndroid Build Coastguard Worker public class SimdLoop implements Runnable { 21*795d594fSAndroid Build Coastguard Worker static final int numberOfThreads = 2; 22*795d594fSAndroid Build Coastguard Worker volatile static boolean sExitFlag = false; 23*795d594fSAndroid Build Coastguard Worker volatile static boolean sEntered = false; 24*795d594fSAndroid Build Coastguard Worker int threadIndex; 25*795d594fSAndroid Build Coastguard Worker SimdLoop(int index)26*795d594fSAndroid Build Coastguard Worker SimdLoop(int index) { 27*795d594fSAndroid Build Coastguard Worker threadIndex = index; 28*795d594fSAndroid Build Coastguard Worker } 29*795d594fSAndroid Build Coastguard Worker main()30*795d594fSAndroid Build Coastguard Worker public static void main() throws Exception { 31*795d594fSAndroid Build Coastguard Worker final Thread[] threads = new Thread[numberOfThreads]; 32*795d594fSAndroid Build Coastguard Worker for (int t = 0; t < threads.length; t++) { 33*795d594fSAndroid Build Coastguard Worker threads[t] = new Thread(new SimdLoop(t)); 34*795d594fSAndroid Build Coastguard Worker threads[t].start(); 35*795d594fSAndroid Build Coastguard Worker } 36*795d594fSAndroid Build Coastguard Worker for (Thread t : threads) { 37*795d594fSAndroid Build Coastguard Worker t.join(); 38*795d594fSAndroid Build Coastguard Worker } 39*795d594fSAndroid Build Coastguard Worker 40*795d594fSAndroid Build Coastguard Worker System.out.println("Simd loop finishing"); 41*795d594fSAndroid Build Coastguard Worker } 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker static final int kArraySize = 3000000; 44*795d594fSAndroid Build Coastguard Worker expectEqual(int value, int expected)45*795d594fSAndroid Build Coastguard Worker public void expectEqual(int value, int expected) { 46*795d594fSAndroid Build Coastguard Worker if (value != expected) { 47*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + value); 48*795d594fSAndroid Build Coastguard Worker } 49*795d594fSAndroid Build Coastguard Worker } 50*795d594fSAndroid Build Coastguard Worker $noinline$busyLoop()51*795d594fSAndroid Build Coastguard Worker public void $noinline$busyLoop() { 52*795d594fSAndroid Build Coastguard Worker Main.assertIsManaged(); 53*795d594fSAndroid Build Coastguard Worker 54*795d594fSAndroid Build Coastguard Worker int[] array = new int[kArraySize]; 55*795d594fSAndroid Build Coastguard Worker sEntered = true; 56*795d594fSAndroid Build Coastguard Worker 57*795d594fSAndroid Build Coastguard Worker // On Arm64: 58*795d594fSAndroid Build Coastguard Worker // These loops are likely to be vectorized; when deoptimizing to interpreter the induction 59*795d594fSAndroid Build Coastguard Worker // variable i will be set to wrong value (== 0). 60*795d594fSAndroid Build Coastguard Worker // 61*795d594fSAndroid Build Coastguard Worker // Copy-paste instead of nested loop is here to avoid extra loop suspend check. 62*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < kArraySize; i++) { 63*795d594fSAndroid Build Coastguard Worker array[i]++; 64*795d594fSAndroid Build Coastguard Worker } 65*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < kArraySize; i++) { 66*795d594fSAndroid Build Coastguard Worker array[i]++; 67*795d594fSAndroid Build Coastguard Worker } 68*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < kArraySize; i++) { 69*795d594fSAndroid Build Coastguard Worker array[i]++; 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < kArraySize; i++) { 72*795d594fSAndroid Build Coastguard Worker array[i]++; 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker // We might have managed to execute the whole loop before deoptimizeAll() happend. 76*795d594fSAndroid Build Coastguard Worker if (sExitFlag) { 77*795d594fSAndroid Build Coastguard Worker Main.assertIsInterpreted(); 78*795d594fSAndroid Build Coastguard Worker } 79*795d594fSAndroid Build Coastguard Worker // Regression: the value of the induction variable might have been set to 0 when 80*795d594fSAndroid Build Coastguard Worker // deoptimizing causing to have another array[0]++. 81*795d594fSAndroid Build Coastguard Worker expectEqual(array[0], 4); 82*795d594fSAndroid Build Coastguard Worker } 83*795d594fSAndroid Build Coastguard Worker run()84*795d594fSAndroid Build Coastguard Worker public void run() { 85*795d594fSAndroid Build Coastguard Worker if (threadIndex == 0) { 86*795d594fSAndroid Build Coastguard Worker while (!sEntered) { 87*795d594fSAndroid Build Coastguard Worker Thread.yield(); 88*795d594fSAndroid Build Coastguard Worker } 89*795d594fSAndroid Build Coastguard Worker 90*795d594fSAndroid Build Coastguard Worker Main.deoptimizeAll(); 91*795d594fSAndroid Build Coastguard Worker sExitFlag = true; 92*795d594fSAndroid Build Coastguard Worker } else { 93*795d594fSAndroid Build Coastguard Worker $noinline$busyLoop(); 94*795d594fSAndroid Build Coastguard Worker } 95*795d594fSAndroid Build Coastguard Worker } 96*795d594fSAndroid Build Coastguard Worker } 97