HLSL race condition when writing to shared memory passed to function

Started by
9 comments, last by Adam Miles 6 years, 9 months ago

I have code like this:


groupshared uint tempData[ElementsCount];

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    tempData[gtID.x] = 0;
}

And it works fine. Now I change it to this:


void MyFunc(inout uint3 gtID: SV_GroupThreadID, inout uint inputData[ElementsCount])
{
    inputData[gtID.x] = 0;
}

groupshared uint tempData[ElementsCount];

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID, tempData);
}

and I get "error X3695: race condition writing to shared memory detected, consider making this write conditional.". Any way to go around this?

Advertisement

Based on a quick search I found these:

https://www.gamedev.net/topic/594131-dx11-compute-shader-race-condition-error-when-using-optimization-level-2-or-3/

http://xboxforums.create.msdn.com/forums/t/63981.aspx

Based on those it sounds like there might be a bug in certain versions of the compiler. I'd suggest trying to use either command line fxc.exe or a more recent version of the d3dcompiler dll to see if it makes any difference.

I stumbled upon those threads as well and it's not it.

Also, I'm not really sure how to update my d3dcompiler. I'm using Windows 10 so I presume it gets updated automatically. Although I use Visual Studio 2013 so I cannot really be sure if the most up-to-date dll is used.

I found out that the problem appears even in this code:


static const int ElementsCount = 512;


groupshared uint tempData[2 * ElementsCount];


void MyFunc(inout uint3 gtID: SV_GroupThreadID, inout uint inputData[2 * ElementsCount])
{

}


[numthreads(ElementsCount, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID, tempData);
}

Note that I don't even write anything to tempData in MyFunc.
I also found out the problem goes away if I remove the "inout" modifier but then the array just gets copied probably as the code doesn't work as expected.

https://blogs.msdn.microsoft.com/chuckw/2012/05/07/hlsl-fxc-and-d3dcompile/ explains all the details of how the compiler DLL works.

If you want to check which version of the DLL your program is using, then just pause it in the debugger and look through the modules window for the DLL. I believe the latest version is D3dcompiler_47.dll

Have you tried compiling the shader using fxc.exe?

Yeah, I have D3dcompiler_47.dll indeed.

I did try. Forgot to mention that in previous post. The same problem persists.

There is many version of d3dcompiler_47.dll, a very dumb idea…

If your shader just compile from visual studio as a hlsl source file, the fxc and dll you use is probably bound to the windows sdk that is setup in your project.

Not saying that getting the latest one would solve this, but you may still run an outdated compiler :)

I've reproduced the behaviour, and simplified the case that goes wrong. Here's my minimal failing case:


groupshared uint tempData[1];

void MyFunc(inout uint inputData[1])
{
}

[numthreads(2, 1, 1)]
void CSMain()
{
    MyFunc(tempData);
}

It looks like just passing the argument to the function is enough to make it fail to compile.

Here's a workaround for the problem - don't pass the array as a function argument:


#define ElementsCount 256

groupshared uint tempData[ElementsCount];

void MyFunc(in uint3 gtID)
{
      tempData[gtID.x] = 0;
}

[numthreads(ElementsCount/2, 1, 1)]
void CSMain(uint3 gID: SV_GroupID, uint3 gtID: SV_GroupThreadID)
{
    MyFunc(gtID);
}

Yeah, I'm perfectly aware of that workaround and I do it this way. But because I can't pass a shared memory array to function I can't make the function more general. Instead I need to copy it to a few files I use it in.

I found a better workaround. So simple I can't imagine how I could had not come up with it before. I just used macro.

Still, would be nice if this bug was fixed. In the meantime I will be using macros on functions getting shared buffers as input.

In the latest Xbox compiler the alleged race condition is just a warning rather than an error like it is in the 15063 Windows SDK. I've added a bug to make sure it's neither.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

This topic is closed to new replies.

Advertisement