Skip to content

Commit

Permalink
JIT: refactor AddCodeDsc
Browse files Browse the repository at this point in the history
The main aim of this change is to remove the block reference from AddCodeDsc,
as it can become stale over time, making it impossible to determine where
a throw helper block should be placed, if we have subsequently eliminated
EH regions.

Instead we use the try/handler indices plus a bit of extra info to
track where the throw helper block should be placed.

I also moved the key formation logic nearby and reworked it a bit, and
gave the AddCodeDscs debug indices for dumps.

This should unblock doing late EH region removal.
  • Loading branch information
AndyAyersMS committed Sep 21, 2024
1 parent b14e2f5 commit 5ac85cd
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 121 deletions.
6 changes: 2 additions & 4 deletions src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,8 +1593,7 @@ void CodeGen::genJumpToThrowHlpBlk(emitJumpKind jumpKind, SpecialCodeKind codeKi
excpRaisingBlock = failBlk;

#ifdef DEBUG
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
assert(add->acdUsed);
assert(excpRaisingBlock == add->acdDstBlk);
#if !FEATURE_FIXED_OUT_ARGS
Expand All @@ -1605,8 +1604,7 @@ void CodeGen::genJumpToThrowHlpBlk(emitJumpKind jumpKind, SpecialCodeKind codeKi
else
{
// Find the helper-block which raises the exception.
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
PREFIX_ASSUME_MSG((add != nullptr), ("ERROR: failed to find exception throw block"));
assert(add->acdUsed);
excpRaisingBlock = add->acdDstBlk;
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/jit/codegenloongarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6841,8 +6841,7 @@ inline void CodeGen::genJumpToThrowHlpBlk_la(
excpRaisingBlock = failBlk;

#ifdef DEBUG
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
assert(add->acdUsed);
assert(excpRaisingBlock == add->acdDstBlk);
#if !FEATURE_FIXED_OUT_ARGS
Expand All @@ -6853,8 +6852,7 @@ inline void CodeGen::genJumpToThrowHlpBlk_la(
else
{
// Find the helper-block which raises the exception.
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
PREFIX_ASSUME_MSG((add != nullptr), ("ERROR: failed to find exception throw block"));
assert(add->acdUsed);
excpRaisingBlock = add->acdDstBlk;
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/jit/codegenriscv64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6871,8 +6871,7 @@ void CodeGen::genJumpToThrowHlpBlk_la(
excpRaisingBlock = failBlk;

#ifdef DEBUG
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
assert(add->acdUsed);
assert(excpRaisingBlock == add->acdDstBlk);
#if !FEATURE_FIXED_OUT_ARGS
Expand All @@ -6883,8 +6882,7 @@ void CodeGen::genJumpToThrowHlpBlk_la(
else
{
// Find the helper-block which raises the exception.
Compiler::AddCodeDsc* add =
compiler->fgFindExcptnTarget(codeKind, compiler->bbThrowIndex(compiler->compCurBB));
Compiler::AddCodeDsc* add = compiler->fgFindExcptnTarget(codeKind, compiler->compCurBB);
PREFIX_ASSUME_MSG((add != nullptr), ("ERROR: failed to find exception throw block"));
assert(add->acdUsed);
excpRaisingBlock = add->acdDstBlk;
Expand Down
34 changes: 28 additions & 6 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2897,8 +2897,6 @@ class Compiler
bool ehAnyFunclets(); // Are there any funclets in this function?
unsigned ehFuncletCount(); // Return the count of funclets in the function

unsigned bbThrowIndex(BasicBlock* blk); // Get the index to use as the cache key for sharing throw blocks

FlowEdge* BlockPredsWithEH(BasicBlock* blk);
FlowEdge* BlockDominancePreds(BasicBlock* blk);

Expand Down Expand Up @@ -6794,29 +6792,50 @@ class Compiler
// range checking or explicit calls to enable GC, and so on.
//
public:

enum class AcdKeyDesignator { KD_NONE, KD_TRY, KD_HND, KD_FLT };

struct AddCodeDsc
{
AddCodeDsc* acdNext;

// Initially the source block of the exception. After fgCreateThrowHelperBlocks, the block to which
// After fgCreateThrowHelperBlocks, the block to which
// we jump to raise the exception.
BasicBlock* acdDstBlk;

// EH region key used to look up this dsc in the map
unsigned acdData;

// EH regions for this dsc
unsigned short acdTryIndex;
unsigned short acdHndIndex;

// Which EH region forms the key?
AcdKeyDesignator acdKeyDsg;

SpecialCodeKind acdKind; // what kind of a special block is this?
bool acdUsed; // do we need to keep this helper block?

#if !FEATURE_FIXED_OUT_ARGS
bool acdStkLvlInit; // has acdStkLvl value been already set?
unsigned acdStkLvl; // stack level in stack slots.
#endif // !FEATURE_FIXED_OUT_ARGS

INDEBUG(unsigned acdNum);
};

unsigned acdCount = 0;

// Get the index to use as part of the AddCodeDsc key for sharing throw blocks
unsigned bbThrowIndex(BasicBlock* blk, AcdKeyDesignator* dsg);

struct AddCodeDscKey
{
public:
AddCodeDscKey(): acdKind(SCK_NONE), acdData(0) {}
AddCodeDscKey(SpecialCodeKind kind, unsigned data): acdKind(kind), acdData(data) {}

AddCodeDscKey(SpecialCodeKind kind, BasicBlock* block, Compiler* comp);
AddCodeDscKey(AddCodeDsc* add);

static bool Equals(const AddCodeDscKey& x, const AddCodeDscKey& y)
{
return (x.acdData == y.acdData) && (x.acdKind == y.acdKind);
Expand All @@ -6827,7 +6846,10 @@ class Compiler
return (x.acdData << 3) | (unsigned) x.acdKind;
}

unsigned Data() const { return acdData; }

private:

SpecialCodeKind acdKind;
unsigned acdData;
};
Expand All @@ -6848,7 +6870,7 @@ class Compiler


public:
AddCodeDsc* fgFindExcptnTarget(SpecialCodeKind kind, unsigned refData);
AddCodeDsc* fgFindExcptnTarget(SpecialCodeKind kind, BasicBlock* fromBlock);

bool fgUseThrowHelperBlocks();

Expand Down
Loading

0 comments on commit 5ac85cd

Please sign in to comment.