Skip to content

Commit 1214e57

Browse files
committed
PorcE: Add Java agent which rewrites scala classes optimize better.
1 parent ab30cd3 commit 1214e57

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed

ScalaGraalAgent/.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="lib" path="lib/asm-6.2.jar" sourcepath="/home/amp/Downloads/asm-6.2-sources.jar"/>
6+
<classpathentry kind="output" path="build/classes"/>
7+
</classpath>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
3+
<booleanAttribute key="org.eclipse.ant.ui.ATTR_TARGETS_UPDATED" value="true"/>
4+
<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
5+
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
6+
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/>
7+
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="true"/>
8+
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="ScalaGraalAgent"/>
9+
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/ScalaGraalAgent/build.xml}"/>
10+
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value=""/>
11+
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
12+
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/ScalaGraalAgent}"/>
13+
</launchConfiguration>

ScalaGraalAgent/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/
2+
/build/

ScalaGraalAgent/.project

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ScalaGraalAgent</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
15+
<triggers>full,incremental,</triggers>
16+
<arguments>
17+
<dictionary>
18+
<key>LaunchConfigHandle</key>
19+
<value>&lt;project&gt;/.externalToolBuilders/Build Jar.launch</value>
20+
</dictionary>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.jdt.core.javanature</nature>
26+
</natures>
27+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.8
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.8

ScalaGraalAgent/build.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- build.xml - Ant buildfile (tasks for project build)
4+
Project ScalaGraalAgent
5+
6+
Copyright (c) 2018 The University of Texas at Austin. All rights reserved.
7+
8+
Use and redistribution of this file is governed by the license terms in
9+
the LICENSE file found in the project's top-level directory and also found at
10+
URL: http://orc.csres.utexas.edu/license.shtml .
11+
-->
12+
13+
<project name="ScalaGraalAgent" default="all" basedir=".">
14+
<description>
15+
Ant build file for Scala Graal rewrite agent.
16+
</description>
17+
18+
<property name="version" value="0.1"/>
19+
<property name="target.name" value="${ant.project.name}-${version}"/>
20+
21+
<fileset id="runtime.jars" dir="lib" description="JARs needed by Orc in its classpath at runtime">
22+
<include name="*.jar"/>
23+
</fileset>
24+
25+
<!-- =================================
26+
target: jar
27+
================================= -->
28+
<target name="jar" description="Build jar">
29+
<jar jarfile="build/${target.name}.jar" basedir="build/classes" whenmanifestonly="fail" duplicate="fail" index="true" strict="fail">
30+
<manifest>
31+
<attribute name="Premain-Class" value="orc.scalagraalagent.Agent"/>
32+
<attribute name="Implementation-Title" value="Scala Graal rewrite agent"/>
33+
<attribute name="Implementation-Version" value="0.1"/>
34+
<attribute name="Implementation-Vendor" value="The University of Texas at Austin"/>
35+
</manifest>
36+
<zipfileset src="lib/asm-6.2.jar" excludes="**/module-info.class" includes="**/*.class"/>
37+
</jar>
38+
</target>
39+
40+
<!-- =================================
41+
target: all
42+
================================= -->
43+
<target name="all" depends="jar" description="Build all targets"/>
44+
</project>

ScalaGraalAgent/lib/asm-6.2.jar

109 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package orc.scalagraalagent;
2+
3+
import java.lang.instrument.ClassFileTransformer;
4+
import java.lang.instrument.IllegalClassFormatException;
5+
import java.lang.instrument.Instrumentation;
6+
import java.security.ProtectionDomain;
7+
import java.util.logging.Logger;
8+
9+
import org.objectweb.asm.ClassReader;
10+
import org.objectweb.asm.ClassVisitor;
11+
import org.objectweb.asm.ClassWriter;
12+
13+
public class Agent {
14+
private static final Logger logger = Logger.getLogger("orc.scalagraalagent");
15+
16+
public static void premain(String agentArgs, Instrumentation inst) {
17+
logger.info("Rewriting static \"MODULE$\" to be final for all loaded classes. This helps Graal optimize Scala objects.");
18+
inst.addTransformer(new ClassFileTransformer() {
19+
@Override
20+
public byte[] transform(ClassLoader l, String name, Class<?> c, ProtectionDomain d, byte[] b)
21+
throws IllegalClassFormatException {
22+
ClassReader cr = new ClassReader(b);
23+
ClassWriter cw = new ClassWriter(0);
24+
//TraceClassVisitor t = new TraceClassVisitor(cw, new PrintWriter(System.out));
25+
ClassVisitor cv = new MarkModulesAsCompileConstant(cw);
26+
cr.accept(cv, 0);
27+
return cw.toByteArray();
28+
}
29+
});
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package orc.scalagraalagent;
2+
3+
import java.util.logging.Logger;
4+
5+
import org.objectweb.asm.AnnotationVisitor;
6+
import org.objectweb.asm.ClassVisitor;
7+
import org.objectweb.asm.FieldVisitor;
8+
import org.objectweb.asm.Opcodes;
9+
import org.objectweb.asm.Type;
10+
11+
public class MarkModulesAsCompileConstant extends ClassVisitor {
12+
private static final Logger logger = Logger.getLogger("orc.scalagraalagent");
13+
14+
private String className;
15+
16+
public MarkModulesAsCompileConstant(ClassVisitor cv) {
17+
super(Opcodes.ASM6, cv);
18+
this.cv = cv;
19+
}
20+
21+
@Override
22+
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
23+
this.className = name;
24+
super.visit(version, access, name, signature, superName, interfaces);
25+
}
26+
27+
@Override
28+
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
29+
if (name.equals("MODULE$") && (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) {
30+
FieldVisitor fv = super.visitField(access | Opcodes.ACC_FINAL, name, descriptor, signature, value);
31+
32+
logger.fine(() -> "Set " + this.className.replace('/', '.') + "." + name + " as final.");
33+
34+
return fv;
35+
} else {
36+
return super.visitField(access, name, descriptor, signature, value);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)