Skip to content

Remove BBF_TRY_BEG #93367

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

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/coreclr/jit/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,6 @@ void BasicBlock::dspFlags()
{
printf("failV ");
}
if (bbFlags & BBF_TRY_BEG)
{
printf("try ");
}
if (bbFlags & BBF_RUN_RARELY)
{
printf("rare ");
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ enum BasicBlockFlags : unsigned __int64
BBF_IMPORTED = MAKE_BBFLAG( 4), // BB byte-code has been imported
BBF_INTERNAL = MAKE_BBFLAG( 5), // BB has been added by the compiler
BBF_FAILED_VERIFICATION = MAKE_BBFLAG( 6), // BB has verification exception
BBF_TRY_BEG = MAKE_BBFLAG( 7), // BB starts a 'try' block
// BBF_UNUSED = MAKE_BBFLAG( 7),
BBF_FUNCLET_BEG = MAKE_BBFLAG( 8), // BB is the beginning of a funclet
BBF_CLONED_FINALLY_BEGIN = MAKE_BBFLAG( 9), // First block of a cloned finally region
BBF_CLONED_FINALLY_END = MAKE_BBFLAG(10), // Last block of a cloned finally region
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4260,7 +4260,7 @@ class Compiler

void impReimportMarkBlock(BasicBlock* block);

void impVerifyEHBlock(BasicBlock* block, bool isTryStart);
void impVerifyEHBlock(BasicBlock* block);

void impImportBlockPending(BasicBlock* block);

Expand Down
9 changes: 2 additions & 7 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3917,10 +3917,6 @@ void Compiler::fgFindBasicBlocks()
}
}

/* Mark the initial block and last blocks in the 'try' region */

tryBegBB->bbFlags |= BBF_TRY_BEG;

/* Prevent future optimizations of removing the first block */
/* of a TRY block and the first block of an exception handler */

Expand Down Expand Up @@ -4790,9 +4786,8 @@ BasicBlock* Compiler::fgSplitBlockAtEnd(BasicBlock* curr)
newBlock->bbFlags = curr->bbFlags;

// Remove flags that the new block can't have.
newBlock->bbFlags &=
~(BBF_TRY_BEG | BBF_LOOP_HEAD | BBF_LOOP_CALL0 | BBF_LOOP_CALL1 | BBF_FUNCLET_BEG | BBF_LOOP_PREHEADER |
BBF_KEEP_BBJ_ALWAYS | BBF_PATCHPOINT | BBF_BACKWARD_JUMP_TARGET | BBF_LOOP_ALIGN);
newBlock->bbFlags &= ~(BBF_LOOP_HEAD | BBF_LOOP_CALL0 | BBF_LOOP_CALL1 | BBF_FUNCLET_BEG | BBF_LOOP_PREHEADER |
BBF_KEEP_BBJ_ALWAYS | BBF_PATCHPOINT | BBF_BACKWARD_JUMP_TARGET | BBF_LOOP_ALIGN);

