From 487ae034e2b03ddd67945132c8fecbd937952705 Mon Sep 17 00:00:00 2001 From: Sriram Murali <85252063+sriramm-nv@users.noreply.github.com> Date: Mon, 13 May 2024 23:57:57 -0700 Subject: Add LoadAligned and StoreAligned methods to ByteAddressBuffers (#4066) Fixes #4062 This change enables wide load/stores for byte-address-buffer backed resources, when the data is accessed at an offset that is aligned. **Goals** - Improve performance by issuing wider instructions instead of sequence of scalar instructions, for load and stores of byte-address buffers. - Reduce code-size and readability of the generated shaders. - Help naive users as well as ninja programmers, generate optimal code. **Non Goals** - Help with Structured buffers, or other resources. - Target compilation time improvements. **Key changes** Adds 2 new overloads for Load and Store operations on ByteAddress Buffers. 1. Load / Store with an extra alignment parameter ``` resource.Load(offset, alignment); resource.Store(offset, value, alignment); ``` 2. LoadAligned / StoreAligned with no extra parameter, with the same signature as orignial Load / Store. ``` resource.LoadAligned(offset); resource.StoreAligned(offset, value); ``` - This overload will implicitly identify the alignment value, from the base type T of the elementary unit of the resource. **Supported resources** 1. Vectors This can be upto 4 elements, i.e. float -- float4. 2. Arrays This does not have a limit on number of elements, but on a conservative estimate, we can limit to few hundreds. 3. Structures This is used to group a resource of a single type. ``` struct { float4 x; } ``` **Code updates** - Modified byte-address-ir legalize to handle struct, array and vector kinds of load or store access - Added custom hlsl stdlib functions to implement all the overloads for Load, Store etc. - Added C-like emitter, SPIR-V emitter for handling ByteAddressBuffers. - Added a new core stdlib function intrinsic to wrap around alignOf(). - Added a new peephole optimization entry to identify the equivalent IntLiteral value from the alignOf() inst. - Added tests to check explicit, and implicit aligned Load and Store operations. --- source/slang/slang-emit-c-like.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'source/slang/slang-emit-c-like.cpp') diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index b44fa677c..19a7930f6 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -2674,6 +2674,7 @@ void CLikeSourceEmitter::defaultEmitInstExpr(IRInst* inst, const EmitOpInfo& inO break; case kIROp_ByteAddressBufferLoad: + { m_writer->emit("("); emitOperand(inst->getOperand(0), getInfo(EmitOp::General)); m_writer->emit(").Load<"); @@ -2682,20 +2683,21 @@ void CLikeSourceEmitter::defaultEmitInstExpr(IRInst* inst, const EmitOpInfo& inO emitOperand(inst->getOperand(1), getInfo(EmitOp::General)); m_writer->emit(")"); break; + } case kIROp_ByteAddressBufferStore: - { - auto prec = getInfo(EmitOp::Postfix); - needClose = maybeEmitParens(outerPrec, prec); + { + auto prec = getInfo(EmitOp::Postfix); + needClose = maybeEmitParens(outerPrec, prec); - emitOperand(inst->getOperand(0), leftSide(outerPrec, prec)); - m_writer->emit(".Store("); - emitOperand(inst->getOperand(1), getInfo(EmitOp::General)); - m_writer->emit(","); - emitOperand(inst->getOperand(2), getInfo(EmitOp::General)); - m_writer->emit(")"); - } + emitOperand(inst->getOperand(0), leftSide(outerPrec, prec)); + m_writer->emit(".Store("); + emitOperand(inst->getOperand(1), getInfo(EmitOp::General)); + m_writer->emit(","); + emitOperand(inst->getOperand(inst->getOperandCount() - 1), getInfo(EmitOp::General)); + m_writer->emit(")"); break; + } case kIROp_PackAnyValue: { m_writer->emit("packAnyValue<"); -- cgit v1.2.3