Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent JIT bodies from strictly outliving methods inlined into them #7673

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 127 additions & 2 deletions compiler/compile/OMRCompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
#include "env/CompilerEnv.hpp"
#include "env/CompileTimeProfiler.hpp"
#include "env/IO.hpp"
#include "env/ObjectModel.hpp"
#include "env/KnownObjectTable.hpp"
#include "env/ObjectModel.hpp"
#include "env/OMRRetainedMethodSet.hpp"
#include "env/PersistentInfo.hpp"
#include "env/StackMemoryRegion.hpp"
#include "env/TRMemory.hpp"
Expand Down Expand Up @@ -287,6 +288,7 @@ OMR::Compilation::Compilation(
_bitVectorPool(self()),
_typeLayoutMap((LayoutComparator()), LayoutAllocator(self()->region())),
_currentILGenCallTarget(NULL),
_retainedMethods(NULL),
_tlsManager(*self())
{
if (target != NULL)
Expand Down Expand Up @@ -924,6 +926,49 @@ static int32_t strHash(const char *str)
return result;
}

/**
* \brief Print all bond methods of the root retained method set to the log
*/
static void traceBondMethods(TR::Compilation *comp)
{
if (!comp->getOption(TR_TraceOptDetails) && !comp->getOption(TR_TraceCG))
{
return;
}

traceMsg(comp, "\nBond methods:\n");

TR_ResolvedMethod *m = NULL;
bool haveBondMethods = false;
auto bondIter = comp->retainedMethods()->bondMethods();
while (bondIter.next(&m))
{
haveBondMethods = true;
traceMsg(
comp,
" %p %.*s.%.*s%.*s\n",
m->getNonPersistentIdentifier(),
m->classNameLength(),
m->classNameChars(),
m->nameLength(),
m->nameChars(),
m->signatureLength(),
m->signatureChars());
}

if (comp->compileRelocatableCode())
{
TR_ASSERT_FATAL(!haveBondMethods, "bonds are meaningless in AOT compilation");
traceMsg(comp, " (to be determined at load time)\n");
}
else if (!haveBondMethods)
{
traceMsg(comp, " (none)\n");
}

traceMsg(comp, "\n");
}

int32_t OMR::Compilation::compile()
{

Expand Down Expand Up @@ -1101,6 +1146,8 @@ int32_t OMR::Compilation::compile()
self()->failCompilation<TR::CompilationException>("Aborting after IL Gen due to TR_TOSS_IL");
}

traceBondMethods(self());

if (_recompilationInfo)
_recompilationInfo->beforeCodeGen();

Expand Down Expand Up @@ -1262,11 +1309,70 @@ TR_YesNoMaybe OMR::Compilation::isCpuExpensiveCompilation(int64_t threshold)

void OMR::Compilation::performOptimizations()
{

_optimizer = TR::Optimizer::createOptimizer(self(), self()->getJittedMethodSymbol(), false);

if (_optimizer)
{
_optimizer->optimize();

if (self()->getOption(TR_DontInlineUnloadableMethods))
{
// Check the inlining table to make sure that all inlined methods are
// guaranteed to outlive this body.
bool trace = self()->getOption(TR_TraceRetainedMethods);
if (trace)
{
traceMsg(self(), "dontInlineUnloadableMethods: check inlining table\n");
}

// Keepalives must be taken into account, since without them it might
// be possible for some inlined methods to be unloaded earlier.
OMR::RetainedMethodSet *retainedMethods =
self()->retainedMethods()->withKeepalivesAttested();

for (uint32_t i = 0; i < self()->getNumInlinedCallSites(); i++)
{
// The bci identifies a call site in the bytecode of the outermost
// method or a previous inlined site, so we already know that the
// call site outlives the JIT body and it's therefore OK to attest
// for the call site.
//
// We need to attest here because the compilation's root retained
// method set doesn't account for any call site attestation that
// was done during inlining on an inline call path that required at
// least one keepalive. For example, if A calls B calls C calls D,
// and if B, C, D are all inlined, and if B needed a keepalive, and
// if there was something useful to attest at the C->D call site,
// that attestation will be missing from the root set.
//
TR_ByteCodeInfo bci = self()->getInlinedCallSite(i)._byteCodeInfo;
if (trace)
{
traceMsg(
self(),
"check inlined site %u, bci=%d:%d\n",
i,
bci.getCallerIndex(),
bci.getByteCodeIndex());
}

retainedMethods->attestLinkedCalleeWillRemainLoaded(bci);

TR_ResolvedMethod *m = self()->getInlinedResolvedMethod(i);
TR_ASSERT_FATAL(
retainedMethods->willRemainLoaded(m),
"unexpectedly inlined method that could get unloaded separately:\n"
" %p %.*s.%.*s%.*s",
m->getPersistentIdentifier(),
m->classNameLength(),
m->classNameChars(),
m->nameLength(),
m->nameChars(),
m->signatureLength(),
m->signatureChars());
}
}
}
}

bool OMR::Compilation::incInlineDepth(TR::ResolvedMethodSymbol * method, TR::Node *callNode, bool directCall, TR_VirtualGuardSelection *guard, TR_OpaqueClassBlock *receiverClass, TR_PrexArgInfo *argInfo)
Expand Down Expand Up @@ -2749,3 +2855,22 @@ OMR::Compilation::insertNewFirstBlock()

return newFirstBlock;
}

