001/* 002 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.truffle.test.builtins; 024 025import java.util.concurrent.*; 026 027import com.oracle.graal.truffle.*; 028import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 029import com.oracle.truffle.api.*; 030import com.oracle.truffle.api.dsl.*; 031import com.oracle.truffle.api.frame.*; 032import com.oracle.truffle.api.nodes.*; 033import com.oracle.truffle.sl.*; 034import com.oracle.truffle.sl.runtime.*; 035 036/** 037 * Calls a given function until the Graal runtime decides to optimize the function. Use 038 * <code>waitForOptimization(function)</code> to wait until the runtime system has completed the 039 * possibly parallel optimization. 040 * 041 * @see SLWaitForOptimizationBuiltin 042 */ 043@NodeInfo(shortName = "callUntilOptimized") 044public abstract class SLCallUntilOptimizedBuiltin extends SLGraalRuntimeBuiltin { 045 046 private static final int MAX_CALLS = 10000; 047 private static final Object[] EMPTY_ARGS = new Object[0]; 048 049 @Child private IndirectCallNode indirectCall = Truffle.getRuntime().createIndirectCallNode(); 050 051 @Specialization 052 public SLFunction callUntilCompiled(VirtualFrame frame, SLFunction function, @SuppressWarnings("unused") SLNull checkTarget) { 053 return callUntilCompiled(frame, function, false); 054 } 055 056 @Specialization 057 public SLFunction callUntilCompiled(VirtualFrame frame, SLFunction function, boolean checkTarget) { 058 OptimizedCallTarget target = ((OptimizedCallTarget) function.getCallTarget()); 059 for (int i = 0; i < MAX_CALLS; i++) { 060 if (isCompiling(target)) { 061 break; 062 } else { 063 indirectCall.call(frame, target, EMPTY_ARGS); 064 } 065 } 066 waitForCompilation(target); 067 068 // call one more in compiled 069 indirectCall.call(frame, target, EMPTY_ARGS); 070 071 if (checkTarget) { 072 checkTarget(target); 073 } 074 075 return function; 076 } 077 078 @TruffleBoundary 079 private static void checkTarget(OptimizedCallTarget target) throws SLAssertionError { 080 if (!target.isValid()) { 081 throw new SLAssertionError("Function " + target + " invalidated."); 082 } 083 } 084 085 @TruffleBoundary 086 private static void waitForCompilation(OptimizedCallTarget target) { 087 try { 088 ((GraalTruffleRuntime) Truffle.getRuntime()).waitForCompilation(target, 640000); 089 } catch (ExecutionException | TimeoutException e) { 090 throw new RuntimeException(e); 091 } 092 } 093 094 @TruffleBoundary 095 private static boolean isCompiling(OptimizedCallTarget target) { 096 return ((GraalTruffleRuntime) Truffle.getRuntime()).isCompiling(target) || target.isValid(); 097 } 098}