Skip to content

Commit

Permalink
Add dummy_calls to ensure query_stable and free_result not optimized …
Browse files Browse the repository at this point in the history
…out (#171)

* Fix warnings

* Fix typo

* Run libchdb.so test on linux arm64

* Print all global symbols

* Debug libchdb on macOS

* Add dummy_calls to ensure query_stable and free_result not optimized out
  • Loading branch information
auxten authored Dec 22, 2023
1 parent bb18fbd commit faee5df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,20 @@ jobs:
python3 -m pip install pandas pyarrow
bash -x ./chdb/test_smoke.sh
continue-on-error: false
- name: Show libchdb
- name: Debug libchdb
run: |
ls -lh
nm libchdb.so | grep query_stable || true
echo "Global Symbol in libchdb.so:"
nm -g libchdb.so || true
echo "Global Symbol in libclickhouse-local-chdb.a:"
nm -g buildlib/programs/local/libclickhouse-local-chdb.a || true
echo "Global Symbol in libclickhouse-local-lib.a:"
nm -g buildlib/programs/local/libclickhouse-local-lib.a || true
echo "pychdb_cmd.sh:"
cat buildlib/pychdb_cmd.sh
echo "libchdb_cmd.sh:"
cat buildlib/libchdb_cmd.sh
- name: Run libchdb stub in examples dir
run: |
bash -x ./examples/runStub.sh
Expand Down
22 changes: 22 additions & 0 deletions programs/local/LocalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,31 @@ void free_result(local_result * result)
result->_vec = nullptr;
}

/**
* The dummy_calls function is used to prevent certain functions from being optimized out by the compiler.
* It includes calls to 'query_stable' and 'free_result' within a condition that is always false.
* This approach ensures these functions are recognized as used by the compiler, particularly under high
* optimization levels like -O3, where unused functions might otherwise be discarded.
*
* Without this the Github runner macOS 12 builder will fail to find query_stable and free_result.
* It is strange because the same code works fine on my own macOS 12 x86_64 and arm64 machines.
*
* To prevent further clang or ClickHouse compile flags from affecting this, the function is defined here
* and called from mainEntryClickHouseLocal.
*/
bool always_false = false;
void dummy_calls()
{
if (always_false)
{
struct local_result * result = query_stable(0, nullptr);
free_result(result);
}
}

int mainEntryClickHouseLocal(int argc, char ** argv)
{
dummy_calls();
auto result = pyEntryClickHouseLocal(argc, argv);
if (result)
{
Expand Down
7 changes: 4 additions & 3 deletions programs/local/chdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ extern "C" {
# include <stdint.h>
#endif

struct __attribute__((visibility("default"))) local_result
#define CHDB_EXPORT __attribute__((visibility("default")))
struct CHDB_EXPORT local_result
{
char * buf;
size_t len;
Expand All @@ -19,8 +20,8 @@ struct __attribute__((visibility("default"))) local_result
uint64_t bytes_read;
};

__attribute__((visibility("default"))) struct local_result * query_stable(int argc, char ** argv);
__attribute__((visibility("default"))) void free_result(struct local_result * result);
CHDB_EXPORT struct local_result * query_stable(int argc, char ** argv);
CHDB_EXPORT void free_result(struct local_result * result);

#ifdef __cplusplus
}
Expand Down

0 comments on commit faee5df

Please sign in to comment.