Skip to content

Commit 2ada5be

Browse files
committed
feat: expose region profiling in DSL
Add `startProfiling` and `stopProfiling` with region names to DSL and runtime kernels. Update the profiling pass default region, and consolidate profiling docs.
1 parent dfadf67 commit 2ada5be

File tree

10 files changed

+64
-57
lines changed

10 files changed

+64
-57
lines changed

doc/Profiling.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@ limitations under the License.
1616

1717
# Profiling DAPHNE Using PAPI
1818

19-
You can profile your DaphneDSL script by using the `--enable-profiling` CLI
20-
switch.
19+
You can profile specific regions of your DaphneDSL script by inserting
20+
`startProfiling` and `stopProfiling` calls with a string region name.
2121
Note that profiling is only available if DAPHNE was built *without* the `--no-papi` flag.
2222

2323
DAPHNE supports profiling via the [PAPI](https://github.com/icl-utk-edu/papi)
2424
profiling library, specifically the
2525
[high-level (HL) PAPI API](https://github.com/icl-utk-edu/papi/wiki/PAPI-HL).
2626

27-
When run with profiling enabled, the DAPHNE compiler will generate code that
28-
automatically starts and stops profiling (via PAPI) at the start and end of the
29-
DaphneDSL script.
27+
For example, you can mark a region like this:
28+
29+
```r
30+
startProfiling("train");
31+
# ... computation ...
32+
stopProfiling("train");
33+
```
34+
The region name must match between the start and stop calls.
35+
36+
If you pass `--enable-profiling` and do not insert explicit profiling calls,
37+
the compiler will wrap the entire script in a default region named `script`.
3038

3139
You can configure which events to profile via the `PAPI_EVENTS`
3240
environment variable, e.g.:
3341

3442
```bash
35-
$ PAPI_EVENTS="perf::CYCLES,perf::INSTRUCTIONS,perf::CACHE-REFERENCES,perf::CACHE MISSES,perf::BRANCHES,perf::BRANCH-MI SSES" PAPI_REPORT=1 bin/daphne --enable-profiling script.daph
43+
$ PAPI_EVENTS="perf::CYCLES,perf::INSTRUCTIONS,perf::CACHE-REFERENCES,perf::CACHE MISSES,perf::BRANCHES,perf::BRANCH-MI SSES" PAPI_REPORT=1 bin/daphne script.daph
3644
```
3745

3846
For more details about the supported events as well as other PAPI-HL configuration

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ limitations under the License.
5656
- [Building DAPHNE with the build.sh script](/doc/development/BuildingDaphne.md)
5757
- [Building in Unsupported Environments](/doc/BuildEnvironment.md)
5858
- [Logging in DAPHNE](/doc/development/Logging.md)
59-
- [Profiling in DAPHNE](/doc/development/Profiling.md)
59+
- [Profiling in DAPHNE](/doc/Profiling.md)
6060
- [Testing](/doc/development/Testing.md)
6161
- [Installing Python Libraries in the `daphne-dev` Container](/doc/development/InstallPythonLibsInContainer.md)
6262

doc/development/Profiling.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ nav:
8080
- development/ExtendingSchedulingKnobs.md
8181
- development/HandlingPRs.md
8282
- development/ImplementBuiltinKernel.md
83-
- development/Profiling.md
8483
- development/WriteDocs.md
8584
- development/Testing.md
8685
- development/InstallPythonLibsInContainer.md

src/compiler/lowering/ProfilingPass.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,28 @@ struct ProfilingPass : public PassWrapper<ProfilingPass, OperationPass<func::Fun
3333

3434
void ProfilingPass::runOnOperation() {
3535
func::FuncOp f = getOperation();
36+
bool hasProfilingOps = false;
37+
f.walk([&](Operation *op) {
38+
if (isa<daphne::StartProfilingOp, daphne::StopProfilingOp>(op)) {
39+
hasProfilingOps = true;
40+
return WalkResult::interrupt();
41+
}
42+
return WalkResult::advance();
43+
});
44+
45+
if (hasProfilingOps)
46+
return;
47+
3648
Block &b = f.getBody().front();
3749

3850
OpBuilder builder(&b, b.begin());
3951
Location loc = builder.getUnknownLoc();
4052

41-
builder.create<daphne::StartProfilingOp>(loc);
53+
auto regionName =
54+
builder.create<daphne::ConstantOp>(loc, builder.getType<daphne::StringType>(), builder.getStringAttr("script"));
55+
builder.create<daphne::StartProfilingOp>(loc, regionName);
4256
builder.setInsertionPoint(b.getTerminator());
43-
builder.create<daphne::StopProfilingOp>(loc);
57+
builder.create<daphne::StopProfilingOp>(loc, regionName);
4458
}
4559

4660
std::unique_ptr<Pass> daphne::createProfilingPass() { return std::make_unique<ProfilingPass>(); }

src/ir/daphneir/DaphneOps.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,16 +1987,16 @@ def Daphne_DecRefOp : Daphne_Op<"decRef"> {
19871987
// ****************************************************************************
19881988

19891989
def Daphne_StartProfilingOp : Daphne_Op<"startProfiling"> {
1990-
let summary = "Starts profiling";
1990+
let summary = "Starts profiling for the given region name";
19911991

1992-
let arguments = (ins); // no arguments
1992+
let arguments = (ins StrScalar:$regionName);
19931993
let results = (outs); // no results
19941994
}
19951995

19961996
def Daphne_StopProfilingOp : Daphne_Op<"stopProfiling"> {
1997-
let summary = "Stops profiling";
1997+
let summary = "Stops profiling for the given region name";
19981998

1999-
let arguments = (ins); //no arguments
1999+
let arguments = (ins StrScalar:$regionName);
20002000
let results = (outs); // no results
20012001
}
20022002

src/parser/daphnedsl/DaphneDSLBuiltins.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const std::unordered_map<std::string, BuiltinParamInfo> &builtinParamInfos() {
9292
{"readFrame", {{"filename"}, 1}},
9393
{"receiveFromNumpy", {{"address", "rows", "cols", "valueType"}, 4}},
9494
{"stop", {{"message"}, 0}},
95+
{"startProfiling", {{"region"}, 1}},
96+
{"stopProfiling", {{"region"}, 1}},
9597
{"openFile", {{"filename"}, 1}},
9698
{"openDevice", {{"device"}, 1}},
9799
{"openFileOnTarget", {{"target", "filename"}, 2}},
@@ -1350,6 +1352,17 @@ antlrcpp::Any DaphneDSLBuiltins::build(mlir::Location loc, const std::string &fu
13501352
return builder.create<StopOp>(loc, message).getOperation();
13511353
}
13521354

1355+
if (func == "startProfiling") {
1356+
checkNumArgsExact(loc, func, numArgs, 1);
1357+
mlir::Value regionName = args[0];
1358+
return builder.create<StartProfilingOp>(loc, regionName).getOperation();
1359+
}
1360+
if (func == "stopProfiling") {
1361+
checkNumArgsExact(loc, func, numArgs, 1);
1362+
mlir::Value regionName = args[0];
1363+
return builder.create<StopProfilingOp>(loc, regionName).getOperation();
1364+
}
1365+
13531366
// --------------------------------------------------------------------
13541367
// Low-level
13551368
// --------------------------------------------------------------------

src/runtime/local/kernels/StartProfiling.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
// Convenience function
2727
// ****************************************************************************
2828

29-
void startProfiling(DCTX(ctx)) {
29+
inline void startProfiling(const char *regionName, DCTX(ctx)) {
3030
#ifdef USE_PAPI
31-
PAPI_hl_region_begin("fixme");
31+
PAPI_hl_region_begin(regionName);
3232
#else
3333
throw std::runtime_error("daphne was built without support for PAPI");
3434
#endif

src/runtime/local/kernels/StopProfiling.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
// Convenience function
2727
// ****************************************************************************
2828

29-
void stopProfiling(DCTX(ctx)) {
29+
inline void stopProfiling(const char *regionName, DCTX(ctx)) {
3030
#ifdef USE_PAPI
31-
PAPI_hl_region_end("fixme");
31+
PAPI_hl_region_end(regionName);
3232
#else
3333
throw std::runtime_error("daphne was built without support for PAPI");
3434
#endif

src/runtime/local/kernels/kernels.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6177,7 +6177,12 @@
61776177
"opName": "startProfiling",
61786178
"returnType": "void",
61796179
"templateParams": [],
6180-
"runtimeParams": []
6180+
"runtimeParams": [
6181+
{
6182+
"type": "const char *",
6183+
"name": "regionName"
6184+
}
6185+
]
61816186
},
61826187
"instantiations": [[]]
61836188
},
@@ -6187,7 +6192,12 @@
61876192
"opName": "stopProfiling",
61886193
"returnType": "void",
61896194
"templateParams": [],
6190-
"runtimeParams": []
6195+
"runtimeParams": [
6196+
{
6197+
"type": "const char *",
6198+
"name": "regionName"
6199+
}
6200+
]
61916201
},
61926202
"instantiations": [[]]
61936203
},

0 commit comments

Comments
 (0)