Skip to content

Commit b93d93c

Browse files
jpienaarcopybara-github
authored andcommitted
[xls][mlir] Enable populating codegen metrics.
Plumb through callback to enable populating using the returned package and BlockMetricsProto. PiperOrigin-RevId: 714680081
1 parent a00d2c2 commit b93d93c

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

xls/contrib/mlir/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ cc_library(
541541
],
542542
deps = [
543543
":mlir_xls",
544+
"//xls/codegen:module_signature",
545+
"//xls/codegen:xls_metrics_cc_proto",
544546
"//xls/codegen/vast",
545547
"//xls/common/file:filesystem",
546548
"//xls/common/file:get_runfile_path",

xls/contrib/mlir/tools/xls_translate/xls_translate.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "mlir/include/mlir/Support/LLVM.h"
5959
#include "mlir/include/mlir/Support/LogicalResult.h"
6060
#include "mlir/include/mlir/Transforms/Passes.h"
61+
#include "xls/codegen/module_signature.h"
6162
#include "xls/codegen/vast/vast.h"
6263
#include "xls/common/file/filesystem.h"
6364
#include "xls/common/file/get_runfile_path.h"
@@ -1363,7 +1364,8 @@ LogicalResult setTop(Operation* op, std::string_view name, Package* package) {
13631364
} // namespace
13641365

13651366
LogicalResult MlirXlsToXlsTranslate(Operation* op, llvm::raw_ostream& output,
1366-
MlirXlsToXlsTranslateOptions options) {
1367+
MlirXlsToXlsTranslateOptions options,
1368+
MetricsReporter metrics_reporter) {
13671369
DslxPackageCache maybe_cache;
13681370
if (options.dslx_cache == nullptr) {
13691371
options.dslx_cache = &maybe_cache;
@@ -1416,6 +1418,20 @@ LogicalResult MlirXlsToXlsTranslate(Operation* op, llvm::raw_ostream& output,
14161418
return failure();
14171419
}
14181420

1421+
if (metrics_reporter) {
1422+
const ::xls::verilog::ModuleSignature signature =
1423+
xls_codegen_results.value().module_generator_result.signature;
1424+
if (signature.proto().has_metrics()) {
1425+
const ::xls::verilog::XlsMetricsProto& metrics =
1426+
signature.proto().metrics();
1427+
if (metrics.has_block_metrics()) {
1428+
const ::xls::verilog::BlockMetricsProto& block_metrics =
1429+
metrics.block_metrics();
1430+
metrics_reporter(**package, block_metrics);
1431+
}
1432+
}
1433+
}
1434+
14191435
output << xls_codegen_results->module_generator_result.verilog_text;
14201436
return success();
14211437
}

xls/contrib/mlir/tools/xls_translate/xls_translate.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "xls/tools/opt.h"
21
// Copyright 2024 The XLS Authors
32
//
43
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,10 +21,13 @@
2221
#include "absl/container/flat_hash_map.h"
2322
#include "absl/log/check.h"
2423
#include "absl/status/statusor.h"
24+
#include "llvm/include/llvm/ADT/STLFunctionalExtras.h"
2525
#include "llvm/include/llvm/ADT/StringRef.h"
2626
#include "mlir/include/mlir/Support/LLVM.h"
27+
#include "xls/codegen/xls_metrics.pb.h"
2728
#include "xls/tools/codegen_flags.h"
2829
#include "xls/tools/codegen_flags.pb.h"
30+
#include "xls/tools/opt.h"
2931
#include "xls/tools/scheduling_options_flags.h"
3032
#include "xls/tools/scheduling_options_flags.pb.h"
3133

@@ -89,9 +91,14 @@ struct MlirXlsToXlsTranslateOptions {
8991
DieUnlessOk(::xls::GetSchedulingOptionsFlagsProto());
9092
};
9193

94+
// Callback for reporting codegen metrics.
95+
using MetricsReporter = llvm::function_ref<void(
96+
const ::xls::Package&, const ::xls::verilog::BlockMetricsProto&)>;
97+
9298
// Translates an operation with XLS dialect to DSLX.
9399
LogicalResult MlirXlsToXlsTranslate(Operation* op, llvm::raw_ostream& output,
94-
MlirXlsToXlsTranslateOptions options = {});
100+
MlirXlsToXlsTranslateOptions options = {},
101+
MetricsReporter metrics_reporter = nullptr);
95102

96103
} // namespace mlir::xls
97104

xls/contrib/mlir/tools/xls_translate/xls_translate_main.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ llvm::cl::opt<bool> privatizeAndDceFunctions(
6464
"SymbolDCE first"),
6565
llvm::cl::init(false));
6666

67+
// NOLINTNEXTLINE
68+
llvm::cl::opt<bool> dumpCodegenMetrics(
69+
"dump-codegen-metrics",
70+
llvm::cl::desc("Whether to dump XLS codegen metric"),
71+
llvm::cl::init(false));
72+
6773
void registerInputDialects(DialectRegistry& registry) {
6874
// TODO(jpienaar): Registering all as start/prototyping.
6975
mlir::registerAllDialects(registry);
@@ -73,6 +79,23 @@ void registerInputDialects(DialectRegistry& registry) {
7379
mlir::tensor::TensorDialect>();
7480
}
7581

82+
static void printCodegenMetrics(
83+
const ::xls::Package& package,
84+
const ::xls::verilog::BlockMetricsProto& metrics) {
85+
llvm::errs() << "Generated XLS metrics:\n";
86+
llvm::errs() << " xls_node count: " << package.GetFunctionNodeCount()
87+
<< "\n";
88+
llvm::errs() << " xls_flop count: " << metrics.flop_count() << "\n";
89+
llvm::errs() << " xls_max_input_to_reg_delay_ps: "
90+
<< metrics.max_input_to_reg_delay_ps() << "\n";
91+
llvm::errs() << " xls_max_reg_to_reg_delay_ps: "
92+
<< metrics.max_reg_to_reg_delay_ps() << "\n";
93+
llvm::errs() << " xls_max_reg_to_output_delay_ps: "
94+
<< metrics.max_reg_to_output_delay_ps() << "\n";
95+
llvm::errs() << " xls_max_feedthrough_path_delay_ps: "
96+
<< metrics.max_feedthrough_path_delay_ps() << "\n";
97+
}
98+
7699
LogicalResult mlirXlsToXlsTranslate(Operation* op, llvm::raw_ostream& output) {
77100
MlirXlsToXlsTranslateOptions options;
78101
options.main_function = mainFunction;
@@ -90,6 +113,9 @@ LogicalResult mlirXlsToVerilogTranslate(Operation* op,
90113
options.optimize_ir = optimizeIr;
91114
options.dslx_search_path = dslxSearchPath;
92115
options.generate_verilog = true;
116+
if (dumpCodegenMetrics) {
117+
return MlirXlsToXlsTranslate(op, output, options, printCodegenMetrics);
118+
}
93119
return MlirXlsToXlsTranslate(op, output, options);
94120
}
95121

0 commit comments

Comments
 (0)