From 2db8c15c04f2aade49636e42f0adee636afb3b73 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Fri, 5 Aug 2022 13:19:20 -0400 Subject: Added a new differential type system and various improvements (#2343) * Merge slang-ir-diff-jvp.cpp * Added support and tests for other float vector types * Added swizzle test and code to handle it (tests failing currently) * Fixed one test, the other is still pending * Fixed instruction cloning logic to avoid modifying original function * Fixed an issue with custom 'pow_jvp' and added support for vector contructor * Minor update to comments * Fixed support for division * Fixed an issue with uninitialized diagnostic sink * Moved derivative processing to after mandatory inlining. Skip instructions that don't have side-effects and aren't used by anything. * WIP: Handling unconditional control flow and multi-block functions * Support for unconditional multi-block functions * Added a dead code elimination step to the derivative pass * Changed name of 'hasNoSideEffects()' * Refactored variable names * Added initial IR defs for new type system * Added necessary logic for semantic checking * Overhauled type system to use builtin pair types and conform to the IDifferentiable interface * Automatically replace IRDifferentiablePairType to a custom IRStructType * Added generics handling by expanding the conformance context functionality and allowing for type parameters * Minor fix: early return in processPairTypes() * Minor fixes to differentiable resolution on generic types * Added new instructions for differential pairs. Basic tests work now. Looking into generic types. * Adjusted most tests to the new type system. OutType and InOutType are still not properly working. * Updated __jvp to produce both primal and differential output * Moved autodiff related declarations to diff.meta.slang * Refactored variable names * Added initial IR defs for new type system * Added necessary logic for semantic checking * Overhauled type system to use builtin pair types and conform to the IDifferentiable interface * Automatically replace IRDifferentiablePairType to a custom IRStructType * Added generics handling by expanding the conformance context functionality and allowing for type parameters * Minor fix: early return in processPairTypes() * Minor fixes to differentiable resolution on generic types * Added new instructions for differential pairs. Basic tests work now. Looking into generic types. * Adjusted most tests to the new type system. OutType and InOutType are still not properly working. * Updated __jvp to produce both primal and differential output * Moved autodiff related declarations to diff.meta.slang * Removed external changes * Cleanup the transcription logic: each case returns a pair of insts for the primal and differential computation. --- source/slang/slang-ast-builder.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-ast-builder.cpp') diff --git a/source/slang/slang-ast-builder.cpp b/source/slang/slang-ast-builder.cpp index 7f23f5492..868763f76 100644 --- a/source/slang/slang-ast-builder.cpp +++ b/source/slang/slang-ast-builder.cpp @@ -265,7 +265,7 @@ VectorExpressionType* ASTBuilder::getVectorType( IntVal* elementCount) { auto vectorGenericDecl = as(m_sharedASTBuilder->findMagicDecl("Vector")); - + auto vectorTypeDecl = vectorGenericDecl->inner; auto substitutions = create(); @@ -278,6 +278,30 @@ VectorExpressionType* ASTBuilder::getVectorType( return as(DeclRefType::create(this, declRef)); } +DifferentialPairType* ASTBuilder::getDifferentialPairType(Type* valueType, Witness* conformanceWitness) +{ + auto genericDecl = dynamicCast(m_sharedASTBuilder->findMagicDecl("DifferentialPairType")); + + auto typeDecl = genericDecl->inner; + + auto substitutions = create(); + substitutions->genericDecl = genericDecl; + substitutions->args.add(valueType); + substitutions->args.add(conformanceWitness); + + auto declRef = DeclRef(typeDecl, substitutions); + auto rsType = DeclRefType::create(this, declRef); + + return as(rsType); +} + +DeclRef ASTBuilder::getDifferentiableInterface() +{ + DeclRef declRef; + declRef.decl = dynamicCast(m_sharedASTBuilder->findMagicDecl("DifferentiableType")); + return declRef; +} + DeclRef ASTBuilder::getBuiltinDeclRef(const char* builtinMagicTypeName, ConstArrayView genericArgs) { DeclRef declRef; -- cgit v1.2.3