Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
growing hash
  • Loading branch information
davidwrighton committed Jul 28, 2020
commit 2cc30913b74dd6a4b6cf6ef3f89dcab594ddddc9
4 changes: 4 additions & 0 deletions src/coreclr/src/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6364,6 +6364,10 @@ class Compiler
};

static const size_t s_optCSEhashSize;
static const size_t s_optCSEhashGrowthFactor;
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be renamed to indicate that it is the initial hash table size

s_optCSEhashSize => s_optCSEhashSizeInitial

size_t optCSEhashSize; // Size of hashtable
size_t optCSEhashCount; // Number of entries in hashtable
size_t optCSEhashMaxCountBeforeResize; // Number of entries before resize
CSEdsc** optCSEhash;
CSEdsc** optCSEtab;

Expand Down
64 changes: 53 additions & 11 deletions src/coreclr/src/jit/optcse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/*****************************************************************************/

/* static */
const size_t Compiler::s_optCSEhashSize = EXPSET_SZ * 2;
const size_t Compiler::s_optCSEhashSize = EXPSET_SZ * 2;
const size_t Compiler::s_optCSEhashGrowthFactor = 2;

/*****************************************************************************
*
Expand All @@ -40,7 +41,7 @@ void Compiler::optCSEstop()

optCSEtab = new (this, CMK_CSE) CSEdsc*[optCSECandidateCount]();

for (cnt = s_optCSEhashSize, ptr = optCSEhash; cnt; cnt--, ptr++)
for (cnt = optCSEhashSize, ptr = optCSEhash; cnt; cnt--, ptr++)
{
for (dsc = *ptr; dsc; dsc = dsc->csdNextInBucket)
{
Expand Down Expand Up @@ -375,13 +376,31 @@ void Compiler::optValnumCSE_Init()
// Allocate and clear the hash bucket table
optCSEhash = new (this, CMK_CSE) CSEdsc*[s_optCSEhashSize]();

optCSEhashSize = s_optCSEhashSize;
optCSEhashMaxCountBeforeResize = optCSEhashSize * s_optCSEhashGrowthFactor;
optCSEhashCount = 0

optCSECandidateCount = 0;
optDoCSE = false; // Stays false until we find duplicate CSE tree

// optCseCheckedBoundMap is unused in most functions, allocated only when used
optCseCheckedBoundMap = nullptr;
}

unsigned optCSEKeyToHashIndex(size_t key, size_t optCSEhashSize)
{
unsigned hash;

hash = (unsigned)key;
#ifdef TARGET_64BIT
hash ^= (unsigned)(key >> 32);
#endif
hash *= (unsigned)(optCSEhashSize + 1);
hash >>= 7;

return hash % optCSEhashSize;
}

//---------------------------------------------------------------------------
// optValnumCSE_Index:
// - Returns the CSE index to use for this tree,
Expand All @@ -402,7 +421,6 @@ void Compiler::optValnumCSE_Init()
unsigned Compiler::optValnumCSE_Index(GenTree* tree, Statement* stmt)
{
size_t key;
unsigned hash;
unsigned hval;
CSEdsc* hashDsc;
bool isIntConstHash = false;
Expand Down Expand Up @@ -502,14 +520,7 @@ unsigned Compiler::optValnumCSE_Index(GenTree* tree, Statement* stmt)

// Compute the hash value for the expression

hash = (unsigned)key;
#ifdef TARGET_64BIT
hash ^= (unsigned)(key >> 32);
#endif
hash *= (unsigned)(s_optCSEhashSize + 1);
hash >>= 7;

hval = hash % s_optCSEhashSize;
hval = optCSEKeyToHashIndex(key, optCSEhashSize);

/* Look for a matching index in the hash table */

Expand Down Expand Up @@ -627,6 +638,37 @@ unsigned Compiler::optValnumCSE_Index(GenTree* tree, Statement* stmt)

if (optCSECandidateCount < MAX_CSE_CNT)
{
if (optCSEhashCount == optCSEhashMaxCountBeforeResize)
{
size_t newOptCSEhashSize = optCSEhashSize * s_optCSEhashGrowthFactor;
CSEdsc** newOptCSEhash = new (this, CMK_CSE) CSEdsc*[new_optCSEhashSize]();

// Iterate through each existing entry, moving to the new table
CSEdsc** ptr;
CSEdsc* dsc;
size_t cnt;
for (cnt = optCSEhashSize, ptr = optCSEhash; cnt; cnt--, ptr++)
{
for (dsc = *ptr; dsc;)
{
CSEdsc* nextDsc = dsc->csdNextInBucket;

size_t newHval = optCSEKeyToHashIndex(dsc->csdHashKey, newOptCSEhashSize);

// Move CSEdsc to bucket in enlarged table
dsc->csdNextInBucket = newOptCSEhash[newHval];
newOptCSEhash[newHval] = dsc;

dsc = nextDsc;
}
}

optCSEhash = newOptCSEhash;
optCSEhashSize = newOptCSEhashSize;
optCSEhashMaxCountBeforeResize = optCSEhashMaxCountBeforeResize * s_optCSEhashGrowthFactor;
}

++optCSEhashCount;
hashDsc = new (this, CMK_CSE) CSEdsc;

hashDsc->csdHashKey = key;
Expand Down