Skip to content

Commit 5d77191

Browse files
committed
Use a C-safe return type for __rust_[ui]128_* overflowing intrinsics
Combined with [1], this will change the overflowing multiplication operations to return an `extern "C"`-safe type. Link: rust-lang/compiler-builtins#735 [1]
1 parent a2d7c81 commit 5d77191

File tree

4 files changed

+73
-74
lines changed

4 files changed

+73
-74
lines changed

compiler/rustc_codegen_cranelift/src/codegen_i128.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,22 @@ pub(crate) fn maybe_codegen_mul_checked<'tcx>(
7676
}
7777

7878
let is_signed = type_sign(lhs.layout().ty);
79-
80-
let out_ty = Ty::new_tup(fx.tcx, &[lhs.layout().ty, fx.tcx.types.bool]);
81-
let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty));
79+
let oflow_out_place = CPlace::new_stack_slot(fx, fx.layout_of(fx.tcx.types.i32));
8280
let param_types = vec![
83-
AbiParam::special(fx.pointer_type, ArgumentPurpose::StructReturn),
8481
AbiParam::new(types::I128),
8582
AbiParam::new(types::I128),
83+
AbiParam::special(fx.pointer_type, ArgumentPurpose::Normal),
8684
];
87-
let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)];
88-
fx.lib_call(
85+
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx), oflow_out_place.to_ptr().get_addr(fx)];
86+
let ret = fx.lib_call(
8987
if is_signed { "__rust_i128_mulo" } else { "__rust_u128_mulo" },
9088
param_types,
91-
vec![],
89+
vec![AbiParam::new(types::I128)],
9290
&args,
9391
);
94-
Some(out_place.to_cvalue(fx))
92+
let mul = ret[0];
93+
let oflow = oflow_out_place.to_cvalue(fx).load_scalar(fx);
94+
let oflow = clif_intcast(fx, oflow, types::I8, false);
95+
let layout = fx.layout_of(Ty::new_tup(fx.tcx, &[lhs.layout().ty, fx.tcx.types.bool]));
96+
Some(CValue::by_val_pair(mul, oflow, layout))
9597
}

compiler/rustc_codegen_cranelift/src/compiler_builtins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ builtin_functions! {
4343
fn __divti3(n: i128, d: i128) -> i128;
4444
fn __umodti3(n: u128, d: u128) -> u128;
4545
fn __modti3(n: i128, d: i128) -> i128;
46-
fn __rust_u128_mulo(a: u128, b: u128) -> (u128, bool);
46+
fn __rust_u128_mulo(a: u128, b: u128, oflow: &mut i32) -> u128;
4747

4848
// floats
4949
fn __floattisf(i: i128) -> f32;

compiler/rustc_codegen_gcc/src/int.rs

+58-63
Original file line numberDiff line numberDiff line change
@@ -322,36 +322,26 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
322322
},
323323
}
324324
} else {
325-
match new_kind {
326-
Int(I128) | Uint(U128) => {
327-
let func_name = match oop {
328-
OverflowOp::Add => match new_kind {
329-
Int(I128) => "__rust_i128_addo",
330-
Uint(U128) => "__rust_u128_addo",
331-
_ => unreachable!(),
332-
},
333-
OverflowOp::Sub => match new_kind {
334-
Int(I128) => "__rust_i128_subo",
335-
Uint(U128) => "__rust_u128_subo",
336-
_ => unreachable!(),
337-
},
338-
OverflowOp::Mul => match new_kind {
339-
Int(I128) => "__rust_i128_mulo", // TODO(antoyo): use __muloti4d instead?
340-
Uint(U128) => "__rust_u128_mulo",
341-
_ => unreachable!(),
342-
},
343-
};
344-
return self.operation_with_overflow(func_name, lhs, rhs);
345-
}
346-
_ => match oop {
347-
OverflowOp::Mul => match new_kind {
348-
Int(I32) => "__mulosi4",
349-
Int(I64) => "__mulodi4",
350-
_ => unreachable!(),
351-
},
352-
_ => unimplemented!("overflow operation for {:?}", new_kind),
325+
let (func_name, width) = match oop {
326+
OverflowOp::Add => match new_kind {
327+
Int(I128) => ("__rust_i128_addo", 128),
328+
Uint(U128) => ("__rust_u128_addo", 128),
329+
_ => unreachable!(),
353330
},
354-
}
331+
OverflowOp::Sub => match new_kind {
332+
Int(I128) => ("__rust_i128_subo", 128),
333+
Uint(U128) => ("__rust_u128_subo", 128),
334+
_ => unreachable!(),
335+
},
336+
OverflowOp::Mul => match new_kind {
337+
Int(I32) => ("__mulosi4", 32),
338+
Int(I64) => ("__mulodi4", 64),
339+
Int(I128) => ("__rust_i128_mulo", 128), // TODO(antoyo): use __muloti4d instead?
340+
Uint(U128) => ("__rust_u128_mulo", 128),
341+
_ => unreachable!(),
342+
},
343+
};
344+
return self.operation_with_overflow(func_name, lhs, rhs, width);
355345
};
356346

357347
let intrinsic = self.context.get_builtin_function(name);
@@ -364,80 +354,85 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
364354
(res.dereference(self.location).to_rvalue(), overflow)
365355
}
366356

