Skip to content

Commit 6cb1835

Browse files
committed
Fix placement new codegen test
1 parent 874cac1 commit 6cb1835

File tree

2 files changed

+201
-5
lines changed

2 files changed

+201
-5
lines changed

library/alloc/src/raw_rc.rs

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,13 +1242,205 @@ where
12421242
}
12431243
}
12441244

1245+
trait SpecRcDefault {
1246+
fn spec_default() -> Self;
1247+
}
1248+
1249+
impl<T, A> SpecRcDefault for RawRc<T, A>
1250+
where
1251+
T: Default,
1252+
A: Allocator + Default,
1253+
{
1254+
default fn spec_default() -> Self {
1255+
Self::new(T::default())
1256+
}
1257+
}
1258+
1259+
impl<T> SpecRcDefault for RawRc<T, Global>
1260+
where
1261+
T: Default,
1262+
{
1263+
default fn spec_default() -> Self {
1264+
unsafe fn default_impl<T, const N: usize>() -> RawRc<T, Global>
1265+
where
1266+
T: Default,
1267+
{
1268+
#[repr(C)]
1269+
struct RcAllocation<T, const N: usize> {
1270+
_padding: MaybeUninit<[u8; N]>,
1271+
_ref_counts: RefCounts,
1272+
_value: T,
1273+
}
1274+
1275+
impl<T, const N: usize> Default for RcAllocation<T, N>
1276+
where
1277+
T: Default,
1278+
{
1279+
fn default() -> Self {
1280+
Self {
1281+
_padding: MaybeUninit::uninit(),
1282+
_ref_counts: RefCounts::new(1),
1283+
_value: T::default(),
1284+
}
1285+
}
1286+
}
1287+
1288+
let (allocation_ptr, alloc) =
1289+
Box::into_non_null_with_allocator(Box::<RcAllocation<T, N>>::default());
1290+
1291+
unsafe {
1292+
RawRc::from_raw_parts(
1293+
allocation_ptr.byte_add(T::RC_LAYOUT.allocation_offset_bytes).cast(),
1294+
alloc,
1295+
)
1296+
}
1297+
}
1298+
1299+
macro_rules! select_impl {
1300+
($($padding:literal,)*) => {
1301+
{
1302+
let padding = RefCounts::LAYOUT.padding_needed_for(align_of::<T>());
1303+
1304+
match padding {
1305+
$($padding => default_impl::<T, $padding>,)*
1306+
_ => panic!("invalid padding"),
1307+
}
1308+
}
1309+
};
1310+
}
1311+
1312+
#[cfg(target_pointer_width = "16")]
1313+
let selected_impl: unsafe fn() -> Self = const {
1314+
select_impl![
1315+
0x0000, // ((1 << 2) - 4)
1316+
0x0004, // ((1 << 3) - 4)
1317+
0x000c, // ((1 << 4) - 4)
1318+
0x001c, // ((1 << 5) - 4)
1319+
0x003c, // ((1 << 6) - 4)
1320+
0x007c, // ((1 << 7) - 4)
1321+
0x00fc, // ((1 << 8) - 4)
1322+
0x01fc, // ((1 << 9) - 4)
1323+
0x03fc, // ((1 << 10) - 4)
1324+
0x07fc, // ((1 << 11) - 4)
1325+
0x0ffc, // ((1 << 12) - 4)
1326+
0x1ffc, // ((1 << 13) - 4)
1327+
0x3ffc, // ((1 << 14) - 4)
1328+
0x7ffc, // ((1 << 15) - 4)
1329+
]
1330+
};
1331+
1332+
#[cfg(target_pointer_width = "32")]
1333+
let selected_impl: unsafe fn() -> Self = const {
1334+
select_impl![
1335+
0x00000000, // ((1 << 3) - 8)
1336+
0x00000008, // ((1 << 4) - 8)
1337+
0x00000018, // ((1 << 5) - 8)
1338+
0x00000038, // ((1 << 6) - 8)
1339+
0x00000078, // ((1 << 7) - 8)
1340+
0x000000f8, // ((1 << 8) - 8)
1341+
0x000001f8, // ((1 << 9) - 8)
1342+
0x000003f8, // ((1 << 10) - 8)
1343+
0x000007f8, // ((1 << 11) - 8)
1344+
0x00000ff8, // ((1 << 12) - 8)
1345+
0x00001ff8, // ((1 << 13) - 8)
1346+
0x00003ff8, // ((1 << 14) - 8)
1347+
0x00007ff8, // ((1 << 15) - 8)
1348+
0x0000fff8, // ((1 << 16) - 8)
1349+
0x0001fff8, // ((1 << 17) - 8)
1350+
0x0003fff8, // ((1 << 18) - 8)
1351+
0x0007fff8, // ((1 << 19) - 8)
1352+
0x000ffff8, // ((1 << 20) - 8)
1353+
0x001ffff8, // ((1 << 21) - 8)
1354+
0x003ffff8, // ((1 << 22) - 8)
1355+
0x007ffff8, // ((1 << 23) - 8)
1356+
0x00fffff8, // ((1 << 24) - 8)
1357+
0x01fffff8, // ((1 << 25) - 8)
1358+
0x03fffff8, // ((1 << 26) - 8)
1359+
0x07fffff8, // ((1 << 27) - 8)
1360+
0x0ffffff8, // ((1 << 28) - 8)
1361+
0x1ffffff8, // ((1 << 29) - 8)
1362+
0x3ffffff8, // ((1 << 30) - 8)
1363+
0x7ffffff8, // ((1 << 31) - 8)
1364+
]
1365+
};
1366+
1367+
#[cfg(target_pointer_width = "64")]
1368+
let selected_impl: unsafe fn() -> Self = const {
1369+
select_impl![
1370+
0x0000000000000000, // ((1 << 4) - 16)
1371+
0x0000000000000010, // ((1 << 5) - 16)
1372+
0x0000000000000030, // ((1 << 6) - 16)
1373+
0x0000000000000070, // ((1 << 7) - 16)
1374+
0x00000000000000f0, // ((1 << 8) - 16)
1375+
0x00000000000001f0, // ((1 << 9) - 16)
1376+
0x00000000000003f0, // ((1 << 10) - 16)
1377+
0x00000000000007f0, // ((1 << 11) - 16)
1378+
0x0000000000000ff0, // ((1 << 12) - 16)
1379+
0x0000000000001ff0, // ((1 << 13) - 16)
1380+
0x0000000000003ff0, // ((1 << 14) - 16)
1381+
0x0000000000007ff0, // ((1 << 15) - 16)
1382+
0x000000000000fff0, // ((1 << 16) - 16)
1383+
0x000000000001fff0, // ((1 << 17) - 16)
1384+
0x000000000003fff0, // ((1 << 18) - 16)
1385+
0x000000000007fff0, // ((1 << 19) - 16)
1386+
0x00000000000ffff0, // ((1 << 20) - 16)
1387+
0x00000000001ffff0, // ((1 << 21) - 16)
1388+
0x00000000003ffff0, // ((1 << 22) - 16)
1389+
0x00000000007ffff0, // ((1 << 23) - 16)
1390+
0x0000000000fffff0, // ((1 << 24) - 16)
1391+
0x0000000001fffff0, // ((1 << 25) - 16)
1392+
0x0000000003fffff0, // ((1 << 26) - 16)
1393+
0x0000000007fffff0, // ((1 << 27) - 16)
1394+
0x000000000ffffff0, // ((1 << 28) - 16)
1395+
0x000000001ffffff0, // ((1 << 29) - 16)
1396+
0x000000003ffffff0, // ((1 << 30) - 16)
1397+
0x000000007ffffff0, // ((1 << 31) - 16)
1398+
0x00000000fffffff0, // ((1 << 32) - 16)
1399+
0x00000001fffffff0, // ((1 << 33) - 16)
1400+
0x00000003fffffff0, // ((1 << 34) - 16)
1401+
0x00000007fffffff0, // ((1 << 35) - 16)
1402+
0x0000000ffffffff0, // ((1 << 36) - 16)
1403+
0x0000001ffffffff0, // ((1 << 37) - 16)
1404+
0x0000003ffffffff0, // ((1 << 38) - 16)
1405+
0x0000007ffffffff0, // ((1 << 39) - 16)
1406+
0x000000fffffffff0, // ((1 << 40) - 16)
1407+
0x000001fffffffff0, // ((1 << 41) - 16)
1408+
0x000003fffffffff0, // ((1 << 42) - 16)
1409+
0x000007fffffffff0, // ((1 << 43) - 16)
1410+
0x00000ffffffffff0, // ((1 << 44) - 16)
1411+
0x00001ffffffffff0, // ((1 << 45) - 16)
1412+
0x00003ffffffffff0, // ((1 << 46) - 16)
1413+
0x00007ffffffffff0, // ((1 << 47) - 16)
1414+
0x0000fffffffffff0, // ((1 << 48) - 16)
1415+
0x0001fffffffffff0, // ((1 << 49) - 16)
1416+
0x0003fffffffffff0, // ((1 << 50) - 16)
1417+
0x0007fffffffffff0, // ((1 << 51) - 16)
1418+
0x000ffffffffffff0, // ((1 << 52) - 16)
1419+
0x001ffffffffffff0, // ((1 << 53) - 16)
1420+
0x003ffffffffffff0, // ((1 << 54) - 16)
1421+
0x007ffffffffffff0, // ((1 << 55) - 16)
1422+
0x00fffffffffffff0, // ((1 << 56) - 16)
1423+
0x01fffffffffffff0, // ((1 << 57) - 16)
1424+
0x03fffffffffffff0, // ((1 << 58) - 16)
1425+
0x07fffffffffffff0, // ((1 << 59) - 16)
1426+
0x0ffffffffffffff0, // ((1 << 60) - 16)
1427+
0x1ffffffffffffff0, // ((1 << 61) - 16)
1428+
0x3ffffffffffffff0, // ((1 << 62) - 16)
1429+
0x7ffffffffffffff0, // ((1 << 63) - 16)
1430+
]
1431+
};
1432+
1433+
unsafe { selected_impl() }
1434+
}
1435+
}
1436+
12451437
impl<T, A> Default for RawRc<T, A>
12461438
where
12471439
T: Default,
12481440
A: Allocator + Default,
12491441
{
12501442
fn default() -> Self {
1251-
Self::new(T::default())
1443+
Self::spec_default()
12521444
}
12531445
}
12541446

