Skip to content
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

Two fixes for the -g (debug info) option #6931

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cranelift.dbg*
docs/_build
docs/book
.vscode/
.vs/
rusty-tags.*
tags
target
Expand Down
82 changes: 65 additions & 17 deletions cranelift/codegen/src/machinst/vcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ impl<I: VCodeInst> VCode<I> {
}
}

self.monotonize_inst_offsets(&mut inst_offsets[..], func_body_len);
let value_labels_ranges =
self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
Comment on lines 1103 to 1104
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is interesting to note that optimize_branches is invoked after this code one last time and can nominally modify the current buffer pointer, make func_body_len stale, etc. At the same time, for it to remove a branch, that branch needs to point past the end of the function, which doesn't seem possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm -- it's theoretically possible, if it were a block that had just a jump instruction, no fallthrough (previous block ended in an uncond branch), and we rewrote branches to this last block to go directly to the jump dest instead.

Perhaps you could pull out the result of buffer.finish(...) (which is where the last optimization call occurs) to before this line?

let frame_size = self.abi.frame_size();
Expand All @@ -1115,6 +1116,43 @@ impl<I: VCodeInst> VCode<I> {
}
}

fn monotonize_inst_offsets(&self, inst_offsets: &mut [CodeOffset], func_body_len: u32) {
if self.debug_value_labels.is_empty() {
return;
}

// During emission, branch removal can make offsets of instructions incorrect.
// Consider the following sequence: [insi][jmp0][jmp1][jmp2][insj]
// It will be recorded as (say): [30] [34] [38] [42] [<would be 46>]
// When the jumps get removed we are left with (in "inst_offsets"):
// [insi][jmp0][jmp1][jmp2][insj][...]
// [30] [34] [38] [42] [34]
// Which violates the monotonicity invariant. This method sets offsets of these
// removed instructions such as to make them appear zero-sized:
// [insi][jmp0][jmp1][jmp2][insj][...]
// [30] [34] [34] [34] [34]
//
let mut next_offset = func_body_len;
for inst_index in (0..(inst_offsets.len() - 1)).rev() {
let inst_offset = inst_offsets[inst_index];
if inst_offset > next_offset {
trace!(
"Fixing code offset of the removed Inst {}: {} -> {}",
inst_index,
inst_offset,
next_offset
);
inst_offsets[inst_index] = next_offset;
continue;
}

// Not all instructions get their offsets recorded.
if inst_offset != 0 {
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
next_offset = inst_offset;
}
}
}

fn compute_value_labels_ranges(
&self,
regalloc: &regalloc2::Output,
Expand Down Expand Up @@ -1158,24 +1196,34 @@ impl<I: VCodeInst> VCode<I> {
continue;
};

ranges.push(ValueLocRange {
// ValueLocRanges are recorded by *instruction-end
// offset*. `from_offset` is the *start* of the
// instruction; that is the same as the end of another
// instruction, so we only want to begin coverage once
// we are past the previous instruction's end.
let start = from_offset + 1;

// Likewise, `end` is exclusive, but we want to
// *include* the end of the last
// instruction. `to_offset` is the start of the
// `to`-instruction, which is the exclusive end, i.e.,
// the first instruction not covered. That
// instruction's start is the same as the end of the
// last instruction that is included, so we go one
// byte further to be sure to include it.
let end = to_offset + 1;

trace!(
"Recording debug range for VL{} in {:?}: [Inst {}..Inst {}) [{}..{})",
label,
loc,
// ValueLocRanges are recorded by *instruction-end
// offset*. `from_offset` is the *start* of the
// instruction; that is the same as the end of another
// instruction, so we only want to begin coverage once
// we are past the previous instruction's end.
start: from_offset + 1,
// Likewise, `end` is exclusive, but we want to
// *include* the end of the last
// instruction. `to_offset` is the start of the
// `to`-instruction, which is the exclusive end, i.e.,
// the first instruction not covered. That
// instruction's start is the same as the end of the
// last instruction that is included, so we go one
// byte further to be sure to include it.
end: to_offset + 1,
});
from.inst().index(),
to.inst().index(),
start,
end
);

ranges.push(ValueLocRange { loc, start, end });
}

value_labels_ranges
Expand Down
1 change: 1 addition & 0 deletions crates/cranelift/src/debug/transform/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ where
let addr = addr_tr.translate(u).unwrap_or(write::Address::Constant(0));
write::AttributeValue::Address(addr)
}
AttributeValue::Block(d) => write::AttributeValue::Block(d.to_slice()?.to_vec()),
AttributeValue::Udata(u) => write::AttributeValue::Udata(u),
AttributeValue::Data1(d) => write::AttributeValue::Data1(d),
AttributeValue::Data2(d) => write::AttributeValue::Data2(d),
Expand Down