Shilpa B via llvm-dev
2016-Nov-18 11:20 UTC
[llvm-dev] Linking LLVM IR with standard library
Hi, I have a LLVM IR file generated for a different source language (Not C). I have added a rand() function in it to generate random numbers. I compiled the .ll file to .o using clang. However, when I execute the .o file all generated numbers are zeros. How should I link the .ll file with the standard library for the clang to generate the random numbers? Thanks -Shilpa -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161118/6afad5b2/attachment.html>
mats petersson via llvm-dev
2016-Nov-18 16:09 UTC
[llvm-dev] Linking LLVM IR with standard library
Without reproducible steps, it's hard to give meaningful advice. In my experience, linking using `clang` (or `gcc`) will work. My compiler does that around in this function: https://github.com/Leporacanthicus/lacsap/blob/master/binary.cpp#L126 -- Mats On 18 November 2016 at 11:20, Shilpa B via llvm-dev <llvm-dev at lists.llvm.org> wrote:> Hi, > > I have a LLVM IR file generated for a different source language (Not C). I > have added a rand() function in it to generate random numbers. > > I compiled the .ll file to .o using clang. However, when I execute the .o > file all generated numbers are zeros. > > How should I link the .ll file with the standard library for the clang to > generate the random numbers? > > > Thanks > -Shilpa > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161118/e837efc6/attachment.html>
mats petersson via llvm-dev
2016-Nov-18 18:06 UTC
[llvm-dev] Linking LLVM IR with standard library
Do reply to ALL recipients (in particular to the mailing list) when replying! On 18 November 2016 at 17:27, Shilpa B <shilpabvb at gmail.com> wrote:> Hi Mats, > > Thanks for the reply and the link. I am attaching the sample file that I > had used to test. > It is an LLVM IR file and I modified it to include rand function to > generate random numbers and used the command > > clang print.ll -std=c++11 -Wl,-rpath,/usr/include -o print.o > > to generate the executable file. However, when I execute print.o it shows > the random numbers generated as zero. >Nope, rand is (most likely) returning correct values you are printing floating point values from integer values, and that quite often results in zero being printed - depending on the architecture, register vs. memory used for varargs, etc. .... @.str = private unnamed_addr constant [4 x i8] c"%f\09\00", align 1 .... %callr = call i32 @rand() store i32 %callr, i32* %arrayidx5, align 8 %val = load i32, i32* %arrayidx5, align 8 %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %val) .... change %f to %d, and it lists a bunch of seemingly random numbers, at least on my machine. -- Mats> > Thanks, > Shilpa > > On Fri, Nov 18, 2016 at 9:39 PM, mats petersson <mats at planetcatfish.com> > wrote: > >> Without reproducible steps, it's hard to give meaningful advice. In my >> experience, linking using `clang` (or `gcc`) will work. >> >> My compiler does that around in this function: >> https://github.com/Leporacanthicus/lacsap/blob/master/binary.cpp#L126 >> >> -- >> Mats >> >> On 18 November 2016 at 11:20, Shilpa B via llvm-dev < >> llvm-dev at lists.llvm.org> wrote: >> >>> Hi, >>> >>> I have a LLVM IR file generated for a different source language (Not C). >>> I have added a rand() function in it to generate random numbers. >>> >>> I compiled the .ll file to .o using clang. However, when I execute the >>> .o file all generated numbers are zeros. >>> >>> How should I link the .ll file with the standard library for the clang >>> to generate the random numbers? >>> >>> >>> Thanks >>> -Shilpa >>> >>> _______________________________________________ >>> LLVM Developers mailing list >>> llvm-dev at lists.llvm.org >>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >>> >>> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161118/0600d0d1/attachment-0001.html>
Shilpa B via llvm-dev
2016-Nov-19 02:13 UTC
[llvm-dev] Linking LLVM IR with standard library
Hi Mats, Thanks for pointing out. Your suggestion solved my problem. You have been of great help. Cheers..:) Thanks, Shilpa On Fri, Nov 18, 2016 at 11:36 PM, mats petersson <mats at planetcatfish.com> wrote:> Do reply to ALL recipients (in particular to the mailing list) when > replying! > > > > On 18 November 2016 at 17:27, Shilpa B <shilpabvb at gmail.com> wrote: > >> Hi Mats, >> >> Thanks for the reply and the link. I am attaching the sample file that I >> had used to test. >> It is an LLVM IR file and I modified it to include rand function to >> generate random numbers and used the command >> >> clang print.ll -std=c++11 -Wl,-rpath,/usr/include -o print.o >> >> to generate the executable file. However, when I execute print.o it shows >> the random numbers generated as zero. >> > > Nope, rand is (most likely) returning correct values you are printing > floating point values from integer values, and that quite often results in > zero being printed - depending on the architecture, register vs. memory > used for varargs, etc. > > .... > @.str = private unnamed_addr constant [4 x i8] c"%f\09\00", align 1 > .... > %callr = call i32 @rand() > store i32 %callr, i32* %arrayidx5, align 8 > %val = load i32, i32* %arrayidx5, align 8 > %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x > i8], [4 x i8]* @.str, i32 0, i32 0), i32 %val) > .... > > change %f to %d, and it lists a bunch of seemingly random numbers, at > least on my machine. > > -- > Mats > >> >> Thanks, >> Shilpa >> >> On Fri, Nov 18, 2016 at 9:39 PM, mats petersson <mats at planetcatfish.com> >> wrote: >> >>> Without reproducible steps, it's hard to give meaningful advice. In my >>> experience, linking using `clang` (or `gcc`) will work. >>> >>> My compiler does that around in this function: >>> https://github.com/Leporacanthicus/lacsap/blob/master/binary.cpp#L126 >>> >>> -- >>> Mats >>> >>> On 18 November 2016 at 11:20, Shilpa B via llvm-dev < >>> llvm-dev at lists.llvm.org> wrote: >>> >>>> Hi, >>>> >>>> I have a LLVM IR file generated for a different source language (Not >>>> C). I have added a rand() function in it to generate random numbers. >>>> >>>> I compiled the .ll file to .o using clang. However, when I execute the >>>> .o file all generated numbers are zeros. >>>> >>>> How should I link the .ll file with the standard library for the clang >>>> to generate the random numbers? >>>> >>>> >>>> Thanks >>>> -Shilpa >>>> >>>> _______________________________________________ >>>> LLVM Developers mailing list >>>> llvm-dev at lists.llvm.org >>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >>>> >>>> >>> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161119/920c038e/attachment.html>