Skip to content
Merged
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
155 changes: 153 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 @@ -272,6 +273,7 @@ OMR::Compilation::Compilation(int32_t id, OMR_VMThread *omrVMThread, TR_FrontEnd
, _bitVectorPool(self())
, _typeLayoutMap((LayoutComparator()), LayoutAllocator(self()->region()))
, _currentILGenCallTarget(NULL)
, _retainedMethods(NULL)
, _tlsManager(*self())
{
if (target != NULL) {
Expand Down Expand Up @@ -818,6 +820,41 @@ 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");

const char *bondMethodsTraceNote = comp->bondMethodsTraceNote();
if (bondMethodsTraceNote != NULL) {
traceMsg(comp, " note: %s\n", bondMethodsTraceNote);
}

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()
{
if (!self()->getOption(TR_DisableSupportForCpuSpentInCompilation))
Expand Down Expand Up @@ -988,6 +1025,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 @@ -1142,8 +1181,65 @@ void OMR::Compilation::performOptimizations()
{
_optimizer = TR::Optimizer::createOptimizer(self(), self()->getJittedMethodSymbol(), false);

if (_optimizer)
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.

uint32_t numInlinedSites = self()->getNumInlinedCallSites();
if (numInlinedSites != 0) {
OMR::RetainedMethodSet *root = self()->retainedMethods()->withKeepalivesAttested();

TR::vector<OMR::RetainedMethodSet *, TR::Region &> retainedMethods(
self()->trMemory()->currentStackRegion());

retainedMethods.reserve(numInlinedSites);
retainedMethods.resize(numInlinedSites, NULL);

for (uint32_t i = 0; i < numInlinedSites; 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.
//
TR_ByteCodeInfo bci = self()->getInlinedCallSite(i)._byteCodeInfo;
if (trace) {
traceMsg(self(), "check inlined site %u, bci=%d:%d\n", i, bci.getCallerIndex(),
bci.getByteCodeIndex());
}

int32_t caller = bci.getCallerIndex();
OMR::RetainedMethodSet *parent = caller == -1 ? root : retainedMethods[caller];

retainedMethods[i] = parent->withLinkedCalleeAttested(bci);

TR_ResolvedMethod *m = self()->getInlinedResolvedMethod(i);
TR_ASSERT_FATAL(retainedMethods[i]->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());
}
}

if (trace) {
traceMsg(self(), "\n");
}
}
}
}

bool OMR::Compilation::incInlineDepth(TR::ResolvedMethodSymbol *method, TR::Node *callNode, bool directCall,
Expand Down Expand Up @@ -2074,6 +2170,45 @@ TR_AOTMethodInfo *OMR::Compilation::getInlinedAOTMethodInfo(uint32_t index)
return _inlinedCallSites[index].aotMethodInfo();
}

TR_ResolvedMethod *OMR::Compilation::getInlinedCallSiteRefinedMethod(uint32_t i)
{
return _inlinedCallSites[i].refinedMethod();
}

void OMR::Compilation::setCurrentCallSiteRefinedMethod(TR_ResolvedMethod *m)
{
TR_ASSERT_FATAL(!_inlinedCallStack.isEmpty(), "no current inlined site");
_inlinedCallSites[_inlinedCallStack.top()].setRefinedMethod(m);
}

bool OMR::Compilation::didInlinedSiteGenerateKeepalive(uint32_t i) { return _inlinedCallSites[i].generatedKeepalive(); }

void OMR::Compilation::setCurrentInlinedSiteGeneratedKeepalive(TR_ResolvedMethod *keepaliveMethod)
{
TR_ASSERT_FATAL(keepaliveMethod != NULL, "no keepalive method");
TR_ASSERT_FATAL(!_inlinedCallStack.isEmpty(), "no current inlined site");
TR_InlinedCallSiteInfo &siteInfo = _inlinedCallSites[_inlinedCallStack.top()];
TR_ASSERT_FATAL(keepaliveMethod->isSameMethod(siteInfo.refinedMethod()),
"keepalive method %p differs from call site refined method %p", keepaliveMethod->getNonPersistentIdentifier(),
siteInfo.refinedMethod()->getNonPersistentIdentifier());

siteInfo.setGeneratedKeepalive();
}

bool OMR::Compilation::didInlinedSiteGenerateBond(uint32_t i) { return _inlinedCallSites[i].generatedBond(); }

void OMR::Compilation::setCurrentInlinedSiteGeneratedBond(TR_ResolvedMethod *bondMethod)
{
TR_ASSERT_FATAL(bondMethod != NULL, "no bond method");
TR_ASSERT_FATAL(!_inlinedCallStack.isEmpty(), "no current inlined site");
TR_InlinedCallSiteInfo &siteInfo = _inlinedCallSites[_inlinedCallStack.top()];
TR_ASSERT_FATAL(bondMethod->isSameMethod(siteInfo.resolvedMethod()),
"bond method %p differs from inlined resolved method %p", bondMethod->getNonPersistentIdentifier(),
siteInfo.resolvedMethod()->getNonPersistentIdentifier());

siteInfo.setGeneratedBond();
}

TR_InlinedCallSite *OMR::Compilation::getCurrentInlinedCallSite()
{
return _inlinedCallStack.isEmpty() ? 0 : &_inlinedCallSites[_inlinedCallStack.top()].site();
Expand Down Expand Up @@ -2403,3 +2538,19 @@ TR::Block *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);
}
113 changes: 112 additions & 1 deletion compiler/compile/OMRCompilation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ 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 All @@ -91,8 +92,10 @@ class RegisterCandidates;
class TR_ResolvedMethod;

namespace OMR {
class RetainedMethodSet;
class RuntimeAssumption;
}
} // namespace OMR

