iOS Simulator running metal hangs entire OS when using discrete GPU

I am working on an app that uses some Metal compute shaders and trying to get this to work on the simulator. I am having an issue where the simulator freezes the entire OS (which eventually results in either a panic or WindowServer being killed and dumping me on the login screen). After some trial and error, and many reboots, I isolated the problem to a combination of 2 things:

  1. Using an atomic_fetch_add_explicit on an MTLBuffer from a compute kernel.
  2. Having the simulator set to prefer the discrete GPU.

Without the atomic operation, everything runs fine on either GPU. With the atomic operation, it runs fine on the integrated GPU, but if I select the discrete GPU it freezes WindowServer, the GUI is completely unresponsive and frozen, but I can SSH into the machine (until it decides to panic). When it's frozen there aren't any processes using significant amounts of CPU time. I can reproduce this 100% of the time.

I'm using a MacBook Pro 15" (2018, radeon 560X 4GB). It doesn't matter wether or not the discrete GPU is used by the OS.

Hi Aaargh!

Could you please confirm the following:

  1. You are running recent macOS12 beta with recent Xcode beta (to make sure your GPU compiler stack is most recent)
  2. You are able to attach a reproducer

After it is confirmed, could you please file a Feedback Assistant issue with the repro attached and come back here with the FB number? Thanks!

I tried creating a minimal test case, but couldn’t reproduce it, so I did some more digging.

The atomic add is called inside a loop. The loop runs 40 times. I reduced it to only loop once and this works without crashes. I then increased it step-by-step and it crashes if it loops more than 16 times. It seems to be purely the run time of the kernel that causes the freeze. Strangely enough the same loop takes much longer on the integrated GPU but this doesn’t cause any issues.

Why would this cause a hang ? The difference in timing between 16 or 17 runs is minimal, and the integrated GPU has no issues while taking 20 times longer.

Hi Aaargh! I am glad you are having a workaround at least. Can you confirm the OS you are running at? AMD drivers and compiler are part of the macOS stack, so there is a chance the issue is already fixed in macOS12 beta. Simple reproducer would help localizing the issue and providing a fix, otherwise we can't do anything about it. I hope you could provide a reproducer and a FB ticket number. Thanks!

iOS Simulator running metal hangs entire OS when using discrete GPU
 
 
Q