<div dir="ltr"><div>Is there a good way to have different versions of a function in LLVM IR used depending on codegen options/attributes?<br></div><div><br></div><div>Suppose there is bitcode library containing math functions (written in OpenCL). It's then linked with user bitcode and optimized before codegen. The user code can, for example, invoke the log function and this would expand into different versions of log, based, for example, on unsafe-fp-math flag.<br></div><div><br></div><div>One solution is to have special control functions that return 0 or 1 and are injected (or linked) into IR based on value of LLVM TargetOptions:<br></div><div>  float log(float f) {</div><div>    if (__control_unsafe_fp()) {</div><div>      unsafe-fp code</div><div>    } else {</div><div>      non-unsafe-fp code</div><div>    }</div><div>  }</div><div><br></div><div>However, this seems to assume unsafe-fp attribute is same for all functions globally (it actually seems to be true for OpenCL). In Latest LLVM unsafe-fp-math is attached as function metadata,</div><div><br></div><div>I don't think there is currently a way to overload function (have different versions) in bitcode library based on function attributes/metadata. Any other ways to do the same?</div><div><br></div><div>I appreciate any advice or thoughts on how this can be implemented.</div><div><br></div><div>Thanks<br></div>Nikolay<div><br></div></div>