Skip to content

Add CompExactlyDependsOn to some methods so that they are compiled on wasm - #135869

Merged
BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-wasm-comp-packedsimd
Oct 16, 2026
Merged

BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-wasm-comp-packedsimd

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Oct 15, 2026

Copy link
Copy Markdown
Member

A method having multiple CompExactlyDependsOn attributes with various instruction sets is compiled only if at least one of these instruction sets is supported (in addition to the requirement of all ISAs to be deterministically supported or unsupported at run time). CompHasFallback serves the purpose of still compiling the method even if none of the ISAs is supported. In our case here, all these methods are already used in the context of other methods depending on PackedSimd so we just need to add this missing attribute to enable their compilation.

For example, this makes System.Text.RegularExpressions.Tests.Perf_Regex_Common.ReplaceWords 100x faster.

… wasm

A method having multiple `CompExactlyDependsOn` attributes with various instruction sets is compiled only if at least one of these instruction sets is supported (in addition to the requirement of all ISAs to be deterministically supported or unsupported at run time). `CompHasFallback` serves the purpose of still compiling the method even if none of the ISAs is supported. In our case here, all these methods are already used in the context of other methods depending on PackedSimd so we just need to add this missing attribute to enable their compilation.

For example, this makes `System.Text.RegularExpressions.Tests.Perf_Regex_Common.ReplaceWords` 100x faster.
@BrzVlad BrzVlad added the arch-wasm WebAssembly architecture label Oct 15, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@BrzVlad

BrzVlad commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

/ba-g failure unrelated

@BrzVlad
BrzVlad merged commit e33ea58 into dotnet:main Oct 16, 2026
152 of 154 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
lewing added a commit that referenced this pull request Nov 17, 2026
`PackedSpanHelpers` narrows two `Vector128<ushort>` into one
`Vector128<byte>` to search 16 chars per compare, but was gated on
`Sse2` only, so WebAssembly always took the non-packed path.
`PackedSimd.ConvertNarrowingSaturateUnsigned` (`i8x16.narrow_i16x8_u`)
has the same saturation semantics as `Sse2.PackUnsignedSaturate`, so the
existing `CanUsePackedIndexOf` rules apply unchanged.

- `PackedIndexOfIsSupported` becomes `Sse2 || PackedSimd`;
`PackSources`/`LoadPacked128` narrow with `PackedSimd`, using explicit
`IsSupported` chains so ILLink removes dead branches.
- `string.Split` and `SingleStringSearchValuesPackedThreeChars` use the
packed path on wasm.
- `[CompExactlyDependsOn(typeof(PackedSimd))]` lets CoreCLR wasm R2R
precompile these methods.

This applies to Mono (interpreter and AOT) and CoreCLR R2R; the CoreCLR
interpreter reports `PackedSimd.IsSupported == false`.

| CoreCLR browser-wasm R2R, new/base | len=16 | len=1024 |
|---|--:|--:|
| `span.IndexOf(char)` | 1.07 | 0.60 |
| `span.IndexOfAny(c0, c1, c2)` | 1.17 | 0.71 |
| `SearchValues<string>` | 1.05 | 0.61 |

Short inputs cost about 1–2 ns per call on Arm hosts, where V8 and
Cranelift lower the narrow and bitmask to multi-instruction sequences;
on x64 they map to `packuswb`/`pmovmskb` (not measured).

<details><summary>Full benchmark table and Arm details</summary>

Apple M-series host under Node/V8, five interleaved rounds per build,
medians; ratio = new / baseline.

| Benchmark | len=16 | len=64 | len=1024 |
|---|---:|---:|---:|
| `span.IndexOf(char)` | 1.07 | 1.01 | 0.60 |
| `span.IndexOfAny(c0, c1)` | 1.10 | 1.02 | 0.70 |
| `span.IndexOfAny(c0, c1, c2)` | 1.17 | 1.01 | 0.71 |
| `span.IndexOfAnyExcept(char)` | 1.11 | 1.00 | 0.60 |
| `span.Contains(char)` | 1.04 | 1.11 | 0.63 |
| `string.IndexOf(char, OrdinalIgnoreCase)` | 1.05 | 0.96 | 0.64 |
| `SearchValues<char>` (3 values) | 1.13 | 1.05 | 0.73 |
| `SearchValues<string>` (1 value) | 1.05 | 1.04 | 0.61 |
| `string.Split` (3 separators, dense) | 1.07 | 1.06 | 1.03 |
| `string.Replace(string, string)` (1-char old value, dense) | 1.04 |
1.07 | 1.15 |

Mono browser-wasm shows the same shape: AOT 0.66–0.80 at len=1024 and
1.09–1.27 at len=16; the interpreter 0.65–0.85 at most sizes. Results
break even around 64–96 chars.

On aarch64, V8 (`d8 --print-wasm-code`) and Cranelift lower
`i8x16.narrow_i16x8_u` to a dependent `sqxtun`/`sqxtun2` pair and
`i8x16.bitmask` to a two-`pmull` + `trn2` sequence; `i16x8.bitmask`
needs one `pmull`. A possible follow-up keeps the packed `any_true` loop
but computes the match index from the unpacked 16-bit vectors, as
`MakeSeparatorListVectorized` does.

Related: #135869 (other `CompExactlyDependsOn(PackedSimd)` gaps),
#135892 (crossgen2 doesn't fold `box T; isinst X`).
</details>

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

---------

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: a42c1090-4687-4af7-92e0-1aa50cdd1826
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasm WebAssembly architecture area-ReadyToRun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants