Created
March 29, 2023 22:39
-
-
Save navyxliu/6239ce24f1ae447060302cc8562cbb71 to your computer and use it in GitHub Desktop.
Passive Materialization
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
import java.lang.Math; | |
class Example1_Point { | |
static class Point { | |
double x; | |
double y; | |
public Point(double x, double y) { | |
this.x = x; | |
this.y = y; | |
} | |
// distance from zero | |
public double length() { | |
return Math.sqrt(x * x + y * y); | |
} | |
} | |
private static Point _cache; | |
public double test(boolean cond, double x, double y) { | |
Point pt = new Point(x, y); | |
pt.x = 3.0d; | |
if (cond) { | |
_cache = pt; // materialize here. pt1 = Mat(pt) | |
} | |
// pt2 = phi(pt, pt1) | |
return pt.length(); | |
} | |
public static void main(String[] args) { | |
var kase = new Example1_Point(); | |
long iterations = 0; | |
try { | |
while (true) { | |
kase.test(0 == (iterations & 0xf), 2.0d, 1.0d); | |
iterations++; | |
} | |
} finally { | |
System.err.println("Epsilon Test: " + iterations); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Statement: PEA without passive materialization doesn't duplicate the allocation when C2 applies to JDK-8287061
Prove:
o denotes the original object: o0 = Allocate(Class).
if o0 never encounters an escaping point, PEA doesn't change o0. no duplication.
if o0 encounters an escaping point in a branch, PEA materializes it
o1 = Mat(Obj)
. o1 is EscapeIt's natural to extend this proof to multiple branches. As long as we retain SSA property, we can prove that the original allocation is either dead or get scalar replaced if PEA does materialize the object.
JDK-8287061 determines whether o2 is a reducible phi based on both inputs and outputs. it's possible that JDK-8287061 opts out phi reduction because the consumer of o2 is not recognizable. I think this problem is amendable. we can extend it to include all possible codeshape in the future.