Skip to content

[ADT] Use brace initialization in Set/Map (NFC) #155182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kazutakahirata
Copy link
Contributor

With brace initialization, we get to avoid type decay and enjoy
guaranteed copy elision as part of C++17.

With brace initialization, we get to avoid type decay and enjoy
guaranteed copy elision as part of C++17.
@llvmbot
Copy link
Member

llvmbot commented Aug 24, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

With brace initialization, we get to avoid type decay and enjoy
guaranteed copy elision as part of C++17.


Full diff: https://github.com/llvm/llvm-project/pull/155182.diff

5 Files Affected:

  • (modified) llvm/include/llvm/ADT/DenseMap.h (+6-9)
  • (modified) llvm/include/llvm/ADT/MapVector.h (+4-4)
  • (modified) llvm/include/llvm/ADT/SmallPtrSet.h (+3-3)
  • (modified) llvm/include/llvm/ADT/SparseSet.h (+2-2)
  • (modified) llvm/include/llvm/ADT/StringMap.h (+3-3)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 643dee98f0e28..ab1bc6356dcb9 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -257,13 +257,12 @@ class DenseMapBase : public DebugEpochBase {
   std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
     BucketT *TheBucket;
     if (LookupBucketFor(Key, TheBucket))
-      return std::make_pair(makeInsertIterator(TheBucket),
-                            false); // Already in map.
+      return {makeInsertIterator(TheBucket), false}; // Already in map.
 
     // Otherwise, insert the new element.
     TheBucket =
         InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
-    return std::make_pair(makeInsertIterator(TheBucket), true);
+    return {makeInsertIterator(TheBucket), true};
   }
 
   // Inserts key,value pair into the map if the key isn't already in the map.
@@ -273,12 +272,11 @@ class DenseMapBase : public DebugEpochBase {
   std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
     BucketT *TheBucket;
     if (LookupBucketFor(Key, TheBucket))
-      return std::make_pair(makeInsertIterator(TheBucket),
-                            false); // Already in map.
+      return {makeInsertIterator(TheBucket), false}; // Already in map.
 
     // Otherwise, insert the new element.
     TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
-    return std::make_pair(makeInsertIterator(TheBucket), true);
+    return {makeInsertIterator(TheBucket), true};
   }
 
   /// Alternate version of insert() which allows a different, and possibly
@@ -291,13 +289,12 @@ class DenseMapBase : public DebugEpochBase {
                                       const LookupKeyT &Val) {
     BucketT *TheBucket;
     if (LookupBucketFor(Val, TheBucket))
-      return std::make_pair(makeInsertIterator(TheBucket),
-                            false); // Already in map.
+      return {makeInsertIterator(TheBucket), false}; // Already in map.
 
     // Otherwise, insert the new element.
     TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
                                            std::move(KV.second), Val);
-    return std::make_pair(makeInsertIterator(TheBucket), true);
+    return {makeInsertIterator(TheBucket), true};
   }
 
   /// insert - Range insertion of pairs.
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 015605fd98dff..24976535716d1 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -123,9 +123,9 @@ class MapVector {
       It->second = Vector.size();
       Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
                           std::forward_as_tuple(std::forward<Ts>(Args)...));
-      return std::make_pair(std::prev(end()), true);
+      return {std::prev(end()), true};
     }
-    return std::make_pair(begin() + It->second, false);
+    return {begin() + It->second, false};
   }
   template <typename... Ts>
   std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
@@ -135,9 +135,9 @@ class MapVector {
       Vector.emplace_back(std::piecewise_construct,
                           std::forward_as_tuple(std::move(Key)),
                           std::forward_as_tuple(std::forward<Ts>(Args)...));
-      return std::make_pair(std::prev(end()), true);
+      return {std::prev(end()), true};
     }
