use of org.jikesrvm.compilers.opt.driver.CompilationPlan in project JikesRVM by JikesRVM.
the class OSRProfiler method invalidateState.
// invalidate an execution state
private static synchronized void invalidateState(ExecutionState state) {
// step 1: invalidate the compiled method with this OSR assumption
// how does this affect the performance?
CompiledMethod mostRecentlyCompiledMethod = CompiledMethods.getCompiledMethod(state.cmid);
if (VM.VerifyAssertions) {
VM._assert(mostRecentlyCompiledMethod.getMethod() == state.meth);
}
// be invalidated in more than one thread at the same time
if (mostRecentlyCompiledMethod != state.meth.getCurrentCompiledMethod()) {
return;
}
// make sure the compiled method is an opt one
if (!(mostRecentlyCompiledMethod instanceof OptCompiledMethod)) {
return;
}
// reset the compiled method to null first, if other thread invokes
// this method before following opt recompilation, it can avoid OSR
state.meth.invalidateCompiledMethod(mostRecentlyCompiledMethod);
// a list of state from callee -> caller
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OSR " + OSRProfiler.invalidations + " : " + state.bcIndex + "@" + state.meth);
}
// simply reset the compiled method to null is not good
// for long run loops, because invalidate may cause
// the method falls back to the baseline again...
// NOW, we look for the previous compilation plan, and reuse
// the compilation plan.
boolean recmplsucc = false;
if (Controller.enabled) {
CompilationPlan cmplplan = null;
if (Controller.options.ENABLE_PRECOMPILE && CompilerAdviceAttribute.hasAdvice()) {
CompilerAdviceAttribute attr = CompilerAdviceAttribute.getCompilerAdviceInfo(state.meth);
if (VM.VerifyAssertions) {
VM._assert(attr.getCompiler() == CompiledMethod.OPT);
}
if (Controller.options.counters()) {
// for invocation counter, we only use one optimization level
cmplplan = InvocationCounts.createCompilationPlan(state.meth);
} else {
// for now there is not two options for sampling, so
// we don't have to use: if (Controller.options.sampling())
cmplplan = Controller.recompilationStrategy.createCompilationPlan(state.meth, attr.getOptLevel(), null);
}
} else {
ControllerPlan ctrlplan = ControllerMemory.findMatchingPlan(mostRecentlyCompiledMethod);
if (ctrlplan != null) {
cmplplan = ctrlplan.getCompPlan();
}
}
if (cmplplan != null) {
if (VM.VerifyAssertions) {
VM._assert(cmplplan.getMethod() == state.meth);
}
// for invalidated method, we do not perform OSR guarded inlining anymore.
// the Options object may be shared by several methods,
// we have to reset it back
boolean savedOsr = cmplplan.options.OSR_GUARDED_INLINING;
cmplplan.options.OSR_GUARDED_INLINING = false;
int newcmid = RuntimeCompiler.recompileWithOpt(cmplplan);
cmplplan.options.OSR_GUARDED_INLINING = savedOsr;
if (newcmid != -1) {
AOSLogging.logger.debug("recompiling state with opt succeeded " + state.cmid);
AOSLogging.logger.debug("new cmid " + newcmid);
// transfer hotness to the new cmid
double oldSamples = Controller.methodSamples.getData(state.cmid);
Controller.methodSamples.reset(state.cmid);
Controller.methodSamples.augmentData(newcmid, oldSamples);
recmplsucc = true;
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" recompile " + state.meth + " at -O" + cmplplan.options.getOptLevel());
}
}
}
}
if (!recmplsucc) {
int newcmid = RuntimeCompiler.recompileWithOpt(state.meth);
if (newcmid == -1) {
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" opt recompilation failed!");
}
state.meth.invalidateCompiledMethod(mostRecentlyCompiledMethod);
}
}
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" opt recompilation done!");
}
}
use of org.jikesrvm.compilers.opt.driver.CompilationPlan in project JikesRVM by JikesRVM.
the class SpecialCompiler method optCompile.
/**
* <ol>
* <li>generate prologue PSEUDO_bytecode from the state.
* <li>make new bytecodes with prologue.
* <li>set method's bytecode to specialized one.
* <li>adjust exception map, line number map.
* <li>compile the method.
* <li>restore bytecode, exception, linenumber map to the original one.
* </ol>
*
* @param state the execution state for the compilation
* @return the compiled method produced by the optimizing compiler
*/
public static CompiledMethod optCompile(ExecutionState state) {
NormalMethod method = state.getMethod();
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : starts compiling " + method);
}
ControllerPlan latestPlan = ControllerMemory.findLatestPlan(method);
OptOptions _options = null;
if (latestPlan != null) {
_options = latestPlan.getCompPlan().options.dup();
} else {
// no previous compilation plan, a long run loop promoted from baseline.
// this only happens when testing, not in real code
_options = new OptOptions();
_options.setOptLevel(0);
}
// disable OSR points in specialized method
_options.OSR_GUARDED_INLINING = false;
CompilationPlan compPlan = new CompilationPlan(method, (OptimizationPlanElement[]) RuntimeCompiler.optimizationPlan, null, _options);
// it is also necessary to recompile the current method
// without OSR.
/* generate prologue bytes */
byte[] prologue = state.generatePrologue();
int prosize = prologue.length;
method.setForOsrSpecialization(prologue, state.getMaxStackHeight());
int[] oldStartPCs = null;
int[] oldEndPCs = null;
int[] oldHandlerPCs = null;
/* adjust exception table. */
{
// if (VM.TraceOnStackReplacement) {
// VM.sysWriteln("OPT adjust exception table.");
// }
ExceptionHandlerMap exceptionHandlerMap = method.getExceptionHandlerMap();
if (exceptionHandlerMap != null) {
oldStartPCs = exceptionHandlerMap.getStartPC();
oldEndPCs = exceptionHandlerMap.getEndPC();
oldHandlerPCs = exceptionHandlerMap.getHandlerPC();
int n = oldStartPCs.length;
int[] newStartPCs = new int[n];
System.arraycopy(oldStartPCs, 0, newStartPCs, 0, n);
exceptionHandlerMap.setStartPC(newStartPCs);
int[] newEndPCs = new int[n];
System.arraycopy(oldEndPCs, 0, newEndPCs, 0, n);
exceptionHandlerMap.setEndPC(newEndPCs);
int[] newHandlerPCs = new int[n];
System.arraycopy(oldHandlerPCs, 0, newHandlerPCs, 0, n);
exceptionHandlerMap.setHandlerPC(newHandlerPCs);
for (int i = 0; i < n; i++) {
newStartPCs[i] += prosize;
newEndPCs[i] += prosize;
newHandlerPCs[i] += prosize;
}
}
}
CompiledMethod newCompiledMethod = RuntimeCompiler.recompileWithOptOnStackSpecialization(compPlan);
// restore original bytecode, exception table, and line number table
method.finalizeOsrSpecialization();
{
ExceptionHandlerMap exceptionHandlerMap = method.getExceptionHandlerMap();
if (exceptionHandlerMap != null) {
exceptionHandlerMap.setStartPC(oldStartPCs);
exceptionHandlerMap.setEndPC(oldEndPCs);
exceptionHandlerMap.setHandlerPC(oldHandlerPCs);
}
}
// reverse back to the baseline
if (newCompiledMethod == null) {
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : fialed, because compilation in progress, " + "fall back to baseline");
}
return baselineCompile(state);
}
// mark the method is a specialized one
newCompiledMethod.setSpecialForOSR();
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : done");
VM.sysWriteln();
}
return newCompiledMethod;
}
Aggregations