This is an archive of the discontinued LLVM Phabricator instance.

[LowerTypeTests,ARM] Support Armv8-M BTI in jump tables.
ClosedPublic

Authored by simon_tatham on Aug 11 2023, 1:49 AM.

Details

Summary

This adds support for the branch target enforcement system available
in 32-bit Armv8-M, following the pattern of the implementation that
already exists for the analogous AArch64 feature. If we're generating
a Thumb jump table, and branch target enforcement is enabled in the
module being processed, then we must put a BTI instruction at the
start of each jump table entry, to make it a valid target for an
indirect branch.

Also, we clear the branch-target-enforcement flag on the naked
function that contains the jump table asm statement, to ensure a spare
BTI doesn't accidentally appear at the start of that. (Again, this is
how AArch64 already does it.) This is done unconditionally, on the
theory that clearing those flags should be harmless in any existing
situation.

To avoid too much tedious code duplication, I've factored out
AArch64's check for branch target enforcement into a helper method of
LowerTypeTestsModule, so that it can be called more conveniently from
lots of places. While I was doing that, I also made it cache its
result, so it no longer calls M.getModuleFlag with the same string key
for every single jump table entry it generates.

Diff Detail

Event Timeline

simon_tatham created this revision.Aug 11 2023, 1:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 1:49 AM
simon_tatham requested review of this revision.Aug 11 2023, 1:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2023, 1:49 AM
olista01 added inline comments.
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1484

It would be better to add pacbti to target-features, because setting the cpu might enable other features, such as MVE.

Changed target-cpu setting to target-features as suggested.

simon_tatham marked an inline comment as done.Aug 11 2023, 4:01 AM
olista01 added inline comments.Aug 11 2023, 4:31 AM
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1484

Does calling addFnAttr twice concatenate the value strings, or will this replace the "+thumb-mode" attribute? This should also be added to the test.

simon_tatham marked an inline comment as done.

Update to be clearer about setting target-features.

llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1484

Ugh, apparently you're right, it replaces the previous value of target-features.

It probably doesn't actually matter at the moment, because PACBTI is only supported on CPUs where there's no choice but Thumb anyway. It certainly generated sensible output code when I tried an end-to-end compile-and-run test. But I've changed it anyway, and added the extra test check.

olista01 accepted this revision.Aug 11 2023, 5:21 AM

Thanks, LGTM

This revision is now accepted and ready to land.Aug 11 2023, 5:21 AM
danielkiss accepted this revision.Aug 11 2023, 6:05 AM
MaskRay accepted this revision.Aug 11 2023, 7:06 AM
This revision was landed with ongoing or failed builds.Aug 11 2023, 9:42 AM
This revision was automatically updated to reflect the committed changes.