Skip to content

Commit

Permalink
Make disable CAS and CAE options consistant
Browse files Browse the repository at this point in the history
Changes TR_DisableCASInlining envvar to disable inlining Unsafe
compareAndSwap on all platforms. This used to only work on X and would
also disable inlining compareAndExchange at the same time. Now it only
disables inlining compareAndSwap.

Renames TR_DisableCAEIntrinsic envvar to TR_DisableCAEIntrinsic and it
disables inlining Unsafe compareAndExchange on all platforms.

Envvars are now checked in fewer locations and set an option bit
instead. The following methods were added to support this:
getSupportsInlineUnsafeCompareAndSet
setSupportsInlineUnsafeCompareAndSet
getSupportsInlineUnsafeCompareAndExchange
setSupportsInlineUnsafeCompareAndExchange

These methods are used to indicate support for inlining CAS/CAE and
also to query for suppport.

Signed-off-by: jimmyk <[email protected]>
  • Loading branch information
IBMJimmyk committed Dec 2, 2024
1 parent 735ab88 commit 0e98eec
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 72 deletions.
12 changes: 12 additions & 0 deletions runtime/compiler/aarch64/codegen/J9CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ J9::ARM64::CodeGenerator::initialize()
}
if (comp->fej9()->hasFixedFrameC_CallingConvention())
cg->setHasFixedFrameC_CallingConvention();

static bool disableCASInlining = feGetEnv("TR_DisableCASInlining") != NULL;
if (!disableCASInlining)
{
cg->setSupportsInlineUnsafeCompareAndSet();
}

static bool disableCAEInlining = feGetEnv("TR_DisableCAEInlining") != NULL;
if (!disableCAEInlining)
{
cg->setSupportsInlineUnsafeCompareAndExchange();
}
}

TR::Linkage *
Expand Down
30 changes: 20 additions & 10 deletions runtime/compiler/aarch64/codegen/J9TreeEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6863,7 +6863,8 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
break;
}

