Skip to content

Commit af6008d

Browse files
Auto merge of #145817 - Zalathar:llvm-bool, r=<try>
cg_llvm: Replace the `llvm::Bool` typedef with a proper newtype try-job: dist-various-2
2 parents a1dbb44 + b4e97e5 commit af6008d

File tree

14 files changed

+103
-63
lines changed

14 files changed

+103
-63
lines changed

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use smallvec::SmallVec;
1212

1313
use crate::builder::SBuilder;
1414
use crate::declare::declare_simple_fn;
15-
use crate::llvm::{self, False, True, Type, Value};
15+
use crate::llvm::{self, FALSE, TRUE, Type, Value};
1616
use crate::{SimpleCx, attributes, debuginfo, llvm_util};
1717

1818
pub(crate) unsafe fn codegen(
@@ -80,7 +80,7 @@ pub(crate) unsafe fn codegen(
8080
&cx,
8181
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
8282
&i8,
83-
&llvm::LLVMConstInt(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as u64, False),
83+
&llvm::LLVMConstInt(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as u64, FALSE),
8484
);
8585

8686
// __rust_no_alloc_shim_is_unstable_v2
@@ -201,7 +201,7 @@ fn create_wrapper_function(
201201
.map(|(i, _)| llvm::get_param(llfn, i as c_uint))
202202
.collect::<Vec<_>>();
203203
let ret = bx.call(ty, callee, &args, None);
204-
llvm::LLVMSetTailCall(ret, True);
204+
llvm::LLVMSetTailCall(ret, TRUE);
205205
if output.is_some() {
206206
bx.ret(ret);
207207
} else {

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tracing::debug;
1616
use crate::builder::Builder;
1717
use crate::common::Funclet;
1818
use crate::context::CodegenCx;
19+
use crate::llvm::ToLlvmBool;
1920
use crate::type_::Type;
2021
use crate::type_of::LayoutLlvmExt;
2122
use crate::value::Value;
@@ -470,10 +471,6 @@ pub(crate) fn inline_asm_call<'ll>(
470471
dest: Option<&'ll llvm::BasicBlock>,
471472
catch_funclet: Option<(&'ll llvm::BasicBlock, Option<&Funclet<'ll>>)>,
472473
) -> Option<&'ll Value> {
473-
let volatile = if volatile { llvm::True } else { llvm::False };
474-
let alignstack = if alignstack { llvm::True } else { llvm::False };
475-
let can_throw = if unwind { llvm::True } else { llvm::False };
476-
477474
let argtys = inputs
478475
.iter()
479476
.map(|v| {
@@ -500,10 +497,10 @@ pub(crate) fn inline_asm_call<'ll>(
500497
asm.len(),
501498
cons.as_ptr(),
502499
cons.len(),
503-
volatile,
504-
alignstack,
500+
volatile.to_llvm_bool(),
501+
alignstack.to_llvm_bool(),
505502
dia,
506-
can_throw,
503+
unwind.to_llvm_bool(),
507504
)
508505
};
509506

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ fn embed_bitcode(
11101110

11111111
llvm::set_section(llglobal, bitcode_section_name(cgcx));
11121112
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1113-
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
1113+
llvm::LLVMSetGlobalConstant(llglobal, llvm::TRUE);
11141114

11151115
let llconst = common::bytes_in_context(llcx, &[]);
11161116
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::attributes;
3535
use crate::common::Funclet;
3636
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
3737
use crate::llvm::{
38-
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True,
38+
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, GEPNoWrapFlags, Metadata, TRUE, ToLlvmBool,
3939
};
4040
use crate::type_::Type;
4141
use crate::type_of::LayoutLlvmExt;
@@ -493,8 +493,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
493493
unsafe {
494494
let add = llvm::LLVMBuildAdd(self.llbuilder, a, b, UNNAMED);
495495
if llvm::LLVMIsAInstruction(add).is_some() {
496-
llvm::LLVMSetNUW(add, True);
497-
llvm::LLVMSetNSW(add, True);
496+
llvm::LLVMSetNUW(add, TRUE);
497+
llvm::LLVMSetNSW(add, TRUE);
498498
}
499499
add
500500
}
@@ -503,8 +503,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
503503
unsafe {
504504
let sub = llvm::LLVMBuildSub(self.llbuilder, a, b, UNNAMED);
505505
if llvm::LLVMIsAInstruction(sub).is_some() {
506-
llvm::LLVMSetNUW(sub, True);
507-
llvm::LLVMSetNSW(sub, True);
506+
llvm::LLVMSetNUW(sub, TRUE);
507+
llvm::LLVMSetNSW(sub, TRUE);
508508
}
509509
sub
510510
}
@@ -513,8 +513,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
513513
unsafe {
514514
let mul = llvm::LLVMBuildMul(self.llbuilder, a, b, UNNAMED);
515515
if llvm::LLVMIsAInstruction(mul).is_some() {
516-
llvm::LLVMSetNUW(mul, True);
517-
llvm::LLVMSetNSW(mul, True);
516+
llvm::LLVMSetNUW(mul, TRUE);
517+
llvm::LLVMSetNSW(mul, TRUE);
518518
}
519519
mul
520520
}
@@ -528,7 +528,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
528528
// an instruction, so we need to check before setting the flag.
529529
// (See also `LLVMBuildNUWNeg` which also needs a check.)
530530
if llvm::LLVMIsAInstruction(or).is_some() {
531-
llvm::LLVMSetIsDisjoint(or, True);
531+
llvm::LLVMSetIsDisjoint(or, TRUE);
532532
}
533533
or
534534
}
@@ -629,7 +629,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
629629
fn volatile_load(&mut self, ty: &'ll Type, ptr: &'ll Value) -> &'ll Value {
630630
unsafe {
631631
let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
632-
llvm::LLVMSetVolatile(load, llvm::True);
632+
llvm::LLVMSetVolatile(load, llvm::TRUE);
633633
load
634634
}
635635
}
@@ -717,7 +717,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
717717
let mut const_llval = None;
718718
let llty = place.layout.llvm_type(self);
719719
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
720-
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
720+
if llvm::LLVMIsGlobalConstant(global).is_true() {
721721
if let Some(init) = llvm::LLVMGetInitializer(global) {
722722
if self.val_ty(init) == llty {
723723
const_llval = Some(init);
@@ -838,7 +838,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
838838
if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
839839
llvm::LLVMSetAlignment(store, align);
840840
if flags.contains(MemFlags::VOLATILE) {
841-
llvm::LLVMSetVolatile(store, llvm::True);
841+
llvm::LLVMSetVolatile(store, llvm::TRUE);
842842
}
843843
if flags.contains(MemFlags::NONTEMPORAL) {
844844
// Make sure that the current target architectures supports "sane" non-temporal
@@ -956,7 +956,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
956956
let trunc = self.trunc(val, dest_ty);
957957
unsafe {
958958
if llvm::LLVMIsAInstruction(trunc).is_some() {
959-
llvm::LLVMSetNUW(trunc, True);
959+
llvm::LLVMSetNUW(trunc, TRUE);
960960
}
961961
}
962962
trunc
@@ -968,7 +968,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
968968
let trunc = self.trunc(val, dest_ty);
969969
unsafe {
970970
if llvm::LLVMIsAInstruction(trunc).is_some() {
971-
llvm::LLVMSetNSW(trunc, True);
971+
llvm::LLVMSetNSW(trunc, TRUE);
972972
}
973973
}
974974
trunc
@@ -1067,13 +1067,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10671067

10681068
fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
10691069
unsafe {
1070-
llvm::LLVMBuildIntCast2(
1071-
self.llbuilder,
1072-
val,
1073-
dest_ty,
1074-
if is_signed { True } else { False },
1075-
UNNAMED,
1076-
)
1070+
llvm::LLVMBuildIntCast2(self.llbuilder, val, dest_ty, is_signed.to_llvm_bool(), UNNAMED)
10771071
}
10781072
}
10791073

@@ -1229,7 +1223,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12291223
let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
12301224
let landing_pad = self.landing_pad(ty, pers_fn, 0);
12311225
unsafe {
1232-
llvm::LLVMSetCleanup(landing_pad, llvm::True);
1226+
llvm::LLVMSetCleanup(landing_pad, llvm::TRUE);
12331227
}
12341228
(self.extract_value(landing_pad, 0), self.extract_value(landing_pad, 1))
12351229
}
@@ -1317,7 +1311,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
13171311
failure_order: rustc_middle::ty::AtomicOrdering,
13181312
weak: bool,
13191313
) -> (&'ll Value, &'ll Value) {
1320-
let weak = if weak { llvm::True } else { llvm::False };
13211314
unsafe {
13221315
let value = llvm::LLVMBuildAtomicCmpXchg(
13231316
self.llbuilder,
@@ -1326,9 +1319,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
13261319
src,
13271320
AtomicOrdering::from_generic(order),
13281321
AtomicOrdering::from_generic(failure_order),
1329-
llvm::False, // SingleThreaded
1322+
llvm::FALSE, // SingleThreaded
13301323
);
1331-
llvm::LLVMSetWeak(value, weak);
1324+
llvm::LLVMSetWeak(value, weak.to_llvm_bool());
13321325
let val = self.extract_value(value, 0);
13331326
let success = self.extract_value(value, 1);
13341327
(val, success)
@@ -1353,7 +1346,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
13531346
dst,
13541347
src,
13551348
AtomicOrdering::from_generic(order),
1356-
llvm::False, // SingleThreaded
1349+
llvm::FALSE, // SingleThreaded
13571350
)
13581351
};
13591352
if ret_ptr && self.val_ty(res) != self.type_ptr() {
@@ -1368,14 +1361,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
13681361
scope: SynchronizationScope,
13691362
) {
13701363
let single_threaded = match scope {
1371-
SynchronizationScope::SingleThread => llvm::True,
1372-
SynchronizationScope::CrossThread => llvm::False,
1364+
SynchronizationScope::SingleThread => true,
1365+
SynchronizationScope::CrossThread => false,
13731366
};
13741367
unsafe {
13751368
llvm::LLVMBuildFence(
13761369
self.llbuilder,
13771370
AtomicOrdering::from_generic(order),
1378-
single_threaded,
1371+
single_threaded.to_llvm_bool(),
13791372
UNNAMED,
13801373
);
13811374
}

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::builder::{Builder, PlaceRef, UNNAMED};
1111
use crate::context::SimpleCx;
1212
use crate::declare::declare_simple_fn;
1313
use crate::llvm;
14-
use crate::llvm::{Metadata, True, Type};
14+
use crate::llvm::{Metadata, TRUE, Type};
1515
use crate::value::Value;
1616

1717
pub(crate) fn adjust_activity_to_abi<'tcx>(
@@ -293,7 +293,7 @@ pub(crate) fn generate_enzyme_call<'ll, 'tcx>(
293293
// ret double %0
294294
// }
295295
// ```
296-
let enzyme_ty = unsafe { llvm::LLVMFunctionType(ret_ty, ptr::null(), 0, True) };
296+
let enzyme_ty = unsafe { llvm::LLVMFunctionType(ret_ty, ptr::null(), 0, TRUE) };
297297

298298
// FIXME(ZuseZ4): the CC/Addr/Vis values are best effort guesses, we should look at tests and
299299
// think a bit more about what should go here.

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::debug;
2020
use crate::consts::const_alloc_to_llvm;
2121
pub(crate) use crate::context::CodegenCx;
2222
use crate::context::{GenericCx, SCx};
23-
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True};
23+
use crate::llvm::{self, BasicBlock, ConstantInt, FALSE, Metadata, TRUE, ToLlvmBool};
2424
use crate::type_::Type;
2525
use crate::value::Value;
2626

@@ -158,7 +158,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
158158
self.type_kind(t) == TypeKind::Integer,
159159
"only allows integer types in const_int"
160160
);
161-
unsafe { llvm::LLVMConstInt(t, i as u64, True) }
161+
unsafe { llvm::LLVMConstInt(t, i as u64, TRUE) }
162162
}
163163

164164
fn const_u8(&self, i: u8) -> &'ll Value {
@@ -192,7 +192,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
192192
self.type_kind(t) == TypeKind::Integer,
193193
"only allows integer types in const_uint"
194194
);
195-
unsafe { llvm::LLVMConstInt(t, i, False) }
195+
unsafe { llvm::LLVMConstInt(t, i, FALSE) }
196196
}
197197