// Remove the GC safe bit on the new block. It seems clear that if we split 'curr' at the end,
// such that all the code is left in 'curr', and 'newBlock' just gets the control flow, then
Expand Down
12 changes: 7 additions & 5 deletions src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,15 @@ bool Compiler::fgDumpFlowGraph(Phases phase, PhasePosition pos)
if (displayBlockFlags)
{
// Don't display the `[` `]` unless we're going to display something.
const BasicBlockFlags allDisplayedBlockFlags = BBF_TRY_BEG | BBF_FUNCLET_BEG | BBF_RUN_RARELY |
BBF_LOOP_HEAD | BBF_LOOP_PREHEADER | BBF_LOOP_ALIGN;
if (block->bbFlags & allDisplayedBlockFlags)
const bool isTryEntryBlock = bbIsTryBeg(block);
const BasicBlockFlags allDisplayedBlockFlags =
BBF_FUNCLET_BEG | BBF_RUN_RARELY | BBF_LOOP_HEAD | BBF_LOOP_PREHEADER | BBF_LOOP_ALIGN;

if (isTryEntryBlock || ((block->bbFlags & allDisplayedBlockFlags) != 0))
{
// Display a very few, useful, block flags
fprintf(fgxFile, " [");
if (block->bbFlags & BBF_TRY_BEG)
if (isTryEntryBlock)
{
fprintf(fgxFile, "T");
}
Expand Down Expand Up @@ -2203,7 +2205,7 @@ void Compiler::fgTableDispBasicBlock(BasicBlock* block, int ibcColWidth /* = 0 *
/* brace matching editor workaround to compensate for the preceding line: } */
}

if (flags & BBF_TRY_BEG)
if (bbIsTryBeg(block))
{
// Output a brace for every try region that this block opens

Expand Down
11 changes: 1 addition & 10 deletions src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ PhaseStatus Compiler::fgRemoveEmptyFinally()
BasicBlock* const lastTryBlock = HBtab->ebdTryLast;
assert(firstTryBlock->getTryIndex() == XTnum);

assert((firstTryBlock->bbFlags & BBF_TRY_BEG) != 0);
firstTryBlock->bbFlags &= ~BBF_TRY_BEG;

for (BasicBlock* const block : Blocks(firstTryBlock, lastTryBlock))
{
// Look for blocks directly contained in this try, and
Expand Down Expand Up @@ -495,12 +492,6 @@ PhaseStatus Compiler::fgRemoveEmptyTry()
}
}

if (block == firstTryBlock)
{
assert((block->bbFlags & BBF_TRY_BEG) != 0);
block->bbFlags &= ~BBF_TRY_BEG;
}

if (block == lastTryBlock)
{
break;
Expand Down Expand Up @@ -2080,7 +2071,7 @@ PhaseStatus Compiler::fgTailMergeThrows()
// Workaround: don't consider try entry blocks as candidates
// for merging; if the canonical throw is later in the same try,
// we'll create invalid flow.
if ((block->bbFlags & BBF_TRY_BEG) != 0)
if (bbIsTryBeg(block))
{
continue;
}
Expand Down
15 changes: 7 additions & 8 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ PhaseStatus Compiler::fgPostImportationCleanup()
fgSetTryBeg(HBtab, newTryEntry);

// Try entry blocks get specially marked and have special protection.
HBtab->ebdTryBeg->bbFlags |= BBF_DONT_REMOVE | BBF_TRY_BEG;
HBtab->ebdTryBeg->bbFlags |= BBF_DONT_REMOVE;

// We are keeping this try region
removeTryRegion = false;
Expand Down Expand Up @@ -2050,8 +2050,8 @@ void Compiler::fgCompactBlocks(BasicBlock* block, BasicBlock* bNext)

// Make sure the second block is not the start of a TRY block or an exception handler

noway_assert(!bbIsTryBeg(bNext));
noway_assert(bNext->bbCatchTyp == BBCT_NONE);
noway_assert((bNext->bbFlags & BBF_TRY_BEG) == 0);
noway_assert((bNext->bbFlags & BBF_DONT_REMOVE) == 0);

/* both or none must have an exception handler */
Expand Down Expand Up @@ -6386,10 +6386,9 @@ bool Compiler::fgUpdateFlowGraph(bool doTailDuplication, bool isPhase)
goto REPEAT;
}

/* Remove unreachable or empty blocks - do not consider blocks marked BBF_DONT_REMOVE or genReturnBB block
* These include first and last block of a TRY, exception handlers and RANGE_CHECK_FAIL THROW blocks */

if ((block->bbFlags & BBF_DONT_REMOVE) == BBF_DONT_REMOVE || block == genReturnBB)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the genReturnBB check just unnecessary? (obsolete?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unnecessary; the block is already always marked with BBF_DONT_REMOVE.

// Remove unreachable or empty blocks - do not consider blocks marked BBF_DONT_REMOVE
// These include first and last block of a TRY, exception handlers and THROW blocks.
if ((block->bbFlags & BBF_DONT_REMOVE) != 0)
{
bPrev = block;
continue;
Expand All @@ -6407,8 +6406,8 @@ bool Compiler::fgUpdateFlowGraph(bool doTailDuplication, bool isPhase)
}
#endif // defined(FEATURE_EH_FUNCLETS) && defined(TARGET_ARM)

noway_assert(!block->bbCatchTyp);
noway_assert(!(block->bbFlags & BBF_TRY_BEG));
assert(!bbIsTryBeg(block));
noway_assert(block->bbCatchTyp == BBCT_NONE);

/* Remove unreachable blocks
*
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ void Compiler::fgAddSyncMethodEnterExit()
// EH regions in fgFindBasicBlocks(). Note that the try has no enclosing
// handler, and the fault has no enclosing try.

tryBegBB->bbFlags |= BBF_DONT_REMOVE | BBF_TRY_BEG | BBF_IMPORTED;
tryBegBB->bbFlags |= BBF_DONT_REMOVE | BBF_IMPORTED;

faultBB->bbFlags |= BBF_DONT_REMOVE | BBF_IMPORTED;
faultBB->bbCatchTyp = BBCT_FAULT;
Expand Down
72 changes: 8 additions & 64 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11043,30 +11043,17 @@ inline void Compiler::impReimportMarkBlock(BasicBlock* block)
block->bbFlags &= ~BBF_IMPORTED;
}

/*****************************************************************************
*
* Filter wrapper to handle only passed in exception code
* from it).
*/

void Compiler::impVerifyEHBlock(BasicBlock* block, bool isTryStart)
void Compiler::impVerifyEHBlock(BasicBlock* block)
{
assert(block->hasTryIndex());
assert(!compIsForInlining());

unsigned tryIndex = block->getTryIndex();
EHblkDsc* HBtab = ehGetDsc(tryIndex);

if (isTryStart)
if (bbIsTryBeg(block) && (block->bbStkDepth != 0))
{
assert(block->bbFlags & BBF_TRY_BEG);

// The Stack must be empty
//
if (block->bbStkDepth != 0)
{
BADCODE("Evaluation stack must be empty on entry into a try block");
}
BADCODE("Evaluation stack must be empty on entry into a try block");
}

// Save the stack contents, we'll need to restore it later
Expand Down Expand Up @@ -11121,12 +11108,6 @@ void Compiler::impVerifyEHBlock(BasicBlock* block, bool isTryStart)
// Process the filter block, if we haven't already done so.
if (HBtab->HasFilter())
{
/* @VERIFICATION : Ideally the end of filter state should get
propagated to the catch handler, this is an incompleteness,
but is not a security/compliance issue, since the only
interesting state is the 'thisInit' state.
*/

BasicBlock* filterBB = HBtab->ebdFilter;

if (((filterBB->bbFlags & BBF_IMPORTED) == 0) && (impGetPendingBlockMember(filterBB) == 0))
Expand Down Expand Up @@ -11203,51 +11184,14 @@ void Compiler::impImportBlock(BasicBlock* block)
/* Set the current stack state to the merged result */
verResetCurrentState(block, &verCurrentState);

/* Now walk the code and import the IL into GenTrees */

if (block->hasTryIndex())
{
/* @VERIFICATION : For now, the only state propagation from try
to it's handler is "thisInit" state (stack is empty at start of try).
In general, for state that we track in verification, we need to
model the possibility that an exception might happen at any IL
instruction, so we really need to merge all states that obtain
between IL instructions in a try block into the start states of
all handlers.

However we do not allow the 'this' pointer to be uninitialized when
entering most kinds try regions (only try/fault are allowed to have
an uninitialized this pointer on entry to the try)

Fortunately, the stack is thrown away when an exception
leads to a handler, so we don't have to worry about that.
We DO, however, have to worry about the "thisInit" state.
But only for the try/fault case.

The only allowed transition is from TIS_Uninit to TIS_Init.

So for a try/fault region for the fault handler block
we will merge the start state of the try begin
and the post-state of each block that is part of this try region
*/

// merge the start state of the try begin
//
if (block->bbFlags & BBF_TRY_BEG)
{
impVerifyEHBlock(block, true);
}

impImportBlockCode(block);

// As discussed above:
// merge the post-state of each block that is part of this try region
//
if (block->hasTryIndex())
{
impVerifyEHBlock(block, false);
}
impVerifyEHBlock(block);
}

// Now walk the code and import the IL into GenTrees.
impImportBlockCode(block);

if (compDonotInline())
{
return;
Expand Down
Loading