[clang] [Driver] Add ExclusiveGroup feature to multilib.yaml. (PR #69447)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 26 21:20:53 PDT 2023


================
@@ -96,13 +97,39 @@ bool MultilibSet::select(const Multilib::flags_list &Flags,
                          llvm::SmallVector<Multilib> &Selected) const {
   llvm::StringSet<> FlagSet(expandFlags(Flags));
   Selected.clear();
-  llvm::copy_if(Multilibs, std::back_inserter(Selected),
-                [&FlagSet](const Multilib &M) {
-                  for (const std::string &F : M.flags())
-                    if (!FlagSet.contains(F))
-                      return false;
-                  return true;
-                });
+
+  // Decide which multilibs we're going to select at all
+  std::vector<bool> IsSelected(Multilibs.size(), false);
+  std::map<std::string, size_t> ExclusiveGroupMembers;
+  for (size_t i = 0, e = Multilibs.size(); i < e; ++i) {
+    const Multilib &M = Multilibs[i];
+
+    // If this multilib doesn't match all our flags, don't select it
+    if (!llvm::all_of(M.flags(), [&FlagSet](const std::string &F) {
+          return FlagSet.contains(F);
+        }))
+      continue;
+
+    // If this multilib has the same ExclusiveGroup as one we've already
+    // selected, de-select the previous one
+    const std::string &group = M.exclusiveGroup();
+    if (!group.empty()) {
----------------
MaskRay wrote:

The hash table entry is looked up twice, and can be simplified to once.
```
if (!group.empty()) {
  auto [It, Inserted] = ExclusiveGroupMembers.try_emplace(...);
  if (!Inserted) {
    IsSelected[it->second] = false;
    it->second = i;
  }
}
```

Actually, I'd iterate over the array in the reverse order and add an element if its `ExclusiveGroup` hasn't been seen. Then reverse `Selected`

https://github.com/llvm/llvm-project/pull/69447


More information about the cfe-commits mailing list