Merge "Make sure desugaring already publicize private methods in interfaces."
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index b775fd3..f896eee 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
// This field is accessed from release scripts using simple pattern matching.
// Therefore, changing this field could break our release scripts.
- public static final String LABEL = "1.3.0-dev";
+ public static final String LABEL = "1.3.1-dev";
private Version() {
}
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index ae57f48..e610d20 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -762,6 +762,11 @@
return;
}
+ DexClass clazz = appInfo.definitionFor(method.method.holder);
+ if (clazz == null) {
+ return;
+ }
+
boolean receiverUsedAsReturnValue = false;
boolean seenSuperInitCall = false;
for (Instruction insn : receiver.uniqueUsers()) {
@@ -773,7 +778,7 @@
if (insn.isInstanceGet() ||
(insn.isInstancePut() && insn.asInstancePut().object() == receiver)) {
DexField field = insn.asFieldInstruction().getField();
- if (field.clazz == method.method.holder) {
+ if (field.clazz == clazz.type && clazz.lookupInstanceField(field) != null) {
// Since class inliner currently only supports classes directly extending
// java.lang.Object, we don't need to worry about fields defined in superclasses.
continue;
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/ClassInliner.java b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/ClassInliner.java
index 7bdff33..d7a57df 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/ClassInliner.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/ClassInliner.java
@@ -169,14 +169,16 @@
Map<InvokeMethodWithReceiver, InliningInfo> methodCalls = new IdentityHashMap<>();
+ DexClass definition = appInfo.definitionFor(clazz);
+
for (Instruction user : receiver.uniqueUsers()) {
// Field read/write.
if (user.isInstanceGet() ||
(user.isInstancePut() && user.asInstancePut().value() != receiver)) {
- if (user.asFieldInstruction().getField().clazz == newInstanceInsn.clazz) {
- // Eligible field read or write. Note: as long as we consider only classes eligible
- // if they directly extend java.lang.Object we don't need to check if the field
- // really exists in the class.
+ DexField field = user.asFieldInstruction().getField();
+ if (field.clazz == newInstanceInsn.clazz && definition.lookupInstanceField(field) != null) {
+ // Since class inliner currently only supports classes directly extending
+ // java.lang.Object, we don't need to worry about fields defined in superclasses.
continue;
}
return null; // Not eligible.
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 2b51720..dae98ad 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -1651,7 +1651,7 @@
rewriteKeysWhileMergingValues(previous.staticFieldWrites, lense::lookupField);
this.fieldsRead = rewriteItems(previous.fieldsRead, lense::lookupField);
this.fieldsWritten = rewriteItems(previous.fieldsWritten, lense::lookupField);
- this.pinnedItems = rewriteMixedItems(previous.pinnedItems, lense);
+ this.pinnedItems = rewriteMixedItemsConservatively(previous.pinnedItems, lense);
this.virtualInvokes = rewriteMethodsConservatively(previous.virtualInvokes, lense);
this.interfaceInvokes = rewriteMethodsConservatively(previous.interfaceInvokes, lense);
this.superInvokes = rewriteMethodsConservatively(previous.superInvokes, lense);
@@ -1665,7 +1665,8 @@
this.assumedValues = previous.assumedValues;
assert assertNotModifiedByLense(previous.alwaysInline, lense);
this.alwaysInline = previous.alwaysInline;
- this.identifierNameStrings = rewriteMixedItems(previous.identifierNameStrings, lense);
+ this.identifierNameStrings =
+ rewriteMixedItemsConservatively(previous.identifierNameStrings, lense);
// Switchmap classes should never be affected by renaming.
assert assertNotModifiedByLense(
previous.switchMaps.keySet().stream().map(this::definitionFor).filter(Objects::nonNull)
@@ -1846,7 +1847,7 @@
return Collections.unmodifiableMap(result);
}
- private static ImmutableSet<DexItem> rewriteMixedItems(
+ private static ImmutableSet<DexItem> rewriteMixedItemsConservatively(
Set<DexItem> original, GraphLense lense) {
ImmutableSet.Builder<DexItem> builder = ImmutableSet.builder();
for (DexItem item : original) {
@@ -1854,7 +1855,12 @@
if (item instanceof DexType) {
builder.add(lense.lookupType((DexType) item));
} else if (item instanceof DexMethod) {
- builder.add(lense.lookupMethod((DexMethod) item));
+ DexMethod method = (DexMethod) item;
+ if (lense.isContextFreeForMethod(method)) {
+ builder.add(lense.lookupMethod(method));
+ } else {
+ builder.addAll(lense.lookupMethodInAllContexts(method));
+ }
} else if (item instanceof DexField) {
builder.add(lense.lookupField((DexField) item));
} else {
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
index 4b57209..c737055 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
@@ -11,6 +11,7 @@
import com.android.tools.r8.graph.DexEncodedField;
import com.android.tools.r8.graph.DexEncodedMethod;
import com.android.tools.r8.graph.DexField;
+import com.android.tools.r8.graph.DexItem;
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexProgramClass;
import com.android.tools.r8.graph.DexProto;
@@ -87,28 +88,20 @@
// Returns a set of types that must not be merged into other types.
private Set<DexType> getPinnedTypes(Iterable<DexProgramClass> classes) {
Set<DexType> pinnedTypes = new HashSet<>();
+
+ // For all pinned fields, also pin the type of the field (because changing the type of the field
+ // implicitly changes the signature of the field). Similarly, for all pinned methods, also pin
+ // the return type and the parameter types of the method.
+ extractPinnedItems(appInfo.pinnedItems, pinnedTypes);
+
+ // TODO(christofferqa): Remove the invariant that the graph lense should not modify any
+ // methods from the sets alwaysInline and noSideEffects (see use of assertNotModifiedBy-
+ // Lense).
+ extractPinnedItems(appInfo.alwaysInline, pinnedTypes);
+ extractPinnedItems(appInfo.noSideEffects.keySet(), pinnedTypes);
+
for (DexProgramClass clazz : classes) {
for (DexEncodedMethod method : clazz.methods()) {
- // TODO(christofferqa): Remove the invariant that the graph lense should not modify any
- // methods from the sets alwaysInline and noSideEffects (see use of assertNotModifiedBy-
- // Lense).
- if (appInfo.alwaysInline.contains(method) || appInfo.noSideEffects.containsKey(method)) {
- DexClass other = appInfo.definitionFor(method.method.proto.returnType);
- if (other != null && other.isProgramClass()) {
- // If we were to merge [other] into its sub class, then we would implicitly change the
- // signature of this method, and therefore break the invariant.
- pinnedTypes.add(other.type);
- }
- for (DexType parameterType : method.method.proto.parameters.values) {
- other = appInfo.definitionFor(parameterType);
- if (other != null && other.isProgramClass()) {
- // If we were to merge [other] into its sub class, then we would implicitly change the
- // signature of this method, and therefore break the invariant.
- pinnedTypes.add(other.type);
- }
- }
- }
-
// Avoid merging two types if this could remove a NoSuchMethodError, as illustrated by the
// following example. (Alternatively, it would be possible to merge A and B and rewrite the
// "invoke-super A.m" instruction into "invoke-super Object.m" to preserve the error. This
@@ -143,6 +136,38 @@
return pinnedTypes;
}
+ private void extractPinnedItems(Iterable<DexItem> items, Set<DexType> pinnedTypes) {
+ for (DexItem item : items) {
+ // Note: Nothing to do for the case where item is a DexType, since we check for this case
+ // using appInfo.isPinned.
+ if (item instanceof DexField) {
+ // Pin the type of the field.
+ DexField field = (DexField) item;
+ DexClass clazz = appInfo.definitionFor(field.type);
+ if (clazz != null && clazz.isProgramClass()) {
+ pinnedTypes.add(clazz.type);
+ }
+ } else if (item instanceof DexMethod) {
+ // Pin the return type and the parameter types of the method.
+ DexMethod method = (DexMethod) item;
+ DexClass clazz = appInfo.definitionFor(method.proto.returnType);
+ if (clazz != null && clazz.isProgramClass()) {
+ // If we were to merge [other] into its sub class, then we would implicitly change the
+ // signature of this method, and therefore break the invariant.
+ pinnedTypes.add(clazz.type);
+ }
+ for (DexType parameterType : method.proto.parameters.values) {
+ clazz = appInfo.definitionFor(parameterType);
+ if (clazz != null && clazz.isProgramClass()) {
+ // If we were to merge [other] into its sub class, then we would implicitly change the
+ // signature of this method, and therefore break the invariant.
+ pinnedTypes.add(clazz.type);
+ }
+ }
+ }
+ }
+ }
+
private boolean isMergeCandidate(DexProgramClass clazz, Set<DexType> pinnedTypes) {
if (appInfo.instantiatedTypes.contains(clazz.type)
|| appInfo.isPinned(clazz.type)
@@ -432,6 +457,10 @@
directMethods.add(renameConstructor(directMethod));
} else {
directMethods.add(directMethod);
+
+ if (!directMethod.isStaticMethod()) {
+ blockRedirectionOfSuperCalls(directMethod.method);
+ }
}
}
@@ -467,6 +496,7 @@
// Record that invoke-super instructions in the target class should be redirected to the
// newly created direct method.
redirectSuperCallsInTarget(virtualMethod.method, resultingDirectMethod.method);
+ blockRedirectionOfSuperCalls(resultingDirectMethod.method);
if (shadowedBy == null) {
// In addition to the newly added direct method, create a virtual method such that we do
@@ -490,6 +520,7 @@
(existing, method) -> {
DexEncodedMethod renamedMethod = renameMethod(method, target.type, true);
deferredRenamings.map(method.method, renamedMethod.method);
+ blockRedirectionOfSuperCalls(renamedMethod.method);
return renamedMethod;
});
Collection<DexEncodedMethod> mergedVirtualMethods =
@@ -578,6 +609,26 @@
}
}
+ private void blockRedirectionOfSuperCalls(DexMethod method) {
+ // We are merging a class B into C. The methods from B are being moved into C, and then we
+ // subsequently rewrite the invoke-super instructions in C that hit a method in B, such that
+ // they use an invoke-direct instruction instead. In this process, we need to avoid rewriting
+ // the invoke-super instructions that originally was in the superclass B.
+ //
+ // Example:
+ // class A {
+ // public void m() {}
+ // }
+ // class B extends A {
+ // public void m() { super.m(); } <- invoke must not be rewritten to invoke-direct
+ // (this would lead to an infinite loop)
+ // }
+ // class C extends B {
+ // public void m() { super.m(); } <- invoke needs to be rewritten to invoke-direct
+ // }
+ deferredRenamings.markMethodAsMerged(method);
+ }
+
private boolean resolutionSucceeds(DexMethod targetMethod) {
DexClass enclosingClass = appInfo.definitionFor(targetMethod.holder);
return enclosingClass != null
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
index ea57d1d..942a857 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
@@ -45,16 +45,19 @@
private final Map<DexField, DexField> fieldMap;
private final Map<DexMethod, DexMethod> methodMap;
+ private final Set<DexMethod> mergedMethods;
private final Map<DexType, Map<DexMethod, DexMethod>> contextualVirtualToDirectMethodMaps;
public VerticalClassMergerGraphLense(
Map<DexField, DexField> fieldMap,
Map<DexMethod, DexMethod> methodMap,
+ Set<DexMethod> mergedMethods,
Map<DexType, Map<DexMethod, DexMethod>> contextualVirtualToDirectMethodMaps,
GraphLense previousLense) {
this.previousLense = previousLense;
this.fieldMap = fieldMap;
this.methodMap = methodMap;
+ this.mergedMethods = mergedMethods;
this.contextualVirtualToDirectMethodMaps = contextualVirtualToDirectMethodMaps;
}
@@ -67,7 +70,7 @@
public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type) {
assert isContextFreeForMethod(method) || (context != null && type != null);
DexMethod previous = previousLense.lookupMethod(method, context, type);
- if (type == Type.SUPER) {
+ if (type == Type.SUPER && !mergedMethods.contains(context.method)) {
Map<DexMethod, DexMethod> virtualToDirectMethodMap =
contextualVirtualToDirectMethodMaps.get(context.method.holder);
if (virtualToDirectMethodMap != null) {
@@ -127,6 +130,7 @@
private final ImmutableMap.Builder<DexField, DexField> fieldMapBuilder = ImmutableMap.builder();
private final ImmutableMap.Builder<DexMethod, DexMethod> methodMapBuilder =
ImmutableMap.builder();
+ private final ImmutableSet.Builder<DexMethod> mergedMethodsBuilder = ImmutableSet.builder();
private final Map<DexType, Map<DexMethod, DexMethod>> contextualVirtualToDirectMethodMaps =
new HashMap<>();
@@ -141,7 +145,15 @@
return previousLense;
}
return new VerticalClassMergerGraphLense(
- fieldMap, methodMap, contextualVirtualToDirectMethodMaps, previousLense);
+ fieldMap,
+ methodMap,
+ mergedMethodsBuilder.build(),
+ contextualVirtualToDirectMethodMaps,
+ previousLense);
+ }
+
+ public void markMethodAsMerged(DexMethod method) {
+ mergedMethodsBuilder.add(method);
}
public void map(DexField from, DexField to) {
@@ -161,6 +173,7 @@
public void merge(VerticalClassMergerGraphLense.Builder builder) {
fieldMapBuilder.putAll(builder.fieldMapBuilder.build());
methodMapBuilder.putAll(builder.methodMapBuilder.build());
+ mergedMethodsBuilder.addAll(builder.mergedMethodsBuilder.build());
for (DexType context : builder.contextualVirtualToDirectMethodMaps.keySet()) {
Map<DexMethod, DexMethod> current = contextualVirtualToDirectMethodMaps.get(context);
Map<DexMethod, DexMethod> other = builder.contextualVirtualToDirectMethodMaps.get(context);
diff --git a/src/test/examples/classmerging/RewritePinnedMethodTest.java b/src/test/examples/classmerging/RewritePinnedMethodTest.java
new file mode 100644
index 0000000..3d25805
--- /dev/null
+++ b/src/test/examples/classmerging/RewritePinnedMethodTest.java
@@ -0,0 +1,41 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package classmerging;
+
+public class RewritePinnedMethodTest {
+
+ public static void main(String[] args) {
+ C obj = new C();
+ obj.m();
+ }
+
+ // A.m is pinned by a keep rule, to prevent it from being merged into B.
+ public static class A {
+ public void m() {
+ System.out.println("In A.m");
+ }
+ }
+
+ // B is merged into C.
+ public static class B extends A {
+ @Override
+ public void m() {
+ System.out.println("In B.m");
+ super.m();
+ }
+ }
+
+ public static class C extends B {
+ @Override
+ public void m() {
+ System.out.println("In C.m");
+ // This invocation is changed from invoke-super to invoke-direct. It would be valid for this
+ // instruction to be on the form "invoke-super A.m". Therefore, the graph lense contains a
+ // mapping for "A.m" from (context: "C.m", type: SUPER) to C.m$B, where the method C.m$B is
+ // the direct method that gets created for B.m during vertical class merging.
+ super.m();
+ }
+ }
+}
diff --git a/src/test/examples/classmerging/keep-rules.txt b/src/test/examples/classmerging/keep-rules.txt
index 01e35f3..1a20ce3 100644
--- a/src/test/examples/classmerging/keep-rules.txt
+++ b/src/test/examples/classmerging/keep-rules.txt
@@ -13,6 +13,9 @@
-keep public class classmerging.ExceptionTest {
public static void main(...);
}
+-keep public class classmerging.RewritePinnedMethodTest {
+ public static void main(...);
+}
-keep public class classmerging.SimpleInterfaceAccessTest {
public static void main(...);
}
diff --git a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
index 7016163..1e71a82 100644
--- a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
+++ b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
@@ -289,6 +289,32 @@
"-keep public class classmerging.pkg.SimpleInterfaceImplRetriever"));
}
+ // TODO(christofferqa): This test checks that the invoke-super instruction in B is not rewritten
+ // into an invoke-direct instruction after it gets merged into class C. We should add a test that
+ // checks that this works with and without inlining of the method B.m().
+ @Test
+ public void testRewritePinnedMethod() throws Exception {
+ String main = "classmerging.RewritePinnedMethodTest";
+ Path[] programFiles =
+ new Path[] {
+ CF_DIR.resolve("RewritePinnedMethodTest.class"),
+ CF_DIR.resolve("RewritePinnedMethodTest$A.class"),
+ CF_DIR.resolve("RewritePinnedMethodTest$B.class"),
+ CF_DIR.resolve("RewritePinnedMethodTest$C.class")
+ };
+ Set<String> preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.RewritePinnedMethodTest",
+ "classmerging.RewritePinnedMethodTest$A",
+ "classmerging.RewritePinnedMethodTest$C");
+ runTest(
+ main,
+ programFiles,
+ preservedClassNames,
+ getProguardConfig(
+ EXAMPLE_KEEP, "-keep class classmerging.RewritePinnedMethodTest$A { *; }"));
+ }
+
@Test
public void testTemplateMethodPattern() throws Exception {
String main = "classmerging.TemplateMethodTest";
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerTest.java b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerTest.java
index 5df1cee..38768cd 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerTest.java
@@ -4,9 +4,11 @@
package com.android.tools.r8.ir.optimize.classinliner;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import com.android.tools.r8.OutputMode;
@@ -32,10 +34,13 @@
import com.android.tools.r8.ir.optimize.classinliner.trivial.Iface2Impl;
import com.android.tools.r8.ir.optimize.classinliner.trivial.ReferencedFields;
import com.android.tools.r8.ir.optimize.classinliner.trivial.TrivialTestClass;
+import com.android.tools.r8.jasmin.JasminBuilder;
+import com.android.tools.r8.jasmin.JasminBuilder.ClassBuilder;
import com.android.tools.r8.naming.MemberNaming.MethodSignature;
import com.android.tools.r8.utils.AndroidApp;
import com.android.tools.r8.utils.DexInspector;
import com.android.tools.r8.utils.DexInspector.ClassSubject;
+import com.android.tools.r8.utils.InternalOptions;
import com.google.common.collect.Sets;
import java.nio.file.Path;
import java.util.Collections;
@@ -178,6 +183,39 @@
assertFalse(inspector.clazz(BuildersTestClass.Pos.class).isPresent());
}
+ @Test
+ public void testErroneousInput() throws Exception {
+ JasminBuilder builder = new JasminBuilder();
+
+ ClassBuilder testClass = builder.addClass("A");
+ testClass.addStaticFinalField("f", "I", "123");
+ testClass.addDefaultConstructor();
+
+ ClassBuilder mainClass = builder.addClass("Main");
+ mainClass.addMainMethod(
+ ".limit stack 3",
+ ".limit locals 1",
+ " getstatic java/lang/System/out Ljava/io/PrintStream;",
+ " new A",
+ " dup",
+ " invokespecial A/<init>()V",
+ " getfield A/f I",
+ " invokevirtual java/io/PrintStream/print(I)V",
+ " return");
+
+ AndroidApp compiled =
+ compileWithR8(builder.build(), getProguardConfig(mainClass.name), this::configure);
+
+ // Check that the code fails with an IncompatibleClassChangeError with Java.
+ ProcessResult javaResult =
+ runOnJavaRaw(mainClass.name, builder.buildClasses().toArray(new byte[2][]));
+ assertThat(javaResult.stderr, containsString("IncompatibleClassChangeError"));
+
+ // Check that the code fails with an IncompatibleClassChangeError with ART.
+ ProcessResult artResult = runOnArtRaw(compiled, mainClass.name);
+ assertThat(artResult.stderr, containsString("IncompatibleClassChangeError"));
+ }
+
private Set<String> collectNewInstanceTypes(
ClassSubject clazz, String methodName, String... params) {
assertNotNull(clazz);
@@ -189,15 +227,8 @@
}
private AndroidApp runR8(AndroidApp app, Class mainClass) throws Exception {
- String config = keepMainProguardConfiguration(mainClass) + "\n"
- + "-dontobfuscate\n"
- + "-allowaccessmodification";
-
- AndroidApp compiled = compileWithR8(app, config,
- o -> {
- o.enableClassInlining = true;
- o.classInliningInstructionLimit = 10000;
- });
+ AndroidApp compiled =
+ compileWithR8(app, getProguardConfig(mainClass.getCanonicalName()), this::configure);
// Materialize file for execution.
Path generatedDexFile = temp.getRoot().toPath().resolve("classes.jar");
@@ -220,4 +251,16 @@
return compiled;
}
+
+ private String getProguardConfig(String main) {
+ return keepMainProguardConfiguration(main)
+ + "\n"
+ + "-dontobfuscate\n"
+ + "-allowaccessmodification";
+ }
+
+ private void configure(InternalOptions options) {
+ options.enableClassInlining = true;
+ options.classInliningInstructionLimit = 10000;
+ }
}
diff --git a/third_party/r8.tar.gz.sha1 b/third_party/r8.tar.gz.sha1
index fbb2aa8..06f1922 100644
--- a/third_party/r8.tar.gz.sha1
+++ b/third_party/r8.tar.gz.sha1
@@ -1 +1 @@
-38b85dcea75f12c37332a5425c87733e78754ba6
\ No newline at end of file
+ad3858bd8a8597f674d1af6bb762c16672dc6436
\ No newline at end of file
diff --git a/tools/minify_tool.py b/tools/minify_tool.py
index 8f440dd..8b086e5 100755
--- a/tools/minify_tool.py
+++ b/tools/minify_tool.py
@@ -19,9 +19,10 @@
import re
import sys
import time
+import zipfile
+
import toolhelper
import utils
-import zipfile
KEEP = '-keep public class %s { public static void main(...); }\n'
MANIFEST_PATH = 'META-INF/MANIFEST.MF'
@@ -85,7 +86,8 @@
return mo.group(1)
def minify_tool(mainclass=None, input_jar=utils.R8_JAR, output_jar=None,
- lib=utils.RT_JAR, debug=True, build=True, benchmark_name=None):
+ lib=utils.RT_JAR, debug=True, build=True, benchmark_name=None,
+ track_memory_file=None):
if output_jar is None:
output_jar = generate_output_name(input_jar, mainclass)
with utils.TempDir() as path:
@@ -105,10 +107,15 @@
'--release',
tmp_input_path)
start_time = time.time()
- return_code = toolhelper.run('r8', args, debug=debug, build=build)
+ return_code = toolhelper.run('r8', args, debug=debug, build=build,
+ track_memory_file=track_memory_file)
if benchmark_name:
elapsed_ms = 1000 * (time.time() - start_time)
print('%s(RunTimeRaw): %s ms' % (benchmark_name, elapsed_ms))
+ if track_memory_file:
+ print('%s(MemoryUse): %s' %
+ (benchmark_name, utils.grep_memoryuse(track_memory_file)))
+
return return_code
if __name__ == '__main__':
diff --git a/tools/run_bootstrap_benchmark.py b/tools/run_bootstrap_benchmark.py
index 2ae97b2..8230c71 100755
--- a/tools/run_bootstrap_benchmark.py
+++ b/tools/run_bootstrap_benchmark.py
@@ -3,21 +3,53 @@
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
-import argparse
-import minify_tool
import os
import sys
+
+import minify_tool
+import toolhelper
import utils
-
PINNED_R8_JAR = os.path.join(utils.REPO_ROOT, 'third_party/r8/r8.jar')
+PINNED_PGR8_JAR = os.path.join(utils.REPO_ROOT, 'third_party/r8/r8-pg6.0.1.jar')
-parser = argparse.ArgumentParser()
-parser.add_argument(
- '--name', metavar='NAME', dest='benchmark_name',
- help='Print "<NAME>(RunTimeRaw): <elapsed> ms" at the end')
-
+def dex(input, output):
+ return_code = toolhelper.run(
+ 'd8', [
+ input,
+ '--output', output,
+ '--lib', utils.RT_JAR,
+ '--min-api', '10000',
+ '--no-desugaring',
+ ],
+ debug=False,
+ build=False)
+ if return_code != 0:
+ sys.exit(return_code)
if __name__ == '__main__':
- sys.exit(minify_tool.minify_tool(input_jar=PINNED_R8_JAR, debug=False,
- build=False, **vars(parser.parse_args())))
+ with utils.TempDir() as temp:
+ memory_file = os.path.join(temp, 'memory.dump')
+ r8_output = os.path.join(temp, 'r8.zip')
+ d8_r8_output = os.path.join(temp, 'd8r8.zip')
+ d8_pg_output = os.path.join(temp, 'd8pg.zip')
+
+ return_code = minify_tool.minify_tool(
+ input_jar=PINNED_R8_JAR,
+ output_jar=r8_output,
+ debug=False,
+ build=False,
+ track_memory_file=memory_file,
+ benchmark_name="BootstrapR8")
+ if return_code != 0:
+ sys.exit(return_code)
+
+ dex(r8_output, d8_r8_output)
+ print "BootstrapR8(CodeSize):", os.path.getsize(r8_output)
+ print "BootstrapR8Dex(CodeSize):", os.path.getsize(d8_r8_output)
+
+ dex(PINNED_PGR8_JAR, d8_pg_output)
+ print "BootstrapPG(CodeSize):", os.path.getsize(PINNED_PGR8_JAR)
+ print "BootstrapPGDex(CodeSize):", os.path.getsize(d8_pg_output)
+
+ sys.exit(0)
\ No newline at end of file