Skip to content

Commit

Permalink
Fix BOX import to not create "wrong" casts (dotnet#57094)
Browse files Browse the repository at this point in the history
All casts in the IR have TYP_INT, except for this one
case. Fix it by using genActualType.

Also add a comment explaining the reason a cast is needed
at all in case the types are small.
  • Loading branch information
SingleAccretion authored Aug 25, 2021
1 parent f97c18b commit e74bc2a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6855,18 +6855,26 @@ void Compiler::impImportAndPushBox(CORINFO_RESOLVED_TOKEN* pResolvedToken)
{
lclTyp = JITtype2varType(jitType);
}
assert(genActualType(exprToBox->TypeGet()) == genActualType(lclTyp) ||
varTypeIsFloating(lclTyp) == varTypeIsFloating(exprToBox->TypeGet()));

var_types srcTyp = exprToBox->TypeGet();
var_types dstTyp = lclTyp;

// We allow float <-> double mismatches and implicit truncation for small types.
assert((genActualType(srcTyp) == genActualType(dstTyp)) ||
(varTypeIsFloating(srcTyp) == varTypeIsFloating(dstTyp)));

// Note regarding small types.
// We are going to store to the box here via an indirection, so the cast added below is
// redundant, since the store has an implicit truncation semantic. The reason we still
// add this cast is so that the code which deals with GT_BOX optimizations does not have
// to account for this implicit truncation (e. g. understand that BOX<byte>(0xFF + 1) is
// actually BOX<byte>(0) or deal with signedness mismatch and other GT_CAST complexities).
if (srcTyp != dstTyp)
{
assert((varTypeIsFloating(srcTyp) && varTypeIsFloating(dstTyp)) ||
(varTypeIsIntegral(srcTyp) && varTypeIsIntegral(dstTyp)));
exprToBox = gtNewCastNode(dstTyp, exprToBox, false, dstTyp);
exprToBox = gtNewCastNode(genActualType(dstTyp), exprToBox, false, dstTyp);
}
op1 = gtNewAssignNode(gtNewOperNode(GT_IND, lclTyp, op1), exprToBox);

op1 = gtNewAssignNode(gtNewOperNode(GT_IND, dstTyp, op1), exprToBox);
}

// Spill eval stack to flush out any pending side effects.
Expand Down

0 comments on commit e74bc2a

Please sign in to comment.