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

Do not skip over fence instructions #99

Merged
merged 3 commits into from
Feb 6, 2024
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: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/polkavm-linker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polkavm-linker"
version = "0.8.1"
version = "0.8.2"
authors.workspace = true
license.workspace = true
edition.workspace = true
Expand Down
21 changes: 17 additions & 4 deletions crates/polkavm-linker/src/program_from_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,10 @@ fn convert_instruction(
emit(InstExt::Control(ControlInst::Unimplemented));
Ok(())
}
Inst::FenceI | Inst::Fence { .. } => Ok(()),
Inst::FenceI | Inst::Fence { .. } => {
emit(InstExt::Basic(BasicInst::Nop));
Ok(())
}
Inst::Load { kind, dst, base, offset } => {
let Some(dst) = cast_reg_non_zero(dst)? else {
return Err(ProgramFromElfError::other("found a load with a zero register as the destination"));
Expand Down Expand Up @@ -2046,16 +2049,21 @@ fn parse_code_section(
}
}

let original_length = output.len();
convert_instruction(section, current_location, original_inst, |inst| {
output.push((source, inst));
})?
})?;

// We need to always emit at least one instruction (even if it's a NOP) to handle potential jumps.
assert_ne!(output.len(), original_length, "internal error: no instructions were emitted");
}
}

Ok(())
}

fn split_code_into_basic_blocks(
elf: &Elf,
jump_targets: &HashSet<SectionTarget>,
instructions: Vec<(Source, InstExt<SectionTarget, SectionTarget>)>,
) -> Result<Vec<BasicBlock<SectionTarget, SectionTarget>>, ProgramFromElfError> {
Expand All @@ -2064,6 +2072,11 @@ fn split_code_into_basic_blocks(
let mut block_start_opt = None;
let mut last_source_in_block = None;
for (source, op) in instructions {
log::trace!(
"Instruction at {source} (0x{:x}): {op:?}",
elf.section_by_index(source.section_index).original_address() + source.offset_range.start
);

if let Some(last_source_in_block) = last_source_in_block {
// Handle the case where we've emitted multiple instructions from a single RISC-V instruction.
if source == last_source_in_block {
Expand Down Expand Up @@ -2228,7 +2241,7 @@ fn resolve_basic_block_references(
else {
return Err(ProgramFromElfError::other(format!(
"found control instruction at the end of block at {block_source} whose target doesn't resolve to any basic block: {next:?}",
block_source = block.source.begin(),
block_source = block.source,
next = block.next.instruction,
)));
};
Expand Down Expand Up @@ -6507,7 +6520,7 @@ pub fn program_from_elf(config: Config, data: &[u8]) -> Result<ProgramBlob, Prog
.collect();

let all_jump_targets = harvest_all_jump_targets(&elf, &data_sections_set, &code_sections_set, &instructions, &relocations, &exports)?;
let all_blocks = split_code_into_basic_blocks(&all_jump_targets, instructions)?;
let all_blocks = split_code_into_basic_blocks(&elf, &all_jump_targets, instructions)?;
for block in &all_blocks {
for source in block.next.source.as_slice() {
assert!(source.offset_range.start < source.offset_range.end);
Expand Down