static bool disableCAEIntrinsic = feGetEnv("TR_DisableCAEIntrinsic") != NULL;
bool disableCASInlining = !cg->getSupportsInlineUnsafeCompareAndSet();
bool disableCAEInlining = !cg->getSupportsInlineUnsafeCompareAndExchange();
switch (methodSymbol->getRecognizedMethod())
{
case TR::java_lang_Thread_onSpinWait:
Expand Down Expand Up @@ -6957,8 +6958,11 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSwap(node, cg, false);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSwap(node, cg, false);
return true;
}
}
break;
}
Expand All @@ -6971,8 +6975,11 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSwap(node, cg, true);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSwap(node, cg, true);
return true;
}
}
break;
}
Expand All @@ -6984,8 +6991,11 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSwapObject(node, cg);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSwapObject(node, cg);
return true;
}
}
break;
}
Expand All @@ -6994,7 +7004,7 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
{
if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSwap(node, cg, false, true);
return true;
Expand All @@ -7007,7 +7017,7 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
{
if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSwap(node, cg, true, true);
return true;
Expand All @@ -7029,7 +7039,7 @@ J9::ARM64::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
{
if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSwapObject(node, cg, true);
return true;
Expand Down
25 changes: 13 additions & 12 deletions runtime/compiler/codegen/J9CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,31 +650,32 @@ J9::CodeGenerator::lowerTreesPreChildrenVisit(TR::Node *parent, TR::TreeTop *tre

if (parent->getOpCode().isFunctionCall())
{
// J9
//
/* Hiding compressedref logic from CodeGen isn't a good practice, and the evaluator still needs the uncompressedref node for write barriers.
/* J9
*
* Hiding compressedref logic from CodeGen isn't a good practice, and the evaluator still needs the uncompressedref node for write barriers.
* Therefore, this part is deprecated. It can only be activated on X, P or Z with the TR_UseOldCompareAndSwapObject envvar.
*
* If TR_DisableCAEIntrinsic is set to disable inlining of compareAndExchange, compressedref logic will not be hidden for compareAndExchange
* calls even if TR_UseOldCompareAndSwapObject is set. The reason is that TR_DisableCAEIntrinsic takes priority over TR_UseOldCompareAndSwapObject
* If TR_DisableCAEInlining is set to disable inlining of compareAndExchange, compressedref logic will not be hidden for compareAndExchange
* calls even if TR_UseOldCompareAndSwapObject is set. The reason is that TR_DisableCAEInlining takes priority over TR_UseOldCompareAndSwapObject
* so neither the old nor new version of the inlined compareAndExchange are used and the non-inlined version expects that the compressedrefs are
* not hidden.
*
* Similarly, TR_DisableCASInlining (which is only supported on X) can be used to disable inlining on both compareAndSwap and compareAndExchange.
* This also takes priority over TR_UseOldCompareAndSwapObject. Once again, the compressedrefs logic will not be hidden since it is expected by
* the non-inlined version.
* Similarly, TR_DisableCASInlining can be used to disable inlining of compareAndSet. This also takes priority over TR_UseOldCompareAndSwapObject.
* Once again, the compressedrefs logic will not be hidden since it is expected by the non-inlined version.
*/
static bool useOldCompareAndSwapObject = (bool)feGetEnv("TR_UseOldCompareAndSwapObject");
static bool disableCASInlining = feGetEnv("TR_DisableCASInlining") != NULL;
if (((self()->comp()->target().cpu.isX86() && !disableCASInlining) || self()->comp()->target().cpu.isPower() || self()->comp()->target().cpu.isZ()) &&
if ((self()->comp()->target().cpu.isX86() || self()->comp()->target().cpu.isPower() || self()->comp()->target().cpu.isZ()) &&
self()->comp()->useCompressedPointers() && useOldCompareAndSwapObject)
{
TR::MethodSymbol *methodSymbol = parent->getSymbol()->castToMethodSymbol();
static bool disableCAEIntrinsic = feGetEnv("TR_DisableCAEIntrinsic") != NULL;

bool disableCASInlining = !self()->getSupportsInlineUnsafeCompareAndSet();
bool disableCAEIntrinsic = !self()->getSupportsInlineUnsafeCompareAndExchange();

// In Java9 Unsafe could be the jdk.internal JNI method or the sun.misc ordinary method wrapper,
// while in Java8 it can only be the sun.misc package which will itself contain the JNI method.
// Test for isNative to distinguish between them.
if (((methodSymbol->getRecognizedMethod() == TR::sun_misc_Unsafe_compareAndSwapObject_jlObjectJjlObjectjlObject_Z) ||
if ((((methodSymbol->getRecognizedMethod() == TR::sun_misc_Unsafe_compareAndSwapObject_jlObjectJjlObjectjlObject_Z) && !disableCASInlining) ||
((methodSymbol->getRecognizedMethod() == TR::jdk_internal_misc_Unsafe_compareAndExchangeObject) && !disableCAEIntrinsic) ||
((methodSymbol->getRecognizedMethod() == TR::jdk_internal_misc_Unsafe_compareAndExchangeReference) && !disableCAEIntrinsic)) &&
methodSymbol->isNative() &&
Expand Down
22 changes: 22 additions & 0 deletions runtime/compiler/codegen/J9CodeGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,26 @@ void addMonClass(TR::Node* monNode, TR_OpaqueClassBlock* clazz);
*/
void setSupportsInlineVectorizedHashCode() { _j9Flags.set(SupportsInlineVectorizedHashCode); }

/** \brief
* Determines whether the code generator supports inlining of jdk/internal/misc/Unsafe.CompareAndSet[Object|Reference|Int|Long]
*/
bool getSupportsInlineUnsafeCompareAndSet() { return _j9Flags.testAny(SupportsInlineUnsafeCompareAndSet); }

/** \brief
* The code generator supports inlining of jdk/internal/misc/Unsafe.CompareAndSet[Object|Reference|Int|Long]
*/
void setSupportsInlineUnsafeCompareAndSet() { _j9Flags.set(SupportsInlineUnsafeCompareAndSet); }

/** \brief
* Determines whether the code generator supports inlining of jdk/internal/misc/Unsafe.CompareAndExchange[Object|Reference|Int|Long]
*/
bool getSupportsInlineUnsafeCompareAndExchange() { return _j9Flags.testAny(SupportsInlineUnsafeCompareAndExchange); }

/** \brief
* The code generator supports inlining of jdk/internal/misc/Unsafe.CompareAndExchange[Object|Reference|Int|Long]
*/
void setSupportsInlineUnsafeCompareAndExchange() { _j9Flags.set(SupportsInlineUnsafeCompareAndExchange); }

/**
* \brief
* The number of nodes between a monext and the next monent before
Expand Down Expand Up @@ -688,6 +708,8 @@ void addMonClass(TR::Node* monNode, TR_OpaqueClassBlock* clazz);
SupportsInlineVectorizedMismatch = 0x00001000,
SupportsInlineVectorizedHashCode = 0x00002000,
SupportsInlineStringCodingHasNegatives = 0x00004000,
SupportsInlineUnsafeCompareAndSet = 0x00008000,
SupportsInlineUnsafeCompareAndExchange = 0x00010000,
};

flags32_t _j9Flags;
Expand Down
16 changes: 10 additions & 6 deletions runtime/compiler/optimizer/InlinerTempForJ9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,8 @@ TR_J9InlinerPolicy::inlineUnsafeCall(TR::ResolvedMethodSymbol *calleeSymbol, TR:
!comp()->fej9()->traceableMethodsCanBeInlined()))
return false;

static bool disableCAEIntrinsic = feGetEnv("TR_DisableCAEIntrinsic") != NULL;
bool disableCASInlining = !comp()->cg()->getSupportsInlineUnsafeCompareAndSet();
bool disableCAEInlining = !comp()->cg()->getSupportsInlineUnsafeCompareAndExchange();
// I am not sure if having the same type between C/S and B/Z matters here.. ie. if the type is being used as the only distinguishing factor
switch (callNode->getSymbol()->castToResolvedMethodSymbol()->getRecognizedMethod())
{
Expand Down Expand Up @@ -2705,18 +2706,21 @@ TR_J9InlinerPolicy::inlineUnsafeCall(TR::ResolvedMethodSymbol *calleeSymbol, TR:
case TR::jdk_internal_misc_Unsafe_compareAndExchangeLong:
case TR::jdk_internal_misc_Unsafe_compareAndExchangeObject:
case TR::jdk_internal_misc_Unsafe_compareAndExchangeReference:
if (disableCAEIntrinsic)
if (disableCAEInlining || callNode->isSafeForCGToFastPathUnsafeCall())
{
break;
return false;
}
// Fallthrough if previous if condition is not met.
return createUnsafeCASCallDiamond(callNodeTreeTop, callNode);

case TR::sun_misc_Unsafe_compareAndSwapInt_jlObjectJII_Z:
case TR::sun_misc_Unsafe_compareAndSwapLong_jlObjectJJJ_Z:
case TR::sun_misc_Unsafe_compareAndSwapObject_jlObjectJjlObjectjlObject_Z:
if (callNode->isSafeForCGToFastPathUnsafeCall())
if (disableCASInlining || callNode->isSafeForCGToFastPathUnsafeCall())
{
return false;
}
#if defined(J9VM_GC_SPARSE_HEAP_ALLOCATION)
if(TR::Compiler->om.isOffHeapAllocationEnabled())
if (TR::Compiler->om.isOffHeapAllocationEnabled())
return createUnsafeCASCallDiamond(callNodeTreeTop, callNode);
#endif /* J9VM_GC_SPARSE_HEAP_ALLOCATION */
switch (callerSymbol->castToMethodSymbol()->getRecognizedMethod())
Expand Down
12 changes: 12 additions & 0 deletions runtime/compiler/p/codegen/J9CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ J9::Power::CodeGenerator::initialize()
!disableStringInflateIntrinsic)
cg->setSupportsInlineStringLatin1Inflate();

static bool disableCASInlining = feGetEnv("TR_DisableCASInlining") != NULL;
if (!disableCASInlining)
{
cg->setSupportsInlineUnsafeCompareAndSet();
}

static bool disableCAEInlining = feGetEnv("TR_DisableCAEInlining") != NULL;
if (!disableCAEInlining)
{
cg->setSupportsInlineUnsafeCompareAndExchange();
}

if (!comp->getOption(TR_DisableReadMonitors))
cg->setSupportsReadOnlyLocks();

Expand Down
37 changes: 25 additions & 12 deletions runtime/compiler/p/codegen/J9TreeEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11793,7 +11793,8 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
}
else if (methodSymbol)
{
static bool disableCAEIntrinsic = feGetEnv("TR_DisableCAEIntrinsic") != NULL;
bool disableCASInlining = !cg->getSupportsInlineUnsafeCompareAndSet();
bool disableCAEInlining = !cg->getSupportsInlineUnsafeCompareAndExchange();
switch (methodSymbol->getRecognizedMethod())
{
case TR::java_util_concurrent_ConcurrentLinkedQueue_tmOffer:
Expand Down Expand Up @@ -12073,8 +12074,11 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 4, false);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 4, false);
return true;
}
}
break;

Expand All @@ -12087,13 +12091,19 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if (comp->target().is64Bit() && (node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 8, false);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 8, false);
return true;
}
}
else if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = inlineAtomicOperation(node, cg, methodSymbol);
return true;
if (!disableCASInlining)
{
resultReg = inlineAtomicOperation(node, cg, methodSymbol);
return true;
}
}
break;

Expand All @@ -12104,15 +12114,18 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result

if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
resultReg = VMinlineCompareAndSetOrExchangeReference(node, cg, false);
return true;
if (!disableCASInlining)
{
resultReg = VMinlineCompareAndSetOrExchangeReference(node, cg, false);
return true;
}
}
break;

case TR::jdk_internal_misc_Unsafe_compareAndExchangeInt:
if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 4, true);
return true;
Expand All @@ -12123,7 +12136,7 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
case TR::jdk_internal_misc_Unsafe_compareAndExchangeLong:
if (comp->target().is64Bit() && (node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSetOrExchange(node, cg, 8, true);
return true;
Expand All @@ -12143,7 +12156,7 @@ J9::Power::CodeGenerator::inlineDirectCall(TR::Node *node, TR::Register *&result
case TR::jdk_internal_misc_Unsafe_compareAndExchangeReference:
if ((node->isUnsafeGetPutCASCallOnNonArray() || !TR::Compiler->om.canGenerateArraylets()) && node->isSafeForCGToFastPathUnsafeCall())
{
if (!disableCAEIntrinsic)
if (!disableCAEInlining)
{
resultReg = VMinlineCompareAndSetOrExchangeReference(node, cg, true);
return true;
Expand Down
Loading

0 comments on commit 0e98eec

Please sign in to comment.