Repository navigation
Description
I'm unsure whether this should be opened here or in https://github.com/microsoft/hlsl-specs because the attribute already exists and can be applied to any function argument without the compiler complaining (no diagnostics emitted).
Is your feature request related to a problem? Please describe.
Unless something changed w.r.t vk::ext_reference in microsoft/hlsl-specs#59 then after the merge of #5249 it will become impossible for me to implement certain load types and atomics if I want to "default" certain OpCode operands, for example.
template<typename T>
[[vk::ext_instruction(/* OpLoad */ 61)]]
T special_load([[vk::ext_reference]] T arg, [[vk::ext_literal]] int aligned, , [[vk::ext_literal]] int alignment, [[vk::ext_literal]] int memoryOperand);
template<typename T,>
T volatile_load(inout T myref)
{
return special_load(myref,/*Aligned*/0x2,Alignment,/*Volatile*/0x1);
}Currently it works beautifully on RWStructuredBuffer( https://godbolt.org/z/1j3hczKcT ), groupshared ( https://godbolt.org/z/9qso3Y7eW ) and might even work with the new current implementation of BDA with vk::BufferPointer<T,A>::Get().
However this relies on inout working as a reference, which is a bug (is contrary to the HLSL spec, and this will be fixed in #5249).
One that behaviour of inout goes away, the only way to achieve this is to replace any function in the call chain with a Preprocessor Macro Function which copy-pastes a scope with block of code. This brings us back to the feature parity with GLSL as:
- the macro cannot be in a namespace
- the macro is a free floating function (you cannot make it into a struct method)
- the macro cannot return a value, has to behave like a void function
- now every argument/parameter is a reference
- no ability to use
returnwithin the macro (would need to wrap it in a weirddo-while(false)and usecontinueinstead ofreturn) - possible variable shadowing as its just code copy-paste
TL;DR Its possible to work around, but leads to completely unmaintainable code.
This is especially important for things like atomic operations and atomic load/store which depending on the Vulkan environment you're targetting (with or without Vulkan Memory Model) you'd want to differentiate and "hide"/default some of the ext_literal operands.
Other DXC users have ran into this before ( #5077 ) when attempting to shim GLSL-compatible code, albeit without SPIR-V intrinsics like I'm doing.
Describe the solution you'd like
The ability to mark arguments in the the non-intrinsic functions (i.e. volatile_load and `` in the examples above).
Describe alternatives you've considered
Waiting for HLSL proposal 0006 to go under review, get approved, implemented in DXC.
Additional context
Currently inout works like a reference "by accident" but this behaviour will go away after #5377 gets merged.
Actually Alexander Sannikov Raikiri (working on Path of Exile) mentioned to me that they were "considering writing our code generator that automatically inlines all our shader functions before compiling them, just to avoid relying on inout being sane" and his definition of "sane" was for inout to act as a reference.
However as we know both HLSL and GLSL use "value return" calling conventions and inout is specified to be exactly "copy-in copy-out".
From my limited knowledge and investigations, it does seem that as long as spirv-opt and DXC's own DXIL opimizers work properly and after optimization (inlining and temporary store elimination) a true reference vs "copy-in copy-out" shouldn't differ at all in code emitted... unless we're dealing with temporal effects like volatile reads/writes and atomics.
However my example & problem clearly demonstrate the need for references and atomic/volatile access.
This touches microsoft/hlsl-specs#83
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriaged
Activity
[-][Feature Request] Ability to use `[vk::ext_reference]` attribute on any function argument to indicate to treat is a reference[/-][+][Feature Request] Ability to use `[vk::ext_reference]` attribute on any function (not just inline SPIR-V) argument to indicate to treat is a reference[/+]s-perron commentedon Sep 25, 2023
Sounds good we will try to not remove this functionality. To make sure we do not, we need to add a test. Once we add a test, we can consider this closed.
s-perron commentedon Sep 26, 2023
Below is a full test for what I expect is wanted. It does not currently pass. The
vk::ext_referenceis ignored on the call to SequentiallyConsistentAtomicIncrement, so the atomic operation ends up being on the temporary that is created. I can see why this is desirable.However, this is out of scope for what was expected from vk::ext_reference originally. The fact that it was silently ignored instead of giving an error is more of an oversight.
From the initial description, I thought that
vk::ext_referencewas already doing what we wanted it to do, which is why I suggested adding a test. I would accept a patch that make this work the way that Mateusz Kielan (@devshgraphicsprogramming) wants it to, but it is not a priority for the maintainers at this time.devshgraphicsprogramming commentedon Sep 29, 2023
we can assign someone and implement it
devshgraphicsprogramming commentedon Oct 21, 2023
Actually there's another thing I noticed I can do which is pretty nasty
Neither SPIR-V or DXIL output know what to do with a reference type though.
But its useful enough to template on (type_traits), so things like
alignment_of<T>::valuework nicely.I just need to remember to not attempt to use a reference type as a variable in an evaluated context.
devshgraphicsprogramming commentedon May 6, 2024
I'm actualy trying to implement this with Proposal 0011 #6578
devshgraphicsprogramming commentedon May 16, 2024
I'm going to close this, but I'd appreciate #6578 getting reopened because there's no way to "link up" the inline SPIR-V
vk::SpirvTypelike BDA, or CombinedSamplerImages that need to refer to a HLSL "regular" type.#6578 (comment)
A
vk::__spirvtype(type)to obtain a symbolic%result_idof a declaration HLSL codegen will create in the future would be indispensable.And because
OpBitcastshouldn't be used to cast a pointer to the same type (not legal SPIR-V the types need to be different as per the spec) an intrinsic to getvk::SpirvType<> refof([[vk::ext_reference]] T)is necessary to link up inline spir-v code properly,vk::ext_referenceisn't enough for some usages (BDA emulation, manual OpAccessChains)Note: a
vk::__spirvtype(expr)might be necessary because a typeTdeclated in HLSL may exist in the SPIR-V codegen in multiple versions depending on member decorations and storage class.implement microsoft#5675