Csound Csound-dev Csound-tekno Search About

[Csnd-dev] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound

Date2021-10-02 02:12
FromMichael 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 Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

Date2021-10-02 09:57
FromVictor Lazzarini
SubjectRe: [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:



*Warning*

This email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.

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 Gogins
Irreducible Productions
http://michaelgogins.tumblr.com
Michael dot Gogins at gmail dot com

Date2021-10-02 22:09
Fromluis jure
SubjectRe: [Csnd] ANNOUNCEMENT: Embedded C++ JIT Compiler for Csound
el Fri, 1 Oct 2021 21:12:43 -0400
Michael Gogins  escribió:

> 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.

Sounds terrific! Congratulations, Mike.


-- 
ljc 

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2021-10-08 22:40
FromVictor Lazzarini
SubjectRe: [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  wrote:
> 
> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> 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 Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com


Date2021-10-08 22:58
From"Dr. Richard Boulanger"
SubjectRe: [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  wrote:
> 
> 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  wrote:
>> 
>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>> 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 Gogins
>> Irreducible Productions
>> http://michaelgogins.tumblr.com
>> Michael dot Gogins at gmail dot com
> 

Date2021-10-08 23:29
FromMichael Gogins
SubjectRe: [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.

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 <michael.gogins@gmail.com> wrote:
>
> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> 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 Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com


Date2021-10-08 23:32
FromMichael Gogins
SubjectRe: [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:
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.

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 <michael.gogins@gmail.com> wrote:
>
> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> 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 Gogins
> Irreducible Productions
> http://michaelgogins.tumblr.com
> Michael dot Gogins at gmail dot com


Date2021-10-09 15:24
FromVictor Lazzarini
SubjectRe: [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 findSymbol(const StringRef &name)
   176 	    {
   177 	        return execution_session.lookup({&main_jit_dylib}, Mangle(name));
-> 178 	    }
   179 	    Expected getSymbolAddress(const StringRef &name)
   180 	    {
   181 	        auto symbol = findSymbol(name);


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
> 
> 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  wrote:
> Run ldd on existing opcodes to find the libs.
> 
> On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
> 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  wrote:
> > 
> > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > 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 Gogins
> > Irreducible Productions
> > http://michaelgogins.tumblr.com
> > Michael dot Gogins at gmail dot com
> 


Date2021-10-09 15:37
FromMichael Gogins
SubjectRe: [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

####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
   176      {
   177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
-> 178      }
   179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
   180      {
   181          auto symbol = findSymbol(name);


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>
> 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:
> 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.
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > 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 Gogins
> > Irreducible Productions
> > http://michaelgogins.tumblr.com
> > Michael dot Gogins at gmail dot com
>


Date2021-10-09 17:50
FromVictor Lazzarini
SubjectRe: [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:


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

####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
   176      {
   177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
-> 178      }
   179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
   180      {
   181          auto symbol = findSymbol(name);


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>
> 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:
> 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.
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > 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 Gogins
> > Irreducible Productions
> > http://michaelgogins.tumblr.com
> > Michael dot Gogins at gmail dot com
>


Date2021-10-09 22:11
FromVictor Lazzarini
SubjectRe: [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)



-odac --opcode-lib="./clang_opcodes.dylib" 


0dbfs = 1

SCode = {{
 #include 
 struct DATASPACE {
    OPDS h;
    MYFLT *out, *in, *gain;
 };

 static int init(CSOUND *csound, DATASPACE *p) {
       return OK;
 }

 static int perf(CSOUND *csound, DATASPACE *p) {
    MYFLT *out = p->out, *in = p->in, g = *p->gain;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;

    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&out[nsmps],0, early*sizeof(MYFLT));
    }

    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
    return OK;
 }

 void* __dso_handle = (void *)&__dso_handle;
 extern "C" int csound_main(CSOUND *csound) {
    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
                         (SUBR) init, (SUBR) perf, NULL);
    return 0;
 };
}}

SCscode = {{
    instr 1
     a1 oscili 1,440
     a2 amp a1, 0.5
     out a2
    endin
}}

SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
SLflags = ""

ires clang_compile "csound_main", SCode, SCflags, SLflags
ires compilestr SCscode



i1 0 2

 


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
> 
> Ok, I'll carry on experimenting and report back.
> 
> Prof. Victor Lazzarini
> Maynooth University
> Ireland
> 
>> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
>> 
>> 
>> 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  wrote:
>> 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 findSymbol(const StringRef &name)
>>    176      {
>>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>> -> 178      }
>>    179      Expected getSymbolAddress(const StringRef &name)
>>    180      {
>>    181          auto symbol = findSymbol(name);
>> 
>> 
>> ========================
>> Prof. Victor Lazzarini
>> Maynooth University
>> Ireland
>> 
>> > On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
>> > 
>> > 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  wrote:
>> > Run ldd on existing opcodes to find the libs.
>> > 
>> > On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
>> > 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  wrote:
>> > > 
>> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>> > > 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 Gogins
>> > > Irreducible Productions
>> > > http://michaelgogins.tumblr.com
>> > > Michael dot Gogins at gmail dot com
>> > 
>> 


Date2021-10-09 23:15
FromMichael Gogins
SubjectRe: [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.
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)

<CsoundSyntheizer>
<CsOptions>
-odac --opcode-lib="./clang_opcodes.dylib"
</CsOptions>
<CsInstruments>
0dbfs = 1

SCode = {{
 #include <csdl.h>
 struct DATASPACE {
    OPDS h;
    MYFLT *out, *in, *gain;
 };

 static int init(CSOUND *csound, DATASPACE *p) {
       return OK;
 }

 static int perf(CSOUND *csound, DATASPACE *p) {
    MYFLT *out = p->out, *in = p->in, g = *p->gain;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;

    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&out[nsmps],0, early*sizeof(MYFLT));
    }

    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
    return OK;
 }

 void* __dso_handle = (void *)&__dso_handle;
 extern "C" int csound_main(CSOUND *csound) {
    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
                         (SUBR) init, (SUBR) perf, NULL);
    return 0;
 };
}}

SCscode = {{
    instr 1
     a1 oscili 1,440
     a2 amp a1, 0.5
     out a2
    endin
}}

SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
SLflags = ""

ires clang_compile "csound_main", SCode, SCflags, SLflags
ires compilestr SCscode

</CsInstruments>
<CsScore>
i1 0 2
</CsScore>
</CsoundSynthesizer>


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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:
>>
>> 
>> 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
>>
>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>    176      {
>>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>> -> 178      }
>>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>    180      {
>>    181          auto symbol = findSymbol(name);
>>
>>
>> ========================
>> Prof. Victor Lazzarini
>> Maynooth University
>> Ireland
>>
>> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>> >
>> > 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:
>> > 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.
>> >
>> > 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 <michael.gogins@gmail.com> wrote:
>> > >
>> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>> > > 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 Gogins
>> > > Irreducible Productions
>> > > http://michaelgogins.tumblr.com
>> > > Michael dot Gogins at gmail dot com
>> >
>>


Date2021-10-09 23:58
FromVictor Lazzarini
SubjectRe: [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:


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.
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)

<CsoundSyntheizer>
<CsOptions>
-odac --opcode-lib="./clang_opcodes.dylib"
</CsOptions>
<CsInstruments>
0dbfs = 1

SCode = {{
 #include <csdl.h>
 struct DATASPACE {
    OPDS h;
    MYFLT *out, *in, *gain;
 };

 static int init(CSOUND *csound, DATASPACE *p) {
       return OK;
 }

 static int perf(CSOUND *csound, DATASPACE *p) {
    MYFLT *out = p->out, *in = p->in, g = *p->gain;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;

    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&out[nsmps],0, early*sizeof(MYFLT));
    }

    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
    return OK;
 }

 void* __dso_handle = (void *)&__dso_handle;
 extern "C" int csound_main(CSOUND *csound) {
    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
                         (SUBR) init, (SUBR) perf, NULL);
    return 0;
 };
}}

SCscode = {{
    instr 1
     a1 oscili 1,440
     a2 amp a1, 0.5
     out a2
    endin
}}

SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
SLflags = ""

ires clang_compile "csound_main", SCode, SCflags, SLflags
ires compilestr SCscode

</CsInstruments>
<CsScore>
i1 0 2
</CsScore>
</CsoundSynthesizer>


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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:
>>
>> 
>> 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
>>
>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>    176      {
>>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>> -> 178      }
>>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>    180      {
>>    181          auto symbol = findSymbol(name);
>>
>>
>> ========================
>> Prof. Victor Lazzarini
>> Maynooth University
>> Ireland
>>
>> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>> >
>> > 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:
>> > 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.
>> >
>> > 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 <michael.gogins@gmail.com> wrote:
>> > >
>> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>> > > 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 Gogins
>> > > Irreducible Productions
>> > > http://michaelgogins.tumblr.com
>> > > Michael dot Gogins at gmail dot com
>> >
>>


Date2021-10-10 03:00
FromMichael Gogins
SubjectRe: [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:
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.
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)

<CsoundSyntheizer>
<CsOptions>
-odac --opcode-lib="./clang_opcodes.dylib"
</CsOptions>
<CsInstruments>
0dbfs = 1

SCode = {{
 #include <csdl.h>
 struct DATASPACE {
    OPDS h;
    MYFLT *out, *in, *gain;
 };

 static int init(CSOUND *csound, DATASPACE *p) {
       return OK;
 }

 static int perf(CSOUND *csound, DATASPACE *p) {
    MYFLT *out = p->out, *in = p->in, g = *p->gain;
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t n, nsmps = CS_KSMPS;

    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&out[nsmps],0, early*sizeof(MYFLT));
    }

    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
    return OK;
 }

 void* __dso_handle = (void *)&__dso_handle;
 extern "C" int csound_main(CSOUND *csound) {
    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
                         (SUBR) init, (SUBR) perf, NULL);
    return 0;
 };
}}

SCscode = {{
    instr 1
     a1 oscili 1,440
     a2 amp a1, 0.5
     out a2
    endin
}}

SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
SLflags = ""

ires clang_compile "csound_main", SCode, SCflags, SLflags
ires compilestr SCscode

</CsInstruments>
<CsScore>
i1 0 2
</CsScore>
</CsoundSynthesizer>


========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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:
>>
>> 
>> 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
>>
>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>    176      {
>>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>> -> 178      }
>>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>    180      {
>>    181          auto symbol = findSymbol(name);
>>
>>
>> ========================
>> Prof. Victor Lazzarini
>> Maynooth University
>> Ireland
>>
>> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>> >
>> > 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:
>> > 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.
>> >
>> > 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 <michael.gogins@gmail.com> wrote:
>> > >
>> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>> > > 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 Gogins
>> > > Irreducible Productions
>> > > http://michaelgogins.tumblr.com
>> > > Michael dot Gogins at gmail dot com
>> >
>>


Date2021-10-11 13:57
FromVictor Lazzarini
SubjectRe: [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  wrote:
> 
> I think I have fixed the crash on exit. In git now. 
> 
> On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
> 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  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.
> 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)
> 
> 
> 
> -odac --opcode-lib="./clang_opcodes.dylib" 
> 
> 
> 0dbfs = 1
> 
> SCode = {{
>  #include 
>  struct DATASPACE {
>     OPDS h;
>     MYFLT *out, *in, *gain;
>  };
> 
>  static int init(CSOUND *csound, DATASPACE *p) {
>        return OK;
>  }
> 
>  static int perf(CSOUND *csound, DATASPACE *p) {
>     MYFLT *out = p->out, *in = p->in, g = *p->gain;
>     uint32_t offset = p->h.insdshead->ksmps_offset;
>     uint32_t early  = p->h.insdshead->ksmps_no_end;
>     uint32_t n, nsmps = CS_KSMPS;
> 
>     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>     if (UNLIKELY(early)) {
>       nsmps -= early;
>       memset(&out[nsmps],0, early*sizeof(MYFLT));
>     }
> 
>     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>     return OK;
>  }
> 
>  void* __dso_handle = (void *)&__dso_handle;
>  extern "C" int csound_main(CSOUND *csound) {
>     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>                          (SUBR) init, (SUBR) perf, NULL);
>     return 0;
>  };
> }}
> 
> SCscode = {{
>     instr 1
>      a1 oscili 1,440
>      a2 amp a1, 0.5
>      out a2
>     endin
> }}
> 
> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> SLflags = ""
> 
> ires clang_compile "csound_main", SCode, SCflags, SLflags
> ires compilestr SCscode
> 
> 
> 
> i1 0 2
> 
>  
> 
> 
> ========================
> Prof. Victor Lazzarini
> Maynooth University
> Ireland
> 
> > On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
> > 
> > Ok, I'll carry on experimenting and report back.
> > 
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> > 
> >> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
> >> 
> >> 
> >> 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  wrote:
> >> 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 findSymbol(const StringRef &name)
> >>    176      {
> >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> >> -> 178      }
> >>    179      Expected getSymbolAddress(const StringRef &name)
> >>    180      {
> >>    181          auto symbol = findSymbol(name);
> >> 
> >> 
> >> ========================
> >> Prof. Victor Lazzarini
> >> Maynooth University
> >> Ireland
> >> 
> >> > On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
> >> > 
> >> > 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  wrote:
> >> > Run ldd on existing opcodes to find the libs.
> >> > 
> >> > On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
> >> > 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  wrote:
> >> > > 
> >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> >> > > 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 Gogins
> >> > > Irreducible Productions
> >> > > http://michaelgogins.tumblr.com
> >> > > Michael dot Gogins at gmail dot com
> >> > 
> >> 
> 


Date2021-10-11 14:15
FromRory Walsh
SubjectRe: [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  section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini  wrote:
>
> 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  wrote:
> >
> > I think I have fixed the crash on exit. In git now.
> >
> > On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
> > 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  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.
> > 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)
> >
> > 
> > 
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > 
> > 
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include 
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > 
> > 
> > i1 0 2
> > 
> > 
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
> > >
> > > Ok, I'll carry on experimenting and report back.
> > >
> > > Prof. Victor Lazzarini
> > > Maynooth University
> > > Ireland
> > >
> > >> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
> > >>
> > >> 
> > >> 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  wrote:
> > >> 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 findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
> > >> >
> > >> > 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  wrote:
> > >> > Run ldd on existing opcodes to find the libs.
> > >> >
> > >> > On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
> > >> > 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  wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>

Date2021-10-11 15:34
FromVictor Lazzarini
SubjectRe: [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  wrote:
> 
> 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  section where the
> opcodes parsed could be appended to the list of copcodes before Csound
> runs?
> 
> 
> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini  wrote:
>> 
>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360530633%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=bnZUY5zb5v%2B%2BoQkEdErNWqdKXcXj06COgcKto3eRFQI%3D&reserved=0
>> 
>> 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  wrote:
>>> 
>>> I think I have fixed the crash on exit. In git now.
>>> 
>>> On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
>>> 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  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.
>>> 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)
>>> 
>>> 
>>> 
>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>> 
>>> 
>>> 0dbfs = 1
>>> 
>>> SCode = {{
>>> #include 
>>> struct DATASPACE {
>>>    OPDS h;
>>>    MYFLT *out, *in, *gain;
>>> };
>>> 
>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>       return OK;
>>> }
>>> 
>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>    MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>    uint32_t offset = p->h.insdshead->ksmps_offset;
>>>    uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>    uint32_t n, nsmps = CS_KSMPS;
>>> 
>>>    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>    if (UNLIKELY(early)) {
>>>      nsmps -= early;
>>>      memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>    }
>>> 
>>>    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>    return OK;
>>> }
>>> 
>>> void* __dso_handle = (void *)&__dso_handle;
>>> extern "C" int csound_main(CSOUND *csound) {
>>>    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>                         (SUBR) init, (SUBR) perf, NULL);
>>>    return 0;
>>> };
>>> }}
>>> 
>>> SCscode = {{
>>>    instr 1
>>>     a1 oscili 1,440
>>>     a2 amp a1, 0.5
>>>     out a2
>>>    endin
>>> }}
>>> 
>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>> SLflags = ""
>>> 
>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>> ires compilestr SCscode
>>> 
>>> 
>>> 
>>> i1 0 2
>>> 
>>> 
>>> 
>>> 
>>> ========================
>>> Prof. Victor Lazzarini
>>> Maynooth University
>>> Ireland
>>> 
>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
>>>> 
>>>> Ok, I'll carry on experimenting and report back.
>>>> 
>>>> Prof. Victor Lazzarini
>>>> Maynooth University
>>>> Ireland
>>>> 
>>>>> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
>>>>> 
>>>>> 
>>>>> 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  wrote:
>>>>> 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 findSymbol(const StringRef &name)
>>>>>   176      {
>>>>>   177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>> -> 178      }
>>>>>   179      Expected getSymbolAddress(const StringRef &name)
>>>>>   180      {
>>>>>   181          auto symbol = findSymbol(name);
>>>>> 
>>>>> 
>>>>> ========================
>>>>> Prof. Victor Lazzarini
>>>>> Maynooth University
>>>>> Ireland
>>>>> 
>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
>>>>>> 
>>>>>> 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  wrote:
>>>>>> Run ldd on existing opcodes to find the libs.
>>>>>> 
>>>>>> On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
>>>>>> 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  wrote:
>>>>>>> 
>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360530633%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jgjY1qWEWevC978CtP5NJO4VB1bSunWMoj64eiPL0qA%3D&reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360540627%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=lF1LDaxBITk%2FLfb%2Bv2vRfhwgQ8vpCtXZHqsED4gAaJQ%3D&reserved=0
>>>>>>> 
>>>>>>> Meeting ID: 843 5408 8766
>>>>>>> Passcode: 630670
>>>>>>> 
>>>>>>> Regards,
>>>>>>> Mike
>>>>>>> 
>>>>>>> -----------------------------------------------------
>>>>>>> Michael Gogins
>>>>>>> Irreducible Productions
>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360540627%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=du2vVHosmAmTH12d0ie6dpOPFJsUcLAZueKZEsOx4ko%3D&reserved=0
>>>>>>> Michael dot Gogins at gmail dot com
>>>>>> 
>>>>> 
>>> 
>> 


Date2021-10-11 16:03
FromRory Walsh
SubjectRe: [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  wrote:
>
> 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  wrote:
> >
> > 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  section where the
> > opcodes parsed could be appended to the list of copcodes before Csound
> > runs?
> >
> >
> > On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini  wrote:
> >>
> >> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360530633%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=bnZUY5zb5v%2B%2BoQkEdErNWqdKXcXj06COgcKto3eRFQI%3D&reserved=0
> >>
> >> 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  wrote:
> >>>
> >>> I think I have fixed the crash on exit. In git now.
> >>>
> >>> On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
> >>> 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  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.
> >>> 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)
> >>>
> >>> 
> >>> 
> >>> -odac --opcode-lib="./clang_opcodes.dylib"
> >>> 
> >>> 
> >>> 0dbfs = 1
> >>>
> >>> SCode = {{
> >>> #include 
> >>> struct DATASPACE {
> >>>    OPDS h;
> >>>    MYFLT *out, *in, *gain;
> >>> };
> >>>
> >>> static int init(CSOUND *csound, DATASPACE *p) {
> >>>       return OK;
> >>> }
> >>>
> >>> static int perf(CSOUND *csound, DATASPACE *p) {
> >>>    MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >>>    uint32_t offset = p->h.insdshead->ksmps_offset;
> >>>    uint32_t early  = p->h.insdshead->ksmps_no_end;
> >>>    uint32_t n, nsmps = CS_KSMPS;
> >>>
> >>>    if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >>>    if (UNLIKELY(early)) {
> >>>      nsmps -= early;
> >>>      memset(&out[nsmps],0, early*sizeof(MYFLT));
> >>>    }
> >>>
> >>>    for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >>>    return OK;
> >>> }
> >>>
> >>> void* __dso_handle = (void *)&__dso_handle;
> >>> extern "C" int csound_main(CSOUND *csound) {
> >>>    csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >>>                         (SUBR) init, (SUBR) perf, NULL);
> >>>    return 0;
> >>> };
> >>> }}
> >>>
> >>> SCscode = {{
> >>>    instr 1
> >>>     a1 oscili 1,440
> >>>     a2 amp a1, 0.5
> >>>     out a2
> >>>    endin
> >>> }}
> >>>
> >>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> >>> SLflags = ""
> >>>
> >>> ires clang_compile "csound_main", SCode, SCflags, SLflags
> >>> ires compilestr SCscode
> >>>
> >>> 
> >>> 
> >>> i1 0 2
> >>> 
> >>> 
> >>>
> >>>
> >>> ========================
> >>> Prof. Victor Lazzarini
> >>> Maynooth University
> >>> Ireland
> >>>
> >>>> On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
> >>>>
> >>>> Ok, I'll carry on experimenting and report back.
> >>>>
> >>>> Prof. Victor Lazzarini
> >>>> Maynooth University
> >>>> Ireland
> >>>>
> >>>>> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
> >>>>>
> >>>>> 
> >>>>> 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  wrote:
> >>>>> 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 findSymbol(const StringRef &name)
> >>>>>   176      {
> >>>>>   177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> >>>>> -> 178      }
> >>>>>   179      Expected getSymbolAddress(const StringRef &name)
> >>>>>   180      {
> >>>>>   181          auto symbol = findSymbol(name);
> >>>>>
> >>>>>
> >>>>> ========================
> >>>>> Prof. Victor Lazzarini
> >>>>> Maynooth University
> >>>>> Ireland
> >>>>>
> >>>>>> On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
> >>>>>>
> >>>>>> 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  wrote:
> >>>>>> Run ldd on existing opcodes to find the libs.
> >>>>>>
> >>>>>> On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
> >>>>>> 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  wrote:
> >>>>>>>
> >>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> >>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360530633%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jgjY1qWEWevC978CtP5NJO4VB1bSunWMoj64eiPL0qA%3D&reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360540627%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=lF1LDaxBITk%2FLfb%2Bv2vRfhwgQ8vpCtXZHqsED4gAaJQ%3D&reserved=0
> >>>>>>>
> >>>>>>> Meeting ID: 843 5408 8766
> >>>>>>> Passcode: 630670
> >>>>>>>
> >>>>>>> Regards,
> >>>>>>> Mike
> >>>>>>>
> >>>>>>> -----------------------------------------------------
> >>>>>>> Michael Gogins
> >>>>>>> Irreducible Productions
> >>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7C8e8993e7d25e492c336808d98cc196a8%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695585360540627%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=du2vVHosmAmTH12d0ie6dpOPFJsUcLAZueKZEsOx4ko%3D&reserved=0
> >>>>>>> Michael dot Gogins at gmail dot com
> >>>>>>
> >>>>>
> >>>
> >>
>

Date2021-10-11 16:23
FromVictor Lazzarini
SubjectRe: [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  wrote:
> 
> 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  wrote:
>> 
>> 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  wrote:
>>> 
>>> 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  section where the
>>> opcodes parsed could be appended to the list of copcodes before Csound
>>> runs?
>>> 
>>> 
>>> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini  wrote:
>>>> 
>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=et35m%2BpLSwONi%2BXjvCn7rBBh4yl7SzTfQj5H9CmCFbo%3D&reserved=0
>>>> 
>>>> 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  wrote:
>>>>> 
>>>>> I think I have fixed the crash on exit. In git now.
>>>>> 
>>>>> On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
>>>>> 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  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.
>>>>> 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)
>>>>> 
>>>>> 
>>>>> 
>>>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>>>> 
>>>>> 
>>>>> 0dbfs = 1
>>>>> 
>>>>> SCode = {{
>>>>> #include 
>>>>> struct DATASPACE {
>>>>>   OPDS h;
>>>>>   MYFLT *out, *in, *gain;
>>>>> };
>>>>> 
>>>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>>>      return OK;
>>>>> }
>>>>> 
>>>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>>>   MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>>>   uint32_t offset = p->h.insdshead->ksmps_offset;
>>>>>   uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>>>   uint32_t n, nsmps = CS_KSMPS;
>>>>> 
>>>>>   if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>>>   if (UNLIKELY(early)) {
>>>>>     nsmps -= early;
>>>>>     memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>>>   }
>>>>> 
>>>>>   for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>>>   return OK;
>>>>> }
>>>>> 
>>>>> void* __dso_handle = (void *)&__dso_handle;
>>>>> extern "C" int csound_main(CSOUND *csound) {
>>>>>   csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>>>                        (SUBR) init, (SUBR) perf, NULL);
>>>>>   return 0;
>>>>> };
>>>>> }}
>>>>> 
>>>>> SCscode = {{
>>>>>   instr 1
>>>>>    a1 oscili 1,440
>>>>>    a2 amp a1, 0.5
>>>>>    out a2
>>>>>   endin
>>>>> }}
>>>>> 
>>>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>>>> SLflags = ""
>>>>> 
>>>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>>>> ires compilestr SCscode
>>>>> 
>>>>> 
>>>>> 
>>>>> i1 0 2
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> ========================
>>>>> Prof. Victor Lazzarini
>>>>> Maynooth University
>>>>> Ireland
>>>>> 
>>>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
>>>>>> 
>>>>>> Ok, I'll carry on experimenting and report back.
>>>>>> 
>>>>>> Prof. Victor Lazzarini
>>>>>> Maynooth University
>>>>>> Ireland
>>>>>> 
>>>>>>> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
>>>>>>> 
>>>>>>> 
>>>>>>> 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  wrote:
>>>>>>> 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 findSymbol(const StringRef &name)
>>>>>>>  176      {
>>>>>>>  177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>>>> -> 178      }
>>>>>>>  179      Expected getSymbolAddress(const StringRef &name)
>>>>>>>  180      {
>>>>>>>  181          auto symbol = findSymbol(name);
>>>>>>> 
>>>>>>> 
>>>>>>> ========================
>>>>>>> Prof. Victor Lazzarini
>>>>>>> Maynooth University
>>>>>>> Ireland
>>>>>>> 
>>>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
>>>>>>>> 
>>>>>>>> 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  wrote:
>>>>>>>> Run ldd on existing opcodes to find the libs.
>>>>>>>> 
>>>>>>>> On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
>>>>>>>> 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  wrote:
>>>>>>>>> 
>>>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=9tex3Nt4BH7xKwYsXaMZ1lspxv00WnPxzTSOuuKxhMg%3D&reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=H1nfoy06uovBaeyNi%2BzqxwLkXxS4Yra8XL7jmSwLMkY%3D&reserved=0
>>>>>>>>> 
>>>>>>>>> Meeting ID: 843 5408 8766
>>>>>>>>> Passcode: 630670
>>>>>>>>> 
>>>>>>>>> Regards,
>>>>>>>>> Mike
>>>>>>>>> 
>>>>>>>>> -----------------------------------------------------
>>>>>>>>> Michael Gogins
>>>>>>>>> Irreducible Productions
>>>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jghKzaoFVgTRw2%2FhYFCsQaonpdxp8vWnoY%2B6ncR71Jw%3D&reserved=0
>>>>>>>>> Michael dot Gogins at gmail dot com
>>>>>>>> 
>>>>>>> 
>>>>> 
>>>> 
>> 


Date2021-10-11 16:37
FromRory Walsh
SubjectRe: [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
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 <rorywalsh@ear.ie> wrote:
>
> 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 <Victor.Lazzarini@mu.ie> wrote:
>>
>> 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 <rorywalsh@ear.ie> wrote:
>>>
>>> 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 <CsOpcodes> section where the
>>> opcodes parsed could be appended to the list of copcodes before Csound
>>> runs?
>>>
>>>
>>> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>
>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=et35m%2BpLSwONi%2BXjvCn7rBBh4yl7SzTfQj5H9CmCFbo%3D&amp;reserved=0
>>>>
>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>
>>>>> 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:
>>>>> 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.
>>>>> 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)
>>>>>
>>>>> <CsoundSyntheizer>
>>>>> <CsOptions>
>>>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>>>> </CsOptions>
>>>>> <CsInstruments>
>>>>> 0dbfs = 1
>>>>>
>>>>> SCode = {{
>>>>> #include <csdl.h>
>>>>> struct DATASPACE {
>>>>>   OPDS h;
>>>>>   MYFLT *out, *in, *gain;
>>>>> };
>>>>>
>>>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>>>      return OK;
>>>>> }
>>>>>
>>>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>>>   MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>>>   uint32_t offset = p->h.insdshead->ksmps_offset;
>>>>>   uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>>>   uint32_t n, nsmps = CS_KSMPS;
>>>>>
>>>>>   if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>>>   if (UNLIKELY(early)) {
>>>>>     nsmps -= early;
>>>>>     memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>>>   }
>>>>>
>>>>>   for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>>>   return OK;
>>>>> }
>>>>>
>>>>> void* __dso_handle = (void *)&__dso_handle;
>>>>> extern "C" int csound_main(CSOUND *csound) {
>>>>>   csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>>>                        (SUBR) init, (SUBR) perf, NULL);
>>>>>   return 0;
>>>>> };
>>>>> }}
>>>>>
>>>>> SCscode = {{
>>>>>   instr 1
>>>>>    a1 oscili 1,440
>>>>>    a2 amp a1, 0.5
>>>>>    out a2
>>>>>   endin
>>>>> }}
>>>>>
>>>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>>>> SLflags = ""
>>>>>
>>>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>>>> ires compilestr SCscode
>>>>>
>>>>> </CsInstruments>
>>>>> <CsScore>
>>>>> i1 0 2
>>>>> </CsScore>
>>>>> </CsoundSynthesizer>
>>>>>
>>>>>
>>>>> ========================
>>>>> Prof. Victor Lazzarini
>>>>> Maynooth University
>>>>> Ireland
>>>>>
>>>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>>>
>>>>>> 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:
>>>>>>>
>>>>>>> 
>>>>>>> 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
>>>>>>>
>>>>>>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>>>>>>  176      {
>>>>>>>  177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>>>> -> 178      }
>>>>>>>  179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>>>>>>  180      {
>>>>>>>  181          auto symbol = findSymbol(name);
>>>>>>>
>>>>>>>
>>>>>>> ========================
>>>>>>> Prof. Victor Lazzarini
>>>>>>> Maynooth University
>>>>>>> Ireland
>>>>>>>
>>>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>>>>>>>>
>>>>>>>> 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:
>>>>>>>> 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.
>>>>>>>>
>>>>>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>>>>>
>>>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=9tex3Nt4BH7xKwYsXaMZ1lspxv00WnPxzTSOuuKxhMg%3D&amp;reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=H1nfoy06uovBaeyNi%2BzqxwLkXxS4Yra8XL7jmSwLMkY%3D&amp;reserved=0
>>>>>>>>>
>>>>>>>>> Meeting ID: 843 5408 8766
>>>>>>>>> Passcode: 630670
>>>>>>>>>
>>>>>>>>> Regards,
>>>>>>>>> Mike
>>>>>>>>>
>>>>>>>>> -----------------------------------------------------
>>>>>>>>> Michael Gogins
>>>>>>>>> Irreducible Productions
>>>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Cdf47ca974a42467937b608d98cc85abe%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695614413511441%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=jghKzaoFVgTRw2%2FhYFCsQaonpdxp8vWnoY%2B6ncR71Jw%3D&amp;reserved=0
>>>>>>>>> Michael dot Gogins at gmail dot com
>>>>>>>>
>>>>>>>
>>>>>
>>>>
>>


Date2021-10-11 16:40
FromVictor Lazzarini
SubjectRe: [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  wrote:
> 
> 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  wrote:
>> 
>> 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  wrote:
>>> 
>>> 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  wrote:
>>>> 
>>>> 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  section where the
>>>> opcodes parsed could be appended to the list of copcodes before Csound
>>>> runs?
>>>> 
>>>> 
>>>> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini  wrote:
>>>>> 
>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=5LwVSbVubhWzMNHr%2FD8ZLk2xFKEB1RhfeHknrRWQwXA%3D&reserved=0
>>>>> 
>>>>> 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  wrote:
>>>>>> 
>>>>>> I think I have fixed the crash on exit. In git now.
>>>>>> 
>>>>>> On Sat, Oct 9, 2021, 18:15 Michael Gogins  wrote:
>>>>>> 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  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.
>>>>>> 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)
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>>>>> 
>>>>>> 
>>>>>> 0dbfs = 1
>>>>>> 
>>>>>> SCode = {{
>>>>>> #include 
>>>>>> struct DATASPACE {
>>>>>>  OPDS h;
>>>>>>  MYFLT *out, *in, *gain;
>>>>>> };
>>>>>> 
>>>>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>>>>     return OK;
>>>>>> }
>>>>>> 
>>>>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>>>>  MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>>>>  uint32_t offset = p->h.insdshead->ksmps_offset;
>>>>>>  uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>>>>  uint32_t n, nsmps = CS_KSMPS;
>>>>>> 
>>>>>>  if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>>>>  if (UNLIKELY(early)) {
>>>>>>    nsmps -= early;
>>>>>>    memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>>>>  }
>>>>>> 
>>>>>>  for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>>>>  return OK;
>>>>>> }
>>>>>> 
>>>>>> void* __dso_handle = (void *)&__dso_handle;
>>>>>> extern "C" int csound_main(CSOUND *csound) {
>>>>>>  csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>>>>                       (SUBR) init, (SUBR) perf, NULL);
>>>>>>  return 0;
>>>>>> };
>>>>>> }}
>>>>>> 
>>>>>> SCscode = {{
>>>>>>  instr 1
>>>>>>   a1 oscili 1,440
>>>>>>   a2 amp a1, 0.5
>>>>>>   out a2
>>>>>>  endin
>>>>>> }}
>>>>>> 
>>>>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>>>>> SLflags = ""
>>>>>> 
>>>>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>>>>> ires compilestr SCscode
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> i1 0 2
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> ========================
>>>>>> Prof. Victor Lazzarini
>>>>>> Maynooth University
>>>>>> Ireland
>>>>>> 
>>>>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini  wrote:
>>>>>>> 
>>>>>>> Ok, I'll carry on experimenting and report back.
>>>>>>> 
>>>>>>> Prof. Victor Lazzarini
>>>>>>> Maynooth University
>>>>>>> Ireland
>>>>>>> 
>>>>>>>> On 9 Oct 2021, at 15:37, Michael Gogins  wrote:
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 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  wrote:
>>>>>>>> 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 findSymbol(const StringRef &name)
>>>>>>>> 176      {
>>>>>>>> 177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>>>>> -> 178      }
>>>>>>>> 179      Expected getSymbolAddress(const StringRef &name)
>>>>>>>> 180      {
>>>>>>>> 181          auto symbol = findSymbol(name);
>>>>>>>> 
>>>>>>>> 
>>>>>>>> ========================
>>>>>>>> Prof. Victor Lazzarini
>>>>>>>> Maynooth University
>>>>>>>> Ireland
>>>>>>>> 
>>>>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins  wrote:
>>>>>>>>> 
>>>>>>>>> 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  wrote:
>>>>>>>>> Run ldd on existing opcodes to find the libs.
>>>>>>>>> 
>>>>>>>>> On Fri, Oct 8, 2021, 17:40 Victor Lazzarini  wrote:
>>>>>>>>> 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  wrote:
>>>>>>>>>> 
>>>>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=w7RvjGaT4BJc6URhGMP0S8XhcHO5psQ9Zbh49MPIyKM%3D&reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=4R0EqOPqBXr%2FThOWrqrw5kVwaMxv5rDpzbecPtnjJ1g%3D&reserved=0
>>>>>>>>>> 
>>>>>>>>>> Meeting ID: 843 5408 8766
>>>>>>>>>> Passcode: 630670
>>>>>>>>>> 
>>>>>>>>>> Regards,
>>>>>>>>>> Mike
>>>>>>>>>> 
>>>>>>>>>> -----------------------------------------------------
>>>>>>>>>> Michael Gogins
>>>>>>>>>> Irreducible Productions
>>>>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=wHs1Npu%2B1l7wCvkq1b%2Fdx2oxqXOz1U2RJ6w04gM4VfY%3D&reserved=0
>>>>>>>>>> Michael dot Gogins at gmail dot com
>>>>>>>>> 
>>>>>>>> 
>>>>>> 
>>>>> 
>>> 
> 


Date2021-10-11 18:37
FromRory Walsh
SubjectRe: [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
function tables.
========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 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
> 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 <rorywalsh@ear.ie> wrote:
>>
>> 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 <Victor.Lazzarini@mu.ie> wrote:
>>>
>>> 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 <rorywalsh@ear.ie> wrote:
>>>>
>>>> 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 <CsOpcodes> section where the
>>>> opcodes parsed could be appended to the list of copcodes before Csound
>>>> runs?
>>>>
>>>>
>>>> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>>
>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=5LwVSbVubhWzMNHr%2FD8ZLk2xFKEB1RhfeHknrRWQwXA%3D&amp;reserved=0
>>>>>
>>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>>
>>>>>> 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:
>>>>>> 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.
>>>>>> 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)
>>>>>>
>>>>>> <CsoundSyntheizer>
>>>>>> <CsOptions>
>>>>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>>>>> </CsOptions>
>>>>>> <CsInstruments>
>>>>>> 0dbfs = 1
>>>>>>
>>>>>> SCode = {{
>>>>>> #include <csdl.h>
>>>>>> struct DATASPACE {
>>>>>>  OPDS h;
>>>>>>  MYFLT *out, *in, *gain;
>>>>>> };
>>>>>>
>>>>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>>>>     return OK;
>>>>>> }
>>>>>>
>>>>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>>>>  MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>>>>  uint32_t offset = p->h.insdshead->ksmps_offset;
>>>>>>  uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>>>>  uint32_t n, nsmps = CS_KSMPS;
>>>>>>
>>>>>>  if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>>>>  if (UNLIKELY(early)) {
>>>>>>    nsmps -= early;
>>>>>>    memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>>>>  }
>>>>>>
>>>>>>  for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>>>>  return OK;
>>>>>> }
>>>>>>
>>>>>> void* __dso_handle = (void *)&__dso_handle;
>>>>>> extern "C" int csound_main(CSOUND *csound) {
>>>>>>  csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>>>>                       (SUBR) init, (SUBR) perf, NULL);
>>>>>>  return 0;
>>>>>> };
>>>>>> }}
>>>>>>
>>>>>> SCscode = {{
>>>>>>  instr 1
>>>>>>   a1 oscili 1,440
>>>>>>   a2 amp a1, 0.5
>>>>>>   out a2
>>>>>>  endin
>>>>>> }}
>>>>>>
>>>>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>>>>> SLflags = ""
>>>>>>
>>>>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>>>>> ires compilestr SCscode
>>>>>>
>>>>>> </CsInstruments>
>>>>>> <CsScore>
>>>>>> i1 0 2
>>>>>> </CsScore>
>>>>>> </CsoundSynthesizer>
>>>>>>
>>>>>>
>>>>>> ========================
>>>>>> Prof. Victor Lazzarini
>>>>>> Maynooth University
>>>>>> Ireland
>>>>>>
>>>>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>>>>
>>>>>>> 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:
>>>>>>>>
>>>>>>>> 
>>>>>>>> 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
>>>>>>>>
>>>>>>>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>>>>>>> 176      {
>>>>>>>> 177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>>>>> -> 178      }
>>>>>>>> 179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>>>>>>> 180      {
>>>>>>>> 181          auto symbol = findSymbol(name);
>>>>>>>>
>>>>>>>>
>>>>>>>> ========================
>>>>>>>> Prof. Victor Lazzarini
>>>>>>>> Maynooth University
>>>>>>>> Ireland
>>>>>>>>
>>>>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>>>>>>>>>
>>>>>>>>> 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:
>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=w7RvjGaT4BJc6URhGMP0S8XhcHO5psQ9Zbh49MPIyKM%3D&amp;reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=4R0EqOPqBXr%2FThOWrqrw5kVwaMxv5rDpzbecPtnjJ1g%3D&amp;reserved=0
>>>>>>>>>>
>>>>>>>>>> Meeting ID: 843 5408 8766
>>>>>>>>>> Passcode: 630670
>>>>>>>>>>
>>>>>>>>>> Regards,
>>>>>>>>>> Mike
>>>>>>>>>>
>>>>>>>>>> -----------------------------------------------------
>>>>>>>>>> Michael Gogins
>>>>>>>>>> Irreducible Productions
>>>>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=wHs1Npu%2B1l7wCvkq1b%2Fdx2oxqXOz1U2RJ6w04gM4VfY%3D&amp;reserved=0
>>>>>>>>>> Michael dot Gogins at gmail dot com
>>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>
>>>
>


Date2021-10-11 18:46
FromVictor Lazzarini
SubjectRe: [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:


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
function tables.
========================
Prof. Victor Lazzarini
Maynooth University
Ireland

> On 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
> 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 <rorywalsh@ear.ie> wrote:
>>
>> 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 <Victor.Lazzarini@mu.ie> wrote:
>>>
>>> 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 <rorywalsh@ear.ie> wrote:
>>>>
>>>> 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 <CsOpcodes> section where the
>>>> opcodes parsed could be appended to the list of copcodes before Csound
>>>> runs?
>>>>
>>>>
>>>> On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>>
>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvlazzarini%2Fopcode_compiler&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=5LwVSbVubhWzMNHr%2FD8ZLk2xFKEB1RhfeHknrRWQwXA%3D&amp;reserved=0
>>>>>
>>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>>
>>>>>> 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:
>>>>>> 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.
>>>>>> 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)
>>>>>>
>>>>>> <CsoundSyntheizer>
>>>>>> <CsOptions>
>>>>>> -odac --opcode-lib="./clang_opcodes.dylib"
>>>>>> </CsOptions>
>>>>>> <CsInstruments>
>>>>>> 0dbfs = 1
>>>>>>
>>>>>> SCode = {{
>>>>>> #include <csdl.h>
>>>>>> struct DATASPACE {
>>>>>>  OPDS h;
>>>>>>  MYFLT *out, *in, *gain;
>>>>>> };
>>>>>>
>>>>>> static int init(CSOUND *csound, DATASPACE *p) {
>>>>>>     return OK;
>>>>>> }
>>>>>>
>>>>>> static int perf(CSOUND *csound, DATASPACE *p) {
>>>>>>  MYFLT *out = p->out, *in = p->in, g = *p->gain;
>>>>>>  uint32_t offset = p->h.insdshead->ksmps_offset;
>>>>>>  uint32_t early  = p->h.insdshead->ksmps_no_end;
>>>>>>  uint32_t n, nsmps = CS_KSMPS;
>>>>>>
>>>>>>  if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
>>>>>>  if (UNLIKELY(early)) {
>>>>>>    nsmps -= early;
>>>>>>    memset(&out[nsmps],0, early*sizeof(MYFLT));
>>>>>>  }
>>>>>>
>>>>>>  for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
>>>>>>  return OK;
>>>>>> }
>>>>>>
>>>>>> void* __dso_handle = (void *)&__dso_handle;
>>>>>> extern "C" int csound_main(CSOUND *csound) {
>>>>>>  csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
>>>>>>                       (SUBR) init, (SUBR) perf, NULL);
>>>>>>  return 0;
>>>>>> };
>>>>>> }}
>>>>>>
>>>>>> SCscode = {{
>>>>>>  instr 1
>>>>>>   a1 oscili 1,440
>>>>>>   a2 amp a1, 0.5
>>>>>>   out a2
>>>>>>  endin
>>>>>> }}
>>>>>>
>>>>>> SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
>>>>>> SLflags = ""
>>>>>>
>>>>>> ires clang_compile "csound_main", SCode, SCflags, SLflags
>>>>>> ires compilestr SCscode
>>>>>>
>>>>>> </CsInstruments>
>>>>>> <CsScore>
>>>>>> i1 0 2
>>>>>> </CsScore>
>>>>>> </CsoundSynthesizer>
>>>>>>
>>>>>>
>>>>>> ========================
>>>>>> Prof. Victor Lazzarini
>>>>>> Maynooth University
>>>>>> Ireland
>>>>>>
>>>>>>> On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>>>>>>>
>>>>>>> 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:
>>>>>>>>
>>>>>>>> 
>>>>>>>> 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
>>>>>>>>
>>>>>>>> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
>>>>>>>> 176      {
>>>>>>>> 177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
>>>>>>>> -> 178      }
>>>>>>>> 179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
>>>>>>>> 180      {
>>>>>>>> 181          auto symbol = findSymbol(name);
>>>>>>>>
>>>>>>>>
>>>>>>>> ========================
>>>>>>>> Prof. Victor Lazzarini
>>>>>>>> Maynooth University
>>>>>>>> Ireland
>>>>>>>>
>>>>>>>>> On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
>>>>>>>>>
>>>>>>>>> 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:
>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>> 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 <michael.gogins@gmail.com> wrote:
>>>>>>>>>>
>>>>>>>>>> WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
>>>>>>>>>> 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgogins%2Fclang-opcodes&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=w7RvjGaT4BJc6URhGMP0S8XhcHO5psQ9Zbh49MPIyKM%3D&amp;reserved=0. 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://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus02web.zoom.us%2Fj%2F84354088766%3Fpwd%3DektPSkpXeFQrUlZYUFM4cVNPMU01QT09&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=4R0EqOPqBXr%2FThOWrqrw5kVwaMxv5rDpzbecPtnjJ1g%3D&amp;reserved=0
>>>>>>>>>>
>>>>>>>>>> Meeting ID: 843 5408 8766
>>>>>>>>>> Passcode: 630670
>>>>>>>>>>
>>>>>>>>>> Regards,
>>>>>>>>>> Mike
>>>>>>>>>>
>>>>>>>>>> -----------------------------------------------------
>>>>>>>>>> Michael Gogins
>>>>>>>>>> Irreducible Productions
>>>>>>>>>> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmichaelgogins.tumblr.com%2F&amp;data=04%7C01%7CVictor.Lazzarini%40mu.ie%7Ce90057231104491909da08d98ccb14b1%7C1454f5ccbb354685bbd98621fd8055c9%7C1%7C0%7C637695626126269396%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=wHs1Npu%2B1l7wCvkq1b%2Fdx2oxqXOz1U2RJ6w04gM4VfY%3D&amp;reserved=0
>>>>>>>>>> Michael dot Gogins at gmail dot com
>>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>
>>>
>


Date2021-10-11 21:07
FromMichael Gogins
SubjectRe: [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 Gogins
Irreducible 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.
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 <CsOpcodes> section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > 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:
> > 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.
> > 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)
> >
> > <CsoundSyntheizer>
> > <CsOptions>
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > </CsOptions>
> > <CsInstruments>
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include <csdl.h>
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > </CsInstruments>
> > <CsScore>
> > i1 0 2
> > </CsScore>
> > </CsoundSynthesizer>
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
> > >
> > > 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:
> > >>
> > >> 
> > >> 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
> > >>
> > >> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
> > >> >
> > >> > 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:
> > >> > 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.
> > >> >
> > >> > 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 <michael.gogins@gmail.com> wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>

Date2021-10-12 06:55
FromVictor Lazzarini
SubjectRe: [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:


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 Gogins
Irreducible 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.
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 <CsOpcodes> section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > 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:
> > 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.
> > 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)
> >
> > <CsoundSyntheizer>
> > <CsOptions>
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > </CsOptions>
> > <CsInstruments>
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include <csdl.h>
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > </CsInstruments>
> > <CsScore>
> > i1 0 2
> > </CsScore>
> > </CsoundSynthesizer>
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
> > >
> > > 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:
> > >>
> > >> 
> > >> 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
> > >>
> > >> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
> > >> >
> > >> > 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:
> > >> > 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.
> > >> >
> > >> > 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 <michael.gogins@gmail.com> wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>

Date2021-10-12 11:24
FromMichael Gogins
SubjectRe: [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:
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:


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 Gogins
Irreducible 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.
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 <CsOpcodes> section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > 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:
> > 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.
> > 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)
> >
> > <CsoundSyntheizer>
> > <CsOptions>
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > </CsOptions>
> > <CsInstruments>
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include <csdl.h>
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > </CsInstruments>
> > <CsScore>
> > i1 0 2
> > </CsScore>
> > </CsoundSynthesizer>
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
> > >
> > > 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:
> > >>
> > >> 
> > >> 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
> > >>
> > >> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
> > >> >
> > >> > 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:
> > >> > 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.
> > >> >
> > >> > 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 <michael.gogins@gmail.com> wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>

Date2021-10-12 18:15
FromVictor Lazzarini
SubjectRe: [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:


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:
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:


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 Gogins
Irreducible 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.
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 <CsOpcodes> section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > 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:
> > 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.
> > 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)
> >
> > <CsoundSyntheizer>
> > <CsOptions>
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > </CsOptions>
> > <CsInstruments>
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include <csdl.h>
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > </CsInstruments>
> > <CsScore>
> > i1 0 2
> > </CsScore>
> > </CsoundSynthesizer>
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
> > >
> > > 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:
> > >>
> > >> 
> > >> 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
> > >>
> > >> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
> > >> >
> > >> > 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:
> > >> > 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.
> > >> >
> > >> > 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 <michael.gogins@gmail.com> wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>

Date2021-10-12 22:41
FromMichael Gogins
SubjectRe: [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:
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:


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:
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:


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 Gogins
Irreducible 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.
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 <CsOpcodes> section where the
opcodes parsed could be appended to the list of copcodes before Csound
runs?


On Mon, 11 Oct 2021 at 13:57, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
>
> 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 <michael.gogins@gmail.com> wrote:
> >
> > 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:
> > 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.
> > 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)
> >
> > <CsoundSyntheizer>
> > <CsOptions>
> > -odac --opcode-lib="./clang_opcodes.dylib"
> > </CsOptions>
> > <CsInstruments>
> > 0dbfs = 1
> >
> > SCode = {{
> >  #include <csdl.h>
> >  struct DATASPACE {
> >     OPDS h;
> >     MYFLT *out, *in, *gain;
> >  };
> >
> >  static int init(CSOUND *csound, DATASPACE *p) {
> >        return OK;
> >  }
> >
> >  static int perf(CSOUND *csound, DATASPACE *p) {
> >     MYFLT *out = p->out, *in = p->in, g = *p->gain;
> >     uint32_t offset = p->h.insdshead->ksmps_offset;
> >     uint32_t early  = p->h.insdshead->ksmps_no_end;
> >     uint32_t n, nsmps = CS_KSMPS;
> >
> >     if (UNLIKELY(offset)) memset(out,0, offset*sizeof(MYFLT));
> >     if (UNLIKELY(early)) {
> >       nsmps -= early;
> >       memset(&out[nsmps],0, early*sizeof(MYFLT));
> >     }
> >
> >     for(n=offset; n < nsmps; n++) out[n] = in[n]*g;
> >     return OK;
> >  }
> >
> >  void* __dso_handle = (void *)&__dso_handle;
> >  extern "C" int csound_main(CSOUND *csound) {
> >     csound->AppendOpcode(csound, "amp",sizeof(DATASPACE),0,3,"a","ak",
> >                          (SUBR) init, (SUBR) perf, NULL);
> >     return 0;
> >  };
> > }}
> >
> > SCscode = {{
> >     instr 1
> >      a1 oscili 1,440
> >      a2 amp a1, 0.5
> >      out a2
> >     endin
> > }}
> >
> > SCflags =  "-DTARGET_OS_OSX -DTARGET_OS_IPHONE -I/Users/victor/Library/Frameworks/CsoundLib64.framework/Headers -I/usr/local/lib/clang/13.0.0/include/ -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/"
> > SLflags = ""
> >
> > ires clang_compile "csound_main", SCode, SCflags, SLflags
> > ires compilestr SCscode
> >
> > </CsInstruments>
> > <CsScore>
> > i1 0 2
> > </CsScore>
> > </CsoundSynthesizer>
> >
> >
> > ========================
> > Prof. Victor Lazzarini
> > Maynooth University
> > Ireland
> >
> > > On 9 Oct 2021, at 17:50, Victor Lazzarini <Victor.Lazzarini@mu.ie> wrote:
> > >
> > > 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:
> > >>
> > >> 
> > >> 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
> > >>
> > >> ####### 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<JITEvaluatedSymbol> findSymbol(const StringRef &name)
> > >>    176      {
> > >>    177          return execution_session.lookup({&main_jit_dylib}, Mangle(name));
> > >> -> 178      }
> > >>    179      Expected<JITTargetAddress> getSymbolAddress(const StringRef &name)
> > >>    180      {
> > >>    181          auto symbol = findSymbol(name);
> > >>
> > >>
> > >> ========================
> > >> Prof. Victor Lazzarini
> > >> Maynooth University
> > >> Ireland
> > >>
> > >> > On 8 Oct 2021, at 23:32, Michael Gogins <michael.gogins@gmail.com> wrote:
> > >> >
> > >> > 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:
> > >> > 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.
> > >> >
> > >> > 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 <michael.gogins@gmail.com> wrote:
> > >> > >
> > >> > > WARNINGThis email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
> > >> > > 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 Gogins
> > >> > > Irreducible Productions
> > >> > > http://michaelgogins.tumblr.com
> > >> > > Michael dot Gogins at gmail dot com
> > >> >
> > >>
> >
>