> > However, if your instructions aren't first class (i.e. can't be passed
> > or returned to / from methods, or stored / loaded from variables), then
[quoted text clipped - 14 lines]
> Because it is restricted to the IL level, the composed IL functions
> could not come from an untrusted source.
> > > > I was wondering if there has been any research into adding higher-
> > > > order instruction to the CIL? In other words instructions that either
[quoted text clipped - 16 lines]
> Sure, but the CLR (& JVM) don't model von Neumann machines, as I sent a
> comment to your blog :)
That doesn't matter though. The point is that if we simply add typed
higher order instructions to a stack machine these can be trivially
translated to a von neuman machine (i.e. common CPU architecture). For
those interested a naive (read unoptimized) scheme for translating
from higher order stack instructions to assembly can be found at
http://cdiggins.com/2007/04/11/cat-higher-order-instructions-to-assembly/
> And I take slight exception to suggesting that 'data as instructions' is
> being not uncommon in assembly, because it's effectively self-modifying
> code, which has a bad reputation of being hard to understand and debug -
Yes the technique is uncommon in human written code, but there is no
reason not to generate it.
> which is why we use higher order models that have type systems etc. that
> can be more formally & rigourously tested etc.
Yes, like the Cat type system (I'll refer readers again to
http://www.cat-language.com/paper.html )
> My point was efficiency though, and even more importantly, intuitiveness
> of the efficiency of imperative code in the ordinary linear style, with
> similar arguments to the old "worse is better" story re C/Lisp etc.
I don't understand what you are saying.
> You can't deny that your ideas are very similar to a kind of 'Lisp on a
> stack' :)
I would characterize the ideas as more of "Haskell" on a stack, due to
the type safe nature of the operations. Lisp is just too flexible to
afford the kinds of optimizations and safety we need from intermediate
languages.
> It's not something I'd rule out on this kind of basis though, don't get
> me wrong, I love writing compilers and this kind of thing has appeal. I
> only expand on it because it's the biggest thing I see.
> > > However, if your instructions aren't first class (i.e. can't be passed
> > > or returned to / from methods, or stored / loaded from variables), then
[quoted text clipped - 23 lines]
>
> * every method has its own stack
That restriction is still fine. I can combine functions through
inlining.
> * by simulating instructions, it should not be possible for the stack to
> ever have a different height or type composition for any possible
> execution path - this is the foundation of verification
This is guaranteed by the type system.
> * the stack is not arbitrarily permutable without using external storage
> like a local etc.
This is also guaranteed by the type system.
> Would your proposed changes break these things?
Nope.
> In particular, would
> these composed instructions turn into .NET delegates at any point?
Nope.
> Or
> would they be condemned to live only on the frame of the method that
> created them?
Well I would expect one to be able to return opcode blocks from
methods.
So let me be clear here, what I propose requires the introduction of a
new data type, which consists of a list of opcodes. It is like a
method, except it isn't ever seen by the user, only compiler writers.
They use it to emulate higher order functions.
> > > Also, as it exists, CIL can be trivially interpreted, in a pinch (type
> > > info added to stack values or to instructions after single-pass
[quoted text clipped - 8 lines]
> up an object. With this model, I'm trying to figure out what you'd be
> pushing on after one of those compose operations.
An opcode block. An array of raw opcodes allocated either on the stack
(if small enough) or on the heap.
> It seems to me to be a
> dynamically allocated structure from the GC heap, since it can be grown
> to arbitrary size with successive compose operations, yet it's not an
> object.
Yes, we can at the very least make a simple optimization to separate
between small opcode blocks (allocated on the stack and "copied") and
large opcode blocks, placed on the heap.
> Also, it needs to magically turn into machine code or stay as a
> graph, depending on whether it's going to be called or composed again.
I don't see why you wouldn't just turn it into machine code right off
the bat, and use that as your representation.
> Turning it into machine code for calling isn't going to be totally
> trivially cheap, it's going to have to enter a compiler somewhere...
Well yes, just like any byte code.
> If it turns into a delegate, won't you want to cache that somewhere, to
> avoid getting that hit again next time... and doesn't this work seem
> like not such a big win over just doing it yourself with
> DynamicMethod...?
Sorry, you lost me.
> > It is true that
> > higher order functions would require a graph reduction machine,
[quoted text clipped - 8 lines]
> off if the runtime *interpreted* your instructions - or at least, that's
> the only way to get a net win out of it.
You have come to the conclusion that interpreting higher order
functions would be faster than compiling? I completely disagree.
What would you say, if I generated assembly code for the following
example:
int[] a = new array[1000000];
for (int i=0; i < a.length(); ++i) a[i] = i;
Map(a, MapDelegate(int x) { return x % 2 == 0 ? x * 2 : x })
int sum = Fold(a, FoldDelegate(int x, int y) { return x + y; })
And it was over 20 times as fast as C#? Would you buy me a pint of
guinness?
Cheers,
Christopher Diggins
Barry Kelly - 12 Apr 2007 21:48 GMT
> > My point was efficiency though, and even more importantly, intuitiveness
> > of the efficiency of imperative code in the ordinary linear style, with
> > similar arguments to the old "worse is better" story re C/Lisp etc.
>
> I don't understand what you are saying.
I was referring to this essay:
http://www.dreamsongs.com/WIB.html
> So let me be clear here, what I propose requires the introduction of a
> new data type, which consists of a list of opcodes. It is like a
> method, except it isn't ever seen by the user, only compiler writers.
> They use it to emulate higher order functions.
What I'm trying to get at, to make myself clear, is that in the CLR
model, opcodes are compiled by the JIT compiler into machine code; yet
your instructions manipulate opcodes - that is, they manipulate the
source code of the JIT compiler. (This is what I was referring to with
the Lisp reference.)
> > Also, it needs to magically turn into machine code or stay as a
> > graph, depending on whether it's going to be called or composed again.
>
> I don't see why you wouldn't just turn it into machine code right off
> the bat, and use that as your representation.
How would it allocate registers? How are arguments passed in? How does
it compose, yet ensure that the parts composed don't clobber each
other's registers and locals?
Efficient allocation of registers is probably the most important thing a
compiler does that relies on seeing as much of the source structure as
possible before doing stuff, and is also one of the more important
issues in efficient compilation.
> > Turning it into machine code for calling isn't going to be totally
> > trivially cheap, it's going to have to enter a compiler somewhere...
>
> Well yes, just like any byte code.
My point being though, that it will have to enter the compiler every
time, rather than just once, if the operations created through
composition are to be efficient. The alternative is to have a fast
interpreter, maybe using your self-described naive scheme where simple
cookie-cutter machine code blocks get pasted together.
I will say that I know much thought has gone into this kind of thing in
the Lisp world, and I'm not deeply familiar with optimization techniques
that have been applied there, so I'm actually unqualified to object to
it. But I brought it up anyway, since no one else is reacting :)
> > If it turns into a delegate, won't you want to cache that somewhere, to
> > avoid getting that hit again next time... and doesn't this work seem
> > like not such a big win over just doing it yourself with
> > DynamicMethod...?
>
> Sorry, you lost me.
I was musing out loud about possibilities for efficient compilation. I
can't get specific without trying to design something that would work,
which would take too much time out of my day, unfortunately. Though, see
the thoughts below...
> > > It is true that
> > > higher order functions would require a graph reduction machine,
[quoted text clipped - 11 lines]
> You have come to the conclusion that interpreting higher order
> functions would be faster than compiling? I completely disagree.
Yes, I would say that interpreting would typically be faster than an
efficient compiler for less-complex inputs, i.e. dependent on a low 'n'.
But when I think about it now, it's like layering e.g. the Hotspot JIT
inside itself; that is, the JIT generates code that interprets yet falls
over to (re)compilation if enough reps are done...
You'll have to forgive me, I'm a little slow for new ideas :)
> What would you say, if I generated assembly code for the following
> example:
[quoted text clipped - 6 lines]
> And it was over 20 times as fast as C#? Would you buy me a pint of
> guinness?
Well now, technically, a Lisp compiler could evaluate that at compile
time :P
The main limiting factors on the speed of typical CLR & C#
implementation of above snippet are the cost of calling through a
delegate, and the opaqueness of a delegate to optimization - that is,
when the compiler is processing the definitions of Map and Fold, it
can't use information about the actual arguments to optimize as much as
it could if things were coded as a loop. That isn't to say that Map &
Fold aka Reduce don't have interesting optimization strategies in their
own right, the point is that the extra indirection of delegates is a
black box for the compiler.
I'm not sure what you're suggesting will improve that specifically
though, it's more oriented towards dynamic composition of new functions,
as I see it - and that case certainly isn't served well by the current
.NET delegate paradigm, because every composition accumulates delegate
invocation costs. Let me make that concrete:
bool P(bool value);
P Not(P pred)
{ return delegate(bool x) { return !pred(x); }; }
P And(P left, T right)
{ return delegate(bool x) { return left(x) && right(x); }; }
// ...
If someone wants to compose a predicate dynamically in C#/.NET,
currently they accumulate these costs. They can mitigate these costs
with the new stuff coming in C# 3.0 though, with LINQ Func<> trees,
which can be passed to a compiler and get the benefit of full
compilation. However, that forces the user to make the interpretation /
compilation tradeoff themselves.
OK. Now that I've thought about it more, it looks interesting, and with
the nesting of JIT compilation, it could be made efficient in both the
seldom-called and repeatedly-called cases. Have you considered modifying
e.g. Mono or one of the other open-source VM implementations to try and
get something along these lines? Because it seems to me that this kind
of thing could indeed make the cases above better, e.g. lambda
constructions could be improved.
I'm curious: can the compose operation itself be composed? :) That would
be something which would be particularly painful to handle in the LINQ
sense, although applications for it don't exactly jump out at me.
-- Barry

