[llvm] [DenseMap] Work around a MSVC bug in lookup_or (PR #142268)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 08:33:47 PDT 2025


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/142268

>From a70a2b0fcf36bca5c2570cc049ddfc6b0c0bab01 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <r at artagnon.com>
Date: Sat, 31 May 2025 13:16:35 +0200
Subject: [PATCH 1/2] [DenseMap] Work around a MSVC bug in lookup_or

4bf67cd ([DenseMap] Fix constness issues with lookup_or) introduced a
buildbot failure:

  https://lab.llvm.org/buildbot/#/builders/63/builds/6559

This seems to be a bug in MSVC, where it doesn't bind an lvalue to an
rvalue reference. Work around it by introducing a const-lvalue-reference
variant of lookup_or.
---
 llvm/include/llvm/ADT/DenseMap.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index de41a212c65ab..62a33500e3ad2 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -226,6 +226,12 @@ class DenseMapBase : public DebugEpochBase {
     return Default;
   }
 
+  ValueT lookup_or(const_arg_type_t<KeyT> Val, const ValueT &Default) const {
+    if (const BucketT *Bucket = doFind(Val))
+      return Bucket->getSecond();
+    return Default;
+  }
+
   /// at - Return the entry for the specified key, or abort if no such
   /// entry exists.
   const ValueT &at(const_arg_type_t<KeyT> Val) const {

>From 5fdd3bfa21325017dce58119ed82033d06978bc4 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 2 Jun 2025 16:32:20 +0100
Subject: [PATCH 2/2] [DenseMap] Use std::remove_cv_t

---
 llvm/include/llvm/ADT/DenseMap.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 62a33500e3ad2..3825f8d523728 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -220,13 +220,8 @@ class DenseMapBase : public DebugEpochBase {
   // Return the entry with the specified key, or \p Default. This variant is
   // useful, because `lookup` cannot be used with non-default-constructible
   // values.
-  ValueT lookup_or(const_arg_type_t<KeyT> Val, ValueT &&Default) const {
-    if (const BucketT *Bucket = doFind(Val))
-      return Bucket->getSecond();
-    return Default;
-  }
-
-  ValueT lookup_or(const_arg_type_t<KeyT> Val, const ValueT &Default) const {
+  template <typename U = std::remove_cv_t<ValueT>>
+  ValueT lookup_or(const_arg_type_t<KeyT> Val, U &&Default) const {
     if (const BucketT *Bucket = doFind(Val))
       return Bucket->getSecond();
     return Default;



More information about the llvm-commits mailing list