This is an archive of the discontinued LLVM Phabricator instance.

Resubmit: [Analysis] Refactor MBB hotness/coldness into templated PSI functions
ClosedPublic

Authored by shenhan on Jun 27 2023, 3:40 PM.

Details

Summary

The origin patch D152758 caused gcc-12 build failures, which is caused by a gcc bug, and won't be fixed in version 12.

The workaround here is to move full specialization function
definitions out of class declaration into namespace level.

Tested with linux gcc-12, linux clang, windows cl compilers.

Currently, to use PSI->isFunctionHotInCallGraph, we first need to
calculate BPI->BFI, which is expensive. Instead, we can implement
this directly with MBFI. Also as @wenlei mentioned in another patch
review, that MachineSizeOpts already has isFunctionColdInCallGraph,
isFunctionHotInCallGraphNthPercentile, etc implemented. These can be
refactored and so they can be reused across MachineFunctionSplitting
and MachineSizeOpts passes.

This CL does this - it refactors out those internal static functions
into PSI as templated functions, so they can be accessed easily.

This CL was reverted twice - 1) it hits a gcc bug; 2) it
breaks bazel's dependency rules (bazel building was not conducted
before submitting)

Diff Detail

Event Timeline

shenhan created this revision.Jun 27 2023, 3:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 3:40 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
shenhan requested review of this revision.Jun 27 2023, 3:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 3:40 PM
shenhan updated this revision to Diff 535164.Jun 27 2023, 3:42 PM
shenhan edited the summary of this revision. (Show Details)
shenhan updated this revision to Diff 535167.Jun 27 2023, 3:45 PM
shenhan updated this revision to Diff 535168.Jun 27 2023, 3:51 PM
shenhan updated this revision to Diff 535169.
shenhan updated this revision to Diff 535174.Jun 27 2023, 3:56 PM
shenhan edited the summary of this revision. (Show Details)
wenlei accepted this revision.Jun 27 2023, 4:03 PM

lgtm, thanks.

This revision is now accepted and ready to land.Jun 27 2023, 4:03 PM
shenhan updated this revision to Diff 535227.Jun 27 2023, 9:33 PM
This revision was landed with ongoing or failed builds.Jun 27 2023, 10:03 PM
This revision was automatically updated to reflect the committed changes.
MaskRay added a subscriber: MaskRay.EditedJun 28 2023, 12:27 AM

