This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
132 changed files
with
3,372 additions
and
1,005 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...test/src/org/graalvm/compiler/api/directives/test/SwitchCaseProbabilityDirectiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package org.graalvm.compiler.api.directives.test; | ||
|
||
import org.graalvm.compiler.api.directives.GraalDirectives; | ||
import org.graalvm.compiler.core.test.GraalCompilerTest; | ||
import org.graalvm.compiler.debug.GraalError; | ||
import org.graalvm.compiler.graph.iterators.NodeIterable; | ||
import org.graalvm.compiler.nodes.StructuredGraph; | ||
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode; | ||
import org.graalvm.compiler.nodes.extended.SwitchCaseProbabilityNode; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import jdk.vm.ci.meta.ResolvedJavaMethod; | ||
|
||
public class SwitchCaseProbabilityDirectiveTest extends GraalCompilerTest { | ||
/** | ||
* Called before a test is compiled. | ||
*/ | ||
@Override | ||
protected void before(ResolvedJavaMethod method) { | ||
// don't let -Xcomp pollute profile | ||
method.reprofile(); | ||
} | ||
|
||
public static int switchProbabilitySnippet1(int x) { | ||
switch (x) { | ||
case 0: | ||
GraalDirectives.injectSwitchCaseProbability(0.5); | ||
return 1; | ||
case 1: | ||
GraalDirectives.injectSwitchCaseProbability(0.3); | ||
return 3; | ||
case 2: | ||
GraalDirectives.injectSwitchCaseProbability(0.1); | ||
return x * 3; | ||
default: | ||
GraalDirectives.injectSwitchCaseProbability(0.1); | ||
return x + 5; | ||
} | ||
} | ||
|
||
public static int keyHoleSwitchSnippet(int x) { | ||
switch (x) { | ||
case 3: | ||
GraalDirectives.injectSwitchCaseProbability(0.2); | ||
return 10; | ||
case 4: | ||
GraalDirectives.injectSwitchCaseProbability(0.2); | ||
return 20; | ||
case 6: | ||
GraalDirectives.injectSwitchCaseProbability(0.2); | ||
return 30; | ||
case 7: | ||
GraalDirectives.injectSwitchCaseProbability(0.2); | ||
return 40; | ||
default: | ||
GraalDirectives.injectSwitchCaseProbability(0.2); | ||
return 42; | ||
} | ||
} | ||
|
||
@Test | ||
public void testSwitch() { | ||
test("switchProbabilitySnippet1", 1); | ||
test("keyHoleSwitchSnippet", 4); | ||
} | ||
|
||
public static int missingProbability(int x) { | ||
switch (x) { | ||
case 1: | ||
GraalDirectives.injectSwitchCaseProbability(0.25); | ||
return 10; | ||
case 2: | ||
GraalDirectives.injectSwitchCaseProbability(0.25); | ||
return 20; | ||
case 3: | ||
GraalDirectives.injectSwitchCaseProbability(0.25); | ||
return 3; | ||
case 4: | ||
GraalDirectives.injectSwitchCaseProbability(0.25); | ||
return 15; | ||
default: | ||
/* | ||
* No probability usage here, which is an error, even though the other branches' | ||
* probability adds up to 1 | ||
*/ | ||
return 42; | ||
} | ||
} | ||
|
||
public static int incorrectTotalProbability(int x) { | ||
/* | ||
* Total probability across branches should add up to 1, and if it doesn't an error should | ||
* be thrown. | ||
*/ | ||
switch (x) { | ||
case 1: | ||
GraalDirectives.injectSwitchCaseProbability(0.20); | ||
return 10; | ||
case 2: | ||
GraalDirectives.injectSwitchCaseProbability(0.20); | ||
return 20; | ||
case 3: | ||
GraalDirectives.injectSwitchCaseProbability(0.20); | ||
return 3; | ||
default: | ||
GraalDirectives.injectSwitchCaseProbability(0.20); | ||
return 42; | ||
} | ||
} | ||
|
||
@Test(expected = GraalError.class) | ||
public void testMissingProbability() { | ||
test("missingProbability", 1); | ||
} | ||
|
||
@Test(expected = GraalError.class) | ||
public void testIncorrectTotalProbability() { | ||
test("incorrectTotalProbability", 3); | ||
} | ||
|
||
@Override | ||
protected void checkLowTierGraph(StructuredGraph graph) { | ||
NodeIterable<SwitchCaseProbabilityNode> switchProbabilityInjectionNodes = graph.getNodes().filter(SwitchCaseProbabilityNode.class); | ||
Assert.assertEquals("SwitchCaseProbabilityNode count", 0, switchProbabilityInjectionNodes.count()); | ||
|
||
NodeIterable<IntegerSwitchNode> switchNodes = graph.getNodes().filter(IntegerSwitchNode.class); | ||
Assert.assertEquals("IntegerSwitchNode count", 1, switchNodes.count()); | ||
IntegerSwitchNode switchNode = switchNodes.first(); | ||
for (int i = 0; i < switchNode.keyCount(); ++i) { | ||
double expectedProbability = Double.NaN; | ||
switch (switchNode.intKeyAt(i)) { | ||
case 0: | ||
expectedProbability = 0.5; | ||
break; | ||
case 1: | ||
expectedProbability = 0.3; | ||
break; | ||
case 2: | ||
expectedProbability = 0.1; | ||
break; | ||
case 3: | ||
expectedProbability = 0.2; | ||
break; | ||
case 4: | ||
expectedProbability = 0.2; | ||
break; | ||
case 5: | ||
// Default probability should fill the hole and be divided in two | ||
expectedProbability = 0.1; | ||
break; | ||
case 6: | ||
expectedProbability = 0.2; | ||
break; | ||
case 7: | ||
expectedProbability = 0.2; | ||
break; | ||
default: | ||
GraalError.shouldNotReachHereUnexpectedValue(switchNode.intKeyAt(i)); | ||
} | ||
Assert.assertEquals(expectedProbability, switchNode.keyProbability(i), 0.001); | ||
} | ||
Assert.assertEquals(0.1, switchNode.defaultProbability(), 0.001); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.