Skip to content

Commit 109ef21

Browse files
authored
Merge pull request #20243 from lnicola/sync-from-rust
minor: Sync from downstream
2 parents 10b5477 + 5cb9f97 commit 109ef21

18 files changed

Lines changed: 192 additions & 201 deletions

Cargo.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ dependencies = [
143143
"libc",
144144
]
145145

146+
[[package]]
147+
name = "object"
148+
version = "0.37.1"
149+
source = "registry+https://github.com/rust-lang/crates.io-index"
150+
checksum = "03fd943161069e1768b4b3d050890ba48730e590f57e56d4aa04e7e090e61b4a"
151+
dependencies = [
152+
"memchr",
153+
]
154+
146155
[[package]]
147156
name = "once_cell"
148157
version = "1.20.2"
@@ -179,6 +188,7 @@ dependencies = [
179188
"boml",
180189
"gccjit",
181190
"lang_tester",
191+
"object",
182192
"tempfile",
183193
]
184194

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ master = ["gccjit/master"]
2222
default = ["master"]
2323

2424
[dependencies]
25+
object = { version = "0.37.0", default-features = false, features = ["std", "read"] }
26+
tempfile = "3.20"
2527
gccjit = "2.7"
2628
#gccjit = { git = "https://github.com/rust-lang/gccjit.rs" }
2729

@@ -31,7 +33,6 @@ gccjit = "2.7"
3133
[dev-dependencies]
3234
boml = "0.3.1"
3335
lang_tester = "0.8.0"
34-
tempfile = "3.20"
3536

3637
[profile.dev]
3738
# By compiling dependencies with optimizations, performing tests gets much faster.

build_system/build_sysroot/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ resolver = "2"
66

77
[dependencies]
88
core = { path = "./sysroot_src/library/core" }
9+
compiler_builtins = { path = "./sysroot_src/library/compiler-builtins/compiler-builtins" }
910
alloc = { path = "./sysroot_src/library/alloc" }
1011
std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
1112
test = { path = "./sysroot_src/library/test" }

build_system/src/test.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::build;
99
use crate::config::{Channel, ConfigInfo};
1010
use crate::utils::{
1111
create_dir, get_sysroot_dir, get_toolchain, git_clone, git_clone_root_dir, remove_file,
12-
run_command, run_command_with_env, run_command_with_output, run_command_with_output_and_env,
13-
rustc_version_info, split_args, walk_dir,
12+
run_command, run_command_with_env, run_command_with_output_and_env, rustc_version_info,
13+
split_args, walk_dir,
1414
};
1515

1616
type Env = HashMap<String, String>;
@@ -485,30 +485,6 @@ fn setup_rustc(env: &mut Env, args: &TestArg) -> Result<PathBuf, String> {
485485
run_command_with_output_and_env(&[&"git", &"checkout"], rust_dir, Some(env))?;
486486
}
487487

488-
let mut patches = Vec::new();
489-
walk_dir(
490-
"patches/tests",
491-
&mut |_| Ok(()),
492-
&mut |file_path: &Path| {
493-
patches.push(file_path.to_path_buf());
494-
Ok(())
495-
},
496-
false,
497-
)?;
498-
patches.sort();
499-
// TODO: remove duplication with prepare.rs by creating a apply_patch function in the utils
500-
// module.
501-
for file_path in patches {
502-
println!("[GIT] apply `{}`", file_path.display());
503-
let path = Path::new("../..").join(file_path);
504-
run_command_with_output(&[&"git", &"apply", &path], rust_dir)?;
505-
run_command_with_output(&[&"git", &"add", &"-A"], rust_dir)?;
506-
run_command_with_output(
507-
&[&"git", &"commit", &"--no-gpg-sign", &"-m", &format!("Patch {}", path.display())],
508-
rust_dir,
509-
)?;
510-
}
511-
512488
let cargo = String::from_utf8(
513489
run_command_with_env(&[&"rustup", &"which", &"cargo"], rust_dir, Some(env))?.stdout,
514490
)

patches/tests/0001-Workaround-to-make-a-run-make-test-pass.patch

Lines changed: 0 additions & 25 deletions
This file was deleted.

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2025-06-02"
2+
channel = "nightly-2025-06-28"
33
components = ["rust-src", "rustc-dev", "llvm-tools-preview"]

src/abi.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#[cfg(feature = "master")]
22
use gccjit::FnAttribute;
33
use gccjit::{ToLValue, ToRValue, Type};
4-
use rustc_abi::{ArmCall, CanonAbi, InterruptKind, Reg, RegKind, X86Call};
4+
#[cfg(feature = "master")]
5+
use rustc_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
6+
use rustc_abi::{Reg, RegKind};
57
use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeCodegenMethods};
68
use rustc_data_structures::fx::FxHashSet;
79
use rustc_middle::bug;

