Skip to content

Commit

Permalink
Add perf benchmark utility. (#2977)
Browse files Browse the repository at this point in the history
* Add perf benchmark utility.

* Update documentation.

* Fix.

* Fix doc.

---------

Co-authored-by: Yong He <yhe@nvidia.com>
  • Loading branch information
csyonghe and Yong He authored Jul 11, 2023
1 parent d9c57e6 commit d0901aa
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build/visual-studio/core/core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
<ClInclude Include="..\..\..\source\core\slang-memory-file-system.h" />
<ClInclude Include="..\..\..\source\core\slang-name-value.h" />
<ClInclude Include="..\..\..\source\core\slang-offset-container.h" />
<ClInclude Include="..\..\..\source\core\slang-performance-profiler.h" />
<ClInclude Include="..\..\..\source\core\slang-persistent-cache.h" />
<ClInclude Include="..\..\..\source\core\slang-platform.h" />
<ClInclude Include="..\..\..\source\core\slang-process-util.h" />
Expand Down Expand Up @@ -360,6 +361,7 @@
<ClCompile Include="..\..\..\source\core\slang-memory-file-system.cpp" />
<ClCompile Include="..\..\..\source\core\slang-name-value.cpp" />
<ClCompile Include="..\..\..\source\core\slang-offset-container.cpp" />
<ClCompile Include="..\..\..\source\core\slang-performance-profiler.cpp" />
<ClCompile Include="..\..\..\source\core\slang-persistent-cache.cpp" />
<ClCompile Include="..\..\..\source\core\slang-platform.cpp" />
<ClCompile Include="..\..\..\source\core\slang-process-util.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions build/visual-studio/core/core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<ClInclude Include="..\..\..\source\core\slang-offset-container.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\core\slang-performance-profiler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\core\slang-persistent-cache.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -290,6 +293,9 @@
<ClCompile Include="..\..\..\source\core\slang-offset-container.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\core\slang-performance-profiler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\core\slang-persistent-cache.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions build/visual-studio/slang-rt/slang-rt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@
<ClInclude Include="..\..\..\source\core\slang-memory-file-system.h" />
<ClInclude Include="..\..\..\source\core\slang-name-value.h" />
<ClInclude Include="..\..\..\source\core\slang-offset-container.h" />
<ClInclude Include="..\..\..\source\core\slang-performance-profiler.h" />
<ClInclude Include="..\..\..\source\core\slang-persistent-cache.h" />
<ClInclude Include="..\..\..\source\core\slang-platform.h" />
<ClInclude Include="..\..\..\source\core\slang-process-util.h" />
Expand Down Expand Up @@ -373,6 +374,7 @@
<ClCompile Include="..\..\..\source\core\slang-memory-file-system.cpp" />
<ClCompile Include="..\..\..\source\core\slang-name-value.cpp" />
<ClCompile Include="..\..\..\source\core\slang-offset-container.cpp" />
<ClCompile Include="..\..\..\source\core\slang-performance-profiler.cpp" />
<ClCompile Include="..\..\..\source\core\slang-persistent-cache.cpp" />
<ClCompile Include="..\..\..\source\core\slang-platform.cpp" />
<ClCompile Include="..\..\..\source\core\slang-process-util.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions build/visual-studio/slang-rt/slang-rt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<ClInclude Include="..\..\..\source\core\slang-offset-container.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\core\slang-performance-profiler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\core\slang-persistent-cache.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -293,6 +296,9 @@
<ClCompile Include="..\..\..\source\core\slang-offset-container.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\core\slang-performance-profiler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\core\slang-persistent-cache.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
5 changes: 5 additions & 0 deletions docs/command-line-slangc-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ Treat the rest of the command line as input files.
Reports the time spent in the downstream compiler.


<a id="report-perf-benchmark"></a>
## -report-perf-benchmark
Reports compiler performance benchmark results.


<a id="source-embed-style-1"></a>
## -source-embed-style

Expand Down
3 changes: 3 additions & 0 deletions slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -4128,6 +4128,9 @@ namespace slang

virtual SLANG_NO_THROW void SLANG_MCALL setReportDownstreamTime(bool value) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL setReportPerfBenchmark(bool value) = 0;


};

#define SLANG_UUID_ICompileRequest ICompileRequest::getTypeGuid()
Expand Down
52 changes: 52 additions & 0 deletions source/core/slang-performance-profiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "slang-performance-profiler.h"
#include "slang-dictionary.h"

