Skip to content

Commit

Permalink
Make the default dslx stdlib path an `inline constexpr std::string_vi…
Browse files Browse the repository at this point in the history
…ew`.

PiperOrigin-RevId: 678905008
  • Loading branch information
grebe authored and copybara-github committed Sep 26, 2024
1 parent d7f4738 commit a29cae6
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion xls/dslx/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ cc_library(

cc_library(
name = "default_dslx_stdlib_path",
srcs = ["default_dslx_stdlib_path.cc"],
srcs = ["default_dslx_stdlib_path.inc"],
hdrs = ["default_dslx_stdlib_path.h"],
)

Expand Down
3 changes: 2 additions & 1 deletion xls/dslx/cpp_transpiler/cpp_transpiler_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ ABSL_FLAG(std::string, namespaces, "",
"Double-colon-delimited namespaces with which to wrap the "
"generated code, e.g., \"my::namespace\" or "
"\"::my::explicitly::top::level::namespace\".");
ABSL_FLAG(std::string, dslx_stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library");
ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
Expand Down
11 changes: 8 additions & 3 deletions xls/dslx/default_dslx_stdlib_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
#ifndef XLS_DSLX_DEFAULT_DSLX_STDLIB_PATH_H_
#define XLS_DSLX_DEFAULT_DSLX_STDLIB_PATH_H_

namespace xls {
#include <string_view>
#include <type_traits>

extern const char* kDefaultDslxStdlibPath;
#include "xls/dslx/default_dslx_stdlib_path.inc"

} // namespace xls
// Check that the included file defines ::xls::kDefaultDslxStdlibPath as a
// std::string_view.
static_assert(std::is_same_v<
std::string_view,
std::remove_cvref_t<decltype(::xls::kDefaultDslxStdlibPath)>>);

#endif // XLS_DSLX_DEFAULT_DSLX_STDLIB_PATH_H_
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "xls/dslx/default_dslx_stdlib_path.h"
#include <string_view>

namespace xls {

const char* kDefaultDslxStdlibPath = "xls/dslx/stdlib";
inline constexpr std::string_view kDefaultDslxStdlibPath = "xls/dslx/stdlib";

} // namespace xls
3 changes: 2 additions & 1 deletion xls/dslx/interpreter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
// LINT.IfChange
ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
ABSL_FLAG(std::string, dslx_stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library directory.");
ABSL_FLAG(
std::string, format_preference, "",
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/ir_convert/ir_converter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ABSL_FLAG(std::string, top, "",
"top entity in the generated IR. When not provided, all functions "
"and procs are converted, there is no top entity defined in the "
"generated IR.");
ABSL_FLAG(std::string, stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, stdlib_path, std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library files.");
ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/lsp/dslx_ls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include "xls/dslx/default_dslx_stdlib_path.h"
#include "xls/dslx/lsp/language_server_adapter.h"

ABSL_FLAG(std::string, stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, stdlib_path, std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library files.");

static constexpr char kDslxPath[] = "DSLX_PATH";
Expand Down
3 changes: 2 additions & 1 deletion xls/dslx/prove_quickcheck_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@

ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
ABSL_FLAG(std::string, dslx_stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library directory.");
ABSL_FLAG(std::string, test_filter, "",
"Regexp that must be a full match of test name(s) to run.");
Expand Down
3 changes: 2 additions & 1 deletion xls/dslx/type_system/typecheck_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@

ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
ABSL_FLAG(std::string, dslx_stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library");
ABSL_FLAG(std::string, output_path, "",
"Path to dump the type information to as a protobin -- if not "
Expand Down
17 changes: 11 additions & 6 deletions xls/public/c_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ TEST(XlsCApiTest, ConvertDslxToIrSimple) {
const char* additional_search_paths[] = {};
char* error_out = nullptr;
char* ir_out = nullptr;
std::string dslx_stdlib_path = std::string(xls::kDefaultDslxStdlibPath);
bool ok =
xls_convert_dslx_to_ir(kProgram.c_str(), "my_module.x", "my_module",
/*dslx_stdlib_path=*/xls::kDefaultDslxStdlibPath,
/*dslx_stdlib_path=*/dslx_stdlib_path.c_str(),
additional_search_paths, 0, &error_out, &ir_out);

absl::Cleanup free_cstrs([&] {
Expand All @@ -67,10 +68,11 @@ TEST(XlsCApiTest, ConvertDslxToIrError) {
xls_c_str_free(ir_out);
});

const std::string dslx_stdlib_path = std::string(xls::kDefaultDslxStdlibPath);
bool ok = xls_convert_dslx_to_ir(
kInvalidProgram.c_str(), "my_module.x", "my_module",
/*dslx_stdlib_path=*/xls::kDefaultDslxStdlibPath, additional_search_paths,
0, &error_out, &ir_out);
/*dslx_stdlib_path=*/dslx_stdlib_path.c_str(), additional_search_paths, 0,
&error_out, &ir_out);
ASSERT_FALSE(ok);

// We should get an error and not get IR.
Expand All @@ -92,9 +94,11 @@ TEST(XlsCApiTest, ConvertDslxPathToIr) {
const char* additional_search_paths[] = {};
char* error_out = nullptr;
char* ir_out = nullptr;
const std::string dslx_stdlib_path = std::string(xls::kDefaultDslxStdlibPath);
bool ok = xls_convert_dslx_path_to_ir(
module_path.c_str(), /*dslx_stdlib_path=*/xls::kDefaultDslxStdlibPath,
additional_search_paths, 0, &error_out, &ir_out);
module_path.c_str(),
/*dslx_stdlib_path=*/dslx_stdlib_path.c_str(), additional_search_paths, 0,
&error_out, &ir_out);

absl::Cleanup free_cstrs([&] {
xls_c_str_free(error_out);
Expand Down Expand Up @@ -274,9 +278,10 @@ TEST(XlsCApiTest, InterpretDslxFailFunction) {
const char* additional_search_paths[] = {};
char* error = nullptr;
char* ir = nullptr;
const std::string dslx_stdlib_path = std::string(xls::kDefaultDslxStdlibPath);
ASSERT_TRUE(
xls_convert_dslx_to_ir(kDslxModule.c_str(), "my_module.x", "my_module",
/*dslx_stdlib_path=*/xls::kDefaultDslxStdlibPath,
/*dslx_stdlib_path=*/dslx_stdlib_path.c_str(),
additional_search_paths, 0, &error, &ir))
<< error;

Expand Down
3 changes: 2 additions & 1 deletion xls/tools/eval_ir_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ ABSL_FLAG(std::string, input_validator_expr, "",
ABSL_FLAG(std::string, input_validator_path, "",
"Path to a file containing DSLX for an input validator as with "
"the `--input_validator` flag.");
ABSL_FLAG(std::string, dslx_stdlib_path, xls::kDefaultDslxStdlibPath,
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library");
ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
Expand Down

0 comments on commit a29cae6

Please sign in to comment.