Skip to content

Commit

Permalink
ConstProp: don't pool with a hash table
Browse files Browse the repository at this point in the history
hash tables have high overhead! but individual blocks are small, so the
quadratic search is _much_ faster in practice. knocks 8% off node.

Signed-off-by: Alyssa Rosenzweig <[email protected]>
  • Loading branch information
alyssarosenzweig committed Oct 2, 2024
1 parent 7c91357 commit 3cc627a
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions FEXCore/Source/Interface/IR/Passes/ConstProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class ConstProp final : public FEXCore::IR::Pass {
void HandleConstantPools(IREmitter* IREmit, const IRListView& CurrentIR);
void ConstantPropagation(IREmitter* IREmit, const IRListView& CurrentIR, Ref CodeNode, IROp_Header* IROp);

fextl::unordered_map<uint64_t, Ref> ConstPool;

bool SupportsTSOImm9 {};
const FEXCore::CPUIDEmu* CPUID;

Expand Down Expand Up @@ -144,23 +142,42 @@ void ConstProp::HandleConstantPools(IREmitter* IREmit, const IRListView& Current
// don't have constants leftover after all inlining.
fextl::vector<Ref> Remap {};

struct Entry {
int64_t Value;
Ref R;
};


fextl::vector<Entry> Pool {};

for (auto [BlockNode, BlockIROp] : CurrentIR.GetBlocks()) {
Pool.clear();

for (auto [CodeNode, IROp] : CurrentIR.GetCode(BlockNode)) {
if (IROp->Op == OP_CONSTANT) {
auto Op = IROp->C<IR::IROp_Constant>();
auto it = ConstPool.find(Op->Constant);

if (it != ConstPool.end()) {
uint32_t Value = CurrentIR.GetID(CodeNode).Value;
LOGMAN_THROW_A_FMT(Value < SSACount, "def not yet remapped");

if (Remap.empty()) {
Remap.resize(SSACount, NULL);
bool Found = false;

// Search for the constant. This is O(n^2) but n is small since it's
// local and most constants are inlined. In practice, it ends up much
// faster than a hash table.
for (auto K : Pool) {
if (K.Value == Op->Constant) {
uint32_t Value = CurrentIR.GetID(CodeNode).Value;
LOGMAN_THROW_A_FMT(Value < SSACount, "def not yet remapped");

if (Remap.empty()) {
Remap.resize(SSACount, NULL);
}

Remap[Value] = K.R;
Found = true;
break;
}
}

Remap[Value] = it->second;
} else {
ConstPool[Op->Constant] = CodeNode;
if (!Found) {
Pool.push_back({.Value = Op->Constant, .R = CodeNode});
}
} else if (!Remap.empty()) {
const uint8_t NumArgs = IR::GetArgs(IROp->Op);
Expand All @@ -179,7 +196,6 @@ void ConstProp::HandleConstantPools(IREmitter* IREmit, const IRListView& Current
}
}
}
ConstPool.clear();
}
}

Expand Down

0 comments on commit 3cc627a

Please sign in to comment.