Skip to content

Optimize Partial Eval of default functions #7500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
johanfylling opened this issue Apr 8, 2025 · 1 comment
Open

Optimize Partial Eval of default functions #7500

johanfylling opened this issue Apr 8, 2025 · 1 comment

Comments

@johanfylling
Copy link
Contributor

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
Copy link
Contributor Author

For further context, see this discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant