slang icon indicating copy to clipboard operation
slang copied to clipboard

Metal: atomic instructions for buffers

Open jkwak-work opened this issue 8 months ago • 1 comments

Problem description Most of the atomic instructions are implemented for Metal by PR #4473. But the atomic member functions for buffers are not implemented.

The current implementation piggy-back on how Spir-v does, but it doesn't actually work.

struct RWByteAddressBuffer
{
    void InterlockedAdd(UINT dest, UINT value)
    {
        __target_switch
        {
        case metal:
        case spirv:
            let buf = __getEquivalentStructuredBuffer<uint>(this); // <=== DON'T WORK FOR METAL
            ::InterlockedAdd(buf[dest / 4], value);
        }
    }
}

Goal We need to make the atomic member functions working for the buffers.

Repro steps We have atomic test for buffer for HLSL, "tests/hlsl-intrinsic/byte-address-buffer-atomics.slang". It can be copied over to "tests/metal/atomic-buffer.slang" and make it working.

But for a simple repro, the following code can be used for a test.

//TEST:SIMPLE: -entry computeMain -stage compute -target metal
//TEST:SIMPLE: -entry computeMain -stage compute -target metallib

RWByteAddressBuffer bbuffer;

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    bbuffer.InterlockedAdd(0, 1);
}

jkwak-work avatar Jun 25 '24 23:06 jkwak-work