Skip to content

Commit

Permalink
[NFC] Improve time tracing data (#7146)
Browse files Browse the repository at this point in the history
This is a bunch of small changes to improve the quality of the time
traces. This mostly adds new timers breakign down dxcompilerobj and the
always inliner code.
  • Loading branch information
llvm-beanz authored Mar 7, 2025
1 parent f7f1e3d commit 4d3a2f5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/DxilContainer/DxilContainerAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include <algorithm>
#include <assert.h> // Needed for DxilPipelineStateValidation.h
Expand Down Expand Up @@ -1895,6 +1896,7 @@ void hlsl::SerializeDxilContainerForModule(
DxilShaderHash *pShaderHashOut, AbstractMemoryStream *pReflectionStreamOut,
AbstractMemoryStream *pRootSigStreamOut, void *pPrivateData,
size_t PrivateDataSize) {
llvm::TimeTraceScope TimeScope("SerializeDxilContainer", StringRef(""));
// TODO: add a flag to update the module and remove information that is not
// part of DXIL proper and is used only to assemble the container.

Expand Down
6 changes: 5 additions & 1 deletion lib/Transforms/Utils/CloneFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.h"
Expand All @@ -29,7 +28,9 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include <map>
Expand Down Expand Up @@ -473,6 +474,9 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
const char *NameSuffix,
ClonedCodeInfo *CodeInfo,
CloningDirector *Director) {
TimeTraceScope TimeScope("CloneAndPruneIntoFromInst", [&] {
return (Twine(OldFunc->getName()) + "->" + NewFunc->getName()).str();
});
assert(NameSuffix && "NameSuffix cannot be null!");

ValueMapTypeRemapper *TypeMapper = nullptr;
Expand Down
16 changes: 11 additions & 5 deletions lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
Expand All @@ -24,22 +23,24 @@
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
using namespace llvm;

Expand Down Expand Up @@ -291,6 +292,8 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
/// non-aliasing property communicated by the metadata could have
/// call-site-specific control dependencies).
static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
TimeTraceScope TimeScope("CloneAliasScopeMetadata",
[&] { return CS.getCalledFunction()->getName(); });
const Function *CalledFunc = CS.getCalledFunction();
SetVector<const MDNode *> MD;

Expand Down Expand Up @@ -401,6 +404,8 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
/// non-derived loads, stores and memory intrinsics with the new alias scopes.
static void AddAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap,
const DataLayout &DL, AliasAnalysis *AA) {
TimeTraceScope TimeScope("AddAliasScopeMetadata",
[&] { return CS.getCalledFunction()->getName(); });
if (!EnableNoAliasConversion)
return;

Expand Down Expand Up @@ -872,6 +877,7 @@ updateInlinedAtInfo(DebugLoc DL, DILocation *InlinedAtNode, LLVMContext &Ctx,
/// to encode location where these instructions are inlined.
static void fixupLineNumbers(Function *Fn, Function::iterator FI,
Instruction *TheCall) {
TimeTraceScope TimeScope("fixupLineNumbers", [&] { return Fn->getName(); });
DebugLoc TheCallDL = TheCall->getDebugLoc();
#if 0 // HLSL Change
if (!TheCallDL)
Expand Down
6 changes: 6 additions & 0 deletions tools/clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,12 @@ void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {

/// EmitTopLevelDecl - Emit code for a single top level declaration.
void CodeGenModule::EmitTopLevelDecl(Decl *D) {
llvm::TimeTraceScope TimeScope("CGM::EmitTopLevelDecl", [&] {
if (const auto *ND = dyn_cast<NamedDecl>(D))
return ND->getName();
return StringRef("Unnamed decl");
});

// Ignore dependent declarations.
if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
return;
Expand Down
16 changes: 10 additions & 6 deletions tools/clang/lib/Parse/ParseAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,

void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {

// HLSL Change - Support hierarchial time tracing.
llvm::TimeTraceScope TimeScope("Frontend", StringRef(""));
// Collect global stats on Decls/Stmts (until we have a module streamer).
if (PrintStats) {
Decl::EnableStatistics();
Expand Down Expand Up @@ -137,6 +135,8 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
External->StartTranslationUnit(Consumer);

if (!S.getDiagnostics().hasUnrecoverableErrorOccurred()) { // HLSL Change: Skip if fatal error already occurred
// HLSL Change - Support hierarchial time tracing.
llvm::TimeTraceScope TimeScope("Frontend", StringRef(""));
if (P.ParseTopLevelDecl(ADecl)) {
if (!External && !S.getLangOpts().CPlusPlus)
P.Diag(diag::ext_empty_translation_unit);
Expand All @@ -151,10 +151,14 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
}
} // HLSL Change: Skip if fatal error already occurred

// Process any TopLevelDecls generated by #pragma weak.
for (Decl *D : S.WeakTopLevelDecls())
Consumer->HandleTopLevelDecl(DeclGroupRef(D));

{
// HLSL Change - Support hierarchial time tracing.
llvm::TimeTraceScope TimeScope("Frontend - Consumer", StringRef(""));
// Process any TopLevelDecls generated by #pragma weak.
for (Decl *D : S.WeakTopLevelDecls())
Consumer->HandleTopLevelDecl(DeclGroupRef(D));
}

// HLSL Change Starts
// Provide the opportunity to generate translation-unit level validation
// errors in the front-end, without relying on code generation being
Expand Down
5 changes: 5 additions & 0 deletions tools/clang/tools/dxcompiler/dxcompilerobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ class DxcCompiler : public IDxcCompiler3,
bool validateRootSigContainer = false;

if (isPreprocessing) {
TimeTraceScope TimeScope("PreprocessAction", StringRef(""));
// These settings are back-compatible with fxc.
clang::PreprocessorOutputOptions &PPOutOpts =
compiler.getPreprocessorOutputOpts();
Expand Down Expand Up @@ -867,6 +868,7 @@ class DxcCompiler : public IDxcCompiler3,
compiler.getTarget().adjust(compiler.getLangOpts());

if (opts.AstDump) {
TimeTraceScope TimeScope("DumpAST", StringRef(""));
clang::ASTDumpAction dumpAction;
// Consider - ASTDumpFilter, ASTDumpLookups
compiler.getFrontendOpts().ASTDumpDecls = true;
Expand All @@ -876,6 +878,7 @@ class DxcCompiler : public IDxcCompiler3,
dumpAction.EndSourceFile();
outStream.flush();
} else if (opts.DumpDependencies) {
TimeTraceScope TimeScope("DumpDependencies", StringRef(""));
auto dependencyCollector = std::make_shared<DependencyCollector>();
compiler.addDependencyCollector(dependencyCollector);
compiler.createPreprocessor(clang::TranslationUnitKind::TU_Complete);
Expand Down Expand Up @@ -978,6 +981,7 @@ class DxcCompiler : public IDxcCompiler3,
EmitBCAction action(&llvmContext);
FrontendInputFile file(pUtf8SourceName, IK_HLSL);
bool compileOK;
TimeTraceScope TimeScope("Compile Action", StringRef(""));
if (action.BeginSourceFile(compiler, file)) {
action.Execute();
action.EndSourceFile();
Expand Down Expand Up @@ -1032,6 +1036,7 @@ class DxcCompiler : public IDxcCompiler3,
// Do not create a container when there is only a a high-level
// representation in the module.
if (compileOK && !opts.CodeGenHighLevel) {
TimeTraceScope TimeScope("AssembleAndWriteContainer", StringRef(""));
HRESULT valHR = S_OK;
CComPtr<AbstractMemoryStream> pRootSigStream;
IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(),
Expand Down

0 comments on commit 4d3a2f5

Please sign in to comment.