Skip to content

Commit

Permalink
[xls][mlir] Add CodegenFlags and SchedulingOptionsFlags to xls_transl…
Browse files Browse the repository at this point in the history
…ate's options struct

Also accept ABSL flags in the xls_translate binary.

This removes the half-implemented set of flags that we'd baked into xls_translate and allows
the user to provide their own flags on the command line.

PiperOrigin-RevId: 688161886
  • Loading branch information
James Molloy authored and copybara-github committed Oct 21, 2024
1 parent fd70f55 commit e3b4843
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion xls/contrib/mlir/testdata/integration/soft_blocks_lut.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: > %t

// RUN: xls/contrib/mlir/xls_opt --xls-lower %s \
// RUN: | xls/contrib/mlir/xls_translate --mlir-xls-to-verilog \
// RUN: | xls/contrib/mlir/xls_translate --mlir-xls-to-verilog -- --delay_model=asap7 --generator=combinational \
// RUN: | FileCheck --check-prefix=CHECK-VERILOG %s

// RUN: xls/tools/codegen_main %t \
Expand Down
4 changes: 2 additions & 2 deletions xls/contrib/mlir/testdata/translate_combinational.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: xls/contrib/mlir/xls_translate --mlir-xls-to-xls %s --main-function=combinational -- 2>&1 | FileCheck %s --dump-input-filter=all --check-prefix=XLS
// RUN: xls/contrib/mlir/xls_translate --mlir-xls-to-verilog %s --main-function=combinational -- 2>&1 | FileCheck %s --dump-input-filter=all --check-prefix=VERILOG-COMB
// RUN: xls/contrib/mlir/xls_translate --mlir-xls-to-xls %s --main-function=combinational -- --delay_model=asap7 --generator=combinational 2>&1 | FileCheck %s --dump-input-filter=all --check-prefix=XLS
// RUN: xls/contrib/mlir/xls_translate --mlir-xls-to-verilog %s --main-function=combinational -- --delay_model=asap7 --generator=combinational 2>&1 | FileCheck %s --dump-input-filter=all --check-prefix=VERILOG-COMB

// XLS: fn combinational([[ARG0:.*]]: bits[8]{{.*}}, [[ARG1:.*]]: bits[8]{{.*}}, [[ARG2:.*]]: bits[8]{{.*}}) -> bits[8] {
// XLS: [[DIFF:[a-z0-9.]+]]: bits[8] = sub([[ARG0]], [[ARG1]]
Expand Down
27 changes: 2 additions & 25 deletions xls/contrib/mlir/tools/xls_translate/xls_translate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1283,32 +1283,9 @@ LogicalResult MlirXlsToXlsTranslate(Operation* op, llvm::raw_ostream& output,
return success();
}

auto scheduling_options_flags_proto = ::xls::GetSchedulingOptionsFlagsProto();
if (!scheduling_options_flags_proto.ok()) {
llvm::errs() << "Failed to get scheduling options flags proto: "
<< scheduling_options_flags_proto.status().message() << "\n";
return failure();
}
auto codegen_flags_proto = ::xls::GetCodegenFlags();
if (!codegen_flags_proto.ok()) {
llvm::errs() << "Failed to get codegen flags proto: "
<< codegen_flags_proto.status().message() << "\n";
return failure();
}

if (options.pipeline_stages > 0) {
codegen_flags_proto->set_generator(::xls::GENERATOR_KIND_PIPELINE);
} else {
codegen_flags_proto->set_generator(::xls::GENERATOR_KIND_COMBINATIONAL);
}
codegen_flags_proto->set_register_merge_strategy(
::xls::RegisterMergeStrategyProto::STRATEGY_DONT_MERGE);
scheduling_options_flags_proto->set_delay_model(options.delay_model);
scheduling_options_flags_proto->set_pipeline_stages(options.pipeline_stages);
codegen_flags_proto->set_reset(options.reset_signal_name);

auto xls_codegen_results = ::xls::ScheduleAndCodegenPackage(
package->get(), *scheduling_options_flags_proto, *codegen_flags_proto,
package->get(), options.scheduling_options_flags_proto,
options.codegen_flags_proto,
/*with_delay_model=*/false);
if (!xls_codegen_results.ok()) {
llvm::errs() << "Failed to codegen: "
Expand Down
27 changes: 16 additions & 11 deletions xls/contrib/mlir/tools/xls_translate/xls_translate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
#include <string>

#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/status/statusor.h"
#include "llvm/include/llvm/ADT/StringRef.h"
#include "mlir/include/mlir/Support/LLVM.h"
#include "xls/tools/codegen_flags.h"
#include "xls/tools/codegen_flags.pb.h"
#include "xls/tools/scheduling_options_flags.h"
#include "xls/tools/scheduling_options_flags.pb.h"

namespace mlir {
class Operation;
Expand Down Expand Up @@ -50,6 +55,12 @@ class DslxPackageCache {
absl::flat_hash_map<std::string, std::shared_ptr<const ::xls::Package>> cache;
};

template <typename T>
T DieUnlessOk(const absl::StatusOr<T>& status_or) {
CHECK_OK(status_or.status());
return status_or.value();
}

struct MlirXlsToXlsTranslateOptions {
// The name of the main function to translate.
llvm::StringRef main_function = "";
Expand All @@ -69,17 +80,11 @@ struct MlirXlsToXlsTranslateOptions {
// Optional cache for DSLX translation results.
DslxPackageCache* dslx_cache = nullptr;

// Verilog emission options.

// The number of pipeline stages to (attempt to) generate. If zero, the
// combinational generator will be used.
int pipeline_stages = 0;

// The delay model to use.
std::string delay_model = "asap7";

// The name of the reset signal.
std::string reset_signal_name = "rst";
// Codegen options.
::xls::CodegenFlagsProto codegen_flags_proto =
DieUnlessOk(::xls::GetCodegenFlags());
::xls::SchedulingOptionsFlagsProto scheduling_options_flags_proto =
DieUnlessOk(::xls::GetSchedulingOptionsFlagsProto());
};

// Translates an operation with XLS dialect to DSLX.
Expand Down
20 changes: 18 additions & 2 deletions xls/contrib/mlir/tools/xls_translate/xls_translate_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ TranslateFromMLIRRegistration mlirXlsToVerilogTranslateRegistration(
} // namespace mlir::xls

int main(int argc, char** argv) {
xls_init_xls("Initializing XLS", 1, argv);
return failed(mlir::mlirTranslateMain(argc, argv, "XLS translator\n"));
// We allow ABSL flags to be passed to this binary after a double-dash:
// xls_translate ... -- --alsologtostderr
char** mlir_argv = argv;
char** absl_argv = argv;
int mlir_argc = argc, absl_argc = 1;
for (int i = 0; i < argc; ++i) {
if (std::string(argv[i]) == std::string("--")) {
// -- found; split into MLIR and ABSL args.
absl_argv = &argv[i]; // -- becomes argv[0] for absl.
mlir_argc = i;
absl_argc = argc - i;
break;
}
}
xls_init_xls("Initializing XLS", absl_argc, absl_argv);

return failed(
mlir::mlirTranslateMain(mlir_argc, mlir_argv, "XLS translator\n"));
}

0 comments on commit e3b4843

Please sign in to comment.