llvm-project/clang/test/CodeGen/patchable-function-entry.c
Alexandre Ganea 5af2433e17 [clang-cl] Support the /HOTPATCH flag
This patch adds support for the MSVC /HOTPATCH flag: https://docs.microsoft.com/sv-se/cpp/build/reference/hotpatch-create-hotpatchable-image?view=msvc-170&viewFallbackFrom=vs-2019

The flag is translated to a new -fms-hotpatch flag, which in turn adds a 'patchable-function' attribute for each function in the TU. This is then picked up by the PatchableFunction pass which would generate a TargetOpcode::PATCHABLE_OP of minsize = 2 (which means the target instruction must resolve to at least two bytes). TargetOpcode::PATCHABLE_OP is only implemented for x86/x64. When targetting ARM/ARM64, /HOTPATCH isn't required (instructions are always 2/4 bytes and suitable for hotpatching).

Additionally, when using /Z7, we generate a 'hot patchable' flag in the CodeView debug stream, in the S_COMPILE3 record. This flag is then picked up by LLD (or link.exe) and is used in conjunction with the linker /FUNCTIONPADMIN flag to generate extra space before each function, to accommodate for live patching long jumps. Please see: d703b92296/lld/COFF/Writer.cpp (L1298)

The outcome is that we can finally use Live++ or Recode along with clang-cl.

NOTE: It seems that MSVC cl.exe always enables /HOTPATCH on x64 by default, although if we did the same I thought we might generate sub-optimal code (if this flag was active by default). Additionally, MSVC always generates a .debug$S section and a S_COMPILE3 record, which Clang doesn't do without /Z7. Therefore, the following MSVC command-line "cl /c file.cpp" would have to be written with Clang such as "clang-cl /c file.cpp /HOTPATCH /Z7" in order to obtain the same result.

Depends on D43002, D80833 and D81301 for the full feature.

Differential Revision: https://reviews.llvm.org/D116511
2022-01-20 12:57:19 -05:00

42 lines
1.8 KiB
C

// RUN: %clang_cc1 -triple aarch64 -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -fpatchable-function-entry=1 -o - | FileCheck --check-prefixes=CHECK,OPT %s
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -fms-hotpatch -o - | FileCheck --check-prefixes=HOTPATCH %s
// CHECK: define{{.*}} void @f0() #0
__attribute__((patchable_function_entry(0))) void f0() {}
// CHECK: define{{.*}} void @f00() #0
__attribute__((patchable_function_entry(0, 0))) void f00() {}
// CHECK: define{{.*}} void @f2() #1
__attribute__((patchable_function_entry(2))) void f2() {}
// CHECK: define{{.*}} void @f20() #1
__attribute__((patchable_function_entry(2, 0))) void f20() {}
// CHECK: define{{.*}} void @f20decl() #1
__attribute__((patchable_function_entry(2, 0))) void f20decl();
void f20decl() {}
// CHECK: define{{.*}} void @f44() #2
__attribute__((patchable_function_entry(4, 4))) void f44() {}
// CHECK: define{{.*}} void @f52() #3
__attribute__((patchable_function_entry(5, 2))) void f52() {}
// OPT: define{{.*}} void @f() #4
void f() {}
/// No need to emit "patchable-function-entry"="0"
// CHECK: attributes #0 = { {{.*}}
// CHECK-NOT: "patchable-function-entry"
// CHECK: attributes #1 = { {{.*}} "patchable-function-entry"="2"
// CHECK: attributes #2 = { {{.*}} "patchable-function-entry"="0" "patchable-function-prefix"="4"
// CHECK: attributes #3 = { {{.*}} "patchable-function-entry"="3" "patchable-function-prefix"="2"
// OPT: attributes #4 = { {{.*}} "patchable-function-entry"="1"
// HOTPATCH: attributes #0 = { {{.*}} "patchable-function"="prologue-short-redirect"
// HOTPATCH: attributes #1 = { {{.*}} "patchable-function"="prologue-short-redirect"
// HOTPATCH: attributes #2 = { {{.*}} "patchable-function"="prologue-short-redirect"
// HOTPATCH: attributes #3 = { {{.*}} "patchable-function"="prologue-short-redirect"