changeset 12710:60142b15ed42

Truffle: fixed bug where invocation counter was decremented twice after inlining; cleanup of optimized call target and profile.
author Christian Humer <christian.humer@gmail.com>
date Thu, 07 Nov 2013 19:07:41 +0100
parents d2d0c44662bb
children fb4658e16b5d
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/CompilationProfile.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java
diffstat 2 files changed, 38 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/CompilationProfile.java	Thu Nov 07 16:21:06 2013 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/CompilationProfile.java	Thu Nov 07 19:07:41 2013 +0100
@@ -94,9 +94,7 @@
         invalidationCount++;
         int invalidationReprofileCount = TruffleInvalidationReprofileCount.getValue();
         invokeCounter = invalidationReprofileCount;
-        if (TruffleFunctionInlining.getValue()) {
-            originalInvokeCounter += invalidationReprofileCount;
-        }
+        originalInvokeCounter += invalidationReprofileCount;
     }
 
     void reportInterpreterCall() {
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java	Thu Nov 07 16:21:06 2013 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java	Thu Nov 07 19:07:41 2013 +0100
@@ -48,7 +48,7 @@
     private final CompilationProfile compilationProfile;
     private final CompilationPolicy compilationPolicy;
     private final TruffleInlining inlining;
-    private boolean disableCompilation;
+    private boolean compilationEnabled;
     private int callCount;
 
     protected OptimizedCallTarget(RootNode rootNode, FrameDescriptor descriptor, TruffleCompiler compiler, int invokeCounter, int compilationThreshold) {
@@ -63,6 +63,7 @@
         } else {
             compilationPolicy = new DefaultCompilationPolicy();
         }
+        this.compilationEnabled = true;
 
         if (TruffleCallTargetProfiling.getValue()) {
             registerCallTarget(this);
@@ -129,43 +130,48 @@
     private Object interpreterCall(PackedFrame caller, Arguments args) {
         CompilerAsserts.neverPartOfCompilation();
         compilationProfile.reportInterpreterCall();
-        if (disableCompilation || !compilationPolicy.shouldCompile(compilationProfile)) {
-            return executeHelper(caller, args);
-        } else {
-            return compileOrInline(caller, args);
+        if (compilationEnabled) {
+            boolean inlined = shouldInline() && inline();
+            if (!inlined && shouldCompile()) {
+                compile();
+            }
         }
+        return executeHelper(caller, args);
     }
 
-    private Object compileOrInline(PackedFrame caller, Arguments args) {
+    private boolean shouldCompile() {
+        return compilationPolicy.shouldCompile(compilationProfile);
+    }
+
+    private static boolean shouldInline() {
+        return TruffleFunctionInlining.getValue();
+    }
+
+    public void compile() {
+        CompilerAsserts.neverPartOfCompilation();
         if (installedCodeTask != null) {
             // There is already a compilation running.
             if (installedCodeTask.isCancelled()) {
                 installedCodeTask = null;
             } else {
                 if (installedCodeTask.isDone()) {
-                    receiveInstalledCode();
+                    installedCode = receiveInstalledCode();
                 }
-                return executeHelper(caller, args);
+                return;
             }
         }
 
-        if (TruffleFunctionInlining.getValue() && inline()) {
-            compilationProfile.reportInliningPerformed();
-            return call(caller, args);
-        } else {
-            compile();
-            return executeHelper(caller, args);
+        this.installedCodeTask = compiler.compile(this);
+        if (!TruffleBackgroundCompilation.getValue()) {
+            installedCode = receiveInstalledCode();
         }
     }
 
-    private void receiveInstalledCode() {
+    private InstalledCode receiveInstalledCode() {
         try {
-            this.installedCode = installedCodeTask.get();
-            if (TruffleCallTargetProfiling.getValue()) {
-                resetProfiling();
-            }
+            return installedCodeTask.get();
         } catch (InterruptedException | ExecutionException e) {
-            disableCompilation = true;
+            compilationEnabled = false;
             OUT.printf("[truffle] opt failed %-48s  %s\n", rootNode, e.getMessage());
             if (e.getCause() instanceof BailoutException) {
                 // Bailout => move on.
@@ -177,21 +183,21 @@
                     System.exit(-1);
                 }
             }
+            return null;
         }
-        installedCodeTask = null;
     }
 
+    /**
+     * Forces inlining whether or not function inlining is enabled.
+     * 
+     * @return true if an inlining was performed
+     */
     public boolean inline() {
-        CompilerAsserts.neverPartOfCompilation();
-        return inlining.performInlining(this);
-    }
-
-    public void compile() {
-        CompilerAsserts.neverPartOfCompilation();
-        this.installedCodeTask = compiler.compile(this);
-        if (!TruffleBackgroundCompilation.getValue()) {
-            receiveInstalledCode();
+        boolean result = inlining.performInlining(this);
+        if (result) {
+            compilationProfile.reportInliningPerformed();
         }
+        return result;
     }
 
     public Object executeHelper(PackedFrame caller, Arguments args) {
@@ -219,12 +225,6 @@
         invalidate();
     }
 
-    private static void resetProfiling() {
-        for (OptimizedCallTarget callTarget : OptimizedCallTarget.callTargets.keySet()) {
-            callTarget.callCount = 0;
-        }
-    }
-
     private static void printProfiling() {
         List<OptimizedCallTarget> sortedCallTargets = new ArrayList<>(OptimizedCallTarget.callTargets.keySet());
         Collections.sort(sortedCallTargets, new Comparator<OptimizedCallTarget>() {
@@ -252,7 +252,7 @@
             int nodeCount = NodeUtil.countNodes(callTarget.rootNode);
             int inlinedCallSiteCount = NodeUtil.countNodes(callTarget.rootNode, InlinedCallSite.class);
             String comment = callTarget.installedCode == null ? " int" : "";
-            comment += callTarget.disableCompilation ? " fail" : "";
+            comment += callTarget.compilationEnabled ? "" : " fail";
             OUT.printf("%-50s | %10d | %15d | %15d | %10d | %3d%s\n", callTarget.getRootNode(), callTarget.callCount, inlinedCallSiteCount, notInlinedCallSiteCount, nodeCount,
                             callTarget.getCompilationProfile().getInvalidationCount(), comment);