[Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound
Date | 2021-10-02 02:12 |
From | Michael Gogins |
Subject | [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I have developed two new plugin opcodes for Csound. They embed the Clang/LLVM just-in-time C++ compiler into Csound, and allow Csound to compile C++ code in the Csound orchestra at run time, and to call code thus compiled, for example as opcodes.
These new opcodes, naturally, require both Csound and the full monty of Clang/LLVM, version 13, to be installed. Currently there is a binary build of the opcodes only for Linux. However, there is only one source code file, and adapting the build script for other operating systems should be straightforward. The opcodes are available at https://github.com/gogins/clang-opcodes. For more information, see the README.md there. I will demonstrate and discuss these opcodes in the upcoming International Csound Users Group virtual meeting on Zoom this coming Wednesday: Topic: International Csound Users Group Time: Oct 6, 2021 01:30 PM Eastern Time (US and Canada) Join Zoom Meeting https://us02web.zoom.us/j/84354088766?pwd=ektPSkpXeFQrUlZYUFM4cVNPMU01QT09 Meeting ID: 843 5408 8766 Passcode: 630670 Regards, Mike ----------------------------------------------------- Michael GoginsIrreducible Productions http://michaelgogins.tumblr.com Michael dot Gogins at gmail dot com |
Date | 2021-10-02 09:57 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Looks good Mike. I'll have a look at them when I get a chance. Prof. Victor Lazzarini
Maynooth University
Ireland
On 2 Oct 2021, at 02:13, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-02 22:09 |
From | luis jure |
Subject | Re: [Csnd] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
el Fri, 1 Oct 2021 21:12:43 -0400 Michael Gogins |
Date | 2021-10-08 22:40 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I’ve managed to build the opcodes and run a simple C code on MacOS by adapting your scripts and CSD. I have not tried yet your factory example because I have to find where the C++ libs etc are to pass to the linker. First attempt led to segfault but that was probably because of these missing libs. Some system header files are giving me lots of spurious warnings but -w will not suppress them not sure why. Next thing I’ll try is to add a simple C opcode and then call it in the Csound code. I think that may be a simpler way to use the compilation opcode (rather than using the factory). Anyway, it’s great that you have started this. It leads to interesting experiments. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 2 Oct 2021, at 02:12, Michael Gogins |
Date | 2021-10-08 22:58 |
From | "Dr. Richard Boulanger" |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Do you think that this might mean that a lot of the code from The Audio Programming Book could be run in Csound? Dr. Richard Boulanger Professor Electronic Production and Design Berklee College of Music > On Oct 8, 2021, at 5:40 PM, Victor Lazzarini |
Date | 2021-10-08 23:29 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Run ldd on existing opcodes to find the libs. On Fri, Oct 8, 2021, 17:40 Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote: I’ve managed to build the opcodes and run a simple C code on MacOS by adapting your scripts and CSD. |
Date | 2021-10-08 23:32 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
If you mean the clang libs use find. If some are missing install the missing features of clang. On Fri, Oct 8, 2021, 18:29 Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-09 15:24 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Ok, giving it the libc++ and C++ header location, I get ####### clang_compile: invoking "csound_main" at 0x104837000: >>>>>>> Hello, world! This proves csound_main has been called with csound: 0x7fdc5ef00000. >>>>>>> This proves a lot of libstdc++ stuff works: strings.size(): 1 strings[0]: A test string... >>>>>>> Now that we have manually defined our own __dso_handle, this proves std::cerr works as well! ####### clang_compile: "csound_main" returned: 0 ******* Trying to invoke Hello... ####### clang_invoke::init: factory name: "hello_factory" ####### clang_invoke::init: factory function: 0x1048373e0 >>>>>>> hello_factory created: 0x7fdc5ecfb850 ####### clang_invoke::init: instance: 0x7fdc5ecfb850 thread: 0 >>>>>>> This proves clang_invoke has called into this module. ####### clang_invoke::init: invokable::init: result: 0 ******* clang_invoke returned: ">>>>>>> This proves clang_invoke can be used as an opcode that returns a string and multiplies a number by 2." and 4 csound command: Segmentation fault ####### clang_compile: deleting JITCompiler 0x7fdc6049a400 and ending execution session. deativate: ip, nxt = 0x7fdc5fbab020 , 0x0 ####### clang_invoke::noteoff So it runs but then it crashes. The debugger says * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x380) frame #0: 0x0000000102bde3b1 clang_opcodes.dylib`llvm::orc::JITCompiler::getSymbolAddress(llvm::StringRef const&) [inlined] llvm::orc::JITCompiler::findSymbol(this=0x0000000000000000, name=0x00007ffeefbff2a8) at clang_opcodes.cpp:178:43 [opt] 175 Expected |
Date | 2021-10-09 15:37 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Some of my pieces get a similar crash at the end on Ubuntu. Others do not crash. Everything is working until Csound is finished performing and begins to exit. In other words it is working the same for you as for me and seems quite usable. I will debug this. Your error messages are helpful. Regards, Mike On Sat, Oct 9, 2021, 10:24 Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote: Ok, giving it the libc++ and C++ header location, I get |
Date | 2021-10-09 17:50 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Ok, I'll carry on experimenting and report back. Prof. Victor Lazzarini
Maynooth University
Ireland
On 9 Oct 2021, at 15:37, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-09 22:11 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
After fixing a bug in Csound, I am now able to use your JIT compiler opcode to add a new opcode and run it. The main difference with the factory/clang_invokable method is that since the opcode is added by the Csound orchestra code, you can only use it in a subsequent compilation. Here’s the CSD example (with MacOS compile flags) |
Date | 2021-10-09 23:15 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Thanks for the fix. I tried this approach myself and it didn't work, which is why I created the ClangInvokable thing, and I suspect it's also why the Faust team used a similar approach. If your fix has been committed, I'll try this myself, in most cases this would be a better way. I think I have a fix for the crash on exit, I'll let you know soon. Regards, Mike On Sat, Oct 9, 2021, 17:11 Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote: After fixing a bug in Csound, I am now able to use your JIT compiler opcode to add a new opcode and run it. |
Date | 2021-10-09 23:58 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
yes, it's in git now. Any new opcodes added after the initialisation were not being added to the parser symbol table. This then caused syntax errors.
I wrote the faust opcodes, at the time it never occurred to me to add these as normal opcodes. I just took advantage of their JIT and factory mechanisms. I suspect that adding faust opcodes as normal opcodes would require us to implement our own JIT, as
I am not sure it may be easy or even possible to repurpose theirs.
Prof. Victor Lazzarini
Maynooth University
Ireland
On 9 Oct 2021, at 23:15, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-10 03:00 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I think I have fixed the crash on exit. In git now. On Sat, Oct 9, 2021, 18:15 Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-11 13:57 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Thanks, I’ll have a go as soon as I can get round to. In the meanwhile, I have been studying your code and the clang llvm interpreter example and I put together a simplified version of the opcode just designed to compile new opcodes (without dependencies). This currently supports C opcodes only, but I’ll look at adding C++ support later. https://github.com/vlazzarini/opcode_compiler The idea is to use this as a platform for exploring how to compile UDOs down to fast LLVM code. UDOs (without any opcode calls to start with) would be parsed to a csound C opcode and then given to the JIT compiler. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 10 Oct 2021, at 03:00, Michael Gogins |
Date | 2021-10-11 14:15 |
From | Rory Walsh |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I like the look of this a lot guys. Thanks for spending time on it. Victor, in your example you create an orchestra and pass it to compilestr(). I understand you need to recompile to have the opcode available. This seems a little clunky. Don't get me wrong, this is awesome, but is there any way the opcode_compile() call could also recompile, or rerun the orc without the user having to do so? Or could these opcodes be placed in a unique |
Date | 2021-10-11 15:34 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
yes, it’s not ideal. The issue is that when the JIT is run, Csound is already running and the Csound code has already been compiled. If we use a plugin to deploy the JIT as we are doing now, then there is no way round this. An alternative is to build the JIT inside Csound and use a CSD section for plugins. However that means a big dependency being brought into Csound, which is not ideal. However, we can make it an optional feature. In any case, that would be a CS7 thing. One snag with this is that at the moment, the JIT needs a set of system headers that get include when we add csdl.h. That’s not ideal, we should be able to just require Csound-provided headers. I think it is possible to fix this, but we will need to be careful where we place headers in the Csound code. At the moment, since we never needed to watch out for this, they are all over the place. I managed to move stdio.h so it’s only included where it is needed, but there are a number of other headers. So there’s a lot of experimentation ahead. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 11 Oct 2021, at 14:15, Rory Walsh |
Date | 2021-10-11 16:03 |
From | Rory Walsh |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Ah yes, the less dependencies the better. I couldn't agree more with this statement! So once you call compile_opcode(), any subsequent orc compiles will have access to the new custom opcode. Could this be out-sourced to the frontend? For example, could the opcode_compile() opcode trigger a callback that can be set via the API. When it is triggered the frontend could recompile the same orc, only this time ignoring the opcode_compile() call? Also, does this handle multiple opcodes with a single call? If not, I think it should. And if so, now might be a good time to name it compile_opcodes()..? This seems more consistent with other opcodes, i.e, compileorc, compilecsd, etc. On Mon, 11 Oct 2021 at 15:34, Victor Lazzarini |
Date | 2021-10-11 16:23 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
You see you can’t add the new opcode in your initial csound code because that’s not there and it will fail. Once it’s compiled then you can send in an instrument that will use it, so I think callbacks are just complicating the issue. If you add a call to compilestr following the addition of the opcode, then it will work (as in the example). The JIT will take any amount of C code you want to throw at it. So as many opcodes as you’d like. Compile opcodes sounds better alright. I’ll see about changing it. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 11 Oct 2021, at 16:03, Rory Walsh |
Date | 2021-10-11 16:37 |
From | Rory Walsh |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
It's a pity. But if it's the only way then there isn't much one can do about it. Btw, I tried building on my Ubuntu laptop with 18.04, but couldn't install llvm 13 due to screwed up dependencies. I have a PC at home with 20.04, it should build Ok there I think. On Mon, 11 Oct 2021 at 16:23, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote: You see you can’t add the new opcode in your initial csound code because that’s not there and |
Date | 2021-10-11 16:40 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I’ve settled on “module compiler” as it may be used to add other things including callbacks and function tables. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 11 Oct 2021, at 16:23, Victor Lazzarini |
Date | 2021-10-11 18:37 |
From | Rory Walsh |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I can't convince you to put "compile" at the start, to make it consistent with other opcodes? On Mon 11 Oct 2021, 4:41 p.m. Victor Lazzarini, <Victor.Lazzarini@mu.ie> wrote: I’ve settled on “module compiler” as it may be used to add other things including callbacks and |
Date | 2021-10-11 18:46 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I think it's better to make it different from the other opcodes, because this is for C and the others are Csound code. Prof. Victor Lazzarini
Maynooth University
Ireland
On 11 Oct 2021, at 18:37, Rory Walsh <rorywalsh@ear.ie> wrote:
|
Date | 2021-10-11 21:07 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
My initial invention of these opcodes does not impose a need for recompiling to have the opcode available, but in that case it is necessary to use the clang_invoke opcode which serves as a proxy for the C++ module. But this is not difficult and works perfectly. Please take a look at https://github.com/gogins/clang-opcodes/blob/main/clang_hello.csd where instr 1 invokes "hello_factory" as an opcode. Regards, Mike ----------------------------------------------------- Michael GoginsIrreducible Productions http://michaelgogins.tumblr.com Michael dot Gogins at gmail dot com On Mon, Oct 11, 2021 at 10:15 AM Rory Walsh <rorywalsh@ear.ie> wrote: I like the look of this a lot guys. Thanks for spending time on it. |
Date | 2021-10-12 06:55 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I think that's a good approach, using the factory.
My intention is to use this as an experiment to see how best to deploy a JIT to compile Csound code. So it's really far from the end destination. You've shown the way and I am following that path.
Prof. Victor Lazzarini
Maynooth University
Ireland
On 11 Oct 2021, at 21:08, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-12 11:24 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
I like your idea of using the JIT compiler to compile Csound code. I thought about that myself, but I had no idea how to do it, and dreaded the thought of learning how to do it. And I recognize that this requires eliminating any confusing dependencies. I could introduce a no-dependency variant of clang_compile into my repository, or you could introduce a variant of your compiler opcode that takes cflags and dynamic link libraries. We are talking, I assume, about dependencies of modules that are compiled with these opcodes. We are NOT talking about extra dependencies of the JIT compiler opcodes themselves, correct? Regards, Mike On Tue, Oct 12, 2021, 01:55 Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
|
Date | 2021-10-12 18:15 |
From | Victor Lazzarini |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
One of my concerns is to avoid bringing a big dependency to the Csound project. That's what I meant about the issues of integration with the system through means that are not plugin-based.
The other issue is to do with wider distribution and support. While most of the libs are statically linked (llvm), some are dylibs (clang) which would need to be bundled. This is not a major problem as we have dealt with it many times, but requires some
planning. The difficulty is to do with system headers that are required when you include csdl.h. In linux, I think that's ok because they should be always available in /usr/include and if not it's just a package install.
On MacOS, it requires a whole XCode installation, which is a major pain. I also could not locate some headers in this installation and have had to use what comes with the clang lib installation, which makes it even more of a mess.
With Windows, I suspect we would be entering a world of pain, as Walter once said to Smokey.
Prof. Victor Lazzarini
Maynooth University
Ireland
On 12 Oct 2021, at 11:24, Michael Gogins <michael.gogins@gmail.com> wrote:
|
Date | 2021-10-12 22:41 |
From | Michael Gogins |
Subject | Re: [Csnd-dev] [EXTERNAL] [Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound |
Thanks for the explanation. Your concerns obviously are valid if the objective is to put this in core Csound. I hope we can work this all out. I think the DSP stuff in C/C++ when JITed runs 2 to 3 times faster than the same algorithm in Csound orchestra code. I know, because I have benchmarked it, that a big Faust opcode translated to C++ then compiled as a plugin opcode -Ofast -march=native runs only about 3 percent faster than the same code JIT compiled with the same flags. Regards, Mike On Tue, Oct 12, 2021, 13:15 Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
|