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
42 changes: 42 additions & 0 deletions src/emit/ebpf/tracepoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ impl Builder {
pub fn compile_tracepoint(unit: &UnitIr) -> Result<CompiledProgram, String> {
let mut b = Builder::new(unit)?;
let branch_cond_vars = branch_condition_vars(unit);
let reachable_blocks = reachable_block_ids(unit);
b.push(Insn::new(BPF_MOV64_REG, R6, R1, 0, 0));

for block in &unit.blocks {
if !reachable_blocks.contains(&block.id.0) {
continue;
}
b.block_offsets.insert(block.id.0, b.insns.len());

for inst in &block.instructions {
Expand Down Expand Up @@ -322,6 +326,44 @@ pub fn compile_tracepoint(unit: &UnitIr) -> Result<CompiledProgram, String> {
})
}

fn reachable_block_ids(unit: &UnitIr) -> HashSet<u32> {
let blocks = unit
.blocks
.iter()
.map(|block| (block.id.0, block))
.collect::<HashMap<_, _>>();
let entry = match unit.blocks.first() {
Some(block) => block.id.0,
None => return HashSet::new(),
};

let mut reachable = HashSet::new();
let mut pending = vec![entry];
while let Some(block_id) = pending.pop() {
if !reachable.insert(block_id) {
continue;
}

let Some(block) = blocks.get(&block_id) else {
continue;
};
match block.terminator {
Terminator::Return(_) => {}
Terminator::Jump(target) => pending.push(target.0),
Terminator::Branch {
true_block,
false_block,
..
} => {
pending.push(true_block.0);
pending.push(false_block.0);
}
}
}

reachable
}

fn branch_condition_vars(unit: &UnitIr) -> HashSet<u32> {
let mut out = HashSet::new();
for block in &unit.blocks {
Expand Down
2 changes: 0 additions & 2 deletions src/parser/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn parse_unit(parser: &mut Parser) -> Result<Unit, ParseError> {

while !parser.check(TokenKind::RBrace) {
if parser.r#match(TokenKind::KeywordSection) {
expect_token(parser, TokenKind::Colon)?;
let s = parser.expect(TokenKind::StringLiteral)?;

let txt = s.lexeme.trim_matches('"').to_string();
Expand All @@ -31,7 +30,6 @@ pub fn parse_unit(parser: &mut Parser) -> Result<Unit, ParseError> {
}

if parser.r#match(TokenKind::KeywordLicense) {
expect_token(parser, TokenKind::Colon)?;
let s = parser.expect(TokenKind::StringLiteral)?;

let txt = s.lexeme.trim_matches('"').to_string();
Expand Down
Loading