From 368ec3116ea0f10f44acbf76b5dc9e34d6ff3d32 Mon Sep 17 00:00:00 2001 From: Edward Liu Date: Mon, 14 Nov 2022 12:08:01 -0800 Subject: Minimum binary arithmetic reverse autodiff working. (#2514) * Initial plumbing of backward autodiff in the frontend. * More plumbing. * Initial reverse autodiff working. * Bug fixes. * Misc. * Remove redundant code. * More clean up. * Misc. * Rebase and add backward diff test. * Disable test. * Clean up. * Minor fix. Co-authored-by: Yong He --- source/slang/slang-lower-to-ir.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (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 5930875f1..a0158cf38 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -3081,6 +3081,21 @@ struct ExprLoweringVisitorBase : ExprVisitor baseVal.val)); } + // Emit IR to denote the forward-mode derivative + // of the inner func-expr. This will be resolved + // to a concrete function during the derivative + // pass. + LoweredValInfo visitBackwardDifferentiateExpr(BackwardDifferentiateExpr* expr) + { + auto baseVal = lowerSubExpr(expr->baseFunction); + SLANG_ASSERT(baseVal.flavor == LoweredValInfo::Flavor::Simple); + + return LoweredValInfo::simple( + getBuilder()->emitBackwardDifferentiateInst( + lowerType(context, expr->type), + baseVal.val)); + } + LoweredValInfo visitGetArrayLengthExpr(GetArrayLengthExpr* expr) { auto baseVal = lowerSubExpr(expr->arrayExpr); @@ -7799,6 +7814,10 @@ struct DeclLoweringVisitor : DeclVisitor { getBuilder()->addForwardDifferentiableDecoration(irFunc); } + if (decl->findModifier()) + { + getBuilder()->addBackwardDifferentiableDecoration(irFunc); + } if (auto differentialAttr = decl->findModifier()) { lowerDifferentiableAttribute(subContext, irFunc, differentialAttr); -- cgit v1.2.3