Skip to content

fix panic calling code and iteration order #96

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

Merged
merged 3 commits into from
Apr 2, 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
46 changes: 26 additions & 20 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,27 +798,15 @@ unsafe fn create_call<'a>(tgt: &'a Value, src: &'a Value, rev_mode: bool,
LLVMPositionBuilderAtEnd(builder, fail_bb);


let panic_name: CString = get_panic_name(llmod);

let mut arg_vec = vec![add_panic_msg_to_global(llmod, llcx)];
let name1 = "_ZN4core9panicking14panic_explicit17h8607a79b2acfb83bE";
let name2 = "_RN4core9panicking14panic_explicit17h8607a79b2acfb83bE";
let cname1 = CString::new(name1).unwrap();
let cname2 = CString::new(name2).unwrap();

let fnc1 = llvm::LLVMGetNamedFunction(llmod, cname1.as_ptr() as *const c_char);
let call;
if fnc1.is_none() {
let fnc2 = llvm::LLVMGetNamedFunction(llmod, cname2.as_ptr() as *const c_char);
assert!(fnc2.is_some());
let fnc2 = fnc2.unwrap();
let ty = LLVMRustGetFunctionType(fnc2);
// now call with msg
call = LLVMBuildCall2(builder, ty, fnc2, arg_vec.as_mut_ptr(), arg_vec.len(), name2.as_ptr() as *const c_char);
} else {
let fnc1 = fnc1.unwrap();
let ty = LLVMRustGetFunctionType(fnc1);
call = LLVMBuildCall2(builder, ty, fnc1, arg_vec.as_mut_ptr(), arg_vec.len(), name1.as_ptr() as *const c_char);
}

let fnc1 = llvm::LLVMGetNamedFunction(llmod, panic_name.as_ptr() as *const c_char);
assert!(fnc1.is_some());
let fnc1 = fnc1.unwrap();
let ty = LLVMRustGetFunctionType(fnc1);
let call = LLVMBuildCall2(builder, ty, fnc1, arg_vec.as_mut_ptr(), arg_vec.len(), panic_name.as_ptr() as *const c_char);
llvm::LLVMSetTailCall(call, 1);
llvm::LLVMBuildUnreachable(builder);
LLVMPositionBuilderAtEnd(builder, success_bb);
Expand Down Expand Up @@ -877,7 +865,25 @@ unsafe fn create_call<'a>(tgt: &'a Value, src: &'a Value, rev_mode: bool,
let _fnc_ok =
LLVMVerifyFunction(tgt, llvm::LLVMVerifierFailureAction::LLVMAbortProcessAction);
}

unsafe fn get_panic_name(llmod: &llvm::Module) -> CString {
// The names are mangled and their ending changes based on a hash, so just take whichever.
let mut f = LLVMGetFirstFunction(llmod);
loop {
if let Some(lf) = f {
f = LLVMGetNextFunction(lf);
let fnc_name = llvm::get_value_name(lf);
let fnc_name: String = String::from_utf8(fnc_name.to_vec()).unwrap();
if fnc_name.starts_with("_ZN4core9panicking14panic_explicit") {
return CString::new(fnc_name).unwrap();
} else if fnc_name.starts_with("_RN4core9panicking14panic_explicit") {
return CString::new(fnc_name).unwrap();
}
} else {
break;
}
}
panic!("Could not find panic function");
}
unsafe fn add_panic_msg_to_global<'a>(llmod: &'a llvm::Module, llcx: &'a llvm::Context) -> &'a llvm::Value {
use llvm::*;

Expand Down
15 changes: 12 additions & 3 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,8 @@ pub fn fnc_typetrees<'tcx>(tcx: TyCtxt<'tcx>, fn_ty: Ty<'tcx>, da: &mut Vec<Diff
// https://discord.com/channels/273534239310479360/957720175619215380/1223454360676208751
let x = tcx.instantiate_bound_regions_with_erased(fnc_binder);

let mut offset = 0;
let mut new_activities = vec![];
let mut new_positions = vec![];
let mut visited = vec![];
let mut args = vec![];
for (i, ty) in x.inputs().iter().enumerate() {
Expand Down Expand Up @@ -2771,8 +2772,8 @@ pub fn fnc_typetrees<'tcx>(tcx: TyCtxt<'tcx>, fn_ty: Ty<'tcx>, da: &mut Vec<Diff
DiffActivity::Const => DiffActivity::Const,
_ => panic!("unexpected activity for ptr/ref"),
};
da.insert(i + 1 + offset, activity);
offset += 1;
new_activities.push(activity);
new_positions.push(i + 1);
}
trace!("ABI MATCHING!");
continue;
Expand All @@ -2782,6 +2783,14 @@ pub fn fnc_typetrees<'tcx>(tcx: TyCtxt<'tcx>, fn_ty: Ty<'tcx>, da: &mut Vec<Diff
args.push(arg_tt);
}

// now add the extra activities coming from slices
// Reverse order to not invalidate the indices
for _ in 0..new_activities.len() {
let pos = new_positions.pop().unwrap();
let activity = new_activities.pop().unwrap();
da.insert(pos, activity);
}

visited.clear();
let ret = typetree_from_ty(x.output(), tcx, 0, false, &mut visited);

Expand Down