Skip to content

Fix any()/all() reductions with masked subsets and scalar semantics - #92

Merged
t-kalinowski merged 6 commits into
mainfrom
add-any-and-all-handlers
Feb 7, 2026
Merged

Fix any()/all() reductions with masked subsets and scalar semantics#92
t-kalinowski merged 6 commits into
mainfrom
add-any-and-all-handlers

Conversation

@t-kalinowski

Copy link
Copy Markdown
Owner
  • Implement any()/all() lowering for logical reductions, including mask-hoisting.
  • Fix scalar masked subset semantics (empty selection yields FALSE for any(), TRUE for all()).
  • Handle 1-element array constructors (e.g. c(FALSE) -> [ .false. ]) so reductions stay scalar.
  • Avoid pack() temporaries for masked reductions by rewriting:
    • any(x[mask]) -> any(x .and. mask)
    • all(x[mask]) -> all((.not. mask) .or. x)
  • Fix scalar+hoisted-mask merge() so it never emits rank-1 RHS when x is a 1-element array constructor.

Tests:

  • Added regression coverage in tests/testthat/test-subset-reduction.R
  • Ran: R -q -e "devtools::test()"

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31b94e9d2b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread R/r2f-reductions.R Outdated
Comment on lines +168 to +171
if (identical(call_name, "any")) {
glue("(({x}) .and. ({hoisted_mask}))")
} else {
glue("((.not. ({hoisted_mask})) .or. ({x}))")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Treat length-1 mask constructors as scalars in reductions

When a logical mask like c(TRUE) is used in subsetting (e.g., any(x[c(TRUE)]) with x a length>1 vector), [ hoists the mask as a rank-1 array constructor. This branch then emits any((x) .and. ([.true.])) or all((.not. ([.true.])) .or. (x)). In Fortran, a length-1 array constructor is not conformable with a length-N array, so the .and./.or. operations are invalid and the generated code will fail to compile. The mask needs to be treated as a scalar in this case (e.g., by reducing it with any() or special-casing length-1 constructors) to match R’s recycling semantics.

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Feb 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.78%. Comparing base (3ef7c49) to head (1d1340e).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   92.76%   92.78%   +0.01%     
==========================================
  Files          27       27              
  Lines        5734     5749      +15     
==========================================
+ Hits         5319     5334      +15     
  Misses        415      415              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@t-kalinowski
t-kalinowski merged commit c4e86c0 into main Feb 7, 2026
8 checks passed
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.

1 participant