[flang-commits] [clang] [flang] [llvm] [mlir] [ADT] Deprecate DenseMap::getOrInsertDefault (PR #107040)

Kazu Hirata via flang-commits flang-commits at lists.llvm.org
Mon Sep 2 19:37:34 PDT 2024


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

This patch deprecates DenseMap::getOrInsertDefault in favor of
DenseMap::operator[], which does the same thing, has been around
longer, and is also a household name as part of std::map and
std::unordered_map.

Note that DenseMap provides several equivalent ways to insert or
default-construct a key-value pair:

- operator[Key]
- try_emplace(Key).first->second
- getOrInsertDefault(Key)
- FindAndConstruct(Key).second


>From 76913c59aaffd79647d125f6806bd6006122b92b Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 2 Sep 2024 17:58:47 -0700
Subject: [PATCH] [ADT] Deprecate DenseMap::getOrInsertDefault

This patch deprecates DenseMap::getOrInsertDefault in favor of
DenseMap::operator[], which does the same thing, has been around
longer, and is also a household name as part of std::map and
std::unordered_map.

Note that DenseMap provides several equivalent ways to insert or
default-construct a key-value pair:

- operator[Key]
- try_emplace(Key).first->second
- getOrInsertDefault(Key)
- FindAndConstruct(Key).second
---
 clang/lib/Sema/SemaHLSL.cpp                     | 6 ++----
 flang/lib/Optimizer/Transforms/AddAliasTags.cpp | 4 ++--
 llvm/include/llvm/ADT/DenseMap.h                | 2 ++
 mlir/lib/Transforms/SROA.cpp                    | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 83e612c6751644..fabc6f32906b10 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1123,13 +1123,11 @@ class DiagnoseHLSLAvailability
 
   // Helper methods for dealing with shader stage bitmap
   void AddToScannedFunctions(const FunctionDecl *FD) {
-    unsigned &ScannedStages = ScannedDecls.getOrInsertDefault(FD);
+    unsigned &ScannedStages = ScannedDecls[FD];
     ScannedStages |= CurrentShaderStageBit;
   }
 
-  unsigned GetScannedStages(const FunctionDecl *FD) {
-    return ScannedDecls.getOrInsertDefault(FD);
-  }
+  unsigned GetScannedStages(const FunctionDecl *FD) { return ScannedDecls[FD]; }
 
   bool WasAlreadyScannedInCurrentStage(const FunctionDecl *FD) {
     return WasAlreadyScannedInCurrentStage(GetScannedStages(FD));
diff --git a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
index c68df45afce13a..8feba072cfea67 100644
--- a/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
+++ b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp
@@ -103,8 +103,8 @@ void PassState::processFunctionScopes(mlir::func::FuncOp func) {
   if (scopeNames.contains(func))
     return;
 
-  auto &scopeMap = scopeNames.getOrInsertDefault(func);
-  auto &scopeOps = sortedScopeOperations.getOrInsertDefault(func);
+  auto &scopeMap = scopeNames[func];
+  auto &scopeOps = sortedScopeOperations[func];
   func.walk([&](fir::DummyScopeOp op) { scopeOps.push_back(op); });
   llvm::stable_sort(scopeOps, [&](const fir::DummyScopeOp &op1,
                                   const fir::DummyScopeOp &op2) {
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index f71cd5b4771b75..e78700f9a9f3ac 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -322,6 +322,7 @@ class DenseMapBase : public DebugEpochBase {
   /// Returns the value associated to the key in the map if it exists. If it
   /// does not exist, emplace a default value for the key and returns a
   /// reference to the newly created value.
+  LLVM_DEPRECATED("Use operator[] instead", "[Key]")
   ValueT &getOrInsertDefault(KeyT &&Key) {
     return try_emplace(Key).first->second;
   }
@@ -329,6 +330,7 @@ class DenseMapBase : public DebugEpochBase {
   /// Returns the value associated to the key in the map if it exists. If it
   /// does not exist, emplace a default value for the key and returns a
   /// reference to the newly created value.
+  LLVM_DEPRECATED("Use operator[] instead", "[Key]")
   ValueT &getOrInsertDefault(const KeyT &Key) {
     return try_emplace(Key).first->second;
   }
diff --git a/mlir/lib/Transforms/SROA.cpp b/mlir/lib/Transforms/SROA.cpp
index dc902cc63e0b55..aca252b01dce7b 100644
--- a/mlir/lib/Transforms/SROA.cpp
+++ b/mlir/lib/Transforms/SROA.cpp
@@ -57,7 +57,7 @@ computeDestructuringInfo(DestructurableMemorySlot &slot,
 
   auto scheduleAsBlockingUse = [&](OpOperand &use) {
     SmallPtrSetImpl<OpOperand *> &blockingUses =
-        info.userToBlockingUses.getOrInsertDefault(use.getOwner());
+        info.userToBlockingUses[use.getOwner()];
     blockingUses.insert(&use);
   };
 
@@ -122,7 +122,7 @@ computeDestructuringInfo(DestructurableMemorySlot &slot,
       assert(llvm::is_contained(user->getResults(), blockingUse->get()));
 
       SmallPtrSetImpl<OpOperand *> &newUserBlockingUseSet =
-          info.userToBlockingUses.getOrInsertDefault(blockingUse->getOwner());
+          info.userToBlockingUses[blockingUse->getOwner()];
       newUserBlockingUseSet.insert(blockingUse);
     }
   }



More information about the flang-commits mailing list