I have a read-only lookup table, which takes integer arguments. Optimally I would like to indicate in the C source, that the function is invariant (?), so that the superflous calls can be optimized away. ``` extern void *lookup( int a); void foo( void) { void *a, *b; a = lookup( 0x12345678); bar( a); b = lookup( 0x12345678); // b guaranteed to be the same as a, no call to (slow) lookup needed baz( b); } ``` But I don't really find anything in clang or llvm in terms of function attributes. The best I could come up with is in the llvm code to use attributes "argmemonly=true" readonly=true". But still two calls did happen. (I am on 3.7) Ciao Nat! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160109/b58cd3b6/attachment.html>
Martin J. O'Riordan via llvm-dev
2016-Jan-09 15:00 UTC
[llvm-dev] Attribute for a invariant function ?
I use '__attribute__((pure))' for fast math functions and some other intrinsic function I have added such as getting the current core ID. This tells the compiler that calling the same function with the same arguments will always return the same value. A function declared with this attribute is not supposed to have any side-effects such as altering global variables. So: extern void *lookup(int) __attribute__((pure)); might do what you need though. MartinO From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Nat! via llvm-dev Sent: 09 January 2016 14:41 To: llvm-dev <llvm-dev at lists.llvm.org> Subject: [llvm-dev] Attribute for a invariant function ? I have a read-only lookup table, which takes integer arguments. Optimally I would like to indicate in the C source, that the function is invariant (?), so that the superflous calls can be optimized away. ``` extern void *lookup( int a); void foo( void) { void *a, *b; a = lookup( 0x12345678); bar( a); b = lookup( 0x12345678); // b guaranteed to be the same as a, no call to (slow) lookup needed baz( b); } ``` But I don't really find anything in clang or llvm in terms of function attributes. The best I could come up with is in the llvm code to use attributes "argmemonly=true" readonly=true". But still two calls did happen. (I am on 3.7) Ciao Nat! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160109/09d04d01/attachment.html>
Joerg Sonnenberger via llvm-dev
2016-Jan-09 15:12 UTC
[llvm-dev] Attribute for a invariant function ?
On Sat, Jan 09, 2016 at 03:00:33PM -0000, Martin J. O'Riordan via llvm-dev wrote:> I use '__attribute__((pure))' for fast math functions and some other > intrinsic function I have added such as getting the current core ID. This > tells the compiler that calling the same function with the same arguments > will always return the same value. A function declared with this attribute > is not supposed to have any side-effects such as altering global variables.I am considering a similar use case, which strictly speaking is not a valid use of pure. Consider the xlocale interface: l = newlocale(...); if (isalpha_l(c, l)) .... foo(); if (isalpha_l(c, l)) .... freelocale(l); The call to isalpha_l is redundant, but the value depends on l within the life time -- strictly speaking with pure, the compiler could remember the value of l and use a cached value accordingly. Joerg
Martin J. O'Riordan schrieb:> > > extern void *lookup(int) __attribute__((pure)); > > might do what you need though. >Good, that's pretty much what I was looking for. With that I was able to google for https://stackoverflow.com/questions/29117836/attribute-const-vs-attribute-pure-in-gnu-c which leads me to: __attribute__((const)) function attribute Many functions examine only the arguments passed to them, and have no effects except for the return value. This is a much stricter class than __attribute__((pure)), because a function is not permitted to read global memory. If a function is known to operate only on its arguments then it can be subject to common sub-expression elimination and loop optimizations. The last sentence is what I am after. :) Unfortunately at least in clang 3.7 it does not seem to do anything. Thanks for the help. Ciao Nat! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160109/f78af49c/attachment.html>
Johannes Doerfert via llvm-dev
2016-Jan-09 18:34 UTC
[llvm-dev] Attribute for a invariant function ?
Try: extern void *lookup( int a) __attribute__ ((const)); Cheers, Johannes On 01/09, Nat! via llvm-dev wrote:> I have a read-only lookup table, which takes integer arguments. Optimally I > would like to indicate in the C source, that the function is invariant (?), > so that the superflous calls can be optimized away. > > ``` > extern void *lookup( int a); > > void foo( void) > { > void *a, *b; > > a = lookup( 0x12345678); > bar( a); > > b = lookup( 0x12345678); // b guaranteed to be the same as a, no call to > (slow) lookup needed > baz( b); > } > ``` > > But I don't really find anything in clang or llvm in terms of function > attributes. The best I could come up with is in the llvm code to use > attributes "argmemonly=true" readonly=true". But still two calls did happen. > (I am on 3.7) > > Ciao > Nat! >> _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-- Johannes Doerfert Researcher / PhD Student Compiler Design Lab (Prof. Hack) Saarland University, Computer Science Building E1.3, Room 4.31 Tel. +49 (0)681 302-57521 : doerfert at cs.uni-saarland.de Fax. +49 (0)681 302-3065 : http://www.cdl.uni-saarland.de/people/doerfert -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 213 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160109/62e8b126/attachment.sig>