Proposal: @loopHint(hint: LoopHint) void
builtin function
#22151
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Currently there is no mechanism give the compiler information on how a loop should be optimized. For example, to prevent LLVM's unrolling this loop:
you can do something like call
std.mem.doNotOptimizeAway
:but it's not clear precisely what the effect of
std.mem.doNotOptimizeAway
is here and it can't be used to provide fine-grained control, such as specifying an unroll count.Proposal
Similar to
@branchHint
, a new builtin should be used to provide loop optimization hints. It's signature would be:If we look at the loop optimization metadata that LLVM has (start here and scroll down to
irr_loop
to see them all) for controlling loop optimizations, there is potential for theLoopHint
type to be quite complicated. We probably shouldn't provide direct access to all these options; I think it makes sense to provide some high-level options inLoopHint
to control unrolling, vectorisation and interleaving.The clang loop directives, available through
#pragma clang loop ...
are:Clang doesn't seem to provide directives for controlling LLVM's unroll-and-jam pass, which is a kind of unrolling that can be done on nested loop.
If we wanted similar expressive capabilities as clang, we could define
std.builtin.LoopHint
something like:where the
.auto
options are like not calling@loopHint
. There would be some redundancy here: setting a count/width to 1 would be likedisable
, and it's not clear what count/width of 0 should mean. By using default values, we can always start with a more restrictedLoopHint
and expand it over time.Note that the effect of hints provided by
@loopHint
will depend on the backend used and they shouldn't be considered to be specifying hard requirements of the generated code.Similar to
@branchHint
,@loopHint
would have to be at the start of a loop body, but I would suggest that when both@branchHint
and@loopHint
are used, they can come in either order:Motivation
The initial motivation for wanting a
@loopHint
was to control LLVM's unrolling when implementing functions likememcpy
/memset
/memmove
. While it is possible to force a certain amount of unrolling with inline loops, LLVM will still try to unroll these loops further on some targets, and can be too aggressive when doing so. While an@unrollHint
would be sufficient for these use-cases, even if we only want to initially support providing unroll hints@loopHint
could be forwards compatible with any future loop-optimisations hints we want to support, requiring no language change (other than changingstd.builin.LoopHint
, if that counts as a language change).The text was updated successfully, but these errors were encountered: