Merge pull request #14154 from Vectorized/optimize-multiplicity-map

Optimize multiplicity map
This commit is contained in:
Daniel 2023-05-08 14:54:05 +02:00 committed by GitHub
commit 34b13c1f89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -377,6 +377,50 @@ private:
}
};
/// A simple optimized map for mapping StackSlots to ints.
class Multiplicity
{
public:
int& operator[](StackSlot const& _slot)
{
if (auto* p = std::get_if<FunctionCallReturnLabelSlot>(&_slot))
return m_functionCallReturnLabelSlotMultiplicity[*p];
if (std::holds_alternative<FunctionReturnLabelSlot>(_slot))
return m_functionReturnLabelSlotMultiplicity;
if (auto* p = std::get_if<VariableSlot>(&_slot))
return m_variableSlotMultiplicity[*p];
if (auto* p = std::get_if<LiteralSlot>(&_slot))
return m_literalSlotMultiplicity[*p];
if (auto* p = std::get_if<TemporarySlot>(&_slot))
return m_temporarySlotMultiplicity[*p];
yulAssert(std::holds_alternative<JunkSlot>(_slot));
return m_junkSlotMultiplicity;
}
int at(StackSlot const& _slot) const
{
if (auto* p = std::get_if<FunctionCallReturnLabelSlot>(&_slot))
return m_functionCallReturnLabelSlotMultiplicity.at(*p);
if (std::holds_alternative<FunctionReturnLabelSlot>(_slot))
return m_functionReturnLabelSlotMultiplicity;
if (auto* p = std::get_if<VariableSlot>(&_slot))
return m_variableSlotMultiplicity.at(*p);
if (auto* p = std::get_if<LiteralSlot>(&_slot))
return m_literalSlotMultiplicity.at(*p);
if (auto* p = std::get_if<TemporarySlot>(&_slot))
return m_temporarySlotMultiplicity.at(*p);
yulAssert(std::holds_alternative<JunkSlot>(_slot));
return m_junkSlotMultiplicity;
}
private:
std::map<FunctionCallReturnLabelSlot, int> m_functionCallReturnLabelSlotMultiplicity;
int m_functionReturnLabelSlotMultiplicity = 0;
std::map<VariableSlot, int> m_variableSlotMultiplicity;
std::map<LiteralSlot, int> m_literalSlotMultiplicity;
std::map<TemporarySlot, int> m_temporarySlotMultiplicity;
int m_junkSlotMultiplicity = 0;
};
/// Transforms @a _currentStack to @a _targetStack, invoking the provided shuffling operations.
/// Modifies @a _currentStack itself after each invocation of the shuffling operations.
@ -395,7 +439,7 @@ void createStackLayout(Stack& _currentStack, Stack const& _targetStack, Swap _sw
Swap swapCallback;
PushOrDup pushOrDupCallback;
Pop popCallback;
std::map<StackSlot, int> multiplicity;
Multiplicity multiplicity;
ShuffleOperations(
Stack& _currentStack,
Stack const& _targetStack,

View File

@ -172,7 +172,7 @@ Stack createIdealLayout(Stack const& _operationOutput, Stack const& _post, Calla
vector<variant<PreviousSlot, StackSlot>>& layout;
Stack const& post;
std::set<StackSlot> outputs;
std::map<StackSlot, int> multiplicity;
Multiplicity multiplicity;
Callable generateSlotOnTheFly;
ShuffleOperations(
vector<variant<PreviousSlot, StackSlot>>& _layout,