Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid creation of non-faulting null-check nodes. #77078

Merged
merged 12 commits into from
Jan 28, 2023
Merged
6 changes: 4 additions & 2 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9940,8 +9940,8 @@ var_types Compiler::gtTypeForNullCheck(GenTree* tree)
// gtChangeOperToNullCheck: helper to change tree oper to a NULLCHECK.
//
// Arguments:
// tree - the node to change;
// basicBlock - basic block of the node.
// tree - the node to change;
// block - basic block of the node.
//
// Notes:
// the function should not be called after lowering for platforms that do not support
Expand All @@ -9953,6 +9953,8 @@ void Compiler::gtChangeOperToNullCheck(GenTree* tree, BasicBlock* block)
assert(tree->OperIs(GT_FIELD, GT_IND, GT_OBJ, GT_BLK));
tree->ChangeOper(GT_NULLCHECK);
tree->ChangeType(gtTypeForNullCheck(tree));
assert(fgAddrCouldBeNull(tree->gtGetOp1()));
tree->gtFlags |= GTF_EXCEPT;
block->bbFlags |= BBF_HAS_NULLCHECK;
optMethodFlags |= OMF_HAS_NULLCHECK;
}
Expand Down
14 changes: 12 additions & 2 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16050,8 +16050,18 @@ void Compiler::gtExtractSideEffList(GenTree* expr,
Append(node);
if (node->OperIsBlk() && !node->OperIsStoreBlk())
{
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
m_compiler->gtChangeOperToNullCheck(node, m_compiler->compCurBB);
// Check for a guaranteed non-faulting IND, and create a NOP node instead of a NULLCHECK in that
// case.
if (m_compiler->fgAddrCouldBeNull(node->AsBlk()->Addr()))
{
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
m_compiler->gtChangeOperToNullCheck(node, m_compiler->compCurBB);
}
else
{
JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NOTHING node\n", dspTreeID(node));
node = m_compiler->gtNewNothingNode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this does anything, since node is a local, and is unused after the subsequent return.

Does the Append(node); need to be moved below, just before the return?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Append(node) sets class variable GenTree* m_result, and node is used either (1) to be directly assigned to m_result or (2) to make comma tree that is subsequently assigned to m_result.
So, I think the code is correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that Append(node) here happens before you set node = m_compiler->gtNewNothingNode();. The NothingNode gets created then never used. And for the non-faulting case, the original BLK node is still on the side-effect list.

I think you want code more like this:

                if (m_compiler->gtNodeHasSideEffects(node, m_flags))
                {
                    if (node->OperIsBlk() && !node->OperIsStoreBlk())
                    {
                        // Check for a guaranteed non-faulting IND, and create a NOP node instead of a NULLCHECK in that case.
                        if (m_compiler->fgAddrCouldBeNull(node->AsBlk()->Addr()))
                        {
                            Append(node);
                            JITDUMP("Replace an unused OBJ/BLK node [%06d] with a NULLCHECK\n", dspTreeID(node));
                            m_compiler->gtChangeOperToNullCheck(node, m_compiler->compCurBB);
                        }
                        else
                        {
                            JITDUMP("Dropping non-faulting OBJ/BLK node [%06d]\n", dspTreeID(node));
                        }
                    }
                    else
                    {
                        Append(node);
                    }
                    return Compiler::WALK_SKIP_SUBTREES;
                }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
return Compiler::WALK_SKIP_SUBTREES;
}
Expand Down
9 changes: 8 additions & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8251,7 +8251,14 @@ void Compiler::impImportBlockCode(BasicBlock* block)
// via an underlying address, just null check the address.
if (op1->OperIs(GT_FIELD, GT_IND, GT_OBJ))
{
gtChangeOperToNullCheck(op1, block);
if (fgAddrCouldBeNull(op1->gtGetOp1()))
{
gtChangeOperToNullCheck(op1, block);
}
else
{
op1 = gtNewNothingNode();
}
}
else
{
Expand Down
22 changes: 17 additions & 5 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7279,13 +7279,16 @@ void Lowering::LowerIndir(GenTreeIndir* ind)
#if defined(TARGET_ARM64)
// Verify containment safety before creating an LEA that must be contained.
//
const bool isContainable = IsSafeToContainMem(ind, ind->Addr());
const bool isContainable = (ind->Addr() != nullptr) && IsSafeToContainMem(ind, ind->Addr());
JulieLeeMSFT marked this conversation as resolved.
Show resolved Hide resolved
#else
const bool isContainable = true;
#endif

TryCreateAddrMode(ind->Addr(), isContainable, ind);
ContainCheckIndir(ind);
if (!ind->OperIs(GT_NOP))
JulieLeeMSFT marked this conversation as resolved.
Show resolved Hide resolved
{
TryCreateAddrMode(ind->Addr(), isContainable, ind);
ContainCheckIndir(ind);
}

#ifdef TARGET_XARCH
if (ind->OperIs(GT_NULLCHECK) || ind->IsUnusedValue())
Expand Down Expand Up @@ -7333,14 +7336,23 @@ void Lowering::TransformUnusedIndirection(GenTreeIndir* ind, Compiler* comp, Bas
//
assert(ind->OperIs(GT_NULLCHECK, GT_IND, GT_BLK, GT_OBJ));

GenTree* const addr = ind->Addr();
if (!comp->fgAddrCouldBeNull(addr))
{
addr->SetUnusedValue();
ind->gtBashToNOP();
JITDUMP("bash an unused indir [%06u] to NOP.\n", comp->dspTreeID(ind));
return;
}

ind->ChangeType(comp->gtTypeForNullCheck(ind));

#if defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64)
bool useNullCheck = true;
#elif TARGET_ARM
#elif defined(TARGET_ARM)
bool useNullCheck = false;
#else // TARGET_XARCH
bool useNullCheck = !ind->Addr()->isContained();
bool useNullCheck = !addr->isContained();
ind->ClearDontExtend();
#endif // !TARGET_XARCH

Expand Down