Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeGen][X86] Fix lowering of tailcalls when -ms-hotpatch is used #77245

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions llvm/include/llvm/Support/TargetOpcodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,10 @@ HANDLE_TARGET_OPCODE(LOCAL_ESCAPE)
/// comparisons into existing memory operations.
HANDLE_TARGET_OPCODE(FAULTING_OP)

/// Wraps a machine instruction to add patchability constraints. An
/// instruction wrapped in PATCHABLE_OP has to either have a minimum
/// Precedes a machine instruction to add patchability constraints. An
/// instruction after PATCHABLE_OP has to either have a minimum
/// size or be preceded with a nop of that size. The first operand is
/// an immediate denoting the minimum size of the instruction, the
/// second operand is an immediate denoting the opcode of the original
/// instruction. The rest of the operands are the operands of the
/// original instruction.
/// PATCHABLE_OP can be used as second operand to only insert a nop of
/// required size.
/// an immediate denoting the minimum size of the following instruction.
HANDLE_TARGET_OPCODE(PATCHABLE_OP)

/// This is a marker instruction which gets translated into a nop sled, useful
Expand Down
52 changes: 11 additions & 41 deletions llvm/lib/CodeGen/PatchableFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,58 +38,28 @@ struct PatchableFunction : public MachineFunctionPass {
}

bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
MachineBasicBlock &FirstMBB = *MF.begin();

if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
MachineBasicBlock &FirstMBB = *MF.begin();
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
// The initial .loc covers PATCHABLE_FUNCTION_ENTER.
BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
return true;
}

if (!MF.getFunction().hasFnAttribute("patchable-function"))
return false;

} else if (MF.getFunction().hasFnAttribute("patchable-function")) {
#ifndef NDEBUG
Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
StringRef PatchType = PatchAttr.getValueAsString();
assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
StringRef PatchType = PatchAttr.getValueAsString();
assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
#endif

auto &FirstMBB = *MF.begin();
auto *TII = MF.getSubtarget().getInstrInfo();

MachineBasicBlock::iterator FirstActualI = llvm::find_if(
FirstMBB, [](const MachineInstr &MI) { return !MI.isMetaInstruction(); });

if (FirstActualI == FirstMBB.end()) {
// As of Microsoft documentation on /hotpatch feature, we must ensure that
// "the first instruction of each function is at least two bytes, and no
// jump within the function goes to the first instruction"

// When the first MBB is empty, insert a patchable no-op. This ensures the
// first instruction is patchable in two special cases:
// - the function is empty (e.g. unreachable)
// - the function jumps back to the first instruction, which is in a
// successor MBB.
BuildMI(&FirstMBB, DebugLoc(), TII->get(TargetOpcode::PATCHABLE_OP))
.addImm(2)
.addImm(TargetOpcode::PATCHABLE_OP);
auto *TII = MF.getSubtarget().getInstrInfo();
BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
TII->get(TargetOpcode::PATCHABLE_OP))
.addImm(2);
MF.ensureAlignment(Align(16));
return true;
}

auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
TII->get(TargetOpcode::PATCHABLE_OP))
.addImm(2)
.addImm(FirstActualI->getOpcode());

for (auto &MO : FirstActualI->operands())
MIB.add(MO);

FirstActualI->eraseFromParent();
MF.ensureAlignment(Align(16));
return true;
return false;
}

