Zulip Chat Archive

Stream: lean4

Topic: disabling common subexpression elimination


Tomas Skrivan (Oct 10 2023 at 12:53):

I would like to measure how many times a certain function gets called in an expression if naively evaluated. Just adding dbg_trace to that function does not work as there seems to be some form of common subexpression elimination. Consider this code

def foo (x y : Float) : Float :=
  dbg_trace "calling foo"
  x * y

def f (x : Float) := foo x x + foo x x

-- calling foo
-- 8.000000
#eval f 2

it prints out calling foo only once. I would expect twice.

A more complicated example:

code

which prints out calling foo only 17 times, but I would expect 7 + 22 * 2 = 51 times.

I'm generating this kind of code automatically and I want to measure how many times a certain function gets evaluated to get a better idea which code generation is better/worse. I do not want this this measurement be skewed by downstream optimizations.

Tomas Skrivan (Oct 10 2023 at 12:55):

The question is can I disable this common subexpression elimination? Or is there a different way to measure how many times a function gets called when expression is evaluated naively?

James Gallicchio (Oct 10 2023 at 19:36):

wrap it in a state monad that counts each call? :p that might be a bit of a heavy solution though...

James Gallicchio (Oct 10 2023 at 19:37):

(i don't know of any way to turn off the compiler optimizations other than constant lifting...)

Tomas Skrivan (Oct 10 2023 at 23:00):

Yeah I thought of using monads but I do not generate monadic code and turning it into monadic is probably as hard as writing a meta code that inspects the code and counts the number of calls. I really do not know how to approach this apart from effectively writing my own interpreter.


Last updated: Dec 20 2023 at 11:08 UTC