Skip to content

Commit

Permalink
Merge pull request #20721 from IBMJimmyk/CAE-option
Browse files Browse the repository at this point in the history
Make disable CAS and CAE options consistant
  • Loading branch information
zl-wang authored Jan 14, 2025
2 parents 51f7079 + 1e3b2e5 commit 30f423a
Show file tree
Hide file tree
Showing 10 changed files with 183 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 @@ -6883,7 +6883,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 @@ -6977,8 +6978,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 @@ -6991,8 +6995,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 @@ -7004,8 +7011,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 @@ -7014,7 +7024,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 @@ -7027,7 +7037,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 @@ -7049,7 +7059,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 @@ -542,6 +542,26 @@ void addMonClass(TR::Node* monNode, TR_OpaqueClassBlock* clazz);
*/
void setSupportsInlineMath_MaxMin_FD() { _j9Flags.set(SupportsInlineMath_MaxMin_FD); }

/** \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 @@ -710,6 +730,8 @@ void addMonClass(TR::Node* monNode, TR_OpaqueClassBlock* clazz);
SupportsInlineStringCodingHasNegatives = 0x00004000,
SupportsInlineStringCodingCountPositives = 0x00008000,
SupportsInlineMath_MaxMin_FD = 0x00010000,
SupportsInlineUnsafeCompareAndSet = 0x00020000,
SupportsInlineUnsafeCompareAndExchange = 0x00040000,
};

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 @@ -11900,7 +11900,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 @@ -12189,8 +12190,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 @@ -12203,13 +12207,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 @@ -12220,15 +12230,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 @@ -12239,7 +12252,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 @@ -12259,7 +12272,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 30f423a

Please sign in to comment.