Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --ftime-trace functionality. #3624

Merged
merged 5 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ else()
endif()
message(STATUS "LDC version identifier: ${LDC_VERSION}")
configure_file(driver/ldc-version.cpp.in driver/ldc-version.cpp)
configure_file(driver/ldc_version.d.in driver/ldc_version.d)

# Also add the header files to the build so that they are available in IDE
# project files generated via CMake.
Expand All @@ -377,6 +378,7 @@ set(DRV_SRC
driver/dcomputecodegenerator.cpp
driver/exe_path.cpp
driver/targetmachine.cpp
driver/timetrace.cpp
driver/toobj.cpp
driver/tool.cpp
driver/archiver.cpp
Expand Down Expand Up @@ -404,6 +406,7 @@ set(DRV_HDR
driver/linker.h
driver/plugins.h
driver/targetmachine.h
driver/timetrace.h
driver/toobj.h
driver/tool.h
)
Expand Down
10 changes: 10 additions & 0 deletions dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,20 @@ private uint setMangleOverride(Dsymbol s, const(char)[] sym)
* Does semantic analysis on the public face of declarations.
*/
extern(C++) void dsymbolSemantic(Dsymbol dsym, Scope* sc)
{
version (IN_LLVM)
{
import driver.timetrace_sema;
scope v = new DsymbolSemanticVisitor(sc);
scope vtimetrace = new SemanticTimeTraceVisitor!DsymbolSemanticVisitor(v);
dsym.accept(vtimetrace);
}
else
{
scope v = new DsymbolSemanticVisitor(sc);
dsym.accept(v);
}
}

structalign_t getAlignment(AlignDeclaration ad, Scope* sc)
{
Expand Down
10 changes: 10 additions & 0 deletions dmd/semantic2.d
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,20 @@ enum LOG = false;
* Does semantic analysis on initializers and members of aggregates.
*/
extern(C++) void semantic2(Dsymbol dsym, Scope* sc)
{
version (IN_LLVM)
{
import driver.timetrace_sema;
scope v = new Semantic2Visitor(sc);
scope vtimetrace = new SemanticTimeTraceVisitor!Semantic2Visitor(v);
dsym.accept(vtimetrace);
}
else
{
scope v = new Semantic2Visitor(sc);
dsym.accept(v);
}
}

private extern(C++) final class Semantic2Visitor : Visitor
{
Expand Down
10 changes: 10 additions & 0 deletions dmd/semantic3.d
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,20 @@ enum LOG = false;
* Does semantic analysis on function bodies.
*/
extern(C++) void semantic3(Dsymbol dsym, Scope* sc)
{
version (IN_LLVM)
{
import driver.timetrace_sema;
scope v = new Semantic3Visitor(sc);
scope vtimetrace = new SemanticTimeTraceVisitor!Semantic3Visitor(v);
dsym.accept(vtimetrace);
}
else
{
scope v = new Semantic3Visitor(sc);
dsym.accept(v);
}
}

private extern(C++) final class Semantic3Visitor : Visitor
{
Expand Down
2 changes: 2 additions & 0 deletions driver/archiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "dmd/errors.h"
#include "dmd/globals.h"
#include "driver/cl_options.h"
#include "driver/timetrace.h"
#include "driver/tool.h"
#include "gen/logger.h"
#include "llvm/ADT/Triple.h"
Expand Down Expand Up @@ -240,6 +241,7 @@ static llvm::cl::opt<std::string> ar("ar", llvm::cl::desc("Archiver"),

int createStaticLibrary() {
Logger::println("*** Creating static library ***");
::TimeTraceScope timeScope("Create static library");

const bool isTargetMSVC =
global.params.targetTriple->isWindowsMSVCEnvironment();
Expand Down
14 changes: 14 additions & 0 deletions driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,20 @@ static cl::opt<DummyDataType, false, CoverageParser> coverageAnalysis(
"Use -cov=<n> for n% minimum required coverage\n"
"Use -cov=ctfe to include code executed during CTFE"));

// Compilation time tracing options
cl::opt<bool> fTimeTrace(
"ftime-trace", cl::ZeroOrMore,
cl::desc("Turn on time profiler. Generates JSON file "
"based on the output filename (also see --ftime-trace-file)."));
cl::opt<unsigned> fTimeTraceGranularity(
"ftime-trace-granularity", cl::ZeroOrMore,
cl::desc(
"Minimum time granularity (in microseconds) traced by time profiler"));
cl::opt<std::string>
fTimeTraceFile("ftime-trace-file",
cl::desc("Specify time trace file destination"),
cl::value_desc("filename"));

cl::opt<LTOKind> ltoMode(
"flto", cl::ZeroOrMore, cl::desc("Set LTO mode, requires linker support"),
cl::init(LTO_None),
Expand Down
5 changes: 5 additions & 0 deletions driver/cl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ extern std::vector<std::string> debugArgs;
void createClashingOptions();
void hideLLVMOptions();

// Compilation time tracing options
extern cl::opt<bool> fTimeTrace;
extern cl::opt<std::string> fTimeTraceFile;
extern cl::opt<unsigned> fTimeTraceGranularity;

// LTO options
enum LTOKind {
LTO_None,
Expand Down
12 changes: 12 additions & 0 deletions driver/ldc_version.d.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- driver/ldc_version.d - ------------------------------------*- D -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//

module driver.ldc_version;

enum LLVM_VERSION_MAJOR = @LLVM_VERSION_MAJOR@;
4 changes: 4 additions & 0 deletions driver/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "dmd/errors.h"
#include "driver/cl_options.h"
#include "driver/timetrace.h"
#include "driver/tool.h"
#include "gen/llvm.h"
#include "gen/logger.h"
Expand Down Expand Up @@ -284,6 +285,7 @@ static std::string gExePath;

int linkObjToBinary() {
Logger::println("*** Linking executable ***");
TimeTraceScope timeScope("Linking executable");

// remember output path for later
gExePath = getOutputName();
Expand All @@ -310,6 +312,8 @@ void deleteExeFile() {
//////////////////////////////////////////////////////////////////////////////

int runProgram() {
TimeTraceScope timeScope("Run user program");

assert(!gExePath.empty());

// Run executable
Expand Down
29 changes: 24 additions & 5 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "driver/linker.h"
#include "driver/plugins.h"
#include "driver/targetmachine.h"
#include "driver/timetrace.h"
#include "gen/abi.h"
#include "gen/cl_helpers.h"
#include "gen/irstate.h"
Expand Down Expand Up @@ -989,6 +990,8 @@ int cppmain() {
fatal();
}

initializeTimeTracer();

// Set up the TargetMachine.
const auto arch = getArchStr();
if ((m32bits || m64bits) && (!arch.empty() || !mTargetTriple.empty())) {
Expand Down Expand Up @@ -1075,17 +1078,26 @@ int cppmain() {

loadAllPlugins();

Strings libmodules;
const int rc = mars_mainBody(global.params, files, libmodules);
int status;
{
TimeTraceScope timeScope("ExecuteCompiler");
Strings libmodules;
status = mars_mainBody(global.params, files, libmodules);
}

writeTimeTraceProfile();
deinitializeTimeTracer();

llvm::llvm_shutdown();

return rc;
return status;
}

void codegenModules(Modules &modules) {
// Generate one or more object/IR/bitcode files/dcompute kernels.
if (global.params.obj && !modules.empty()) {
TimeTraceScope timeScope("Codegen all modules");

#if LDC_MLIR_ENABLED
mlir::MLIRContext mlircontext;
ldc::CodeGenerator cg(getGlobalContext(), mlircontext,
Expand Down Expand Up @@ -1115,6 +1127,7 @@ void codegenModules(Modules &modules) {
const auto atCompute = hasComputeAttr(m);
if (atCompute == DComputeCompileFor::hostOnly ||
atCompute == DComputeCompileFor::hostAndDevice) {
TimeTraceScope timeScope(("Codegen module " + llvm::SmallString<20>(m->toChars())).str());
#if LDC_MLIR_ENABLED
if (global.params.output_mlir == OUTPUTFLAGset)
cg.emitMLIR(m);
Expand All @@ -1140,8 +1153,11 @@ void codegenModules(Modules &modules) {
}

if (!computeModules.empty()) {
for (auto &mod : computeModules)
TimeTraceScope timeScope("Codegen DCompute device modules");
for (auto &mod : computeModules) {
TimeTraceScope timeScope(("Codegen DCompute device module " + llvm::SmallString<20>(mod->toChars())).str());
dccg.emit(mod);
}
}
dccg.writeModules();

Expand All @@ -1150,7 +1166,10 @@ void codegenModules(Modules &modules) {
global.params.link = false;
}

cache::pruneCache();
{
TimeTraceScope timeScope("Prune object file cache");
cache::pruneCache();
}

freeRuntime();
}
61 changes: 61 additions & 0 deletions driver/timetrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===-- driver/timetrace.cpp ------------------------------------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Compilation time tracing support implementation, --ftime-trace.
//
//===----------------------------------------------------------------------===//

#include "driver/timetrace.h"

#if LDC_WITH_TIMETRACER

#include "dmd/errors.h"
#include "driver/cl_options.h"
#include "llvm/Support/TimeProfiler.h"

void initializeTimeTracer() {
if (opts::fTimeTrace) {
llvm::timeTraceProfilerInitialize(opts::fTimeTraceGranularity,
opts::allArguments[0]);
}
}

void deinitializeTimeTracer() {
if (opts::fTimeTrace) {
llvm::timeTraceProfilerCleanup();
}
}

void writeTimeTraceProfile() {
if (llvm::timeTraceProfilerEnabled()) {
std::string filename = opts::fTimeTraceFile;
if (filename.empty()) {
filename = global.params.objfiles[0] ? global.params.objfiles[0] : "out";
filename += ".time-trace";
}

std::error_code err;
llvm::raw_fd_ostream outputstream(filename, err, llvm::sys::fs::OF_Text);
if (err) {
error(Loc(), "Error writing Time Trace profile: could not open %s",
filename.c_str());
return;
}

llvm::timeTraceProfilerWrite(outputstream);
}
}

void timeTraceProfilerBegin(size_t name_length, const char *name_ptr,
size_t detail_length, const char *detail_ptr) {
llvm::timeTraceProfilerBegin(llvm::StringRef(name_ptr, name_length),
llvm::StringRef(detail_ptr, detail_length));
}

#endif
Loading