[llvm-dev] Trying to use unordered_map

Krzysztof Parzyszek via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 30 12:27:57 PST 2020


Actually, even this would be wrong, because vector can reallocate storage.  If you want to look up the values in the vector, store indices in the map, not pointers.

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

-----Original Message-----
From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Krzysztof Parzyszek via llvm-dev
Sent: Monday, November 30, 2020 2:25 PM
To: llvm-dev <llvm-dev at lists.llvm.org>
Subject: [EXT] Re: [llvm-dev] Trying to use unordered_map

>  void addValue(const RecordVal &RV) {
>    Values.push_back(RV);
>    ValueMap[RV.getNameInit()] = &RV;

I think the last line should be
ValueMap[RV.getNameInit()] = &Values.back();

Are you trying to look them up in "Values"?

--
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: Monday, November 30, 2020 2:19 PM
To: llvm-dev at lists.llvm.org
Subject: [EXT] [llvm-dev] Trying to use unordered_map

Okay, I'm at a loss to understand what I'm doing wrong with the unordered_map class. Thank you for any help you can give.

I have two data structures:

  SmallVector<RecordVal, 0> Values;
  std::unordered_map<const Init *, const RecordVal *> ValueMap;

I just added the unordered_map to experiment with faster lookups for the fields in  TableGen records. The key is a StringInit and the corresponding value is a RecordVal. I made it a pointer to the RecordVal since the existing SmallVector holds the actual RecordVal instance.

Here is the code that adds a field to the structures:

  void addValue(const RecordVal &RV) {
    Values.push_back(RV);
    ValueMap[RV.getNameInit()] = &RV;

The RecordVal is push_backed onto the vector and inserted into the map by assignment.

Here is the code that looks up an entry. First the new code and then the old code.

  const RecordVal *getValue(const Init *Name) const {
    auto It = ValueMap.find(Name);
    if (It != ValueMap.end()) {
      return It->second;
    }
    return nullptr;

    for (const RecordVal &Val : Values)
      if (Val.Name == Name) {
        return &Val;
      }
    return nullptr;
  }

As far as I can tell, 'return It->second' is not returning the RecordVal. However, I'm thoroughly confused because I threw in a ton of prints to check it. Using It->Second I can print the name and value of the RecordVal and they are correct. I even printed the addresses of the name and value data structures from both It-Second and Val and they are the same. But when I print the address It-Second and  the address &Val, they are different.

(Indeed, I should use Visual Studio to look at this, but that is a lesson for another day.)

_______________________________________________
LLVM Developers mailing list
llvm-dev at lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
_______________________________________________
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