<p dir="ltr">Hi Everyone,</p>
<p dir="ltr">I would like to implement GCC ifunc attribute support in Clang and LLVM. At the moment Clang ignores the attribute with a warning. On LLVM IR level there is no support for ifunc. But there is some support for ELF symbol type `@gnu_indirect_function` in ELF reader/writer/target asm parser. This RFC is looking for thoughts and suggestions about representation of ifunc in LLVM IR.</p>
<p dir="ltr">Alternatives for ifunc representation:</p>
<p dir="ltr">1. Add a boolean flag isIFunc to`GlobalAlias`<br>
From implementation perspective ifunc is very close to a function alias that points to resolver function as a target. During asm emissions `@gnu_indirect_function` symbol attribute is added. In printed LLVM it could look like this:</p>
<p dir="ltr">`@foo = alias ifunc i32 (i32), bitcast (i64 ()* @foo_ifunc to i32 (i32)*)`</p>
<p dir="ltr">Pros:<br>
- minimal changes LLVM code base</p>
<p dir="ltr">Cons:<br>
- ifunc is not really an alias, if some code passes through alias and starts using resolver function instead of ifunc, it is always an error<br>
- in particular in my prototype I had to add weak linkage to inhibit optimizations (it prevents them because LLVM assumes that linker could change the symbol but it could be fixed without changing linkage)</p>
<p dir="ltr">2. Extract common parts for alias and ifunc into a base class<br>
Both alias and ifunc will be derived classes. Similar to first proposal add `@gnu_indirect_function` symbol attribute. In printed LLVM it could look like:</p>
<p dir="ltr">`@foo = ifunc i32 (i32), i64 ()* @foo_ifunc`</p>
<p dir="ltr">Pros:<br>
- no confusion for existing code<br>
- cleaner from design perspective</p>
<p dir="ltr">Cons:<br>
- Add new type of Global i.e. more textual changes <br>
- Some potential code duplication (need prototyping to estimate)</p>
<p dir="ltr">3. Emit global asm statement from Clang<br>
Generate global asm statement like `__asm__ (".type resolver_alias_name, @gnu_indirect_function")` in Clang for alias generated for resolver function.</p>
<p dir="ltr">Pros:<br>
- (almost?) no changes in LLVM</p>
<p dir="ltr">Cons:<br>
- ifunc is not marked in LLVM IR at all so some hacks are required to detect and inhibit optimizations in LLVM for such alias (it is always wrong to use resolver instead of ifunc they even have different function type)<br>
- asm statement in general not very reliable mechanism in general, highly depends on expected asm generated by compiler<br>
- using low-level platform dependent information in Clang</p>
<p dir="ltr">I prototyped first approach, changes in<a href="http://reviews.llvm.org/D15525"> http://reviews.llvm.org/D15525</a>. But got feedback that I should use second approach instead + request to write RFC about this IR extension for broader discussion. I'm convinced that the second approach is cleaner and if there is no better suggestion or push back, I'm going to prepare such patch. Third approach mostly added for completeness.<br></p>
<p dir="ltr">References:<br>
- GCC ifunc documentation<a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html"> https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html</a><br>
- IFUNC low-level technical details<a href="http://www.airs.com/blog/archives/403"> http://www.airs.com/blog/archives/403</a><br></p>
<p dir="ltr">Thanks,<br>
Dmitry Polukhin<br>
--<br>
Software Engineer<br>
Intel Compiler Team</p>