OMR::RetainedMethodSet *
OMR::Compilation::retainedMethods()
{
if (_retainedMethods == NULL)
{
_retainedMethods = self()->createRetainedMethods(self()->getMethodBeingCompiled());
}

return _retainedMethods;
}

OMR::RetainedMethodSet *
OMR::Compilation::createRetainedMethods(TR_ResolvedMethod *method)
{
OMR::RetainedMethodSet *parent = NULL;
TR::Region &region = self()->trMemory()->heapMemoryRegion();
return new (region) OMR::RetainedMethodSet(self(), method, parent);
}
24 changes: 24 additions & 0 deletions compiler/compile/OMRCompilation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace OMR { typedef OMR::Compilation CompilationConnector; }
#include "infra/Flags.hpp"
#include "infra/Link.hpp"
#include "infra/List.hpp"
#include "infra/set.hpp"
#include "infra/Stack.hpp"
#include "infra/ThreadLocal.hpp"
#include "optimizer/Optimizations.hpp"
Expand Down Expand Up @@ -108,6 +109,7 @@ namespace TR { class Optimizer; }
namespace TR { class Recompilation; }
namespace TR { class RegisterMappedSymbol; }
namespace TR { class ResolvedMethodSymbol; }
namespace OMR { class RetainedMethodSet; }
namespace TR { class Symbol; }
namespace TR { class SymbolReference; }
namespace TR { class SymbolReferenceTable; }
Expand Down Expand Up @@ -1155,6 +1157,26 @@ class OMR_EXTENSIBLE Compilation
*/
void setCurrentILGenCallTarget(TR_CallTarget *x) { _currentILGenCallTarget = x; }

/**
* \brief
* Get the set of methods that will remain loaded while the JIT body
* resulting from this compilation is still running.
*
* \return the root retained method set for this compilation
*/
OMR::RetainedMethodSet *retainedMethods();

/**
* \brief
* Create a root OMR::RetainedMethodSet for the given method.
*
* This is used to initialize _retainedMethods. A project can override this
* to instantiate a subclass instead.
*
* \return the newly created root retained method set
*/
OMR::RetainedMethodSet *createRetainedMethods(TR_ResolvedMethod *method);

private:
void resetVisitCounts(vcount_t, TR::ResolvedMethodSymbol *);
int16_t restoreInlineDepthUntil(int32_t stopIndex, TR_ByteCodeInfo &currentInfo);
Expand Down Expand Up @@ -1364,6 +1386,8 @@ class OMR_EXTENSIBLE Compilation

TR_CallTarget *_currentILGenCallTarget;

OMR::RetainedMethodSet *_retainedMethods;

