From 366a947cdf2e3c6958b7a9e17d561ce76ab0f594 Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:18:32 -0400 Subject: Support derivative functions in compute & capabilities adjustments (#4014) * Support derivative functions in compute & capabilities adjustments fixes #4000 PR implements derivative functions in compute shaders properly so we have the functionality for SPIR-V & GLSL. Tests reflect fragment and compute paths. PR also adjusts capabilities to correct wrong SPRI-V target capabilities for when using textures. Remarks: 1. __requireComputeDerivative(); is a intrinsic_op and not modifier since inlining will destroy the modifier. 2. Derivative mode is tied to an entry point decoration `[DerivativeGroupQuad]`/`[DerivativeGroupLinear]` or GLSL syntax ``derivative_group_linearNV`. Default is to set the mode to `[DerivativeGroupQuad]` * remove -emit-spirv-directly * fixes 1. fix minor issue fwidth change where I returned the wrong type 2. fix issue where glslang{glsl->spirv} is wrong, so we don't run that test and just run the glsl test & direct spir-v test for intrinsic-texture.slang * adjust as per review and refine code 1. add test to ensure multi-diverging-in-logic entry points work -- 2 functions which may cause computeDerivatives + 1 that uses, 1 that does not. 2. naming 3. use entry point ref graph for c-like-targets 4. reordered some code to util's and removed `static linline` since that was just for ease of coding on my end (should not have been pushed). * Grammer * split up source file + issolate GLSL emit path change. --------- Co-authored-by: Yong He --- source/slang/slang-lower-to-ir.cpp | 83 +++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 14 deletions(-) (limited to 'source/slang/slang-lower-to-ir.cpp') diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index a600963a7..e4fc33e33 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -12,6 +12,7 @@ #include "slang-ir-bit-field-accessors.h" #include "slang-ir-loop-inversion.h" #include "slang-ir.h" +#include "slang-ir-util.h" #include "slang-ir-constexpr.h" #include "slang-ir-dce.h" #include "slang-ir-diff-call.h" @@ -7264,26 +7265,61 @@ struct DeclLoweringVisitor : DeclVisitor #undef IGNORED_CASE + void getAllEntryPointsNoOverride(List& entryPoints) + { + if(entryPoints.getCount() != 0 ) + return; + for(const auto d : context->irBuilder->getModule()->getModuleInst()->getGlobalInsts()) + if(d->findDecoration()) + entryPoints.add(d); + } + LoweredValInfo visitEmptyDecl(EmptyDecl* decl) { + bool verifyComputeDerivativeGroupModifier = false; + List entryPoints {}; for(const auto modifier : decl->modifiers) { if(const auto layoutLocalSizeAttr = as(modifier)) { - for(const auto d : context->irBuilder->getModule()->getModuleInst()->getGlobalInsts()) - { - if(d->findDecoration()) - { - getBuilder()->addNumThreadsDecoration( + verifyComputeDerivativeGroupModifier = true; + getAllEntryPointsNoOverride(entryPoints); + for(auto d : entryPoints) + as(getBuilder()->addNumThreadsDecoration( d, getSimpleVal(context, lowerVal(context, layoutLocalSizeAttr->x)), getSimpleVal(context, lowerVal(context, layoutLocalSizeAttr->y)), getSimpleVal(context, lowerVal(context, layoutLocalSizeAttr->z)) - ); - } - } + )); + } + else if(as(modifier)) + { + verifyComputeDerivativeGroupModifier = true; + getAllEntryPointsNoOverride(entryPoints); + for(auto d : entryPoints) + getBuilder()->addSimpleDecoration(d); + } + else if(as(modifier)) + { + verifyComputeDerivativeGroupModifier = true; + getAllEntryPointsNoOverride(entryPoints); + for(auto d : entryPoints) + getBuilder()->addSimpleDecoration(d); } } + + if(!verifyComputeDerivativeGroupModifier) + return LoweredValInfo(); + for(auto d : entryPoints) + { + verifyComputeDerivativeGroupModifiers( + getSink(), + decl->loc, + d->findDecoration(), + d->findDecoration(), + d->findDecoration()); + } + return LoweredValInfo(); } @@ -9677,6 +9713,9 @@ struct DeclLoweringVisitor : DeclVisitor addBitFieldAccessorDecorations(irFunc, decl); + IRNumThreadsDecoration* numThreadsDecor = nullptr; + IRDecoration* derivativeGroupQuadDecor = nullptr; + IRDecoration* derivativeGroupLinearDecor = nullptr; for (auto modifier : decl->modifiers) { if (as(modifier)) @@ -9691,6 +9730,14 @@ struct DeclLoweringVisitor : DeclVisitor { getBuilder()->addSimpleDecoration(irFunc); } + else if (auto derivativeGroupQuadMod = as(modifier)) + { + derivativeGroupQuadDecor = getBuilder()->addSimpleDecoration(irFunc); + } + else if (auto derivativeGroupLinearMod = as(modifier)) + { + derivativeGroupLinearDecor = getBuilder()->addSimpleDecoration(irFunc); + } else if (auto instanceAttr = as(modifier)) { IRIntLit* intLit = _getIntLitFromAttribute(getBuilder(), instanceAttr); @@ -9703,12 +9750,13 @@ struct DeclLoweringVisitor : DeclVisitor } else if (auto numThreadsAttr = as(modifier)) { - getBuilder()->addNumThreadsDecoration( - irFunc, - getSimpleVal(context, lowerVal(context, numThreadsAttr->x)), - getSimpleVal(context, lowerVal(context, numThreadsAttr->y)), - getSimpleVal(context, lowerVal(context, numThreadsAttr->z)) - ); + numThreadsDecor = as( + getBuilder()->addNumThreadsDecoration( + irFunc, + getSimpleVal(context, lowerVal(context, numThreadsAttr->x)), + getSimpleVal(context, lowerVal(context, numThreadsAttr->y)), + getSimpleVal(context, lowerVal(context, numThreadsAttr->z)) + )); } else if (auto waveSizeAttr = as(modifier)) { @@ -9862,6 +9910,13 @@ struct DeclLoweringVisitor : DeclVisitor getBuilder()->addDecoration(irFunc, kIROp_NonDynamicUniformReturnDecoration); } + verifyComputeDerivativeGroupModifiers( + getSink(), + decl->loc, + derivativeGroupQuadDecor, + derivativeGroupLinearDecor, + numThreadsDecor); + if (!isInline) { // If there are any constant expr rate parameters, we should inline this function. -- cgit v1.2.3