Safe Haskell | None |
---|---|
Language | Haskell2010 |
IR.InsertRefCounting
Description
This inserts dup
and drop
primitives according to a caller dup
,
callee drop
policy. The value returned by a function should be
passed back referenced (ownership transfers from the callee back to
the caller).
The dup : a -> a
primitive behaves like the identity function,
evaluating and returning its first argument and increasing the
reference count on the result. It is meant to be wrapped around
function arguments.
The drop : a -> b -> a
primitive evaluates and returns its first
argument. It decrements the reference count to its second argument
after it has evaluated its first argument. It is meant to be wrapped
around function bodies that need to use and then de-reference their
arguments.
Thus, something like
add a b = a + b
becomes
add a b = drop (drop ((dup a) + (dup b)) b) a
Arguments a
and b
to the +
primitive are duplicated and the
result of +
is duplicated internally, so add
does not need to
duplicate its result. Both arguments a
and b
are dropped.
Try running sslc --dump-ir-final
on an example to see the inserted
dup
and drop
constructs.
Our approach was inspired by Perceus https://www.microsoft.com/en-us/research/publication/perceus-garbage-free-reference-counting-with-reuse/