Skip to content

Commit 3f8b20b

Browse files
committed
compiler: use vector instead of hashmap in rustc_abi's TargetDataLayout
1 parent 73aaf57 commit 3f8b20b

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use std::ops::{Add, AddAssign, Deref, Mul, RangeInclusive, Sub};
4747
use std::str::FromStr;
4848

4949
use bitflags::bitflags;
50-
use rustc_data_structures::fx::FxHashMap;
5150
#[cfg(feature = "nightly")]
5251
use rustc_data_structures::stable_hasher::StableOrd;
5352
use rustc_hashes::Hash64;
@@ -292,14 +291,14 @@ impl Default for TargetDataLayout {
292291
(Size::from_bits(128), AbiAlign::new(align(128))),
293292
],
294293
default_address_space: AddressSpace::ZERO,
295-
address_space_info: FxHashMap::from_iter([(
294+
address_space_info: vec![(
296295
AddressSpace::ZERO,
297296
AddressSpaceInfo {
298297
pointer_size: Size::from_bits(64),
299298
pointer_align: AbiAlign::new(align(64)),
300299
pointer_index: Size::from_bits(64),
301300
},
302-
)]),
301+
)],
303302
instruction_address_space: AddressSpace::ZERO,
304303
c_enum_min_size: Integer::I32,
305304
}
@@ -396,11 +395,16 @@ impl TargetDataLayout {
396395
pointer_size,
397396
pointer_align: parse_align(a, p)?,
398397
};
399-
400-
dl.address_space_info
401-
.entry(addr_space)
402-
.and_modify(|v| *v = info)
403-
.or_insert(info);
398+
match dl.address_space_info.iter_mut().find(|(a, _)| *a == addr_space) {
399+
Some(e) => e.1 = info,
400+
None => {
401+
if addr_space == default_address_space {
402+
dl.address_space_info.insert(0, (addr_space, info));
403+
} else {
404+
dl.address_space_info.push((addr_space, info));
405+
}
406+
}
407+
}
404408
}
405409
[p, s, _pr, i, a @ ..] if p.starts_with("p") => {
406410
// Some targets, such as CHERI, use the 'f' suffix in the p- spec to signal that
@@ -419,10 +423,16 @@ impl TargetDataLayout {
419423
pointer_index: parse_size(i, p)?,
420424
};
421425

422-
dl.address_space_info
423-
.entry(addr_space)
424-
.and_modify(|v| *v = info)
425-
.or_insert(info);
426+
match dl.address_space_info.iter_mut().find(|(a, _)| *a == addr_space) {
427+
Some(e) => e.1 = info,
428+
None => {
429+
if addr_space == default_address_space {
430+
dl.address_space_info.insert(0, (addr_space, info));
431+
} else {
432+
dl.address_space_info.push((addr_space, info));
433+
}
434+
}
435+
}
426436
}
427437

428438
[s, a @ ..] if s.starts_with('i') => {
@@ -460,7 +470,7 @@ impl TargetDataLayout {
460470
}
461471
}
462472

463-
if !dl.address_space_info.contains_key(&default_address_space) {
473+
if dl.address_space_info.iter().find(|(a, _)| *a == default_address_space).is_none() {
464474
return Err(TargetDataLayoutErrors::MissingAddressSpaceInfo {
465475
addr_space: default_address_space,
466476
});
@@ -469,11 +479,17 @@ impl TargetDataLayout {
469479
// Inherit, if not given, address space informations for specific LLVM elements from the
470480
// default data address space.
471481

472-
if !dl.address_space_info.contains_key(&dl.instruction_address_space) {
473-
dl.address_space_info.insert(
482+
if dl.address_space_info.iter().find(|(a, _)| *a == dl.instruction_address_space).is_none()
483+
{
484+
dl.address_space_info.push((
474485
dl.instruction_address_space,
475-
dl.address_space_info.get(&default_address_space).unwrap().clone(),
476-
);
486+
dl.address_space_info
487+
.iter()
488+
.find(|(a, _)| *a == default_address_space)
489+
.unwrap()
490+
.1
491+
.clone(),
492+
));
477493
}
478494

479495
Ok(dl)
@@ -491,7 +507,12 @@ impl TargetDataLayout {
491507
/// so we adopt such a more-constrained size bound due to its technical limitations.
492508
#[inline]
493509
pub fn obj_size_bound(&self) -> u64 {
494-
self.obj_size_bound_in(self.default_address_space)
510+
match self.pointer_size().bits() {
511+
16 => 1 << 15,
512+
32 => 1 << 31,
513+
64 => 1 << 61,
514+
bits => panic!("obj_size_bound: unknown pointer bit size {bits}"),
515+
}
495516
}
496517

497518
/// Returns **exclusive** upper bound on object size in bytes.
@@ -515,7 +536,13 @@ impl TargetDataLayout {
515536

516537
#[inline]
517538
pub fn ptr_sized_integer(&self) -> Integer {
518-
self.ptr_sized_integer_in(self.default_address_space)
539+
use Integer::*;
540+
match self.pointer_index().bits() {
541+
16 => I16,
542+
32 => I32,
543+
64 => I64,
544+
bits => panic!("ptr_sized_integer: unknown pointer bit size {bits}"),
545+
}
519546
}
520547

521548
#[inline]
@@ -549,14 +576,14 @@ impl TargetDataLayout {
549576
/// Get the pointer size in the default data address space.
550577
#[inline]
551578
pub fn pointer_size(&self) -> Size {
552-
self.pointer_size_in(self.default_address_space)
579+
self.address_space_info[0].1.pointer_size
553580
}
554581

555582
/// Get the pointer size in a specific address space.
556583
#[inline]
557584
pub fn pointer_size_in(&self, c: AddressSpace) -> Size {
558-
if let Some(c) = self.address_space_info.get(&c) {
559-
c.pointer_size
585+
if let Some(e) = self.address_space_info.iter().find(|(a, _)| a == &c) {
586+
e.1.pointer_size
560587
} else {
561588
panic!("Use of unknown address space {c:?}");
562589
}
@@ -565,14 +592,14 @@ impl TargetDataLayout {
565592
/// Get the pointer index in the default data address space.
566593
#[inline]
567594
pub fn pointer_index(&self) -> Size {
568-
self.pointer_index_in(self.default_address_space)
595+
self.address_space_info[0].1.pointer_index
569596
}
570597

571598
/// Get the pointer index in a specific address space.
572599
#[inline]
573600
pub fn pointer_index_in(&self, c: AddressSpace) -> Size {
574-
if let Some(c) = self.address_space_info.get(&c) {
575-
c.pointer_index
601+
if let Some(e) = self.address_space_info.iter().find(|(a, _)| a == &c) {
602+
e.1.pointer_index
576603
} else {
577604
panic!("Use of unknown address space {c:?}");
578605
}
@@ -581,14 +608,14 @@ impl TargetDataLayout {
581608
/// Get the pointer alignment in the default data address space.
582609
#[inline]
583610
pub fn pointer_align(&self) -> AbiAlign {
584-
self.pointer_align_in(self.default_address_space)
611+
self.address_space_info[0].1.pointer_align
585612
}
586613

587614
/// Get the pointer alignment in a specific address space.
588615
#[inline]
589616
pub fn pointer_align_in(&self, c: AddressSpace) -> AbiAlign {
590-
if let Some(c) = self.address_space_info.get(&c) {
591-
c.pointer_align
617+
if let Some(e) = self.address_space_info.iter().find(|(a, _)| a == &c) {
618+
e.1.pointer_align
592619
} else {
593620
panic!("Use of unknown address space {c:?}");
594621
}

0 commit comments

Comments
 (0)