[edk2-devel] CpuDeadLoop() is optimized by compiler

Pedro Falcato pedro.falcato at gmail.com
Thu May 18 13:19:10 UTC 2023


On Thu, May 18, 2023 at 10:59 AM Ni, Ray <ray.ni at intel.com> wrote:
>
> Hi,
>
> Starting from certain version of Visual Studio C compiler (I don’t have the exact version. I am using VS2019), CpuDeadLoop is now optimized quite well by compiler.
>
>
>
> The optimization is so “good” that it becomes harder for developers to break out of the deadloop.
>
>
>
> I copied the assembly instructions as below for your reference.
>
> The compiler does not generate instructions that jump out of the loop when the Index is not zero.
>
> So in order to break out of the loop, developers need to:
>
> Manually adjust rsp by increasing 40
> Manually “ret”
>
>
>
> I am not sure if anyone has interest to re-write this function so that compiler can be “fooled” again.
>
> Thanks,
> Ray
>
>
>
> =======================
>
> ; Function compile flags: /Ogspy
>
> ; File e:\work\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c
>
> ;              COMDAT CpuDeadLoop
>
> _TEXT    SEGMENT
>
> Index$ = 48
>
> CpuDeadLoop PROC                                                                    ; COMDAT
>
>
>
> ; 26   : {
>
>
>
> $LN12:
>
>   00000  48 83 ec 28         sub        rsp, 40                                ; 00000028H
>
>
>
> ; 27   :   volatile UINTN  Index;
>
> ; 28   :
>
> ; 29   :   for (Index = 0; Index == 0;) {
>
>
>
>   00004  48 c7 44 24 30
>
>                00 00 00 00        mov      QWORD PTR Index$[rsp], 0
>
> $LN10 at CpuDeadLoo:
>
>
>
> ; 30   :     CpuPause ();
>
>
>
>   0000d  48 8b 44 24 30   mov      rax, QWORD PTR Index$[rsp]
>
>   00012  e8 00 00 00 00   call        CpuPause
>
>   00017  eb f4                     jmp       SHORT $LN10 at CpuDeadLoo
>
> CpuDeadLoop ENDP
>
> _TEXT    ENDS
>
> END

Hi Ray,

Can you try something like this? https://godbolt.org/z/x7P1PqY59

Seems to work, but godbolt does not support MSVC LTO :/

-- 
Pedro


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#105025): https://edk2.groups.io/g/devel/message/105025
Mute This Topic: https://groups.io/mt/98987896/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list