Skip to content

Stop generating unnecessary omni indexes in C++#1508

Merged
WardBrian merged 3 commits intomasterfrom
unenforce-initialize-assign-bug
Apr 7, 2025
Merged

Stop generating unnecessary omni indexes in C++#1508
WardBrian merged 3 commits intomasterfrom
unenforce-initialize-assign-bug

Conversation

@WardBrian
Copy link
Copy Markdown
Member

I ran into this while looking at #1295 and it seemed much easier to fix:

If we are using --O1, with code like:

  vector[2] x;
  x[:] = zeros_vector(2);

ends up generating an exception complaining that x has size 0.

This case is correctly (...ish, see #1295!) handled when the code is omits the omni indexing, and when
the only indexing done is omni, the extra checks that get done are completely redundant, so we
can in fact just omit them entirely.

Submission Checklist

  • Run unit tests
  • Documentation
    • If a user-facing facing change was made, the documentation PR is here:
    • OR, no user-facing changes were made

Release notes

Fixed a bug with assigning to an entire vector or matrix using : with the --O1 flag.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

@WardBrian WardBrian requested a review from SteveBronder April 1, 2025 19:51
@WardBrian
Copy link
Copy Markdown
Member Author

Once this is merged I plan on adding the test added here to https://github.com/stan-dev/performance-tests-cmdstan/tree/master/optimizer-stress-models -- since this is a runtime error, testing the result of actually running it will be more useful

@bob-carpenter
Copy link
Copy Markdown
Member

I couldn't tell from the PR title what was being done here. I hope the intention is to fix the bug so that the program you list works as intended. In the end, x[:] is equivalent to x[] and x, so if the proposal's just to avoid generating a needless multi-index, that sounds like a great idea.

I'd look at not just x[:], but also x and x[], all of which are synonyms here.

P.S. I had no idea that there is a zeros_vector(n) function! I see rep_vector(0, n) everywhere in my own and others' code.

@WardBrian
Copy link
Copy Markdown
Member Author

so if the proposal's just to avoid generating a needless multi-index, that sounds like a great idea.

Yes. If the left-hand-side of an assignment is indexed only with what we call "omni" indexes (blank or :, they're the same by this part of the compiler), we can leave them out of the generated C++ rather than pass them and end up in a different code path

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.56%. Comparing base (e511e97) to head (16f05b4).
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1508      +/-   ##
==========================================
+ Coverage   89.55%   89.56%   +0.01%     
==========================================
  Files          66       66              
  Lines        9695     9698       +3     
==========================================
+ Hits         8682     8686       +4     
+ Misses       1013     1012       -1     
Files with missing lines Coverage Δ
src/analysis_and_optimization/Optimize.ml 92.51% <100.00%> (+0.19%) ⬆️
src/middle/Index.ml 86.20% <100.00%> (+0.49%) ⬆️
src/stan_math_backend/Lower_stmt.ml 95.67% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WardBrian WardBrian merged commit b597a79 into master Apr 7, 2025
3 checks passed
@WardBrian WardBrian deleted the unenforce-initialize-assign-bug branch April 7, 2025 20:28
@WardBrian WardBrian mentioned this pull request Apr 7, 2025
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.

3 participants