Skip to content

Conversation

mgudim
Copy link
Contributor

@mgudim mgudim commented Sep 24, 2025

We need to clear TreeEntryToStridedPtrInfoMap in deleteTree.

@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Mikhail Gudim (mgudim)

Changes

We need to clear TreeEntryToStridedPtrInfoMap in deleteTree.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+1)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 1814d9a6811c0..39530e92e692d 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -2099,6 +2099,7 @@ class BoUpSLP {
     UserIgnoreList = nullptr;
     PostponedGathers.clear();
     ValueToGatherNodes.clear();
+    TreeEntryToStridedPtrInfoMap.clear();
   }
 
   unsigned getTreeSize() const { return VectorizableTree.size(); }

Copy link
Member

@alexey-bataev alexey-bataev left a comment

Choose a reason for hiding this comment

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

The test is required. Or revert the original commit and fix it there

@mgudim
Copy link
Contributor Author

mgudim commented Sep 25, 2025

The test is required. Or revert the original commit and fix it there

What kind of test should it be?

Reverting seems more complicated because several other merged commits were based on this one.

@alexey-bataev
Copy link
Member

The one that reveals the issue with not clearing the map

@mgudim
Copy link
Contributor Author

mgudim commented Sep 25, 2025

The one that reveals the issue with not clearing the map

The issue would be revealed only if we use the same BoUpSLP and when building a tree some memory is reused for a TreeNode. I don't see a way to set this up except a unit test and even that would be non-trivial. There is no test for any of lines is deleteTree as far as I see.

Should I start a new test in unittests to test SLPVectrorizer?

@alexey-bataev
Copy link
Member

The one that reveals the issue with not clearing the map

The issue would be revealed only if we use the same BoUpSLP and when building a tree some memory is reused for a TreeNode. I don't see a way to set this up except a unit test and even that would be non-trivial. There is no test for any of lines is deleteTree as far as I see.

Should I start a new test in unittests to test SLPVectrorizer?

Up to you, it can be a unittest or a lit test, that crashes for you.

@mgudim
Copy link
Contributor Author

mgudim commented Sep 25, 2025

Up to you, it can be a unittest or a lit test, that crashes for you.

Would it be ok that in a different commit I'll add asserts in "buildTree" that make sure everything from deleteTree is empty except TreeEntryToStridedPtrInfoMap. In this commit I'll add the assert and clear for the TreeEntryToStridedPtrInfoMap. This way it's a test every time buildTree is called!

@alexey-bataev
Copy link
Member

Ok, but still need a test

@mgudim
Copy link
Contributor Author

mgudim commented Sep 26, 2025

Why do we need a test if we have asserts? Don't asserts make it self-evident that the map is cleared? The test would essentially be the same this as an assert, just much more effort to set up.

@alexey-bataev
Copy link
Member

Each non-nfc patch requires a test

@mgudim
Copy link
Contributor Author

mgudim commented Sep 26, 2025

I am out of ideas.

I don't see how to add asserts to the code so it doesn't look stupid. Current code is like this:

"buildTree(...) {
deleteTree(...);
...
buildTreeRec(...)
}
"
adding asserts after deleteTree is like this code:

int x = 2;
assert (x == 2);

I thought that maybe I can set up a unit test. There is no public header to include which would contain BoUpSLP.

Constructing test by hand to try to make SLP crash is very hard.

There won't be a memory leak because the map gets deleted automatically with BoUpSLP object so setting up some kind of valgrind - based test is also not an option.

I can measure the size of BoUpSLP object right after it's created and right after deleteTree and assert that they are equal. Test would check that the assert is not hit. Will this be acceptable?

Can you suggest something else?

@mgudim
Copy link
Contributor Author

mgudim commented Sep 26, 2025

Each non-nfc patch requires a test

Is this an official rule written somewhere? I'm pretty sure that any reasonable guidelines would allow merging something like this without a test.

@mgudim mgudim requested a review from HanKuanChen September 29, 2025 15:46
@mgudim
Copy link
Contributor Author

mgudim commented Sep 29, 2025

I can measure the size of BoUpSLP object right after it's created and right after deleteTree and assert that they are equal. Test would check that the assert is not hit. Will this be acceptable?

ping

@alexey-bataev
Copy link
Member

Just add a test that crahes

@mgudim
Copy link
Contributor Author

mgudim commented Sep 29, 2025

I can't come up with a test that crashes

@alexey-bataev
Copy link
Member

Simple test that crashes with the assertion but without clearing the map

@mgudim
Copy link
Contributor Author

mgudim commented Sep 29, 2025

sorry, I don't understand what you mean. If I add an assertion somewhere that map is not cleared, it will break llvm for everyone until the commit which clears the map is merged

@alexey-bataev
Copy link
Member

I understand it. Just add a new test, that will crash with assertion but without clearing.

@mgudim
Copy link
Contributor Author

mgudim commented Sep 29, 2025

I understand it. Just add a new test, that will crash with assertion but without clearing.

Is this what you mean:

#161227

Copy link

github-actions bot commented Sep 29, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@@ -0,0 +1,74 @@
; RUN: opt -S --passes=slp-vectorizer < %s | FileCheck %s

; CHECK-NOT: TreeEntryToStridedPtrInfoMap is not cleared
Copy link
Member

Choose a reason for hiding this comment

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

Add real checks here

Mikhail Gudim added 3 commits September 29, 2025 10:59
@mgudim mgudim merged commit e485d5e into llvm:main Sep 30, 2025
9 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
We need to clear `TreeEntryToStridedPtrInfoMap` in `deleteTree`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants