Skip to content

Commit 2af4408

Browse files
author
Sergey
committed
Fix for the fields.
1 parent 5cb7245 commit 2af4408

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/coreclr/jit/importer.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17284,9 +17284,10 @@ bool Compiler::impReturnInstruction(int prefixFlags, OPCODE& opcode)
1728417284
GenTree* addrChild = effectiveRetVal->gtGetOp1();
1728517285
if (addrChild->OperGet() == GT_LCL_VAR)
1728617286
{
17287-
LclVarDsc* varDsc = lvaGetDesc(addrChild->AsLclVarCommon());
17287+
GenTreeLclVar* lclVar = addrChild->AsLclVar();
17288+
LclVarDsc* varDsc = lvaGetDesc(lclVar);
1728817289

17289-
if (varTypeIsStruct(addrChild->TypeGet()) && !isOpaqueSIMDLclVar(varDsc))
17290+
if (varTypeIsStruct(lclVar->TypeGet()) && !isOpaqueSIMDLclVar(varDsc))
1729017291
{
1729117292
CORINFO_CLASS_HANDLE referentClassHandle;
1729217293
CorInfoType referentType =
@@ -17300,15 +17301,13 @@ bool Compiler::impReturnInstruction(int prefixFlags, OPCODE& opcode)
1730017301
// to struct2. struct1 and struct2 are different so we are "reinterpreting" the struct.
1730117302
// This may happen in, for example, System.Runtime.CompilerServices.Unsafe.As<TFrom,
1730217303
// TTo>.
17303-
// We need to mark the source struct variable as having overlapping fields because its
17304-
// fields may be accessed using field handles of a different type, which may confuse
17305-
// optimizations, in particular, value numbering.
17304+
// We need to exclude the local from VN because VN optimizations don't expect such
17305+
// transformations.
1730617306

17307-
JITDUMP("\nSetting lvOverlappingFields to true on V%02u because of struct "
17308-
"reinterpretation\n",
17309-
addrChild->AsLclVarCommon()->GetLclNum());
17307+
JITDUMP("Setting DontVN() on [%06u] because of struct reinterpretation.\n",
17308+
dspTreeID(lclVar));
1731017309

17311-
varDsc->lvOverlappingFields = true;
17310+
lclVar->SetDontVN();
1731217311
}
1731317312
}
1731417313
}

src/coreclr/jit/lclmorph.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
3939
unsigned m_lclNum;
4040
unsigned m_offset;
4141
bool m_address;
42+
bool m_excludeFromVN; // This value should not participate in VN optimizations,
43+
// for example, because it represents a reinterpreted struct.
44+
// When we transform it we should set `DontVN()` for `LclVar`
45+
// and `NotAField` for `LclFld`.
4246
INDEBUG(bool m_consumed;)
4347

4448
public:
@@ -49,6 +53,7 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
4953
, m_lclNum(BAD_VAR_NUM)
5054
, m_offset(0)
5155
, m_address(false)
56+
, m_excludeFromVN(false)
5257
#ifdef DEBUG
5358
, m_consumed(false)
5459
#endif // DEBUG
@@ -112,6 +117,10 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
112117
assert(!IsLocation() && !IsAddress());
113118

114119
m_lclNum = lclVar->GetLclNum();
120+
if (lclVar->DontVN())
121+
{
122+
m_excludeFromVN = true;
123+
}
115124

116125
assert(m_offset == 0);
117126
assert(m_fieldSeq == nullptr);
@@ -195,10 +204,11 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
195204

196205
if (val.IsLocation())
197206
{
198-
m_address = true;
199-
m_lclNum = val.m_lclNum;
200-
m_offset = val.m_offset;
201-
m_fieldSeq = val.m_fieldSeq;
207+
m_address = true;
208+
m_lclNum = val.m_lclNum;
209+
m_offset = val.m_offset;
210+
m_fieldSeq = val.m_fieldSeq;
211+
m_excludeFromVN = val.m_excludeFromVN;
202212
}
203213

204214
INDEBUG(val.Consume();)
@@ -246,7 +256,7 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
246256
m_lclNum = val.m_lclNum;
247257
m_offset = newOffset.Value();
248258

249-
if (field->gtFldMayOverlap)
259+
if (field->gtFldMayOverlap || val.m_excludeFromVN)
250260
{
251261
m_fieldSeq = FieldSeqStore::NotAField();
252262
}
@@ -287,9 +297,10 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
287297

288298
if (val.IsAddress())
289299
{
290-
m_lclNum = val.m_lclNum;
291-
m_offset = val.m_offset;
292-
m_fieldSeq = val.m_fieldSeq;
300+
m_lclNum = val.m_lclNum;
301+
m_offset = val.m_offset;
302+
m_fieldSeq = val.m_fieldSeq;
303+
m_excludeFromVN = val.m_excludeFromVN;
293304
}
294305

295306
INDEBUG(val.Consume();)

0 commit comments

Comments
 (0)