357+
/// Non-`__builtin_*` overflow operations with a `fn(T, T, &mut i32) -> T` signature.
367358
pub fn operation_with_overflow(
368359
&self,
369360
func_name: &str,
370361
lhs: RValue<'gcc>,
371362
rhs: RValue<'gcc>,
363+
width: u64,
372364
) -> (RValue<'gcc>, RValue<'gcc>) {
373365
let a_type = lhs.get_type();
374366
let b_type = rhs.get_type();
375367
debug_assert!(a_type.dyncast_array().is_some());
376368
debug_assert!(b_type.dyncast_array().is_some());
369+
let oflow_type = self.i32_type;
370+
let oflow_param_type = oflow_type.make_pointer();
371+
let res_type = a_type;
372+
373+
let oflow_value = self.current_func().new_local(self.location, oflow_type, "overflow");
374+
let oflow_addr = oflow_value.get_address(self.location);
375+
377376
let param_a = self.context.new_parameter(self.location, a_type, "a");
378377
let param_b = self.context.new_parameter(self.location, b_type, "b");
379-
let result_field = self.context.new_field(self.location, a_type, "result");
380-
let overflow_field = self.context.new_field(self.location, self.bool_type, "overflow");
381-
382-
let ret_ty = Ty::new_tup(self.tcx, &[self.tcx.types.i128, self.tcx.types.bool]);
378+
let param_oflow = self.context.new_parameter(self.location, oflow_param_type, "oflow");
379+
380+
let a_elem_type = a_type.dyncast_array().expect("non-array a value");
381+
debug_assert!(a_elem_type.is_integral());
382+
let res_ty = match width {
383+
32 => self.tcx.types.i32,
384+
64 => self.tcx.types.i64,
385+
128 => self.tcx.types.i128,
386+
_ => unreachable!("unexpected integer size"),
387+
};
383388
let layout = self
384389
.tcx
385-
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ret_ty))
390+
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(res_ty))
386391
.unwrap();
387392

388393
let arg_abi = ArgAbi { layout, mode: PassMode::Direct(ArgAttributes::new()) };
389394
let mut fn_abi = FnAbi {
390-
args: vec![arg_abi.clone(), arg_abi.clone()].into_boxed_slice(),
395+
args: vec![arg_abi.clone(), arg_abi.clone(), arg_abi.clone()].into_boxed_slice(),
391396
ret: arg_abi,
392397
c_variadic: false,
393-
fixed_count: 2,
398+
fixed_count: 3,
394399
conv: Conv::C,
395400
can_unwind: false,
396401
};
397402
fn_abi.adjust_for_foreign_abi(self.cx, spec::abi::Abi::C { unwind: false }).unwrap();
398403

399-
let indirect = matches!(fn_abi.ret.mode, PassMode::Indirect { .. });
400-
401-
let return_type = self
402-
.context
403-
.new_struct_type(self.location, "result_overflow", &[result_field, overflow_field]);
404-
let result = if indirect {
405-
let return_value =
406-
self.current_func().new_local(self.location, return_type.as_type(), "return_value");
407-
let return_param_type = return_type.as_type().make_pointer();
408-
let return_param =
409-
self.context.new_parameter(self.location, return_param_type, "return_value");
404+
let ret_indirect = matches!(fn_abi.ret.mode, PassMode::Indirect { .. });
405+
406+
let result = if ret_indirect {
407+
let res_value = self.current_func().new_local(self.location, res_type, "result_value");
408+
let res_addr = res_value.get_address(self.location);
409+
let res_param_type = res_type.make_pointer();
410+
let param_res = self.context.new_parameter(self.location, res_param_type, "result");
411+
410412
let func = self.context.new_function(
411413
self.location,
412414
FunctionType::Extern,
413415
self.type_void(),
414-
&[return_param, param_a, param_b],
416+
&[param_res, param_a, param_b, param_oflow],
415417
func_name,
416418
false,
417419
);
418-
self.llbb().add_eval(
419-
self.location,
420-
self.context.new_call(self.location, func, &[
421-
return_value.get_address(self.location),
422-
lhs,
423-
rhs,
424-
]),
425-
);
426-
return_value.to_rvalue()
420+
let _void =
421+
self.context.new_call(self.location, func, &[res_addr, lhs, rhs, oflow_addr]);
422+
res_value.to_rvalue()
427423
} else {
428424
let func = self.context.new_function(
429425
self.location,
430426
FunctionType::Extern,
431-
return_type.as_type(),
432-
&[param_a, param_b],
427+
res_type,
428+
&[param_a, param_b, param_oflow],
433429
func_name,
434430
false,
435431
);
436-
self.context.new_call(self.location, func, &[lhs, rhs])
432+
self.context.new_call(self.location, func, &[lhs, rhs, oflow_addr])
437433
};
438-
let overflow = result.access_field(self.location, overflow_field);
439-
let int_result = result.access_field(self.location, result_field);
440-
(int_result, overflow)
434+
435+
(result, self.context.new_cast(self.location, oflow_value, self.bool_type).to_rvalue())
441436
}
442437

443438
pub fn gcc_icmp(

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,8 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
988988
128 => "__rust_i128_addo",
989989
_ => unreachable!(),
990990
};
991-
let (int_result, overflow) = self.operation_with_overflow(func_name, lhs, rhs);
991+
let (int_result, overflow) =
992+
self.operation_with_overflow(func_name, lhs, rhs, width);
992993
self.llbb().add_assignment(self.location, res, int_result);
993994
overflow
994995
};
@@ -1058,7 +1059,8 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
10581059
128 => "__rust_i128_subo",
10591060
_ => unreachable!(),
10601061
};
1061-
let (int_result, overflow) = self.operation_with_overflow(func_name, lhs, rhs);
1062+
let (int_result, overflow) =
1063+
self.operation_with_overflow(func_name, lhs, rhs, width);
10621064
self.llbb().add_assignment(self.location, res, int_result);
10631065
overflow
10641066
};

0 commit comments

Comments
 (0)