diff options
| author | 2017-06-01 15:45:09 -0700 | |
|---|---|---|
| committer | 2017-06-16 15:31:48 -0700 | |
| commit | d964e3b7841edbc9e653dedbb26bd3e050fc6aba (patch) | |
| tree | 709df4838353f428512afb484c5c432e7b0324ca | |
| parent | 9a7f386ef0307f3cac72ee0dfd8d8671731cb413 (diff) | |
Fixed bug in relying on precise FP in periodic sequence.
Rationale:
FP arithmetic is not always precise, so relying on FP
period sequences to "compute back" precisely is not
valid; when all values in the period are "fetches"
however, the rotation is precise.
Bug found by fuzz testing. With regression test.
Bug: 62196559
Test: test-art-host
(cherry picked from commit 8523ea11a677b78e1fc05915976c04b1ff081451)
Change-Id: Idd90dbece8b65de8c5296b8676377c377f71629c
| -rw-r--r-- | compiler/optimizing/induction_var_range.cc | 24 | ||||
| -rw-r--r-- | test/654-checker-periodic/expected.txt | 1 | ||||
| -rw-r--r-- | test/654-checker-periodic/info.txt | 1 | ||||
| -rw-r--r-- | test/654-checker-periodic/src/Main.java | 173 |
4 files changed, 195 insertions, 4 deletions
diff --git a/compiler/optimizing/induction_var_range.cc b/compiler/optimizing/induction_var_range.cc index 7c833cf70c..c0ec58f824 100644 --- a/compiler/optimizing/induction_var_range.cc +++ b/compiler/optimizing/induction_var_range.cc @@ -1132,11 +1132,27 @@ bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::Inducti /*out*/bool* needs_taken_test) const { DCHECK(info != nullptr); DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic); - // Count period. + // Count period and detect all-invariants. int64_t period = 1; - for (HInductionVarAnalysis::InductionInfo* p = info; - p->induction_class == HInductionVarAnalysis::kPeriodic; - p = p->op_b, ++period) {} + bool all_invariants = true; + HInductionVarAnalysis::InductionInfo* p = info; + for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) { + DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant); + if (p->op_a->operation != HInductionVarAnalysis::kFetch) { + all_invariants = false; + } + } + DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant); + if (p->operation != HInductionVarAnalysis::kFetch) { + all_invariants = false; + } + // Don't rely on FP arithmetic to be precise, unless the full period + // consist of pre-computed expressions only. + if (info->type == Primitive::kPrimFloat || info->type == Primitive::kPrimDouble) { + if (!all_invariants) { + return false; + } + } // Handle any periodic(x, periodic(.., y)) for known maximum index value m. int64_t m = 0; if (IsConstant(trip->op_a, kExact, &m) && m >= 1) { diff --git a/test/654-checker-periodic/expected.txt b/test/654-checker-periodic/expected.txt new file mode 100644 index 0000000000..b0aad4deb5 --- /dev/null +++ b/test/654-checker-periodic/expected.txt @@ -0,0 +1 @@ +passed diff --git a/test/654-checker-periodic/info.txt b/test/654-checker-periodic/info.txt new file mode 100644 index 0000000000..7c8a7770ae --- /dev/null +++ b/test/654-checker-periodic/info.txt @@ -0,0 +1 @@ +Periodic sequence on integer and floating-point. diff --git a/test/654-checker-periodic/src/Main.java b/test/654-checker-periodic/src/Main.java new file mode 100644 index 0000000000..7a0c98cfae --- /dev/null +++ b/test/654-checker-periodic/src/Main.java @@ -0,0 +1,173 @@ +/* + * 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 last value of a few periodic sequences + * (found by fuzz testing). + */ +public class Main { + + /// CHECK-START: int Main.doitUpInt(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: int Main.doitUpInt(int) loop_optimization (after) + /// CHECK-NOT: Phi + static int doitUpInt(int n) { + // Complete loop is replaced by last-value. + int lI = 1; + for (int i1 = 0; i1 < n; i1++) { + lI = (1486662021 - lI); + } + return lI; + } + + /// CHECK-START: int Main.doitDownInt(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: int Main.doitDownInt(int) loop_optimization (after) + /// CHECK-NOT: Phi + static int doitDownInt(int n) { + // Complete loop is replaced by last-value. + int lI = 1; + for (int i1 = n - 1; i1 >= 0; i1--) { + lI = (1486662021 - lI); + } + return lI; + } + + /// CHECK-START: float Main.doitUpFloat(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: float Main.doitUpFloat(int) loop_optimization (after) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + static float doitUpFloat(int n) { + // FP arithmetic is not sufficiently precise. + // The loop remains. + float lF = 1.0f; + for (int i1 = 0; i1 < n; i1++) { + lF = (1486662021.0f - lF); + } + return lF; + } + + /// CHECK-START: float Main.doitDownFloat(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: float Main.doitDownFloat(int) loop_optimization (after) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + static float doitDownFloat(int n) { + // FP arithmetic is not sufficiently precise. + // The loop remains. + float lF = 1.0f; + for (int i1 = n - 1; i1 >= 0; i1--) { + lF = (1486662021.0f - lF); + } + return lF; + } + + /// CHECK-START: float Main.doitUpFloatAlt(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: float Main.doitUpFloatAlt(int) loop_optimization (after) + /// CHECK-NOT: Phi + static float doitUpFloatAlt(int n) { + // Complete loop is replaced by last-value + // since the values are now precise. + float lF = 1.0f; + float l2 = 1486662020.0f; + for (int i1 = 0; i1 < n; i1++) { + float old = lF; + lF = l2; + l2 = old; + } + return lF; + } + + /// CHECK-START: float Main.doitDownFloatAlt(int) loop_optimization (before) + /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none + /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none + // + /// CHECK-START: float Main.doitDownFloatAlt(int) loop_optimization (after) + /// CHECK-NOT: Phi + static float doitDownFloatAlt(int n) { + // Complete loop is replaced by last-value + // since the values are now precise. + float lF = 1.0f; + float l2 = 1486662020.0f; + for (int i1 = n - 1; i1 >= 0; i1--) { + float old = lF; + lF = l2; + l2 = old; + } + return lF; + } + + // Main driver. + public static void main(String[] args) { + for (int i = 0; i < 10; i++) { + int ei = (i & 1) == 0 ? 1 : 1486662020; + int ci = doitUpInt(i); + expectEquals(ei, ci); + } + for (int i = 0; i < 10; i++) { + int ei = (i & 1) == 0 ? 1 : 1486662020; + int ci = doitDownInt(i); + expectEquals(ei, ci); + } + for (int i = 0; i < 10; i++) { + float ef = i == 0 ? 1.0f : ((i & 1) == 0 ? 0.0f : 1486662021.0f); + float cf = doitUpFloat(i); + expectEquals(ef, cf); + } + for (int i = 0; i < 10; i++) { + float ef = i == 0 ? 1.0f : ((i & 1) == 0 ? 0.0f : 1486662021.0f); + float cf = doitDownFloat(i); + expectEquals(ef, cf); + } + for (int i = 0; i < 10; i++) { + float ef = (i & 1) == 0 ? 1.0f : 1486662020.0f; + float cf = doitUpFloatAlt(i); + expectEquals(ef, cf); + } + for (int i = 0; i < 10; i++) { + float ef = (i & 1) == 0 ? 1.0f : 1486662020.0f; + float cf = doitDownFloatAlt(i); + expectEquals(ef, cf); + } + 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(float expected, float result) { + if (expected != result) { + throw new Error("Expected: " + expected + ", found: " + result); + } + } +} + + |