/*
* This must be last
* NOTE: TLS for Compilation needs to be set before any object that may use it is initialized.
Expand Down
7 changes: 7 additions & 0 deletions compiler/control/OMROptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ TR::OptionTable OMR::Options::_jitOptions[] = {
"aggressive recompilation mechanism",
TR::Options::setStaticNumeric, (intptr_t)&OMR::Options::_aggressiveRecompilationChances, 0, "F%d", NOT_IN_SUBSET},
{"aggressiveSwitchingToProfiling", "O\tAllow switching hot methods to profiling more aggressively", SET_OPTION_BIT(TR_AggressiveSwitchingToProfiling), "F" },
{"allowJitBodyToOutliveInlinedCode",
"I\tAllow JIT body to outlive inlined code. "
"This avoids tracking which code will remain loaded. "
"The generated code may not be correct once inlined code is actually unloaded.",
SET_OPTION_BIT(TR_AllowJitBodyToOutliveInlinedCode), "F" },
{"allowVPRangeNarrowingBasedOnDeclaredType",
"I\tallow value propagation to assume that integers declared "
"narrower than 32-bits (boolean, byte, char, short) are in-range",
Expand Down Expand Up @@ -646,6 +651,7 @@ TR::OptionTable OMR::Options::_jitOptions[] = {
{"dontIncreaseCountsForNonBootstrapMethods", "M\t", RESET_OPTION_BIT(TR_IncreaseCountsForNonBootstrapMethods), "F", NOT_IN_SUBSET }, // Xjit: option
{"dontInline=", "O{regex}\tlist of callee methods to not inline",
TR::Options::setRegex, offsetof(OMR::Options, _dontInline), 0, "P"},
{"dontInlineUnloadableMethods", "O\trefuse to inline methods that could be unloaded before the outermost method", SET_OPTION_BIT(TR_DontInlineUnloadableMethods), "F"},
{"dontJitIfSlotsSharedByRefAndNonRef", "O\tfail the compilation (in FSD mode) if a slot needs to be shared between an address and a nonaddress.", SET_OPTION_BIT(TR_DontJitIfSlotsSharedByRefAndNonRef), "F"},
{"dontLowerCountsForAotCold", "M\tDo not lower counts for cold aot runs", RESET_OPTION_BIT(TR_LowerCountsForAotCold), "F", NOT_IN_SUBSET },
{"dontRestrictInlinerDuringStartup", "O\tdo not restrict trivial inliner during startup", RESET_OPTION_BIT(TR_RestrictInlinerDuringStartup), "F", NOT_IN_SUBSET},
Expand Down Expand Up @@ -1277,6 +1283,7 @@ TR::OptionTable OMR::Options::_jitOptions[] = {
{"traceRelocatableDataDetailsCG", "L\ttrace relocation data details when generating relocatable code", SET_OPTION_BIT(TR_TraceRelocatableDataDetailsCG), "P"},
{"traceRematerialization", "L\ttrace rematerialization", TR::Options::traceOptimization, rematerialization, 0, "P"},
{"traceReorderArrayIndexExpr", "L\ttrace reorder array index expressions", TR::Options::traceOptimization, reorderArrayIndexExpr, 0, "P"},
{"traceRetainedMethods", "L\ttrace retained methods", SET_OPTION_BIT(TR_TraceRetainedMethods), "P" },
{"traceSamplingJProfiling", "L\ttrace samplingjProfiling", TR::Options::traceOptimization, samplingJProfiling, 0, "P"},
{"traceSEL", "L\ttrace sign extension load", TR::Options::traceOptimization, signExtendLoads, 0, "P"},
{"traceSequenceSimplification", "L\ttrace arithmetic sequence simplification", TR::Options::traceOptimization, expressionsSimplification, 0, "P"},
Expand Down
6 changes: 3 additions & 3 deletions compiler/control/OMROptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ enum TR_CompilationOptions
TR_DisableNewX86VolatileSupport = 0x04000000 + 10,
// Available = 0x08000000 + 10,
// Available = 0x10000000 + 10,
// Available = 0x20000000 + 10,
TR_AllowJitBodyToOutliveInlinedCode = 0x20000000 + 10,
// Available = 0x40000000 + 10,
TR_DisableDirectToJNIInline = 0x80000000 + 10,

Expand Down Expand Up @@ -658,7 +658,7 @@ enum TR_CompilationOptions
// Available = 0x00040000 + 19,
// Available = 0x00080000 + 19,
// Available = 0x00100000 + 19,
// Available = 0x00200000 + 19,
TR_TraceRetainedMethods = 0x00200000 + 19,
// Available = 0x00400000 + 19,
// Available = 0x00800000 + 19,
TR_EnableRATPurging = 0x01000000 + 19, // enable periodic RAT table purging
Expand Down Expand Up @@ -823,7 +823,7 @@ enum TR_CompilationOptions
TR_PerfTool = 0x00010000 + 25,
// Available = 0x00020000 + 25,
TR_DisableBranchOnCount = 0x00040000 + 25,
// Available = 0x00080000 + 25,
TR_DontInlineUnloadableMethods = 0x00080000 + 25,
TR_DisableLoopEntryAlignment = 0x00100000 + 25,
TR_EnableLoopEntryAlignment = 0x00200000 + 25,
TR_DisableLeafRoutineDetection = 0x00400000 + 25,
Expand Down
1 change: 1 addition & 0 deletions compiler/env/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ compiler_library(env
${CMAKE_CURRENT_LIST_DIR}/OMRArithEnv.cpp
${CMAKE_CURRENT_LIST_DIR}/OMRClassEnv.cpp
${CMAKE_CURRENT_LIST_DIR}/OMRDebugEnv.cpp
${CMAKE_CURRENT_LIST_DIR}/OMRRetainedMethodSet.cpp
${CMAKE_CURRENT_LIST_DIR}/OMRVMEnv.cpp
${CMAKE_CURRENT_LIST_DIR}/OMRVMMethodEnv.cpp
${CMAKE_CURRENT_LIST_DIR}/SegmentAllocator.cpp
Expand Down
Loading