From a6deb5ed82cb8fc6b4f4c5c5fee264e09f97ff89 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 29 Sep 2025 17:45:08 -0700 Subject: Rewriting the lower-buffer-element-type pass to avoid unnecessary packing/unpacking. (#8526) Part of the effort to improve the performance of generated SPIRV code. The existing lower-buffer-element-type pass works by loading the entire buffer element content from memory, and translate it to logical type stored in a local variable at the earliest reference of a buffer handle. This means that is can generate inefficient code that reads more than necessary. Consider this example: ``` struct BigStruct { bool values[1024]; } ConstantBuffer cb; void test(BigStruct v) { if (v.values[0]) { printf("ok"); } } [numthreads(1,1,1)] void computeMain() { test(cb); } ``` In IR, the `computeMain` function before lower-buffer-element-type pass is something like following: ``` func test: %v = param : BigStruct %barr = fieldExtract(%v, "values") %element = elementExtract(%barr, 0) ... // uses %element func computeMain: %v = load(cb) call %test %v ``` The existing lower-buffer-element-type pass will rewrite the bool array in `BigStruct` into `int` array so it is legal in SPIRV. However, it does so by inserting the translation on the first `load` of the constant buffer: ``` struct BigStruct_std430 { int values[1024]; } var cb : ConstantBuffer; func computeMain: %tmpVar : var call %unpackStorage(%tmpVar, cb) %v : BigStruct = load %tmpVar call %test %v ``` This means that the entire array will be loaded and translated to int, before calling `test`, which only uses one element. It turns out that the downstream compiler isn't always able to optimize out this inefficient translation/copy. This PR completely rewrites the way buffer-element-type lowering is handled to avoid producing this inefficient code. It works in two parts: first we turn on the `transformParamsToConstRef` pass for SPIRV target as well, so we will translate the `test` function to take the `v` parameter as `constref`. The second part is a redesigned buffer-element-type pass that defers the storage-type to logical-type translation until a value is actually used by a `load` instruction. In this example, after `transformParamsToConstRef`, the IR is: ``` func test: %v = param : ConstRef %barr = fieldAddr(%v, "values") %elementPtr = elementAddr(%barr, 0) %element = load(%elementPtr) ... // uses %element func computeMain: call %test %cb ``` The new `buffer-element-type-lowering` pass will take this IR, and insert translation at latest possible time across the entire call graph, and translate the IR into: ``` func test: %v = param : ConstRef %barr = fieldAddr(%v, "values") %elementPtr : ptr = elementAddr(%barr, 0) %element_int = load(%elementPtr) %element = cast(%element_int) : %bool ... // uses %element func computeMain: call %test %cb ``` In this new IR, there is no longer a load and conversion of the entire array. See new comment in `slang-ir-lower-buffer-element-type.cpp` for more details of how the pass works. This PR also address many other issues surfaced by turning on `transformParamsToConstRef` pass on SPIRV backend. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-ir-util.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'source/slang/slang-ir-util.cpp') diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 9b852b803..8584ea95e 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -2128,6 +2128,40 @@ IRType* getIRVectorBaseType(IRType* type) return as(type)->getElementType(); } +IRType* getElementType(IRBuilder& builder, IRType* valueType) +{ + valueType = (IRType*)unwrapAttributedType(valueType); + if (auto arrayType = as(valueType)) + { + return arrayType->getElementType(); + } + else if (auto vectorType = as(valueType)) + { + return vectorType->getElementType(); + } + else if (auto basicType = as(valueType)) + { + return basicType; + } + else if (auto coopVecType = as(valueType)) + { + return coopVecType->getElementType(); + } + else if (auto matrixType = as(valueType)) + { + return builder.getVectorType(matrixType->getElementType(), matrixType->getColumnCount()); + } + else if (auto coopMatType = as(valueType)) + { + return coopMatType->getElementType(); + } + else if (auto hlslInputPatchType = as(valueType)) + { + return hlslInputPatchType->getElementType(); + } + return nullptr; +} + Int getSpecializationConstantId(IRGlobalParam* param) { auto layout = findVarLayout(param); @@ -2483,4 +2517,47 @@ bool isIROpaqueType(IRType* type) } } +bool isPointerToImmutableLocation(IRInst* loc) +{ + switch (loc->getOp()) + { + case kIROp_GetStructuredBufferPtr: + case kIROp_ImageSubscript: + return isPointerToImmutableLocation(loc->getOperand(0)); + default: + break; + } + + auto type = loc->getDataType(); + if (!type) + return false; + + switch (type->getOp()) + { + case kIROp_HLSLStructuredBufferType: + case kIROp_HLSLByteAddressBufferType: + case kIROp_ConstantBufferType: + case kIROp_ParameterBlockType: + return true; + default: + break; + } + + if (auto textureType = as(type)) + return textureType->getAccess() == SLANG_RESOURCE_ACCESS_READ; + + if (auto ptrType = as(type)) + { + switch (ptrType->getAddressSpace()) + { + case AddressSpace::BuiltinInput: + case AddressSpace::Input: + case AddressSpace::MetalObjectData: + case AddressSpace::Uniform: + case AddressSpace::UniformConstant: + return true; + } + } + return false; +} } // namespace Slang -- cgit v1.2.3