src/allocator.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
21
#[cfg(feature = "master")]
3-
use gccjit::{FnAttribute, VarAttribute};
2+
use gccjit::FnAttribute;
3+
use gccjit::{Context, FunctionType, RValue, ToRValue, Type};
44
use rustc_ast::expand::allocator::{
55
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
66
alloc_error_handler_name, default_fn_name, global_fn_name,
@@ -71,15 +71,13 @@ pub(crate) unsafe fn codegen(
7171
None,
7272
);
7373

74-
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
75-
let global = context.new_global(None, GlobalKind::Exported, i8, name);
76-
#[cfg(feature = "master")]
77-
global.add_attribute(VarAttribute::Visibility(symbol_visibility_to_gcc(
78-
tcx.sess.default_visibility(),
79-
)));
80-
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
81-
let value = context.new_rvalue_from_int(i8, value as i32);
82-
global.global_set_initializer_rvalue(value);
74+
create_const_value_function(
75+
tcx,
76+
context,
77+
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
78+
i8,
79+
context.new_rvalue_from_int(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as i32),
80+
);
8381

8482
create_wrapper_function(
8583
tcx,
@@ -91,6 +89,30 @@ pub(crate) unsafe fn codegen(
9189
);
9290
}
9391

92+
fn create_const_value_function(
93+
tcx: TyCtxt<'_>,
94+
context: &Context<'_>,
95+
name: &str,
96+
output: Type<'_>,
97+
value: RValue<'_>,
98+
) {
99+
let func = context.new_function(None, FunctionType::Exported, output, &[], name, false);
100+
101+
#[cfg(feature = "master")]
102+
func.add_attribute(FnAttribute::Visibility(symbol_visibility_to_gcc(
103+
tcx.sess.default_visibility(),
104+
)));
105+
106+
func.add_attribute(FnAttribute::AlwaysInline);
107+
108+
if tcx.sess.must_emit_unwind_tables() {
109+
// TODO(antoyo): emit unwind tables.
110+
}
111+
112+
let block = func.new_block("entry");
113+
block.end_with_return(None, value);
114+
}
115+
94116
fn create_wrapper_function(
95117
tcx: TyCtxt<'_>,
96118
context: &Context<'_>,

src/builder.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
538538
}
539539

540540
fn ret(&mut self, mut value: RValue<'gcc>) {
541-
if self.structs_as_pointer.borrow().contains(&value) {
542-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
543-
// CodegenCx.structs_as_pointer
544-
value = value.dereference(self.location).to_rvalue();
545-
}
546541
let expected_return_type = self.current_func().get_return_type();
547542
if !expected_return_type.is_compatible_with(value.get_type()) {
548543
// NOTE: due to opaque pointers now being used, we need to cast here.
@@ -700,7 +695,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
700695
let a = self.gcc_int_cast(a, a_type);
701696
let b_type = b.get_type().to_unsigned(self);
702697
let b = self.gcc_int_cast(b, b_type);
703-
a / b
698+
self.gcc_udiv(a, b)
704699
}
705700

706701
fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
@@ -712,8 +707,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
712707
// FIXME(antoyo): rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
713708
// should be the same.
714709
let typ = a.get_type().to_signed(self);
715-
let b = self.context.new_cast(self.location, b, typ);
716-
a / b
710+
let b = self.gcc_int_cast(b, typ);
711+
self.gcc_sdiv(a, b)
717712
}
718713

719714
fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
@@ -931,10 +926,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
931926
.get_address(self.location)
932927
}
933928

934-
fn dynamic_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
935-
unimplemented!();
936-
}
937-
938929
fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
939930
let block = self.llbb();
940931
let function = block.get_function();
@@ -1119,13 +1110,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11191110
// TODO(antoyo)
11201111
}
11211112

1122-
fn store(&mut self, mut val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
1123-
if self.structs_as_pointer.borrow().contains(&val) {
1124-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1125-
// CodegenCx.structs_as_pointer
1126-
val = val.dereference(self.location).to_rvalue();
1127-
}
1128-
1113+
fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
11291114
self.store_with_flags(val, ptr, align, MemFlags::empty())
11301115
}
11311116

