Commit Graph

3098 Commits

Author SHA1 Message Date
Matthias Krüger
5408ec3069 Rollup merge of #147576 - Mark-Simulacrum:fix-offset-zst, r=nnethercote,RalfJung
Fix ICE on offsetted ZST pointer

I'm not sure this is the *right* fix, but it's simple enough and does roughly what I'd expect. Like with the previous optimization to codegen usize rather than a zero-sized static, there's no guarantee that we continue returning a particular value from the offsetting.

A grep for `const_usize.*align` found the same code copied to rustc_codegen_gcc and cranelift but a quick skim didn't find other cases of similar 'optimization'. That said, I'm not convinced I caught everything, it's not trivial to search for this.

Closes rust-lang/rust#147516
2025-10-16 19:35:24 +02:00
Mark Rousskov
a8c79b876b Fix ICE on offsetted ZST pointer
A grep for `const_usize.*align` found the same code copied to
rustc_codegen_gcc but I don't see other cases where we get this wrong.
2025-10-15 20:06:46 -04:00
Ralf Jung
804af99457 miri: use allocator_shim_contents codegen helper 2025-10-15 21:23:14 +02:00
Matthias Krüger
041ecb124a Rollup merge of #146949 - pmur:murp/improve-ppc-inline-asm, r=Amanieu
Add vsx register support for ppc inline asm, and implement preserves_flag option

This should address the last(?) missing pieces of inline asm for ppc:

* Explicit VSX register support. ISA 2.06 (POWER7) added a 64x128b register overlay extending the fpr's to 128b, and unifies them with the vmx (altivec) registers. Implementations details within gcc/llvm percolate up, and require using the `x` template modifier. I have updated the inline asm to implicitly include this for vsx arguments which do not specify it. ~~Support for the gcc codegen backend is still a todo.~~

* Implement the `preserves_flags` option. All ABI's, and all ISAs store their flags in `cr`, and the carry bit lives inside `xer`. The other status registers hold sticky bits or control bits which do not affect branch instructions.

