-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Cranelift: add debug tag infrastructure. #11768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9409a84
Cranelift: add debug tag infrastructure.
cfallin 504c853
Review feedback: add back sequence points and enforce tags only on se…
cfallin 1c12a68
Use Vecs for debug metadata in MachBuffer to avoid SmallVec size pena…
cfallin 3e14d77
Review feedback: switch from inlined stackslot descriptor blobs to u6…
cfallin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//! Debug tag storage. | ||
//! | ||
//! Cranelift permits the embedder to place "debug tags" on | ||
//! instructions in CLIF. These tags are sequences of items of various | ||
//! kinds, with no other meaning imposed by Cranelift. They are passed | ||
//! through to metadata provided alongside the compilation result. | ||
//! | ||
//! When Cranelift inlines a function, it will prepend any tags from | ||
//! the call instruction at the inlining callsite to tags on all | ||
//! inlined instructions. | ||
//! | ||
//! These tags can be used, for example, to identify stackslots that | ||
//! store user state, or to denote positions in user source. In | ||
//! general, the intent is to allow perfect reconstruction of original | ||
//! (source-level) program state in an instrumentation-based | ||
//! debug-info scheme, as long as the instruction(s) on which these | ||
//! tags are attached are preserved. This will be the case for any | ||
//! instructions with side-effects. | ||
//! | ||
//! A few answers to design questions that lead to this design: | ||
//! | ||
//! - Why not use the SourceLoc mechanism? Debug tags are richer than | ||
//! that infrastructure because they preserve inlining location and | ||
//! are interleaved properly with any other tags describing the | ||
//! frame. | ||
//! - Why not attach debug tags only to special sequence-point | ||
//! instructions? This is driven by inlining: we should have the | ||
//! semantic information about a callsite attached directly to the | ||
//! call and observe it there, not have a magic "look backward to | ||
//! find a sequence point" behavior in the inliner. | ||
//! | ||
//! In other words, the needs of preserving "virtual" frames across an | ||
//! inlining transform drive this design. | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use crate::ir::{Inst, StackSlot}; | ||
use alloc::collections::BTreeMap; | ||
use alloc::vec::Vec; | ||
use core::ops::Range; | ||
|
||
/// Debug tags for instructions. | ||
#[derive(Clone, PartialEq, Hash, Default)] | ||
#[cfg_attr( | ||
feature = "enable-serde", | ||
derive(serde_derive::Serialize, serde_derive::Deserialize) | ||
)] | ||
pub struct DebugTags { | ||
/// Pool of tags, referred to by `insts` below. | ||
tags: Vec<DebugTag>, | ||
|
||
/// Per-instruction range for its list of tags in the tag pool (if | ||
/// any). | ||
/// | ||
/// Note: we don't use `PackedOption` and `EntityList` here | ||
/// because the values that we are storing are not entities. | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
insts: BTreeMap<Inst, Range<u32>>, | ||
} | ||
|
||
/// One debug tag. | ||
#[derive(Clone, Debug, PartialEq, Hash)] | ||
#[cfg_attr( | ||
feature = "enable-serde", | ||
derive(serde_derive::Serialize, serde_derive::Deserialize) | ||
)] | ||
pub enum DebugTag { | ||
/// User-specified `u32` value, opaque to Cranelift. | ||
User(u32), | ||
|
||
/// A stack slot reference. | ||
StackSlot(StackSlot), | ||
} | ||
|
||
impl DebugTags { | ||
/// Set the tags on an instruction, overwriting existing tag list. | ||
pub fn set(&mut self, inst: Inst, tags: impl IntoIterator<Item = DebugTag>) { | ||
let start = u32::try_from(self.tags.len()).unwrap(); | ||
self.tags.extend(tags); | ||
let end = u32::try_from(self.tags.len()).unwrap(); | ||
if end > start { | ||
self.insts.insert(inst, start..end); | ||
} else { | ||
self.insts.remove(&inst); | ||
} | ||
} | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Get the tags associated with an instruction. | ||
pub fn get(&self, inst: Inst) -> &[DebugTag] { | ||
if let Some(range) = self.insts.get(&inst) { | ||
let start = usize::try_from(range.start).unwrap(); | ||
let end = usize::try_from(range.end).unwrap(); | ||
&self.tags[start..end] | ||
} else { | ||
&[] | ||
} | ||
} | ||
|
||
/// Clone the tags from one instruction to another. | ||
/// | ||
/// This clone is cheap (references the same underlying storage) | ||
/// because the tag lists are immutable. | ||
pub fn clone_tags(&mut self, from: Inst, to: Inst) { | ||
if let Some(range) = self.insts.get(&from).cloned() { | ||
self.insts.insert(to, range); | ||
} | ||
cfallin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Are any debug tags present? | ||
/// | ||
/// This is used for adjusting margins when pretty-printing CLIF. | ||
pub fn is_empty(&self) -> bool { | ||
self.insts.is_empty() | ||
} | ||
|
||
/// Clear all tags. | ||
pub fn clear(&mut self) { | ||
self.insts.clear(); | ||
self.tags.clear(); | ||
} | ||
} | ||
|
||
impl core::fmt::Display for DebugTag { | ||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { | ||
match self { | ||
DebugTag::User(value) => write!(f, "{value}"), | ||
DebugTag::StackSlot(slot) => write!(f, "{slot}"), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.