Skip to content

Inline - #1880

Merged
Bike merged 35 commits into
mainfrom
inline
Nov 11, 2026
Merged

Inline#1880
Bike merged 35 commits into
mainfrom
inline

Conversation

@Bike

@Bike Bike commented Nov 7, 2026

Copy link
Copy Markdown
Member

restores inline functionality, finally. Stores everything as bytecode, but inlining in the sense of actual procedure integration is only done in native code.

There are a few holes, the most severe of which are that if an inline definition has load time values or makes closures, inlining doesn't work - native compilation fails. I've got ideas for fixing it but I just want to merge the basic functionality first.

Bike added 30 commits October 9, 2026 12:24
previously local functions would just be compiled as references of
the constant, which was a bytecode function. So we'd compile the
function (since it was part of the module) but then it would be
eliminated by the entry point checker, and we'd just call the
bytecode function.

compile-file'd modules had a smarter literals mechanism so they
didn't have this problem.

"may help inlining and such". jesus christ i am such a dumbass
these used to be used for cl:macro-function and stuff
This is the organization I've come up with after several prior
designs that had various problems. Basically, we want functions to
be inlinable, at least in native compilation. Furthermore:

1) Compiling a bytecode function should result in ideally zero
   observable semantic changes.
2) The b2b compiler should be as little aware of the environment as
   we can practically manage.
3) The file compiler and loader need to be able to deal with code
   with inline calls.
4) The bytecode compiler should do as little complex optimization
   as possible.
5) We don't want another interchange format for code, since we
   already have bytecode.

So: When a function is declared inline and an inline definition is
available, it's inlined even in bytecode, so that b2b doesn't
change the semantics. The bytecode function is insensitive to
changes in the inline function's global definition, as we'd expect.
However the inline function is not meaningfully incorporated into
the bytecode since that's more complex to do. (Not that much more
complex, so this could be revisited, but it would take work.)
Instead it shows up pretty much like a literal function.
The inline function is a cfunction rather than an actual loaded
function, since cfunctions are already dumpable by the file compiler.

None of this is hooked up right now but it does seem to work from
a very basic test. Next up we need the native compiler to actually
do something useful and correct, and for DEFUN to actually save
inline definitions.
wowee it seemingly works. Need to do a more hardcore test by
actually having inline definitions, tho.
and use them. it works, seemingly. neato beans. But they're not
saved during cross compilation, and they're not _loaded_ from
FASLs which will be a whole other hullaballoo.
1) gotta link, obviously. dopey
2) since the same cfunction can appear multiple times, it can be
   linked multiple times, and that shouldn't damage it. I might
   eventually need some kind of "cmodule but already linked" but
   this seems to work for now.
Using cfunctions directly doesn't work, because we can use a given
inline function more than once, and so try to link the module
multiple times. I thought that might be okay, but it turns out to
cause segfaults due to the "extra" fields going into the
stratosphere. I could have just fixed that, but multiple linking is
unnecessary work anyway. Furthermore we want as few bytecode
functions as possible for the same cfunction.

We also don't want to use loaded bytecode functions, because during
compilation, we don't want to be loading constants for inline
definitions. That would be stupid.

So instead we have this halfway point of "linked" functions and
modules. This is more complex than I'd like due to trying to avoid
multiple linking, but the essential idea is just to link a compiler
module and keep the bytecode and debug info around for dumping by
the file compiler. Loading constants is only done with a function
is in fact loaded.

Again works in small tests but it's not hooked up yet.
btb works with inlining again.
i.e. save inline definitions for later use; everything doing the
use is already hooked up.
breaks native build since saved-functions aren't dumpable yet

Need some compile/runtime environment distinction since inlining
everything for compile-time execution is stupid, and means we need
that internal maybe-intern function.
instead of constructing them anew for each FASL.
Grolter on the Lisp discord pointed out to me the (in retrospect
obvious) fact that since implementations are never required to do
inlining, loading a function without any functions it calls inline
in an environment without those functions is undefined behavior.
That is, the function being inlined has necessarily already been
loaded. (Hypothetically load order shenanigans could be a bit of a
problem here - if we compile an inline definition, use it in some
function, and only load the inlined function afterward - but we
don't do that.)

Long story short we don't need to serialize inline functions. We do
still need linked/saved functions so that the file compiler has
something to work with, but we don't need to dump them except in
CFASLs which is a marginal case. And I'm going to keep the
serialization code since it's fairly trivial and might help later.
so taking out the dump code after all. Keeping the name associated
with the compiler info object makes more sense to me and should
let us stick actual bytecode functions into inline data, which we'll
need if that's what we're loading.
get some use out of all of this. Some other stuff could be inlined
as well but needs some specific thought. In paritcular:
* svref, bit, etc
* standard-instance-access etc
* char< etc
This is basically an expanded version of the mutable LTVs setup.
I've been thinking this is a good idea for a while, but inlining
problems are really pushing it forward. It should hopefully make
the native compiler much more sensible, since what a literal means
is now obvious.
hopefully will make native compilation less chaotic
fixes NIL compilation in weird environments, and results in less
noise in the literal kinds.
this is so much cleaner than the guess-as-we-go shit hot damn
obviously POSITION can get you a kind instead of an index
The immediate cause is inlining. (defun FOO ...) expands into some
code that includes the actual FOO as a closure, so if you compile
the entire module, FOO already has an ENCLOSE instruction, so you
can't make a new one for the inline call.

But this is better anyway. It means we don't have to compile that
extra outer code that we don't need, and it unbreaks my brain as
far as compiling closures go, although that's not yet implemented
correctly.

autocompile is totally broken now but it was crashing anyway. I
think I know how to make it work with this new paradigm but I'll
not bother with that until later.
sorta. LTVs are complicated. The really correct thing to do is to
pick them out of the inline function, which is nutty. Regular
constants we don't really need to worry about.

load-time-values wouldn't normally be used in inlined functions,
except that we actually have them everywhere because we have
(find-class 'foo) expand into something like
(get-class (load-time-value (find-class-holder 'foo) t)).
Whoopsie!
Really we should probably special case class cells/holders in FASLs
but it would be nice to have things be really correct.

Closures are also complicated. If an inline function actually creates
a closure, or even returns a function, that function needs corresponding
bytecode. But different uses of the same inline function could result
in that inner function having different native code after optimization,
so we can't even just grab the bytecode from the definition.
For now we punt entirely but that's pretty suboptimal.

also incidentally fix the handling of named constants in FCGEs
Bike added 3 commits November 6, 2026 11:10
MERGE works on total orders, not partial orders. Stupid me.

It's important to try more specific transforms first. The example
I ran into was (ash (the integer x) 0). Obviously this should
reduce to (the integer x), but because MERGE wasn't doing what I
expected, the (t (integer 0 *)) transform fired first, leaving us
with (ash-right x 0) which is stupid.
the CONS declaration applied to the bindings, but even with a
proper list it ends up being null at the end of things.
Bike added 2 commits November 10, 2026 14:55
faster in cleavir-compilation. Also means these don't use a TYPEP
with a class, so no load-time-value, so they can actually be
inlined without the compiler crying.
not working after my changes, and it wasn't working before either,
with some goofy segfault type problem. AOT compilation is enough
for the time being.
@Bike
Bike merged commit 6dafd1f into main Nov 11, 2026
7 of 12 checks passed
@Bike
Bike deleted the inline branch November 11, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant