Skip to content

Commit

Permalink
refactor(ci): llvm components mapping && mac minimize build size
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Aug 7, 2024
1 parent a74d7ee commit 2d787ea
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 5 deletions.
22 changes: 18 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,33 @@ nanobind_add_module(
${SOURCES}
)

llvm_map_components_to_libnames(llvm_libs core transformutils analysis support
target bitwriter bitreader)

# Link against LLVM libraries
# # note: code references llvmlite
if ($ENV{LLVMPYM_SHARED})
set(llvm_libs LLVM)
else()
set(llvm_libs ${LLVM_AVAILABLE_LIBS})
# List of libraries that are part of the monolithic LLVM shared library (possibly)
# and should be removed. They will cause
# CommandLine Error: Option 'verify-dom-info' registered more than once!
# error when not removed
list(REMOVE_ITEM llvm_libs LLVMTableGenCommon LLVMDebuginfod LTO LLVMCFIVerify LLVMDiff Remarks LLVM)
endif()

message(INFO " ${llvm_libs}")

target_link_libraries(llvmpym_ext PRIVATE ${llvm_libs} fmt::fmt)

# -flto and --exclude-libs allow us to remove those parts of LLVM we don't use
# although we are going build a full llvm python binding :-
# TODO for MacOS immitate llvmlite only expose certain symbols. Can it work?
# https://github.com/numba/llvmlite/blob/78ebf9bf188379b2642112aff388480384306c6b/ffi/CMakeLists.txt#L76C53-L76C61

if(UNIX AND CMAKE_BUILD_TYPE STREQUAL "Release")
set_property(TARGET llvmpym_ext APPEND_STRING PROPERTY LINK_FLAGS "-flto")
if(NOT APPLE)
if(APPLE)
set_property(TARGET llvmpym_ext APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-dead_strip")
else()
set_property(TARGET llvmpym_ext APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--exclude-libs,ALL")
endif()
endif()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Past versions of document can be downloaded on the Release Page.
## Related Projects

[lliv](https://codeberg.org/meow_king/lliv): An interactive LLVM IR viewer based on llvmpym.

3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

llvm_18
cmake # need for finding ZLIB::ZLIB LLVM cmake dependency
libxml2
libffi
libpfm
];
};
});
Expand Down
13 changes: 13 additions & 0 deletions src/llvm/Disassembler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Disassembler.h"

#include <nanobind/nanobind.h>
#include <llvm-c/Disassembler.h>
#include "types_priv.h"

namespace nb = nanobind;
using namespace nb::literals;

void populateDisassembler(nb::module_ &m) {
// m.def("create_disasm")
}

9 changes: 9 additions & 0 deletions src/llvm/Disassembler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LLVMPYM_DISASSEMBLER_H
#define LLVMPYM_DISASSEMBLER_H

#include <nanobind/nanobind.h>

void populateDisassembler(nanobind::module_ &m);


#endif
5 changes: 4 additions & 1 deletion src/llvm/types_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "types_priv/PymTargetMachine.h"
#include "types_priv/PymTargetMachineOptions.h"
#include "types_priv/PymLLVMObject.h"
#include "types_priv/PymDisasmContext.h"


#define DEFINE_PY_WRAPPER_CLASS(ClassName, UnderlyingType) \
Expand Down Expand Up @@ -292,7 +293,9 @@ enum class PymLLVMFastMathFlags {
BIND_PYLLVMOBJECT_(PymTargetMachineOptions, LLVMTargetMachineOptionsRef, \
PymTargetMachineOptionsObject) \
\
BIND_PYLLVMOBJECT_(PymTargetData, LLVMTargetDataRef, PymTargetDataObject)
BIND_PYLLVMOBJECT_(PymTargetData, LLVMTargetDataRef, PymTargetDataObject) \
\
BIND_PYLLVMOBJECT_(PymDisasmContext, LLVMDisasmContextRef, PymDisasmContextObject)


// Core --------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions src/llvm/types_priv/PymDisasmContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "PymDisasmContext.h"
#include <llvm-c/Disassembler.h>

PymDisasmContext::PymDisasmContext(LLVMDisasmContextRef obj)
: obj(get_shared_obj(obj)) { }

LLVMDisasmContextRef PymDisasmContext::get() const {
return obj.get();
}

SHARED_POINTER_IMPL(PymDisasmContext, LLVMDisasmContextRef, void,
LLVMDisasmDispose)


21 changes: 21 additions & 0 deletions src/llvm/types_priv/PymDisasmContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef LLVMPYM_TYPES_PRIV_PYMDISASMCONTEXT_H
#define LLVMPYM_TYPES_PRIV_PYMDISASMCONTEXT_H

#include <llvm-c/DisassemblerTypes.h>
#include <memory>
#include <unordered_map>
#include <mutex>
#include "PymLLVMObject.h"
#include "utils.h"

class PymDisasmContext : public PymLLVMObject<PymDisasmContext, LLVMDisasmContextRef> {
public:
explicit PymDisasmContext(LLVMDisasmContextRef data);
LLVMDisasmContextRef get() const;

private:
SHARED_POINTER_DEF(LLVMDisasmContextRef, void);
};


#endif
1 change: 1 addition & 0 deletions src/llvmpym/disassembler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .llvmpym_ext.disassembler import *
5 changes: 5 additions & 0 deletions src/llvmpym_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "llvm/Analysis.h"
#include "llvm/Target.h"
#include "llvm/TargetMachine.h"
#include "llvm/Disassembler.h"
#include "llvm/BitReader.h"

namespace nb = nanobind;
Expand Down Expand Up @@ -41,4 +42,8 @@ NB_MODULE(llvmpym_ext, m) {

auto bitReaderModule = m.def_submodule("bit_reader", "bit_reader");
populateBitReader(bitReaderModule);

auto disassemblerModule = m.def_submodule("disassembler", "disassembler");
populateDisassembler(disassemblerModule);

}

0 comments on commit 2d787ea

Please sign in to comment.