Skip to content

Commit 4111907

Browse files
authored
[CIR][CIRGen] Add time trace to several CIRGen pieces (#898)
Then we can observe the time consumed in different part of CIR. This patch is not complete. But I think it is fine given we can always add them easily.
1 parent ecbb10b commit 4111907

File tree

7 files changed

+39
-0
lines changed

7 files changed

+39
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "llvm/Support/ErrorHandling.h"
7676
#include "llvm/Support/FileSystem.h"
7777
#include "llvm/Support/raw_ostream.h"
78+
#include "llvm/Support/TimeProfiler.h"
7879

7980
#include <iterator>
8081
#include <numeric>
@@ -462,6 +463,19 @@ void CIRGenModule::setDSOLocal(CIRGlobalValueInterface GV) const {
462463
}
463464

464465
void CIRGenModule::buildGlobal(GlobalDecl GD) {
466+
llvm::TimeTraceScope scope("build CIR Global", [&]() -> std::string {
467+
auto *ND = dyn_cast<NamedDecl>(GD.getDecl());
468+
if (!ND)
469+
// TODO: How to print decls which is not named decl?
470+
return "Unnamed decl";
471+
472+
std::string Name;
473+
llvm::raw_string_ostream OS(Name);
474+
ND->getNameForDiagnostic(OS, getASTContext().getPrintingPolicy(),
475+
/*Qualified=*/true);
476+
return Name;
477+
});
478+
465479
const auto *Global = cast<ValueDecl>(GD.getDecl());
466480

467481
assert(!Global->hasAttr<IFuncAttr>() && "NYI");

clang/lib/CIR/CodeGen/CIRPasses.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "mlir/Support/LogicalResult.h"
2020
#include "mlir/Transforms/Passes.h"
2121

22+
#include "llvm/Support/TimeProfiler.h"
23+
2224
namespace cir {
2325
mlir::LogicalResult runCIRToCIRPasses(
2426
mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx,
@@ -29,6 +31,8 @@ mlir::LogicalResult runCIRToCIRPasses(
2931
bool enableCIRSimplify, bool flattenCIR, bool emitMLIR,
3032
bool enableCallConvLowering, bool enableMem2Reg) {
3133

34+
llvm::TimeTraceScope scope("CIR To CIR Passes");
35+
3236
mlir::PassManager pm(mlirCtx);
3337
pm.addPass(mlir::createCIRCanonicalizePass());
3438

clang/lib/CIR/Dialect/Transforms/CallConvLowering.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define GEN_PASS_DEF_CALLCONVLOWERING
1919
#include "clang/CIR/Dialect/Passes.h.inc"
2020

21+
#include "llvm/Support/TimeProfiler.h"
22+
2123
namespace mlir {
2224
namespace cir {
2325

@@ -30,6 +32,8 @@ struct CallConvLoweringPattern : public OpRewritePattern<FuncOp> {
3032

3133
LogicalResult matchAndRewrite(FuncOp op,
3234
PatternRewriter &rewriter) const final {
35+
llvm::TimeTraceScope scope("Call Conv Lowering Pass", op.getSymName().str());
36+
3337
const auto module = op->getParentOfType<mlir::ModuleOp>();
3438

3539
if (!op.getAst())

clang/lib/CIR/Dialect/Transforms/GotoSolver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "clang/CIR/Dialect/IR/CIRDialect.h"
88
#include "clang/CIR/Dialect/Passes.h"
99

10+
#include "llvm/Support/TimeProfiler.h"
11+
1012
using namespace mlir;
1113
using namespace mlir::cir;
1214

@@ -43,6 +45,7 @@ static void process(mlir::cir::FuncOp func) {
4345
}
4446

4547
void GotoSolverPass::runOnOperation() {
48+
llvm::TimeTraceScope scope("Goto Solver");
4649
SmallVector<Operation *, 16> ops;
4750
getOperation()->walk([&](mlir::cir::FuncOp op) { process(op); });
4851
}

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class CIRGenConsumer : public clang::ASTConsumer {
169169
}
170170

171171
void HandleTranslationUnit(ASTContext &C) override {
172+
llvm::TimeTraceScope scope("CIR Gen");
173+
172174
// Note that this method is called after `HandleTopLevelDecl` has already
173175
// ran all over the top level decls. Here clang mostly wraps defered and
174176
// global codegen, followed by running CIR passes.

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include "llvm/IR/DerivedTypes.h"
6565
#include "llvm/Support/Casting.h"
6666
#include "llvm/Support/ErrorHandling.h"
67+
#include "llvm/Support/TimeProfiler.h"
6768
#include <cstdint>
6869
#include <deque>
6970
#include <optional>
@@ -4322,6 +4323,8 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar() {
43224323
}
43234324

43244325
void ConvertCIRToLLVMPass::runOnOperation() {
4326+
llvm::TimeTraceScope scope("Convert CIR to LLVM Pass");
4327+
43254328
auto module = getOperation();
43264329
mlir::DataLayout dataLayout(module);
43274330
mlir::LLVMTypeConverter converter(&getContext());
@@ -4404,6 +4407,8 @@ extern void registerCIRDialectTranslation(mlir::MLIRContext &context);
44044407
std::unique_ptr<llvm::Module>
44054408
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
44064409
bool disableVerifier) {
4410+
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");
4411+
44074412
mlir::MLIRContext *mlirCtx = theModule.getContext();
44084413
mlir::PassManager pm(mlirCtx);
44094414
populateCIRToLLVMPasses(pm);
@@ -4435,6 +4440,8 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
44354440
mlir::registerOpenMPDialectTranslation(*mlirCtx);
44364441
registerCIRDialectTranslation(*mlirCtx);
44374442

4443+
llvm::TimeTraceScope __scope("translateModuleToLLVMIR");
4444+
44384445
auto ModuleName = theModule.getName();
44394446
auto llvmModule = mlir::translateModuleToLLVMIR(
44404447
theModule, llvmCtx, ModuleName ? *ModuleName : "CIRToLLVMModule");

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "llvm/ADT/Sequence.h"
5454
#include "llvm/ADT/SmallVector.h"
5555
#include "llvm/ADT/TypeSwitch.h"
56+
#include "llvm/Support/TimeProfiler.h"
5657

5758
using namespace cir;
5859
using namespace llvm;
@@ -1419,6 +1420,8 @@ std::unique_ptr<llvm::Module>
14191420
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
14201421
std::unique_ptr<mlir::MLIRContext> mlirCtx,
14211422
LLVMContext &llvmCtx) {
1423+
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");
1424+
14221425
mlir::PassManager pm(mlirCtx.get());
14231426

14241427
pm.addPass(createConvertCIRToMLIRPass());
@@ -1451,6 +1454,8 @@ std::unique_ptr<mlir::Pass> createConvertCIRToMLIRPass() {
14511454

14521455
mlir::ModuleOp lowerFromCIRToMLIR(mlir::ModuleOp theModule,
14531456
mlir::MLIRContext *mlirCtx) {
1457+
llvm::TimeTraceScope scope("Lower CIR To MLIR");
1458+
14541459
mlir::PassManager pm(mlirCtx);
14551460

14561461
pm.addPass(createConvertCIRToMLIRPass());

0 commit comments

Comments
 (0)