Skip to content

Commit 7eb64b8

Browse files
committed
Auto merge of #46583 - scottmcm:fix-static-i128-lower, r=eddyb
Fix -Z lower_128bit_ops handling of statics Avoids ICEs such as the following: > error: internal compiler error: src\librustc_metadata\cstore_impl.rs:131: > get_optimized_mir: missing MIR for `DefId(8/0:40 ~ > compiler_builtins[9532]::int[0]::addsub[0]::rust_i128_addo[0])` r? @nagisa cc #45676 @est31
2 parents 81622c6 + 4b95ca8 commit 7eb64b8

File tree

3 files changed

+311
-4
lines changed

3 files changed

+311
-4
lines changed

src/librustc_trans/mir/constant.rs

+67
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,42 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
410410
}
411411
_ => span_bug!(span, "{:?} in constant", terminator.kind)
412412
}
413+
} else if let Some((op, is_checked)) = self.is_binop_lang_item(def_id) {
414+
(||{
415+
assert_eq!(arg_vals.len(), 2);
416+
let rhs = arg_vals.pop().unwrap()?;
417+
let lhs = arg_vals.pop().unwrap()?;
418+
if !is_checked {
419+
let binop_ty = op.ty(tcx, lhs.ty, rhs.ty);
420+
let (lhs, rhs) = (lhs.llval, rhs.llval);
421+
Ok(Const::new(const_scalar_binop(op, lhs, rhs, binop_ty),
422+
binop_ty))
423+
} else {
424+
let ty = lhs.ty;
425+
let val_ty = op.ty(tcx, lhs.ty, rhs.ty);
426+
let binop_ty = tcx.intern_tup(&[val_ty, tcx.types.bool], false);
427+
let (lhs, rhs) = (lhs.llval, rhs.llval);
428+
assert!(!ty.is_fp());
429+
430+
match const_scalar_checked_binop(tcx, op, lhs, rhs, ty) {
431+
Some((llval, of)) => {
432+
Ok(trans_const_adt(
433+
self.ccx,
434+
binop_ty,
435+
&mir::AggregateKind::Tuple,
436+
&[
437+
Const::new(llval, val_ty),
438+
Const::new(C_bool(self.ccx, of), tcx.types.bool)
439+
]))
440+
}
441+
None => {
442+
span_bug!(span,
443+
"{:?} got non-integer operands: {:?} and {:?}",
444+
op, Value(lhs), Value(rhs));
445+
}
446+
}
447+
}
448+
})()
413449
} else {
414450
MirConstContext::trans_def(self.ccx, def_id, substs, arg_vals)
415451
};
@@ -425,6 +461,37 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
425461
}
426462
}
427463

