This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Lower READCYCLECOUNTER using MRS CNTVCT_EL0
ClosedPublic

Authored by salvatoredipietro on Oct 28 2022, 4:54 PM.

Details

Summary

As suggested in D12425 it would be better for the readcyclecounter function on ARM architecture to use the CNTVCT_EL0 register (Counter-timer Virtual Count register) instead of the PMCCNTR_EL0 (Performance Monitors Cycle Count Register) because the PMCCNTR_EL0 is a PMU register which, depending on the configuration, it might always return zeroes and it doesn't guaranteed to always be increased.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptOct 28 2022, 4:54 PM
salvatoredipietro requested review of this revision.Oct 28 2022, 4:54 PM
sebpop added a subscriber: sebpop.Oct 31 2022, 11:04 AM

Hi Salvatore,
the patch looks good to me.
As you suggested to me, we should also remove this part let Predicates = [HasPerfMon] in.

A little bit more context on what this patch fixes: MariaDB compiled with clang crashes on arm64-freebsd https://jira.mariadb.org/browse/MDEV-23249

Hi Sebastian,
have update the diff removing the predicate for the READCYCLECOUNTER function

sebpop accepted this revision.Nov 4 2022, 2:15 PM

The change looks good to me.
Could one of the arm64 maintainers also approve this patch? Thanks!

This revision is now accepted and ready to land.Nov 4 2022, 2:15 PM
salvatoredipietro added a comment.EditedNov 22 2022, 4:10 PM

Thank @sebpop for approving it.
Since I do not have commit access and if there is no other objections, Can you please commit the change on my behalf? Thanks

Ping patch.
Could one of the arm64 maintainers approve this patch and commit?
Thank you.

sebpop added a comment.Dec 5 2022, 1:58 PM

Ping patch.

In the Linux kernel rdtsc for Arm64 is implemented with CNTVCT_EL0:

https://lore.kernel.org/patchwork/patch/1305380/

The system register CNTVCT_EL0 can be used to retrieve the counter from
user space. Add rdtsc() for Arm64.

Hello. I gave the two registers a look and CNTVCT_EL0 seemed to work better than PMCCNTR_EL0 on the systems I tried. The existing PMCCNTR_EL0 would run into illegal instruction exceptions.

This was added by @ab though and I only tried linux/android. It would be good to hear from them about whether changing this is OK for their platform.

For context, MySQL had also to overwrite this function and it is using the CNTVCT_EL0 register for over 3 years now

ab accepted this revision.Dec 8 2022, 9:12 AM

This makes sense, thanks for checking! I'll have a closer look on my side and follow-up if needed

OK great, thanks.

If you provide a way to attribute this patch ("name <em@ail>"), I am happy to commit it.

OK great, thanks.

If you provide a way to attribute this patch ("name <em@ail>"), I am happy to commit it.

sure, "Salvatore Dipietro <dipiets@amazon.com>"

This revision was automatically updated to reflect the committed changes.