runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[ARM/softfp] ProfileLeave profiler hook overwrites a float return value.

Open kvochko opened this issue 10 years ago • 6 comments

When enabling profiling on ARM/softfp, the generated JIT code for the call to ProfileLeave overwrites the function's floating-point return value with an argument to ProfileLeave. Here is an example to illustrate. The following function:

   public static float retFloat() {
        float f = 1.8f;
        for (int i = 0; i < 10; ++i); // prevent the function from being optimized out
        return f;
   }

gets jitted into the following:

;; Assembly listing for method Program:retFloat():float
; Emitting BLENDED_CODE for generic ARM CPU
; optimized code
; r11 based frame
; fully interruptible
; Final local variable assignments
;
;* V00 loc0         [V00,T02] (  0,   0  )   float  ->  zero-ref   
;  V01 loc1         [V01,T00] (  4,  13  )     int  ->   r3        
;  V02 tmp0         [V02,T01] (  2,   4  )   float  ->   f0        
;# V03 OutArgs      [V03    ] (  1,   1  )  lclBlk ( 0) [sp+0x00]  
;
; Lcl frame size = 4

G_M53947_IG01:
000000  B40F           push    {r0,r1,r2,r3}
000002  E92D 4C08      push    {r3,r10,r11,lr}
000006  F10D 0B08      add     r11, sp, 8
00000A  F248 10AC      movw    r0, 0x81ac
00000E  F2CB 50A3      movt    r0, 0xb5a3
000012  F10B 0118      add     r1, r11, 24
000016  F24F 5397      movw    r3, 0xf597
00001A  F2CB 6363      movt    r3, 0xb663
00001E  4798           blx     r3       // CORINFO_HELP_PROF_FCN_ENTER

G_M53947_IG02:
000020  2300           movs    r3, 0

G_M53947_IG03:
000022  3301           adds    r3, 1
000024  2B0A           cmp     r3, 10
000026  DBFC           blt     SHORT G_M53947_IG03

G_M53947_IG04:
000028  F246 6366      movw    r3, 0x6666
00002C  F6C3 73E6      movt    r3, 0x3fe6
000030  EE00 3A10      vmov.i2f s0, r3

G_M53947_IG05:
000034  EE10 0A10      vmov.f2i r0, s0
000038  F248 10AC      movw    r0, 0x81ac
00003C  F2CB 50A3      movt    r0, 0xb5a3
000040  F24F 53D7      movw    r3, 0xf5d7
000044  F2CB 6363      movt    r3, 0xb663
000048  4798           blx     r3       // CORINFO_HELP_PROF_FCN_LEAVE

G_M53947_IG06:
00004A  E8BD 4C08      pop     {r3,r10,r11,lr}
00004E  B004           add     sp, 16
000050  4770           bx      lr

; Total bytes of code 82, prolog size 32 for method Program:retFloat():float
; ============================================================

@papaslavik @Dmitri-Botcharnikov @sam-baravy @seanshpark @janvorli

kvochko avatar Jul 06 '16 11:07 kvochko

A similar function that returns an int saves the return value into r2, and restores it to r0 after the call:

G_M40600_IG05:
000028  4602           mov     r2, r0
00002A  F248 10CC      movw    r0, 0x81cc
00002E  F2CB 50A3      movt    r0, 0xb5a3
000032  F24F 53D7      movw    r3, 0xf5d7
000036  F2CB 6363      movt    r3, 0xb663
00003A  4798           blx     r3       // CORINFO_HELP_PROF_FCN_LEAVE
00003C  4610           mov     r0, r2

G_M40600_IG06:
00003E  E8BD 4800      pop     {r11,lr}
000042  B004           add     sp, 16
000044  4770           bx      lr

; Total bytes of code 70, prolog size 30 for method Program:retInt():int
; ============================================================

kvochko avatar Jul 06 '16 11:07 kvochko

cc: @wateret @papaslavik @hseok-oh @leemgs @myungjoo

hqueue avatar Jul 07 '16 05:07 hqueue

LGTM

papaslavik avatar Jul 07 '16 07:07 papaslavik

LGTM

Dmitri-Botcharnikov avatar Jul 07 '16 09:07 Dmitri-Botcharnikov

There's a few questions regarding this.

  1. Why is r0 saved to r2, which is a volatile register, according to the ABI?
  2. For doubles, shouldn't both r0 and r1 be saved?
  3. On AMD64 ProfileLeave (and ProfileEnter for that matter) callbacks take 2 arguments: profiler handle and caller's SP, but on ARM it takes just one. Is there a reason for that?

kvochko avatar Jul 07 '16 12:07 kvochko

Resolved by dotnet/coreclr#6976

kvochko avatar Sep 05 '16 11:09 kvochko