[llvm] r352227 - [MC] Teach the MachO object writer about N_FUNC_COLD
Vedant Kumar via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 25 10:30:22 PST 2019
Author: vedantk
Date: Fri Jan 25 10:30:22 2019
New Revision: 352227
URL: http://llvm.org/viewvc/llvm-project?rev=352227&view=rev
Log:
[MC] Teach the MachO object writer about N_FUNC_COLD
N_FUNC_COLD is a new MachO symbol attribute. It's a hint to the linker
to order a symbol towards the end of its section, to improve locality.
Example:
```
void a1() {}
__attribute__((cold)) void a2() {}
void a3() {}
int main() {
a1();
a2();
a3();
return 0;
}
```
A linker that supports N_FUNC_COLD will order _a2 to the end of the text
section. From `nm -njU` output, we see:
```
_a1
_a3
_main
_a2
```
Differential Revision: https://reviews.llvm.org/D57190
Added:
llvm/trunk/test/MC/AArch64/cold.s
llvm/trunk/test/tools/llvm-nm/AArch64/Inputs/cold-func.ll
llvm/trunk/test/tools/llvm-nm/AArch64/macho-cold.test
Modified:
llvm/trunk/include/llvm/BinaryFormat/MachO.h
llvm/trunk/include/llvm/MC/MCDirectives.h
llvm/trunk/include/llvm/MC/MCSymbolMachO.h
llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/trunk/lib/MC/MCAsmStreamer.cpp
llvm/trunk/lib/MC/MCELFStreamer.cpp
llvm/trunk/lib/MC/MCMachOStreamer.cpp
llvm/trunk/lib/MC/MCParser/AsmParser.cpp
llvm/trunk/tools/llvm-nm/llvm-nm.cpp
Modified: llvm/trunk/include/llvm/BinaryFormat/MachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryFormat/MachO.h?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/include/llvm/BinaryFormat/MachO.h (original)
+++ llvm/trunk/include/llvm/BinaryFormat/MachO.h Fri Jan 25 10:30:22 2019
@@ -334,6 +334,7 @@ enum {
N_WEAK_DEF = 0x0080u,
N_SYMBOL_RESOLVER = 0x0100u,
N_ALT_ENTRY = 0x0200u,
+ N_COLD_FUNC = 0x0400u,
// For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
// as these are in the top 8 bits.
SELF_LIBRARY_ORDINAL = 0x0,
Modified: llvm/trunk/include/llvm/MC/MCDirectives.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCDirectives.h?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCDirectives.h (original)
+++ llvm/trunk/include/llvm/MC/MCDirectives.h Fri Jan 25 10:30:22 2019
@@ -19,6 +19,7 @@ enum MCSymbolAttr {
MCSA_Invalid = 0, ///< Not a valid directive.
// Various directives in alphabetical order.
+ MCSA_Cold, ///< .cold (MachO)
MCSA_ELF_TypeFunction, ///< .type _foo, STT_FUNC # aka @function
MCSA_ELF_TypeIndFunction, ///< .type _foo, STT_GNU_IFUNC
MCSA_ELF_TypeObject, ///< .type _foo, STT_OBJECT # aka @object
Modified: llvm/trunk/include/llvm/MC/MCSymbolMachO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCSymbolMachO.h?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCSymbolMachO.h (original)
+++ llvm/trunk/include/llvm/MC/MCSymbolMachO.h Fri Jan 25 10:30:22 2019
@@ -34,6 +34,7 @@ class MCSymbolMachO : public MCSymbol {
SF_WeakDefinition = 0x0080,
SF_SymbolResolver = 0x0100,
SF_AltEntry = 0x0200,
+ SF_Cold = 0x0400,
// Common alignment
SF_CommonAlignmentMask = 0xF0FF,
@@ -97,6 +98,10 @@ public:
return getFlags() & SF_AltEntry;
}
+ void setCold() const { modifyFlags(SF_Cold, SF_Cold); }
+
+ bool isCold() const { return getFlags() & SF_Cold; }
+
void setDesc(unsigned Value) const {
assert(Value == (Value & SF_DescFlagsMask) &&
"Invalid .desc value!");
Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Fri Jan 25 10:30:22 2019
@@ -663,6 +663,9 @@ void AsmPrinter::EmitFunctionHeader() {
if (MAI->hasDotTypeDotSizeDirective())
OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
+ if (F.hasFnAttribute(Attribute::Cold))
+ OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_Cold);
+
if (isVerbose()) {
F.printAsOperand(OutStreamer->GetCommentOS(),
/*PrintType=*/false, F.getParent());
Modified: llvm/trunk/lib/MC/MCAsmStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmStreamer.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp Fri Jan 25 10:30:22 2019
@@ -656,6 +656,9 @@ bool MCAsmStreamer::EmitSymbolAttribute(
// .weak_reference
case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
+ case MCSA_Cold:
+ // Assemblers currently do not support a .cold directive.
+ return false;
}
Symbol->print(OS, MAI);
Modified: llvm/trunk/lib/MC/MCELFStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCELFStreamer.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCELFStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCELFStreamer.cpp Fri Jan 25 10:30:22 2019
@@ -201,6 +201,7 @@ bool MCELFStreamer::EmitSymbolAttribute(
// In the future it might be worth trying to make these operations more well
// defined.
switch (Attribute) {
+ case MCSA_Cold:
case MCSA_LazyReference:
case MCSA_Reference:
case MCSA_SymbolResolver:
Modified: llvm/trunk/lib/MC/MCMachOStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCMachOStreamer.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCMachOStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCMachOStreamer.cpp Fri Jan 25 10:30:22 2019
@@ -386,6 +386,10 @@ bool MCMachOStreamer::EmitSymbolAttribut
Symbol->setWeakDefinition();
Symbol->setWeakReference();
break;
+
+ case MCSA_Cold:
+ Symbol->setCold();
+ break;
}
return true;
Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Fri Jan 25 10:30:22 2019
@@ -426,6 +426,7 @@ private:
DK_WEAK_DEFINITION,
DK_WEAK_REFERENCE,
DK_WEAK_DEF_CAN_BE_HIDDEN,
+ DK_COLD,
DK_COMM,
DK_COMMON,
DK_LCOMM,
@@ -1983,6 +1984,8 @@ bool AsmParser::parseStatement(ParseStat
return parseDirectiveSymbolAttribute(MCSA_WeakReference);
case DK_WEAK_DEF_CAN_BE_HIDDEN:
return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
+ case DK_COLD:
+ return parseDirectiveSymbolAttribute(MCSA_Cold);
case DK_COMM:
case DK_COMMON:
return parseDirectiveComm(/*IsLocal=*/false);
@@ -5223,6 +5226,7 @@ void AsmParser::initializeDirectiveKindM
DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
+ DirectiveKindMap[".cold"] = DK_COLD;
DirectiveKindMap[".comm"] = DK_COMM;
DirectiveKindMap[".common"] = DK_COMMON;
DirectiveKindMap[".lcomm"] = DK_LCOMM;
Added: llvm/trunk/test/MC/AArch64/cold.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AArch64/cold.s?rev=352227&view=auto
==============================================================================
--- llvm/trunk/test/MC/AArch64/cold.s (added)
+++ llvm/trunk/test/MC/AArch64/cold.s Fri Jan 25 10:30:22 2019
@@ -0,0 +1,13 @@
+// RUN: llvm-mc -triple aarch64-apple-ios -o - -filetype=obj %s | \
+// RUN: llvm-readobj -symbols | FileCheck %s -check-prefix=READOBJ
+
+// READOBJ-LABEL: Name: cold_func
+// READOBJ-NEXT: Type: Section
+// READOBJ-NEXT: Section: __text
+// READOBJ-NEXT: RefType: UndefinedNonLazy (0x0)
+// READOBJ-NEXT: Flags [ (0x400)
+
+ .text
+ .cold cold_func
+cold_func:
+ ret
Added: llvm/trunk/test/tools/llvm-nm/AArch64/Inputs/cold-func.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/AArch64/Inputs/cold-func.ll?rev=352227&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/AArch64/Inputs/cold-func.ll (added)
+++ llvm/trunk/test/tools/llvm-nm/AArch64/Inputs/cold-func.ll Fri Jan 25 10:30:22 2019
@@ -0,0 +1,3 @@
+define void @cold_func() cold {
+ ret void
+}
Added: llvm/trunk/test/tools/llvm-nm/AArch64/macho-cold.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/AArch64/macho-cold.test?rev=352227&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/AArch64/macho-cold.test (added)
+++ llvm/trunk/test/tools/llvm-nm/AArch64/macho-cold.test Fri Jan 25 10:30:22 2019
@@ -0,0 +1,4 @@
+RUN: llc -O0 -mtriple=aarch64-apple-ios %p/Inputs/cold-func.ll -filetype=obj -o %t.aarch64.o
+RUN: llvm-nm -m %t.aarch64.o | FileCheck %s
+
+CHECK: [cold] _cold_func
Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=352227&r1=352226&r2=352227&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Fri Jan 25 10:30:22 2019
@@ -554,6 +554,11 @@ static void darwinPrintSymbol(SymbolicFi
(NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
outs() << "[alt entry] ";
+ if (Filetype == MachO::MH_OBJECT &&
+ ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
+ (NDesc & MachO::N_COLD_FUNC) == MachO::N_COLD_FUNC)
+ outs() << "[cold] ";
+
if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
outs() << "[Thumb] ";
More information about the llvm-commits
mailing list