Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pecos-qis-ffi-types = { version = "0.1.1", path = "crates/pecos-qis-ffi-types" }
pecos-qis-selene = { version = "0.1.1", path = "crates/pecos-qis-selene" }
pecos-qis-core = { version = "0.1.1", path = "crates/pecos-qis-core" }
pecos-hugr-qis = { version = "0.1.1", path = "crates/pecos-hugr-qis" }
pecos-llvm = { version = "0.1.1", path = "crates/pecos-llvm" }
pecos-rslib = { version = "0.1.1", path = "python/pecos-rslib/rust" }
pecos-wasm = { version = "0.1.1", path = "crates/pecos-wasm" }
pecos-build-utils = { version = "0.1.1", path = "crates/pecos-build-utils" }
Expand Down
32 changes: 32 additions & 0 deletions crates/pecos-llvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "pecos-llvm"
version.workspace = true
edition.workspace = true
description = "Rust wrapper for LLVM IR generation using inkwell"
readme.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
pecos-core.workspace = true
thiserror.workspace = true
log.workspace = true
regex.workspace = true

# Inkwell for LLVM IR generation
[dependencies.inkwell]
workspace = true
features = ["llvm14-0"]

[features]
default = []

[build-dependencies]
pecos-llvm-utils.workspace = true

[lints]
workspace = true
126 changes: 126 additions & 0 deletions crates/pecos-llvm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
fn main() {
// Always validate LLVM since this crate requires LLVM
validate_llvm();
}

fn validate_llvm() {
use pecos_llvm_utils::is_valid_llvm_14;
use std::env;
use std::path::PathBuf;

// Check if LLVM_SYS_140_PREFIX is already set and valid
if let Ok(sys_prefix) = env::var("LLVM_SYS_140_PREFIX") {
let path = PathBuf::from(&sys_prefix);
if is_valid_llvm_14(&path) {
// LLVM is configured and valid, we're good!
return;
}
eprintln!("\n═══════════════════════════════════════════════════════════════");
eprintln!("ERROR: Invalid LLVM_SYS_140_PREFIX");
eprintln!("═══════════════════════════════════════════════════════════════");
eprintln!();
eprintln!("LLVM_SYS_140_PREFIX is set to: {sys_prefix}");
eprintln!("But this is not a valid LLVM 14 installation.");
eprintln!();
eprintln!("Please either:");
eprintln!(" 1. Fix the path to point to a valid LLVM 14 installation");
eprintln!(" 2. Unset it and configure LLVM:");
eprintln!(" unset LLVM_SYS_140_PREFIX");
eprintln!(" cargo run -p pecos-llvm-utils --bin pecos-llvm -- configure");
eprintln!("═══════════════════════════════════════════════════════════════\n");
panic!("Invalid LLVM_SYS_140_PREFIX. See error message above.");
}

// LLVM_SYS_140_PREFIX not set - print setup instructions
print_llvm_not_found_error_extended();
panic!("LLVM 14 not configured. See error message above for setup instructions.");
}

fn print_llvm_not_found_error_extended() {
eprintln!("\n═══════════════════════════════════════════════════════════════");
eprintln!("LLVM 14 Setup Required for pecos-qir");
eprintln!("═══════════════════════════════════════════════════════════════");
eprintln!();
eprintln!("The pecos-qir crate requires LLVM 14 for QIR generation.");
eprintln!("Choose one of these installation methods:");
eprintln!();
eprintln!("Option 1: Use pecos-llvm installer (recommended)");
eprintln!(" cargo run -p pecos-llvm-utils --bin pecos-llvm -- install");
eprintln!(" cargo build");
eprintln!();
eprintln!(" The installer automatically configures PECOS.");
eprintln!(" (Downloads LLVM 14.0.6 to ~/.pecos/llvm/ - ~400MB, ~5 minutes)");
eprintln!();

#[cfg(target_os = "macos")]
{
eprintln!("Option 2: Install via Homebrew");
eprintln!(" # Install LLVM 14");
eprintln!(" brew install llvm@14");
eprintln!();
eprintln!(" # Configure PECOS to use it");
eprintln!(" cargo run -p pecos-llvm-utils --bin pecos-llvm -- configure");
eprintln!();
eprintln!(" # Build PECOS");
eprintln!(" cargo build");
eprintln!();
eprintln!(" Note: Works on both Intel and Apple Silicon Macs");
eprintln!();
}

#[cfg(target_os = "linux")]
{
eprintln!("Option 2: Install via system package manager");
eprintln!();
eprintln!(" Debian/Ubuntu:");
eprintln!(" sudo apt update");
eprintln!(" sudo apt install llvm-14 llvm-14-dev");
eprintln!();
eprintln!(" Fedora/RHEL:");
eprintln!(" sudo dnf install llvm14 llvm14-devel");
eprintln!();
eprintln!(" Arch Linux:");
eprintln!(" # LLVM 14 may need to be built from AUR");
eprintln!(" yay -S llvm14");
eprintln!();
eprintln!(" Then configure and build:");
eprintln!(" cargo run -p pecos-llvm-utils --bin pecos-llvm -- configure");
eprintln!(" cargo build");
eprintln!();
}

#[cfg(target_os = "windows")]
{
eprintln!("Option 2: Manual installation (advanced)");
eprintln!();
eprintln!(" WARNING: The official LLVM installer lacks development files.");
eprintln!(" You need a FULL development package from community sources:");
eprintln!();
eprintln!(" Recommended sources:");
eprintln!(" https://github.com/bitgate/llvm-windows-full-builds");
eprintln!(" https://github.com/vovkos/llvm-package-windows");
eprintln!();
eprintln!(" After extracting to C:\\LLVM (or similar):");
eprintln!(" set LLVM_SYS_140_PREFIX=C:\\LLVM");
eprintln!(" cargo run -p pecos-llvm-utils --bin pecos-llvm -- configure");
eprintln!(" cargo build");
eprintln!();
}

eprintln!("Alternative: Set LLVM path manually");
eprintln!(" Instead of 'configure', you can set environment variables:");
eprintln!();
#[cfg(target_os = "windows")]
eprintln!(" set LLVM_SYS_140_PREFIX=C:\\path\\to\\llvm");
#[cfg(not(target_os = "windows"))]
eprintln!(" export LLVM_SYS_140_PREFIX=/path/to/llvm");
#[cfg(not(target_os = "windows"))]
eprintln!(" Or add llvm-config to PATH:");
#[cfg(not(target_os = "windows"))]
eprintln!(" export PATH=\"/path/to/llvm/bin:$PATH\"");
eprintln!();
eprintln!("For detailed instructions, see:");
eprintln!(" https://github.com/CQCL/PECOS/blob/master/docs/user-guide/getting-started.md");
eprintln!();
eprintln!("═══════════════════════════════════════════════════════════════\n");
}
30 changes: 30 additions & 0 deletions crates/pecos-llvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 The PECOS Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! LLVM IR generation using inkwell
//!
//! This crate provides Rust types and functions for generating LLVM IR,
//! designed to be compatible with Python's llvmlite usage patterns.
//!
//! The main module is `llvm_compat`, which provides types for LLVM IR generation
//! that are compatible with Python's llvmlite API.

pub mod llvm_compat;
pub mod prelude;

// Re-export main types at crate root for convenience
pub use llvm_compat::{
LLConstant, LLContext, LLFunction, LLFunctionType, LLIRBuilder, LLModule, LLResult, LLType,
LLValue,
};
Loading
Loading