Skip to content

Fixing SpanHelpers.LastIndexOfAnyValueType to no longer create out of bounds GC refs#75857

Merged
adamsitnik merged 3 commits into
dotnet:mainfrom
tannergooding:fix-75792
Sep 20, 2022
Merged

Fixing SpanHelpers.LastIndexOfAnyValueType to no longer create out of bounds GC refs#75857
adamsitnik merged 3 commits into
dotnet:mainfrom
tannergooding:fix-75792

Conversation

@tannergooding

Copy link
Copy Markdown
Member

This resolves #75792

@ghost ghost added the area-System.Memory label Sep 19, 2022
@ghost ghost assigned tannergooding Sep 19, 2022
@ghost

ghost commented Sep 19, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Issue Details

This resolves #75792

Author: tannergooding
Assignees: -
Labels:

area-System.Memory

Milestone: -

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@tannergooding big thanks for your help! PTAL at my comments, I think I've found a bug

Comment thread src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs Outdated
Comment thread src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs Outdated
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Comment thread src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs Outdated

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

Comment thread src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
@tannergooding

Copy link
Copy Markdown
Member Author

@stephentoub, would appreciate a secondary sign-off from you before merging.

Great to have a second pair of eyes since we're backporting for .NET 7

}

return ComputeLastIndex(ref searchSpace, ref currentSearchSpace, equals);
return ComputeLastIndex(offset, equals);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It doesn't affect this PR, but I wonder why it's written like this rather than:

if (equals != Vector128<TValue>.Zero)
{
    return ComputeLastIndex(offset, equals);
}

Is this just style, or we expect the not found case to be the most common and want the branch predictor to default to that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we expect the not found case to be the most common and want the branch predictor to default to that

That was my assumption when I was implementing it.

@adamsitnik
adamsitnik merged commit 81d75bd into dotnet:main Sep 20, 2022
@tannergooding

Copy link
Copy Markdown
Member Author

/backport to release/7.0-rc2

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/7.0-rc2: https://github.com/dotnet/runtime/actions/runs/3089699377

@ghost ghost locked as resolved and limited conversation to collaborators Oct 20, 2022
@tannergooding
tannergooding deleted the fix-75792 branch November 11, 2022 15:09
tannergooding added a commit that referenced this pull request Jul 21, 2026
)

The general SIMD and hardware-intrinsics guidance now lives in the
official docs, which were recently rewritten in dotnet/docs#54834. Our
in-repo `docs/coding-guidelines/vectorization-guidelines.md` had become
largely duplicative and out of sync, so this trims it down to defer to
the official article and keep only the dotnet/runtime-specific nuance:

- The `BoundedMemory` test helper for access-violation testing (with a
repo-relative link and a short usage example).
- The real `LastIndexOf` GC-hole case study (#73768 / fix #75857) behind
the "prefer the element-offset overloads" rule.
- The #64451 catalogue of real vectorized implementations to learn from.

The lane-crossing worked example that the official rewrite dropped is
being restored upstream in dotnet/docs#54840 rather than kept here,
since it''s general teaching content and belongs where it stays in sync.

----------

Also adds a `vectorization` skill covering both authoring and reviewing
SIMD code. Vectorization is a cross-cutting domain rather than a folder,
so a task-triggered skill fits better than a file-scoped
`.instructions.md`. The skill defers to the official docs and carries
the same repo-specific nuance (prefer the span/`LoadUnsafe`
element-offset overloads, `BoundedMemory` AV testing, the
acceleration-toggle env vars, `char`/`bool` reinterpretation, and
benchmarking before committing complexity), plus
authoring/testing/review checklists. `copilot-instructions.md` gets a
one-line nudge toward it, matching the existing
`code-review`/`performance-benchmark` pattern.

> [!NOTE]
> This PR description was drafted by GitHub Copilot.

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GCStress crash stable repro

5 participants