Skip to content

Add a few C APIs #2047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged

Add a few C APIs #2047

merged 2 commits into from
Apr 25, 2025

Conversation

benquike
Copy link
Contributor

No description provided.

@benquike
Copy link
Contributor Author

@cdleary
Can you please take a look?

Copy link
Collaborator

@cdleary cdleary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good -- just the only thing I think should change is that the API should offer an xls_bytes_free to do the delete[] of the xls_bits_to_bytes() result under the hood.

With that in place all looks good!

I'm not sure why the CLA scan didn't happen automatically but I think to land the change I imagine you'll need to do https://cla.developers.google.com/about/google-individual

const auto* cpp_bits = reinterpret_cast<const xls::Bits*>(bits);
auto bytes = cpp_bits->ToBytes();
*byte_count_out = bytes.size();
*bytes_out = (uint8_t*)malloc(bytes.size());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need a xls_bytes_free() for this (because the libxls allocator can be different from the surrounding process so you can't assume the surrounding process can use the same free()), and generally I think we've been doing new[] inside the impl e.g. https://sourcegraph.com/github.com/google/xls@ef07f4bc7db8df70dc32e52a04d268653b58847a/-/blob/xls/public/c_api_impl_helpers.cc?L72

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, can you take another look?

@benquike benquike force-pushed the main branch 3 times, most recently from 19089f8 to 03d519c Compare April 23, 2025 16:21
@benquike benquike requested a review from cdleary April 23, 2025 16:26
@benquike
Copy link
Contributor Author

Looks good -- just the only thing I think should change is that the API should offer an xls_bytes_free to do the delete[] of the xls_bits_to_bytes() result under the hood.

With that in place all looks good!

I'm not sure why the CLA scan didn't happen automatically but I think to land the change I imagine you'll need to do https://cla.developers.google.com/about/google-individual

I am a Googler thus have signed CLA.

@@ -199,6 +213,21 @@ bool xls_bits_eq(const struct xls_bits* a, const struct xls_bits* b);
// returns 1, `xls_bits_get_bit(bits, 1)` returns 0.
bool xls_bits_get_bit(const struct xls_bits* bits, int64_t index);

// Returns the bytes in the given bits value. The caller must
// free the returned `bytes_out` pointer via `free`, The caller
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be freed via xls_bytes_free?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

// Converts the given bits value to a signed or unsigned integer 64-bit integer
// value. if the conversion is not possible, false is returned and `error_out`
// contains the error message and the caller must free the `value_out` pointer
// itself via `free`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the success case, no memory is returned, so there is nothing to free, right?

Copy link
Contributor Author

@benquike benquike Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc in comment updated.

@benquike benquike force-pushed the main branch 2 times, most recently from e205c20 to 2eadc0f Compare April 24, 2025 16:03
Copy link
Contributor

@ingomueller-net ingomueller-net left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Disclaimer: I am not familiar with the coding conventions of this project. Some of the code does not respect the Google style guide (like the use of auto) but similar patterns are used in the rest of the file so I guess it's fine.

uint8_t** bytes_out, size_t* byte_count_out);

// Converts the given bits value to a signed or unsigned integer 64-bit integer
// value. if the conversion is not possible, false is returned, `error_out`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultra-nit:

Suggested change
// value. if the conversion is not possible, false is returned, `error_out`
// value. If the conversion is not possible, false is returned, `error_out`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@benquike
Copy link
Contributor Author

LGTM!

Disclaimer: I am not familiar with the coding conventions of this project. Some of the code does not respect the Google style guide (like the use of auto) but similar patterns are used in the rest of the file so I guess it's fine.

It would be nice to have a lint tool for style conformance.

@benquike
Copy link
Contributor Author

@cdleary can you take another look and if it is OK, can we proceed with the merge?

Add xls_value_get_kind to allow inspecting the type of a Value object.

Signed-off-by: Hui Peng <[email protected]>
Copy link
Collaborator

@cdleary cdleary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! You may want to check that this has nothing flagged under bazel test -c opt --config=asan or it may hit a snag when folks attempt to import it to the repo.

CHECK(error_out != nullptr);
CHECK(byte_count_out != nullptr);
const auto* cpp_bits = reinterpret_cast<const xls::Bits*>(bits);
auto bytes = cpp_bits->ToBytes();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the type is not obvious from the right hand side expression this auto should probably be elided. (That's the general C++ style guide guideline.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the type is not obvious from the right hand side expression this auto should probably be elided. (That's the general C++ style guide guideline.)

Fixed.

@cdleary
Copy link
Collaborator

cdleary commented Apr 25, 2025

Disclaimer: I am not familiar with the coding conventions of this project. Some of the code does not respect the Google style guide (like the use of auto) but similar patterns are used in the rest of the file so I guess it's fine.

