wenlei created this revision. wenlei added reviewers: wmi, davidxl, hoy. Herald added subscribers: llvm-commits, cfe-commits, hiraditya. Herald added projects: clang, LLVM.
Add call site location info into inline remarks so we can differentiate inline sites. This can be useful for inliner tuning. We can also reconstruct full hierarchical inline tree from parsing such remarks. The messege of inline remark is also tweaked so we can differentiate SampleProfileLoader inline from CGSCC inline. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D82213 Files: clang/test/Frontend/optimization-remark-with-hotness-new-pm.c clang/test/Frontend/optimization-remark-with-hotness.c llvm/include/llvm/Analysis/InlineAdvisor.h llvm/lib/Analysis/InlineAdvisor.cpp llvm/lib/Transforms/IPO/SampleProfile.cpp llvm/test/Transforms/Inline/optimization-remarks-passed-yaml.ll llvm/test/Transforms/SampleProfile/remarks.ll
Index: llvm/test/Transforms/SampleProfile/remarks.ll =================================================================== --- llvm/test/Transforms/SampleProfile/remarks.ll +++ llvm/test/Transforms/SampleProfile/remarks.ll @@ -21,7 +21,7 @@ ; We are expecting foo() to be inlined in main() (almost all the cycles are ; spent inside foo). -; CHECK: remark: remarks.cc:13:21: inlined callee '_Z3foov' into 'main' +; CHECK: remark: remarks.cc:13:21: _Z3foov inlined into main by profile guided inliner with (cost=130, threshold=225) at callsite main:0 ; The back edge for the loop is the hottest edge in the loop subgraph. ; CHECK: remark: remarks.cc:6:9: most popular destination for conditional branches at remarks.cc:5:3 @@ -32,17 +32,26 @@ ; Checking to see if YAML file is generated and contains remarks ;YAML: --- !Passed ;YAML-NEXT: Pass: sample-profile-inline -;YAML-NEXT: Name: InlineSuccess +;YAML-NEXT: Name: Inlined ;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 21 } ;YAML-NEXT: Function: main ;YAML-NEXT: Args: -;YAML-NEXT: - String: 'inlined callee ''' ;YAML-NEXT: - Callee: _Z3foov ;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 3, Column: 0 } -;YAML-NEXT: - String: ''' into ''' +;YAML-NEXT: - String: ' inlined into ' ;YAML-NEXT: - Caller: main ;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 0 } -;YAML-NEXT: - String: '''' +;YAML-NEXT: - String: ' by profile guided inliner' +;YAML-NEXT: - String: ' with ' +;YAML-NEXT: - String: '(cost=' +;YAML-NEXT: - Cost: '130' +;YAML-NEXT: - String: ', threshold=' +;YAML-NEXT: - Threshold: '225' +;YAML-NEXT: - String: ')' +;YAML-NEXT: - String: ' at callsite ' +;YAML-NEXT: - String: main +;YAML-NEXT: - String: ':' +;YAML-NEXT: - Line: '0' ;YAML-NEXT: ... ;YAML: --- !Analysis ;YAML-NEXT: Pass: sample-profile Index: llvm/test/Transforms/Inline/optimization-remarks-passed-yaml.ll =================================================================== --- llvm/test/Transforms/Inline/optimization-remarks-passed-yaml.ll +++ llvm/test/Transforms/Inline/optimization-remarks-passed-yaml.ll @@ -22,7 +22,7 @@ ; 4 return foo(); ; 5 } -; CHECK: remark: /tmp/s.c:4:10: foo inlined into bar with (cost={{[0-9\-]+}}, threshold={{[0-9]+}}) (hotness: 30) +; CHECK: remark: /tmp/s.c:4:10: foo inlined into bar with (cost={{[0-9\-]+}}, threshold={{[0-9]+}}) at callsite bar:1 (hotness: 30) ; YAML: --- !Passed ; YAML-NEXT: Pass: inline @@ -42,6 +42,10 @@ ; YAML-NEXT: - String: ', threshold=' ; YAML-NEXT: - Threshold: '{{[0-9]+}}' ; YAML-NEXT: - String: ')' +; YAML-NEXT: - String: ' at callsite ' +; YAML-NEXT: - String: bar +; YAML-NEXT: - String: ':' +; YAML-NEXT: - Line: '1' ; YAML-NEXT: ... ; ModuleID = '/tmp/s.c' Index: llvm/lib/Transforms/IPO/SampleProfile.cpp =================================================================== --- llvm/lib/Transforms/IPO/SampleProfile.cpp +++ llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -37,6 +37,7 @@ #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" +#include "llvm/Analysis/InlineAdvisor.h" #include "llvm/Analysis/InlineCost.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" @@ -916,9 +917,8 @@ InlineFunctionInfo IFI(nullptr, GetAC); if (InlineFunction(CB, IFI).isSuccess()) { // The call to InlineFunction erases I, so we can't pass it here. - ORE->emit(OptimizationRemark(CSINLINE_DEBUG, "InlineSuccess", DLoc, BB) - << "inlined callee '" << ore::NV("Callee", CalledFunction) - << "' into '" << ore::NV("Caller", BB->getParent()) << "'"); + emitInlinedInto(*ORE, DLoc, BB, *CalledFunction, *BB->getParent(), Cost, + true, CSINLINE_DEBUG); return true; } return false; Index: llvm/lib/Analysis/InlineAdvisor.cpp =================================================================== --- llvm/lib/Analysis/InlineAdvisor.cpp +++ llvm/lib/Analysis/InlineAdvisor.cpp @@ -18,6 +18,7 @@ #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/Instructions.h" #include "llvm/Support/raw_ostream.h" @@ -354,14 +355,43 @@ return IC; } +void llvm::addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc) { + if (!DLoc.get()) + return; + + bool First = true; + Remark << " at callsite "; + for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) { + if (!First) + Remark << " @ "; + unsigned int Offset = DIL->getLine(); + Offset -= DIL->getScope()->getSubprogram()->getLine(); + unsigned int Discriminator = DIL->getBaseDiscriminator(); + StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName(); + if (Name.empty()) + Name = DIL->getScope()->getSubprogram()->getName(); + Remark << Name << ":" << ore::NV("Line", Offset); + if (Discriminator) + Remark << "." << ore::NV("Disc", Discriminator); + First = false; + } +} + void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc, const BasicBlock *Block, const Function &Callee, - const Function &Caller, const InlineCost &IC) { + const Function &Caller, const InlineCost &IC, + bool ProfileGuidedInline, const char *PassName) { ORE.emit([&]() { bool AlwaysInline = IC.isAlways(); StringRef RemarkName = AlwaysInline ? "AlwaysInline" : "Inlined"; - return OptimizationRemark(DEBUG_TYPE, RemarkName, DLoc, Block) - << ore::NV("Callee", &Callee) << " inlined into " - << ore::NV("Caller", &Caller) << " with " << IC; + OptimizationRemark Remark(PassName ? PassName : DEBUG_TYPE, RemarkName, + DLoc, Block); + Remark << ore::NV("Callee", &Callee) << " inlined into "; + Remark << ore::NV("Caller", &Caller); + if (ProfileGuidedInline) + Remark << " by profile guided inliner"; + Remark << " with " << IC; + addLocationToRemarks(Remark, DLoc); + return Remark; }); } Index: llvm/include/llvm/Analysis/InlineAdvisor.h =================================================================== --- llvm/include/llvm/Analysis/InlineAdvisor.h +++ llvm/include/llvm/Analysis/InlineAdvisor.h @@ -217,7 +217,12 @@ /// Emit ORE message. void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc, const BasicBlock *Block, const Function &Callee, - const Function &Caller, const InlineCost &IC); + const Function &Caller, const InlineCost &IC, + bool ProfileGuidedInline = false, + const char *PassName = nullptr); + +/// Add location info to ORE message. +void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc); /// Set the inline-remark attribute. void setInlineRemark(CallBase &CB, StringRef Message); Index: clang/test/Frontend/optimization-remark-with-hotness.c =================================================================== --- clang/test/Frontend/optimization-remark-with-hotness.c +++ clang/test/Frontend/optimization-remark-with-hotness.c @@ -66,7 +66,7 @@ // THRESHOLD-NOT: hotness // NO_PGO: '-fdiagnostics-show-hotness' requires profile-guided optimization information // NO_PGO: '-fdiagnostics-hotness-threshold=' requires profile-guided optimization information - // expected-remark@+1 {{foo inlined into bar with (cost=always): always inliner (hotness:}} + // expected-remark@+1 {{foo inlined into bar with (cost=always): always inliner at callsite bar:8 (hotness:}} sum += foo(x, x - 2); } Index: clang/test/Frontend/optimization-remark-with-hotness-new-pm.c =================================================================== --- clang/test/Frontend/optimization-remark-with-hotness-new-pm.c +++ clang/test/Frontend/optimization-remark-with-hotness-new-pm.c @@ -73,7 +73,7 @@ // THRESHOLD-NOT: hotness // NO_PGO: '-fdiagnostics-show-hotness' requires profile-guided optimization information // NO_PGO: '-fdiagnostics-hotness-threshold=' requires profile-guided optimization information - // expected-remark@+1 {{foo inlined into bar with (cost=always): always inline attribute (hotness:}} + // expected-remark@+1 {{foo inlined into bar with (cost=always): always inline attribute at callsite bar:8 (hotness:}} sum += foo(x, x - 2); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits