Rob Pieke
2010-Nov-15 10:40 UTC
[LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)
In http://llvm.org/docs/tutorial/LangImpl4.html#jit there's an example that optimizes calls to functions without side effects. Specifically, ready> extern sin(x); ready> extern cos(x); ready> def foo(x) sin(x)*sin(x) + cos(x)*cos(x); Read function definition: define double @foo(double %x) { entry: %calltmp = call double @sin(double %x) %multmp = fmul double %calltmp, %calltmp %calltmp2 = call double @cos(double %x) %multmp4 = fmul double %calltmp2, %calltmp2 %addtmp = fadd double %multmp, %multmp4 ret double %addtmp } I find that when I run the code, the calls to sin and cos aren't optimized and, instead, I end up with: define double @foo(double %x) { entry: %calltmp = call double @sin(double %x) %calltmp1 = call double @sin(double %x) %multmp = fmul double %calltmp, %calltmp1 %calltmp2 = call double @cos(double %x) %calltmp3 = call double @cos(double %x) %multmp4 = fmul double %calltmp2, %calltmp3 %addtmp = fadd double %multmp, %multmp4 ret double %addtmp } How do you tell LLVM if a function has side effects or not? Cheers! - Rob
Duncan Sands
2010-Nov-15 12:55 UTC
[LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)
Hi,> You need to set attribute ReadOnly on the sin / cos functions, using Function::addFnAttr(Attribute) for example.the readnone attribute means that the function doesn't dereference any pointers, doesn't read any global variables etc; while readonly means that while the function can dereference pointers, it only reads memory and doesn't write it. Declaring something readnone is stronger than declaring it readonly. If sin and cos do not write to errno (or you want to ignore that they write to errno) then you can declare them readonly. If they also don't depend on the floating point rounding mode (which is modelled as a global variable in LLVM) or you don't care about rounding modes, then you can declare them readnone. Ciao, Duncan.> > Best regards, > -- > Arnaud de Grandmaison > > -----Original Message----- > From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Rob Pieke > Sent: Monday, November 15, 2010 11:41 AM > To: llvmdev at cs.uiuc.edu > Subject: [LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example) > > In http://llvm.org/docs/tutorial/LangImpl4.html#jit there's an example that optimizes calls to functions without side effects. Specifically, > > ready> extern sin(x); > ready> extern cos(x); > ready> def foo(x) sin(x)*sin(x) + cos(x)*cos(x); > Read function definition: > define double @foo(double %x) { > entry: > %calltmp = call double @sin(double %x) > %multmp = fmul double %calltmp, %calltmp > %calltmp2 = call double @cos(double %x) > %multmp4 = fmul double %calltmp2, %calltmp2 > %addtmp = fadd double %multmp, %multmp4 > ret double %addtmp > } > > I find that when I run the code, the calls to sin and cos aren't optimized and, instead, I end up with: > > define double @foo(double %x) { > entry: > %calltmp = call double @sin(double %x) > %calltmp1 = call double @sin(double %x) > %multmp = fmul double %calltmp, %calltmp1 > %calltmp2 = call double @cos(double %x) > %calltmp3 = call double @cos(double %x) > %multmp4 = fmul double %calltmp2, %calltmp3 > %addtmp = fadd double %multmp, %multmp4 > ret double %addtmp > } > > How do you tell LLVM if a function has side effects or not? > > Cheers! > > - Rob > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Rob Pieke
2010-Nov-15 13:39 UTC
[LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)
> You need to set attribute ReadOnly on the sin / cos functions, using > Function::addFnAttr(Attribute) for example.Hmm ... I tried setting that right after Function::Create but I still get the same result (though flagged with "readonly") declare double @sin(double) readonly declare double @cos(double) readonly define double @foo(double %x) readonly { entry: %calltmp = call double @sin(double %x) %calltmp1 = call double @sin(double %x) %multmp = fmul double %calltmp, %calltmp1 %calltmp2 = call double @cos(double %x) %calltmp3 = call double @cos(double %x) %multmp4 = fmul double %calltmp2, %calltmp3 %addtmp = fadd double %multmp, %multmp4 ret double %addtmp }
Maybe Matching Threads
- [LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)
- [LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)
- Kaleidoscope tutorial: comments, corrections and Windows support
- [LLVMdev] C Backend's future
- [LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)