tests/codegen/placement-new.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ pub fn box_default_inplace() -> Box<(String, String)> {
2222
#[no_mangle]
2323
pub fn rc_default_inplace() -> Rc<(String, String)> {
2424
// CHECK-NOT: alloca
25-
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
25+
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(i[[#BITS:]]
2626
// CHECK-NOT: call void @llvm.memcpy
27-
// CHECK: ret ptr [[RC]]
27+
// CHECK: [[DATA:%.*]] = getelementptr inbounds i8, ptr [[RC]], i[[#BITS]] [[#div(BITS,4)]]
28+
// CHECK-NOT: call void @llvm.memcpy
29+
// CHECK: ret ptr [[DATA]]
2830
Rc::default()
2931
}
3032

3133
// CHECK-LABEL: @arc_default_inplace
3234
#[no_mangle]
3335
pub fn arc_default_inplace() -> Arc<(String, String)> {
3436
// CHECK-NOT: alloca
35-
// CHECK: [[ARC:%.*]] = {{.*}}call {{.*}}__rust_alloc(
37+
// CHECK: [[RC:%.*]] = {{.*}}call {{.*}}__rust_alloc(i[[#BITS:]]
38+
// CHECK-NOT: call void @llvm.memcpy
39+
// CHECK: [[DATA:%.*]] = getelementptr inbounds i8, ptr [[RC]], i[[#BITS]] [[#div(BITS,4)]]
3640
// CHECK-NOT: call void @llvm.memcpy
37-
// CHECK: ret ptr [[ARC]]
41+
// CHECK: ret ptr [[DATA]]
3842
Arc::default()
3943
}

0 commit comments

Comments
 (0)