Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created December 24, 2013 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/8107598 to your computer and use it in GitHub Desktop.
Save rednaxelafx/8107598 to your computer and use it in GitHub Desktop.
Example of HotSpot 25's C1 compiling a MethodHandle (Part I): unable to inline, because root of compilation is the invokeExact() itself, and it can't see the MH is a "constant".
exclude java/io/* *
exclude java/nio/* *
exclude java/util/* *
exclude java/lang/StringBuilder *
exclude java/lang/String *
exclude java/lang/Integer *
exclude java/lang/AbstractStringBuilder *
TieredStopAtLevel=1
CICompilerCount=1
CompileThreshold=1000
-BackgroundCompilation
+PrintVMOptions
+PrintCompilation
+PrintInlining
# The following 2 flags go together, they're used to trust
# final instance fields marked with @Stable as constants
#+UnlockExperimentalVMOptions
#+TrustFinalNonStaticFields
#+FoldStableValues
diff -r a632dd6ef1f9 src/share/vm/c1/c1_GraphBuilder.cpp
--- a/src/share/vm/c1/c1_GraphBuilder.cpp Mon Dec 16 00:44:46 2013 -0800
+++ b/src/share/vm/c1/c1_GraphBuilder.cpp Mon Dec 23 17:32:22 2013 -0800
@@ -4015,6 +4015,9 @@
// get MethodHandle receiver
const int args_base = state()->stack_size() - callee->arg_size();
ValueType* type = state()->stack_at(args_base)->type();
+ tty->print(__FILE__ ":%d: type = ", __LINE__);
+ type->print();
+ tty->print_cr(" %s", type->is_constant() ? "is constant" : "not constant");
if (type->is_constant()) {
ciMethod* target = type->as_ObjectType()->constant_value()->as_method_handle()->get_vmtarget();
// We don't do CHA here so only inline static and statically bindable methods.
import java.lang.invoke.*;
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
public class MHTest {
private static final Lookup LK = lookup();
private static final MethodHandle MH0;
private static final MethodHandle MH1;
static {
MethodHandle mh0 = null;
MethodHandle mh1 = null;
try {
mh0 = LK.findStatic(LK.lookupClass(),
"foo",
methodType(int.class,
int.class, int.class));
mh1 = insertArguments(mh0, 1, 42);
} catch (Throwable t) {
t.printStackTrace();
}
MH0 = mh0;
MH1 = mh1;
}
public static int foo(int x, int y) {
// System.out.println("x: " + x + ", y: " + y);
return x + y;
}
public static void main(String[] args) throws Throwable {
for (int i = 0; i < 1200; i++) {
int dummy = (int) MH1.invokeExact(0);
}
}
}
504 132 b 1 java.lang.invoke.LambdaForm$MH/1414644648::invokeExact_MT (14 bytes)
@ 2 java.lang.invoke.Invokers::checkExactType (30 bytes) force inline by annotation
@ 11 java.lang.invoke.MethodHandle::type (5 bytes)
@ 25 java.lang.invoke.Invokers::newWrongMethodTypeException (36 bytes) callee is too large
/Users/krismo/code/hotspot-comp/hotspot/src/share/vm/c1/c1_GraphBuilder.cpp:4018: type = object not constant
@ 10 java.lang.invoke.MethodHandle::invokeBasic(I)I (0 bytes) receiver not constant
505 133 b 1 java.lang.invoke.Invokers::checkExactType (30 bytes)
@ 11 java.lang.invoke.MethodHandle::type (5 bytes)
@ 25 java.lang.invoke.Invokers::newWrongMethodTypeException (36 bytes) callee is too large
506 134 b 1 java.lang.invoke.BoundMethodHandle$Species_LI::reinvokerTarget (8 bytes)
506 135 b 1 MHTest::foo (4 bytes)
507 136 b 1 java.lang.invoke.LambdaForm$DMH/621009875::invokeStatic_II_I (15 bytes)
@ 1 java.lang.invoke.DirectMethodHandle::internalMemberName (8 bytes) force inline by annotation
@ 11 java.lang.invoke.MethodHandle::linkToStatic(IIL)I (0 bytes) MemberName not constant
508 137 b 1 java.lang.invoke.LambdaForm$BMH/1072591677::reinvoke (26 bytes)
@ 12 java.lang.invoke.MethodHandle::reinvokerTarget (42 bytes) no static binding
/Users/krismo/code/hotspot-comp/hotspot/src/share/vm/c1/c1_GraphBuilder.cpp:4018: type = object not constant
@ 22 java.lang.invoke.MethodHandle::invokeBasic(II)I (0 bytes) receiver not constant
#!/bin/sh
# -Djava.lang.invoke.MethodHandle.COMPILE_THRESHOLD=2
java -XX:Flags=.hotspotrc -XX:CompileCommandFile=.hotspot_compiler MHTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment