Skip to content

Commit e64a5a0

Browse files
committed
Treat undef bytes as equal to any other byte
1 parent 330a16b commit e64a5a0

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

compiler/rustc_codegen_llvm/src/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
126126
unsafe { llvm::LLVMGetUndef(t) }
127127
}
128128

129+
fn is_undef(&self, v: &'ll Value) -> bool {
130+
unsafe { llvm::LLVMIsUndef(v) == True }
131+
}
132+
129133
fn const_poison(&self, t: &'ll Type) -> &'ll Value {
130134
unsafe { llvm::LLVMGetPoison(t) }
131135
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ unsafe extern "C" {
917917
pub fn LLVMMetadataTypeInContext(C: &Context) -> &Type;
918918

919919
// Operations on all values
920+
pub fn LLVMIsUndef(Val: &Value) -> Bool;
920921
pub fn LLVMTypeOf(Val: &Value) -> &Type;
921922
pub fn LLVMGetValueName2(Val: &Value, Length: *mut size_t) -> *const c_char;
922923
pub fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
88
use rustc_middle::{bug, mir, span_bug};
99
use rustc_session::config::OptLevel;
1010
use rustc_span::{DUMMY_SP, Span};
11-
use tracing::{debug, instrument};
11+
use tracing::{debug, instrument, trace};
1212

1313
use super::FunctionCx;
1414
use super::operand::{OperandRef, OperandValue};
@@ -117,13 +117,33 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
117117
false
118118
};
119119

120+
trace!(?cg_elem.val);
120121
match cg_elem.val {
121122
OperandValue::Immediate(v) => {
122123
if try_init_all_same(bx, v) {
123124
return;
124125
}
125126
}
126-
_ => (),
127+
OperandValue::Pair(a, b) => {
128+
let a_is_undef = bx.cx().is_undef(a);
129+
match (a_is_undef, bx.cx().is_undef(b)) {
130+
// Can happen for uninit unions
131+
(true, true) => {
132+
// FIXME: can we produce better output here?
133+
}
134+
(false, true) | (true, false) => {
135+
let val = if a_is_undef { b } else { b };
136+
if try_init_all_same(bx, val) {
137+
return;
138+
}
139+
}
140+
(false, false) => {
141+
// FIXME: if both are the same value, use try_init_all_same
142+
}
143+
}
144+
}
145+
OperandValue::ZeroSized => unreachable!("checked above"),
146+
OperandValue::Ref(..) => {}
127147
}
128148

129149
let count = self

compiler/rustc_codegen_ssa/src/traits/consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub trait ConstCodegenMethods<'tcx>: BackendTypes {
99
/// Generate an uninitialized value (matching uninitialized memory in MIR).
1010
/// Whether memory is initialized or not is tracked byte-for-byte.
1111
fn const_undef(&self, t: Self::Type) -> Self::Value;
12+
fn is_undef(&self, v: Self::Value) -> bool;
1213
/// Generate a fake value. Poison always affects the entire value, even if just a single byte is
1314
/// poison. This can only be used in codepaths that are already UB, i.e., UB-free Rust code
1415
/// (including code that e.g. copies uninit memory with `MaybeUninit`) can never encounter a

tests/codegen/slice-init.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,14 @@ pub fn u16_init_one_bytes() -> [u16; N] {
7676
[const { u16::from_be_bytes([1, 1]) }; N]
7777
}
7878

79-
// FIXME: undef bytes can just be initialized with the same value as the
80-
// defined bytes, if the defines bytes are all the same.
8179
// CHECK-LABEL: @option_none_init
8280
#[no_mangle]
8381
pub fn option_none_init() -> [Option<u8>; N] {
8482
// CHECK-NOT: select
85-
// CHECK: br label %repeat_loop_header{{.*}}
83+
// CHECK-NOT: br
8684
// CHECK-NOT: switch
87-
// CHECK: icmp
88-
// CHECK-NOT: call void @llvm.memset.p0
85+
// CHECK-NOT: icmp
86+
// CHECK: call void @llvm.memset.p0
8987
[None; N]
9088
}
9189

0 commit comments

Comments
 (0)