fix corner case for optional aliasing (#18093)
authorMichael Suo <suo@fb.com>
Sun, 17 Mar 2019 21:53:41 +0000 (14:53 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sun, 17 Mar 2019 21:56:40 +0000 (14:56 -0700)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18093
ghimport-source-id: 021adc52aa7bfe5fff74531c76a8cd28cab30b2a

Stack:
* **#18093 [jit] fix corner case for optional aliasing**

Occasionally the compiler can insert constant Nones to make types line
up. In that case, don't try to make a pointer from the optional type to
None, since we know statically that None won't be mutated or whatever.

Reviewed By: shannonzhu

Differential Revision: D14493004

fbshipit-source-id: 6564065f39d99ee5af664f3a0fe235892973d9be

torch/csrc/jit/passes/alias_analysis.cpp

index 8ebec19..f3bbf8a 100644 (file)
@@ -709,6 +709,16 @@ void AliasDb::makePointerTo(const Value* from, const Value* to) {
     return;
   }
 
+  // Special case: if `from` is an optional, `to` could be a None. Don't
+  // create a pointer in that case
+  if (from->type()->kind() == TypeKind::OptionalType &&
+      to->type()->kind() == TypeKind::NoneType) {
+    return;
+  }
+
+  // At this point, we should be dealing with two mutable types.
+  AT_ASSERT(shouldAnnotate(from) && shouldAnnotate(to));
+
   // If either value is a wildcard, don't insert anything into the graph;
   // wildcards are tracked separately since they have different aliasing rules.
   if (isWildcard(to) || isWildcard(from)) {
@@ -1171,6 +1181,9 @@ TORCH_API bool aliasAnalysisHasSpecialCaseFor(Symbol symbol) {
 
 // Register `v` as a wildcard value.
 void AliasDb::setWildcard(const Value* v) {
+  if (!shouldAnnotate(v)) {
+    return;
+  }
   wildcards_.insert(v);
 }