@@ -1508,16 +1493,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15081493
element.get_address(self.location)
15091494
} else if value_type.dyncast_vector().is_some() {
15101495
panic!();
1511-
} else if let Some(pointer_type) = value_type.get_pointee() {
1512-
if let Some(struct_type) = pointer_type.is_struct() {
1513-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1514-
// CodegenCx.structs_as_pointer
1515-
aggregate_value
1516-
.dereference_field(self.location, struct_type.get_field(idx as i32))
1517-
.to_rvalue()
1518-
} else {
1519-
panic!("Unexpected type {:?}", value_type);
1520-
}
15211496
} else if let Some(struct_type) = value_type.is_struct() {
15221497
aggregate_value
15231498
.access_field(self.location, struct_type.get_field(idx as i32))
@@ -1537,21 +1512,18 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15371512
assert_eq!(idx as usize as u64, idx);
15381513
let value_type = aggregate_value.get_type();
15391514

1515+
let new_val = self.current_func().new_local(None, value_type, "aggregate_value");
1516+
self.block.add_assignment(None, new_val, aggregate_value);
1517+
15401518
let lvalue = if value_type.dyncast_array().is_some() {
15411519
let index = self
15421520
.context
15431521
.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1544-
self.context.new_array_access(self.location, aggregate_value, index)
1522+
self.context.new_array_access(self.location, new_val, index)
15451523
} else if value_type.dyncast_vector().is_some() {
15461524
panic!();
1547-
} else if let Some(pointer_type) = value_type.get_pointee() {
1548-
if let Some(struct_type) = pointer_type.is_struct() {
1549-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
1550-
// CodegenCx.structs_as_pointer
1551-
aggregate_value.dereference_field(self.location, struct_type.get_field(idx as i32))
1552-
} else {
1553-
panic!("Unexpected type {:?}", value_type);
1554-
}
1525+
} else if let Some(struct_type) = value_type.is_struct() {
1526+
new_val.access_field(None, struct_type.get_field(idx as i32))
15551527
} else {
15561528
panic!("Unexpected type {:?}", value_type);
15571529
};
@@ -1568,7 +1540,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15681540

15691541
self.llbb().add_assignment(self.location, lvalue, value);
15701542

1571-
aggregate_value
1543+
new_val.to_rvalue()
15721544
}
15731545

15741546
fn set_personality_fn(&mut self, _personality: Function<'gcc>) {

src/common.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use gccjit::{LValue, RValue, ToRValue, Type};
2-
use rustc_abi as abi;
3-
use rustc_abi::HasDataLayout;
42
use rustc_abi::Primitive::Pointer;
3+
use rustc_abi::{self as abi, HasDataLayout};
54
use rustc_codegen_ssa::traits::{
65
BaseTypeCodegenMethods, ConstCodegenMethods, MiscCodegenMethods, StaticCodegenMethods,
76
};
@@ -117,15 +116,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
117116

118117
fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
119118
let local = self.current_func.borrow().expect("func").new_local(None, typ, "undefined");
120-
if typ.is_struct().is_some() {
121-
// NOTE: hack to workaround a limitation of the rustc API: see comment on
122-
// CodegenCx.structs_as_pointer
123-
let pointer = local.get_address(None);
124-
self.structs_as_pointer.borrow_mut().insert(pointer);
125-
pointer
126-
} else {
127-
local.to_rvalue()
128-
}
119+
local.to_rvalue()
129120
}
130121

131122
fn const_poison(&self, typ: Type<'gcc>) -> RValue<'gcc> {
@@ -170,7 +161,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
170161
}
171162

172163
fn const_usize(&self, i: u64) -> RValue<'gcc> {
173-
let bit_size = self.data_layout().pointer_size.bits();
164+
let bit_size = self.data_layout().pointer_size().bits();
174165
if bit_size < 64 {
175166
// make sure it doesn't overflow
176167
assert!(i < (1 << bit_size));
@@ -248,7 +239,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
248239
}
249240
}
250241
Scalar::Ptr(ptr, _size) => {
251-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
242+
let (prov, offset) = ptr.prov_and_relative_offset();
252243
let alloc_id = prov.alloc_id();
253244
let base_addr = match self.tcx.global_alloc(alloc_id) {
254245
GlobalAlloc::Memory(alloc) => {
@@ -290,6 +281,13 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
290281
let init = self.const_data_from_alloc(alloc);
291282
self.static_addr_of(init, alloc.inner().align, None)
292283
}
284+
GlobalAlloc::TypeId { .. } => {
285+
let val = self.const_usize(offset.bytes());
286+
// This is still a variable of pointer type, even though we only use the provenance
287+
// of that pointer in CTFE and Miri. But to make LLVM's type system happy,
288+
// we need an int-to-ptr cast here (it doesn't matter at all which provenance that picks).
289+
return self.context.new_cast(None, val, ty);
290+
}
293291
GlobalAlloc::Static(def_id) => {
294292
assert!(self.tcx.is_static(def_id));
295293
self.get_static(def_id).get_address(None)

0 commit comments

Comments
 (0)