Skip to content

Commit 53f16c8

Browse files
ChuanqiXu9lanza
authored andcommitted
[CIR][CIRGen] Add time trace to several CIRGen pieces (llvm#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 e7d7a7a commit 53f16c8

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-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>
@@ -463,6 +464,19 @@ void CIRGenModule::setDSOLocal(CIRGlobalValueInterface GV) const {
463464
}
464465

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

468482
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
@@ -172,6 +172,8 @@ class CIRGenConsumer : public clang::ASTConsumer {
172172
}
173173

174174
void HandleTranslationUnit(ASTContext &C) override {
175+
llvm::TimeTraceScope scope("CIR Gen");
176+
175177
// Note that this method is called after `HandleTopLevelDecl` has already
176178
// ran all over the top level decls. Here clang mostly wraps defered and
177179
// 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>
@@ -4313,6 +4314,8 @@ void ConvertCIRToLLVMPass::buildGlobalAnnotationsVar() {
43134314
}
43144315

43154316
void ConvertCIRToLLVMPass::runOnOperation() {
4317+
llvm::TimeTraceScope scope("Convert CIR to LLVM Pass");
4318+
43164319
auto module = getOperation();
43174320
mlir::DataLayout dataLayout(module);
43184321
mlir::LLVMTypeConverter converter(&getContext());
@@ -4395,6 +4398,8 @@ extern void registerCIRDialectTranslation(mlir::MLIRContext &context);
43954398
std::unique_ptr<llvm::Module>
43964399
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
43974400
bool disableVerifier) {
4401+
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");
4402+
43984403
mlir::MLIRContext *mlirCtx = theModule.getContext();
43994404
mlir::PassManager pm(mlirCtx);
44004405
populateCIRToLLVMPasses(pm);
@@ -4426,6 +4431,8 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
44264431
mlir::registerOpenMPDialectTranslation(*mlirCtx);
44274432
registerCIRDialectTranslation(*mlirCtx);
44284433

4434+
llvm::TimeTraceScope __scope("translateModuleToLLVMIR");
4435+
44294436
auto ModuleName = theModule.getName();
44304437
auto llvmModule = mlir::translateModuleToLLVMIR(
44314438
theModule, llvmCtx, ModuleName ? *ModuleName : "CIRToLLVMModule");

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/ADT/Sequence.h"
5858
#include "llvm/ADT/SmallVector.h"
5959
#include "llvm/ADT/TypeSwitch.h"
60+
#include "llvm/Support/TimeProfiler.h"
6061

6162
using namespace cir;
6263
using namespace llvm;
@@ -1506,6 +1507,7 @@ std::unique_ptr<llvm::Module>
15061507
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
15071508
std::unique_ptr<mlir::MLIRContext> mlirCtx,
15081509
llvm::LLVMContext &llvmCtx) {
1510+
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");
15091511
mlir::PassManager pm(mlirCtx.get());
15101512

15111513
pm.addPass(createConvertCIRToMLIRPass());
@@ -1560,6 +1562,8 @@ std::unique_ptr<mlir::Pass> createConvertCIRToMLIRPass() {
15601562

15611563
mlir::ModuleOp lowerFromCIRToMLIR(mlir::ModuleOp theModule,
15621564
mlir::MLIRContext *mlirCtx) {
1565+
llvm::TimeTraceScope scope("Lower CIR To MLIR");
1566+
15631567
mlir::PassManager pm(mlirCtx);
15641568
pm.addPass(createConvertCIRToMLIRPass());
15651569

0 commit comments

Comments
 (0)