Skip to content

Commit

Permalink
feat: add optional llvm feature
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Apr 8, 2024
1 parent d6ba71a commit f5f925f
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/windows_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
working-directory: .

# Rust unit test
- run: cargo test -p kclvm-*
- run: cargo test -p kclvm-* --features llvm
working-directory: ./kclvm

- uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $bin_path = Join-Path $PSScriptRoot 'scripts\build-windows\_output\kclvm-windows
$env:Path += ";$bin_path"
# rust unit test
Set-Location .\kclvm
cargo test --workspace -r -- --nocapture
cargo test --workspace -r --features llvm -- --nocapture
Set-Location $PSScriptRoot
# rust runtime test
Set-Location .\kclvm\tests\test_units
Expand Down
3 changes: 3 additions & 0 deletions kclvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ members = [
"utils",
"tools/src/LSP"
]

[features]
llvm = ["kclvm-compiler/llvm"]
5 changes: 4 additions & 1 deletion kclvm/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["target-webassembly", "llvm12-0"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", optional = true }
time = "0.2.23"
phf = { version = "0.9", features = ["macros"] }
ahash = "0.7.2"
Expand All @@ -20,3 +20,6 @@ kclvm-ast = {path = "../ast"}
kclvm-sema = {path = "../sema"}
kclvm-runtime = {path = "../runtime"}
kclvm-error = {path = "../error"}

[features]
llvm = ["inkwell/target-webassembly", "inkwell/llvm12-0"]
18 changes: 9 additions & 9 deletions kclvm/compiler/src/codegen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use std::error;
use std::fmt::{self, Debug};

pub(crate) const VALUE_TYPE_NOT_FOUND_MSG: &str = "Type is not found";
pub(crate) const CONTEXT_VAR_NOT_FOUND_MSG: &str = "Context variable is not found";
pub(crate) const FUNCTION_RETURN_VALUE_NOT_FOUND_MSG: &str = "Function return value is not found";
pub(crate) const COMPILE_ERROR_MSG: &str = "Compile error";
pub(crate) const INTERNAL_ERROR_MSG: &str = "Internal error, please report a bug to us";
pub(crate) const CODE_GEN_ERROR_MSG: &str = "Code gen error";
pub(crate) const INVALID_OPERATOR_MSG: &str = "Invalid operator";
pub(crate) const INVALID_JOINED_STR_MSG: &str = "Invalid AST JoinedString value";
pub(crate) const INVALID_STR_INTERPOLATION_SPEC_MSG: &str =
pub const VALUE_TYPE_NOT_FOUND_MSG: &str = "Type is not found";
pub const CONTEXT_VAR_NOT_FOUND_MSG: &str = "Context variable is not found";
pub const FUNCTION_RETURN_VALUE_NOT_FOUND_MSG: &str = "Function return value is not found";
pub const COMPILE_ERROR_MSG: &str = "Compile error";
pub const INTERNAL_ERROR_MSG: &str = "Internal error, please report a bug to us";
pub const CODE_GEN_ERROR_MSG: &str = "Code gen error";
pub const INVALID_OPERATOR_MSG: &str = "Invalid operator";
pub const INVALID_JOINED_STR_MSG: &str = "Invalid AST JoinedString value";
pub const INVALID_STR_INTERPOLATION_SPEC_MSG: &str =
"Invalid string interpolation format specification";

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::codegen::{
use crate::codegen::{CodeGenContext, GLOBAL_LEVEL};
use crate::value;

use super::OBJECT_FILE_SUFFIX;
use crate::codegen::OBJECT_FILE_SUFFIX;

/// SCALAR_KEY denotes the temp scalar key for the global variable json plan process.
const SCALAR_KEY: &str = "";
Expand Down
2 changes: 1 addition & 1 deletion kclvm/compiler/src/codegen/llvm/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ pub fn emit_code(
workdir,
);
// Generate user KCL code LLVM IR
crate::codegen::emit_code(ctx, opts)
crate::codegen::emit_code_with(ctx, opts)
}
8 changes: 0 additions & 8 deletions kclvm/compiler/src/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,3 @@ mod schema;
mod utils;

pub use emit::emit_code;

/// Object file type format suffix.
#[cfg(target_os = "windows")]
pub const OBJECT_FILE_SUFFIX: &str = ".obj";
#[cfg(not(target_os = "windows"))]
pub const OBJECT_FILE_SUFFIX: &str = ".o";
/// LLVM IR text format suffix .ll
pub const LL_FILE_SUFFIX: &str = ".ll";
51 changes: 42 additions & 9 deletions kclvm/compiler/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
//! Copyright 2021 The KCL Authors. All rights reserved.

use indexmap::IndexMap;
use kclvm_ast::ast;

mod abi;
pub mod error;
#[cfg(feature = "llvm")]
pub mod llvm;
mod traits;

/// The kclvm runner main function name.
pub(crate) const MODULE_NAME: &str = "kclvm_main";
pub const MODULE_NAME: &str = "kclvm_main";
/// The kclvm runner main function entry block name.
pub(crate) const ENTRY_NAME: &str = "entry";
pub const ENTRY_NAME: &str = "entry";
/// The kclvm runtime value type name.
pub(crate) const VALUE_TYPE_NAME: &str = "kclvm_value_ref_t";
pub const VALUE_TYPE_NAME: &str = "kclvm_value_ref_t";
/// The kclvm runtime context type name.
pub(crate) const CONTEXT_TYPE_NAME: &str = "kclvm_context_t";
pub const CONTEXT_TYPE_NAME: &str = "kclvm_context_t";
/// Package init function name suffix
pub(crate) const PKG_INIT_FUNCTION_SUFFIX: &str = "init";
pub const PKG_INIT_FUNCTION_SUFFIX: &str = "init";
/// Global level
pub(crate) const GLOBAL_LEVEL: usize = 1;
pub const GLOBAL_LEVEL: usize = 1;
/// Inner level
pub(crate) const INNER_LEVEL: usize = 2;
pub const INNER_LEVEL: usize = 2;
/// Global variable alignment
pub(crate) const GLOBAL_VAL_ALIGNMENT: u32 = 8;
pub const GLOBAL_VAL_ALIGNMENT: u32 = 8;
/// Object file type format suffix.
#[cfg(target_os = "windows")]
pub const OBJECT_FILE_SUFFIX: &str = ".obj";
#[cfg(not(target_os = "windows"))]
pub const OBJECT_FILE_SUFFIX: &str = ".o";
/// LLVM IR text format suffix .ll
pub const LL_FILE_SUFFIX: &str = ".ll";

/// CodeGenContext is a trait used by the compiler to emit code to different targets.
pub trait CodeGenContext: traits::ProgramCodeGen {
Expand All @@ -39,9 +50,31 @@ pub struct EmitOptions<'a> {
}

/// Emit code with the options using CodeGenContext.
pub fn emit_code(
pub fn emit_code_with(
ctx: impl CodeGenContext,
opt: &EmitOptions,
) -> Result<(), Box<dyn std::error::Error>> {
ctx.emit(opt)
}

/// Generate LLVM IR of KCL ast module.
#[inline]
pub fn emit_code(
program: &ast::Program,
workdir: String,
import_names: IndexMap<String, IndexMap<String, String>>,
opts: &EmitOptions,
) -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "llvm")]
{
llvm::emit_code(program, workdir, import_names, opts)
}
#[cfg(not(feature = "llvm"))]
{
let _ = program;
let _ = workdir;
let _ = import_names;
let _ = opts;
Err("error: llvm feature is not enabled. Note: Set KCL_FAST_EVAL=1 or rebuild the crate with the llvm feature.".to_string().into())
}
}
6 changes: 3 additions & 3 deletions kclvm/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ fix:

