llvm-d

Moritz Maxeiner moritz at ucworks.org
Sat Mar 23 13:59:24 PDT 2013


On Saturday, 23 March 2013 at 19:57:52 UTC, Chris Cain wrote:
> On Saturday, 23 March 2013 at 10:01:19 UTC, Moritz Maxeiner 
> wrote:
>> No problem at all. There is an example quoted in the README 
>> and how to compile it, so without further information form 
>> your side I don't know what the problem is. To get the example 
>> from the README working:
>> - Download/Clone the github repo into a folder (let's say 
>> llvm-d)
>> - cd into that folder
>> - execute rdmd -L-ldl sample/multithreaded.d
>>
>> The need for the -dl flag is only for POSIX platforms and will 
>> be gone with the next commit as I have included a lib pragma.
>>
>> If the above doesn't help could you please tell me what 
>> exactly you have done and at which point you have the problems?
>>
>> -- Moritz
>
> Thanks so much.
>
> I had dub upgrade the llvm-d package and now I have different 
> problems. That said, I do have a working example (as long as 
> rdmd is used).
>
> I have updated the gist (with repro steps): 
> https://gist.github.com/Zshazz/c7dbd6eee0b6b242252b

Regarding the two warnings:
The second one for line 346 has been fixed.
I have no idea how you could get the first one as the statement 
is - at least according to my logic - reachable: It should be 
reached if the "isSizedDerivedType" is used on a StructType 
instance. I'll look into that shortly.

Other than that, the link time errors appear because the dl 
library (which allows for loading of dynamic libraries at 
runtime) did not get linked in. That should not be possible as 
llvm.util.shlib contains a lib pragma that ensures the dl library 
does get linked in.

Also, you're giving d string to c function which expect c 
strings. Not a good idea (c strings are \0 terminated, d strings 
aren't). Use std.string.toStringz (which allocates GC memory, 
though and the c strings may dissappear while LLVM is using them 
as D collects them, because the GC doesn't know about any use on 
the C side) or use llvm.util.memory.toCString (which does the 
same thing as toStringz, only it doesn't allocate GC memory, but 
"unmanaged" memory).

And you don't need the two lines with the lib directory if you 
have LLVM installed. That exists in the README sample only to 
show how it is done for people who wish to ship the LLLVM library 
together with their program inside a subfolder, I've removed them 
from the README example since it seems to cause confusion and 
also added the fibonacci example to the README to showcase 
something more complex.

>
> I'm running LLVM 3.2, so the example given in the README 
> doesn't work. But the code in the gist works as long as you run 
> 'dub --rdmd', so that's some progress! Not really sure about 
> the linking problems without rdmd. This is sufficient to start 
> some work with it to really try some things out, though.

There another example now available under samples/fibonacci.d 
that shows a complex example that works with LLVM 3.2.

>
> I'll keep messing around with it. It seems like you already 
> know that JIT doesn't work because we need 
> InitializeNativeTarget.

It does work, that is precisely what I reimplemented 
LLVMInitializeNativeTarget and LLVMInitializeAllTargets in D for. 
See llvm.c.functions.

>
> That all said, the tutorials, instructions, and documentation 
> for the C API of LLVM is pretty sparse. The best I've seen to 
> figure out how things work is to go through 
> http://llvm.org/doxygen/modules.html and piece together (very 
> slowly) how something might work. I'm also having to cross 
> reference tutorials made for the C++ API and figure out how to 
> make it work in C by reading the raw source code to get the 
> equivalent calls. Fortunately I did find that one post that 
> gave a close-to-complete example in C. Is there any better way 
> to do this at this point?

You just pretty much described what I've been doing myself, I 
don't know of a better way at present, sorry. I'm trying to 
advance the D API to a point where it's usable and then I'll add 
decent documentation to the D API, but until then you'll have to 
suffer through the doxygen "documentation" the same way I do^^

- Moritz


More information about the Digitalmars-d-announce mailing list