Skip to content

Commit 69bf40c

Browse files
committed
[Driver][CodeGen] Support -fpatchable-function-entry=N,M and __attribute__((patchable_function_entry(N,M))) where M>0
Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D73072
1 parent 01da05b commit 69bf40c

File tree

12 files changed

+49
-25
lines changed

12 files changed

+49
-25
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,8 +3998,6 @@ def PatchableFunctionEntryDocs : Documentation {
39983998
before the function entry and N-M NOPs after the function entry. This attribute
39993999
takes precedence over the command line option ``-fpatchable-function-entry=N,M``.
40004000
``M`` defaults to 0 if omitted.
4001-
4002-
Currently, only M=0 is supported.
40034001
}];
40044002
}
40054003

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ CODEGENOPT(XRayIgnoreLoops , 1, 0)
115115
VALUE_CODEGENOPT(XRayInstructionThreshold , 32, 200)
116116

117117
VALUE_CODEGENOPT(PatchableFunctionEntryCount , 32, 0) ///< Number of NOPs at function entry
118+
VALUE_CODEGENOPT(PatchableFunctionEntryOffset , 32, 0)
118119

119120
CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled.
120121
CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled.

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def err_drv_unsupported_indirect_jump_opt : Error<
408408
def err_drv_unknown_indirect_jump_opt : Error<
409409
"unknown '-mindirect-jump=' option '%0'">;
410410
def err_drv_unsupported_fpatchable_function_entry_argument : Error<
411-
"the second argument of '-fpatchable-function-entry' must be 0 or omitted">;
411+
"the second argument of '-fpatchable-function-entry' must be smaller than the first argument">;
412412

413413
def warn_drv_unable_to_find_directory_expected : Warning<
414414
"unable to find %0 directory, expected to be in '%1'">,

clang/include/clang/Driver/CC1Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ def fsanitize_coverage_no_prune
372372
def fsanitize_coverage_stack_depth
373373
: Flag<["-"], "fsanitize-coverage-stack-depth">,
374374
HelpText<"Enable max stack depth tracing">;
375+
def fpatchable_function_entry_offset_EQ
376+
: Joined<["-"], "fpatchable-function-entry-offset=">, MetaVarName<"<M>">,
377+
HelpText<"Generate M NOPs before function entry">;
375378
def fprofile_instrument_EQ : Joined<["-"], "fprofile-instrument=">,
376379
HelpText<"Enable PGO instrumentation. The accepted value is clang, llvm, "
377380
"or none">, Values<"none,clang,llvm">;

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ def fno_max_type_align : Flag<["-"], "fno-max-type-align">, Group<f_Group>;
17141714
def fpascal_strings : Flag<["-"], "fpascal-strings">, Group<f_Group>, Flags<[CC1Option]>,
17151715
HelpText<"Recognize and construct Pascal-style string literals">;
17161716
def fpatchable_function_entry_EQ : Joined<["-"], "fpatchable-function-entry=">, Group<f_Group>, Flags<[CC1Option]>,
1717-
HelpText<"Generate N NOPs at function entry">;
1717+
MetaVarName<"<N,M>">, HelpText<"Generate M NOPs before function entry and N-M NOPs after function entry">;
17181718
def fpcc_struct_return : Flag<["-"], "fpcc-struct-return">, Group<f_Group>, Flags<[CC1Option]>,
17191719
HelpText<"Override the default ABI to return all structs on the stack">;
17201720
def fpch_preprocess : Flag<["-"], "fpch-preprocess">, Group<f_Group>;

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,13 +833,18 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
833833
}
834834
}
835835

836+
unsigned Count, Offset;
836837
if (const auto *Attr = D->getAttr<PatchableFunctionEntryAttr>()) {
837-
// Attr->getStart is currently ignored.
838-
Fn->addFnAttr("patchable-function-entry",
839-
std::to_string(Attr->getCount()));
840-
} else if (unsigned Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount) {
841-
Fn->addFnAttr("patchable-function-entry",
842-
std::to_string(Count));
838+
Count = Attr->getCount();
839+
Offset = Attr->getOffset();
840+
} else {
841+
Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount;
842+
Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset;
843+
}
844+
if (Count && Offset <= Count) {
845+
Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset));
846+
if (Offset)
847+
Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset));
843848
}
844849
}
845850

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,20 +5111,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51115111

51125112
if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) {
51135113
StringRef S0 = A->getValue(), S = S0;
5114-
unsigned Size, Start = 0;
5114+
unsigned Size, Offset = 0;
51155115
if (!Triple.isAArch64() && Triple.getArch() != llvm::Triple::x86 &&
51165116
Triple.getArch() != llvm::Triple::x86_64)
51175117
D.Diag(diag::err_drv_unsupported_opt_for_target)
51185118
<< A->getAsString(Args) << TripleStr;
51195119
else if (S.consumeInteger(10, Size) ||
51205120
(!S.empty() && (!S.consume_front(",") ||
5121-
S.consumeInteger(10, Start) || !S.empty())))
5121+
S.consumeInteger(10, Offset) || !S.empty())))
51225122
D.Diag(diag::err_drv_invalid_argument_to_option)
51235123
<< S0 << A->getOption().getName();
5124-
else if (Start)
5124+
else if (Size < Offset)
51255125
D.Diag(diag::err_drv_unsupported_fpatchable_function_entry_argument);
5126-
else
5126+
else {
51275127
CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size)));
5128+
CmdArgs.push_back(Args.MakeArgString(
5129+
"-fpatchable-function-entry-offset=" + Twine(Offset)));
5130+
}
51285131
}
51295132