198198
fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
@@ -377,7 +377,7 @@ pub(crate) fn val_ty(v: &Value) -> &Type {
377377
pub(crate) fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
378378
unsafe {
379379
let ptr = bytes.as_ptr() as *const c_char;
380-
llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), True)
380+
llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), TRUE)
381381
}
382382
}
383383

@@ -392,7 +392,7 @@ fn struct_in_context<'ll>(
392392
packed: bool,
393393
) -> &'ll Value {
394394
let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
395-
unsafe { llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), len, packed as Bool) }
395+
unsafe { llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), len, packed.to_llvm_bool()) }
396396
}
397397

398398
#[inline]

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
701701
}
702702

703703
pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
704-
unsafe { llvm::LLVMConstInt(ty, val, llvm::False) }
704+
unsafe { llvm::LLVMConstInt(ty, val, llvm::FALSE) }
705705
}
706706

707707
pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
7272
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
7373
llvm::set_section(section_var, c".debug_gdb_scripts");
7474
llvm::set_initializer(section_var, cx.const_bytes(section_contents));
75-
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
75+
llvm::LLVMSetGlobalConstant(section_var, llvm::TRUE);
7676
llvm::set_unnamed_address(section_var, llvm::UnnamedAddr::Global);
7777
llvm::set_linkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
7878
// This should make sure that the whole section is not larger than

compiler/rustc_codegen_llvm/src/debuginfo/namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) fn item_namespace<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'l
3838
parent_scope,
3939
namespace_name_string.as_ptr(),
4040
namespace_name_string.len(),
41-
llvm::False, // ExportSymbols (only relevant for C++ anonymous namespaces)
41+
llvm::FALSE, // ExportSymbols (only relevant for C++ anonymous namespaces)
4242
)
4343
};
4444

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
//! the need for an extra cast from `*const u8` on the Rust side.
1212
1313
#![allow(non_camel_case_types)]
14-
#![allow(non_upper_case_globals)]
1514

16-
use std::fmt::Debug;
15+
use std::fmt::{self, Debug};
1716
use std::marker::PhantomData;
1817
use std::num::NonZero;
1918
use std::ptr;
@@ -33,10 +32,59 @@ use crate::llvm;
3332

3433
/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
3534
/// which has a different ABI from Rust or C++ `bool`.
36-
pub(crate) type Bool = c_int;
35+
///
36+
/// This wrapper does not implement `PartialEq`.
37+
/// To test the underlying boolean value, use [`Self::is_true`].
38+
#[derive(Clone, Copy)]
39+
#[repr(transparent)]
40+
pub(crate) struct Bool {
41+
value: c_int,
42+
}
43+
44+
pub(crate) const TRUE: Bool = Bool::TRUE;
45+
pub(crate) const FALSE: Bool = Bool::FALSE;
46+
47+
impl Bool {
48+
pub(crate) const TRUE: Self = Self { value: 1 };
49+
pub(crate) const FALSE: Self = Self { value: 0 };
50+
51+
pub(crate) const fn from_bool(rust_bool: bool) -> Self {
52+
if rust_bool { Self::TRUE } else { Self::FALSE }
53+
}
54+
55+
/// Converts this LLVM-C boolean to a Rust `bool`
56+
pub(crate) fn is_true(self) -> bool {
57+
// Since we're interacting with a C API, follow the C convention of
58+
// treating any nonzero value as true.
59+
self.value != Self::FALSE.value
60+
}
61+
}
3762

38-
pub(crate) const True: Bool = 1 as Bool;
39-
pub(crate) const False: Bool = 0 as Bool;
63+
impl Debug for Bool {
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
match self.value {
66+
0 => f.write_str("FALSE"),
67+
1 => f.write_str("TRUE"),
68+
// As with `Self::is_true`, treat any nonzero value as true.
69+
v => write!(f, "TRUE ({v})"),
70+
}
71+
}
72+
}
73+
74+
/// Convenience trait to convert `bool` to `llvm::Bool` with an explicit method call.
75+
///
76+
/// Being able to write `b.to_llvm_bool()` is less noisy than `llvm::Bool::from(b)`,
77+
/// while being more explicit and less mistake-prone than something like `b.into()`.
78+
pub(crate) trait ToLlvmBool: Copy {
79+
fn to_llvm_bool(self) -> llvm::Bool;
80+
}
81+
82+
impl ToLlvmBool for bool {
83+
#[inline(always)]
84+
fn to_llvm_bool(self) -> llvm::Bool {
85+
llvm::Bool::from_bool(self)
86+
}
87+
}
4088

4189
/// Wrapper for a raw enum value returned from LLVM's C APIs.
4290
///

0 commit comments

Comments
 (0)