Skip to content

Commit 4d80f6b

Browse files
added precompilation for lowering
1 parent 77a00a6 commit 4d80f6b

File tree

18 files changed

+2984
-29
lines changed

18 files changed

+2984
-29
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ ark-secp256r1 = "0.5.0"
8686
ark-std = "0.5.0"
8787
assert_matches = "1.5"
8888
bimap = "0.6.3"
89+
bincode = "1.3.3"
8990
cairo-lang-primitive-token = "1"
9091
cairo-vm = { version = "1.0.2", features = ["mod_builtin"] }
9192
clap = { version = "4.5.27", features = ["derive"] }

crates/cairo-lang-compiler/src/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn update_crate_root(
8585
let (crate_id, crate_settings) = get_crate_id_and_settings(db, crate_identifier, config);
8686
db.set_crate_config(
8787
crate_id,
88-
Some(CrateConfiguration { root, settings: crate_settings.clone() }),
88+
Some(CrateConfiguration { root, settings: crate_settings.clone(), cache_file: None }),
8989
);
9090
}
9191

crates/cairo-lang-defs/src/diagnostic_utils.rs

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ impl StableLocation {
3030
self.0.lookup(db.upcast())
3131
}
3232

33+
/// Returns the [SyntaxStablePtrId] of the [StableLocation].
34+
pub fn stable_ptr(&self) -> SyntaxStablePtrId {
35+
self.0
36+
}
37+
3338
/// Returns the [DiagnosticLocation] that corresponds to the [StableLocation].
3439
pub fn diagnostic_location(&self, db: &dyn DefsGroup) -> DiagnosticLocation {
3540
let syntax_node = self.syntax_node(db);

crates/cairo-lang-filesystem/src/db.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use smol_str::{SmolStr, ToSmolStr};
1313
use crate::cfg::CfgSet;
1414
use crate::flag::Flag;
1515
use crate::ids::{
16-
CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId, FileLongId, FlagId,
17-
FlagLongId, VirtualFile,
16+
BlobId, BlobLongId, CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId,
17+
FileLongId, FlagId, FlagLongId, VirtualFile,
1818
};
1919
use crate::span::{FileSummary, TextOffset, TextSpan, TextWidth};
2020

@@ -50,11 +50,12 @@ pub struct CrateConfiguration {
5050
/// The root directory of the crate.
5151
pub root: Directory,
5252
pub settings: CrateSettings,
53+
pub cache_file: Option<BlobId>,
5354
}
5455
impl CrateConfiguration {
5556
/// Returns a new configuration.
5657
pub fn default_for_root(root: Directory) -> Self {
57-
Self { root, settings: CrateSettings::default() }
58+
Self { root, settings: CrateSettings::default(), cache_file: None }
5859
}
5960
}
6061

@@ -182,6 +183,8 @@ pub trait FilesGroup: ExternalFiles {
182183
#[salsa::interned]
183184
fn intern_file(&self, file: FileLongId) -> FileId;
184185
#[salsa::interned]
186+
fn intern_blob(&self, blob: BlobLongId) -> BlobId;
187+
#[salsa::interned]
185188
fn intern_flag(&self, flag: FlagLongId) -> FlagId;
186189

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

221+
/// Query for the blob content.
222+
fn blob_content(&self, blob_id: BlobId) -> Option<Arc<[u8]>>;
218223
/// Query to get a compilation flag by its ID.
219224
fn get_flag(&self, id: FlagId) -> Option<Arc<Flag>>;
220225
}
@@ -244,6 +249,7 @@ pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathB
244249
coupons: true,
245250
},
246251
},
252+
cache_file: None,
247253
}),
248254
);
249255
}
@@ -302,13 +308,17 @@ fn crates(db: &dyn FilesGroup) -> Vec<CrateId> {
302308
fn crate_config(db: &dyn FilesGroup, crt: CrateId) -> Option<CrateConfiguration> {
303309
match crt.lookup_intern(db) {
304310
CrateLongId::Real { .. } => db.crate_configs().get(&crt).cloned(),
305-
CrateLongId::Virtual { name: _, file_id, settings } => Some(CrateConfiguration {
306-
root: Directory::Virtual {
307-
files: BTreeMap::from([("lib.cairo".into(), file_id)]),
308-
dirs: Default::default(),
309-
},
310-
settings: toml::from_str(&settings).expect("Failed to parse virtual crate settings."),
311-
}),
311+
CrateLongId::Virtual { name: _, file_id, settings, cache_file } => {
312+
Some(CrateConfiguration {
313+
root: Directory::Virtual {
314+
files: BTreeMap::from([("lib.cairo".into(), file_id)]),
315+
dirs: Default::default(),
316+
},
317+
settings: toml::from_str(&settings)
318+
.expect("Failed to parse virtual crate settings."),
319+
cache_file,
320+
})
321+
}
312322
}
313323
}
314324

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

361+
fn blob_content(db: &dyn FilesGroup, blob: BlobId) -> Option<Arc<[u8]>> {
362+
match blob.lookup_intern(db) {
363+
BlobLongId::OnDisk(path) => {
364+
// This does not result in performance cost due to OS caching and the fact that salsa
365+
// will re-execute only this single query if the file content did not change.
366+
db.salsa_runtime().report_synthetic_read(Durability::LOW);
367+
368+
match fs::read(path) {
369+
Ok(content) => Some(content.into()),
370+
Err(_) => None,
371+
}
372+
}
373+
BlobLongId::Virtual(content) => Some(content),
374+
}
375+
}
376+
351377
/// Returns the location of the originating user code.
352378
pub fn get_originating_location(
353379
db: &dyn FilesGroup,

crates/cairo-lang-filesystem/src/ids.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44

55
use cairo_lang_utils::{Intern, LookupIntern, define_short_id};
66
use path_clean::PathClean;
7+
use serde::{Deserialize, Serialize};
78
use smol_str::SmolStr;
89

910
use crate::db::{CORELIB_CRATE_NAME, FilesGroup};
@@ -17,7 +18,7 @@ pub enum CrateLongId {
1718
/// A crate that appears in crate_roots(), and on the filesystem.
1819
Real { name: SmolStr, discriminator: Option<SmolStr> },
1920
/// A virtual crate, not a part of the crate_roots(). Used mainly for tests.
20-
Virtual { name: SmolStr, file_id: FileId, settings: String },
21+
Virtual { name: SmolStr, file_id: FileId, settings: String, cache_file: Option<BlobId> },
2122
}
2223
impl CrateLongId {
2324
pub fn name(&self) -> SmolStr {
@@ -78,14 +79,14 @@ pub enum FileLongId {
7879
External(salsa::InternId),
7980
}
8081
/// Whether the file holds syntax for a module or for an expression.
81-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
82+
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
8283
pub enum FileKind {
8384
Module,
8485
Expr,
8586
}
8687

8788
/// A mapping for a code rewrite.
88-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
89+
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
8990
pub struct CodeMapping {
9091
pub span: TextSpan,
9192
pub origin: CodeOrigin,
@@ -108,7 +109,7 @@ impl CodeMapping {
108109
}
109110

110111
/// The origin of a code mapping.
111-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
112+
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
112113
pub enum CodeOrigin {
113114
/// The origin is a copied node starting at the given offset.
114115
Start(TextOffset),
@@ -214,3 +215,18 @@ impl Directory {
214215
}
215216
}
216217
}
218+
219+
/// A FileId for data that is not necessarily a valid UTF-8 string.
220+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
221+
pub enum BlobLongId {
222+
OnDisk(PathBuf),
223+
Virtual(Arc<[u8]>),
224+
}
225+
226+
define_short_id!(BlobId, BlobLongId, FilesGroup, lookup_intern_blob, intern_blob);
227+
228+
impl BlobId {
229+
pub fn new(db: &dyn FilesGroup, path: PathBuf) -> BlobId {
230+
BlobLongId::OnDisk(path.clean()).intern(db)
231+
}
232+
}

crates/cairo-lang-lowering/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ num-bigint = { workspace = true, default-features = true }
2323
num-integer = { workspace = true, default-features = true }
2424
num-traits = { workspace = true, default-features = true }
2525
salsa.workspace = true
26+
serde = { workspace = true, default-features = true }
27+
smol_str.workspace = true
2628

2729
[dev-dependencies]
2830
cairo-lang-plugins = { path = "../cairo-lang-plugins" }

0 commit comments

Comments
 (0)