-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validation for destination of atomic operations #6093
Conversation
ffeaabc
to
070707b
Compare
@csyonghe This validation currently breaks the following tests:
|
you need to call If the root addr is If the root addr is If the root addr is a pointer where the address space is one of In tests/spirv/ptr-vector-member.slang, the operand of the atomic operation is a pointer (whose address space is UserPointer), the pointer itself is a push constant, but that is irrelevant to the issue: the address where we are performing the atomic operation on is in PhyiscalStorageBuffer address space since we are doing atomics on a user defined pointer. |
070707b
to
9ddc2c2
Compare
Yes, I'm following addressing instructions to get to the root.
Thanks, I hadn't added the ptr case yet and that's why
Ok, the syntax mislead me. I think would have expected the syntax
|
9ddc2c2
to
797fade
Compare
I'm still having an issue with this case:
@csyonghe When I chase I see a few options:
|
I do t think we should be chasing OpLoad, because InterlockedAdd(*ptr, …) does not translate to AtomicAdd(Load(ptr)) and is just AtomicAdd(ptr). *ptr returns a reference, and at IR level a reference is still a pointer. using atomic operation on an inout array is technically unsound and we may want to disallow this in the future, but for now, make sure your check is after address space specialization pass to get rid of the issue. After address space specialization, the IRParam’s type will be a IROutTypeBase with a specialized address space, and you can apply the same rules to check that address space. |
797fade
to
509fa2a
Compare
Done -- removed.
Thanks, that made the test pass. |
/format |
🌈 Formatted, please merge the changes from this PR |
e4491f3
to
82dfacf
Compare
/format |
🌈 Formatted, please merge the changes from this PR |
Format code for PR shader-slang#6093
@@ -1322,6 +1322,11 @@ Result linkAndOptimizeIR( | |||
byteAddressBufferOptions); | |||
} | |||
|
|||
// For SPIR-V, this function is called elsewhere, so that it can happen after address space |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could mean we will still be generating errors when emitting hlsl or other non-spirv target code.
I think we should amend validateAtomicOperations
such that for any non-spirv target code, we silently treat inout/out IRParam
as valid target for atomic operations for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked that it works for Metal, at least. (Address space specialization is run in Metal IR legalization, which happens before this point in the code.)
I think we should amend validateAtomicOperations such that for any non-spirv target code, we silently treat inout/out IRParam as valid target for atomic operations for now.
Sure, then I guess I don't need two separate call sites for this function. (Edit: well, not if we only make the exception for non-SPIR-V targets... I'll keep the separate call site then.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: I added a parameter that lets the caller specify whether to skip full IRParam checks in this way.
The caller can set this parameter to false for SPIR-V and true for other targets.
source/slang/slang-ir-validate.cpp
Outdated
{ | ||
// There may be unused functions containing violations after address space specialization. | ||
if (auto func = as<IRFunc>(inst)) | ||
if (!(func->hasUses() || func->findDecoration<IREntryPointDecoration>())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this check because all functions that exist in the IR at this point are existing for a reason, due to DCE has already run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this because of the IRParam issue: it seemed like the original function (with inout IRParam) was left in the IR with no uses after address space specialization had run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed: I still hit this issue in the call from slang-ir-spirv-legalize.cpp (since that's SPIR-V target I don't allow skipping the validation of the IRParam, as for other targets), if I delete this check.
Some options I can see:
- Do the check in address space specialization and clean up any non-entrypoint functions left without uses there.
- Keep the check here.
- Move the call to
validateAtomicOperations
to some other place where the function has been cleaned up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I attempted the first option and removed the check.
@@ -0,0 +1,13 @@ | |||
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -allow-glsl -stage compute -entry computeMain -target glsl -DTARGET_GLSL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can merge all these tests into one single file, and use
// CHECK: ([[# @LINE+1]]): error 41043
before all the atomic calls to ensure there are error messages for each one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
d67584b
to
5e60b4f
Compare
fea98f0
to
0c9e9a7
Compare
/format |
🌈 Formatted, please merge the changes from this PR |
Format code for PR shader-slang#6093
Many of these functions map directly to atomic IR instructions. The functions taking atomic_uint are left as they are. This helps to address shader-slang#5989, since the destination pointer type validation can then be written only for the atomic IR instructions.
Diagnose an error if the destination of the atomic operation is not appropriate, where appropriate means it's either: - 'groupshared' - from a device buffer This closes shader-slang#5989.
Attempting to use the GLSL atomic functions on destinations that are neither groupshared nor from a device buffer should fail with the following error: error 41403: cannot perform atomic operation because destination is neither groupshared nor from a device buffer.
Address space specialization for SPIR-V is not done as part of `linkAndOptimizeIR`, as it is for e.g. Metal, so opt out and add a separate call for SPIR-V.
1c823ac
to
c32ab5f
Compare
I changed the entrypoint check into an assert, as discussed yesterday. |
/format |
🌈 Formatted, please merge the changes from this PR |
Format code for PR shader-slang#6093
This closes issue #5989.