[llvm] [ADT] Use try_emplace in SCCIterator.h (NFC) (PR #164187)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 19 16:22:32 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/164187

This patch replaces an unidiomatic operator[] usage with try_emplace
to ensure in-place NodeInfo construction and correct Group field
initialization.

This patch also clarifies the comment.  In particular, "insertion
operation" by itself isn't clear whether it's referring to insert() or
all of insertion operations like insert(), insert_or_assign(),
try_emplace(), etc.


>From d2bf15f544a5b10d2db60367b05f551d0e77f9d3 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 18 Oct 2025 01:55:49 -0700
Subject: [PATCH] [ADT] Use try_emplace in SCCIterator.h (NFC)

This patch replaces an unidiomatic operator[] usage with try_emplace
to ensure in-place NodeInfo construction and correct Group field
initialization.

This patch also clarifies the comment.  In particular, "insertion
operation" by itself isn't clear whether it's referring to insert() or
all of insertion operations like insert(), insert_or_assign(),
try_emplace(), etc.
---
 llvm/include/llvm/ADT/SCCIterator.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/ADT/SCCIterator.h b/llvm/include/llvm/ADT/SCCIterator.h
index 64ee217eeed61..205fa669a12de 100644
--- a/llvm/include/llvm/ADT/SCCIterator.h
+++ b/llvm/include/llvm/ADT/SCCIterator.h
@@ -313,10 +313,10 @@ scc_member_iterator<GraphT, GT>::scc_member_iterator(
   // Initialize auxilary node information.
   NodeInfoMap.clear();
   for (auto *Node : InputNodes) {
-    // This is specifically used to construct a `NodeInfo` object in place. An
-    // insert operation will involve a copy construction which invalidate the
-    // initial value of the `Group` field which should be `this`.
-    (void)NodeInfoMap[Node].Group;
+    // Construct a `NodeInfo` object in place.  `insert()` would involve a copy
+    // construction, invalidating the initial value of the `Group` field, which
+    // should be `this`.
+    NodeInfoMap.try_emplace(Node);
   }
 
   // Sort edges by weights.



More information about the llvm-commits mailing list