namespace Slang
{
struct FuncProfileInfo
{
int invocationCount = 0;
std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero();
};
class PerformanceProfilerImpl : public PerformanceProfiler
{
public:
OrderedDictionary<const char*, FuncProfileInfo> data;

virtual FuncProfileContext enterFunction(const char* funcName) override
{
auto entry = data.tryGetValue(funcName);
if (!entry)
{
data.add(funcName, FuncProfileInfo());
entry = data.tryGetValue(funcName);
}
entry->invocationCount++;
FuncProfileContext ctx;
ctx.funcName = funcName;
ctx.startTime = std::chrono::high_resolution_clock::now();
return ctx;
}
virtual void exitFunction(FuncProfileContext ctx) override
{
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = endTime - ctx.startTime;
auto entry = data.tryGetValue(ctx.funcName);
entry->duration += duration;
}
virtual void getResult(StringBuilder& out) override
{
for (auto func : data)
{
out << func.key << ": \t";
out << func.value.invocationCount << "\t" << func.value.duration.count()/1000000 << "\n";
}
}
};

PerformanceProfiler* Slang::PerformanceProfiler::getProfiler()
{
static PerformanceProfilerImpl profiler = PerformanceProfilerImpl();
return &profiler;
}
}
42 changes: 42 additions & 0 deletions source/core/slang-performance-profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef SLANG_CORE_PERFORMANCE_PROFILER_H
#define SLANG_CORE_PERFORMANCE_PROFILER_H

#include "slang-string.h"
#include <chrono>

namespace Slang
{

struct FuncProfileContext
{
const char* funcName = nullptr;
std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
};

class PerformanceProfiler
{
public:
virtual FuncProfileContext enterFunction(const char* funcName) = 0;
virtual void exitFunction(FuncProfileContext context) = 0;
virtual void getResult(StringBuilder& out) = 0;
public:
static PerformanceProfiler* getProfiler();
};

struct PerformanceProfilerFuncRAIIContext
{
FuncProfileContext context;
PerformanceProfilerFuncRAIIContext(const char* funcName)
{
context = PerformanceProfiler::getProfiler()->enterFunction(funcName);
}
~PerformanceProfilerFuncRAIIContext()
{
PerformanceProfiler::getProfiler()->exitFunction(context);
}
};

#define SLANG_PROFILE PerformanceProfilerFuncRAIIContext _profileContext(__func__)
}

#endif
4 changes: 4 additions & 0 deletions source/slang/slang-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,7 @@ namespace Slang
virtual SLANG_NO_THROW void SLANG_MCALL setDiagnosticFlags(SlangDiagnosticFlags flags) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL setDebugInfoFormat(SlangDebugInfoFormat format) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL setReportDownstreamTime(bool value) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL setReportPerfBenchmark(bool value) SLANG_OVERRIDE;
void setHLSLToVulkanLayoutOptions(int targetIndex, HLSLToVulkanLayoutOptions* vulkanLayoutOptions);

EndToEndCompileRequest(
Expand Down Expand Up @@ -2691,6 +2692,9 @@ namespace Slang
bool m_isCommandLineCompile = false;

bool m_reportDownstreamCompileTime = false;

// If set, will print out compiler performance benchmark results.
bool m_reportPerfBenchmark = false;

String m_diagnosticOutput;

Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ DIAGNOSTIC( 99, Error, unknownDebugOption, "unknown debug option, known optio
DIAGNOSTIC( 100, Error, failedToLoadDownstreamCompiler, "failed to load downstream compiler '$0'")
DIAGNOSTIC( 101, Error, downstreamCompilerDoesntSupportWholeProgramCompilation, "downstream compiler '$0' doesn't support whole program compilation")
DIAGNOSTIC( 102, Note, downstreamCompileTime, "downstream compile time: $0s")

DIAGNOSTIC( 103, Note, performanceBenchmarkResult, "compiler performance benchmark:\n$0")
DIAGNOSTIC(99999, Note, noteFailedToLoadDynamicLibrary, "failed to load dynamic library '$0'")

//
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "../compiler-core/slang-artifact-associated-impl.h"

#include "../core/slang-castable.h"
#include "../core/slang-performance-profiler.h"

#include <assert.h>

Expand Down Expand Up @@ -199,6 +200,7 @@ Result linkAndOptimizeIR(
LinkingAndOptimizationOptions const& options,
LinkedIR& outLinkedIR)
{
SLANG_PROFILE;
auto session = codeGenContext->getSession();
auto sink = codeGenContext->getSink();
auto target = codeGenContext->getTargetFormat();
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-ir-autodiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "slang-ir-single-return.h"
#include "slang-ir-ssa-simplification.h"
#include "slang-ir-validate.h"
#include "../core/slang-performance-profiler.h"

namespace Slang
{
Expand Down Expand Up @@ -1726,6 +1727,7 @@ bool processAutodiffCalls(
DiagnosticSink* sink,
IRAutodiffPassOptions const&)
{
SLANG_PROFILE;
bool modified = false;

// Create shared context for all auto-diff related passes
Expand Down
5 changes: 5 additions & 0 deletions source/slang/slang-ir-inline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "slang-ir-ssa-simplification.h"

#include "../core/slang-performance-profiler.h"
// This file provides general facilities for inlining function calls.

//
Expand Down Expand Up @@ -671,6 +672,8 @@ struct MandatoryEarlyInliningPass : InliningPassBase

void performMandatoryEarlyInlining(IRModule* module)
{
SLANG_PROFILE;

MandatoryEarlyInliningPass pass(module);
pass.considerAllCallSites();
}
Expand Down Expand Up @@ -785,6 +788,8 @@ struct ForceInliningPass : InliningPassBase

void performForceInlining(IRModule* module)
{
SLANG_PROFILE;

ForceInliningPass pass(module);
pass.considerAllCallSites();
}
Expand Down
5 changes: 5 additions & 0 deletions source/slang/slang-ir-legalize-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// that the concrete type of everything is known.

#include "../compiler-core/slang-name.h"
#include "../core/slang-performance-profiler.h"

#include "slang-ir.h"
#include "slang-ir-clone.h"
Expand Down Expand Up @@ -3843,6 +3844,8 @@ void legalizeResourceTypes(
IRModule* module,
DiagnosticSink* sink)
{
SLANG_PROFILE;

SLANG_UNUSED(sink);

IRResourceTypeLegalizationContext context(module);
Expand All @@ -3853,6 +3856,8 @@ void legalizeExistentialTypeLayout(
IRModule* module,
DiagnosticSink* sink)
{
SLANG_PROFILE;

SLANG_UNUSED(module);
SLANG_UNUSED(sink);

Expand Down
3 changes: 3 additions & 0 deletions source/slang/slang-ir-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "slang-ir-autodiff.h"
#include "slang-module-library.h"

#include "../core/slang-performance-profiler.h"
#include "../compiler-core/slang-artifact.h"

namespace Slang
Expand Down Expand Up @@ -1428,6 +1429,8 @@ static bool _isPublicOrHLSLExported(IRInst* inst)
LinkedIR linkIR(
CodeGenContext* codeGenContext)
{
SLANG_PROFILE;

auto linkage = codeGenContext->getLinkage();
auto program = codeGenContext->getProgram();
auto session = codeGenContext->getSession();
Expand Down
3 changes: 3 additions & 0 deletions source/slang/slang-ir-loop-unroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "slang-ir-util.h"
#include "slang-ir-simplify-cfg.h"
#include "slang-ir-dce.h"
#include "../core/slang-performance-profiler.h"

namespace Slang
{
Expand Down Expand Up @@ -508,6 +509,8 @@ bool unrollLoopsInFunc(

bool unrollLoopsInModule(IRModule* module, DiagnosticSink* sink)
{
SLANG_PROFILE;

for (auto inst : module->getGlobalInsts())
{
if (auto genFunc = as<IRGeneric>(inst))
Expand Down
4 changes: 3 additions & 1 deletion source/slang/slang-ir-lower-generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "slang-ir-witness-table-wrapper.h"
#include "slang-ir-ssa-simplification.h"
#include "slang-ir-util.h"

#include "../core/slang-performance-profiler.h"

namespace Slang
{
Expand Down Expand Up @@ -181,6 +181,8 @@ namespace Slang
IRModule* module,
DiagnosticSink* sink)
{
SLANG_PROFILE;

SharedGenericsLoweringContext sharedContext;
sharedContext.targetReq = targetReq;
sharedContext.module = module;
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-ir-specialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "slang-ir-ssa-simplification.h"
#include "slang-ir-lower-witness-lookup.h"
#include "slang-ir-dce.h"
#include "../core/slang-performance-profiler.h"

namespace Slang
{
Expand Down Expand Up @@ -2340,6 +2341,7 @@ bool specializeModule(
IRModule* module,
DiagnosticSink* sink)
{
SLANG_PROFILE;
SpecializationContext context;
context.module = module;
context.sink = sink;
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-ir-ssa-simplification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
#include "slang-ir-remove-unused-generic-param.h"
#include "slang-ir-redundancy-removal.h"
#include "slang-ir-propagate-func-properties.h"
#include "../core/slang-performance-profiler.h"

namespace Slang
{
// Run a combination of SSA, SCCP, SimplifyCFG, and DeadCodeElimination pass
// until no more changes are possible.
void simplifyIR(IRModule* module, DiagnosticSink* sink)
{
SLANG_PROFILE;
bool changed = true;
const int kMaxIterations = 8;
const int kMaxFuncIterations = 16;
Expand Down
Loading

0 comments on commit d0901aa

Please sign in to comment.