[jdk23] RFR: 8335390: C2 MergeStores: wrong result with Unsafe

Shaojin Wen duke at openjdk.org
Thu Jul 25 01:19:37 UTC 2024


On Tue, 2 Jul 2024 14:26:09 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

>> Looks good to me.
>
> Thanks @TobiHartmann for the review.

@eme64 Unsafe.putByte MergeStore of master branch doesn't work

import sun.misc.Unsafe;

public class PutBytesTest {
    static final Unsafe UNSAFE = JDKUtils.UNSAFE;

    // MergeStore not work
    static void putCharsAt(byte[] val, int index, int c1, int c2, int c3, int c4) {
        putByte(val, index    , (byte)(c1));
        putByte(val, index + 1, (byte)(c2));
        putByte(val, index + 2, (byte)(c3));
        putByte(val, index + 3, (byte)(c4));
    }

    // MergeStore work
    static void putCharsAtArrayStore(byte[] val, int index, int c1, int c2, int c3, int c4) {
        val[index] = (byte) c1;
        val[index + 1] = (byte) c2;
        val[index + 2] = (byte) c3;
        val[index + 3] = (byte) c4;
    }

    static void putByte(byte[] bytes, int index, byte c) {
        UNSAFE.putByte(bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET + index, c);
    }

    static void putNull(byte[] bytes, int index) {
        putCharsAt(bytes, index, 'n', 'u', 'l', 'l');
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            testNull();
        }
        System.out.println("done");
    }

    private static void testNull() {
        byte[] bytes = new byte[4096];
        for (int i = 0; i < 1000; i++) {
            int index = 0;
            for (int j = 0; j < 1024; j++) {
                putNull(bytes, index);
                index += 4;
            }
        }
    }
}

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

PR Comment: https://git.openjdk.org/jdk/pull/19985#issuecomment-2249178226


More information about the hotspot-compiler-dev mailing list