Skip to content

[ADT] Deprecate DenseMap::getOrInsertDefault #107040

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
merged 1 commit into from
Sep 3, 2024

Conversation

kazutakahirata
Copy link
Contributor

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

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
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" mlir:core MLIR Core Infrastructure mlir flang Flang issues not falling into any other category HLSL HLSL Language Support flang:fir-hlfir llvm:adt labels Sep 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2024

@llvm/pr-subscribers-llvm-adt
@llvm/pr-subscribers-mlir-core
@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-clang

Author: Kazu Hirata (kazutakahirata)

Changes

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

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

4 Files Affected:

  • (modified) clang/lib/Sema/SemaHLSL.cpp (+2-4)
  • (modified) flang/lib/Optimizer/Transforms/AddAliasTags.cpp (+2-2)
  • (modified) llvm/include/llvm/ADT/DenseMap.h (+2)
  • (modified) mlir/lib/Transforms/SROA.cpp (+2-2)
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);
     }
   }

@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2024

@llvm/pr-subscribers-mlir

Author: Kazu Hirata (kazutakahirata)

Changes

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

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

4 Files Affected:

  • (modified) clang/lib/Sema/SemaHLSL.cpp (+2-4)
  • (modified) flang/lib/Optimizer/Transforms/AddAliasTags.cpp (+2-2)
  • (modified) llvm/include/llvm/ADT/DenseMap.h (+2)
  • (modified) mlir/lib/Transforms/SROA.cpp (+2-2)
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);
     }
   }

@kazutakahirata kazutakahirata merged commit 59a3b41 into llvm:main Sep 3, 2024
17 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_DenseMap_brackets branch September 3, 2024 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category flang:fir-hlfir flang Flang issues not falling into any other category HLSL HLSL Language Support llvm:adt mlir:core MLIR Core Infrastructure mlir
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants