Compiler Design Spring 2017 7.5 Method invocation Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1
Admin issues There will be a recitation session today In CAB G 11 @ 15:15 Discussion of Homework 4 (code generation) Feedback for HWP (design patterns) available Check your subversion repository Next lecture takes place next Thursday (May 4) 2
Outline 7.5.1 Call-by-value 7.5.2 Call-by-reference 7.5.3 Call-by-result 7.5.4 Call-by-name 7.5.5 Profiling 3
4
7.5.2 Call-by-reference Caller passes address to callee Could be address of an object a field a local variable an array element Indicate with keyword or symbol if a parameter is passed by reference Call-by-value usually supported as well Each parameter can be handled differently Example void foo(ref type x) { } // definition foo(ref y) // call site 5
7.5.2 Call-by-reference (cont d) Caller must evaluate address of parameter(s) Pass address from caller to callee Usually uses space that holds parameter Callee de-references actual to get value at address or to update storage location at the address Two steps Retrieve effective address Use address to read/write parameter 6
Discussion Advantages of call-by-reference Callee can modify variables in caller s scope Efficient passing of large objects/arrays [Maybe] restrict callee access to caller s objects Disadvantages of call-by-reference Callee can modify variables in caller s scope May be able to access any location Bug in callee may have global effect Aliasing the norm 8
Disadvantages (continued) Possible overhead and/or difficulties in optimization void foo (ref int x, ref int y) { int z; x = x + z ; y = z + 1 ; = x... ; } One extra step for each access Cannot assume that x is unchanged Must prohibit calls like foo(k m, ref x) Or (maybe) generate temporary for k m in caller 9
7.5.3 Call-by-result Call-by-result is a combination of Call-by-value (inside the callee) and Call-by-reference (upon reaching the end of the callee) Callee copies the local value of a parameter (value inside the callee) back to the caller Parameter x is like a local variable initialized by caller void foo(result int x) Effect of call obtained by inspecting x after return 11
void bar (result int x, result int y) { int a, b; x = x + 1; a = 2; y = y 1; b = 4; x = x + a; } // somewhere int k = 1; int m = 2; bar (k, m) // k == 4, m == 1 12
Discussion void foo(result int x) Callee needs address of x To store result at the end Implement passing of parameter(s) like call-by-reference Cannot allow foo(a b) Do we need a return statement? Yes: indication that parameters have meaningful value(s) No: value(s) copied back to caller in any case 13
Implementation void foo(result int x) Access to x inside method foo() different from call-byreference Callee cannot modify caller s context Need to create a temporary, initialize temporary with parameter value Upon return copy value from temporary to caller 14
Implementation (cont d) void bar(result int x, result int y) Use temporary (say xt, yt) Body of bar: xt = x; yt = y; xt = xt + 1; a = 2; yt = yt 1; b = 4; xt = xt + a; x = xt; y = yt; 15
Observation Final result depends on order of copying results Consider int k = 1; bar (k, k) // k == 4 or k == 0 void bar (result int x, result int y) { int a, b; x = x + 1; a = 2; y = y 1; b = 4; x = x + a; } Should be decided by language reference manual Either from y 1 y n or y n y 1 16
Discussion Advantages of call-by-result Multiple return values for a function Disadvantages of call-by-result Implementation overhead Aliasing makes programs difficult to understand 17
7.5.4 Call-by-name Idea: textual substitution of the formal parameter by the actual parameter Hand-off a variable to callee Example void foo (name int x, name int k) { k = 2; x = 5; k = 3; x = 1; } 18
Example (continued) Call site int [] A = new int[10]; int j; foo (A [j * 2], j) With call-by-name, the call means foo(a [j * 2], j) Execution k = 2; j = 2; x = 5; A[j*2] = 5; // A[4] k = 3; j = 3; x = 1; A[j*2] = 1; // A[6] void foo (name int x, name int k) { k = 2; x = 5; k = 3; x = 1; } 19
Discussion Who wants that? How does the compiler implement call-by-name? 20
Option 1: interpreter Use just-in-time compiler Invoke compiler at each call site, for each invocation Compiler automatically captures actual parameters 21
Option 2: stub generation Ahead-of-time compiler produces (for each call-by-name parameter) a stub that generates the address of the parameter Callee invokes stub to access a call-by-name parameter For each parameter P i the stub EP i yields the address 22
Example, continued foo(a [j * 2], j) EP 2 Get address of j Put address into location L 2 Continue as in call-by-reference EP 1 Get address of j Get value, compute 2, put value into temporary location T Get address of A[T] Put address into location L 1 Continue as in call-by-reference 23
Discussion Difficult to implement In ahead-of-time framework Potentially expensive execution Many indirections Frequent compilations (in just-in-time framework) Destroys modularity Cannot deduce effect of method foo() by inspecting body of foo() 24
Comparison (Not really JavaLi) No need to implement But you should know concepts Example Uses global variables Could be in an instance int j; int [] B = new int[2]; 25
x is passed by void quest( int x){ j = 0; x = x + 2; B[j] = 10; j = 1; x = x + 2; } void main() { B[0] = 1; B[1] = 1; j = 0; quest(b[j]); } x = 1 j = 0 x = 3 value reference result name B[0]=10 B[0]=10 B[0]=10 B[0]=10 j = 1 x = 5 } 10 1 1 @B[0]:1 @B[0]:1 B[j] j = 0 j = 0 j = 0 B[0]=3 j = 1 j = 1 j = 1 B[0]=12 } 12 1 1 xt = 3 xt = 5 }B[0]=xt 5 1 1 B[0]=3 B[1]=3 } 10 3 1 30
7.5.5 Profiling Gather information about a program s runtime behavior Today: Where does a program spend its execution time? Where == in which method(s) Guides optimization effort by programmer Profiling performed by external tools By inspecting stack layout E.g., perf_events on Linux, pstack on Solaris Requires close coupling between profilers / compilers Stack layout must allow inspection Symbol information must be available (at well-known location) 31
callee caller caller s caller Temp k... Temp 0 Local m Local 0 old SP old FP Return address target (parameter y0) parameter y1 parameter yn Return value Temp q Local 0 old SP old FP Return address Activation records revisited Stack pointer (%rsp) Frame pointer (%rbp) Question 1: How to get return address? Question 2: Where does old FP point to? Question 3: Where does Return address point to? 33
If an external tool were to interrupt program looking at %rip (instruction pointer) identifies currently executing method E.g., for the call hierarchy m1() à m2() à m3() à m4() %rip identifies m4() Looking at %rbp (frame pointer) gives access to Return address in m4() s activation record Identifies caller of m4() (i.e., m3()) Frame pointer of m3() Looking at frame pointer of m3() gives access to Return address of m3() s activation record and so on 34
Gathering information Interrupt program at regular time intervals Inspect stack (walk the stack), record data Interrupt #1 m1() à m2() à m3() à m4() Interrupt #2 m1() à m2() à m3() à m5() Interrupt #3 m1() à m2() à m3() à m4() Interrupt #4 m6() à m7() à m8() à m9() 35
Presenting result Many different ways to present data to user (programmer) Option #1: Present number of samples m1 m2 m3 m4 m5 m6 m7 m8 m9 # of samples 3 3 3 2 1 1 1 1 1 % of total 19% 19% 19% 13% 6% 6% 6% 6% 6% Problem: Not obvious that call chain m1() à m2() à m3() accounts for 75% of the execution time Other (better?) option: Flame graphs 36
Flame graphs Visualization that gives intuitive insight into call chains Many details missing: Lots of flat regions in Java part JVM JIT compilers do not maintain walkable stack activation records Frame pointer (%rbp) not maintained, used as general purpose register Also done by GCC (-fomit-frame-pointer) Temp k... Temp 0 callee Local m Local 0 old SP old FP Return address Frame pointer (%rbp) 37
Frame pointer support in the JVM Coupling JIT compilers / runtime system / profilers Change compilers to maintain the frame pointer Store program symbols at well-known location Where profiling tool can find them Available since Java 8u60 38
Flame graphs More on the topic http://techblog.netflix.com/2015/07/java-in-flames.html Includes before/after graphs shown in the lecture 39