Skip to content

Commit 07a9c10

Browse files
authored
Rollup merge of #101612 - tmiasko:repeat128, r=lcnr
Fix code generation of `Rvalue::Repeat` with 128 bit values Closes #101585.
2 parents ae49732 + e4d3abf commit 07a9c10

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

compiler/rustc_codegen_llvm/src/common.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
215215
}
216216

217217
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
218-
try_as_const_integral(v).map(|v| unsafe { llvm::LLVMConstIntGetZExtValue(v) })
218+
try_as_const_integral(v).and_then(|v| unsafe {
219+
let mut i = 0u64;
220+
let success = llvm::LLVMRustConstIntGetZExtValue(v, &mut i);
221+
success.then_some(i)
222+
})
219223
}
220224

221225
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ extern "C" {
10961096
pub fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
10971097
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
10981098
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
1099-
pub fn LLVMConstIntGetZExtValue(ConstantVal: &ConstantInt) -> c_ulonglong;
1099+
pub fn LLVMRustConstIntGetZExtValue(ConstantVal: &ConstantInt, Value: &mut u64) -> bool;
11001100
pub fn LLVMRustConstInt128Get(
11011101
ConstantVal: &ConstantInt,
11021102
SExt: bool,

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8787
let size = bx.const_usize(dest.layout.size.bytes());
8888

8989
// Use llvm.memset.p0i8.* to initialize all zero arrays
90-
if bx.cx().const_to_opt_uint(v) == Some(0) {
90+
if bx.cx().const_to_opt_u128(v, false) == Some(0) {
9191
let fill = bx.cx().const_u8(0);
9292
bx.memset(start, fill, size, dest.align, MemFlags::empty());
9393
return bx;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,14 @@ extern "C" LLVMValueRef LLVMRustConstInBoundsGEP2(LLVMTypeRef Ty,
16181618
return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
16191619
}
16201620

1621+
extern "C" bool LLVMRustConstIntGetZExtValue(LLVMValueRef CV, uint64_t *value) {
1622+
auto C = unwrap<llvm::ConstantInt>(CV);
1623+
if (C->getBitWidth() > 64)
1624+
return false;
1625+
*value = C->getZExtValue();
1626+
return true;
1627+
}
1628+
16211629
// Returns true if both high and low were successfully set. Fails in case constant wasn’t any of
16221630
// the common sizes (1, 8, 16, 32, 64, 128 bits)
16231631
extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *high, uint64_t *low)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test for issue 101585.
2+
// run-pass
3+
4+
fn main() {
5+
fn min_array_ok() -> [i128; 1] {
6+
[i128::MIN]
7+
}
8+
assert_eq!(min_array_ok(), [-170141183460469231731687303715884105728i128]);
9+
10+
fn min_array_nok() -> [i128; 1] {
11+
[i128::MIN; 1]
12+
}
13+
assert_eq!(min_array_nok(), [-170141183460469231731687303715884105728i128]);
14+
}

0 commit comments

Comments
 (0)