Skip to content

Fix hash collision in CSE#22061

Merged
rainers merged 2 commits intodlang:masterfrom
limepoutine:cse
Nov 6, 2025
Merged

Fix hash collision in CSE#22061
rainers merged 2 commits intodlang:masterfrom
limepoutine:cse

Conversation

@limepoutine
Copy link
Contributor

In cgcs.d, assignment ops clobber CSE of a symbol, but weirdly do not clobber any larger common expressions referencing it. The checks to avoid false matches are a kludge. Instead, this PR now requires all leaf nodes to have already been merged by CSE before eliminating a larger expression. As a result, DMD's CSE is now immune to hash collision (in terms of correctness). This hopefully fixes #20985 (worked for a few hundred builds on my machine yet still unsure).

On the downside, DMD's optimization is slightly weaker again. I think maybe very few people will care.

@limepoutine
Copy link
Contributor Author

#20985 is much easier to reproduce under low memory like today's Azure pipelines, which very well hints at hash collision as the root cause?

@rainers
Copy link
Member

rainers commented Nov 5, 2025

As mentioned in the bug report, the failures are reproducible with cs_comphash() always returning 0, i.e. always causing collisions. Have you tested your change with that?

@limepoutine
Copy link
Contributor Author

This can pass the whole test suite with hash_t cs_comphash(const elem* e) => 0;. I have no idea how to write a test enforcing that however.

@rainers
Copy link
Member

rainers commented Nov 5, 2025

On the downside, DMD's optimization is slightly weaker again. I think maybe very few people will care.

AFAIU only for expressions where the hashes happen to match, which is very unlikely and non-deterministic anyway.

This can pass the whole test suite with hash_t cs_comphash(const elem* e) => 0;.

Good to know.

I have no idea how to write a test enforcing that however.

Probably not worth the trouble. Do we have a test that CSE elimination works at all?

Copy link
Member

@rainers rainers left a comment

Choose a reason for hiding this comment

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

Prepend the commit message with Fix #20985 to auto-close the issue and include it in the changelog.

@limepoutine
Copy link
Contributor Author

Do we have a test that CSE elimination works at all?

No, I stubbed out all CSE code and tests are still green.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @limepoutine! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22061"

@rainers rainers merged commit 3bd36cd into dlang:master Nov 6, 2025
42 checks passed
dkorpel pushed a commit to dkorpel/dmd that referenced this pull request Dec 9, 2025
* prevent invalid CSE on hash collision

* Restore checks on Ecount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runnable\exe1.c heisenbugs on Win64

3 participants