{"action":"created","comment":{"url":"https://api.github.com/repos/apple/swift/pulls/comments/241317452","pull_request_review_id":184544291,"id":241317452,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDI0MTMxNzQ1Mg==","diff_hunk":"@@ -1652,6 +1670,77 @@ reapplyFunctionConversion(SILValue newFunc, SILValue oldFunc,\n   llvm_unreachable(\"Unhandled function convertion instruction\");\n }\n \n+/// Emits a reference to a VJP corresponding to `original`, differentiated with\n+/// `indices`. On failure, returns a null `SILValue`.\n+///\n+/// Creates new differentiation tasks, if necessary, using `invoker` as the\n+/// invoker. Calls `taskCallback` for all newly-created tasks (but may also call\n+/// `taskCallback` for already-existing tasks), so that the caller can make sure\n+/// that the task actually gets executed.\n+static SILValue emitVJPReference(\n+    ADContext &context, SILBuilder &builder, SILAutoDiffIndices indices,\n+    SILValue original, DifferentiationInvoker invoker,\n+    std::function<void(DifferentiationTask *)> taskCallback) {\n+  if (auto *originalFRI = findReferenceToVisibleFunction(original)) {\n+    auto loc = originalFRI->getLoc();\n+    auto *originalFn = originalFRI->getReferencedFunction();\n+    auto *task = context.lookUpOrRegisterDifferentiationTask(originalFn,\n+                                                             indices, invoker);\n+    taskCallback(task);\n+    auto *vjpRef = builder.createFunctionRef(loc, task->getVJP());\n+    return reapplyFunctionConversion(vjpRef, originalFRI, original, builder,\n+                                     loc);\n+  }\n+  if (auto *witnessMethod = dyn_cast<WitnessMethodInst>(original)) {\n+    auto loc = witnessMethod->getLoc();\n+    auto requirement = witnessMethod->getMember();\n+    auto *requirementDecl = requirement.getDecl();\n+    auto *diffAttr =\n+        requirementDecl->getAttrs().getAttribute<DifferentiableAttr>();\n+    if (!diffAttr) {\n+      // TODO: If we move diagnostics into this function, then we can tell the\n+      // user that differentiation failed because the requirement is not\n+      // differentiable.\n+      return SILValue();\n+    }\n+\n+    // Check that the requirement indices are the same as the desired indices.\n+    auto *requirementParameterIndices = diffAttr->getCheckedParameterIndices();\n+    auto loweredRequirementIndices = requirementParameterIndices->getLowered(\n+        requirementDecl->getInterfaceType()->castTo<AnyFunctionType>(),\n+        /*isMethod*/ true);\n+    SILAutoDiffIndices requirementIndices(/*source*/ 0, loweredRequirementIndices);\n+\n+    // TODO:\n+    // If we desire a superset of the available indices, then we should emit a\n+    // diagnostic that the requirement can't be differentiated as much as we\n+    // want.\n+    // If we desire a subset of the available indices, then we should\n+    // automatically convert the VJP to a VJP with the desired indices.\n+    assert(requirementIndices == indices &&\n+           \"FIXME: handle the case where the requirement indices are \"\n+           \"different from the desired indices.\");\n+\n+    auto originalType = cast<SILFunctionType>(original->getType().getASTType());","path":"lib/SILOptimizer/Mandatory/TFDifferentiation.cpp","position":96,"original_position":96,"commit_id":"d9aec9b51ca2befc502871d02a8d67e0120b10b9","original_commit_id":"d9aec9b51ca2befc502871d02a8d67e0120b10b9","user":{"login":"rxwei","id":856507,"node_id":"MDQ6VXNlcjg1NjUwNw==","avatar_url":"https://avatars0.githubusercontent.com/u/856507?v=4","gravatar_id":"","url":"https://api.github.com/users/rxwei","html_url":"https://github.com/rxwei","followers_url":"https://api.github.com/users/rxwei/followers","following_url":"https://api.github.com/users/rxwei/following{/other_user}","gists_url":"https://api.github.com/users/rxwei/gists{/gist_id}","starred_url":"https://api.github.com/users/rxwei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rxwei/subscriptions","organizations_url":"https://api.github.com/users/rxwei/orgs","repos_url":"https://api.github.com/users/rxwei/repos","events_url":"https://api.github.com/users/rxwei/events{/privacy}","received_events_url":"https://api.github.com/users/rxwei/received_events","type":"User","site_admin":false},"body":"```suggestion\r\n    auto originalType = original->getType().castTo<SILFunctionType>();\r\n```","created_at":"2018-12-13T09:03:05Z","updated_at":"2018-12-13T09:09:02Z","html_url":"https://github.com/apple/swift/pull/21286#discussion_r241317452","pull_request_url":"https://api.github.com/repos/apple/swift/pulls/21286","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/comments/241317452"},"html":{"href":"https://github.com/apple/swift/pull/21286#discussion_r241317452"},"pull_request":{"href":"https://api.github.com/repos/apple/swift/pulls/21286"}}},"pull_request":{"url":"https://api.github.com/repos/apple/swift/pulls/21286","id":238291235,"node_id":"MDExOlB1bGxSZXF1ZXN0MjM4MjkxMjM1","html_url":"https://github.com/apple/swift/pull/21286","diff_url":"https://github.com/apple/swift/pull/21286.diff","patch_url":"https://github.com/apple/swift/pull/21286.patch","issue_url":"https://api.github.com/repos/apple/swift/issues/21286","number":21286,"state":"open","locked":false,"title":"differentiate witness methods","user":{"login":"marcrasi","id":5945760,"node_id":"MDQ6VXNlcjU5NDU3NjA=","avatar_url":"https://avatars0.githubusercontent.com/u/5945760?v=4","gravatar_id":"","url":"https://api.github.com/users/marcrasi","html_url":"https://github.com/marcrasi","followers_url":"https://api.github.com/users/marcrasi/followers","following_url":"https://api.github.com/users/marcrasi/following{/other_user}","gists_url":"https://api.github.com/users/marcrasi/gists{/gist_id}","starred_url":"https://api.github.com/users/marcrasi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcrasi/subscriptions","organizations_url":"https://api.github.com/users/marcrasi/orgs","repos_url":"https://api.github.com/users/marcrasi/repos","events_url":"https://api.github.com/users/marcrasi/events{/privacy}","received_events_url":"https://api.github.com/users/marcrasi/received_events","type":"User","site_admin":false},"body":"The main idea is the new `emitVJPReference` function, which does the existing lookup logic for `function_ref`, and which does new lookup logic for `witness_method`. I refactored the 2 places that had `function_ref` lookup logic (`PrimalGen::visitApplyInstWithVJP` and `Differentiation::processAutoDiffFunctionInst`) to call `emitVJPReference`.\r\n\r\nI added a SIL FileCheck test for it. An end-to-end test is currently not possible: It requires the builtins for looking up method VJPs. I will work on that next in a separate PR.\r\n\r\nOther minor necessary fixes:\r\n* Made `SILFunctionType::getAutoDiffAssociatedFunctionType` work on `@convention(witness_method)` functions.\r\n* Made differentiation process transparent functions. The witness thunks with `autodiff_function`s are transparent for some reason. See `test/AutoDiff/witness_table_silgen.swift` for an example. The CHECKs in that are checking witness thunks.\r\n* Fixed bug so that differentiation processes `autodiff_function`s in modules without `#gradient` or `[differentiable]`.\r\n\r\nAnd finally, I re-enabled the SILVerifier check because now it passes.","created_at":"2018-12-13T07:36:29Z","updated_at":"2018-12-13T09:09:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f7ab79a7c29077f56902ba5b13e6ba1a98c4de6b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apple/swift/pulls/21286/commits","review_comments_url":"https://api.github.com/repos/apple/swift/pulls/21286/comments","review_comment_url":"https://api.github.com/repos/apple/swift/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apple/swift/issues/21286/comments","statuses_url":"https://api.github.com/repos/apple/swift/statuses/d9aec9b51ca2befc502871d02a8d67e0120b10b9","head":{"label":"marcrasi:diff-witness-methods","ref":"diff-witness-methods","sha":"d9aec9b51ca2befc502871d02a8d67e0120b10b9","user":{"login":"marcrasi","id":5945760,"node_id":"MDQ6VXNlcjU5NDU3NjA=","avatar_url":"https://avatars0.githubusercontent.com/u/5945760?v=4","gravatar_id":"","url":"https://api.github.com/users/marcrasi","html_url":"https://github.com/marcrasi","followers_url":"https://api.github.com/users/marcrasi/followers","following_url":"https://api.github.com/users/marcrasi/following{/other_user}","gists_url":"https://api.github.com/users/marcrasi/gists{/gist_id}","starred_url":"https://api.github.com/users/marcrasi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcrasi/subscriptions","organizations_url":"https://api.github.com/users/marcrasi/orgs","repos_url":"https://api.github.com/users/marcrasi/repos","events_url":"https://api.github.com/users/marcrasi/events{/privacy}","received_events_url":"https://api.github.com/users/marcrasi/received_events","type":"User","site_admin":false},"repo":{"id":135738994,"node_id":"MDEwOlJlcG9zaXRvcnkxMzU3Mzg5OTQ=","name":"swift","full_name":"marcrasi/swift","private":false,"owner":{"login":"marcrasi","id":5945760,"node_id":"MDQ6VXNlcjU5NDU3NjA=","avatar_url":"https://avatars0.githubusercontent.com/u/5945760?v=4","gravatar_id":"","url":"https://api.github.com/users/marcrasi","html_url":"https://github.com/marcrasi","followers_url":"https://api.github.com/users/marcrasi/followers","following_url":"https://api.github.com/users/marcrasi/following{/other_user}","gists_url":"https://api.github.com/users/marcrasi/gists{/gist_id}","starred_url":"https://api.github.com/users/marcrasi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcrasi/subscriptions","organizations_url":"https://api.github.com/users/marcrasi/orgs","repos_url":"https://api.github.com/users/marcrasi/repos","events_url":"https://api.github.com/users/marcrasi/events{/privacy}","received_events_url":"https://api.github.com/users/marcrasi/received_events","type":"User","site_admin":false},"html_url":"https://github.com/marcrasi/swift","description":"The Swift Programming Language","fork":true,"url":"https://api.github.com/repos/marcrasi/swift","forks_url":"https://api.github.com/repos/marcrasi/swift/forks","keys_url":"https://api.github.com/repos/marcrasi/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/marcrasi/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/marcrasi/swift/teams","hooks_url":"https://api.github.com/repos/marcrasi/swift/hooks","issue_events_url":"https://api.github.com/repos/marcrasi/swift/issues/events{/number}","events_url":"https://api.github.com/repos/marcrasi/swift/events","assignees_url":"https://api.github.com/repos/marcrasi/swift/assignees{/user}","branches_url":"https://api.github.com/repos/marcrasi/swift/branches{/branch}","tags_url":"https://api.github.com/repos/marcrasi/swift/tags","blobs_url":"https://api.github.com/repos/marcrasi/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/marcrasi/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/marcrasi/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/marcrasi/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/marcrasi/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/marcrasi/swift/languages","stargazers_url":"https://api.github.com/repos/marcrasi/swift/stargazers","contributors_url":"https://api.github.com/repos/marcrasi/swift/contributors","subscribers_url":"https://api.github.com/repos/marcrasi/swift/subscribers","subscription_url":"https://api.github.com/repos/marcrasi/swift/subscription","commits_url":"https://api.github.com/repos/marcrasi/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/marcrasi/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/marcrasi/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/marcrasi/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/marcrasi/swift/contents/{+path}","compare_url":"https://api.github.com/repos/marcrasi/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/marcrasi/swift/merges","archive_url":"https://api.github.com/repos/marcrasi/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/marcrasi/swift/downloads","issues_url":"https://api.github.com/repos/marcrasi/swift/issues{/number}","pulls_url":"https://api.github.com/repos/marcrasi/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/marcrasi/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/marcrasi/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/marcrasi/swift/labels{/name}","releases_url":"https://api.github.com/repos/marcrasi/swift/releases{/id}","deployments_url":"https://api.github.com/repos/marcrasi/swift/deployments","created_at":"2018-06-01T16:13:26Z","updated_at":"2018-06-01T16:13:59Z","pushed_at":"2018-12-13T07:42:26Z","git_url":"git://github.com/marcrasi/swift.git","ssh_url":"git@github.com:marcrasi/swift.git","clone_url":"https://github.com/marcrasi/swift.git","svn_url":"https://github.com/marcrasi/swift","homepage":"https://swift.org/","size":361203,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apple:tensorflow","ref":"tensorflow","sha":"1d9746c11124b87ae3ce0e3b4e456fcf1e55ae81","user":{"login":"apple","id":10639145,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwNjM5MTQ1","avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"repo":{"id":44838949,"node_id":"MDEwOlJlcG9zaXRvcnk0NDgzODk0OQ==","name":"swift","full_name":"apple/swift","private":false,"owner":{"login":"apple","id":10639145,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwNjM5MTQ1","avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/apple/swift","description":"The Swift Programming Language","fork":false,"url":"https://api.github.com/repos/apple/swift","forks_url":"https://api.github.com/repos/apple/swift/forks","keys_url":"https://api.github.com/repos/apple/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apple/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apple/swift/teams","hooks_url":"https://api.github.com/repos/apple/swift/hooks","issue_events_url":"https://api.github.com/repos/apple/swift/issues/events{/number}","events_url":"https://api.github.com/repos/apple/swift/events","assignees_url":"https://api.github.com/repos/apple/swift/assignees{/user}","branches_url":"https://api.github.com/repos/apple/swift/branches{/branch}","tags_url":"https://api.github.com/repos/apple/swift/tags","blobs_url":"https://api.github.com/repos/apple/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apple/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apple/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/apple/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apple/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/apple/swift/languages","stargazers_url":"https://api.github.com/repos/apple/swift/stargazers","contributors_url":"https://api.github.com/repos/apple/swift/contributors","subscribers_url":"https://api.github.com/repos/apple/swift/subscribers","subscription_url":"https://api.github.com/repos/apple/swift/subscription","commits_url":"https://api.github.com/repos/apple/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/apple/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/apple/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/apple/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/apple/swift/contents/{+path}","compare_url":"https://api.github.com/repos/apple/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apple/swift/merges","archive_url":"https://api.github.com/repos/apple/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apple/swift/downloads","issues_url":"https://api.github.com/repos/apple/swift/issues{/number}","pulls_url":"https://api.github.com/repos/apple/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/apple/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/apple/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apple/swift/labels{/name}","releases_url":"https://api.github.com/repos/apple/swift/releases{/id}","deployments_url":"https://api.github.com/repos/apple/swift/deployments","created_at":"2015-10-23T21:15:07Z","updated_at":"2018-12-13T07:42:03Z","pushed_at":"2018-12-13T08:49:30Z","git_url":"git://github.com/apple/swift.git","ssh_url":"git@github.com:apple/swift.git","clone_url":"https://github.com/apple/swift.git","svn_url":"https://github.com/apple/swift","homepage":"https://swift.org","size":373279,"stargazers_count":46021,"watchers_count":46021,"language":"C++","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":7314,"mirror_url":null,"archived":false,"open_issues_count":439,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":7314,"open_issues":439,"watchers":46021,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/21286"},"html":{"href":"https://github.com/apple/swift/pull/21286"},"issue":{"href":"https://api.github.com/repos/apple/swift/issues/21286"},"comments":{"href":"https://api.github.com/repos/apple/swift/issues/21286/comments"},"review_comments":{"href":"https://api.github.com/repos/apple/swift/pulls/21286/comments"},"review_comment":{"href":"https://api.github.com/repos/apple/swift/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apple/swift/pulls/21286/commits"},"statuses":{"href":"https://api.github.com/repos/apple/swift/statuses/d9aec9b51ca2befc502871d02a8d67e0120b10b9"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"rxwei"}}