char PatchableFunction::ID = 0;
Expand Down
37 changes: 15 additions & 22 deletions llvm/lib/Target/X86/X86MCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,24 +948,26 @@ void X86AsmPrinter::LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI) {

void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI,
X86MCInstLower &MCIL) {
// PATCHABLE_OP minsize, opcode, operands
// PATCHABLE_OP minsize

NoAutoPaddingScope NoPadScope(*OutStreamer);

unsigned MinSize = MI.getOperand(0).getImm();
unsigned Opcode = MI.getOperand(1).getImm();
// Opcode PATCHABLE_OP is a special case: there is no instruction to wrap,
// simply emit a nop of size MinSize.
bool EmptyInst = (Opcode == TargetOpcode::PATCHABLE_OP);

MCInst MCI;
MCI.setOpcode(Opcode);
for (auto &MO : drop_begin(MI.operands(), 2))
if (auto MaybeOperand = MCIL.LowerMachineOperand(&MI, MO))
MCI.addOperand(*MaybeOperand);
// Find the next MachineInstr in this MBB.
aganea marked this conversation as resolved.
Show resolved Hide resolved
const MachineInstr *NextMI = MI.getNextNode();
while (NextMI) {
if (!NextMI->isMetaInstruction())
break;
NextMI = NextMI->getNextNode();
}
aganea marked this conversation as resolved.
Show resolved Hide resolved

SmallString<256> Code;
if (!EmptyInst) {
unsigned MinSize = MI.getOperand(0).getImm();

if (NextMI) {
// Lower the next MachineInstr to find its byte size.
sylvain-audi marked this conversation as resolved.
Show resolved Hide resolved
MCInst MCI;
MCIL.Lower(NextMI, MCI);

SmallVector<MCFixup, 4> Fixups;
CodeEmitter->encodeInstruction(MCI, Code, Fixups, getSubtargetInfo());
}
Expand All @@ -981,21 +983,12 @@ void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI,
OutStreamer->emitInstruction(
MCInstBuilder(X86::MOV32rr_REV).addReg(X86::EDI).addReg(X86::EDI),
*Subtarget);
} else if (MinSize == 2 && Opcode == X86::PUSH64r) {
// This is an optimization that lets us get away without emitting a nop in
// many cases.
//
// NB! In some cases the encoding for PUSH64r (e.g. PUSH64r %r9) takes two
// bytes too, so the check on MinSize is important.
MCI.setOpcode(X86::PUSH64rmr);
} else {
unsigned NopSize = emitNop(*OutStreamer, MinSize, Subtarget);
assert(NopSize == MinSize && "Could not implement MinSize!");
(void)NopSize;
}
}
if (!EmptyInst)
OutStreamer->emitInstruction(MCI, getSubtargetInfo());
}

// Lower a stackmap of the form:
Expand Down
55 changes: 55 additions & 0 deletions llvm/test/CodeGen/X86/patchable-prologue-tailcall.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
; RUN: llc -verify-machineinstrs < %s | FileCheck %s --check-prefix=CHECK

; CHECK: "?mi_new_test@@YAPEAX_K@Z":
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: jmp mi_new # TAILCALL

; CHECK: "?builtin_malloc_test@@YAPEAX_K@Z":
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: jmp malloc # TAILCALL

; // Built with: clang-cl.exe /c patchable-prologue-tailcall.cpp /O2 /hotpatch -Xclang -emit-llvm
;
; typedef unsigned long long size_t;
;
; extern "C" {
; void* mi_new(size_t size);
; }
;
; void *mi_new_test(size_t count)
; {
; return mi_new(count);
; }
;
; void *builtin_malloc_test(size_t count)
; {
; return __builtin_malloc(count);
; }

aganea marked this conversation as resolved.
Show resolved Hide resolved
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.38.33133"
aganea marked this conversation as resolved.
Show resolved Hide resolved

define dso_local noundef ptr @"?mi_new_test@@YAPEAX_K@Z"(i64 noundef %count) local_unnamed_addr "patchable-function"="prologue-short-redirect" {
aganea marked this conversation as resolved.
Show resolved Hide resolved
aganea marked this conversation as resolved.
Show resolved Hide resolved
entry:
%call = tail call ptr @mi_new(i64 noundef %count)
ret ptr %call
}

declare dso_local ptr @mi_new(i64 noundef) local_unnamed_addr

define dso_local noalias noundef ptr @"?builtin_malloc_test@@YAPEAX_K@Z"(i64 noundef %count) local_unnamed_addr "patchable-function"="prologue-short-redirect" {
entry:
%call = tail call ptr @malloc(i64 noundef %count)
ret ptr %call
}
aganea marked this conversation as resolved.
Show resolved Hide resolved

declare dso_local noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

attributes #0 = { allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }

!llvm.module.flags = !{!0, !1, !2, !3}

!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"uwtable", i32 2}
!3 = !{i32 1, !"MaxTLSAlign", i32 65536}
4 changes: 3 additions & 1 deletion llvm/test/CodeGen/X86/patchable-prologue.ll
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ define void @f0() "patchable-function"="prologue-short-redirect" {

define void @f1() "patchable-function"="prologue-short-redirect" "frame-pointer"="all" {
; CHECK-LABEL: _f1
; CHECK-NEXT: ff f5 pushq %rbp
; CHECK-NEXT: 66 90 nop
; CHECK-NEXT: 55 pushq %rbp

; CHECK-ALIGN: .p2align 4, 0x90
; CHECK-ALIGN: _f1:
Expand All @@ -47,6 +48,7 @@ define void @f1() "patchable-function"="prologue-short-redirect" "frame-pointer"
; 64: f1:
; 64-NEXT: .seh_proc f1
; 64-NEXT: # %bb.0:
; 64-NEXT: xchgw %ax, %ax
; 64-NEXT: pushq %rbp

ret void
Expand Down