This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Make __attribute__(const) calls speculatable
Needs RevisionPublic

Authored by grosser on Jun 1 2017, 5:25 AM.

Details

Summary

Today 'const' functions are only marked 'readnone' and 'nounwind', but lack the
'speculatable' attribute for historic reasons. As 'const' functions are known to
not have any side effects and are intended to enable loop optimizations, they
are indeed 'speculatable' and consequently should be marked as such.

Some history: Back before r44273 (long time ago) readnone was indeed called
const and LLVM was assuming that readnone functions do not contain infinite
loops and can be hoisted. This worked at the beginning, but LLVM learned over
time to infer the readnone attribute from function definitions. As a result,
infinite functions that do not touch memory were marked as readnone, incorrectly
also stating that they are free of infinite loops, because different LLVM passes
still assumed a one-to-one correspondence between '__attribute(const)' and
LLVM's readnone attribute. Over time, we learned that 'readnone' must not imply
absence of infinite loops and other side effects to allow us to derive this
attribute automatically. Hence, the definition of readnone was changed to not
give information about the termination of functions. To still provide
information about side effects outside of memory effects LLVM recently learned
about speculatable function attributes: (https://reviews.llvm.org/D20116)

With 'speculatable' now available, we can pass information about the absence
of non-memory side effects to LLVM-IR.

This idea was taken from earlier discussions where for example Chris suggested
this solution:

"This really only matters when the compiler is able to infer readnone/readonly,
which typically doesn't include cases with indirect calls. Per #2, I think it
could be handled by making the GCC-style pure/const attributes imply both
readonly/readnone *and* halting. :

Event Timeline

grosser created this revision.Jun 1 2017, 5:25 AM
chapuni added a subscriber: chapuni.Jun 1 2017, 5:30 AM
Meinersbur edited edge metadata.Jun 1 2017, 6:01 AM

Definition of __attibute__((const)) from https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.
Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void.

Definition of speculatable from http://llvm.org/docs/LangRef.html#function-attributes

This function attribute indicates that the function does not have any effects besides calculating its result and does not have undefined behavior.

Consider something like this:

define i32 @div(i32 %x, i32 %y) {
entry:
  %div = sdiv i32 %x, %y
  ret i32 %div
}

We can mark this function readnone, but not speculatable: it doesn't read or write memory, but could exhibit undefined behavior.

Consider another function:

@x = common local_unnamed_addr global i32 0, align 4
define i32 @get_x() {
entry:
  %0 = load i32, i32* @x, align 4, !tbaa !2
  ret i32 %0
}

We can mark this function speculatable, but not readnone: it doesn't exhibit undefined behavior or have side-effects, but it does read memory.

Given that, it seems a little dubious to translate __attribute__((const)) into speculatable.

sanjoy resigned from this revision.Jan 29 2022, 5:34 PM
Meinersbur requested changes to this revision.Mar 29 2022, 6:42 PM

speculable implies no undefined behavior (so it can even be speculatively executed with arguments when the source code would not, but its result eventually discarded), but const does not.

This revision now requires changes to proceed.Mar 29 2022, 6:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 6:42 PM