{"action":"created","comment":{"url":"https://api.github.com/repos/halide/Halide/pulls/comments/23094376","id":23094376,"diff_hunk":"@@ -0,0 +1,114 @@\n+#ifndef HALIDE_JIT_MODULE_H\n+#define HALIDE_JIT_MODULE_H\n+\n+/** \\file\n+ * Defines the struct representing a JIT compiled halide pipeline\n+ */\n+\n+#include \"IntrusivePtr.h\"\n+#include \"runtime/HalideRuntime.h\"\n+\n+#include <map>\n+\n+namespace llvm {\n+class Module;\n+class Type;\n+}\n+\n+namespace Halide {\n+\n+struct Target;\n+\n+namespace Internal {\n+\n+class JITModuleContents;\n+class CodeGen;\n+\n+struct JITModule {\n+    IntrusivePtr<JITModuleContents> jit_module;\n+\n+    struct Symbol {\n+        void *address;\n+        llvm::Type *llvm_type;\n+        Symbol() : address(NULL), llvm_type(NULL) {}\n+        Symbol(void *address, llvm::Type *llvm_type) : address(address), llvm_type(llvm_type) {}\n+    };\n+\n+    JITModule() {}\n+    JITModule(const std::map<std::string, Symbol> &exports);\n+\n+    const std::map<std::string, Symbol> &exports() const;\n+\n+    /** A pointer to the raw halide function. It's true type depends\n+     * on the Argument vector passed to CodeGen::compile. Image\n+     * parameters become (buffer_t *), and scalar parameters become\n+     * pointers to the appropriate values. The final argument is a\n+     * pointer to the buffer_t defining the output. This will be NULL for\n+     * a JITModule which has not yet been compiled or one that is not\n+     * a Halide Func compilation at all. */\n+    void *main_function() const;\n+\n+    /** A slightly more type-safe wrapper around the raw halide","path":"src/JITModule.h","position":51,"original_position":51,"commit_id":"dede0906455dd6b4273e454b563744e63bc72650","original_commit_id":"dede0906455dd6b4273e454b563744e63bc72650","user":{"login":"dsharletg","id":7494796,"avatar_url":"https://avatars.githubusercontent.com/u/7494796?v=3","gravatar_id":"","url":"https://api.github.com/users/dsharletg","html_url":"https://github.com/dsharletg","followers_url":"https://api.github.com/users/dsharletg/followers","following_url":"https://api.github.com/users/dsharletg/following{/other_user}","gists_url":"https://api.github.com/users/dsharletg/gists{/gist_id}","starred_url":"https://api.github.com/users/dsharletg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsharletg/subscriptions","organizations_url":"https://api.github.com/users/dsharletg/orgs","repos_url":"https://api.github.com/users/dsharletg/repos","events_url":"https://api.github.com/users/dsharletg/events{/privacy}","received_events_url":"https://api.github.com/users/dsharletg/received_events","type":"User","site_admin":false},"body":"I'd say this is *less* type safe, not more. The advantage of this function is that it is easier for a program to call, because it is difficult to generate a call site for argument types that aren't known at compile time. This function allows you to build an array at runtime to call with.","created_at":"2015-01-16T17:20:29Z","updated_at":"2015-01-16T17:20:29Z","html_url":"https://github.com/halide/Halide/pull/630#discussion_r23094376","pull_request_url":"https://api.github.com/repos/halide/Halide/pulls/630","_links":{"self":{"href":"https://api.github.com/repos/halide/Halide/pulls/comments/23094376"},"html":{"href":"https://github.com/halide/Halide/pull/630#discussion_r23094376"},"pull_request":{"href":"https://api.github.com/repos/halide/Halide/pulls/630"}}},"pull_request":{"url":"https://api.github.com/repos/halide/Halide/pulls/630","id":27483977,"html_url":"https://github.com/halide/Halide/pull/630","diff_url":"https://github.com/halide/Halide/pull/630.diff","patch_url":"https://github.com/halide/Halide/pull/630.patch","issue_url":"https://api.github.com/repos/halide/Halide/issues/630","number":630,"state":"open","locked":false,"title":"Shared runtime","user":{"login":"zvookin","id":1501716,"avatar_url":"https://avatars.githubusercontent.com/u/1501716?v=3","gravatar_id":"","url":"https://api.github.com/users/zvookin","html_url":"https://github.com/zvookin","followers_url":"https://api.github.com/users/zvookin/followers","following_url":"https://api.github.com/users/zvookin/following{/other_user}","gists_url":"https://api.github.com/users/zvookin/gists{/gist_id}","starred_url":"https://api.github.com/users/zvookin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zvookin/subscriptions","organizations_url":"https://api.github.com/users/zvookin/orgs","repos_url":"https://api.github.com/users/zvookin/repos","events_url":"https://api.github.com/users/zvookin/events{/privacy}","received_events_url":"https://api.github.com/users/zvookin/received_events","type":"User","site_admin":false},"body":"\r\nThis PR moves to sharing the runtime across JITted Func calls. I believe this simplifies a fair bit of logic and makes it possible to reference count the GPU implementation currently owned by a buffer_t in the new gpu_api_naming branch. (The impetus for writing all of this was that source module tracking in the Buffer class does not work with gpu_api_naming as a buffer_t can move from one GPU API to another. This branch has the GPU API implementation ref counting itself by how many device allocations are outstanding.)\r\n\r\nThis PR should make it easily possible to JIT a collection of Funcs that are separately compiled into a pipeline via define_extern, or to provide HalideExtern or non-Halide define_extern symbols to JITted code. We still need an API to do this, such as passing a list of dependent Funcs, or JITModules to the realize method.\r\n\r\nJITModule replaces JITCompiledModule. The name changes as a JITModule may be JITted Halide code, llvm modules for the runtime, or simply a map from function names to their addresses that should be used to resolve extern references in other JITModules. The JITModule is reference counted.\r\n\r\nThe runtime now uses destructors via ```__attribute__((destructor))```. I feel this is much more maintainable than the ad hoc cleanup code that was there before. (E.g. not everything was being cleaned up in the pre-existing code.) Destructors should be pretty reliable, though the ordering cannot be depended on. (Constructors are still highly problematic, even not considering ordering. the attribute thing is basically just like a class destructor but it can be applied to any function.)\r\n\r\nThe basic control over the shared runtime is provided by JITSharedRuntime. (Which is currently in the Internal namespace, but it should likely be moved.) Notably initialization, which usually happens lazily when the first Func is JITted, but could be made to happen at a specific time in programs startup. The set_default_handlers method allows installing handlers for all subsequent JIT Funcs. And release_all releases all the shared runtime resources, though they are ref counted so and Funcs that are still extant and depending on the runtime will continue to work and the resources will be freed when those Funcs are all destructed. If another Func is JITted after release_all is called, a new shared runtime is created.\r\n\r\nThe logic that was ensuring a single GPU context for each JITted Func is now much simpler and basically the same as the AOT case.\r\n\r\nThread safety for JIT is an open issue. The current idea is to use C++11 primitives in the same way Generator does such that under C++11, the code will be thread safe.\r\n\r\nThis branch is intended to support the gpu_api_renaming changes and probably also the codegen_compile_refactor one. I decided to do it separately first as there is a gating bug in gpu_api_naming that requires this and I felt it would make the reviews easier.\r\n\r\nWith GPU API naming, it should be possible to remove the special logic in Buffer which keeps JITted code loaded until all GPU allocations are freed. This logic should now happen automatically on buffer_t, though we will need a way to make halide_device_free available from libHalide.a directly.\r\n\r\nThis code automatically finds all runtime symbols using the heuristic that a routine that starts with \"halide_\" which is weak should be an exported Halide runtime routine. This removes a table that previously had to be updated. Internal symbols should be kept in a C++ namespace such that this heuristic skips them.\r\n\r\nThere is an open question as to whether this code works with earlier versions of LLVM due to the use of the JIT API. I hope it does, but it will require testing.","created_at":"2015-01-16T04:41:14Z","updated_at":"2015-01-16T17:20:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c338aaf6c0616752b4f16e8f033108801f9ecc49","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/halide/Halide/pulls/630/commits","review_comments_url":"https://api.github.com/repos/halide/Halide/pulls/630/comments","review_comment_url":"https://api.github.com/repos/halide/Halide/pulls/comments/{number}","comments_url":"https://api.github.com/repos/halide/Halide/issues/630/comments","statuses_url":"https://api.github.com/repos/halide/Halide/statuses/dede0906455dd6b4273e454b563744e63bc72650","head":{"label":"halide:shared_runtime","ref":"shared_runtime","sha":"dede0906455dd6b4273e454b563744e63bc72650","user":{"login":"halide","id":1548551,"avatar_url":"https://avatars.githubusercontent.com/u/1548551?v=3","gravatar_id":"","url":"https://api.github.com/users/halide","html_url":"https://github.com/halide","followers_url":"https://api.github.com/users/halide/followers","following_url":"https://api.github.com/users/halide/following{/other_user}","gists_url":"https://api.github.com/users/halide/gists{/gist_id}","starred_url":"https://api.github.com/users/halide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halide/subscriptions","organizations_url":"https://api.github.com/users/halide/orgs","repos_url":"https://api.github.com/users/halide/repos","events_url":"https://api.github.com/users/halide/events{/privacy}","received_events_url":"https://api.github.com/users/halide/received_events","type":"Organization","site_admin":false},"repo":{"id":5241312,"name":"Halide","full_name":"halide/Halide","owner":{"login":"halide","id":1548551,"avatar_url":"https://avatars.githubusercontent.com/u/1548551?v=3","gravatar_id":"","url":"https://api.github.com/users/halide","html_url":"https://github.com/halide","followers_url":"https://api.github.com/users/halide/followers","following_url":"https://api.github.com/users/halide/following{/other_user}","gists_url":"https://api.github.com/users/halide/gists{/gist_id}","starred_url":"https://api.github.com/users/halide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halide/subscriptions","organizations_url":"https://api.github.com/users/halide/orgs","repos_url":"https://api.github.com/users/halide/repos","events_url":"https://api.github.com/users/halide/events{/privacy}","received_events_url":"https://api.github.com/users/halide/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/halide/Halide","description":"a language for image processing and computational photography","fork":false,"url":"https://api.github.com/repos/halide/Halide","forks_url":"https://api.github.com/repos/halide/Halide/forks","keys_url":"https://api.github.com/repos/halide/Halide/keys{/key_id}","collaborators_url":"https://api.github.com/repos/halide/Halide/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/halide/Halide/teams","hooks_url":"https://api.github.com/repos/halide/Halide/hooks","issue_events_url":"https://api.github.com/repos/halide/Halide/issues/events{/number}","events_url":"https://api.github.com/repos/halide/Halide/events","assignees_url":"https://api.github.com/repos/halide/Halide/assignees{/user}","branches_url":"https://api.github.com/repos/halide/Halide/branches{/branch}","tags_url":"https://api.github.com/repos/halide/Halide/tags","blobs_url":"https://api.github.com/repos/halide/Halide/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/halide/Halide/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/halide/Halide/git/refs{/sha}","trees_url":"https://api.github.com/repos/halide/Halide/git/trees{/sha}","statuses_url":"https://api.github.com/repos/halide/Halide/statuses/{sha}","languages_url":"https://api.github.com/repos/halide/Halide/languages","stargazers_url":"https://api.github.com/repos/halide/Halide/stargazers","contributors_url":"https://api.github.com/repos/halide/Halide/contributors","subscribers_url":"https://api.github.com/repos/halide/Halide/subscribers","subscription_url":"https://api.github.com/repos/halide/Halide/subscription","commits_url":"https://api.github.com/repos/halide/Halide/commits{/sha}","git_commits_url":"https://api.github.com/repos/halide/Halide/git/commits{/sha}","comments_url":"https://api.github.com/repos/halide/Halide/comments{/number}","issue_comment_url":"https://api.github.com/repos/halide/Halide/issues/comments/{number}","contents_url":"https://api.github.com/repos/halide/Halide/contents/{+path}","compare_url":"https://api.github.com/repos/halide/Halide/compare/{base}...{head}","merges_url":"https://api.github.com/repos/halide/Halide/merges","archive_url":"https://api.github.com/repos/halide/Halide/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/halide/Halide/downloads","issues_url":"https://api.github.com/repos/halide/Halide/issues{/number}","pulls_url":"https://api.github.com/repos/halide/Halide/pulls{/number}","milestones_url":"https://api.github.com/repos/halide/Halide/milestones{/number}","notifications_url":"https://api.github.com/repos/halide/Halide/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/halide/Halide/labels{/name}","releases_url":"https://api.github.com/repos/halide/Halide/releases{/id}","created_at":"2012-07-31T04:05:25Z","updated_at":"2015-01-16T17:11:51Z","pushed_at":"2015-01-16T17:11:51Z","git_url":"git://github.com/halide/Halide.git","ssh_url":"git@github.com:halide/Halide.git","clone_url":"https://github.com/halide/Halide.git","svn_url":"https://github.com/halide/Halide","homepage":"http://halide-lang.org","size":107044,"stargazers_count":904,"watchers_count":904,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":155,"mirror_url":null,"open_issues_count":78,"forks":155,"open_issues":78,"watchers":904,"default_branch":"master"}},"base":{"label":"halide:master","ref":"master","sha":"e0d987d36e8c127d64e25021ca542b0dbf3cbbef","user":{"login":"halide","id":1548551,"avatar_url":"https://avatars.githubusercontent.com/u/1548551?v=3","gravatar_id":"","url":"https://api.github.com/users/halide","html_url":"https://github.com/halide","followers_url":"https://api.github.com/users/halide/followers","following_url":"https://api.github.com/users/halide/following{/other_user}","gists_url":"https://api.github.com/users/halide/gists{/gist_id}","starred_url":"https://api.github.com/users/halide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halide/subscriptions","organizations_url":"https://api.github.com/users/halide/orgs","repos_url":"https://api.github.com/users/halide/repos","events_url":"https://api.github.com/users/halide/events{/privacy}","received_events_url":"https://api.github.com/users/halide/received_events","type":"Organization","site_admin":false},"repo":{"id":5241312,"name":"Halide","full_name":"halide/Halide","owner":{"login":"halide","id":1548551,"avatar_url":"https://avatars.githubusercontent.com/u/1548551?v=3","gravatar_id":"","url":"https://api.github.com/users/halide","html_url":"https://github.com/halide","followers_url":"https://api.github.com/users/halide/followers","following_url":"https://api.github.com/users/halide/following{/other_user}","gists_url":"https://api.github.com/users/halide/gists{/gist_id}","starred_url":"https://api.github.com/users/halide/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halide/subscriptions","organizations_url":"https://api.github.com/users/halide/orgs","repos_url":"https://api.github.com/users/halide/repos","events_url":"https://api.github.com/users/halide/events{/privacy}","received_events_url":"https://api.github.com/users/halide/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/halide/Halide","description":"a language for image processing and computational photography","fork":false,"url":"https://api.github.com/repos/halide/Halide","forks_url":"https://api.github.com/repos/halide/Halide/forks","keys_url":"https://api.github.com/repos/halide/Halide/keys{/key_id}","collaborators_url":"https://api.github.com/repos/halide/Halide/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/halide/Halide/teams","hooks_url":"https://api.github.com/repos/halide/Halide/hooks","issue_events_url":"https://api.github.com/repos/halide/Halide/issues/events{/number}","events_url":"https://api.github.com/repos/halide/Halide/events","assignees_url":"https://api.github.com/repos/halide/Halide/assignees{/user}","branches_url":"https://api.github.com/repos/halide/Halide/branches{/branch}","tags_url":"https://api.github.com/repos/halide/Halide/tags","blobs_url":"https://api.github.com/repos/halide/Halide/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/halide/Halide/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/halide/Halide/git/refs{/sha}","trees_url":"https://api.github.com/repos/halide/Halide/git/trees{/sha}","statuses_url":"https://api.github.com/repos/halide/Halide/statuses/{sha}","languages_url":"https://api.github.com/repos/halide/Halide/languages","stargazers_url":"https://api.github.com/repos/halide/Halide/stargazers","contributors_url":"https://api.github.com/repos/halide/Halide/contributors","subscribers_url":"https://api.github.com/repos/halide/Halide/subscribers","subscription_url":"https://api.github.com/repos/halide/Halide/subscription","commits_url":"https://api.github.com/repos/halide/Halide/commits{/sha}","git_commits_url":"https://api.github.com/repos/halide/Halide/git/commits{/sha}","comments_url":"https://api.github.com/repos/halide/Halide/comments{/number}","issue_comment_url":"https://api.github.com/repos/halide/Halide/issues/comments/{number}","contents_url":"https://api.github.com/repos/halide/Halide/contents/{+path}","compare_url":"https://api.github.com/repos/halide/Halide/compare/{base}...{head}","merges_url":"https://api.github.com/repos/halide/Halide/merges","archive_url":"https://api.github.com/repos/halide/Halide/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/halide/Halide/downloads","issues_url":"https://api.github.com/repos/halide/Halide/issues{/number}","pulls_url":"https://api.github.com/repos/halide/Halide/pulls{/number}","milestones_url":"https://api.github.com/repos/halide/Halide/milestones{/number}","notifications_url":"https://api.github.com/repos/halide/Halide/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/halide/Halide/labels{/name}","releases_url":"https://api.github.com/repos/halide/Halide/releases{/id}","created_at":"2012-07-31T04:05:25Z","updated_at":"2015-01-16T17:11:51Z","pushed_at":"2015-01-16T17:11:51Z","git_url":"git://github.com/halide/Halide.git","ssh_url":"git@github.com:halide/Halide.git","clone_url":"https://github.com/halide/Halide.git","svn_url":"https://github.com/halide/Halide","homepage":"http://halide-lang.org","size":107044,"stargazers_count":904,"watchers_count":904,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":155,"mirror_url":null,"open_issues_count":78,"forks":155,"open_issues":78,"watchers":904,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/halide/Halide/pulls/630"},"html":{"href":"https://github.com/halide/Halide/pull/630"},"issue":{"href":"https://api.github.com/repos/halide/Halide/issues/630"},"comments":{"href":"https://api.github.com/repos/halide/Halide/issues/630/comments"},"review_comments":{"href":"https://api.github.com/repos/halide/Halide/pulls/630/comments"},"review_comment":{"href":"https://api.github.com/repos/halide/Halide/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/halide/Halide/pulls/630/commits"},"statuses":{"href":"https://api.github.com/repos/halide/Halide/statuses/dede0906455dd6b4273e454b563744e63bc72650"}}}}