Skip to content

Commit 7adf916

Browse files
author
git apple-llvm automerger
committed
Merge commit 'c4306e2c79c2' from apple/master into swift/master-next
2 parents 8e6bd71 + c4306e2 commit 7adf916

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

llvm/include/llvm/Support/Automaton.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,13 @@ template <typename ActionT> class Automaton {
161161
/// FIXME: This uses a std::map because ActionT can be a pair type including
162162
/// an enum. In particular DenseMapInfo<ActionT> must be defined to use
163163
/// DenseMap here.
164-
std::map<std::pair<uint64_t, ActionT>, std::pair<uint64_t, unsigned>> M;
164+
/// This is a shared_ptr to allow very quick copy-construction of Automata; this
165+
/// state is immutable after construction so this is safe.
166+
using MapTy = std::map<std::pair<uint64_t, ActionT>, std::pair<uint64_t, unsigned>>;
167+
std::shared_ptr<MapTy> M;
165168
/// An optional transcription object. This uses much more state than simply
166169
/// traversing the DFA for acceptance, so is heap allocated.
167-
std::unique_ptr<internal::NfaTranscriber> Transcriber;
170+
std::shared_ptr<internal::NfaTranscriber> Transcriber;
168171
/// The initial DFA state is 1.
169172
uint64_t State = 1;
170173
/// True if we should transcribe and false if not (even if Transcriber is defined).
@@ -187,13 +190,15 @@ template <typename ActionT> class Automaton {
187190
ArrayRef<NfaStatePair> TranscriptionTable = {}) {
188191
if (!TranscriptionTable.empty())
189192
Transcriber =
190-
std::make_unique<internal::NfaTranscriber>(TranscriptionTable);
193+
std::make_shared<internal::NfaTranscriber>(TranscriptionTable);
191194
Transcribe = Transcriber != nullptr;
195+
M = std::make_shared<MapTy>();
192196
for (const auto &I : Transitions)
193197
// Greedily read and cache the transition table.
194-
M.emplace(std::make_pair(I.FromDfaState, I.Action),
195-
std::make_pair(I.ToDfaState, I.InfoIdx));
198+
M->emplace(std::make_pair(I.FromDfaState, I.Action),
199+
std::make_pair(I.ToDfaState, I.InfoIdx));
196200
}
201+
Automaton(const Automaton &) = default;
197202

198203
/// Reset the automaton to its initial state.
199204
void reset() {
@@ -218,8 +223,8 @@ template <typename ActionT> class Automaton {
218223
/// If this function returns false, all methods are undefined until reset() is
219224
/// called.
220225
bool add(const ActionT &A) {
221-
auto I = M.find({State, A});
222-
if (I == M.end())
226+
auto I = M->find({State, A});
227+
if (I == M->end())
223228
return false;
224229
if (Transcriber && Transcribe)
225230
Transcriber->transition(I->second.second);
@@ -229,8 +234,8 @@ template <typename ActionT> class Automaton {
229234

230235
/// Return true if the automaton can be transitioned based on input symbol A.
231236
bool canAdd(const ActionT &A) {
232-
auto I = M.find({State, A});
233-
return I != M.end();
237+
auto I = M->find({State, A});
238+
return I != M->end();
234239
}
235240

236241
/// Obtain a set of possible paths through the input nondeterministic

llvm/utils/TableGen/DFAPacketizerEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,10 @@ void DFAPacketizerEmitter::emitForItineraries(
538538
OS << "DFAPacketizer *" << SubTargetClassName << "::"
539539
<< "create" << DFAName
540540
<< "DFAPacketizer(const InstrItineraryData *IID) const {\n"
541-
<< " Automaton<uint64_t> A(ArrayRef<" << TargetAndDFAName
541+
<< " static Automaton<uint64_t> A(ArrayRef<" << TargetAndDFAName
542542
<< "Transition>(" << TargetAndDFAName << "Transitions), "
543543
<< TargetAndDFAName << "TransitionInfo);\n"
544-
<< " return new DFAPacketizer(IID, std::move(A));\n"
544+
<< " return new DFAPacketizer(IID, A);\n"
545545
<< "\n}\n\n";
546546
}
547547

0 commit comments

Comments
 (0)