Skip to content

Commit 626b31b

Browse files
author
zhuyunxing
committed
coverage. MCDC ConditionId start from 0 to keep with llvm 19
1 parent aa95c1b commit 626b31b

File tree

3 files changed

+30
-34
lines changed
  • compiler

3 files changed

+30
-34
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub enum RegionKind {
111111
}
112112

113113
pub mod mcdc {
114-
use rustc_middle::mir::coverage::{ConditionInfo, DecisionInfo};
114+
use rustc_middle::mir::coverage::{ConditionId, ConditionInfo, DecisionInfo};
115115

116116
/// Must match the layout of `LLVMRustMCDCDecisionParameters`.
117117
#[repr(C)]
@@ -166,12 +166,13 @@ pub mod mcdc {
166166

167167
impl From<ConditionInfo> for BranchParameters {
168168
fn from(value: ConditionInfo) -> Self {
169+
let to_llvm_cond_id = |cond_id: Option<ConditionId>| {
170+
cond_id.and_then(|id| LLVMConditionId::try_from(id.as_usize()).ok()).unwrap_or(-1)
171+
};
172+
let ConditionInfo { condition_id, true_next_id, false_next_id } = value;
169173
Self {
170-
condition_id: value.condition_id.as_u32() as LLVMConditionId,
171-
condition_ids: [
172-
value.false_next_id.as_u32() as LLVMConditionId,
173-
value.true_next_id.as_u32() as LLVMConditionId,
174-
],
174+
condition_id: to_llvm_cond_id(Some(condition_id)),
175+
condition_ids: [to_llvm_cond_id(false_next_id), to_llvm_cond_id(true_next_id)],
175176
}
176177
}
177178
}

compiler/rustc_middle/src/mir/coverage.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ rustc_index::newtype_index! {
7575
}
7676

7777
impl ConditionId {
78-
pub const NONE: Self = Self::from_u32(0);
78+
pub const START: Self = Self::from_usize(0);
7979
}
8080

8181
/// Enum that can hold a constant zero value, the ID of an physical coverage
@@ -307,18 +307,8 @@ pub struct BranchSpan {
307307
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
308308
pub struct ConditionInfo {
309309
pub condition_id: ConditionId,
310-
pub true_next_id: ConditionId,
311-
pub false_next_id: ConditionId,
312-
}
313-
314-
impl Default for ConditionInfo {
315-
fn default() -> Self {
316-
Self {
317-
condition_id: ConditionId::NONE,
318-
true_next_id: ConditionId::NONE,
319-
false_next_id: ConditionId::NONE,
320-
}
321-
}
310+
pub true_next_id: Option<ConditionId>,
311+
pub false_next_id: Option<ConditionId>,
322312
}
323313

324314
#[derive(Clone, Debug)]

compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ impl BooleanDecisionCtx {
5050
}
5151

5252
fn next_condition_id(&mut self) -> ConditionId {
53+
let id = ConditionId::from_usize(self.condition_id_counter);
5354
self.condition_id_counter += 1;
54-
ConditionId::from_usize(self.condition_id_counter)
55+
id
5556
}
5657

5758
// At first we assign ConditionIds for each sub expression.
@@ -95,20 +96,23 @@ impl BooleanDecisionCtx {
9596
// - If the op is AND, the "false_next" of LHS and RHS should be the parent's "false_next". While "true_next" of the LHS is the RHS, the "true next" of RHS is the parent's "true_next".
9697
// - If the op is OR, the "true_next" of LHS and RHS should be the parent's "true_next". While "false_next" of the LHS is the RHS, the "false next" of RHS is the parent's "false_next".
9798
fn record_conditions(&mut self, op: LogicalOp) {
98-
let parent_condition = self.decision_stack.pop_back().unwrap_or_default();
99-
let lhs_id = if parent_condition.condition_id == ConditionId::NONE {
100-
ConditionId::from(self.next_condition_id())
101-
} else {
102-
parent_condition.condition_id
99+
let parent_condition = match self.decision_stack.pop_back() {
100+
Some(info) => info,
101+
None => ConditionInfo {
102+
condition_id: self.next_condition_id(),
103+
true_next_id: None,
104+
false_next_id: None,
105+
},
103106
};
107+
let lhs_id = parent_condition.condition_id;
104108

105109
let rhs_condition_id = self.next_condition_id();
106110

107111
let (lhs, rhs) = match op {
108112
LogicalOp::And => {
109113
let lhs = ConditionInfo {
110114
condition_id: lhs_id,
111-
true_next_id: rhs_condition_id,
115+
true_next_id: Some(rhs_condition_id),
112116
false_next_id: parent_condition.false_next_id,
113117
};
114118
let rhs = ConditionInfo {
@@ -122,7 +126,7 @@ impl BooleanDecisionCtx {
122126
let lhs = ConditionInfo {
123127
condition_id: lhs_id,
124128
true_next_id: parent_condition.true_next_id,
125-
false_next_id: rhs_condition_id,
129+
false_next_id: Some(rhs_condition_id),
126130
};
127131
let rhs = ConditionInfo {
128132
condition_id: rhs_condition_id,
@@ -143,11 +147,15 @@ impl BooleanDecisionCtx {
143147
true_marker: BlockMarkerId,
144148
false_marker: BlockMarkerId,
145149
) {
146-
let condition_info = self.decision_stack.pop_back().unwrap_or_default();
147-
if condition_info.true_next_id == ConditionId::NONE {
150+
let condition_info = self.decision_stack.pop_back().unwrap_or(ConditionInfo {
151+
condition_id: ConditionId::START,
152+
true_next_id: None,
153+
false_next_id: None,
154+
});
155+
if condition_info.true_next_id.is_none() {
148156
self.decision_info.end_markers.push(true_marker);
149157
}
150-
if condition_info.false_next_id == ConditionId::NONE {
158+
if condition_info.false_next_id.is_none() {
151159
self.decision_info.end_markers.push(false_marker);
152160
}
153161

@@ -339,10 +347,7 @@ impl MCDCInfoBuilder {
339347
ctx
340348
}
341349

342-
fn append_normal_branches(&mut self, mut branches: Vec<MCDCBranchSpan>) {
343-
branches
344-
.iter_mut()
345-
.for_each(|branch| branch.condition_info.condition_id = ConditionId::NONE);
350+
fn append_normal_branches(&mut self, branches: Vec<MCDCBranchSpan>) {
346351
self.normal_branch_spans.extend(branches);
347352
}
348353

0 commit comments

Comments
 (0)