diff --git a/src/coreclr/jit/emit.cpp b/src/coreclr/jit/emit.cpp index e03ec89ac87777..3a7a98a96041b0 100644 --- a/src/coreclr/jit/emit.cpp +++ b/src/coreclr/jit/emit.cpp @@ -1008,7 +1008,13 @@ insGroup* emitter::emitSavIG(bool emitAdd) emitPrevGCrefRegs = emitThisGCrefRegs; emitPrevByrefRegs = emitThisByrefRegs; - emitForceStoreGCState = 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 @@ -1277,6 +1283,7 @@ void emitter::emitBegFN(bool hasFramePtr emitPrevByrefRegs = RBM_NONE; emitForceStoreGCState = false; + emitAddedLabel = false; #ifdef DEBUG @@ -2839,6 +2846,8 @@ void* emitter::emitAddLabel(VARSET_VALARG_TP GCvars, regMaskTP gcrefRegs, regMas } } + 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,