Skip to content

[DAGCombiner] Use SmallDenseMap (NFC) #79681

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

Conversation

kazutakahirata
Copy link
Contributor

The use of SmallDenseMap saves 0.48% of heap allocations during the
compilation of a large preprocessed file, namely X86ISelLowering.cpp,
for the X86 target. During the experiment, the maximum size of
WorklistMap was 24 or less 74% of the time. (Note that DenseMap has
the maximum occupancy rate of 3/4.)

The use of SmallDenseMap saves 0.48% of heap allocations during the
compilation of a large preprocessed file, namely X86ISelLowering.cpp,
for the X86 target.  During the experiment, the maximum size of
WorklistMap was 24 or less 74% of the time.  (Note that DenseMap has
the maximum occupancy rate of 3/4.)
@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jan 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2024

@llvm/pr-subscribers-llvm-selectiondag

Author: Kazu Hirata (kazutakahirata)

Changes

The use of SmallDenseMap saves 0.48% of heap allocations during the
compilation of a large preprocessed file, namely X86ISelLowering.cpp,
for the X86 target. During the experiment, the maximum size of
WorklistMap was 24 or less 74% of the time. (Note that DenseMap has
the maximum occupancy rate of 3/4.)


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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+1-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 87184fe409eade..ab572c5eca7399 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -174,7 +174,7 @@ namespace {
     /// This is used to find and remove nodes from the worklist (by nulling
     /// them) when they are deleted from the underlying DAG. It relies on
     /// stable indices of nodes within the worklist.
-    DenseMap<SDNode *, unsigned> WorklistMap;
+    SmallDenseMap<SDNode *, unsigned, 32> WorklistMap;
 
     /// This records all nodes attempted to be added to the worklist since we
     /// considered a new worklist entry. As we keep do not add duplicate nodes

@kazutakahirata kazutakahirata requested review from nikic and fhahn January 27, 2024 07:59
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@kazutakahirata kazutakahirata merged commit 863b2c8 into llvm:main Jan 27, 2024
@kazutakahirata kazutakahirata deleted the pr_cleanup_memory_alloc_48bp_DAGCombiner branch January 27, 2024 20:18
@nikic
Copy link
Contributor

nikic commented Jan 27, 2024

Looks like this causes a regression rather than an improvement in practice: https://llvm-compile-time-tracker.com/compare.php?from=57a20d2d09bbd3cc501fc6d8b4746be2040c99b7&to=863b2c84c0fbcfb02d969fa36af4932d410a827b&stat=instructions%3Au

I think this is because SmallDenseMap (unlike SmallVector) actually incurs an extra cost because it has to always check the two representations dynamically.

kazutakahirata added a commit that referenced this pull request Jan 28, 2024
This reverts commit 863b2c8.

A compile-time regression has been reported:

#79681 (comment)
@kazutakahirata
Copy link
Contributor Author

Thanks. I've reverted the patch for now: faf555f

I guess if we are to use an inline buffer for SmallDenseMap, the size of the inline buffer must accommodate the vast majority (90%+?) of the use cases.

@nikic
Copy link
Contributor

nikic commented Jan 28, 2024

@kazutakahirata It probably depends a lot on how often the DenseMap is accessed. If it is accessed much more often that it is grown, that may outweigh the allocation overhead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants