[llvm-dev] Trying to use unordered_map

Krzysztof Parzyszek via llvm-dev llvm-dev at lists.llvm.org
Tue Dec 1 08:19:18 PST 2020


MapVector has begin/end that return the range of the embedded vector.  You can create ArrayRef (or a copy of the vector) from that range.  Would that work for you?

--
Krzysztof Parzyszek  kparzysz at quicinc.com   AI tools development

-----Original Message-----
From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Paul C. Anagnostopoulos via llvm-dev
Sent: Tuesday, December 1, 2020 9:51 AM
To: David Blaikie <dblaikie at gmail.com>
Cc: llvm-dev at lists.llvm.org
Subject: [EXT] Re: [llvm-dev] Trying to use unordered_map

MapVector is exactly what I want, except for one feature. Is there a reason why it does not have a function to return an ArrayRef to the vector, so I can iterate over it without using the class's iterators? It does have a function takeVector() that clears the map and moves the vector.

I realize that modifying the returned vector would cause problems, but is that a reason not to have the function?

I'm asking because TableGen has a function to return the vector of record values so that the caller can iterate over them. This function is used both in TableGen itself and in backends.


At 11/30/2020 03:35 PM, David Blaikie wrote:
>Yep, as Krzysztof mentioned - pointer invalidation when inserting elements into a SmallVector (std::vector has similar guarantees here). Adding new elements can require a reallocation - moving all the objects over into the new allocation, so any pointers pointing to the original elements would be invalid. (though, yeah, before you get to that point - you already have dangling references because you're pointing to the RV temporary, uinstead of the copy of RV in the vector)
>
>You could use indexes as Krzysztof mentioned - other options include using non-invalidating data structures (like std::list or std::deque) or indirection (a SmallVector of unique_ptr<RecordVal> - so that pointers remain stable/valid) (though both those solutions involve extra memory/allocation overhead) or putting the RV in the map and pointing to it from the vector (unordered_map has pointer stability through insertion and removal - though llvm's DenseMap does not have such a guarantee). Or maybe llvm's MapVector would abstract you from the complexities of all these options?

_______________________________________________
LLVM Developers mailing list
llvm-dev at lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list