As an embedded platform, we would very much like to reap the code size benefits of MachineOutliner. However, for the moment MachineOutliner is disabled for CHERIoT, as it does not interact well with the backwards sentries used for call/return on CHERIoT. Specifically:
The MachineOutliner uses ct0 as a scratch register, both to materialize the outlined function address prior to the call, as well as to hold the return address for the duration of the call to the outlined function. The outlined outlined function then returns through ct0 back to the original function. Notice that this does not use cra, the normal link register.
In CHERIoT, any cjal or cjalr instruction that produces a link output, regardless of what register it writes to, always produces a backward sentry otype.
The function return variant of cjalr used in the outlined function expects a different otype based on the source register used: for cra it expects a sealed backward sentry, but for all other registers it expects a sealed forward sentry.
Unfortunately, there's no simple way to fix this. A few options are:
Restrict outlining to only cases where cra is dead so we can scavenge it.
Restrict outlining to only produce outlined functions that end in tail calls
Spill/restore the link register around the call to the outlined function, either to the stack or to another register that we scavenge.
The AArch64 backend actually implements all three of these strategies, as an optimization to attain minimal code size. It might make sense to port these strategies from AArch64 to RISCV in upstream, and then have CHERIoT bail out of outlining only when those strategies fail.
As an embedded platform, we would very much like to reap the code size benefits of MachineOutliner. However, for the moment MachineOutliner is disabled for CHERIoT, as it does not interact well with the backwards sentries used for call/return on CHERIoT. Specifically:
ct0
as a scratch register, both to materialize the outlined function address prior to the call, as well as to hold the return address for the duration of the call to the outlined function. The outlined outlined function then returns throughct0
back to the original function. Notice that this does not usecra
, the normal link register.cjal
orcjalr
instruction that produces a link output, regardless of what register it writes to, always produces a backward sentryotype
.cjalr
used in the outlined function expects a differentotype
based on the source register used: forcra
it expects a sealed backward sentry, but for all other registers it expects a sealed forward sentry.Unfortunately, there's no simple way to fix this. A few options are:
cra
is dead so we can scavenge it.The AArch64 backend actually implements all three of these strategies, as an optimization to attain minimal code size. It might make sense to port these strategies from AArch64 to RISCV in upstream, and then have CHERIoT bail out of outlining only when those strategies fail.