cakeml icon indicating copy to clipboard operation
cakeml copied to clipboard

WordLang CSE is not doing enough

Open myreen opened this issue 1 year ago • 0 comments

Using the new --explorer output, I found a bad code pattern. I gave the compiler the following code to compile:

fun add_all x =
  case x of
    A a => (a,a,a)
  | B a b => (a,b,b)
  | C a b c => (a,b,c);

Now looking at the code for loading a, b, c in the last line, I see:

              (move 0 ((417 := 389)))
              (inst (Arith (Shift Lsr 421 417 9)))
              (425 := CurrHeap Add 421)
              (inst (Mem Load 429 (Addr 425 0x8)))
              (move 0 ((433 := 389)))
              (inst (Arith (Shift Lsr 437 433 9)))
              (441 := CurrHeap Add 437)
              (inst (Mem Load 445 (Addr 441 0x10)))
              (move 0 ((449 := 389)))
              (inst (Arith (Shift Lsr 453 449 9)))
              (457 := CurrHeap Add 453)
              (inst (Mem Load 461 (Addr 457 0x18))) 

And this is after common subexpression elimination in WordLang. It ought to be:

              (move 0 ((417 := 389)))
              (inst (Arith (Shift Lsr 421 389 9)))
              (425 := CurrHeap Add 421)
              (inst (Mem Load 429 (Addr 425 0x8)))
              (move 0 ((433 := 389)))
              (move 0 ((433 := 421)))
              (move 0 ((441 := 425)))
              (inst (Mem Load 445 (Addr 425 0x10)))
              (move 0 ((449 := 389)))
              (move 0 ((453 := 421)))
              (move 0 ((457 := 425)))
              (inst (Mem Load 461 (Addr 425 0x18)))  

which after dead code elimination ought to be:

              (move 0 ((417 := 389)))
              (inst (Arith (Shift Lsr 421 389 9)))
              (425 := CurrHeap Add 421)
              (inst (Mem Load 429 (Addr 425 0x8)))
              (inst (Mem Load 445 (Addr 425 0x10)))
              (inst (Mem Load 461 (Addr 425 0x18)))  

i.e. loads generated by straightforward pattern matching should be just a list of loads at this point.

myreen avatar Aug 08 '23 21:08 myreen