Skip to content

Commit

Permalink
Fix llvm-kompile pythonast (#1047)
Browse files Browse the repository at this point in the history
This PR addresses an issue experienced by @gtrepta and @theo25 where the
Python AST bindings for the LLVM backend incorrectly reference runtime
symbols like `FIRST_INJ_TAG` when loaded.

The solution is pretty straightforward; rather than trying to rely on
the linker implicitly stripping out undefined symbols when the library
is built, we just split the components into two parts (runtime and
non-runtime) and selectively include them.

The test case added in
0eb1d7f
fails before the script is amended.

---------

Co-authored-by: rv-jenkins <[email protected]>
  • Loading branch information
Baltoli and rv-jenkins authored May 8, 2024
1 parent ff5dd2e commit d80687a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
45 changes: 30 additions & 15 deletions bin/llvm-kompile-clang
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,38 @@ else
fi

if $link; then
runtime_components=(
"$LIBDIR"/libarithmetic.a
"$LIBDIR"/libutil.a
"$LIBDIR"/libstrings.a
"$LIBDIR"/libnumeric_strings.a
"$LIBDIR"/libio.a
"$LIBDIR"/libcollections.a
"$LIBDIR"/libBindingsCore.a
"$LIBDIR"/libKOREPrinter.a
"$LIBDIR"/liballoc.a
"$LIBDIR"/libcollect.a
"$LIBDIR"/libmeta.a
"$LIBDIR"/libjson.a
)

components=(
"$LIBDIR"/libParser.a
"$LIBDIR"/libAST.a
"$LIBDIR"/libBinaryKore.a
)

# If we're linking a Python AST bindings module, then we don't want to include
# the backend's runtime components. On some platforms and build type
# combinations, the linker won't strip undefined symbols for us, which
# produces an error on trying to load the Python library.
if [ "$main" != "python_ast" ]; then
components=("${components[@]}" "${runtime_components[@]}")
fi

run @CMAKE_CXX_COMPILER@ -Wno-override-module -Wno-return-type-c-linkage "$modopt" "${files[@]}" \
"$LIBDIR"/libarithmetic.a \
"$MAINFILES" \
"$LIBDIR"/libutil.a \
"$LIBDIR"/libstrings.a \
"$LIBDIR"/libnumeric_strings.a \
"$LIBDIR"/libio.a \
"$LIBDIR"/libcollections.a \
"$LIBDIR"/libParser.a \
"$LIBDIR"/libAST.a \
"$LIBDIR"/libBindingsCore.a \
"$LIBDIR"/libBinaryKore.a \
"$LIBDIR"/libKOREPrinter.a \
"$LIBDIR"/liballoc.a \
"$LIBDIR"/libcollect.a \
"$LIBDIR"/libmeta.a \
"$LIBDIR"/libjson.a \
"${components[@]}" \
"${flags[@]}" \
"${all_libraries[@]}" \
-I "$(dirname "$0")"/../include/kllvm \
Expand Down
6 changes: 6 additions & 0 deletions test/python/test_pythonast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# RUN: mkdir -p %t
# RUN: llvm-kompile pythonast --python %py-interpreter --python-output-dir %t
# RUN: cd %t
# RUN: PYTHONPATH=. %py-interpreter %s

import _kllvm

0 comments on commit d80687a

Please sign in to comment.