From 3baef191ccefbf74bb1d7eab60a629fe350e21cf Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Wed, 9 Apr 2025 22:51:39 -0700 Subject: [PATCH 1/2] Force storing GC state until we see a labelled IG --- src/coreclr/jit/emit.cpp | 11 +++++++++++ src/coreclr/jit/emit.h | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/coreclr/jit/emit.cpp b/src/coreclr/jit/emit.cpp index e03ec89ac87777..0217608d874478 100644 --- a/src/coreclr/jit/emit.cpp +++ b/src/coreclr/jit/emit.cpp @@ -1007,8 +1007,14 @@ insGroup* emitter::emitSavIG(bool emitAdd) VarSetOps::Assign(emitComp, emitPrevGCrefVars, emitThisGCrefVars); emitPrevGCrefRegs = emitThisGCrefRegs; emitPrevByrefRegs = emitThisByrefRegs; + } + if (emitAddedLabel) + { + // Reset emitForceStoreGCState only after seeing label. It will keep + // marking IGs with IGF_GC_VARS flag until that. emitForceStoreGCState = false; + emitAddedLabel = false; } #ifdef DEBUG @@ -2839,6 +2845,11 @@ void* emitter::emitAddLabel(VARSET_VALARG_TP GCvars, regMaskTP gcrefRegs, regMas } } + if (emitForceStoreGCState) + { + emitAddedLabel = true; + } + /* Create a new IG if the current one is non-empty */ if (emitCurIGnonEmpty()) diff --git a/src/coreclr/jit/emit.h b/src/coreclr/jit/emit.h index ef1fd2f701fc15..e76de2316c2c09 100644 --- a/src/coreclr/jit/emit.h +++ b/src/coreclr/jit/emit.h @@ -2761,6 +2761,14 @@ class emitter bool emitForceStoreGCState; + // This flag is used together with `emitForceStoreGCState`. After we set + // emitForceStoreGCState = true, we will mark `emitAddedLabel` to true whenever + // we see a label IG. In emitSavIG, we will reset `emitForceStoreGCState = false` + // only after seeing `emitAddedLabel == true`. Until then, we will keep recording + // GC_VARS on the IGs. + + bool emitAddedLabel; + // emitThis* variables are used during emission, to track GC updates // on a per-instruction basis. During code generation, per-instruction // tracking is done with variables gcVarPtrSetCur, gcRegGCrefSetCur, From f2958eb087466a192402bce4652b256e2bbbd40a Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Thu, 10 Apr 2025 20:26:17 -0700 Subject: [PATCH 2/2] review feedback --- src/coreclr/jit/emit.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/coreclr/jit/emit.cpp b/src/coreclr/jit/emit.cpp index 0217608d874478..3a7a98a96041b0 100644 --- a/src/coreclr/jit/emit.cpp +++ b/src/coreclr/jit/emit.cpp @@ -1007,14 +1007,14 @@ insGroup* emitter::emitSavIG(bool emitAdd) VarSetOps::Assign(emitComp, emitPrevGCrefVars, emitThisGCrefVars); emitPrevGCrefRegs = emitThisGCrefRegs; emitPrevByrefRegs = emitThisByrefRegs; - } - if (emitAddedLabel) - { - // Reset emitForceStoreGCState only after seeing label. It will keep - // marking IGs with IGF_GC_VARS flag until that. - emitForceStoreGCState = false; - emitAddedLabel = false; + if (emitAddedLabel) + { + // Reset emitForceStoreGCState only after seeing label. It will keep + // marking IGs with IGF_GC_VARS flag until that. + emitForceStoreGCState = false; + emitAddedLabel = false; + } } #ifdef DEBUG @@ -1283,6 +1283,7 @@ void emitter::emitBegFN(bool hasFramePtr emitPrevByrefRegs = RBM_NONE; emitForceStoreGCState = false; + emitAddedLabel = false; #ifdef DEBUG @@ -2845,10 +2846,7 @@ void* emitter::emitAddLabel(VARSET_VALARG_TP GCvars, regMaskTP gcrefRegs, regMas } } - if (emitForceStoreGCState) - { - emitAddedLabel = true; - } + emitAddedLabel = true; /* Create a new IG if the current one is non-empty */