Author: Paul Murphy Date: 2026-10-22T09:27:19+02:00 New Revision: f514b16972f785fdd3fad1d19195aad5c9324fb5
URL: https://github.com/llvm/llvm-project/commit/f514b16972f785fdd3fad1d19195aad5c9324fb5 DIFF: https://github.com/llvm/llvm-project/commit/f514b16972f785fdd3fad1d19195aad5c9324fb5.diff LOG: [X86] Support -mnop-mcount and -mrecord-mcount on x86 elf targets (#184641) Support mnop-mcount and mrecord-mcount attributes when used with the fentry-call=true attribute. Similar to SystemZ, use the mnop-mcount attribute to place a nop instead of a call to __fentry__. Likewise, use the mrecord-mcount attribute to record the location of the call to fentry (or nop if used with the above) to a section named __mcount_loc. This enables the use of -mnop-mcount -mrecord-mcount for x86 in Clang. Added: llvm/test/CodeGen/X86/mnop-mcount-01.ll llvm/test/CodeGen/X86/mrecord-mcount-01.ll llvm/test/Verifier/mnop-mcount.ll llvm/test/Verifier/mrecord-mcount.ll Modified: clang/lib/Driver/ToolChains/Clang.cpp clang/test/CodeGen/mnop-mcount.c clang/test/CodeGen/mrecord-mcount.c clang/test/Driver/mcount.c llvm/lib/IR/Verifier.cpp llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp llvm/lib/Target/X86/X86MCInstLower.cpp Removed: llvm/test/CodeGen/SystemZ/mnop-mcount-02.ll llvm/test/CodeGen/SystemZ/mrecord-mcount-02.ll ################################################################################ diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index ffb225be471800..454673dd20f9ad 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7326,14 +7326,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, << A->getAsString(Args) << TripleStr; } if (Arg *A = Args.getLastArg(options::OPT_mnop_mcount)) { - if (Arch == llvm::Triple::systemz) + if (Arch == llvm::Triple::systemz || + (TC.getTriple().isX86() && TC.getTriple().isOSBinFormatELF())) A->render(Args, CmdArgs); else D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args) << TripleStr; } if (Arg *A = Args.getLastArg(options::OPT_mrecord_mcount)) { - if (Arch == llvm::Triple::systemz) + if (Arch == llvm::Triple::systemz || + (TC.getTriple().isX86() && TC.getTriple().isOSBinFormatELF())) A->render(Args, CmdArgs); else D.Diag(diag::err_drv_unsupported_opt_for_target) diff --git a/clang/test/CodeGen/mnop-mcount.c b/clang/test/CodeGen/mnop-mcount.c index ebb21cc560e370..9455043b0d4e33 100644 --- a/clang/test/CodeGen/mnop-mcount.c +++ b/clang/test/CodeGen/mnop-mcount.c @@ -6,6 +6,22 @@ // RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s // RUN: %clang_cc1 -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s \ // RUN: 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -pg -mfentry -mnop-mcount -triple x86_64-unknown-linux -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s +// RUN: not %clang_cc1 -pg -mnop-mcount -triple x86_64-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s +// RUN: %clang_cc1 -mfentry -mnop-mcount -triple x86_64-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -mnop-mcount -triple x86_64-unknown-linux -emit-llvm -o - %s \ +// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -pg -mfentry -mnop-mcount -triple i686-unknown-linux -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s +// RUN: not %clang_cc1 -pg -mnop-mcount -triple i686-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s +// RUN: %clang_cc1 -mfentry -mnop-mcount -triple i686-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -mnop-mcount -triple i686-unknown-linux -emit-llvm -o - %s \ +// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s int foo(void) { return 0; diff --git a/clang/test/CodeGen/mrecord-mcount.c b/clang/test/CodeGen/mrecord-mcount.c index 292a807073cefa..f00b4594f5d8ec 100644 --- a/clang/test/CodeGen/mrecord-mcount.c +++ b/clang/test/CodeGen/mrecord-mcount.c @@ -6,6 +6,22 @@ // RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s // RUN: %clang_cc1 -mrecord-mcount -triple s390x-ibm-linux -emit-llvm -o - %s \ // RUN: 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -pg -mfentry -mrecord-mcount -triple x86_64-unknown-linux -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s +// RUN: not %clang_cc1 -pg -mrecord-mcount -triple x86_64-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s +// RUN: %clang_cc1 -mfentry -mrecord-mcount -triple x86_64-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -mrecord-mcount -triple x86_64-unknown-linux -emit-llvm -o - %s \ +// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -pg -mfentry -mrecord-mcount -triple i686-unknown-linux -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s +// RUN: not %clang_cc1 -pg -mrecord-mcount -triple i686-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s +// RUN: %clang_cc1 -mfentry -mrecord-mcount -triple i686-unknown-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -mrecord-mcount -triple i686-unknown-linux -emit-llvm -o - %s \ +// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s int foo(void) { return 0; diff --git a/clang/test/Driver/mcount.c b/clang/test/Driver/mcount.c index 70f554745b9c30..d50055f90344e0 100644 --- a/clang/test/Driver/mcount.c +++ b/clang/test/Driver/mcount.c @@ -1,12 +1,11 @@ // RUN: %clang --target=s390x -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck %s +// RUN: %clang --target=x86_64 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck %s +// RUN: %clang --target=i686 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck %s // CHECK: "-mnop-mcount" // CHECK: "-mrecord-mcount" -// RUN: not %clang --target=x86_64 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR1 %s // RUN: not %clang --target=aarch64 -c -### %s -mnop-mcount -mrecord-mcount 2>&1 | FileCheck --check-prefix=ERR2 %s -// ERR1: error: unsupported option '-mnop-mcount' for target 'x86_64' -// ERR1: error: unsupported option '-mrecord-mcount' for target 'x86_64' // ERR2: error: unsupported option '-mnop-mcount' for target 'aarch64' // ERR2: error: unsupported option '-mrecord-mcount' for target 'aarch64' diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index e3d9cc8d7e6fdf..b78e1453f1dba4 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -2832,6 +2832,18 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs, Check(!(Attrs.hasFnAttr(Attribute::Hot) && Attrs.hasFnAttr(Attribute::Cold)), "Attributes 'hot' and 'cold' are incompatible"); + + if (Attrs.hasFnAttr("mnop-mcount")) { + auto A = Attrs.getFnAttr("fentry-call"); + Check(A.isValid() && A.getValueAsString() == "true", + "mnop-mcount only supported with fentry-call"); + } + + if (Attrs.hasFnAttr("mrecord-mcount")) { + auto A = Attrs.getFnAttr("fentry-call"); + Check(A.isValid() && A.getValueAsString() == "true", + "mrecord-mcount only supported with fentry-call"); + } } void Verifier::verifyUnknownProfileMetadata(MDNode *MD) { diff --git a/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp b/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp index 9ab0a4a7f23689..86fa6985a53ce6 100644 --- a/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp +++ b/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp @@ -373,12 +373,6 @@ class SystemZDAGToDAGISel : public SelectionDAGISel { bool runOnMachineFunction(MachineFunction &MF) override { const Function &F = MF.getFunction(); - if (F.getFnAttribute("fentry-call").getValueAsString() != "true") { - if (F.hasFnAttribute("mnop-mcount")) - report_fatal_error("mnop-mcount only supported with fentry-call"); - if (F.hasFnAttribute("mrecord-mcount")) - report_fatal_error("mrecord-mcount only supported with fentry-call"); - } if (F.getParent()->getStackProtectorGuard() != "global") { if (F.getParent()->hasStackProtectorGuardRecord()) report_fatal_error("mstack-protector-guard-record only supported with " diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp index 7a71b2746e7980..54fb24e7a06f33 100644 --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -26,6 +26,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/BinaryFormat/ELF.h" #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" #include "llvm/CodeGen/MachineConstantPool.h" #include "llvm/CodeGen/MachineFunction.h" @@ -44,6 +45,7 @@ #include "llvm/MC/MCInst.h" #include "llvm/MC/MCInstBuilder.h" #include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/TargetRegistry.h" @@ -661,9 +663,8 @@ static unsigned emitNop(MCStreamer &OS, unsigned NumBytes, // target cpu. 15-bytes is the longest single NOP instruction, but some // platforms can't decode the longest forms efficiently. unsigned MaxNopLength = 1; + unsigned BaseReg = X86::RAX; if (Subtarget->is64Bit()) { - // FIXME: We can use NOOPL on 32-bit targets with FeatureNOPL, but the - // IndexReg/BaseReg below need to be updated. if (Subtarget->hasFeature(X86::TuningFast7ByteNOP)) MaxNopLength = 7; else if (Subtarget->hasFeature(X86::TuningFast15ByteNOP)) @@ -672,16 +673,18 @@ static unsigned emitNop(MCStreamer &OS, unsigned NumBytes, MaxNopLength = 11; else MaxNopLength = 10; - } if (Subtarget->is32Bit()) + } else if (Subtarget->is32Bit() && Subtarget->hasFeature(X86::FeatureNOPL)) { + BaseReg = X86::EAX; + MaxNopLength = 10; + } else if (Subtarget->is32Bit()) MaxNopLength = 2; // Cap a single nop emission at the profitable value for the target NumBytes = std::min(NumBytes, MaxNopLength); unsigned NopSize; - unsigned Opc, BaseReg, ScaleVal, IndexReg, Displacement, SegmentReg; + unsigned Opc, ScaleVal, IndexReg, Displacement, SegmentReg; IndexReg = Displacement = SegmentReg = 0; - BaseReg = X86::RAX; ScaleVal = 1; switch (NumBytes) { case 0: @@ -708,13 +711,13 @@ static unsigned emitNop(MCStreamer &OS, unsigned NumBytes, NopSize = 5; Opc = X86::NOOPL; Displacement = 8; - IndexReg = X86::RAX; + IndexReg = BaseReg; break; case 6: NopSize = 6; Opc = X86::NOOPW; Displacement = 8; - IndexReg = X86::RAX; + IndexReg = BaseReg; break; case 7: NopSize = 7; @@ -725,19 +728,19 @@ static unsigned emitNop(MCStreamer &OS, unsigned NumBytes, NopSize = 8; Opc = X86::NOOPL; Displacement = 512; - IndexReg = X86::RAX; + IndexReg = BaseReg; break; case 9: NopSize = 9; Opc = X86::NOOPW; Displacement = 512; - IndexReg = X86::RAX; + IndexReg = BaseReg; break; default: NopSize = 10; Opc = X86::NOOPW; Displacement = 512; - IndexReg = X86::RAX; + IndexReg = BaseReg; SegmentReg = X86::CS; break; } @@ -884,6 +887,24 @@ void X86AsmPrinter::LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL) { bool Is64Bits = Subtarget->is64Bit(); MCContext &Ctx = OutStreamer->getContext(); + + if (MF->getFunction().hasFnAttribute("mrecord-mcount")) { + MCSymbol *DotSym = OutContext.createTempSymbol(); + OutStreamer->pushSection(); + OutStreamer->switchSection( + Ctx.getELFSection("__mcount_loc", ELF::SHT_PROGBITS, ELF::SHF_ALLOC)); + OutStreamer->emitSymbolValue(DotSym, Is64Bits ? 8 : 4); + OutStreamer->popSection(); + OutStreamer->emitLabel(DotSym); + } + + if (MF->getFunction().hasFnAttribute("mnop-mcount")) { + // Insert a NOP stream which can be rewritten into a 5-byte CALL + // instruction at runtime. + emitX86Nops(*OutStreamer, 5, Subtarget); + return; + } + MCSymbol *fentry = Ctx.getOrCreateSymbol("__fentry__"); const MCSymbolRefExpr *Op = MCSymbolRefExpr::create(fentry, Ctx); diff --git a/llvm/test/CodeGen/SystemZ/mnop-mcount-02.ll b/llvm/test/CodeGen/SystemZ/mnop-mcount-02.ll deleted file mode 100644 index d6bb1ae7150aae..00000000000000 --- a/llvm/test/CodeGen/SystemZ/mnop-mcount-02.ll +++ /dev/null @@ -1,10 +0,0 @@ -; RUN: not --crash llc %s -mtriple=s390x-linux-gnu -o - 2>&1 | FileCheck %s -; -; CHECK: LLVM ERROR: mnop-mcount only supported with fentry-call - -define void @test1() #0 { -entry: - ret void -} - -attributes #0 = { "instrument-function-entry-inlined"="mcount" "mnop-mcount" } diff --git a/llvm/test/CodeGen/SystemZ/mrecord-mcount-02.ll b/llvm/test/CodeGen/SystemZ/mrecord-mcount-02.ll deleted file mode 100644 index e1f4a3748b76e2..00000000000000 --- a/llvm/test/CodeGen/SystemZ/mrecord-mcount-02.ll +++ /dev/null @@ -1,10 +0,0 @@ -; RUN: not --crash llc %s -mtriple=s390x-linux-gnu -o - 2>&1 | FileCheck %s -; -; CHECK: LLVM ERROR: mrecord-mcount only supported with fentry-call - -define void @test1() #0 { -entry: - ret void -} - -attributes #0 = { "instrument-function-entry-inlined"="mcount" "mrecord-mcount" } diff --git a/llvm/test/CodeGen/X86/mnop-mcount-01.ll b/llvm/test/CodeGen/X86/mnop-mcount-01.ll new file mode 100644 index 00000000000000..a6e25e74c7a023 --- /dev/null +++ b/llvm/test/CodeGen/X86/mnop-mcount-01.ll @@ -0,0 +1,50 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 +; RUN: llc %s -mtriple=x86_64-linux-gnu -o - | FileCheck --check-prefixes=X64 %s +; RUN: llc %s -mtriple=i686-linux-gnu -o - | FileCheck --check-prefix=X86 %s +; RUN: llc %s -mtriple=i686-linux-gnu -mcpu=pentium_pro -o - | FileCheck --check-prefix=PPRO %s + +define void @test1() #0 { +; X64-LABEL: test1: +; X64: # %bb.0: # %entry +; X64-NEXT: callq __fentry__ +; X64-NEXT: retq +; +; X86-LABEL: test1: +; X86: # %bb.0: # %entry +; X86-NEXT: calll __fentry__ +; X86-NEXT: retl +; +; PPRO-LABEL: test1: +; PPRO: # %bb.0: # %entry +; PPRO-NEXT: calll __fentry__ +; PPRO-NEXT: retl +entry: + ret void + +} + +define void @test2() #1 { +; X64-LABEL: test2: +; X64: # %bb.0: # %entry +; X64-NEXT: nopl 8(%rax,%rax) +; X64-NEXT: retq +; +; X86-LABEL: test2: +; X86: # %bb.0: # %entry +; X86-NEXT: xchgw %ax, %ax +; X86-NEXT: xchgw %ax, %ax +; X86-NEXT: nop +; X86-NEXT: retl +; +; PPRO-LABEL: test2: +; PPRO: # %bb.0: # %entry +; PPRO-NEXT: nopl 8(%eax,%eax) +; PPRO-NEXT: retl +entry: + ret void + +} + +attributes #0 = { "fentry-call"="true" } +attributes #1 = { "fentry-call"="true" "mnop-mcount" } + diff --git a/llvm/test/CodeGen/X86/mrecord-mcount-01.ll b/llvm/test/CodeGen/X86/mrecord-mcount-01.ll new file mode 100644 index 00000000000000..90dd75f217a43f --- /dev/null +++ b/llvm/test/CodeGen/X86/mrecord-mcount-01.ll @@ -0,0 +1,65 @@ +; RUN: llc %s -mtriple=x86_64-linux-gnu -o - | FileCheck --check-prefix=X64 %s +; RUN: llc %s -mtriple=i686-linux-gnu -o - | FileCheck --check-prefix=X86 %s +; RUN: llc %s -mtriple=i686-linux-gnu -mcpu=pentium_pro -o - | FileCheck --check-prefix=PPRO %s + +define void @test1() #0 { +entry: + ret void + +; X64-LABEL: test1: +; X64: .section __mcount_loc,"a",@progbits +; X64: .quad .Ltmp0 +; X64: .text +; X64: .Ltmp0: +; X64: callq __fentry__ +; X64: retq +; X86-LABEL: test1: +; X86: .section __mcount_loc,"a",@progbits +; X86: .long .Ltmp0 +; X86: .text +; X86: .Ltmp0: +; X86: calll __fentry__ +; X86: retl +; PPRO-LABEL: test1: +; PPRO: .section __mcount_loc,"a",@progbits +; PPRO: .long .Ltmp0 +; PPRO: .text +; PPRO: .Ltmp0: +; PPRO: calll __fentry__ +; PPRO: retl +} + +define void @test2() #1 { +entry: + ret void + +; X64-LABEL: test2: +; X64: .section __mcount_loc,"a",@progbits +; X64: .quad .Ltmp1 +; X64: .text +; X64: .Ltmp1: +; X64: nopl 8(%rax,%rax) +; X64-NOT: callq __fentry__ +; X64: retq +; X86-LABEL: test2: +; X86: .section __mcount_loc,"a",@progbits +; X86: .long .Ltmp1 +; X86: .text +; X86: .Ltmp1: +; X86: xchgw %ax, %ax +; X86: xchgw %ax, %ax +; X86: nop +; X86-NOT: calll __fentry__ +; X86: retl +; PPRO-LABEL: test2: +; PPRO: .section __mcount_loc,"a",@progbits +; PPRO: .long .Ltmp1 +; PPRO: .text +; PPRO: .Ltmp1: +; PPRO: nopl 8(%eax,%eax) +; PPRO-NOT: calll __fentry__ +; PPRO: retl +} + +attributes #0 = { "fentry-call"="true" "mrecord-mcount" } +attributes #1 = { "fentry-call"="true" "mnop-mcount" "mrecord-mcount" } diff --git a/llvm/test/Verifier/mnop-mcount.ll b/llvm/test/Verifier/mnop-mcount.ll new file mode 100644 index 00000000000000..222a8896fa0636 --- /dev/null +++ b/llvm/test/Verifier/mnop-mcount.ll @@ -0,0 +1,10 @@ +; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s +; +; CHECK: mnop-mcount only supported with fentry-call + +define void @test1() #0 { +entry: + ret void +} + +attributes #0 = { "instrument-function-entry-inlined"="mcount" "mnop-mcount" } diff --git a/llvm/test/Verifier/mrecord-mcount.ll b/llvm/test/Verifier/mrecord-mcount.ll new file mode 100644 index 00000000000000..abb69f9427dac3 --- /dev/null +++ b/llvm/test/Verifier/mrecord-mcount.ll @@ -0,0 +1,10 @@ +; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s +; +; CHECK: mrecord-mcount only supported with fentry-call + +define void @test1() #0 { +entry: + ret void +} + +attributes #0 = { "instrument-function-entry-inlined"="mcount" "mrecord-mcount" } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
