Can compiler macros make use of type declarations?

Joshua TAYLOR joshuaaaron at gmail.com
Thu Nov 9 12:31:59 UTC 2017


On Mon, Nov 6, 2017 at 3:43 PM, Chaitanya Gupta <mail at chaitanyagupta.com> wrote:
> Let's say I have created a function called `FOO`:
>
> (defun foo (x)
>   ...)
>
> To optimize this function, I write a compiler macro. Can I make use of
> type declarations that users of `FOO` might have made for the argument
> `X` that is passed in to it?
>
> That is, let's say `FOO` is used like this:
>
> (let ((a (something-that-returns-an-integer)))
>   (declare (integer a))
>   (foo a))
>
> The compiler macro for `FOO` cannot make any optimizations on the
> value of `A`, but can it take advantage of the fact that `A` is
> declared as in `INTEGER` here?

There has  been plenty of discussion about how you might access the
environment, etc., to get this information, and whether it's portable,
etc. Depending on your use case, you could make use of the [THE]
special operator at the call site, and you'd be guaranteed to have it
available to the compiler macro. That is, you'd now write

    (foo (the fixnum a))

That works regardless of what environment introspection you can get,
because you see (THE FIXNUM A) when you're expanding the compiler
macro, and from that you could check the type, and if the use case is
that it's very important to do some optimization at compile time, this
might be an acceptable tradeoff.

[THE]: http://www.lispworks.com/documentation/HyperSpec/Body/s_the.htm


-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/<div
id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br />
<table style="border-top: 1px solid #D3D4DE;">
	<tr>
        <td style="width: 55px; padding-top: 13px;"><a
href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon"
target="_blank"><img
src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif"
alt="" width="46" height="29" style="width: 46px; height: 29px;"
/></a></td>
		<td style="width: 470px; padding-top: 12px; color: #41424e;
font-size: 13px; font-family: Arial, Helvetica, sans-serif;
line-height: 18px;">Virus-free. <a
href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link"
target="_blank" style="color: #4453ea;">www.avast.com</a>
		</td>
	</tr>
</table><a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1"
height="1"></a></div>



More information about the pro mailing list