{"action":"created","comment":{"url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/comments/47006030","id":47006030,"diff_hunk":"@@ -610,19 +610,28 @@ LLVMGEN (llvm_gen_div)\n \n     // The following should handle f/f, v/v, v/f, f/v, i/i\n     // That's all that should be allowed by oslc.\n+    const char *safe_div = is_float ? \"osl_safe_div_fff\" : \"osl_safe_div_iii\";\n     bool deriv = (Result.has_derivs() && (A.has_derivs() || B.has_derivs()));\n     for (int i = 0; i < num_components; i++) {\n         llvm::Value *a = rop.llvm_load_value (A, 0, i, type);\n         llvm::Value *b = rop.llvm_load_value (B, 0, i, type);\n         if (!a || !b)\n             return false;\n-        llvm::Value *a_div_b = rop.ll.op_make_safe_div (type, a, b);\n+        llvm::Value *a_div_b;\n+        if (B.is_constant() && ! rop.is_zero(B))\n+            a_div_b = rop.ll.op_div (a, b);\n+        else\n+            a_div_b = rop.ll.call_function (safe_div, a, b);\n         llvm::Value *rx = NULL, *ry = NULL;\n \n         if (deriv) {\n             // Division of duals: (a/b, 1/b*(ax-a/b*bx), 1/b*(ay-a/b*by))\n             ASSERT (is_float);\n-            llvm::Value *binv = rop.ll.op_make_safe_div (type, rop.ll.constant(1.0f), b);\n+            llvm::Value *binv;\n+            if (B.is_constant() && ! rop.is_zero(B))\n+                binv = rop.ll.op_div (rop.ll.constant(1.0f), b);\n+            else\n+                binv = rop.ll.call_function (safe_div, rop.ll.constant(1.0f), b);","path":"src/liboslexec/llvm_gen.cpp","position":27,"original_position":27,"commit_id":"c6dbb94a544ce9e48e26828d62294d2bb6cce9e6","original_commit_id":"c6dbb94a544ce9e48e26828d62294d2bb6cce9e6","user":{"login":"fpsunflower","id":578593,"avatar_url":"https://avatars.githubusercontent.com/u/578593?v=3","gravatar_id":"","url":"https://api.github.com/users/fpsunflower","html_url":"https://github.com/fpsunflower","followers_url":"https://api.github.com/users/fpsunflower/followers","following_url":"https://api.github.com/users/fpsunflower/following{/other_user}","gists_url":"https://api.github.com/users/fpsunflower/gists{/gist_id}","starred_url":"https://api.github.com/users/fpsunflower/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fpsunflower/subscriptions","organizations_url":"https://api.github.com/users/fpsunflower/orgs","repos_url":"https://api.github.com/users/fpsunflower/repos","events_url":"https://api.github.com/users/fpsunflower/events{/privacy}","received_events_url":"https://api.github.com/users/fpsunflower/received_events","type":"User","site_admin":false},"body":"Its probably that I'm not familiar enough with the code, but can you explain in what cases it will generate a function call vs. generate LLVM IR directly? (ie: the `make_safe_div` call below).","created_at":"2015-12-08T20:06:01Z","updated_at":"2015-12-08T20:06:01Z","html_url":"https://github.com/imageworks/OpenShadingLanguage/pull/596#discussion_r47006030","pull_request_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596","_links":{"self":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/comments/47006030"},"html":{"href":"https://github.com/imageworks/OpenShadingLanguage/pull/596#discussion_r47006030"},"pull_request":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596"}}},"pull_request":{"url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596","id":53029411,"html_url":"https://github.com/imageworks/OpenShadingLanguage/pull/596","diff_url":"https://github.com/imageworks/OpenShadingLanguage/pull/596.diff","patch_url":"https://github.com/imageworks/OpenShadingLanguage/pull/596.patch","issue_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/596","number":596,"state":"open","locked":false,"title":"Fix potential SIGFPE from divide by zero in shaders.","user":{"login":"lgritz","id":504179,"avatar_url":"https://avatars.githubusercontent.com/u/504179?v=3","gravatar_id":"","url":"https://api.github.com/users/lgritz","html_url":"https://github.com/lgritz","followers_url":"https://api.github.com/users/lgritz/followers","following_url":"https://api.github.com/users/lgritz/following{/other_user}","gists_url":"https://api.github.com/users/lgritz/gists{/gist_id}","starred_url":"https://api.github.com/users/lgritz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lgritz/subscriptions","organizations_url":"https://api.github.com/users/lgritz/orgs","repos_url":"https://api.github.com/users/lgritz/repos","events_url":"https://api.github.com/users/lgritz/events{/privacy}","received_events_url":"https://api.github.com/users/lgritz/received_events","type":"User","site_admin":false},"body":"The implementation in LLVM_Util we were relying on for safe div and mod    turned out to have the property that they generated safe results (e.g.,    x/0 returns 0, not inf), but still was capable of generating div-by-zero    exception if the app in which OSL is embedded was trapping SIGFPE.\r\n\r\nInstead, switch to a truly safe implementation that only does the div    or mod if the denominator is nonzero. Also enhance it by doing a simple    unchecked div or mod any time the denominator is known to be a nonzero    constant (no need to have the conditional in that case).","created_at":"2015-12-08T19:52:29Z","updated_at":"2015-12-08T20:06:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ae72c2976381649e3785187e8eb56254686c054","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596/commits","review_comments_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596/comments","review_comment_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/comments{/number}","comments_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/596/comments","statuses_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/statuses/c6dbb94a544ce9e48e26828d62294d2bb6cce9e6","head":{"label":"lgritz:lg-div0","ref":"lg-div0","sha":"c6dbb94a544ce9e48e26828d62294d2bb6cce9e6","user":{"login":"lgritz","id":504179,"avatar_url":"https://avatars.githubusercontent.com/u/504179?v=3","gravatar_id":"","url":"https://api.github.com/users/lgritz","html_url":"https://github.com/lgritz","followers_url":"https://api.github.com/users/lgritz/followers","following_url":"https://api.github.com/users/lgritz/following{/other_user}","gists_url":"https://api.github.com/users/lgritz/gists{/gist_id}","starred_url":"https://api.github.com/users/lgritz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lgritz/subscriptions","organizations_url":"https://api.github.com/users/lgritz/orgs","repos_url":"https://api.github.com/users/lgritz/repos","events_url":"https://api.github.com/users/lgritz/events{/privacy}","received_events_url":"https://api.github.com/users/lgritz/received_events","type":"User","site_admin":false},"repo":{"id":1462077,"name":"OpenShadingLanguage","full_name":"lgritz/OpenShadingLanguage","owner":{"login":"lgritz","id":504179,"avatar_url":"https://avatars.githubusercontent.com/u/504179?v=3","gravatar_id":"","url":"https://api.github.com/users/lgritz","html_url":"https://github.com/lgritz","followers_url":"https://api.github.com/users/lgritz/followers","following_url":"https://api.github.com/users/lgritz/following{/other_user}","gists_url":"https://api.github.com/users/lgritz/gists{/gist_id}","starred_url":"https://api.github.com/users/lgritz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lgritz/subscriptions","organizations_url":"https://api.github.com/users/lgritz/orgs","repos_url":"https://api.github.com/users/lgritz/repos","events_url":"https://api.github.com/users/lgritz/events{/privacy}","received_events_url":"https://api.github.com/users/lgritz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lgritz/OpenShadingLanguage","description":"Advanced shading language for production GI renderers","fork":true,"url":"https://api.github.com/repos/lgritz/OpenShadingLanguage","forks_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/forks","keys_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/teams","hooks_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/hooks","issue_events_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/issues/events{/number}","events_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/events","assignees_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/assignees{/user}","branches_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/branches{/branch}","tags_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/tags","blobs_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/git/refs{/sha}","trees_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/statuses/{sha}","languages_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/languages","stargazers_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/stargazers","contributors_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/contributors","subscribers_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/subscribers","subscription_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/subscription","commits_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/commits{/sha}","git_commits_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/git/commits{/sha}","comments_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/comments{/number}","issue_comment_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/issues/comments{/number}","contents_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/contents/{+path}","compare_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/merges","archive_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/downloads","issues_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/issues{/number}","pulls_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/pulls{/number}","milestones_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/milestones{/number}","notifications_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/labels{/name}","releases_url":"https://api.github.com/repos/lgritz/OpenShadingLanguage/releases{/id}","created_at":"2011-03-10T03:35:03Z","updated_at":"2015-05-08T19:51:06Z","pushed_at":"2015-12-08T19:34:38Z","git_url":"git://github.com/lgritz/OpenShadingLanguage.git","ssh_url":"git@github.com:lgritz/OpenShadingLanguage.git","clone_url":"https://github.com/lgritz/OpenShadingLanguage.git","svn_url":"https://github.com/lgritz/OpenShadingLanguage","homepage":"","size":151652,"stargazers_count":6,"watchers_count":6,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":6,"default_branch":"master"}},"base":{"label":"imageworks:master","ref":"master","sha":"45f7a157d343e2fe327e8e791334fac6dbfa0e11","user":{"login":"imageworks","id":320618,"avatar_url":"https://avatars.githubusercontent.com/u/320618?v=3","gravatar_id":"","url":"https://api.github.com/users/imageworks","html_url":"https://github.com/imageworks","followers_url":"https://api.github.com/users/imageworks/followers","following_url":"https://api.github.com/users/imageworks/following{/other_user}","gists_url":"https://api.github.com/users/imageworks/gists{/gist_id}","starred_url":"https://api.github.com/users/imageworks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/imageworks/subscriptions","organizations_url":"https://api.github.com/users/imageworks/orgs","repos_url":"https://api.github.com/users/imageworks/repos","events_url":"https://api.github.com/users/imageworks/events{/privacy}","received_events_url":"https://api.github.com/users/imageworks/received_events","type":"Organization","site_admin":false},"repo":{"id":1461954,"name":"OpenShadingLanguage","full_name":"imageworks/OpenShadingLanguage","owner":{"login":"imageworks","id":320618,"avatar_url":"https://avatars.githubusercontent.com/u/320618?v=3","gravatar_id":"","url":"https://api.github.com/users/imageworks","html_url":"https://github.com/imageworks","followers_url":"https://api.github.com/users/imageworks/followers","following_url":"https://api.github.com/users/imageworks/following{/other_user}","gists_url":"https://api.github.com/users/imageworks/gists{/gist_id}","starred_url":"https://api.github.com/users/imageworks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/imageworks/subscriptions","organizations_url":"https://api.github.com/users/imageworks/orgs","repos_url":"https://api.github.com/users/imageworks/repos","events_url":"https://api.github.com/users/imageworks/events{/privacy}","received_events_url":"https://api.github.com/users/imageworks/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/imageworks/OpenShadingLanguage","description":"Advanced shading language for production GI renderers","fork":false,"url":"https://api.github.com/repos/imageworks/OpenShadingLanguage","forks_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/forks","keys_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/keys{/key_id}","collaborators_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/teams","hooks_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/hooks","issue_events_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/events{/number}","events_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/events","assignees_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/assignees{/user}","branches_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/branches{/branch}","tags_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/tags","blobs_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/git/refs{/sha}","trees_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/git/trees{/sha}","statuses_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/statuses/{sha}","languages_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/languages","stargazers_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/stargazers","contributors_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/contributors","subscribers_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/subscribers","subscription_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/subscription","commits_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/commits{/sha}","git_commits_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/git/commits{/sha}","comments_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/comments{/number}","issue_comment_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/comments{/number}","contents_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/contents/{+path}","compare_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/compare/{base}...{head}","merges_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/merges","archive_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/downloads","issues_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues{/number}","pulls_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls{/number}","milestones_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/milestones{/number}","notifications_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/labels{/name}","releases_url":"https://api.github.com/repos/imageworks/OpenShadingLanguage/releases{/id}","created_at":"2011-03-10T02:44:05Z","updated_at":"2015-12-08T15:58:46Z","pushed_at":"2015-12-08T19:52:31Z","git_url":"git://github.com/imageworks/OpenShadingLanguage.git","ssh_url":"git@github.com:imageworks/OpenShadingLanguage.git","clone_url":"https://github.com/imageworks/OpenShadingLanguage.git","svn_url":"https://github.com/imageworks/OpenShadingLanguage","homepage":"http://code.google.com/p/openshadinglanguage/","size":153592,"stargazers_count":423,"watchers_count":423,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":115,"mirror_url":null,"open_issues_count":40,"forks":115,"open_issues":40,"watchers":423,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596"},"html":{"href":"https://github.com/imageworks/OpenShadingLanguage/pull/596"},"issue":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/596"},"comments":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/issues/596/comments"},"review_comments":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596/comments"},"review_comment":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/pulls/596/commits"},"statuses":{"href":"https://api.github.com/repos/imageworks/OpenShadingLanguage/statuses/c6dbb94a544ce9e48e26828d62294d2bb6cce9e6"}}}}