futhark icon indicating copy to clipboard operation
futhark copied to clipboard

Duplication of entry points

Open athas opened this issue 10 months ago • 4 comments

-- Duplication of entry points because monomorphisation thought the
-- second use had a different type (because it is a more specific size
-- instantiation).

entry foo [n] [m] (A: [n][m]i32) = A

entry bar [n] (B: [n][n]i32) = foo B

From #2218. It is not so difficult to fix this with a quick workaround, at the cost of more duplications of size-polymorphic functions. A better solution is for monomorphisation to be slightly smarter about when a specific use of a size-polymorphic function is an instance of the general case. Actually, it is pretty rare that functions should be monomorphised based on sizes - in practice, probably only for sizes in instantiations of lifted type parameters.

athas avatar Feb 10 '25 09:02 athas

Is there any way to work around this in the futhark code, by type ascriptions or such?

FluxusMagna avatar Feb 10 '25 12:02 FluxusMagna

Don't call entry points directly; use a wrapper function (it can be a trivial one, e.g. def foo = bar).

athas avatar Feb 10 '25 12:02 athas

This is another symptom of the arguable misdesign that our entry points are too implicit. We should probably have made them a more distinguished thing requiring explicit descriptions of the external interface. We've had lots of bugs related to the fact that they are functions with special properties tacked on.

athas avatar Feb 10 '25 13:02 athas

How about using attributes to designate entry points? It doesn't have any conflict (obvious to me) with the current system. In principle it could even be used to create multiple entry points from one function. It could also remove potential conflicts in naming between the futhark and the C backend, by placing the entry point name in the attribute rather than using the function name directly.

for example something like

#[entry dot_f32 [n] : [n]f32 ->  [n]f32 -> f32 ]
#[entry dot_f64 [n] : [n]f64 ->  [n]f64 -> f64 ]
def dot as bs = map2 (+) as bs |> reduce  (+) 0

equivalent to

entry dot [n] (as:[n]f64) (bs:[n]f32) : f32 = map2 (+) as bs |> reduce  (+) 0

entry dot [n] (as:[n]f64) (bs:[n]f32) : f64 = map2 (+) as bs |> reduce  (+) 0

Perhaps there could be functionality to instantiate a whole module as entry points with consistent types(such that you can easily pass values between the functions).

I think there are probably much bigger fish to fry, but it would be nice to eventually have a more powerful system for this.

FluxusMagna avatar Feb 11 '25 14:02 FluxusMagna