On Mon, 5 Jan 2026 08:14:10 GMT, Eric Fang <[email protected]> wrote:

>> `VectorMaskCastNode` is used to cast a vector mask from one type to another 
>> type. The cast may be generated by calling the vector API `cast` or 
>> generated by the compiler. For example, some vector mask operations like 
>> `trueCount` require the input mask to be integer types, so for floating 
>> point type masks, the compiler will cast the mask to the corresponding 
>> integer type mask automatically before doing the mask operation. This kind 
>> of cast is very common.
>> 
>> If the vector element size is not changed, the `VectorMaskCastNode` don't 
>> generate code, otherwise code will be generated to extend or narrow the 
>> mask. This IR node is not free no matter it generates code or not because it 
>> may block some optimizations. For example:
>> 1. `(VectorStoremask (VectorMaskCast (VectorLoadMask x)))` The middle 
>> `VectorMaskCast` prevented the following optimization: `(VectorStoremask 
>> (VectorLoadMask x)) => (x)`
>> 2. `(VectorMaskToLong (VectorMaskCast (VectorLongToMask x)))`, which blocks 
>> the optimization `(VectorMaskToLong (VectorLongToMask x)) => (x)`.
>> 
>> In these IR patterns, the value of the input `x` is not changed, so we can 
>> safely do the optimization. But if the input value is changed, we can't 
>> eliminate the cast.
>> 
>> The general idea of this PR is introducing an `uncast_mask` helper function, 
>> which can be used to uncast a chain of `VectorMaskCastNode`, like the 
>> existing `Node::uncast(bool)` function. The funtion returns the first non 
>> `VectorMaskCastNode`.
>> 
>> The intended use case is when the IR pattern to be optimized may contain one 
>> or more consecutive `VectorMaskCastNode` and this does not affect the 
>> correctness of the optimization. Then this function can be called to 
>> eliminate the `VectorMaskCastNode` chain.
>> 
>> Current optimizations related to `VectorMaskCastNode` include:
>> 1. `(VectorMaskCast (VectorMaskCast x)) => (x)`, see JDK-8356760.
>> 2. `(XorV (VectorMaskCast (VectorMaskCmp src1 src2 cond)) (Replicate -1)) => 
>> (VectorMaskCast (VectorMaskCmp src1 src2 ncond))`, see JDK-8354242.
>> 
>> This PR does the following optimizations:
>> 1. Extends the optimization pattern `(VectorMaskCast (VectorMaskCast x)) => 
>> (x)` as `(VectorMaskCast (VectorMaskCast  ... (VectorMaskCast x))) => (x)`. 
>> Because as long as types of the head and tail `VectorMaskCastNode` are 
>> consistent, the optimization is correct.
>> 2. Supports a new optimization pattern `(VectorStoreMask (VectorMaskCast ... 
>> (VectorLoadMask x))) => (x)`. Since the value before and after the pattern 
>> is a boolean vect...
>
> Eric Fang has updated the pull request with a new target base due to a merge 
> or a rebase. The pull request now contains 12 commits:
> 
>  - Update copyright year to 2026
>  - Merge branch 'master' into JDK-8370863-mask-cast-opt
>  - Convert the check condition for vector length into an assertion
>    
>    Also refined the tests.
>  - Refine code comments
>  - Merge branch 'master' into JDK-8370863-mask-cast-opt
>  - Merge branch 'master' into JDK-8370863-mask-cast-opt
>  - Add MaxVectorSize IR test condition for VectorStoreMaskIdentityTest.java
>  - Refine the test code and comments
>  - Merge branch 'master' into JDK-8370863-mask-cast-opt
>  - Don't read and write the same memory in the JMH benchmarks
>  - ... and 2 more: https://git.openjdk.org/jdk/compare/6eaabed5...9c38a6d9

src/hotspot/share/opto/vectornode.cpp line 1063:

> 1061:   return n;
> 1062: }
> 1063: 

This has a clear parallel in `Node::uncast`. But there, we may recursively 
uncast.

Your pattern:
`(VectorStoreMask (VectorMaskCast* (VectorLoadMask x))) => (x)`

We could also have a chain of casts here.
Can you explain why you chose only to do a single step here?

test/hotspot/jtreg/compiler/vectorapi/VectorMaskCastIdentityTest.java line 121:

> 119:         VectorMask<Integer> mInt128 = 
> mFloat128.cast(IntVector.SPECIES_128);
> 120:         return mInt128.not().trueCount();
> 121:     }

Why can't the casts be eliminated here? Can you please add a comment to the 
test?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28313#discussion_r2730746738
PR Review Comment: https://git.openjdk.org/jdk/pull/28313#discussion_r2730754926

Reply via email to