#llvm #compiler #back-end #api #safety #safe #interface

llvm-lib

LLVM library with safe and flexibility in mind, without over complexity based on LLVM-C API

20 releases (6 breaking)

new 0.6.1 Sep 1, 2024
0.5.3 Aug 19, 2024
0.4.1-rc.2 Jul 28, 2024

#128 in Programming languages

Download history 1784/week @ 2024-06-22 5092/week @ 2024-06-29 1135/week @ 2024-07-06 1441/week @ 2024-07-13 1498/week @ 2024-07-20 1599/week @ 2024-07-27 1467/week @ 2024-08-03 1473/week @ 2024-08-10 1760/week @ 2024-08-17 1370/week @ 2024-08-24 1440/week @ 2024-08-31

6,219 downloads per month

MIT license

330KB
3.5K SLoC

License: MIT Lints Crates.io version Crates.io Total Downloads

mrLSD/llvm-lib-rs

LLVM library with safety and flexibility in mind, without over complexity based on LLVM-C API (based on llvm-sys crate).

Overview

The llvm-lib-rs provides a robust and comprehensive interface to the LLVM Compiler Infrastructure, leveraging the LLVM-C API to offer a blend of safety, flexibility, and extendability. This library is designed to serve as a powerful tool for developers looking to create backends for compilers, enabling them to harness the full potential of LLVM in a secure and user-friendly manner.

Safety

Safety is a paramount concern in the design of this library. By building on the LLVM-C API, we ensure that interactions with the LLVM infrastructure are conducted in a type-safe and memory-safe manner. The library employs Rust’s stringent safety guarantees to prevent common issues such as null pointer dereferencing, buffer overflows, and memory leaks. This commitment to safety allows developers to focus on the functionality of their compiler backends without worrying about underlying security vulnerabilities.

Flexibility

Flexibility is another core attribute of the llvm-lib-rs. The library provides a rich set of APIs that cover a wide range of LLVM’s capabilities, from module management and inline assembly to debugging metadata and function iteration. Developers can easily access and manipulate LLVM constructs, enabling the creation of highly customized and optimized compiler backends. The library’s design ensures that it can adapt to various use cases and requirements, making it an ideal choice for a diverse set of compiler development projects.

Extendability

The llvm-lib-rs is built with extendability in mind. It is designed to be easily extendable, allowing developers to add new functionalities and support for additional LLVM features as needed. The modular structure of the library facilitates the incorporation of new components, ensuring that it can grow and evolve alongside the LLVM ecosystem. This extendability ensures that the library remains relevant and useful as LLVM continues to advance and expand its capabilities.

Why LLVM?

LLVM (Low-Level Virtual Machine) is a powerful and versatile compiler infrastructure that provides a collection of modular and reusable compiler and toolchain technologies. It is widely used in the development of modern compilers, providing a framework for optimizing intermediate representations and generating machine code for various target architectures. LLVM’s ability to support multiple languages and platforms, coupled with its extensive optimization capabilities, makes it an essential tool for compiler developers. By leveraging LLVM, developers can create highly efficient and portable compilers that meet the demands of today’s diverse computing environments.

Design

The llvm-lib-rs library adheres to the structure of the LLVM C API, ensuring easy navigation through the extensive LLVM functions. Logical elements are grouped into modules, providing a clear organizational structure. Within these modules, Rust structures are introduced to wrap LLVM types, implementing corresponding functions for the wrapped LLVM types. This approach enhances flexibility and usability while maintaining the original LLVM code structure. The design avoids unnecessary complexity in the code and documentation, remaining fully aligned with the LLVM API. This alignment allows developers to easily navigate the llvm-lib-rs library using existing LLVM-C documentation.

Safety Considerations

When implementing functions using the LLVM-C API through FFI (Foreign Function Interface), all unsafe operations and data types are managed separately and wrapped in new types and structures. This separation ensures that unsafe code is isolated and not exposed in the final API, guaranteeing safety without introducing excessive complexity. By encapsulating unsafe operations within safe Rust abstractions, the library maintains a clean and understandable codebase while preventing unsafe memory interactions. This design choice provides a robust layer of protection, ensuring that users of the llvm-lib-rs library can work with LLVM functionalities securely and confidently.

Status

Based on llvm-sys rust crate.

Support LLVM:

  • llvm-18

Development in progress and API suppose to be unstable before completion.

LICENSE: MIT

Dependencies

~280–560KB
~11K SLoC