From f7f0dcadd3b2aca4c0bcd03a96e11c617cf69fc2 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:21:29 -0400 Subject: Adding a differentiable standard library (#2465) --- source/slang/slang-lower-to-ir.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 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 1e58a456e..ff66caa90 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -8204,12 +8204,28 @@ struct DeclLoweringVisitor : DeclVisitor getBuilder()->addDecoration(irFunc, kIROp_ForceInlineDecoration); } + // Register the value now, to avoid any possible infinite recursion when lowering CustomJVPAttribute + setGlobalValue(context, decl, LoweredValInfo::simple(findOuterMostGeneric(irFunc))); + if (auto attr = decl->findModifier()) { - auto loweredVal = lowerLValueExpr(this->context, attr->funcDeclRef); + // TODO(Sai): HACK.. we need to emit a decl-ref to handle this modifier correctly. + // If we don't move the cursor to the parent, we sometimes emit supporting + // insts into the function body, which shouldn't happen. + // + subContext->irBuilder->setInsertInto(irFunc->getParent()); + + auto diffFuncType = getFuncType(subContext->astBuilder, attr->funcDeclRef->declRef.as()); + auto irDiffFuncType = lowerType(subContext, diffFuncType); + + auto loweredVal = emitDeclRef(subContext, attr->funcDeclRef->declRef, irDiffFuncType); + SLANG_ASSERT(loweredVal.flavor == LoweredValInfo::Flavor::Simple); - IRFunc* jvpFunc = as(loweredVal.val); + IRInst* jvpFunc = loweredVal.val; getBuilder()->addDecoration(irFunc, kIROp_JVPDerivativeReferenceDecoration, jvpFunc); + + // Reset cursor. + subContext->irBuilder->setInsertInto(irFunc); } // For convenience, ensure that any additional global -- cgit v1.2.3