Skip to content

Commit 1f33485

Browse files
authored
Consolidate timetrace implementations (#4884)
1 parent f641425 commit 1f33485

24 files changed

+160
-908
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ set(DRV_HDR
413413
driver/linker.h
414414
driver/plugins.h
415415
driver/targetmachine.h
416-
driver/timetrace.h
417416
driver/toobj.h
418417
driver/tool.h
419418
)

dmd/dinterpret.d

-16
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,9 @@ public Expression ctfeInterpret(Expression e)
100100

101101
auto rgnpos = ctfeGlobals.region.savePos();
102102

103-
version (IN_LLVM) // TODO: required?
104-
{
105-
import driver.timetrace, std.format, std.conv;
106-
auto timeScope = TimeTraceScope(text("CTFE start: ", e.toChars()), e.toChars().to!string, e.loc);
107-
}
108-
else
109-
{
110103
import dmd.timetrace;
111104
timeTraceBeginEvent(TimeTraceEventType.ctfe);
112105
scope (exit) timeTraceEndEvent(TimeTraceEventType.ctfe, e);
113-
}
114106

115107
Expression result = interpret(e, null);
116108

@@ -461,17 +453,9 @@ private Expression interpretFunction(UnionExp* pue, FuncDeclaration fd, InterSta
461453
strbuf.write(")");
462454
return strbuf.extractSlice();
463455
};
464-
version (IN_LLVM) // TODO: required?
465-
{
466-
import driver.timetrace, std.format, std.conv;
467-
auto timeScope = TimeTraceScopeDelayedDetail(text("CTFE func: ", fd.toChars()), dlg, fd.loc);
468-
}
469-
else
470-
{
471456
import dmd.timetrace;
472457
timeTraceBeginEvent(TimeTraceEventType.ctfeCall);
473458
scope (exit) timeTraceEndEvent(TimeTraceEventType.ctfeCall, fd, dlg);
474-
}
475459

476460
void fdError(const(char)* msg)
477461
{

dmd/dsymbolsem.d

-10
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,10 @@ enum LOG = false;
9393
* Does semantic analysis on the public face of declarations.
9494
*/
9595
void dsymbolSemantic(Dsymbol dsym, Scope* sc)
96-
{
97-
version (IN_LLVM)
98-
{
99-
import driver.timetrace_sema;
100-
scope v = new DsymbolSemanticVisitor(sc);
101-
scope vtimetrace = new SemanticTimeTraceVisitor!DsymbolSemanticVisitor(v);
102-
dsym.accept(vtimetrace);
103-
}
104-
else
10596
{
10697
scope v = new DsymbolSemanticVisitor(sc);
10798
dsym.accept(v);
10899
}
109-
}
110100

111101
/***************************************************
112102
* Determine the numerical value of the AlignmentDeclaration

dmd/main.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ else
517517
import dmd.timetrace;
518518
version (IN_LLVM)
519519
{
520-
initializeTimeTrace(params.timeTraceGranularityUs, "ldc2");
520+
initializeTimeTrace(params.timeTraceGranularityUs, params.argv0.toCString.ptr);
521521
}
522522
else
523523
{

dmd/semantic2.d

-10
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,10 @@ enum LOG = false;
7777
* Does semantic analysis on initializers and members of aggregates.
7878
*/
7979
void semantic2(Dsymbol dsym, Scope* sc)
80-
{
81-
version (IN_LLVM)
82-
{
83-
import driver.timetrace_sema;
84-
scope v = new Semantic2Visitor(sc);
85-
scope vtimetrace = new SemanticTimeTraceVisitor!Semantic2Visitor(v);
86-
dsym.accept(vtimetrace);
87-
}
88-
else
8980
{
9081
scope v = new Semantic2Visitor(sc);
9182
dsym.accept(v);
9283
}
93-
}
9484

9585
private extern(C++) final class Semantic2Visitor : Visitor
9686
{

dmd/semantic3.d

-10
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,10 @@ enum LOG = false;
8080
* Does semantic analysis on function bodies.
8181
*/
8282
void semantic3(Dsymbol dsym, Scope* sc)
83-
{
84-
version (IN_LLVM)
85-
{
86-
import driver.timetrace_sema;
87-
scope v = new Semantic3Visitor(sc);
88-
scope vtimetrace = new SemanticTimeTraceVisitor!Semantic3Visitor(v);
89-
dsym.accept(vtimetrace);
90-
}
91-
else
9283
{
9384
scope v = new Semantic3Visitor(sc);
9485
dsym.accept(v);
9586
}
96-
}
9787

9888
private extern(C++) final class Semantic3Visitor : Visitor
9989
{

dmd/timetrace.d

+21-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TimeTraceProfiler* timeTraceProfiler = null;
3434
* timeGranularityUs = minimum event size in microseconds
3535
* processName = name of this executable
3636
*/
37-
extern (C++)
37+
extern (C++, "dmd")
3838
void initializeTimeTrace(uint timeGranularityUs, const(char)* processName)
3939
{
4040
assert(timeTraceProfiler is null, "Double initialization of timeTraceProfiler");
@@ -46,7 +46,7 @@ void initializeTimeTrace(uint timeGranularityUs, const(char)* processName)
4646
*
4747
* After this, no more calls to timeTrace functions can be made.
4848
*/
49-
extern (C++)
49+
extern (C++, "dmd")
5050
void deinitializeTimeTrace()
5151
{
5252
if (timeTraceProfilerEnabled())
@@ -60,7 +60,7 @@ void deinitializeTimeTrace()
6060
* Returns: Whether time tracing is enabled.
6161
*/
6262
pragma(inline, true)
63-
extern (C++)
63+
extern (C++, "dmd")
6464
bool timeTraceProfilerEnabled()
6565
{
6666
version (LDC)
@@ -80,7 +80,7 @@ bool timeTraceProfilerEnabled()
8080
* Params:
8181
* buf = output buffer to write JSON into
8282
*/
83-
extern (C++)
83+
extern (C++, "dmd")
8484
void writeTimeTraceProfile(OutBuffer* buf)
8585
{
8686
timeTraceProfiler.writeToBuffer(*buf);
@@ -94,7 +94,7 @@ void writeTimeTraceProfile(OutBuffer* buf)
9494
* detail_ptr = further details, visible when this event is selected
9595
* loc = source location corresponding to this event
9696
*/
97-
extern (C++)
97+
extern (C++, "dmd")
9898
void timeTraceBeginEvent(scope const(char)* name_ptr, scope const(char)* detail_ptr, Loc loc)
9999
{
100100
import dmd.root.rmem : xarraydup;
@@ -117,7 +117,7 @@ void timeTraceBeginEvent(scope const(char)* name_ptr, scope const(char)* detail_
117117
* eventType = what compilation stage the event belongs to
118118
* (redundant with the eventType of `timeTraceEndEvent` but used by GDC)
119119
*/
120-
extern (C++)
120+
extern (C++, "dmd")
121121
void timeTraceBeginEvent(TimeTraceEventType eventType)
122122
{
123123
if (timeTraceProfilerEnabled)
@@ -135,7 +135,7 @@ void timeTraceBeginEvent(TimeTraceEventType eventType)
135135
* e = Expression which was analyzed, used to generate 'name' and 'detail'
136136
* detail = custom lazy string for 'detail' of event
137137
*/
138-
extern (C++)
138+
extern (C++, "dmd")
139139
void timeTraceEndEvent(TimeTraceEventType eventType)
140140
{
141141
if (timeTraceProfilerEnabled)
@@ -157,14 +157,27 @@ void timeTraceEndEvent(TimeTraceEventType eventType, Dsymbol sym, scope const(ch
157157
}
158158

159159
/// ditto
160-
extern (C++)
160+
extern (C++, "dmd")
161161
void timeTraceEndEvent(TimeTraceEventType eventType, Expression e)
162162
{
163163
if (timeTraceProfilerEnabled)
164164
timeTraceProfiler.endScope(eventType, () => e.toChars().toDString(),
165165
() => e.toChars().toDString(), e.loc);
166166
}
167167

168+
version (IN_LLVM)
169+
{
170+
import dmd.func : FuncDeclaration;
171+
172+
extern (C++, "dmd")
173+
void timeTraceEndEvent(TimeTraceEventType eventType, FuncDeclaration fd)
174+
{
175+
if (timeTraceProfilerEnabled)
176+
timeTraceProfiler.endScope(eventType, () => fd.toChars().toDString(),
177+
() => fd.toPrettyChars().toDString(), fd.loc);
178+
}
179+
}
180+
168181
/// Identifies which compilation stage the event is associated to
169182
enum TimeTraceEventType
170183
{

dmd/timetrace.h

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/* Compiler implementation of the D programming language
3+
* Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
4+
* written by Walter Bright
5+
* https://www.digitalmars.com
6+
* Distributed under the Boost Software License, Version 1.0.
7+
* https://www.boost.org/LICENSE_1_0.txt
8+
* https://github.com/dlang/dmd/blob/master/compiler/src/dmd/timetrace.h
9+
*/
10+
11+
#pragma once
12+
13+
#include "dmd/globals.h"
14+
15+
class Expression;
16+
class FuncDeclaration;
17+
class OutBuffer;
18+
19+
enum class TimeTraceEventType
20+
{
21+
generic,
22+
parseGeneral,
23+
parse,
24+
semaGeneral,
25+
sema1Import,
26+
sema1Module,
27+
sema2,
28+
sema3,
29+
ctfe,
30+
ctfeCall,
31+
codegenGlobal,
32+
codegenModule,
33+
codegenFunction,
34+
link
35+
};
36+
37+
namespace dmd
38+
{
39+
void initializeTimeTrace(unsigned timeGranularityUs, const char *processName);
40+
void deinitializeTimeTrace();
41+
bool timeTraceProfilerEnabled();
42+
void writeTimeTraceProfile(OutBuffer *buf);
43+
44+
void timeTraceBeginEvent(const char *name_ptr, const char *detail_ptr, Loc loc);
45+
void timeTraceBeginEvent(TimeTraceEventType eventType);
46+
47+
void timeTraceEndEvent(TimeTraceEventType eventType);
48+
void timeTraceEndEvent(TimeTraceEventType eventType, Expression *e);
49+
#if IN_LLVM
50+
void timeTraceEndEvent(TimeTraceEventType eventType, FuncDeclaration *fd);
51+
#endif
52+
53+
54+
/// RAII helper class to call the begin and end functions of the time trace
55+
/// profiler. When the object is constructed, it begins the event; and when
56+
/// it is destroyed, it stops it.
57+
/// The strings pointed to are copied (pointers are not stored).
58+
struct TimeTraceScope
59+
{
60+
TimeTraceScope() = delete;
61+
TimeTraceScope(const TimeTraceScope &) = delete;
62+
TimeTraceScope &operator=(const TimeTraceScope &) = delete;
63+
TimeTraceScope(TimeTraceScope &&) = delete;
64+
TimeTraceScope &operator=(TimeTraceScope &&) = delete;
65+
66+
TimeTraceScope(const char *name, const char *detail = nullptr, Loc loc = Loc())
67+
: TimeTraceScope(TimeTraceEventType::generic, name, detail, loc)
68+
{}
69+
TimeTraceScope(TimeTraceEventType type, const char *name = nullptr, const char *detail = nullptr, Loc loc = Loc())
70+
: type(type)
71+
{
72+
if (timeTraceProfilerEnabled())
73+
timeTraceBeginEvent(name, detail, loc);
74+
}
75+
76+
~TimeTraceScope()
77+
{
78+
if (timeTraceProfilerEnabled())
79+
timeTraceEndEvent(type);
80+
}
81+
82+
private:
83+
TimeTraceEventType type = TimeTraceEventType::generic;
84+
};
85+
}

driver/archiver.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include "dmd/errors.h"
1111
#include "dmd/globals.h"
1212
#include "dmd/target.h"
13+
#include "dmd/timetrace.h"
1314
#include "driver/cl_options.h"
14-
#include "driver/timetrace.h"
1515
#include "driver/tool.h"
1616
#include "gen/logger.h"
1717
#if LDC_LLVM_VER < 1700
@@ -280,7 +280,7 @@ static std::string gStaticLibPath;
280280

281281
int createStaticLibrary() {
282282
Logger::println("*** Creating static library ***");
283-
::TimeTraceScope timeScope("Create static library");
283+
dmd::TimeTraceScope timeScope("Create static library");
284284

285285
const bool isTargetMSVC =
286286
global.params.targetTriple->isWindowsMSVCEnvironment();

driver/cl_options.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -677,18 +677,19 @@ cl::opt<CoverageIncrement> coverageIncrement(
677677
"Don't read, just set counter to 1")));
678678

679679
// Compilation time tracing options
680-
cl::opt<bool> fTimeTrace(
681-
"ftime-trace", cl::ZeroOrMore,
680+
static cl::opt<bool, true> fTimeTrace(
681+
"ftime-trace", cl::ZeroOrMore, cl::location(global.params.timeTrace),
682682
cl::desc("Turn on time profiler. Generates JSON file "
683683
"based on the output filename (also see --ftime-trace-file)."));
684-
cl::opt<unsigned> fTimeTraceGranularity(
685-
"ftime-trace-granularity", cl::ZeroOrMore, cl::init(500),
684+
static cl::opt<unsigned, true> fTimeTraceGranularity(
685+
"ftime-trace-granularity", cl::ZeroOrMore,
686+
cl::location(global.params.timeTraceGranularityUs),
686687
cl::desc(
687688
"Minimum time granularity (in microseconds) traced by time profiler"));
688689
cl::opt<std::string>
689-
fTimeTraceFile("ftime-trace-file",
690-
cl::desc("Specify time trace file destination"),
691-
cl::value_desc("filename"));
690+
fTimeTraceFile("ftime-trace-file",
691+
cl::desc("Specify time trace file destination"),
692+
cl::value_desc("filename"));
692693

693694
cl::opt<LTOKind> ltoMode(
694695
"flto", cl::ZeroOrMore, cl::desc("Set LTO mode, requires linker support"),

driver/cl_options.h

-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ enum class CoverageIncrement
121121
extern cl::opt<CoverageIncrement> coverageIncrement;
122122

123123
// Compilation time tracing options
124-
extern cl::opt<bool> fTimeTrace;
125124
extern cl::opt<std::string> fTimeTraceFile;
126-
extern cl::opt<unsigned> fTimeTraceGranularity;
127125

128126
// LTO options
129127
enum LTOKind {

driver/cpreprocessor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "driver/cpreprocessor.h"
22

33
#include "dmd/errors.h"
4+
#include "dmd/timetrace.h"
45
#include "driver/cl_options.h"
5-
#include "driver/timetrace.h"
66
#include "driver/tool.h"
77
#include "gen/irstate.h"
88
#include "llvm/Support/FileSystem.h"
@@ -76,7 +76,7 @@ FileName getOutputPath(const Loc &loc, const char *csrcfile) {
7676

7777
FileName runCPreprocessor(FileName csrcfile, const Loc &loc,
7878
OutBuffer &defines) {
79-
TimeTraceScope timeScope("Preprocess C file", csrcfile.toChars());
79+
dmd::TimeTraceScope timeScope("Preprocess C file", csrcfile.toChars(), loc);
8080

8181
const char *importc_h = getPathToImportc_h(loc);
8282

driver/linker.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include "dmd/errors.h"
1313
#include "dmd/target.h"
14+
#include "dmd/timetrace.h"
1415
#include "driver/cl_options.h"
15-
#include "driver/timetrace.h"
1616
#include "driver/tool.h"
1717
#include "gen/llvm.h"
1818
#include "gen/logger.h"
@@ -289,7 +289,7 @@ static std::string gExePath;
289289

290290
int linkObjToBinary() {
291291
Logger::println("*** Linking executable ***");
292-
TimeTraceScope timeScope("Linking executable");
292+
dmd::TimeTraceScope timeScope(TimeTraceEventType::link);
293293

294294
// remember output path for later
295295
gExePath = getOutputPath();
@@ -321,7 +321,7 @@ void deleteExeFile() {
321321
//////////////////////////////////////////////////////////////////////////////
322322

323323
int runProgram() {
324-
TimeTraceScope timeScope("Run user program");
324+
dmd::TimeTraceScope timeScope("Run user program");
325325

326326
assert(!gExePath.empty());
327327

0 commit comments

Comments
 (0)