# Unit tests without code cov
test:
cargo test --workspace -r -- --nocapture
cargo test --workspace -r --features llvm -- --nocapture

# Unit tests with code cov (Requires rust 1.60+)
codecov:
rustup component add llvm-tools-preview
cargo install cargo-llvm-cov
cargo llvm-cov --workspace --ignore-filename-regex gpyrpc.rs --html --open -- --nocapture
cargo llvm-cov --workspace --features llvm --ignore-filename-regex gpyrpc.rs --html --open -- --nocapture

# Unit tests with code cov and output the lcov file (Requires rust 1.60+)
codecov-lcov:
rustup component add llvm-tools-preview
cargo install cargo-llvm-cov
rm -rf $(PWD)/.kclvm_cov
mkdir $(PWD)/.kclvm_cov
cargo llvm-cov --lcov --output-path $(PWD)/.kclvm_cov/lcov.info -r --workspace --ignore-filename-regex gpyrpc.rs -- --nocapture
cargo llvm-cov --features llvm --lcov --output-path $(PWD)/.kclvm_cov/lcov.info -r --workspace --ignore-filename-regex gpyrpc.rs -- --nocapture

# Test runtime libaries using python functions
test-runtime: install-kclvm-py install-pytest
Expand Down
5 changes: 1 addition & 4 deletions kclvm/runner/src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use anyhow::Result;
use compiler_base_macros::bug;
use indexmap::IndexMap;
use kclvm_ast::ast::{self, Program};
use kclvm_compiler::codegen::{
llvm::{emit_code, OBJECT_FILE_SUFFIX},
EmitOptions,
};
use kclvm_compiler::codegen::{emit_code, EmitOptions, OBJECT_FILE_SUFFIX};
use kclvm_config::cache::{load_pkg_cache, save_pkg_cache, CacheOption, KCL_CACHE_PATH_ENV_VAR};
use kclvm_sema::resolver::scope::ProgramScope;
use std::{
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{execute, runner::ExecProgramArgs};
use anyhow::Context;
use anyhow::Result;
use kclvm_ast::ast::{Module, Program};
use kclvm_compiler::codegen::llvm::OBJECT_FILE_SUFFIX;
use kclvm_compiler::codegen::OBJECT_FILE_SUFFIX;
use kclvm_config::settings::load_file;
use kclvm_parser::load_program;
use kclvm_parser::ParseSession;
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-windows/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Set-Location $PSScriptRoot
# 1. Install kclvm_cli_cdylib.dll
Set-Location "..\..\kclvm"
cargo build --release
cargo build --release --features llvm
Set-Location $PSScriptRoot

New-Item -ErrorAction Ignore -Path ".\_output" -ItemType "directory"
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ prepare_dirs

cd $topdir/kclvm
export PATH=$PATH:/root/.cargo/bin:/usr/lib/llvm-12/bin
cargo build --release
cargo build --release --features llvm

## Switch dll file extension according to os.
dll_extension="so"
Expand Down

0 comments on commit f5f925f

Please sign in to comment.