There is some interest in the e500 (powerpcspe) port. Architecturally, it has a very different FP ISA, and includes a simd extension called SPR (which is not IBM's cell SPE). Notably, it does not have altivec/fpr/vsx registers. It also has an SPE accumulator register which its ABI marks as volatile, but I am not sure if the compiler uses it.
2025-10-15 07:09:54 +02:00
Matthias Krüger
f8b65f7bc7 Rollup merge of #147526 - bjorn3:alloc_shim_weak_shape, r=petrochenkov,RalfJung
Move computation of allocator shim contents to cg_ssa

In the future this should make it easier to use weak symbols for the allocator shim on platforms that properly support weak symbols. And it would allow reusing the allocator shim code for handling default implementations of the upcoming externally implementable items feature on platforms that don't properly support weak symbols.

In addition to make this possible, the alloc error handler is now handled in a way such that it is possible to avoid using the allocator shim when liballoc is compiled without `no_global_oom_handling` if you use `#[alloc_error_handler]`. Previously this was only possible if you avoided liballoc entirely or compiled it with `no_global_oom_handling`. You still need to avoid libstd and to define the symbol that indicates that avoiding the allocator shim is unstable.
2025-10-14 19:47:29 +02:00
Paul Murphy
3c09d4a582 Allow vector-scalar (vs) registers in ppc inline assembly
Where supported, VSX is a 64x128b register set which encompasses
both the floating point and vector registers.

In the type tests, xvsqrtdp is used as it is the only two-argument
vsx opcode supported by all targets on llvm. If you need to copy
a vsx register, the preferred way is "xxlor xt, xa, xa".
2025-10-14 09:52:56 -05:00
Guillaume Gomez
3938f42bb1 Rollup merge of #147608 - Zalathar:debuginfo, r=nnethercote
cg_llvm: Use `LLVMDIBuilderCreateGlobalVariableExpression`

- Part of rust-lang/rust#134001
- Follow-up to rust-lang/rust#146763

---

This PR dismantles the somewhat complicated `LLVMRustDIBuilderCreateStaticVariable` function, and replaces it with equivalent calls to `LLVMDIBuilderCreateGlobalVariableExpression` and `LLVMGlobalSetMetadata`.

A key difference is that the new code does not replicate the attempted downcast of `InitVal`. As far as I can tell, those downcasts were actually dead, because `llvm::ConstantInt` and `llvm::ConstantFP` are not subclasses of `llvm::GlobalVariable`. I tried replacing those code paths with fatal errors, and was unable to induce failure in any of the relevant test suites I ran.

I have also confirmed that if the calls to `create_static_variable` are commented out, debuginfo tests will fail, demonstrating some amount of relevant test coverage.

The new `DIBuilder` methods have been added via an extension trait, not as inherent methods, to avoid impeding rust-lang/rust#142897.
2025-10-13 11:25:23 +02:00
Zalathar
1081d98551 Use LLVMDIBuilderCreateGlobalVariableExpression
Note that the code in `LLVMRustDIBuilderCreateStaticVariable` that tried to
downcast `InitVal` appears to have been dead, because `llvm::ConstantInt` and
`llvm::ConstantFP` are not subclasses of `llvm::GlobalVariable`.
2025-10-12 23:36:26 +11:00
Zalathar
1db7d41665 Extract DIBuilderExt::create_static_variable 2025-10-12 23:34:44 +11:00
Zalathar
45e9ebee31 Extract DIBuilderExt::create_expression 2025-10-12 23:34:44 +11:00
Zalathar
b6ea8242fd Hoist some stranded use declarations 2025-10-12 23:34:39 +11:00
AMS21
0abecda9ed Replace LLVMRustContextCreate with normal LLVM-C API calls
Since `LLVMRustContextCreate` can easily be replaced with a call to
`LLVMContextCreate` and `LLVMContextSetDiscardValueNames`.
2025-10-10 15:45:40 +02:00
bjorn3
7e467cd132 Move computation of allocator shim contents to cg_ssa
In the future this should make it easier to use weak symbols for the
allocator shim on platforms that properly support weak symbols. And it
would allow reusing the allocator shim code for handling default
implementations of the upcoming externally implementable items feature
on platforms that don't properly support weak symbols.
2025-10-10 13:04:55 +00:00
bjorn3
116f4ae171 Support #[alloc_error_handler] without the allocator shim
Currently it is possible to avoid linking the allocator shim when
__rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs
directly as some build systems need. However this requires liballoc to
be compiled with --cfg no_global_oom_handling, which places huge
restrictions on what functions you can call and makes it impossible to
use libstd. Or alternatively you have to define
__rust_alloc_error_handler and (when using libstd)
__rust_alloc_error_handler_should_panic
using #[rustc_std_internal_symbol]. With this commit you can either use
libstd and define __rust_alloc_error_handler_should_panic or not use
libstd and use #[alloc_error_handler] instead. Both options are still
unstable though.

Eventually the alloc_error_handler may either be removed entirely
(though the PR for that has been stale for years now) or we may start
using weak symbols for it instead. For the latter case this commit is a
prerequisite anyway.
2025-10-10 13:04:53 +00:00
bjorn3
f47c48048d Reduce scope of unsafe block in cg_llvm allocator codegen 2025-10-09 12:35:14 +00:00
bors
4b57d8154a Auto merge of #147519 - Zalathar:rollup-o5f16uo, r=Zalathar
Rollup of 3 pull requests

Successful merges:

 - rust-lang/rust#147446 (PassWrapper: use non-deprecated lookupTarget method)
 - rust-lang/rust#147473 (Do `x check` on various bootstrap tools in CI)
 - rust-lang/rust#147509 (remove intrinsic wrapper functions from LLVM bindings)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-10-09 10:54:43 +00:00
Stuart Cook
4dfd977c8b Rollup merge of #147488 - AMS21:remove_llvm_rust_insert_private_global, r=nikic
refactor: Remove `LLVMRustInsertPrivateGlobal` and `define_private_global`

Since it can easily be implemented using the existing LLVM C API in
terms of `LLVMAddGlobal` and `LLVMSetLinkage` and `define_private_global`
was only used in one place.

Work towards https://github.com/rust-lang/rust/issues/46437
2025-10-09 18:43:26 +11:00
AMS21
064e3b8212 remove intrinsic wrapper functions from LLVM bindings 2025-10-09 09:26:44 +02:00
AMS21
036ab3a925 refactor: Remove LLVMRustInsertPrivateGlobal and define_private_global
Since it can easily be implemented using the existing LLVM C API in
terms of `LLVMAddGlobal` and `LLVMSetLinkage` and `define_private_global`
was only used in one place.
2025-10-08 21:59:48 +02:00
Matthias Krüger
359bfa901b Rollup merge of #147472 - AMS21:remove_llvm_rust_atomics, r=nikic
refactor: replace `LLVMRustAtomicLoad/Store` with LLVM built-in functions

This simplifies the code and reduces the burden of maintaining our own wrappers.

Work towards https://github.com/rust-lang/rust/issues/46437
2025-10-08 15:39:28 +02:00
AMS21
1aed495ed7 refactor: replace LLVMRustAtomicLoad/Store with LLVM built-in functions 2025-10-08 13:53:09 +02:00
Manuel Drehwald
52e7917586 Use globals instead of metadata, since metadata isn't emitted in debug builds 2025-10-07 20:13:59 -04:00
Manuel Drehwald
dcc36a8642 add incremental/debug test for autodiff 2025-10-07 20:13:56 -04:00
Matthias Krüger
d60f0a2b17 Rollup merge of #147322 - Zalathar:llvm-imports, r=jdonszelmann
cg_llvm: Consistently import `llvm::Type` and `llvm::Value`

We already have other modules that import these types and other types from `llvm`, so having the re-exports `type_::Type` and `value::Value` just distracts rust-analyzer and results in messier and less-consistent imports.

No functional change.
2025-10-07 19:39:07 +02:00
Deadbeef
696b6ac14d use declarative macro for #[derive(TryFromU32)] 2025-10-06 14:54:38 +00:00
Zalathar
69a975faa9 Consistently import llvm::Type and llvm::Value 2025-10-06 13:09:16 +11:00
Zalathar
f8c54d24e2 Remove inherent methods from llvm::CallConv::from_conv 2025-10-04 18:47:18 +10:00
Zalathar
3831b60ef3 Remove inherent methods from llvm::Type 2025-10-04 18:47:18 +10:00
Zalathar
0661a3d6fe Remove inherent methods from coverageinfo::ffi::Counter
This patch also moves `Regions` to a different module, since that type can stay
put when the FFI bindings move to another crate.
2025-10-04 18:46:15 +10:00
Zalathar
f955c76a5b Remove inherent methods from several LLVM FFI types
Using a helper trait allows the conversions to remain in `rustc_codegen_llvm`,
even if the FFI types are moved to a different crate.
2025-10-04 18:16:13 +10:00
bors
8b6b15b877 Auto merge of #142771 - dianqk:mir-stmt-debuginfo, r=cjgillot
Introduce debuginfo to statements in MIR

The PR introduces support for debug information within dead statements. Currently, only the reference statement is supported, which is sufficient to fix rust-lang/rust#128081.

I don't modify Stable MIR, as I don't think we need debug information when using it.

This PR represents the debug information for the dead reference statement via `#dbg_value`. For example, `let _foo_b = &foo.b` becomes `#dbg_value(ptr %foo, !22, !DIExpression(DW_OP_plus_uconst, 4, DW_OP_stack_value), !26)`. You can see this here: https://rust.godbolt.org/z/d43js6adv.

The general principle for handling debug information is to never provide less debug information than the optimized LLVM IR.

The current rules for dropping debug information in this PR are:

- If the LLVM IR cannot represent a reference address, it's replaced with poison or simply dropped. For example, see: https://rust.godbolt.org/z/shGqPec8W. I'm using poison in all such cases now.
- All debuginfos is dropped when merging multiple successor BBs. An example is available here: https://rust.godbolt.org/z/TE1q3Wq6M.

I doesn't drop debuginfos in `MatchBranchSimplification`, because LLVM also pick one branch for it.
2025-10-03 11:49:42 +00:00
Zalathar
8ef9821bf2 Extract helper method global_set_metadata_node 2025-10-02 18:06:58 +10:00
Zalathar
6a58f80a3c Extract helper method global_add_metadata_node 2025-10-02 18:06:57 +10:00
Zalathar
ffeed2b94e Extract helper method module_add_named_metadata_node 2025-10-02 18:04:24 +10:00
Zalathar
ecb831dcf4 Extract helper method set_metadata_node 2025-10-02 18:04:23 +10:00
dianqk
1bd89bd42e codegen: Generate dbg_value for the ref statement 2025-10-02 14:55:51 +08:00
Stuart Cook
7b0236fbd8 Rollup merge of #147200 - ZuseZ4:fix-autodiff-emptry-ret, r=Zalathar
Fix autodiff empty ret regression

closes https://github.com/rust-lang/rust/issues/147144

The two gsoc summer projects caused a bit of churn, which was to be expected, especially since we don't run autodiff in CI yet.
This adds a void return testcase that we should have had anyway, and fixes the regression.

r? `@Zalathar` (Just guessing since I've seen you in a few LLVM PRs and Oli is probably still busy. Feel free to reroll!)
2025-10-01 22:15:01 +10:00
bors
1e1a39441b Auto merge of #147198 - matthiaskrgr:rollup-b0ryvvu, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#143069 (Add fast-path for accessing the current thread id)
 - rust-lang/rust#146518 (Improve the documentation around `ZERO_AR_DATE`)
 - rust-lang/rust#146596 (Add a dummy codegen backend)
 - rust-lang/rust#146617 (Don’t suggest foreign `doc(hidden)` types in "the following other types implement trait" diagnostics)
 - rust-lang/rust#146635 (cg_llvm: Stop using `as_c_char_ptr` for coverage-related bindings)
 - rust-lang/rust#147184 (Fix the bevy implied bounds hack for the next solver)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-10-01 04:46:14 +00:00
Manuel Drehwald
ddbaca521e fix void and empty struct ret 2025-09-30 22:47:40 -04:00
Matthias Krüger
b21c438eb5 Rollup merge of #146635 - Zalathar:llvm-cov, r=SparrowLii
cg_llvm: Stop using `as_c_char_ptr` for coverage-related bindings

[As explained by a note in `ffi.rs`](8a1b39995e/compiler/rustc_codegen_llvm/src/llvm/ffi.rs (L4-L11)), passing strings and byte slices through FFI is more convenient if we take advantage of the fact that `*const c_uchar` and `*const c_char` have the same ABI.

Doing so avoids having to rely on a special helper function, since we can just call `as_ptr` instead.

(The same logic applies to every other binding that currently uses the `as_c_char_ptr` helper; I just haven't adjusted all of them yet.)

---

As a drive-by change, this PR also marks some coverage-related FFI bindings as `safe`.
2025-09-30 21:53:35 +02:00
Zalathar
cc6329a9bc Replace MetadataType with the MetadataKindId constants 2025-09-30 20:10:30 +10:00
Zalathar
906bf49ade Declare all "fixed" metadata kinds as MetadataKindId 2025-09-30 20:10:10 +10:00
Zalathar
cd40bbfe29 Move MetadataKindId into its own submodule 2025-09-30 20:07:54 +10:00
Matthias Krüger
379979951e Rollup merge of #147150 - nikic:alloc-shim-attributes, r=bjorn3
Emit allocator attributes for allocator shim

This emits the same attributes we place on allocator declarations on the definitions in the allocator shim as well. This complements https://github.com/rust-lang/rust/pull/146766, which added the attribute for `#[global_allocator]` definitions. Emitting the attributes on the definitions ensures that they cannot be lost of the allocator shim participates in LTO.

See https://github.com/rust-lang/rust/issues/145995 for context, though that one was about `#[global_allocator]`. I'm not sure whether this can occur with the allocator shim as well or not, but better safe than sorry.

I'm not sure whether there is any good way to test this, as the allocator shim is not part of `--emit=llvm-ir`. I've verified this locally by inspecting the bitcode produced by `-C save-temps`.

r? ``@bjorn3``
2025-09-29 21:42:44 +02:00
Nikita Popov
af8fd78142 Emit allocator attributes for allocator shim
This emits the same attributes we place on allocator declarations
(and allocator definitions using `#[global_allocator]`) on the
definitions in the allocator shim as well, making sure that the
attributes are not lost if the allocator shim participates in LTO.
2025-09-29 11:29:59 +02:00
bors
128b36a4a4 Auto merge of #147145 - Zalathar:rollup-s7kcs3w, r=Zalathar
Rollup of 3 pull requests

Successful merges:

 - rust-lang/rust#147100 (tests: Remove ignore-android directive for fixed issue)
 - rust-lang/rust#147116 (compiler: remove AbiAlign inside TargetDataLayout)
 - rust-lang/rust#147134 (remove explicit deref of AbiAlign for most methods)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-09-29 08:43:49 +00:00
Stuart Cook
cd6f32a4eb Rollup merge of #147134 - workingjubilee:remove-explicit-abialign-deref, r=Zalathar
remove explicit deref of AbiAlign for most methods

Much of the compiler calls functions on Align projected from AbiAlign. AbiAlign impls Deref to its inner Align, so we can simplify these away. Also, it will minimize disruption when AbiAlign is removed.

For now, preserve usages that might resolve to PartialOrd or PartialEq, as those have odd inference.
2025-09-29 15:44:55 +10:00
Stuart Cook
6c40c16d83 Rollup merge of #147116 - workingjubilee:remove-tdl-abialign, r=Zalathar
compiler: remove AbiAlign inside TargetDataLayout

AbiAlign is a thin wrapper around Align, extant mostly because we used to track a separate quasi-notion of alignment that was never a real notion of alignment and removing all of it at once was too churny. This PR maintains AbiAlign usage in public API and most of the compiler, but direct access of these fields for TargetDataLayout is now in terms of Align only.
2025-09-29 15:44:54 +10:00
Stuart Cook
af8af6cc6a Rollup merge of #147127 - antoyo:fix/gcc-linker-plugin, r=bjorn3
Add a leading dash to linker plugin arguments in the gcc codegen

Fix rust-lang/rust#130583

r? ``@bjorn3``
2025-09-29 11:56:44 +10:00
Jubilee Young
0c9d0dfe04 remove explicit deref of AbiAlign for most methods
Much of the compiler calls functions on Align projected from AbiAlign.
AbiAlign impls Deref to its inner Align, so we can simplify these away.
Also, it will minimize disruption when AbiAlign is removed.

For now, preserve usages that might resolve to PartialOrd or PartialEq,
as those have odd inference.
2025-09-28 15:02:14 -07:00