Skip to content

Commit

Permalink
added precompilation for lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerStarkware committed Feb 4, 2025
1 parent 78cdd8a commit 15a3e9f
Show file tree
Hide file tree
Showing 18 changed files with 2,972 additions and 31 deletions.
17 changes: 15 additions & 2 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 @@ -87,6 +87,7 @@ ark-secp256r1 = "0.5.0"
ark-std = "0.5.0"
assert_matches = "1.5"
bimap = "0.6.3"
bincode = "1.3.3"
cairo-vm = { version = "1.0.2", features = ["mod_builtin"] }
clap = { version = "4.5.27", features = ["derive"] }
colored = "3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn update_crate_root(
let (crate_id, crate_settings) = get_crate_id_and_settings(db, crate_identifier, config);
db.set_crate_config(
crate_id,
Some(CrateConfiguration { root, settings: crate_settings.clone() }),
Some(CrateConfiguration { root, settings: crate_settings.clone(), cache_file: None }),
);
}

Expand Down
5 changes: 5 additions & 0 deletions crates/cairo-lang-defs/src/diagnostic_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ impl StableLocation {
self.0.lookup(db.upcast())
}

/// Returns the [SyntaxStablePtrId] of the [StableLocation].
pub fn stable_ptr(&self) -> SyntaxStablePtrId {
self.0
}

