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
2 changes: 2 additions & 0 deletions src/bin/obol-driver/translate/translate_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ pub fn translate<'tcx, 'ctx>(
id_map: Default::default(),
reverse_id_map: Default::default(),
file_to_id: Default::default(),
source_file_to_id: Default::default(),
span_cache: Default::default(),
items_to_translate: Default::default(),
processed: Default::default(),
cached_item_metas: Default::default(),
Expand Down
10 changes: 10 additions & 0 deletions src/bin/obol-driver/translate/translate_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
extern crate rustc_hir;
extern crate rustc_middle;
extern crate rustc_public;
extern crate rustc_span;

use super::translate_crate::TransItemSource;
use charon_lib::ast::*;
use charon_lib::formatter::{FmtCtx, IntoFormatter};
use charon_lib::options::TranslateOptions;
use rustc_middle::ty::TyCtxt;
use rustc_span::StableSourceFileId;
use std::cell::RefCell;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::PathBuf;
Expand All @@ -34,6 +36,14 @@ pub struct TranslateCtx<'tcx> {
pub reverse_id_map: HashMap<ItemId, TransItemSource>,
/// The reverse filename map.
pub file_to_id: HashMap<FileName, FileId>,
/// Direct cache: rustc `SourceFile` stable id → `FileId`.
/// `translate_raw_span` is called once per statement/terminator/local/item, and almost all
/// hits land on a handful of source files. This avoids the per-span filename normalization,
/// path component walks, and `FileName` hashing that `file_to_id` would otherwise re-do.
pub source_file_to_id: HashMap<StableSourceFileId, FileId>,
/// Per-span cache: `ty::Span` → translated `SpanData`. The same span is translated repeatedly
/// when statements share a macro call site or were generated from the same MIR node.
pub span_cache: HashMap<rustc_public::ty::Span, meta::SpanData>,

/// Cache of StableMir type IDs to our translated types.
pub type_trans_cache: HashMap<rustc_public::ty::Ty, Ty>,
Expand Down
30 changes: 23 additions & 7 deletions src/bin/obol-driver/translate/translate_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,28 @@ impl<'tcx, 'ctx> TranslateCtx<'tcx> {
}
}

pub fn translate_raw_span(&mut self, span: &ty::Span) -> meta::SpanData {
let span = rustc_public::rustc_internal::internal(self.tcx, *span);
pub fn translate_raw_span(&mut self, rspan: &ty::Span) -> meta::SpanData {
// Top-level cache by `ty::Span`: many MIR statements share the same expansion span.
if let Some(cached) = self.span_cache.get(rspan) {
return *cached;
}

let span = rustc_public::rustc_internal::internal(self.tcx, *rspan);
let span = span.source_callsite();
let smap: &rustc_span::source_map::SourceMap = self.tcx.sess.psess.source_map();
let filename = smap.span_to_filename(span);
let filename = self.translate_filename(&filename);
let file_id = self.register_file(filename, span);
// Resolving the file via `span_to_filename` + `translate_filename` walks path components
// and hashes a `FileName` on every call, so cache by the rustc `SourceFile` stable id.
let source_file = smap.lookup_source_file(span.lo());
let file_id = match self.source_file_to_id.get(&source_file.stable_id) {
Some(id) => *id,
None => {
let filename = smap.span_to_filename(span);
let filename = self.translate_filename(&filename);
let id = self.register_file(filename, span);
self.source_file_to_id.insert(source_file.stable_id, id);
id
}
};

let convert_loc = |pos: rustc_span::BytePos| -> Loc {
let loc = smap.lookup_char_pos(pos);
Expand All @@ -133,8 +148,9 @@ impl<'tcx, 'ctx> TranslateCtx<'tcx> {
let beg = convert_loc(span.lo());
let end = convert_loc(span.hi());

// Put together
meta::SpanData { file_id, beg, end }
let data = meta::SpanData { file_id, beg, end };
self.span_cache.insert(*rspan, data);
data
}

pub(crate) fn translate_span_from_smir(&mut self, span: &ty::Span) -> Span {
Expand Down