From ac2341c3cee452d394b23148ce7357309d098af8 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Fri, 10 Feb 2023 14:37:12 -0800 Subject: [PATCH] JIT: add option to use interlocked add for PGO edge count updates (#81934) Mainly intended for use in determining what is leading to some consistency issues with our current edge profiles. We might consider enabling this in some of our static PGO collections. Enabled via JitInterlockedProfile=1. --- src/coreclr/jit/fgprofile.cpp | 34 +++++++++++++++++++++++++--------- src/coreclr/jit/jitconfigvalues.h | 1 + 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/coreclr/jit/fgprofile.cpp b/src/coreclr/jit/fgprofile.cpp index f411bb3..370dacc 100644 --- a/src/coreclr/jit/fgprofile.cpp +++ b/src/coreclr/jit/fgprofile.cpp @@ -1725,18 +1725,34 @@ void EfficientEdgeCountInstrumentor::Instrument(BasicBlock* block, Schema& schem var_types typ = entry.InstrumentationKind == ICorJitInfo::PgoInstrumentationKind::EdgeIntCount ? TYP_INT : TYP_LONG; - // Read Basic-Block count value - GenTree* valueNode = - m_comp->gtNewIndOfIconHandleNode(typ, addrOfCurrentExecutionCount, GTF_ICON_BBC_PTR, false); - // Increment value by 1 - GenTree* rhsNode = m_comp->gtNewOperNode(GT_ADD, typ, valueNode, m_comp->gtNewIconNode(1, typ)); + if (JitConfig.JitInterlockedProfiling() > 0) + { + // Form counter address + GenTree* addressNode = m_comp->gtNewIconHandleNode(addrOfCurrentExecutionCount, GTF_ICON_BBC_PTR); + + // Interlocked increment + GenTree* xAddNode = m_comp->gtNewOperNode(GT_XADD, typ, addressNode, m_comp->gtNewIconNode(1, typ)); + + // Ignore result. + m_comp->fgNewStmtAtBeg(instrumentedBlock, xAddNode); + } + else + { + // Read Basic-Block count value + GenTree* valueNode = + m_comp->gtNewIndOfIconHandleNode(typ, addrOfCurrentExecutionCount, GTF_ICON_BBC_PTR, false); - // Write new Basic-Block count value - GenTree* lhsNode = m_comp->gtNewIndOfIconHandleNode(typ, addrOfCurrentExecutionCount, GTF_ICON_BBC_PTR, false); - GenTree* asgNode = m_comp->gtNewAssignNode(lhsNode, rhsNode); + // Increment value by 1 + GenTree* rhsNode = m_comp->gtNewOperNode(GT_ADD, typ, valueNode, m_comp->gtNewIconNode(1, typ)); - m_comp->fgNewStmtAtBeg(instrumentedBlock, asgNode); + // Write new Basic-Block count value + GenTree* lhsNode = + m_comp->gtNewIndOfIconHandleNode(typ, addrOfCurrentExecutionCount, GTF_ICON_BBC_PTR, false); + GenTree* asgNode = m_comp->gtNewAssignNode(lhsNode, rhsNode); + + m_comp->fgNewStmtAtBeg(instrumentedBlock, asgNode); + } if (probe->kind != EdgeKind::Duplicate) { diff --git a/src/coreclr/jit/jitconfigvalues.h b/src/coreclr/jit/jitconfigvalues.h index 14d5620..58619de 100644 --- a/src/coreclr/jit/jitconfigvalues.h +++ b/src/coreclr/jit/jitconfigvalues.h @@ -568,6 +568,7 @@ CONFIG_STRING(JitEnablePatchpointRange, W("JitEnablePatchpointRange")) #endif // Profile instrumentation options +CONFIG_INTEGER(JitInterlockedProfiling, W("JitInterlockedProfiling"), 0) CONFIG_INTEGER(JitMinimalJitProfiling, W("JitMinimalJitProfiling"), 1) CONFIG_INTEGER(JitMinimalPrejitProfiling, W("JitMinimalPrejitProfiling"), 0) -- 2.7.4