51305133
if (TC.SupportsProfiling()) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,8 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
11021102

11031103
Opts.PatchableFunctionEntryCount =
11041104
getLastArgIntValue(Args, OPT_fpatchable_function_entry_EQ, 0, Diags);
1105+
Opts.PatchableFunctionEntryOffset = getLastArgIntValue(
1106+
Args, OPT_fpatchable_function_entry_offset_EQ, 0, Diags);
11051107
Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
11061108
Opts.CallFEntry = Args.hasArg(OPT_mfentry);
11071109
Opts.MNopMCount = Args.hasArg(OPT_mnop_mcount);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,9 +4925,9 @@ static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D,
49254925
Expr *Arg = AL.getArgAsExpr(1);
49264926
if (!checkUInt32Argument(S, AL, Arg, Offset, 1, true))
49274927
return;
4928-
if (Offset) {
4928+
if (Count < Offset) {
49294929
S.Diag(getAttrLoc(AL), diag::err_attribute_argument_out_of_range)
4930-
<< &AL << 0 << 0 << Arg->getBeginLoc();
4930+
<< &AL << 0 << Count << Arg->getBeginLoc();
49314931
return;
49324932
}
49334933
}

clang/test/CodeGen/patchable-function-entry.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ __attribute__((patchable_function_entry(2, 0))) void f20() {}
1717
__attribute__((patchable_function_entry(2, 0))) void f20decl();
1818
void f20decl() {}
1919

20-
// OPT: define void @f() #2
20+
// CHECK: define void @f44() #2
21+
__attribute__((patchable_function_entry(4, 4))) void f44() {}
22+
23+
// CHECK: define void @f52() #3
24+
__attribute__((patchable_function_entry(5, 2))) void f52() {}
25+
26+
// OPT: define void @f() #4
2127
void f() {}
2228

23-
/// M in patchable_function_entry(N,M) is currently ignored.
24-
// CHECK: attributes #0 = { {{.*}} "patchable-function-entry"="0"
29+
/// No need to emit "patchable-function-entry"="0"
30+
// CHECK: attributes #0 = { {{.*}}
31+
// CHECK-NOT: "patchable-function-entry"
32+
2533
// CHECK: attributes #1 = { {{.*}} "patchable-function-entry"="2"
26-
// OPT: attributes #2 = { {{.*}} "patchable-function-entry"="1"
34+
// CHECK: attributes #2 = { {{.*}} "patchable-function-entry"="0" "patchable-function-prefix"="4"
35+
// CHECK: attributes #3 = { {{.*}} "patchable-function-entry"="3" "patchable-function-prefix"="2"
36+
// OPT: attributes #4 = { {{.*}} "patchable-function-entry"="1"

clang/test/Driver/fpatchable-function-entry.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
// RUN: %clang -target aarch64 %s -fpatchable-function-entry=1,0 -c -### 2>&1 | FileCheck %s
55
// CHECK: "-fpatchable-function-entry=1"
66

7+
// RUN: %clang -target aarch64 -fsyntax-only %s -fpatchable-function-entry=1,1 -c -### 2>&1 | FileCheck --check-prefix=11 %s
8+
// 11: "-fpatchable-function-entry=1" "-fpatchable-function-entry-offset=1"
9+
// RUN: %clang -target aarch64 -fsyntax-only %s -fpatchable-function-entry=2,1 -c -### 2>&1 | FileCheck --check-prefix=21 %s
10+
// 21: "-fpatchable-function-entry=2" "-fpatchable-function-entry-offset=1"
11+
712
// RUN: not %clang -target ppc64 -fsyntax-only %s -fpatchable-function-entry=1 2>&1 | FileCheck --check-prefix=TARGET %s
813
// TARGET: error: unsupported option '-fpatchable-function-entry=1' for target 'ppc64'
914

10-
// RUN: not %clang -target i386 -fsyntax-only %s -fpatchable-function-entry=1,1 2>&1 | FileCheck --check-prefix=NONZERO %s
11-
// NONZERO: error: the second argument of '-fpatchable-function-entry' must be 0 or omitted
12-
1315
// RUN: not %clang -target x86_64 -fsyntax-only %s -fpatchable-function-entry=1,0, 2>&1 | FileCheck --check-prefix=EXCESS %s
1416
// EXCESS: error: invalid argument '1,0,' to -fpatchable-function-entry=
1517

clang/test/Sema/patchable-function-entry-attr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ int i;
1313
// expected-error@+1 {{'patchable_function_entry' attribute requires parameter 0 to be an integer constant}}
1414
__attribute__((patchable_function_entry(i))) void f();
1515

16-
// expected-error@+1 {{'patchable_function_entry' attribute requires integer constant between 0 and 0 inclusive}}
17-
__attribute__((patchable_function_entry(1, 1))) void f();
16+
// expected-error@+1 {{'patchable_function_entry' attribute requires integer constant between 0 and 2 inclusive}}
17+
__attribute__((patchable_function_entry(2, 3))) void f();

0 commit comments

Comments
 (0)