Home > Software design >  a code piece from MLIR that make me confused
a code piece from MLIR that make me confused

Time:09-27

There is a code piece from the llvm-project/mlir/lib/IR/Dialect.cpp

void DialectRegistry::insert(TypeID typeID, StringRef name,
                             const DialectAllocatorFunction &ctor) {
  auto inserted = registry.insert(
      std::make_pair(std::string(name), std::make_pair(typeID, ctor)));
  if (!inserted.second && inserted.first->second.first != typeID) {
    llvm::report_fatal_error(
        "Trying to register different dialects for the same namespace: "  
        name);
  }
}

The type of registry is std::map<std::string, std::pair<TypeID, DialectAllocatorFunction>>, which can be find in llvm-project/mlir/lib/IR/DialectRegistry.h. Thus the type of inserted should be something like pair<map<std::string(name), std::make_pair(typeID, ctor)>::iterator, bool>, then inserted.first should be of map<std::string(name), std::make_pair(typeID, ctor)> type, and then inserted.first->second should be of std::make_pair(typeID, ctor) type, and finally inserted.first->second.first should have the same type of typeID, thus inserted.first->second.first can be compared with typeID in the if clause.

My question is that, isn't the typeID in the std::make_pair(typeID, ctor) expression assigned by the function parameter TypeID typeID? If it is, and the typeID in the if clause should also be assigned by the function parameter TypeID typeID, then in what condition can the two typeIDs be different? Actually I do encounter with the fatal error cause by the two typeIDs do not equal.

CodePudding user response:

Taking a closer look at what the first entry of the return value of std::map::insert is:

1-3) Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.

(Emphasis added.)

If the insertion fails because an element was already present, inserted.first will be an iterator pointing to the value that prevented insertion. That is, if registry already has an entry for name, inserted.first will be an iterator pointing to a pair with the same name, but a possibly different pair<TypeId, DialectAllocatorFunction> than the arguments passed to DialectRegistry::insert

  • Related