You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are looking for a way to achieve this function:
int atomic_add(int& dest, int value){
int original_value;
InterlockedAdd(dest, value, original_value);
return original_value;
}
// ...
int original_value = atomic_add(buffer[buffer_idx], value);
I'm pretty sure the above is a bug if HLSL is meant to follow the same "value-return" convention as GLSL where inout means we copy the value of the argument to the parameter and back upon a function return, so in SSA form you should get something like that emitted:
uint param_1 = arr[invIdx];
uint param_2 = toAdd;
// block of code that is an inlined function call
uint ret_0 = atomicAdd(param_1,param2);
uint ret_1 = param_1; // because of `inout`
param_1 = ret_1;
which is how SPIRV-Cross would emit its code.
Somehow because the your SPIR-V optimizer/legalizer throws away the redundant loads and stores which would break atomicity.
Yea, the SPIR-V code generator currently generates function call arguments incorrectly (but so does the DXIL generator just in different contexts). I'm working on a change to fix that, but it is proving complicated.
devshgraphicsprogramming commented on Aug 30, 2023
Yea, the SPIR-V code generator currently generates function call arguments incorrectly (but so does the DXIL generator just in different contexts). I'm working on a change to fix that, but it is proving complicated.
btw I'm looking into something interesting microsoft/hlsl-specs#59 (comment)
basically does [[vk::ext_reference]] have any effect on a regular (without [[vk::ext_instruction]] attribute) function's argument
I mean the compiler definitely doesn't complain (warning or error) about lack of [[vk::ext_instruction]] on the argument's function, but does it then silently ignore or actually do a reference?
I'd rather write my hack code with [[vk::ext_reference]] rather than relying on inout's broken codegen if possible.
Activity
llvm-beanz commentedon Jun 30, 2023
HLSL does not support reference types.
inoutandoutparameters are value-like semantics so they will not work as you expect in this case.We are tracking adding references to the language in this proposal
devshgraphicsprogramming commentedon Jul 27, 2023
could using
vk::ext_referenceon a non-spirv intrinsic function work here?https://github.com/microsoft/DirectXShaderCompiler/wiki/GL_EXT_spirv_intrinsics-for-SPIR-V-code-gen
devshgraphicsprogramming commentedon Jul 27, 2023
Actually looks like
inoutactually acts like a reference and constructs the correctOpAccessChainhttps://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:hlsl,selection:(endColumn:2,endLineNumber:28,positionColumn:1,positionLineNumber:1,selectionStartColumn:2,selectionStartLineNumber:28,startColumn:1,startLineNumber:1),source:'template%3Ctypename+T%3E%0A%5B%5Bvk::ext_instruction(234)%5D%5D%0AT+atomicAdd(%5B%5Bvk::ext_reference%5D%5D+T+ptr,+uint+memoryScope,+uint+memorySemantics,+T+value)%3B%0A%0Anamespace+nbl%0A%7B%0Anamespace+hlsl%0A%7B%0Anamespace+glsl_compat%0A%7B%09%0Atemplate%3Ctypename+T%3E%0AT+atomicAdd(inout+T+ptr,+T+value)%0A%7B%0A%09return+::atomicAdd(ptr,+2,+0x8,+value)%3B%0A%7D%0A%0A%7D%0A%7D%0A%7D%0A%0Agroupshared+uint+arr%5B512%5D%3B%0A%0A%5Bnumthreads(32,1,1)%5D%0Avoid+main(uint+invIdx+:+SV_GroupIndex)+%7B%0A++++arr%5BinvIdx%5D+%3D+invIdx%3B%0A++++uint+toAdd+%3D+invIdx%3B%0A++++uint+orig+%3D+nbl::hlsl::glsl_compat::atomicAdd(arr%5BinvIdx%5D,+toAdd)%3B%0A%7D'),l:'5',n:'0',o:'HLSL+source+%231',t:'0')),k:51.41242937853108,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:dxc_trunk,deviceViewOpen:'1',filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:hlsl,libs:!(),options:'-spirv+-T+cs_6_6+-HV+2021+-fspv-target-env%3Dvulkan1.2',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+DXC+(trunk)+(Editor+%231)',t:'0')),k:45.821240249559345,l:'4',m:42.49850567842199,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'DXC+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+DXC+(trunk)+(Compiler+%231)',t:'0')),header:(),l:'4',m:57.50149432157801,n:'0',o:'',s:0,t:'0')),k:48.58757062146892,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4
devshgraphicsprogramming commentedon Jul 27, 2023
I'm pretty sure the above is a bug if HLSL is meant to follow the same "value-return" convention as GLSL where
inoutmeans we copy the value of the argument to the parameter and back upon a function return, so in SSA form you should get something like that emitted:which is how SPIRV-Cross would emit its code.
Somehow because the your SPIR-V optimizer/legalizer throws away the redundant loads and stores which would break atomicity.
llvm-beanz commentedon Jul 27, 2023
Yea, the SPIR-V code generator currently generates function call arguments incorrectly (but so does the DXIL generator just in different contexts). I'm working on a change to fix that, but it is proving complicated.
devshgraphicsprogramming commentedon Aug 30, 2023
btw I'm looking into something interesting
microsoft/hlsl-specs#59 (comment)
basically does
[[vk::ext_reference]]have any effect on a regular (without[[vk::ext_instruction]]attribute) function's argumentI mean the compiler definitely doesn't complain (warning or error) about lack of
[[vk::ext_instruction]]on the argument's function, but does it then silently ignore or actually do a reference?I'd rather write my hack code with
[[vk::ext_reference]]rather than relying oninout's broken codegen if possible.