SANGEETA CHOWDHARY via llvm-dev
2018-Aug-11 19:30 UTC
[llvm-dev] Need help in understanding llvm optimization
Hi, I have below code in C - int main() { double x,y; x = 1e16; y = (x + 1) - x; printf("y:%e\n", y); return 0; } llvm bitcode looks like this for this function - ; Function Attrs: nounwind uwtable define dso_local i32 @main() local_unnamed_addr #0 { entry: %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double 0.000000e+00) ret i32 0 } I am not able to understand how addition and subtraction are performed in this code. There is no fadd or fsub instruction. How llvm knows that result of y is 0? Is there any way to disable this in llvm? Any help would be much appreciated. Regards, Sangeeta -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180811/7e03ffa5/attachment.html>
Craig Topper via llvm-dev
2018-Aug-11 20:06 UTC
[llvm-dev] Need help in understanding llvm optimization
Neither 1e16 or 1e16+1 can be accurately represented in a double. The largest integer than be accurately represented is 2^53. As the number gets larger floating sacrifices precision in the lower digits. Because of this 1e16 and 1e16+1 end up having the same representation. So the result of subtracting them is 0.0. On Sat, Aug 11, 2018 at 12:30 PM SANGEETA CHOWDHARY via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I have below code in C - > > int main() { > > double x,y; > > x = 1e16; > > y = (x + 1) - x; > > printf("y:%e\n", y); > > return 0; > > } > > llvm bitcode looks like this for this function - > > > ; Function Attrs: nounwind uwtable > > define dso_local i32 @main() local_unnamed_addr #0 { > > entry: > > %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 > x i8], [4 x i8]* @.str, i64 0, i64 0), double 0.000000e+00) > > ret i32 0 > > } > > I am not able to understand how addition and subtraction are performed in > this code. There is no fadd or fsub instruction. How llvm knows that result > of y is 0? > > Is there any way to disable this in llvm? > > > Any help would be much appreciated. > > > Regards, > > Sangeeta > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-- ~Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180811/4b1b85ed/attachment.html>
SANGEETA CHOWDHARY via llvm-dev
2018-Aug-11 20:15 UTC
[llvm-dev] Need help in understanding llvm optimization
I am sorry to not make my question clear. My question is how this value is calculated without having fadd and fsub in IR? On Sat, Aug 11, 2018 at 4:06 PM, Craig Topper <craig.topper at gmail.com> wrote:> Neither 1e16 or 1e16+1 can be accurately represented in a double. The > largest integer than be accurately represented is 2^53. As the number gets > larger floating sacrifices precision in the lower digits. Because of this > 1e16 and 1e16+1 end up having the same representation. So the result of > subtracting them is 0.0. > > On Sat, Aug 11, 2018 at 12:30 PM SANGEETA CHOWDHARY via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> Hi, >> >> I have below code in C - >> >> int main() { >> >> double x,y; >> >> x = 1e16; >> >> y = (x + 1) - x; >> >> printf("y:%e\n", y); >> >> return 0; >> >> } >> >> llvm bitcode looks like this for this function - >> >> >> ; Function Attrs: nounwind uwtable >> >> define dso_local i32 @main() local_unnamed_addr #0 { >> >> entry: >> >> %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 >> x i8], [4 x i8]* @.str, i64 0, i64 0), double 0.000000e+00) >> >> ret i32 0 >> >> } >> >> I am not able to understand how addition and subtraction are performed in >> this code. There is no fadd or fsub instruction. How llvm knows that result >> of y is 0? >> >> Is there any way to disable this in llvm? >> >> >> Any help would be much appreciated. >> >> >> Regards, >> >> Sangeeta >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> > -- > ~Craig >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180811/07efab21/attachment-0001.html>
Bruce Hoult via llvm-dev
2018-Aug-12 00:57 UTC
[llvm-dev] Need help in understanding llvm optimization
You might find it useful to write functions that take arguments (of unknown value). If the function has external linkage (the default) then the compiler doesn't know who else might call it and has to keep it around, even if it inlines some copies of it: double foo(double x){ double y = (x + 1) - x; return y; } If you compile it with "-c" or "-S" then you don't need a main() at all. On Sat, Aug 11, 2018 at 12:30 PM, SANGEETA CHOWDHARY via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I have below code in C - > > int main() { > > double x,y; > > x = 1e16; > > y = (x + 1) - x; > > printf("y:%e\n", y); > > return 0; > > } > > llvm bitcode looks like this for this function - > > > ; Function Attrs: nounwind uwtable > > define dso_local i32 @main() local_unnamed_addr #0 { > > entry: > > %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 > x i8], [4 x i8]* @.str, i64 0, i64 0), double 0.000000e+00) > > ret i32 0 > > } > > I am not able to understand how addition and subtraction are performed in > this code. There is no fadd or fsub instruction. How llvm knows that result > of y is 0? > > Is there any way to disable this in llvm? > > > Any help would be much appreciated. > > > Regards, > > Sangeeta > > _______________________________________________ > 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/20180811/f414913f/attachment.html>
SANGEETA CHOWDHARY via llvm-dev
2018-Aug-12 17:11 UTC
[llvm-dev] Need help in understanding llvm optimization
Cool, thank you so much. On Sat, Aug 11, 2018 at 8:57 PM, Bruce Hoult <brucehoult at sifive.com> wrote:> You might find it useful to write functions that take arguments (of > unknown value). If the function has external linkage (the default) then the > compiler doesn't know who else might call it and has to keep it around, > even if it inlines some copies of it: > > double foo(double x){ > double y = (x + 1) - x; > return y; > } > > If you compile it with "-c" or "-S" then you don't need a main() at all. > > On Sat, Aug 11, 2018 at 12:30 PM, SANGEETA CHOWDHARY via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> Hi, >> >> I have below code in C - >> >> int main() { >> >> double x,y; >> >> x = 1e16; >> >> y = (x + 1) - x; >> >> printf("y:%e\n", y); >> >> return 0; >> >> } >> >> llvm bitcode looks like this for this function - >> >> >> ; Function Attrs: nounwind uwtable >> >> define dso_local i32 @main() local_unnamed_addr #0 { >> >> entry: >> >> %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 >> x i8], [4 x i8]* @.str, i64 0, i64 0), double 0.000000e+00) >> >> ret i32 0 >> >> } >> >> I am not able to understand how addition and subtraction are performed in >> this code. There is no fadd or fsub instruction. How llvm knows that result >> of y is 0? >> >> Is there any way to disable this in llvm? >> >> >> Any help would be much appreciated. >> >> >> Regards, >> >> Sangeeta >> >> _______________________________________________ >> 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/20180812/3c4db320/attachment.html>