Skip to content

Commit

Permalink
Deduplicate the struct type
Browse files Browse the repository at this point in the history
This commit deduplicates IRDifferentialPairUserCodeType types by
ignoring its witness table vale when caching them.
  • Loading branch information
jkwak-work committed Feb 14, 2025
1 parent 96e2bbc commit 5665625
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion source/slang/slang-ir-autodiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,33 @@ struct DifferentialPairTypeBuilder
};

// Cache from pair types to lowered type.
Dictionary<IRInst*, IRInst*> pairTypeCache;
//
// When we cache `IRDifferentialPairUserCodeType`, we should ignore the second operand which is witness table.
// If we don't, we will end up emiting duplicated types whose members are identical.
//
struct PairCacheKey
{
IRInst* inst;
int depth = 0;

PairCacheKey(IRInst* i)
{
while (auto userCode = as<IRDifferentialPairUserCodeType>(i))
{
i = userCode->getOperand(0);
depth++;
}
inst = i;
}

HashCode getHashCode() const { return combineHash(HashCode(inst), HashCode(depth)); }

bool operator==(const PairCacheKey& rhs) const
{
return inst == rhs.inst && depth == rhs.depth;
}
};
Dictionary<PairCacheKey, IRInst*> pairTypeCache;

// Cache from existential pair types to their lowered interface keys.
// We use a different cache because an interface type can have
Expand Down

0 comments on commit 5665625

Please sign in to comment.