|
17 | 17 | #define DEBUG_TYPE "differentiation"
|
18 | 18 |
|
19 | 19 | #include "swift/SILOptimizer/Differentiation/Common.h"
|
| 20 | +#include "swift/AST/TypeCheckRequests.h" |
| 21 | +#include "swift/SILOptimizer/Differentiation/ADContext.h" |
20 | 22 |
|
21 | 23 | namespace swift {
|
22 | 24 | namespace autodiff {
|
@@ -244,6 +246,97 @@ void collectMinimalIndicesForFunctionCall(
|
244 | 246 | }));
|
245 | 247 | }
|
246 | 248 |
|
| 249 | +//===----------------------------------------------------------------------===// |
| 250 | +// Diagnostic utilities |
| 251 | +//===----------------------------------------------------------------------===// |
| 252 | + |
| 253 | +SILLocation getValidLocation(SILValue v) { |
| 254 | + auto loc = v.getLoc(); |
| 255 | + if (loc.isNull() || loc.getSourceLoc().isInvalid()) |
| 256 | + loc = v->getFunction()->getLocation(); |
| 257 | + return loc; |
| 258 | +} |
| 259 | + |
| 260 | +SILLocation getValidLocation(SILInstruction *inst) { |
| 261 | + auto loc = inst->getLoc(); |
| 262 | + if (loc.isNull() || loc.getSourceLoc().isInvalid()) |
| 263 | + loc = inst->getFunction()->getLocation(); |
| 264 | + return loc; |
| 265 | +} |
| 266 | + |
| 267 | +//===----------------------------------------------------------------------===// |
| 268 | +// Tangent property lookup utilities |
| 269 | +//===----------------------------------------------------------------------===// |
| 270 | + |
| 271 | +VarDecl *getTangentStoredProperty(ADContext &context, VarDecl *originalField, |
| 272 | + SILLocation loc, |
| 273 | + DifferentiationInvoker invoker) { |
| 274 | + auto &astCtx = context.getASTContext(); |
| 275 | + auto tanFieldInfo = evaluateOrDefault( |
| 276 | + astCtx.evaluator, TangentStoredPropertyRequest{originalField}, |
| 277 | + TangentPropertyInfo(nullptr)); |
| 278 | + // If no error, return the tangent property. |
| 279 | + if (tanFieldInfo) |
| 280 | + return tanFieldInfo.tangentProperty; |
| 281 | + // Otherwise, diagnose error and return nullptr. |
| 282 | + assert(tanFieldInfo.error); |
| 283 | + auto *parentDC = originalField->getDeclContext(); |
| 284 | + assert(parentDC->isTypeContext()); |
| 285 | + auto parentDeclName = parentDC->getSelfNominalTypeDecl()->getNameStr(); |
| 286 | + auto fieldName = originalField->getNameStr(); |
| 287 | + auto sourceLoc = loc.getSourceLoc(); |
| 288 | + switch (tanFieldInfo.error->kind) { |
| 289 | + case TangentPropertyInfo::Error::Kind::NoDerivativeOriginalProperty: |
| 290 | + llvm_unreachable( |
| 291 | + "`@noDerivative` stored property accesses should not be " |
| 292 | + "differentiated; activity analysis should not mark as varied"); |
| 293 | + case TangentPropertyInfo::Error::Kind::NominalParentNotDifferentiable: |
| 294 | + context.emitNondifferentiabilityError( |
| 295 | + sourceLoc, invoker, |
| 296 | + diag::autodiff_stored_property_parent_not_differentiable, |
| 297 | + parentDeclName, fieldName); |
| 298 | + break; |
| 299 | + case TangentPropertyInfo::Error::Kind::OriginalPropertyNotDifferentiable: |
| 300 | + context.emitNondifferentiabilityError( |
| 301 | + sourceLoc, invoker, diag::autodiff_stored_property_not_differentiable, |
| 302 | + parentDeclName, fieldName, originalField->getInterfaceType()); |
| 303 | + break; |
| 304 | + case TangentPropertyInfo::Error::Kind::ParentTangentVectorNotStruct: |
| 305 | + context.emitNondifferentiabilityError( |
| 306 | + sourceLoc, invoker, diag::autodiff_stored_property_tangent_not_struct, |
| 307 | + parentDeclName, fieldName); |
| 308 | + break; |
| 309 | + case TangentPropertyInfo::Error::Kind::TangentPropertyNotFound: |
| 310 | + context.emitNondifferentiabilityError( |
| 311 | + sourceLoc, invoker, |
| 312 | + diag::autodiff_stored_property_no_corresponding_tangent, parentDeclName, |
| 313 | + fieldName); |
| 314 | + break; |
| 315 | + case TangentPropertyInfo::Error::Kind::TangentPropertyWrongType: |
| 316 | + context.emitNondifferentiabilityError( |
| 317 | + sourceLoc, invoker, diag::autodiff_tangent_property_wrong_type, |
| 318 | + parentDeclName, fieldName, tanFieldInfo.error->getType()); |
| 319 | + break; |
| 320 | + case TangentPropertyInfo::Error::Kind::TangentPropertyNotStored: |
| 321 | + context.emitNondifferentiabilityError( |
| 322 | + sourceLoc, invoker, diag::autodiff_tangent_property_not_stored, |
| 323 | + parentDeclName, fieldName); |
| 324 | + break; |
| 325 | + } |
| 326 | + return nullptr; |
| 327 | +} |
| 328 | + |
| 329 | +VarDecl *getTangentStoredProperty(ADContext &context, |
| 330 | + FieldIndexCacheBase *projectionInst, |
| 331 | + DifferentiationInvoker invoker) { |
| 332 | + assert(isa<StructExtractInst>(projectionInst) || |
| 333 | + isa<StructElementAddrInst>(projectionInst) || |
| 334 | + isa<RefElementAddrInst>(projectionInst)); |
| 335 | + auto loc = getValidLocation(projectionInst); |
| 336 | + return getTangentStoredProperty(context, projectionInst->getField(), loc, |
| 337 | + invoker); |
| 338 | +} |
| 339 | + |
247 | 340 | //===----------------------------------------------------------------------===//
|
248 | 341 | // Code emission utilities
|
249 | 342 | //===----------------------------------------------------------------------===//
|
|
0 commit comments