diff options
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/slang-ir-autodiff-transpose.h | 8 | ||||
| -rw-r--r-- | source/slang/slang-ir-autodiff-unzip.h | 16 |
2 files changed, 23 insertions, 1 deletions
diff --git a/source/slang/slang-ir-autodiff-transpose.h b/source/slang/slang-ir-autodiff-transpose.h index 843612df9..739ce6553 100644 --- a/source/slang/slang-ir-autodiff-transpose.h +++ b/source/slang/slang-ir-autodiff-transpose.h @@ -1560,6 +1560,14 @@ struct DiffTransposePass TranspositionResult transposeStore(IRBuilder* builder, IRStore* fwdStore, IRInst*) { IRInst* revVal = builder->emitLoad(fwdStore->getPtr()); + + auto primalType = tryGetPrimalTypeFromDiffInst(fwdStore->getVal()); + SLANG_ASSERT(primalType); + + // Clear the value at the differential address, by setting to 0. + IRInst* emptyVal = emitDZeroOfDiffInstType(builder, primalType); + builder->emitStore(fwdStore->getPtr(), emptyVal); + if (auto diffPairType = as<IRDifferentialPairType>(revVal->getDataType())) { revVal = builder->emitDifferentialPairGetDifferential( diff --git a/source/slang/slang-ir-autodiff-unzip.h b/source/slang/slang-ir-autodiff-unzip.h index 1d05a8081..2a5d73a01 100644 --- a/source/slang/slang-ir-autodiff-unzip.h +++ b/source/slang/slang-ir-autodiff-unzip.h @@ -340,7 +340,21 @@ struct DiffUnzipPass // the remerged diff pair. auto diffPairType = as<IRDifferentialPairType>(as<IRPtrTypeBase>(arg->getDataType())->getValueType()); auto primalValueType = diffPairType->getValueType(); - auto diffPairRef = diffBuilder->emitReverseGradientDiffPairRef(arg->getDataType(), primalArg, diffArg); + + // We can't simply reuse primalArg for an inout parameter since this will represent the value + // after the primal call which can potentially alter primalArg. Therefore, we will find the + // first store into primalArg, and create a temp var holding that value (i.e. value prior to primal call) + // + auto storeUse = findUniqueStoredVal(cast<IRVar>(primalArg)); + auto storeInst = cast<IRStore>(storeUse->getUser()); + + auto storedVal = storeInst->getVal(); + + // Emit the temp var into the primal blocks since it's holding a primal value. + auto tempPrimalVar = primalBuilder->emitVar(primalValueType); + primalBuilder->emitStore(tempPrimalVar, storedVal); + + auto diffPairRef = diffBuilder->emitReverseGradientDiffPairRef(arg->getDataType(), tempPrimalVar, diffArg); diffBuilder->markInstAsDifferential(diffPairRef, primalValueType); diffArgs.add(diffPairRef); } |
