[llvm-bugs] [Bug 44687] New: clang CSE's __builtin_arm_rsr64 et al

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jan 27 15:44:40 PST 2020


            Bug ID: 44687
           Summary: clang CSE's __builtin_arm_rsr64 et al
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: roland at hack.frob.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

Test case:
#include <arm_acle.h>
#include <stdint.h>

// Workaround for Cortex-A73 erratum 858921.
uint64_t read_cntpct_a73(void) {
  const uint64_t old_read = __arm_rsr64("cntpct_el0");
  const uint64_t new_read = __arm_rsr64("cntpct_el0");
  return (((old_read ^ new_read) >> 32) & 1) ? old_read : new_read;

clang --target=aarch64-{fuchsia,linux} -O1 (or higher) compiles this to:
        .file   "aclecse.c"
        .globl  read_cntpct_a73         // -- Begin function read_cntpct_a73
        .p2align        2
        .type   read_cntpct_a73, at function
read_cntpct_a73:                        // @read_cntpct_a73
// %bb.0:
        mrs     x0, CNTPCT_EL0
        .size   read_cntpct_a73, .Lfunc_end0-read_cntpct_a73
                                        // -- End function

It has folded the two __arm_rsr64 (aka __builtin_arm_rsr64) calls together.
This is not kosher for many uses of system registers.  I haven't checked
whether the related builtins like rsr and rsrp have the same problem, but it
would be wrong for them too.

The compiler needs to treat these builtins as having side effects even though
they just "read a value".

Using `-emit-llvm` shows that the CSE is happening in the front end:
; ModuleID = 'aclecse.c'
source_filename = "aclecse.c"
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux"
; Function Attrs: nounwind readonly
define dso_local i64 @read_cntpct_a73() local_unnamed_addr #0 {
  %1 = tail call i64 @llvm.read_register.i64(metadata !2)
  ret i64 %1
; Function Attrs: nounwind readonly
declare i64 @llvm.read_register.i64(metadata) #1

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200127/ff04c32d/attachment.html>

More information about the llvm-bugs mailing list