Yeah this is a good point, an auto for a routine called ToUint64() that returns a StatusOr<uint64_t> is a bit on the edge of the guideline in my experience, if you wanted to be sure you could elide auto for those too.

Thanks for helping build out this API!

cc @meheff @richmckeever and team to tag for import.

@benquike
Copy link
Contributor Author

LGTM! You may want to check that this has nothing flagged under bazel test -c opt --config=asan or it may hit a snag when folks attempt to import it to the repo.

Can we add a check in the CI?

@benquike
Copy link
Contributor Author

Yeah this is a good point, an auto for a routine called ToUint64() that returns a StatusOr<uint64_t> is a bit on the edge of the guideline in my experience, if you wanted to be sure you could elide auto for those too.

Fixed

@benquike
Copy link
Contributor Author

LGTM! You may want to check that this has nothing flagged under bazel test -c opt --config=asan or it may hit a snag when folks attempt to import it to the repo.

I got the following odr violation, is it expected?

=================================================================
==3602574==ERROR: AddressSanitizer: odr-violation (0x7f8181685480):

These globals were registered at these points:
[1]:
#0 0x55f14168b7a6 in __asan_register_globals /tmp/final/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:369:3
#1 0x55f14168c8c9 in __asan_register_elf_globals /tmp/final/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:352:3
#2 0x7f81a6188f4d in call_init elf/dl-init.c:74:3
#3 0x7f81a6188f4d in call_init elf/dl-init.c:26:1

[2]:
#0 0x55f14168b7a6 in __asan_register_globals /tmp/final/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:369:3
#1 0x55f14168c8c9 in __asan_register_elf_globals /tmp/final/llvm-project/compiler-rt/lib/asan/asan_globals.cpp:352:3
#2 0x7f81a6188f4d in call_init elf/dl-init.c:74:3
#3 0x7f81a6188f4d in call_init elf/dl-init.c:26:1

==3602574==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
SUMMARY: AddressSanitizer: odr-violation: global 'deflate_copyright' at /proc/self/cwd/external/llvm_zlib/deflate.c:55 in

@cdleary
Copy link
Collaborator

cdleary commented Apr 25, 2025

I got the following odr violation, is it expected?

Yes I get that as well, use the env var to disable it as it suggests: ASAN_OPTIONS=detect_odr_violation=0

@cdleary
Copy link
Collaborator

cdleary commented Apr 25, 2025

LGTM! You may want to check that this has nothing flagged under bazel test -c opt --config=asan or it may hit a snag when folks attempt to import it to the repo.

Can we add a check in the CI?

#1447 is the issue that tracks this but I'm not sure there's active movement on it

@benquike
Copy link
Contributor Author

I got the following odr violation, is it expected?

Yes I get that as well, use the env var to disable it as it suggests: ASAN_OPTIONS=detect_odr_violation=0

I ran the following command and all tests passed

ASAN_OPTIONS=detect_odr_violation=0 bazel run -c opt --config=asan //xls/public:c_api_test

@cdleary
Copy link
Collaborator

cdleary commented Apr 25, 2025

I ran the following command and all tests passed

ASAN_OPTIONS=detect_odr_violation=0 bazel run -c opt --config=asan //xls/public:c_api_test

Awesome! I expect the google XLS team build gardener should be able to merge successfully then.

@benquike Just out of curiosity are you building out the C APIs for use in the Rust crate? (https://github.com/xlsynth/xlsynth-crate) Or a different use case?

Copy link
Collaborator

@erinzmoore erinzmoore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG overall. Just the comment on auto from cdleary@ to address and then can merge.

- xls_bits_to_bytes
- xls_bits_to_uint64
- xls_bits_to_int64

Signed-off-by: Hui Peng <[email protected]>
@benquike
Copy link
Contributor Author

benquike commented Apr 25, 2025

@benquike Just out of curiosity are you building out the C APIs for use in the Rust crate? (https://github.com/xlsynth/xlsynth-crate) Or a different use case?

No, We are using these APIs to create python bindings.
do you want me to add it in rust binding as well?

@benquike benquike requested a review from erinzmoore April 25, 2025 15:40
@copybara-service copybara-service bot merged commit b0d5ab7 into google:main Apr 25, 2025
6 checks passed
@cdleary
Copy link
Collaborator

cdleary commented Apr 26, 2025

@benquike Just out of curiosity are you building out the C APIs for use in the Rust crate? (https://github.com/xlsynth/xlsynth-crate) Or a different use case?

No, We are using these APIs to create python bindings. do you want me to add it in rust binding as well?

Nah it's ok was just curious, I can add them there no problem, look forward to the Python bindings if you'll be doing them in open source!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants