/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Tests for simple integral reductions: same type for accumulator and data. */ public class Main { static final int N = 500; static final int M = 100; // // Basic reductions in loops. // // TODO: vectorize these (second step of b/64091002 plan) private static byte reductionByte(byte[] x) { byte sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static short reductionShort(short[] x) { short sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static char reductionChar(char[] x) { char sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } /// CHECK-START: int Main.reductionInt(int[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-{ARM,ARM64}: int Main.reductionInt(int[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant {{2|4}} loop:none /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none // Check that full 128-bit Q-Register are saved across SuspendCheck slow path. /// CHECK-START-ARM64: int Main.reductionInt(int[]) disassembly (after) /// CHECK: SuspendCheckSlowPathARM64 /// CHECK: stur q<>, [sp, #<>] /// CHECK: ldur q<>, [sp, #<>] private static int reductionInt(int[] x) { int sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } /// CHECK-START: int Main.reductionIntChain() loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-EVAL: "<>" != "<>" // /// CHECK-START-{ARM,ARM64}: int Main.reductionIntChain() loop_optimization (after) /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none // /// CHECK-EVAL: "<>" != "<>" // // NOTE: pattern is robust with respect to vector loop unrolling and peeling. private static int reductionIntChain() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; int r = 1; for (int i = 0; i < 16; i++) { r += x[i]; } for (int i = 0; i < 16; i++) { r += x[i]; } return r; } /// CHECK-START: int Main.reductionIntToLoop(int[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none // /// CHECK-EVAL: "<>" != "<>" // /// CHECK-START-{ARM,ARM64}: int Main.reductionIntToLoop(int[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant {{2|4}} loop:none /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static int reductionIntToLoop(int[] x) { int r = 0; for (int i = 0; i < 8; i++) { r += x[i]; } for (int i = r; i < 16; i++) { r += i; } return r; } /// CHECK-START: long Main.reductionLong(long[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> LongConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{j\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-ARM64: long Main.reductionLong(long[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant 2 loop:none /// CHECK-DAG: <> VecSetScalars [{{j\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static long reductionLong(long[] x) { long sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static byte reductionByteM1(byte[] x) { byte sum = -1; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static short reductionShortM1(short[] x) { short sum = -1; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static char reductionCharM1(char[] x) { char sum = 0xffff; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } /// CHECK-START: int Main.reductionIntM1(int[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> IntConstant -1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-{ARM,ARM64}: int Main.reductionIntM1(int[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant {{2|4}} loop:none /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static int reductionIntM1(int[] x) { int sum = -1; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } /// CHECK-START: long Main.reductionLongM1(long[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> LongConstant -1 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{j\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-ARM64: long Main.reductionLongM1(long[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant 2 loop:none /// CHECK-DAG: <> VecSetScalars [{{j\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecAdd [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static long reductionLongM1(long[] x) { long sum = -1L; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; } private static byte reductionMinusByte(byte[] x) { byte sum = 0; for (int i = 0; i < x.length; i++) { sum -= x[i]; } return sum; } private static short reductionMinusShort(short[] x) { short sum = 0; for (int i = 0; i < x.length; i++) { sum -= x[i]; } return sum; } private static char reductionMinusChar(char[] x) { char sum = 0; for (int i = 0; i < x.length; i++) { sum -= x[i]; } return sum; } /// CHECK-START: int Main.reductionMinusInt(int[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Sub [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-{ARM,ARM64}: int Main.reductionMinusInt(int[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant {{2|4}} loop:none /// CHECK-DAG: <> VecSetScalars [{{i\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecSub [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static int reductionMinusInt(int[] x) { int sum = 0; for (int i = 0; i < x.length; i++) { sum -= x[i]; } return sum; } /// CHECK-START: long Main.reductionMinusLong(long[]) loop_optimization (before) /// CHECK-DAG: <> IntConstant 0 loop:none /// CHECK-DAG: <> LongConstant 0 loop:none /// CHECK-DAG: <> IntConstant 1 loop:none /// CHECK-DAG: <> Phi [<>,{{i\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> Phi [<>,{{j\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> ArrayGet [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: Sub [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Return [<>] loop:none // /// CHECK-START-ARM64: long Main.reductionMinusLong(long[]) loop_optimization (after) /// CHECK-DAG: <> IntConstant 2 loop:none /// CHECK-DAG: <> VecSetScalars [{{j\d+}}] loop:none /// CHECK-DAG: <> Phi [<>,{{d\d+}}] loop:<> outer_loop:none /// CHECK-DAG: <> VecLoad [{{l\d+}},<>] loop:<> outer_loop:none /// CHECK-DAG: VecSub [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: Add [<>,<>] loop:<> outer_loop:none /// CHECK-DAG: <> VecReduce [<>] loop:none /// CHECK-DAG: <> VecExtractScalar [<>] loop:none private static long reductionMinusLong(long[] x) { long sum = 0; for (int i = 0; i < x.length; i++) { sum -= x[i]; } return sum; } // // A few special cases. // // TODO: consider unrolling private static int reductionInt10(int[] x) { int sum = 0; // Amenable to complete unrolling. for (int i = 10; i <= 10; i++) { sum += x[i]; } return sum; } private static int reductionMinusInt10(int[] x) { int sum = 0; // Amenable to complete unrolling. for (int i = 10; i <= 10; i++) { sum -= x[i]; } return sum; } // // Main driver. // public static void main(String[] args) { byte[] xb = new byte[N]; short[] xs = new short[N]; char[] xc = new char[N]; int[] xi = new int[N]; long[] xl = new long[N]; for (int i = 0, k = -17; i < N; i++, k += 3) { xb[i] = (byte) k; xs[i] = (short) k; xc[i] = (char) k; xi[i] = k; xl[i] = k; } // Arrays with all positive elements. byte[] xpb = new byte[M]; short[] xps = new short[M]; char[] xpc = new char[M]; int[] xpi = new int[M]; long[] xpl = new long[M]; for (int i = 0, k = 3; i < M; i++, k++) { xpb[i] = (byte) k; xps[i] = (short) k; xpc[i] = (char) k; xpi[i] = k; xpl[i] = k; } // Arrays with all negative elements. byte[] xnb = new byte[M]; short[] xns = new short[M]; int[] xni = new int[M]; long[] xnl = new long[M]; for (int i = 0, k = -103; i < M; i++, k++) { xnb[i] = (byte) k; xns[i] = (short) k; xni[i] = k; xnl[i] = k; } // Test various reductions in loops. int[] x0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; int[] x1 = { 0, 0, 0, 1, 0, 0, 0, 0 }; int[] x2 = { 1, 1, 1, 1, 0, 0, 0, 0 }; expectEquals(-74, reductionByte(xb)); expectEquals(-27466, reductionShort(xs)); expectEquals(38070, reductionChar(xc)); expectEquals(365750, reductionInt(xi)); expectEquals(273, reductionIntChain()); expectEquals(120, reductionIntToLoop(x0)); expectEquals(121, reductionIntToLoop(x1)); expectEquals(118, reductionIntToLoop(x2)); expectEquals(-1310, reductionIntToLoop(xi)); expectEquals(365750L, reductionLong(xl)); expectEquals(-75, reductionByteM1(xb)); expectEquals(-27467, reductionShortM1(xs)); expectEquals(38069, reductionCharM1(xc)); expectEquals(365749, reductionIntM1(xi)); expectEquals(365749L, reductionLongM1(xl)); expectEquals(74, reductionMinusByte(xb)); expectEquals(27466, reductionMinusShort(xs)); expectEquals(27466, reductionMinusChar(xc)); expectEquals(-365750, reductionMinusInt(xi)); expectEquals(365750L, reductionLong(xl)); expectEquals(-75, reductionByteM1(xb)); expectEquals(-27467, reductionShortM1(xs)); expectEquals(38069, reductionCharM1(xc)); expectEquals(365749, reductionIntM1(xi)); expectEquals(365749L, reductionLongM1(xl)); expectEquals(74, reductionMinusByte(xb)); expectEquals(27466, reductionMinusShort(xs)); expectEquals(27466, reductionMinusChar(xc)); expectEquals(-365750, reductionMinusInt(xi)); expectEquals(-365750L, reductionMinusLong(xl)); // Test special cases. expectEquals(13, reductionInt10(xi)); expectEquals(-13, reductionMinusInt10(xi)); System.out.println("passed"); } private static void expectEquals(int expected, int result) { if (expected != result) { throw new Error("Expected: " + expected + ", found: " + result); } } private static void expectEquals(long expected, long result) { if (expected != result) { throw new Error("Expected: " + expected + ", found: " + result); } } }