Skip to content

Fix UEFI compilation for cranelift-jit#12602

Draft
RossComputerGuy wants to merge 1 commit intobytecodealliance:mainfrom
RossComputerGuy:fix/jit-uefi
Draft

Fix UEFI compilation for cranelift-jit#12602
RossComputerGuy wants to merge 1 commit intobytecodealliance:mainfrom
RossComputerGuy:fix/jit-uefi

Conversation

@RossComputerGuy
Copy link

Fixes enough of the errors to get cranelift-jit compiled to UEFI via:

$ RUSTFLAGS="--cfg uefi_std" cargo build --target aarch64-unknown-uefi

Requires changes to region-rs but it hasn't been touched in 2 years. darfink/region-rs#39

@github-actions github-actions bot added the cranelift Issues related to the Cranelift code generator label Feb 17, 2026
@RossComputerGuy
Copy link
Author

[TRACE] cranelift/codegen/src/machinst/lower.rs:413 bb block0 param v0: regs ValueRegs(v192)
[TRACE] cranelift/codegen/src/machinst/lower.rs:422 bb block0 inst inst0 (UnaryImm { opcode: Iconst, imm: Imm64(37) }): result v1 regs ValueRegs(v193)
[TRACE] cranelift/codegen/src/machinst/lower.rs:422 bb block0 inst inst1 (Binary { opcode: Iadd, args: [v1, v0] }): result v2 regs ValueRegs(v194)
[TRACE] cranelift/codegen/src/machinst/lower.rs:493 bb block0 inst inst0 has color 1
[TRACE] cranelift/codegen/src/machinst/lower.rs:502  -> constant: 37
[TRACE] cranelift/codegen/src/machinst/lower.rs:493 bb block0 inst inst1 has color 1
[TRACE] cranelift/codegen/src/machinst/lower.rs:493 bb block0 inst inst2 has color 1
[TRACE] cranelift/codegen/src/machinst/lower.rs:496  -> side-effecting; incrementing color for next inst
[TRACE] cranelift/codegen/src/machinst/lower.rs:1313 arg v1 used, old state Unused, new Once
[TRACE] cranelift/codegen/src/machinst/lower.rs:1313 arg v0 used, old state Unused, new Once
[TRACE] cranelift/codegen/src/machinst/lower.rs:1313 arg v2 used, old state Unused, new Once
[DEBUG] cranelift/codegen/src/machinst/compile.rs:35 Number of CLIF instructions to lower: 3
[DEBUG] cranelift/codegen/src/machinst/compile.rs:39 Number of CLIF blocks to lower: 1
[TRACE] cranelift/codegen/src/machinst/lower.rs:1140 about to lower function: function u0:0(i32) -> i32 tail {
block0(v0: i32):
    v1 = iconst.i32 37
    v2 = iadd v1, v0  ; v1 = 37
    return v2
}

[TRACE] cranelift/codegen/src/machinst/lower.rs:776 lower_clif_block: block block0 inst inst2 (MultiAry { opcode: Return, args: EntityList { index: 13, unused: PhantomData<cranelift_codegen::ir::entities::Value> } }) is_branch false side_effect true value_needed false
[TRACE] cranelift/codegen/src/machinst/lower.rs:810 lowering: inst inst2: return v2


Synchronous Exception at 0x000000014043BF50


Synchronous Exception at 0x000000014043BF50

It seems the example crashes in UEFI. May need help figuring this problem out.

@cfallin
Copy link
Member

cfallin commented Feb 17, 2026

@RossComputerGuy thanks for the PR.

Unfortunately I don't think there is much that we can go on regarding your observed crash. The quoted log shows normal compilation followed by "synchronous exception", nothing in-between, and you've told us nothing about your environment or what you're trying to do.

I can make some educated guesses -- e.g., have you set pages to have execute permission? Is that a thing in your environment (do you have paging enabled)? Are you properly managing other requirements of the ABI (e.g. stack alignment)? Does the code that you're compiling have any bugs that could result in a bad memory access?

Basically, for an actionable bug report, we need: (i) information about what you're trying to do; (ii) information about what happened; (iii) information about why you believe something else should have happened; (iv) whatever investigation you've done that narrows down where this divergence is occurring, ideally pointing in the direction of a Cranelift issue. Thanks!

@RossComputerGuy
Copy link
Author

I did narrow it down to this block:

let func_a = module
.declare_function("a", Linkage::Local, &sig_a)
.unwrap();
let func_b = module
.declare_function("b", Linkage::Local, &sig_b)
.unwrap();
ctx.func.signature = sig_a;
ctx.func.name = UserFuncName::user(0, func_a.as_u32());
{
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
let block = bcx.create_block();
bcx.switch_to_block(block);
bcx.append_block_params_for_function_params(block);
let param = bcx.block_params(block)[0];
let cst = bcx.ins().iconst(types::I32, 37);
let add = bcx.ins().iadd(cst, param);
bcx.ins().return_(&[add]);
bcx.seal_all_blocks();
bcx.finalize();
}
module.define_function(func_a, &mut ctx).unwrap();
module.clear_context(&mut ctx);

I'll see if I can get more details.

@cfallin
Copy link
Member

cfallin commented Feb 17, 2026

OK, thanks. That's a function body that returns x + 37, and it compiles to reasonable (and correct) machine code in Cranelift. I suspect there is something wrong with your execution environment in general, which is why I was asking about execute permissions, stack alignment, paging, and all of that.

@RossComputerGuy
Copy link
Author

Yes but isn't that block not even executing anything? I thought that's just doing codegen. I do have region set up to work with paging which hopefully is correct. The problem is UEFI doesn't really have stack traces so it's hard to figure out what is going awry.

@bjorn3
Copy link
Contributor

bjorn3 commented Feb 17, 2026

target-lexicon's default_calling_convention method doesn't look like it handles uefi. Maybe that is the issue? On x86_64 uefi uses the windows callconv.

@RossComputerGuy
Copy link
Author

That is correct, however this is aarch64 that I am compiling for.

@cfallin
Copy link
Member

cfallin commented Feb 17, 2026

Ah, I see, I thought you meant you had narrowed down the exception to the code produced by that IR emission sequence. You mean literally there is a crash in that IR emission sequence...

Do you have a panic handler that can print a panic message at least?

@RossComputerGuy
Copy link
Author

Yeah no, it doesn't even get to the point of emitting the binary. I haven't attached a panic handler but I will. I know there's also a UEFI debug protocol which should allow me to attach into the interrupt vector so hopefully I can catch exceptions and deal with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants