This is an archive of the discontinued LLVM Phabricator instance.

[X86] Don't clobber EBX in stackprobes
ClosedPublic

Authored by staticfloat on Sep 2 2021, 3:58 PM.

Details

Summary

On X86, the stackprobe emission code chooses the R11D register, which
is illegal on i686. This ends up wrapping around to EBX, which does
not get properly callee-saved within the stack probing prologue,
clobbering the register for the callers.

We fix this by explicitly using EAX as the stack probe register.

Diff Detail

Event Timeline

staticfloat created this revision.Sep 2 2021, 3:58 PM
staticfloat requested review of this revision.Sep 2 2021, 3:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 2 2021, 3:58 PM

Need a test fix, but lgtm

pengfei added inline comments.Sep 2 2021, 8:45 PM
llvm/lib/Target/X86/X86FrameLowering.cpp
674–676

How about x32, a model that using 32 bit pointer on 64bit target? Can we use

Register FinalStackProbed = Uses64BitFramePtr ? X86::R11 : Is64Bit ? X86::R11D : X86::EAX;
1097–1099

ditto.

vchuravy added a subscriber: Restricted Project.Sep 2 2021, 10:52 PM

Fix test, address review comments

staticfloat marked 2 inline comments as done.Sep 3 2021, 10:27 AM
staticfloat added inline comments.
llvm/lib/Target/X86/X86FrameLowering.cpp
674–676

Ah, good thinking. Better to use these registers when we can. Updated.

staticfloat marked an inline comment as done.Sep 3 2021, 10:27 AM
pengfei added inline comments.Sep 3 2021, 7:01 PM
llvm/test/CodeGen/X86/stack-clash-large.ll
1–3

We'd better add a RUN for the X64-32 case. How about

; RUN: llc -mtriple=x86_64-linux-android < %s | FileCheck -check-prefix=CHECK-X64 %s 
; RUN: llc -mtriple=i686-linux-android < %s | FileCheck -check-prefix=CHECK-X86 %s 
; RUN: llc -mtriple=x86_64-linux-gnux32 < %s | FileCheck -check-prefix=CHECK-X32 %s

We prefer to use prefix "X32" for the X64-32 case only and use X86 for the common 32 bit case.

pengfei added inline comments.Sep 3 2021, 7:02 PM
llvm/lib/Target/X86/X86FrameLowering.cpp
675

Change to the format Lint suggested?

staticfloat marked an inline comment as done.

Add new testing configuration for x32 hardware, apply lint format.

staticfloat marked an inline comment as done.Sep 6 2021, 5:06 PM

Tests expanded, and lint output applied.

llvm/lib/Target/X86/X86FrameLowering.cpp
675

Funnily enough, the arc diff HEAD~ command told me to lint it the old way. Manually re-formatting (as I just did in my latest change) causes the local arc lint step to complain, but I overrode it.

pengfei accepted this revision.Sep 6 2021, 6:28 PM

LGTM. Thanks!

This revision is now accepted and ready to land.Sep 6 2021, 6:28 PM
vtjnash updated this revision to Diff 371071.Sep 7 2021, 7:41 AM

rebase on main

This revision was automatically updated to reflect the committed changes.