Skip to content

Reference argument for atomic operation #5077

Closed

Description

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);

Is this possible in HLSL?

Activity

  1. llvm-beanz commented on Jun 30, 2023

    @llvm-beanz
    Collaborator

    HLSL does not support reference types. inout and out parameters 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

  2. devshgraphicsprogramming commented on Jul 27, 2023

    @devshgraphicsprogramming

    Actually looks like inout actually acts like a reference and constructs the correct OpAccessChain

    https://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

  3. devshgraphicsprogramming commented on Jul 27, 2023

    @devshgraphicsprogramming

    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.

  4. llvm-beanz commented on Jul 27, 2023

    @llvm-beanz
    Collaborator

    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.

  5. devshgraphicsprogramming commented on Aug 30, 2023

    @devshgraphicsprogramming

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions