[llvm] 6c91e62 - [CodeGen] emit CG profile for COFF object file
Zequan Wu via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 12:03:39 PDT 2020
Author: Zequan Wu
Date: 2020-09-29T12:03:30-07:00
New Revision: 6c91e623e53703560e781b172e9160cae2cf8d21
URL: https://github.com/llvm/llvm-project/commit/6c91e623e53703560e781b172e9160cae2cf8d21
DIFF: https://github.com/llvm/llvm-project/commit/6c91e623e53703560e781b172e9160cae2cf8d21.diff
LOG: [CodeGen] emit CG profile for COFF object file
Differential Revision: https://reviews.llvm.org/D87811
Added:
llvm/test/MC/COFF/cgprofile.ll
Modified:
llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 57cead3dde6c..625137a1f998 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -143,6 +143,7 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
mutable unsigned NextUniqueID = 0;
+ const TargetMachine *TM = nullptr;
public:
~TargetLoweringObjectFileCOFF() override = default;
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index a7f560f3f2c2..676a465c49e2 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1599,18 +1599,62 @@ void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
StringRef Section;
GetObjCImageInfo(M, Version, Flags, Section);
- if (Section.empty())
- return;
+ if (!Section.empty()) {
+ auto &C = getContext();
+ auto *S = C.getCOFFSection(Section,
+ COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
+ COFF::IMAGE_SCN_MEM_READ,
+ SectionKind::getReadOnly());
+ Streamer.SwitchSection(S);
+ Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
+ Streamer.emitInt32(Version);
+ Streamer.emitInt32(Flags);
+ Streamer.AddBlankLine();
+ }
auto &C = getContext();
- auto *S = C.getCOFFSection(
- Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
- SectionKind::getReadOnly());
- Streamer.SwitchSection(S);
- Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
- Streamer.emitInt32(Version);
- Streamer.emitInt32(Flags);
- Streamer.AddBlankLine();
+ SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
+ M.getModuleFlagsMetadata(ModuleFlags);
+
+ MDNode *CFGProfile = nullptr;
+
+ for (const auto &MFE : ModuleFlags) {
+ StringRef Key = MFE.Key->getString();
+ if (Key == "CG Profile") {
+ CFGProfile = cast<MDNode>(MFE.Val);
+ break;
+ }
+ }
+
+ if (!CFGProfile)
+ return;
+
+ auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
+ if (!MDO)
+ return nullptr;
+ auto V = cast<ValueAsMetadata>(MDO);
+ const Function *F = cast<Function>(V->getValue());
+ if (F->hasDLLImportStorageClass())
+ return nullptr;
+ return TM->getSymbol(F);
+ };
+
+ for (const auto &Edge : CFGProfile->operands()) {
+ MDNode *E = cast<MDNode>(Edge);
+ const MCSymbol *From = GetSym(E->getOperand(0));
+ const MCSymbol *To = GetSym(E->getOperand(1));
+ // Skip null functions. This can happen if functions are dead stripped after
+ // the CGProfile pass has been run.
+ if (!From || !To)
+ continue;
+ uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
+ ->getValue()
+ ->getUniqueInteger()
+ .getZExtValue();
+ Streamer.emitCGProfileEntry(
+ MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
+ MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
+ }
}
void TargetLoweringObjectFileCOFF::emitLinkerDirectives(
@@ -1675,6 +1719,7 @@ void TargetLoweringObjectFileCOFF::emitLinkerDirectives(
void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
const TargetMachine &TM) {
TargetLoweringObjectFile::Initialize(Ctx, TM);
+ this->TM = &TM;
const Triple &T = TM.getTargetTriple();
if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
StaticCtorSection =
diff --git a/llvm/test/MC/COFF/cgprofile.ll b/llvm/test/MC/COFF/cgprofile.ll
new file mode 100644
index 000000000000..0156aedb1221
--- /dev/null
+++ b/llvm/test/MC/COFF/cgprofile.ll
@@ -0,0 +1,51 @@
+; RUN: llc -filetype=asm %s -o - -mtriple x86_64-pc-windows-msvc | FileCheck %s
+; RUN: llc -filetype=obj %s -o %t -mtriple x86_64-pc-windows-msvc
+; RUN: llvm-readobj --cg-profile %t | FileCheck %s --check-prefix=OBJ
+
+declare void @b()
+
+define void @a() {
+ call void @b()
+ ret void
+}
+
+define void @freq(i1 %cond) {
+ br i1 %cond, label %A, label %B
+A:
+ call void @a();
+ ret void
+B:
+ call void @b();
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 5, !"CG Profile", !1}
+!1 = !{!2, !3, !4, !5}
+!2 = !{void ()* @a, void ()* @b, i64 32}
+!3 = !{void (i1)* @freq, void ()* @a, i64 11}
+!4 = !{void (i1)* @freq, void ()* @b, i64 20}
+!5 = !{void (i1)* @freq, null, i64 20}
+
+; CHECK: .cg_profile a, b, 32
+; CHECK: .cg_profile freq, a, 11
+; CHECK: .cg_profile freq, b, 20
+
+; OBJ: CGProfile [
+; OBJ: CGProfileEntry {
+; OBJ: From: a
+; OBJ: To: b
+; OBJ: Weight: 32
+; OBJ: }
+; OBJ: CGProfileEntry {
+; OBJ: From: freq
+; OBJ: To: a
+; OBJ: Weight: 11
+; OBJ: }
+; OBJ: CGProfileEntry {
+; OBJ: From: freq
+; OBJ: To: b
+; OBJ: Weight: 20
+; OBJ: }
+; OBJ:]
More information about the llvm-commits
mailing list