-    return std::make_pair(begin() + It->second, false);
+    return {begin() + It->second, false};
   }
 
   std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index f55627c6866f7..16ad3973e054d 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -172,14 +172,14 @@ class SmallPtrSetImplBase : public DebugEpochBase {
       // Check to see if it is already in the set.
       for (const void *&Bucket : small_buckets()) {
         if (Bucket == Ptr)
-          return std::make_pair(&Bucket, false);
+          return {&Bucket, false};
       }
 
       // Nope, there isn't.  If we stay small, just 'pushback' now.
       if (NumEntries < CurArraySize) {
         CurArray[NumEntries++] = Ptr;
         incrementEpoch();
-        return std::make_pair(CurArray + (NumEntries - 1), true);
+        return {CurArray + (NumEntries - 1), true};
       }
       // Otherwise, hit the big set case, which will call grow.
     }
@@ -400,7 +400,7 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
   /// the element equal to Ptr.
   std::pair<iterator, bool> insert(PtrType Ptr) {
     auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr));
-    return std::make_pair(makeIterator(p.first), p.second);
+    return {makeIterator(p.first), p.second};
   }
 
   /// Insert the given pointer with an iterator hint that is ignored. This is
diff --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h
index d9ded9875d377..a8ebc9f786486 100644
--- a/llvm/include/llvm/ADT/SparseSet.h
+++ b/llvm/include/llvm/ADT/SparseSet.h
@@ -258,10 +258,10 @@ class SparseSet {
     unsigned Idx = ValIndexOf(Val);
     iterator I = findIndex(Idx);
     if (I != end())
-      return std::make_pair(I, false);
+      return {I, false};
     Sparse[Idx] = size();
     Dense.push_back(Val);
-    return std::make_pair(end() - 1, true);
+    return {end() - 1, true};
   }
 
   /// array subscript - If an element already exists with this key, return it.
diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
index 0bf062f988f3a..dbc0485967ac6 100644
--- a/llvm/include/llvm/ADT/StringMap.h
+++ b/llvm/include/llvm/ADT/StringMap.h
@@ -380,8 +380,8 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
     unsigned BucketNo = LookupBucketFor(Key, FullHashValue);
     StringMapEntryBase *&Bucket = TheTable[BucketNo];
     if (Bucket && Bucket != getTombstoneVal())
-      return std::make_pair(iterator(TheTable + BucketNo, false),
-                            false); // Already exists in map.
+      return {iterator(TheTable + BucketNo, false),
+              false}; // Already exists in map.
 
     if (Bucket == getTombstoneVal())
       --NumTombstones;
@@ -391,7 +391,7 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
     assert(NumItems + NumTombstones <= NumBuckets);
 
     BucketNo = RehashTable(BucketNo);
-    return std::make_pair(iterator(TheTable + BucketNo, false), true);
+    return {iterator(TheTable + BucketNo, false), true};
   }
 
   // clear - Empties out the StringMap

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@kazutakahirata kazutakahirata merged commit 13bccde into llvm:main Aug 24, 2025
10 of 11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 24, 2025

LLVM Buildbot has detected a new failure on builder openmp-offload-sles-build-only running on rocm-worker-hw-04-sles while building llvm at step 9 "Add check check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/140/builds/29465

Here is the relevant piece of the build log for the reference
Step 9 (Add check check-llvm) failure: 1200 seconds without output running [b'ninja', b'-j 32', b'check-llvm'], attempting to kill
...
     ^~~~~~~~~~~~~~~~~~
[722/725] Linking CXX executable unittests/tools/llvm-exegesis/LLVMExegesisTests
[723/725] Building CXX object unittests/TargetParser/CMakeFiles/TargetParserTests.dir/TargetParserTest.cpp.o
[724/725] Linking CXX executable unittests/TargetParser/TargetParserTests
[724/725] Running the LLVM regression tests
llvm-lit: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/utils/lit/lit/llvm/config.py:527: note: using ld.lld: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/ld.lld
llvm-lit: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/utils/lit/lit/llvm/config.py:527: note: using lld-link: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/lld-link
llvm-lit: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/utils/lit/lit/llvm/config.py:527: note: using ld64.lld: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/ld64.lld
llvm-lit: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.src/llvm/utils/lit/lit/llvm/config.py:527: note: using wasm-ld: /home/botworker/bbot/builds/openmp-offload-sles-build/llvm.build/bin/wasm-ld
-- Testing: 59961 tests, 32 workers --
command timed out: 1200 seconds without output running [b'ninja', b'-j 32', b'check-llvm'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1382.249711
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants