Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid bitcast taking bool out of a union represented as a scalar #56097

Merged
merged 2 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/librustc_codegen_ssa/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,24 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
};

// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
// Bools in union fields needs to be truncated.
let to_immediate_or_cast = |bx: &mut Bx, val, ty| {
if ty == bx.cx().type_i1() {
bx.trunc(val, ty)
} else {
bx.bitcast(val, ty)
}
};

match val {
OperandValue::Immediate(ref mut llval) => {
*llval = bx.bitcast(*llval, bx.cx().immediate_backend_type(field));
*llval = to_immediate_or_cast(bx, *llval, bx.cx().immediate_backend_type(field));
}
OperandValue::Pair(ref mut a, ref mut b) => {
*a = bx.bitcast(*a, bx.cx().scalar_pair_element_backend_type(field, 0, true));
*b = bx.bitcast(*b, bx.cx().scalar_pair_element_backend_type(field, 1, true));
*a = to_immediate_or_cast(bx, *a, bx.cx()
.scalar_pair_element_backend_type(field, 0, true));
*b = to_immediate_or_cast(bx, *b, bx.cx()
.scalar_pair_element_backend_type(field, 1, true));
}
OperandValue::Ref(..) => bug!()
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/codegen/union-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ pub union CUnionU128{a:u128}
#[no_mangle]
pub fn test_CUnionU128(_: CUnionU128) { loop {} }

pub union UnionBool { b:bool }
// CHECK: define zeroext i1 @test_UnionBool(i8 %b)
#[no_mangle]
pub fn test_UnionBool(b: UnionBool) -> bool { unsafe { b.b } }
// CHECK: %0 = trunc i8 %b to i1