Skip to content

Commit 373789b

Browse files
committed
Don't use global initializer if type does not match
This was relying on the presence of a bitcast to avoid using the constant global initializer for a load using a different type. With opaque pointers, we need to check this explicitly.
1 parent b2eba05 commit 373789b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -509,15 +509,20 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
509509
OperandValue::Ref(place.llval, Some(llextra), place.align)
510510
} else if place.layout.is_llvm_immediate() {
511511
let mut const_llval = None;
512+
let llty = place.layout.llvm_type(self);
512513
unsafe {
513514
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
514515
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
515-
const_llval = llvm::LLVMGetInitializer(global);
516+
if let Some(init) = llvm::LLVMGetInitializer(global) {
517+
if self.val_ty(init) == llty {
518+
const_llval = Some(init);
519+
}
520+
}
516521
}
517522
}
518523
}
519524
let llval = const_llval.unwrap_or_else(|| {
520-
let load = self.load(place.layout.llvm_type(self), place.llval, place.align);
525+
let load = self.load(llty, place.llval, place.align);
521526
if let abi::Abi::Scalar(scalar) = place.layout.abi {
522527
scalar_load_metadata(self, load, scalar, place.layout, Size::ZERO);
523528
}

0 commit comments

Comments
 (0)