Skip to content

Keep possibly-effectful expressions when optimizing multiplication by zero#956

Merged
mshinwell merged 2 commits intoocaml:trunkfrom
yallop:zeromul
Dec 9, 2016
Merged

Keep possibly-effectful expressions when optimizing multiplication by zero#956
mshinwell merged 2 commits intoocaml:trunkfrom
yallop:zeromul

Conversation

@yallop
Copy link
Copy Markdown
Member

@yallop yallop commented Dec 8, 2016

Here's a test program:

let () = Printf.printf "%d\n" (0 * (print_endline "erk"; 1))

Output before this PR:

0

Output after this PR:

erk
0

match (c1, c2) with
| (_, Cconst_int 0) | (Cconst_int 0, _) ->
Cconst_int 0
| (c, Cconst_int 0) | (Cconst_int 0, c) -> Csequence (c, Cconst_int 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to preserve the old rule, but guarded with when is_pure c ? (The function is_pure needs to be written.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wrote it that way first, but it turns out to be a much bigger and more controversial change, and the Csequence style is used elsewhere in this file.

I think it's better to fix this as simply and directly as possible, and consider is_pure as a separate enhancement.

@mshinwell mshinwell added the bug label Dec 8, 2016
@mshinwell mshinwell added this to the 4.05.0 milestone Dec 8, 2016
@xavierleroy
Copy link
Copy Markdown
Contributor

Reviewed and approved.

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Dec 9, 2016 via email

@yallop
Copy link
Copy Markdown
Member Author

yallop commented Dec 9, 2016

Shouldn't there be the same kind of code for the constant 1?

The 1 case is fine: simplifying e * 0 to 0 isn't correct if e has effects, but simplifying e * 1 to e doesn't have that problem, since it doesn't discard e.

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Dec 9, 2016 via email

@mshinwell
Copy link
Copy Markdown
Contributor

I had a look through some of the other various arithmetic functions and didn't spot anything else amiss, although these problems can be rather subtle.

@mshinwell mshinwell merged commit f3666f2 into ocaml:trunk Dec 9, 2016
@mshinwell
Copy link
Copy Markdown
Contributor

(Sorry, I merged this before realising there wasn't a test case. I will ask Jeremy)

@yallop yallop deleted the zeromul branch December 15, 2016 17:09
nojb pushed a commit to nojb/ocaml that referenced this pull request Dec 29, 2016
camlspotter pushed a commit to camlspotter/ocaml that referenced this pull request Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants