Skip to content

Commit 9376714

Browse files
committed
[Clang FE] Recognize -mnop-mcount CL option (SystemZ only).
Recognize -mnop-mcount from the command line and add a function attribute "mnop-mcount"="true" when passed. When this option is used, a nop is added instead of a call to fentry. This is used when building the Linux Kernel. If this option is passed for any other target than SystemZ, an error is generated. Review: Ulrich Weigand https://reviews.llvm.org/D67763
1 parent 646896a commit 9376714

File tree

7 files changed

+45
-0
lines changed

7 files changed

+45
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ VALUE_CODEGENOPT(XRayInstructionThreshold , 32, 200)
112112

113113
CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled.
114114
CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled.
115+
CODEGENOPT(MNopMCount , 1, 0) ///< Set when -mnop-mcount is enabled.
115116
CODEGENOPT(LessPreciseFPMAD , 1, 0) ///< Enable less precise MAD instructions to
116117
///< be generated.
117118
CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto is enabled on the

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def err_target_unsupported_mcmse : Error<
271271
"-mcmse is not supported for %0">;
272272
def err_opt_not_valid_with_opt : Error<
273273
"option '%0' cannot be specified with '%1'">;
274+
def err_opt_not_valid_without_opt : Error<
275+
"option '%0' cannot be specified without '%1'">;
274276
def err_opt_not_valid_on_target : Error<
275277
"option '%0' cannot be specified on this target">;
276278

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,8 @@ def mpie_copy_relocations : Flag<["-"], "mpie-copy-relocations">, Group<m_Group>
24482448
def mno_pie_copy_relocations : Flag<["-"], "mno-pie-copy-relocations">, Group<m_Group>;
24492449
def mfentry : Flag<["-"], "mfentry">, HelpText<"Insert calls to fentry at function entry (x86/SystemZ only)">,
24502450
Flags<[CC1Option]>, Group<m_Group>;
2451+
def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
2452+
Flags<[CC1Option]>, Group<m_Group>;
24512453
def mips16 : Flag<["-"], "mips16">, Group<m_mips_Features_Group>;
24522454
def mno_mips16 : Flag<["-"], "mno-mips16">, Group<m_mips_Features_Group>;
24532455
def mmicromips : Flag<["-"], "mmicromips">, Group<m_mips_Features_Group>;

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,16 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
893893
Fn->addFnAttr("instrument-function-entry-inlined",
894894
getTarget().getMCountName());
895895
}
896+
if (CGM.getCodeGenOpts().MNopMCount) {
897+
if (getContext().getTargetInfo().getTriple().getArch() !=
898+
llvm::Triple::systemz)
899+
CGM.getDiags().Report(diag::err_opt_not_valid_on_target)
900+
<< "-mnop-mcount";
901+
if (!CGM.getCodeGenOpts().CallFEntry)
902+
CGM.getDiags().Report(diag::err_opt_not_valid_without_opt)
903+
<< "-mnop-mcount" << "-mfentry";
904+
Fn->addFnAttr("mnop-mcount", "true");
905+
}
896906
}
897907
}
898908

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,6 +4616,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
46164616
if (TC.SupportsProfiling())
46174617
Args.AddLastArg(CmdArgs, options::OPT_mfentry);
46184618

4619+
if (TC.SupportsProfiling())
4620+
Args.AddLastArg(CmdArgs, options::OPT_mnop_mcount);
4621+
46194622
if (Args.getLastArg(options::OPT_fapple_kext) ||
46204623
(Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
46214624
CmdArgs.push_back("-fapple-kext");

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
11031103

11041104
Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
11051105
Opts.CallFEntry = Args.hasArg(OPT_mfentry);
1106+
Opts.MNopMCount = Args.hasArg(OPT_mnop_mcount);
11061107
Opts.EmitOpenCLArgMetadata = Args.hasArg(OPT_cl_kernel_arg_info);
11071108

11081109
if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) {

clang/test/CodeGen/mnop-mcount.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -pg -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm \
2+
// RUN: -o - %s 2>&1 | FileCheck %s
3+
// RUN: not %clang_cc1 -pg -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - \
4+
// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s
5+
// RUN: %clang_cc1 -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - \
6+
// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s
7+
// RUN: %clang_cc1 -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s \
8+
// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s
9+
// RUN: not %clang_cc1 -pg -mfentry -mnop-mcount -triple x86_64-linux-gnu \
10+
// RUN: -emit-llvm -o - %s 2>&1 | FileCheck -check-prefix=X86 %s
11+
12+
int foo(void) {
13+
return 0;
14+
}
15+
16+
int __attribute__((no_instrument_function)) no_instrument(void) {
17+
return foo();
18+
}
19+
20+
//CHECK: attributes #0 = { {{.*}}"mnop-mcount"="true"{{.*}} }
21+
//CHECK: attributes #1 = { {{.*}} }
22+
//CHECK-NOT: attributes #1 = { {{.*}}"mnop-mcount"="true"{{.*}} }
23+
//NOMFENTRY: error: option '-mnop-mcount' cannot be specified without '-mfentry'
24+
//NOPG-NOT: attributes #0 = { {{.*}}"mnop-mcount"{{.*}} }
25+
//NOPG-NOT: attributes #1 = { {{.*}}"mnop-mcount"{{.*}} }
26+
//X86: error: option '-mnop-mcount' cannot be specified on this target

0 commit comments

Comments
 (0)