class TR_VirtualGuard;
class TR_VirtualGuardSite;
struct TR_VirtualGuardSelection;
Expand Down Expand Up @@ -703,7 +706,10 @@ class OMR_EXTENSIBLE Compilation {
int32_t *_osrCallSiteRematTable;
bool _directCall;
bool _cannotAttemptOSRDuring;
bool _generatedKeepalive;
bool _generatedBond;
TR_AOTMethodInfo *_aotMethodInfo;
TR_ResolvedMethod *_refinedMethod;

public:
TR_InlinedCallSiteInfo(TR_OpaqueMethodBlock *method, TR_ByteCodeInfo &bcInfo,
Expand All @@ -714,7 +720,10 @@ class OMR_EXTENSIBLE Compilation {
, _directCall(directCall)
, _osrCallSiteRematTable(0)
, _cannotAttemptOSRDuring(false)
, _generatedKeepalive(false)
, _generatedBond(false)
, _aotMethodInfo(aotMethodInfo)
, _refinedMethod(NULL)
{
_site._methodInfo = method;
_site._byteCodeInfo = bcInfo;
Expand All @@ -739,6 +748,18 @@ class OMR_EXTENSIBLE Compilation {
void setCannotAttemptOSRDuring(bool cannotOSR) { _cannotAttemptOSRDuring = cannotOSR; }

TR_AOTMethodInfo *aotMethodInfo() { return _aotMethodInfo; }

TR_ResolvedMethod *refinedMethod() { return _refinedMethod; }

void setRefinedMethod(TR_ResolvedMethod *m) { _refinedMethod = m; }

bool generatedKeepalive() { return _generatedKeepalive; }

void setGeneratedKeepalive() { _generatedKeepalive = true; }

bool generatedBond() { return _generatedBond; }

void setGeneratedBond() { _generatedBond = true; }
};

uint32_t getNumInlinedCallSites();
Expand All @@ -756,6 +777,65 @@ class OMR_EXTENSIBLE Compilation {
void setCannotAttemptOSRDuring(uint32_t index, bool cannot);
TR_AOTMethodInfo *getInlinedAOTMethodInfo(uint32_t index);

/**
* \brief Get the call site method refined based on constant folding for
* inlined site \p i, if any.
*
* If a keepalive was created for the inlined site, it was for this method.
*
* \param i the inlined site index
* \return the refined method, or null if the call site was not refined
* based on a known object.
*/
TR_ResolvedMethod *getInlinedCallSiteRefinedMethod(uint32_t i);

/**
* \brief Set the call site method refined based on constant folding for
* the current inlined site.
*
* \param m the method
*/
void setCurrentCallSiteRefinedMethod(TR_ResolvedMethod *m);

/**
* \brief Determine whether a keepalive was generated for inlined site \p i.
*
* If a keepalive was generated, it was for getInlinedCallSiteRefinedMethod(i).
*
* \param i the inlined site index
* \return true if a keepalive was generated, false otherwise
*/
bool didInlinedSiteGenerateKeepalive(uint32_t i);

/**
* \brief Mark the current inlined site as having generated a keepalive.
*
* The current inlined site must have a call site refined method, and it
* must be the same as the keepalive method.
*
* \param keepaliveMethod the keepalive method, used to check consistency
*/
void setCurrentInlinedSiteGeneratedKeepalive(TR_ResolvedMethod *keepaliveMethod);

/**
* \brief Determine whether a bond was generated for inlined site \p i.
*
* If a bond was generated, it was for the inlined target method.
*
* \param i the inlined site index
* \return true if a bond was generated, false otherwise
*/
bool didInlinedSiteGenerateBond(uint32_t i);

/**
* \brief Mark the current inlined site as having generated a bond.
*
* The bond method must be the target method that was inlined at this site.
*
* \param bondMethod the bond method, used to check consistency
*/
void setCurrentInlinedSiteGeneratedBond(TR_ResolvedMethod *bondMethod);

TR_InlinedCallSite *getCurrentInlinedCallSite();
int32_t getCurrentInlinedSiteIndex();
TR_PrexArgInfo *getCurrentInlinedCallArgInfo();
Expand Down Expand Up @@ -1250,6 +1330,35 @@ 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);

/**
* \brief Get the extra note, if any, to add when tracing bond methods.
*
* A project can override this to add some context to the trace log.
*
* \return the note message, or null if none.
*/
const char *bondMethodsTraceNote() { return NULL; }

private:
void resetVisitCounts(vcount_t, TR::ResolvedMethodSymbol *);
int16_t restoreInlineDepthUntil(int32_t stopIndex, TR_ByteCodeInfo &currentInfo);
Expand Down Expand Up @@ -1460,6 +1569,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
Loading