Signature
http://barrkel.blogspot.com/
Christopher Diggins - 15 Apr 2007 17:40 GMT
[snip]
> > > Also, it needs to magically turn into machine code or stay as a
> > > graph, depending on whether it's going to be called or composed again.
[quoted text clipped - 5 lines]
> it compose, yet ensure that the parts composed don't clobber each
> other's registers and locals?
I won't get into this here, but in many cases these are easily solved
with a bit of compiler intelligence.
> Efficient allocation of registers is probably the most important thing a
> compiler does that relies on seeing as much of the source structure as
> possible before doing stuff, and is also one of the more important
> issues in efficient compilation.
I would say parallelization of instructions is more important now on
multi-core machines.
> > > Turning it into machine code for calling isn't going to be totally
> > > trivially cheap, it's going to have to enter a compiler somewhere...
[quoted text clipped - 4 lines]
> time, rather than just once, if the operations created through
> composition are to be efficient.
No, not neccessarily.
> The alternative is to have a fast
> interpreter, maybe using your self-described naive scheme where simple
> cookie-cutter machine code blocks get pasted together.
That's one approach. There are more sophistciated approaches as well.
> I will say that I know much thought has gone into this kind of thing in
> the Lisp world, and I'm not deeply familiar with optimization techniques
> that have been applied there, so I'm actually unqualified to object to
> it. But I brought it up anyway, since no one else is reacting :)
Okay, but you have to realize that in typed functional languages like
OCaml and Haskell there are new classes of optimization techniques,
like higher order function fusion, and deforestation, which is very
effective. Untyped languages like Lisp/Scheme, have their own set of
problems which obfuscate the issues of compiling higher order
functions.
[snip]
> Yes, I would say that interpreting would typically be faster than an
> efficient compiler for less-complex inputs, i.e. dependent on a low 'n'.
> But when I think about it now, it's like layering e.g. the Hotspot JIT
> inside itself; that is, the JIT generates code that interprets yet falls
> over to (re)compilation if enough reps are done...
That is simply not the case, but I'm not going to argue it anymore.
> You'll have to forgive me, I'm a little slow for new ideas :)
>
[quoted text clipped - 11 lines]
> Well now, technically, a Lisp compiler could evaluate that at compile
> time :P
Well I intended 100000, and 2 to be run-time values, but either way it
demonstrates the same thing:
if we had higher-order isntructions in the byte-code we could pre-
evaluate many instructions at compile-time.
I think you are stuck with the assumption that higher-order
instructions always neccessitate dynamic functions, where in the
majority of cases the higher-order expressions can be expanded inline
and pre-evaluated.
> The main limiting factors on the speed of typical CLR & C#
> implementation of above snippet are the cost of calling through a
[quoted text clipped - 5 lines]
> own right, the point is that the extra indirection of delegates is a
> black box for the compiler.
And delegates are black boxes because higher order code can't be
expressed as byte-code.
One of the points of higher-order instructiosn is to be able to apply
the optimizations strategies of Map and Fold to the byte-code.
I'm going to have to stop the conversation dead here thought because I
simply don't have time to continue and I've made my point as clear as
I can. Whether the CLR team wants to consider these ideas is their own
choice (but you'd think at least one member of the team would have had
something to say by this point?!). Anyway I will continue work on Cat,
and the Cat to assembly compiler for now.
Thanks for the stimulating discourse Barry,
Christopher Diggins
http://www.cdiggins.,com
Barry Kelly - 15 Apr 2007 22:12 GMT
> > I will say that I know much thought has gone into this kind of thing in
> > the Lisp world, and I'm not deeply familiar with optimization techniques
[quoted text clipped - 7 lines]
> problems which obfuscate the issues of compiling higher order
> functions.
I'm aware it - not intimately, but aware of the possibilities when lots
of information is available and dependable, unlike in memory unsafe
languages such as C etc. Unfortunately, I can't use it in my day to day
work.
> [snip]
> > Yes, I would say that interpreting would typically be faster than an
[quoted text clipped - 4 lines]
>
> That is simply not the case, but I'm not going to argue it anymore.
No, it's really quite simple. Roughly, if the cost of compiling some
snippet is C, and the extra cost of interpreting it is I (extra as in
greater than executing it directly), it will be better to interpret it
the first n times if I < C, until n*I > C, if the number of iterations
isn't known beforehand to be more than n. And it's a sad fact that with
today's libraries etc., most executable code is never actually executed
once in a typical run, much less n times.
> I think you are stuck with the assumption that higher-order
> instructions always neccessitate dynamic functions, where in the
> majority of cases the higher-order expressions can be expanded inline
> and pre-evaluated.
Sure, I'll grant you that I can't stray too far from what your typical
C# programmer would expect, even if I might want to, for very boring
reasons that are too depressing to expand on.
> One of the points of higher-order instructiosn is to be able to apply
> the optimizations strategies of Map and Fold to the byte-code.
Yes. I don't know if you realised it, but you changed my mind as I was
writing my previous response. "Morbo will store this in his sack for
future digestion" - you never know when this might pop up again :)
> I'm going to have to stop the conversation dead here thought because I
> simply don't have time to continue and I've made my point as clear as
[quoted text clipped - 4 lines]
>
> Thanks for the stimulating discourse Barry,
Thanks to you too.
-- Barry

Signature
http://barrkel.blogspot.com/