[LLVMdev] Optimization of calls to functions without side effects (from Kaleidoscope example)

Arnaud Allard de Grandmaison Arnaud.AllardDeGrandMaison at dibcom.com
Mon Nov 15 03:13:53 PST 2010


Hi Rob,

You need to set attribute ReadOnly on the sin / cos functions, using Function::addFnAttr(Attribute) for example.

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




More information about the llvm-dev mailing list