/// Returns the [DiagnosticLocation] that corresponds to the [StableLocation].
pub fn diagnostic_location(&self, db: &dyn DefsGroup) -> DiagnosticLocation {
let syntax_node = self.syntax_node(db);
Expand Down
46 changes: 36 additions & 10 deletions crates/cairo-lang-filesystem/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use smol_str::{SmolStr, ToSmolStr};
use crate::cfg::CfgSet;
use crate::flag::Flag;
use crate::ids::{
CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId, FileLongId, FlagId,
FlagLongId, VirtualFile,
BlobId, BlobLongId, CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId,
FileLongId, FlagId, FlagLongId, VirtualFile,
};
use crate::span::{FileSummary, TextOffset, TextSpan, TextWidth};

Expand Down Expand Up @@ -50,11 +50,12 @@ pub struct CrateConfiguration {
/// The root directory of the crate.
pub root: Directory,
pub settings: CrateSettings,
pub cache_file: Option<BlobId>,
}
impl CrateConfiguration {
/// Returns a new configuration.
pub fn default_for_root(root: Directory) -> Self {
Self { root, settings: CrateSettings::default() }
Self { root, settings: CrateSettings::default(), cache_file: None }
}
}

Expand Down Expand Up @@ -182,6 +183,8 @@ pub trait FilesGroup: ExternalFiles {
#[salsa::interned]
fn intern_file(&self, file: FileLongId) -> FileId;
#[salsa::interned]
fn intern_blob(&self, blob: BlobLongId) -> BlobId;
#[salsa::interned]
fn intern_flag(&self, flag: FlagLongId) -> FlagId;

/// Main input of the project. Lists all the crates configurations.
Expand Down Expand Up @@ -215,6 +218,8 @@ pub trait FilesGroup: ExternalFiles {
fn file_content(&self, file_id: FileId) -> Option<Arc<str>>;
fn file_summary(&self, file_id: FileId) -> Option<Arc<FileSummary>>;

/// Query for the blob content.
fn blob_content(&self, blob_id: BlobId) -> Option<Arc<[u8]>>;
/// Query to get a compilation flag by its ID.
fn get_flag(&self, id: FlagId) -> Option<Arc<Flag>>;
}
Expand Down Expand Up @@ -244,6 +249,7 @@ pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathB
coupons: true,
},
},
cache_file: None,
}),
);
}
Expand Down Expand Up @@ -302,13 +308,17 @@ fn crates(db: &dyn FilesGroup) -> Vec<CrateId> {
fn crate_config(db: &dyn FilesGroup, crt: CrateId) -> Option<CrateConfiguration> {
match crt.lookup_intern(db) {
CrateLongId::Real { .. } => db.crate_configs().get(&crt).cloned(),
CrateLongId::Virtual { name: _, file_id, settings } => Some(CrateConfiguration {
root: Directory::Virtual {
files: BTreeMap::from([("lib.cairo".into(), file_id)]),
dirs: Default::default(),
},
settings: toml::from_str(&settings).expect("Failed to parse virtual crate settings."),
}),
CrateLongId::Virtual { name: _, file_id, settings, cache_file } => {
Some(CrateConfiguration {
root: Directory::Virtual {
files: BTreeMap::from([("lib.cairo".into(), file_id)]),
dirs: Default::default(),
},
settings: toml::from_str(&settings)
.expect("Failed to parse virtual crate settings."),
cache_file,
})
}
}
}

Expand Down Expand Up @@ -348,6 +358,22 @@ fn get_flag(db: &dyn FilesGroup, id: FlagId) -> Option<Arc<Flag>> {
db.flags().get(&id).cloned()
}

fn blob_content(db: &dyn FilesGroup, blob: BlobId) -> Option<Arc<[u8]>> {
match blob.lookup_intern(db) {
BlobLongId::OnDisk(path) => {
// This does not result in performance cost due to OS caching and the fact that salsa
// will re-execute only this single query if the file content did not change.
db.salsa_runtime().report_synthetic_read(Durability::LOW);

match fs::read(path) {
Ok(content) => Some(content.into()),
Err(_) => None,
}
}
BlobLongId::Virtual(content) => Some(content),
}
}

/// Returns the location of the originating user code.
pub fn get_originating_location(
db: &dyn FilesGroup,
Expand Down
24 changes: 20 additions & 4 deletions crates/cairo-lang-filesystem/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;

use cairo_lang_utils::{Intern, LookupIntern, define_short_id};
use path_clean::PathClean;
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use crate::db::{CORELIB_CRATE_NAME, FilesGroup};
Expand All @@ -17,7 +18,7 @@ pub enum CrateLongId {
/// A crate that appears in crate_roots(), and on the filesystem.
Real { name: SmolStr, discriminator: Option<SmolStr> },
/// A virtual crate, not a part of the crate_roots(). Used mainly for tests.
Virtual { name: SmolStr, file_id: FileId, settings: String },
Virtual { name: SmolStr, file_id: FileId, settings: String, cache_file: Option<BlobId> },
}
impl CrateLongId {
pub fn name(&self) -> SmolStr {
Expand Down Expand Up @@ -78,14 +79,14 @@ pub enum FileLongId {
External(salsa::InternId),
}
/// Whether the file holds syntax for a module or for an expression.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileKind {
Module,
Expr,
}

/// A mapping for a code rewrite.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeMapping {
pub span: TextSpan,
pub origin: CodeOrigin,
Expand All @@ -108,7 +109,7 @@ impl CodeMapping {
}

/// The origin of a code mapping.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum CodeOrigin {
/// The origin is a copied node staring at the given offset.
Start(TextOffset),
Expand Down Expand Up @@ -214,3 +215,18 @@ impl Directory {
}
}
}

/// A FileId for data that is not necessarily a valid UTF-8 string.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum BlobLongId {
OnDisk(PathBuf),
Virtual(Arc<[u8]>),
}

define_short_id!(BlobId, BlobLongId, FilesGroup, lookup_intern_blob, intern_blob);

impl BlobId {
pub fn new(db: &dyn FilesGroup, path: PathBuf) -> BlobId {
BlobLongId::OnDisk(path.clean()).intern(db)
}
}
3 changes: 3 additions & 0 deletions crates/cairo-lang-lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license-file.workspace = true
description = "Cairo lowering phase."

[dependencies]
bincode.workspace = true
cairo-lang-debug = { path = "../cairo-lang-debug", version = "~2.9.2" }
cairo-lang-defs = { path = "../cairo-lang-defs", version = "~2.9.2" }
cairo-lang-diagnostics = { path = "../cairo-lang-diagnostics", version = "~2.9.2" }
Expand All @@ -23,6 +24,8 @@ num-bigint = { workspace = true, default-features = true }
num-integer = { workspace = true, default-features = true }
num-traits = { workspace = true, default-features = true }
salsa.workspace = true
serde = { workspace = true, default-features = true }
smol_str.workspace = true

[dev-dependencies]
cairo-lang-plugins = { path = "../cairo-lang-plugins" }
Expand Down
Loading

0 comments on commit 15a3e9f

Please sign in to comment.