464+
fn is_binop_lang_item(&mut self, def_id: DefId) -> Option<(mir::BinOp, bool)> {
465+
let tcx = self.ccx.tcx();
466+
let items = tcx.lang_items();
467+
let def_id = Some(def_id);
468+
if items.i128_add_fn() == def_id { Some((mir::BinOp::Add, false)) }
469+
else if items.u128_add_fn() == def_id { Some((mir::BinOp::Add, false)) }
470+
else if items.i128_sub_fn() == def_id { Some((mir::BinOp::Sub, false)) }
471+
else if items.u128_sub_fn() == def_id { Some((mir::BinOp::Sub, false)) }
472+
else if items.i128_mul_fn() == def_id { Some((mir::BinOp::Mul, false)) }
473+
else if items.u128_mul_fn() == def_id { Some((mir::BinOp::Mul, false)) }
474+
else if items.i128_div_fn() == def_id { Some((mir::BinOp::Div, false)) }
475+
else if items.u128_div_fn() == def_id { Some((mir::BinOp::Div, false)) }
476+
else if items.i128_rem_fn() == def_id { Some((mir::BinOp::Rem, false)) }
477+
else if items.u128_rem_fn() == def_id { Some((mir::BinOp::Rem, false)) }
478+
else if items.i128_shl_fn() == def_id { Some((mir::BinOp::Shl, false)) }
479+
else if items.u128_shl_fn() == def_id { Some((mir::BinOp::Shl, false)) }
480+
else if items.i128_shr_fn() == def_id { Some((mir::BinOp::Shr, false)) }
481+
else if items.u128_shr_fn() == def_id { Some((mir::BinOp::Shr, false)) }
482+
else if items.i128_addo_fn() == def_id { Some((mir::BinOp::Add, true)) }
483+
else if items.u128_addo_fn() == def_id { Some((mir::BinOp::Add, true)) }
484+
else if items.i128_subo_fn() == def_id { Some((mir::BinOp::Sub, true)) }
485+
else if items.u128_subo_fn() == def_id { Some((mir::BinOp::Sub, true)) }
486+
else if items.i128_mulo_fn() == def_id { Some((mir::BinOp::Mul, true)) }
487+
else if items.u128_mulo_fn() == def_id { Some((mir::BinOp::Mul, true)) }
488+
else if items.i128_shlo_fn() == def_id { Some((mir::BinOp::Shl, true)) }
489+
else if items.u128_shlo_fn() == def_id { Some((mir::BinOp::Shl, true)) }
490+
else if items.i128_shro_fn() == def_id { Some((mir::BinOp::Shr, true)) }
491+
else if items.u128_shro_fn() == def_id { Some((mir::BinOp::Shr, true)) }
492+
else { None }
493+
}
494+
428495
fn store(&mut self,
429496
dest: &mir::Place<'tcx>,
430497
value: Result<Const<'tcx>, ConstEvalErr<'tcx>>,

src/test/mir-opt/lower_128bit_debug_test.rs

+122-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
// compile-flags: -Z lower_128bit_ops=yes -C debug_assertions=yes
1717

1818
#![feature(i128_type)]
19+
#![feature(const_fn)]
20+
21+
static TEST_SIGNED: i128 = const_signed(-222);
22+
static TEST_UNSIGNED: u128 = const_unsigned(200);
23+
24+
const fn const_signed(mut x: i128) -> i128 {
25+
((((((x + 1) - 2) * 3) / 4) % 5) << 6) >> 7
26+
}
27+
28+
const fn const_unsigned(mut x: u128) -> u128 {
29+
((((((x + 1) - 2) * 3) / 4) % 5) << 6) >> 7
30+
}
1931

2032
fn test_signed(mut x: i128) -> i128 {
2133
x += 1;
@@ -39,13 +51,121 @@ fn test_unsigned(mut x: u128) -> u128 {
3951
x
4052
}
4153

54+
fn check(x: i128, y: u128) {
55+
assert_eq!(test_signed(x), -1);
56+
assert_eq!(const_signed(x), -1);
57+
assert_eq!(TEST_SIGNED, -1);
58+
assert_eq!(test_unsigned(y), 2);
59+
assert_eq!(const_unsigned(y), 2);
60+
assert_eq!(TEST_UNSIGNED, 2);
61+
}
62+
4263
fn main() {
43-
assert_eq!(test_signed(-222), -1);
44-
assert_eq!(test_unsigned(200), 2);
64+
check(-222, 200);
4565
}
4666

4767
// END RUST SOURCE
4868

69+
// START rustc.const_signed.Lower128Bit.after.mir
70+
// _8 = _1;
71+
// _9 = const compiler_builtins::int::addsub::rust_i128_addo(move _8, const 1i128) -> bb10;
72+
// ...
73+
// _7 = move (_9.0: i128);
74+
// ...
75+
// _10 = const compiler_builtins::int::addsub::rust_i128_subo(move _7, const 2i128) -> bb11;
76+
// ...
77+
// _6 = move (_10.0: i128);
78+
// ...
79+
// _11 = const compiler_builtins::int::mul::rust_i128_mulo(move _6, const 3i128) -> bb12;
80+
// ...
81+
// _5 = move (_11.0: i128);
82+
// ...
83+
// _12 = Eq(const 4i128, const 0i128);
84+
// assert(!move _12, "attempt to divide by zero") -> bb4;
85+
// ...
86+
// _13 = Eq(const 4i128, const -1i128);
87+
// _14 = Eq(_5, const -170141183460469231731687303715884105728i128);
88+
// _15 = BitAnd(move _13, move _14);
89+
// assert(!move _15, "attempt to divide with overflow") -> bb5;
90+
// ...
91+
// _4 = const compiler_builtins::int::sdiv::rust_i128_div(move _5, const 4i128) -> bb13;
92+
// ...
93+
// _17 = Eq(const 5i128, const -1i128);
94+
// _18 = Eq(_4, const -170141183460469231731687303715884105728i128);
95+
// _19 = BitAnd(move _17, move _18);
96+
// assert(!move _19, "attempt to calculate the remainder with overflow") -> bb7;
97+
// ...
98+
// _3 = const compiler_builtins::int::sdiv::rust_i128_rem(move _4, const 5i128) -> bb15;
99+
// ...
100+
// _2 = move (_20.0: i128);
101+
// ...
102+
// _23 = const 7i32 as u128 (Misc);
103+
// _21 = const compiler_builtins::int::shift::rust_i128_shro(move _2, move _23) -> bb16;
104+
// ...
105+
// _0 = move (_21.0: i128);
106+
// ...
107+
// assert(!move (_9.1: bool), "attempt to add with overflow") -> bb1;
108+
// ...
109+
// assert(!move (_10.1: bool), "attempt to subtract with overflow") -> bb2;
110+
// ...
111+
// assert(!move (_11.1: bool), "attempt to multiply with overflow") -> bb3;
112+
// ...
113+
// _16 = Eq(const 5i128, const 0i128);
114+
// assert(!move _16, "attempt to calculate the remainder with a divisor of zero") -> bb6;
115+
// ...
116+
// assert(!move (_20.1: bool), "attempt to shift left with overflow") -> bb8;
117+
// ...
118+
// _22 = const 6i32 as u128 (Misc);
119+
// _20 = const compiler_builtins::int::shift::rust_i128_shlo(move _3, move _22) -> bb14;
120+
// ...
121+
// assert(!move (_21.1: bool), "attempt to shift right with overflow") -> bb9;
122+
// END rustc.const_signed.Lower128Bit.after.mir
123+
124+
// START rustc.const_unsigned.Lower128Bit.after.mir
125+
// _8 = _1;
126+
// _9 = const compiler_builtins::int::addsub::rust_u128_addo(move _8, const 1u128) -> bb8;
127+
// ...
128+
// _7 = move (_9.0: u128);
129+
// ...
130+
// _10 = const compiler_builtins::int::addsub::rust_u128_subo(move _7, const 2u128) -> bb9;
131+
// ...
132+
// _6 = move (_10.0: u128);
133+
// ...
134+
// _11 = const compiler_builtins::int::mul::rust_u128_mulo(move _6, const 3u128) -> bb10;
135+
// ...
136+
// _5 = move (_11.0: u128);
137+
// ...
138+
// _12 = Eq(const 4u128, const 0u128);
139+
// assert(!move _12, "attempt to divide by zero") -> bb4;
140+
// ...
141+
// _4 = const compiler_builtins::int::udiv::rust_u128_div(move _5, const 4u128) -> bb11;
142+
// ...
143+
// _3 = const compiler_builtins::int::udiv::rust_u128_rem(move _4, const 5u128) -> bb13;
144+
// ...
145+
// _2 = move (_14.0: u128);
146+
// ...
147+
// _17 = const 7i32 as u128 (Misc);
148+
// _15 = const compiler_builtins::int::shift::rust_u128_shro(move _2, move _17) -> bb14;
149+
// ...
150+
// _0 = move (_15.0: u128);
151+
// ...
152+
// assert(!move (_9.1: bool), "attempt to add with overflow") -> bb1;
153+
// ...
154+
// assert(!move (_10.1: bool), "attempt to subtract with overflow") -> bb2;
155+
// ...
156+
// assert(!move (_11.1: bool), "attempt to multiply with overflow") -> bb3;
157+
// ...
158+
// _13 = Eq(const 5u128, const 0u128);
159+
// assert(!move _13, "attempt to calculate the remainder with a divisor of zero") -> bb5;
160+
// ...
161+
// assert(!move (_14.1: bool), "attempt to shift left with overflow") -> bb6;
162+
// ...
163+
// _16 = const 6i32 as u128 (Misc);
164+
// _14 = const compiler_builtins::int::shift::rust_u128_shlo(move _3, move _16) -> bb12;
165+
// ...
166+
// assert(!move (_15.1: bool), "attempt to shift right with overflow") -> bb7;
167+
// END rustc.const_unsigned.Lower128Bit.after.mir
168+
49169
// START rustc.test_signed.Lower128Bit.after.mir
50170
// _2 = const compiler_builtins::int::addsub::rust_i128_addo(_1, const 1i128) -> bb10;
51171
// ...

src/test/mir-opt/lower_128bit_test.rs

+122-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
// compile-flags: -Z lower_128bit_ops=yes -C debug_assertions=no
1717

1818
#![feature(i128_type)]
19+
#![feature(const_fn)]
20+
21+
static TEST_SIGNED: i128 = const_signed(-222);
22+
static TEST_UNSIGNED: u128 = const_unsigned(200);
23+
24+
const fn const_signed(mut x: i128) -> i128 {
25+
((((((x + 1) - 2) * 3) / 4) % 5) << 6) >> 7
26+
}
27+
28+
const fn const_unsigned(mut x: u128) -> u128 {
29+
((((((x + 1) - 2) * 3) / 4) % 5) << 6) >> 7
30+
}
1931

2032
fn test_signed(mut x: i128) -> i128 {
2133
x += 1;
@@ -39,13 +51,121 @@ fn test_unsigned(mut x: u128) -> u128 {
3951
x
4052
}
4153

54+
fn check(x: i128, y: u128) {
55+
assert_eq!(test_signed(x), -1);
56+
assert_eq!(const_signed(x), -1);
57+
assert_eq!(TEST_SIGNED, -1);
58+
assert_eq!(test_unsigned(y), 2);
59+
assert_eq!(const_unsigned(y), 2);
60+
assert_eq!(TEST_UNSIGNED, 2);
61+
}
62+
4263
fn main() {
43-
assert_eq!(test_signed(-222), -1);
44-
assert_eq!(test_unsigned(200), 2);
64+
check(-222, 200);
4565
}
4666

4767
// END RUST SOURCE
4868

69+
// START rustc.const_signed.Lower128Bit.after.mir
70+
// _8 = _1;
71+
// _9 = const compiler_builtins::int::addsub::rust_i128_addo(move _8, const 1i128) -> bb10;
72+
// ...
73+
// _7 = move (_9.0: i128);
74+
// ...
75+
// _10 = const compiler_builtins::int::addsub::rust_i128_subo(move _7, const 2i128) -> bb11;
76+
// ...
77+
// _6 = move (_10.0: i128);
78+
// ...
79+
// _11 = const compiler_builtins::int::mul::rust_i128_mulo(move _6, const 3i128) -> bb12;
80+
// ...
81+
// _5 = move (_11.0: i128);
82+
// ...
83+
// _12 = Eq(const 4i128, const 0i128);
84+
// assert(!move _12, "attempt to divide by zero") -> bb4;
85+
// ...
86+
// _13 = Eq(const 4i128, const -1i128);
87+
// _14 = Eq(_5, const -170141183460469231731687303715884105728i128);
88+
// _15 = BitAnd(move _13, move _14);
89+
// assert(!move _15, "attempt to divide with overflow") -> bb5;
90+
// ...
91+
// _4 = const compiler_builtins::int::sdiv::rust_i128_div(move _5, const 4i128) -> bb13;
92+
// ...
93+
// _17 = Eq(const 5i128, const -1i128);
94+
// _18 = Eq(_4, const -170141183460469231731687303715884105728i128);
95+
// _19 = BitAnd(move _17, move _18);
96+
// assert(!move _19, "attempt to calculate the remainder with overflow") -> bb7;
97+
// ...
98+
// _3 = const compiler_builtins::int::sdiv::rust_i128_rem(move _4, const 5i128) -> bb15;
99+
// ...
100+
// _2 = move (_20.0: i128);
101+
// ...
102+
// _23 = const 7i32 as u128 (Misc);
103+
// _21 = const compiler_builtins::int::shift::rust_i128_shro(move _2, move _23) -> bb16;
104+
// ...
105+
// _0 = move (_21.0: i128);
106+
// ...
107+
// assert(!move (_9.1: bool), "attempt to add with overflow") -> bb1;
108+
// ...
109+
// assert(!move (_10.1: bool), "attempt to subtract with overflow") -> bb2;
110+
// ...
111+
// assert(!move (_11.1: bool), "attempt to multiply with overflow") -> bb3;
112+
// ...
113+
// _16 = Eq(const 5i128, const 0i128);
114+
// assert(!move _16, "attempt to calculate the remainder with a divisor of zero") -> bb6;
115+
// ...
116+
// assert(!move (_20.1: bool), "attempt to shift left with overflow") -> bb8;
117+
// ...
118+
// _22 = const 6i32 as u128 (Misc);
119+
// _20 = const compiler_builtins::int::shift::rust_i128_shlo(move _3, move _22) -> bb14;
120+
// ...
121+
// assert(!move (_21.1: bool), "attempt to shift right with overflow") -> bb9;
122+
// END rustc.const_signed.Lower128Bit.after.mir
123+
124+
// START rustc.const_unsigned.Lower128Bit.after.mir
125+
// _8 = _1;
126+
// _9 = const compiler_builtins::int::addsub::rust_u128_addo(move _8, const 1u128) -> bb8;
127+
// ...
128+
// _7 = move (_9.0: u128);
129+
// ...
130+
// _10 = const compiler_builtins::int::addsub::rust_u128_subo(move _7, const 2u128) -> bb9;
131+
// ...
132+
// _6 = move (_10.0: u128);
133+
// ...
134+
// _11 = const compiler_builtins::int::mul::rust_u128_mulo(move _6, const 3u128) -> bb10;
135+
// ...
136+
// _5 = move (_11.0: u128);
137+
// ...
138+
// _12 = Eq(const 4u128, const 0u128);
139+
// assert(!move _12, "attempt to divide by zero") -> bb4;
140+
// ...
141+
// _4 = const compiler_builtins::int::udiv::rust_u128_div(move _5, const 4u128) -> bb11;
142+
// ...
143+
// _3 = const compiler_builtins::int::udiv::rust_u128_rem(move _4, const 5u128) -> bb13;
144+
// ...
145+
// _2 = move (_14.0: u128);
146+
// ...
147+
// _17 = const 7i32 as u128 (Misc);
148+
// _15 = const compiler_builtins::int::shift::rust_u128_shro(move _2, move _17) -> bb14;
149+
// ...
150+
// _0 = move (_15.0: u128);
151+
// ...
152+
// assert(!move (_9.1: bool), "attempt to add with overflow") -> bb1;
153+
// ...
154+
// assert(!move (_10.1: bool), "attempt to subtract with overflow") -> bb2;
155+
// ...
156+
// assert(!move (_11.1: bool), "attempt to multiply with overflow") -> bb3;
157+
// ...
158+
// _13 = Eq(const 5u128, const 0u128);
159+
// assert(!move _13, "attempt to calculate the remainder with a divisor of zero") -> bb5;
160+
// ...
161+
// assert(!move (_14.1: bool), "attempt to shift left with overflow") -> bb6;
162+
// ...
163+
// _16 = const 6i32 as u128 (Misc);
164+
// _14 = const compiler_builtins::int::shift::rust_u128_shlo(move _3, move _16) -> bb12;
165+
// ...
166+
// assert(!move (_15.1: bool), "attempt to shift right with overflow") -> bb7;
167+
// END rustc.const_unsigned.Lower128Bit.after.mir
168+
49169
// START rustc.test_signed.Lower128Bit.after.mir
50170
// _1 = const compiler_builtins::int::addsub::rust_i128_add(_1, const 1i128) -> bb7;
51171
// ...

0 commit comments

Comments
 (0)