kaist-cp / cs420

KAIST CS420: Compiler Design (2023 Spring)
423 stars 28 forks source link

[Question] Is there any `ir*` examples after deadcode elimination pass? #406

Closed gmlwns2000 closed 2 years ago

gmlwns2000 commented 2 years ago

I found out my output of compiler and ir4 is different, during homework.

And I figured out, in ir4 there is no dead code elimination. Is this true?

I hope ir5 exists which is dead code elimination to find out potential bugs in my code for working on homework 7.

minseongg commented 2 years ago

Deadcode elimination pass is applied for ir2 and its results are stored in ir3.

You could check your deadcode elimination implementation works well for examples/deadcode/*.input.ir and ir2/*.ir by using self grader. (scripts/grade-hw6.sh, tests/test_examples.rs#L163-L176)

gmlwns2000 commented 2 years ago

Oh, I thought ir4 was the most optimized one. ir4 seems to be applied only gvn after the ir3.

So my compiler's --optimize result and ir4 must be different because my compiler repeats the optimizing pass until converge. One of case was ir4/foo.ir,

%b0:i0:i32 = minus 1:i32
%b0:i1:i32 = call @foo:[ret:i32 params:(i32, i32, i32)]*(0:i32, 1:i32, %b0:i0:i32)
%b0:i2:i32 = minus 1:i32
%b0:i3:u1 = cmp eq %b0:i1:i32 %b0:i0:i32

%b0:i2:i32 should be dead code, because of gvn optimization. But it was not removed in that ir code.

I hope there are the most optimized ir code examples, because I can not believe my compiler works properly always.

minseongg commented 2 years ago

Thanks for suggestion! We will add IR examples with -O option applied.

minseongg commented 2 years ago

We added most optimized IR examples to examples/opt. (https://github.com/kaist-cp/kecc-public/tree/main/examples/opt)

You could test your optimization works well by using RUST_MIN_STACK=33554432 cargo test test_examples_optimize.

gmlwns2000 commented 2 years ago

@minseongg Thank you so much!

hyn2028 commented 2 years ago

Thanks to test_examples_optimize, I found an infinite optimization bug that couldn't be found with traditional step tests. Thank you.

gmlwns2000 commented 2 years ago

@minseongg @hyn2028 I found my compiler has infinite optimization bug... How can I fix it? I could not know which one is problem, because there is no certain processing output in test code ;-;.

minseongg commented 2 years ago

@gmlwns2000 One possible reason of infinite loop is that your optimize function return true even if there is no change in the code.

You can check it by adding some logic like this:

fn optimize(&mut self, code: &mut FunctionDefinition) -> bool {
    let code_orig = code.clone(); // original code
    ... // your implementations
    assert_eq!(changed, !code_orig.is_equiv(code)); // check validity of return value of `optimize`
    changed
}