opa icon indicating copy to clipboard operation
opa copied to clipboard

Optimize Partial Eval of default functions

Open johanfylling opened this issue 8 months ago • 2 comments

During non-shallow PE, whether a default rule will be added to generated support module(s) is optimized based upon if its return value is compared/unified with a constant value. Because of how the compiler rewrites function calls, default functions don't have the same benefit, as the output argument will always be a non-constant variable without a bound value.

E.g.

package test

default f(x) := true

f(x) := y if {
   y := x.size < 100
}

p if {
   f(input.x) == false
}

is rewritten to

package test

default f(__local0__) := true

f(__local1__) := __local2__ if {
	__local5__ = __local1__.size
	__local3__ = __local5__ < 100
	__local2__ = __local3__
}

p if {
	__local6__ = input.x
	data.test.f(__local6__, __local4__)
	__local4__ = false
}

Possibly, we could update the compiler to reorder generated statements, so that unification happens before the call, so that PE can retrieve any bound constants.

p if {
	__local6__ = input.x
	__local4__ = false
	data.test.f(__local6__, __local4__)
}

This approach must however be investigated to explore whether it breaks any assumptions in other places of compile and eval.

Another alternative could be to detect when the generated output unification expr is superfluous, and can be replaced with a constant value:

p if {
	__local6__ = input.x
	data.test.f(__local6__, false)
}

johanfylling avatar Apr 08 '25 15:04 johanfylling

For further context, see this discussion.

johanfylling avatar Apr 08 '25 15:04 johanfylling

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days. Although currently inactive, the issue could still be considered and actively worked on in the future. More details about the use-case this issue attempts to address, the value provided by completing it or possible solutions to resolve it would help to prioritize the issue.

stale[bot] avatar May 08 '25 16:05 stale[bot]