Apologies but I am going to revert this patch. There is a layering violation (https://llvm.org/docs/CodingStandards.html#library-layering). LLVMAnalysis cannot depend on LLVMCodeGen. This is breaking our Bazel build.

llvm/include/llvm/Analysis/ProfileSummaryInfo.h:19:10: fatal error: 'llvm/CodeGen/MachineFunction.h' file not found
   19 | #include "llvm/CodeGen/MachineFunction.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hokein added a subscriber: hokein.Jun 28 2023, 12:33 AM
hokein added inline comments.
llvm/include/llvm/Analysis/ProfileSummaryInfo.h
19

This seems to introduce a layering violation.

After this patch, the llvm::Analysis target now depends on the llvm::CodeGen target, but llvm::CodeGen already depends on llvm::Analysis.

One fix is to move the moving the full specialization for the MachineFunction to the CodeGen target (MachineSizeOpts.cpp)

MaskRay added inline comments.Jun 28 2023, 12:35 AM
llvm/include/llvm/Analysis/ProfileSummaryInfo.h
134

omit braces

155

omit braces

343

The LLVM convention is to omit all braces as the body has just one line.

Apologies but I am going to revert this patch. There is a layering violation (https://llvm.org/docs/CodingStandards.html#library-layering). LLVMAnalysis cannot depend on LLVMCodeGen. This is breaking our Bazel build.

llvm/include/llvm/Analysis/ProfileSummaryInfo.h:19:10: fatal error: 'llvm/CodeGen/MachineFunction.h' file not found
   19 | #include "llvm/CodeGen/MachineFunction.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Good catch..

shenhan updated this revision to Diff 535480.Jun 28 2023, 11:47 AM
shenhan updated this revision to Diff 535485.Jun 28 2023, 11:54 AM
shenhan marked 3 inline comments as done.
shenhan reopened this revision.EditedJun 28 2023, 12:09 PM
shenhan edited reviewers, added: MaskRay; removed: aidengrossman.

The last revision Diff 8 caused bazel build failures, because ProfileSummaryInfo.h includes MachineFunction.h, which creates a cyclic dependency in bazel BUILD rules.

Fixed the issue by moving the template specialization implementation into MachineFunction.cpp, leaving only the declaration in ProfileSummaryInfo.h file.

Reran the following builds:
gcc linux
clang linux
cl windows
bazel-5.0.0 build --config=generic_clang/gcc @llvm-projectclang/...
bazel-5.0.0 build --config=generic_clang/gcc @llvm-project
clang-tools-extra/...
bazel-5.0.0 build --config=generic_clang/gcc @llvm-projectmlir/...
bazel-5.0.0 build --config=generic_clang/gcc @llvm-project
lld/...
bazel-5.0.0 build --config=generic_clang/gcc @llvm-project//llvm:CodeGen

Added @MaskRay as reviewers.

llvm/include/llvm/Analysis/ProfileSummaryInfo.h
343

Removed all cascading braces.

This revision is now accepted and ready to land.Jun 28 2023, 12:09 PM
shenhan marked an inline comment as done.Jun 28 2023, 12:12 PM
shenhan added inline comments.
llvm/include/llvm/Analysis/ProfileSummaryInfo.h
19

Removed the inclusion. Thanks.

shenhan marked an inline comment as done.Jun 28 2023, 12:18 PM
shenhan retitled this revision from Resubmit: [NFC] Refactor MBB hotness/coldness into templated PSI functions to Resubmit: [Analysis] Refactor MBB hotness/coldness into templated PSI functions.Jun 28 2023, 2:09 PM
MaskRay accepted this revision.EditedJun 28 2023, 7:12 PM

Thanks. This change should support Bazel builds.

The origin CL D152758 caused gcc-12 build failures, which is caused by a gcc bug, and won't be fixed in version 12.

"CL" means https://opensource.google/documentation/reference/glossary#changelist , but our term is Differential, or just "patch" :)

In fact, I'd suggest that you remove

Below is origin CL description.

[NFC] Refactor MBB hotness/coldness into templated PSI functions.

Just start with the original description. You can add information in last paragraph about what's changed from the reverted (two?) commits.

In D152399, we calculate BPI->BFI in MachineFunctionSplit pass just to use PSI->isFunctionHotInCallGraph, which is expensive. Instead, we can implement this directly with MBFI.

Since this patch will land before D152399 , describing the situation with D152399 as the subject may be confusing. I suggest that you describe the current code structure, pretending that D152399 does not exist.
Then, if you think appropriate, describe that this refactoring will make D152399 more focused.

llvm/lib/CodeGen/MachineFunction.cpp
1504 ↗(On Diff #535485)

Remove namespace llvm (see https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions).

ProfileSummaryInfo is in the namespace llvm, so ProfileSummaryInfo::getEntryCount is sufficient.

Ideally getEntryCount should be a const reference to indicate that it cannot be null, but the API uses a pointer, so it seems fine to not change it.

shenhan edited the summary of this revision. (Show Details)Jun 28 2023, 10:10 PM
shenhan edited the summary of this revision. (Show Details)Jun 28 2023, 10:12 PM

Thanks. This change should support Bazel builds.

The origin CL D152758 caused gcc-12 build failures, which is caused by a gcc bug, and won't be fixed in version 12.

"CL" means https://opensource.google/documentation/reference/glossary#changelist , but our term is Differential, or just "patch" :)

Done.

In fact, I'd suggest that you remove

Below is origin CL description.

[NFC] Refactor MBB hotness/coldness into templated PSI functions.

Just start with the original description. You can add information in last paragraph about what's changed from the reverted (two?) commits.

Done

In D152399, we calculate BPI->BFI in MachineFunctionSplit pass just to use PSI->isFunctionHotInCallGraph, which is expensive. Instead, we can implement this directly with MBFI.

Since this patch will land before D152399 , describing the situation with D152399 as the subject may be confusing. I suggest that you describe the current code structure, pretending that D152399 does not exist.
Then, if you think appropriate, describe that this refactoring will make D152399 more focused.

Revised the description.

shenhan updated this revision to Diff 535621.Jun 28 2023, 10:26 PM
shenhan marked an inline comment as done.
This revision was landed with ongoing or failed builds.Jun 28 2023, 10:34 PM
This revision was automatically updated to reflect the committed changes.