502 Commits

Author SHA1 Message Date
bors
d36f964125 Auto merge of #145877 - nikic:capture-address, r=tmiasko
Use captures(address) instead of captures(none) for indirect args

While provenance cannot be captured through these arguments, the address / object identity can.

Fixes https://github.com/rust-lang/rust/issues/137668.

r? `@ghost`
2025-08-28 00:01:22 +00:00
Nikita Popov
c3ab409b4f Use captures(address) instead of captures(none) for indirect args
While provenance cannot be captured through these arguments, the
address / object identity can.
2025-08-26 16:16:23 +02:00
Zalathar
fcff8f7f5a Assert that LLVM range-attribute values don't exceed 128 bits
The underlying implementation of `LLVMCreateConstantRangeAttribute` assumes
that each of `LowerWords` and `UpperWords` points to enough u64 values to
define an integer of the specified bit-length, and will encounter UB if that is
not the case.

Our safe wrapper function always passes pointers to `[u64; 2]` arrays,
regardless of the bit-length specified. That's fine in practice, because scalar
primitives never exceed 128 bits, but it is technically a soundness hole in a
safe function.

We can close the soundness hole by explicitly asserting `size_bits <= 128`.
This is effectively just a stricter version of the existing check that the
value must be small enough to fit in `c_uint`.
2025-08-26 13:07:19 +10:00
Nikita Popov
d71ed8d19b Tell LLVM about read-only captures
`&Freeze` parameters are not only `readonly` within the function,
but any captures of the pointer can also only be used for reads.
This can now be encoded using the `captures(address, read_provenance)`
attribute.
2025-08-20 19:08:16 +02:00
Stuart Cook
8748d8e7d5
Rollup merge of #145484 - Zalathar:archive-builder, r=bjorn3
Remove `LlvmArchiveBuilder` and supporting code/bindings

Switching over to the newer Rust-based `ArArchiveBuilder` happened in rust-lang/rust#128936, a year ago.

Per the comment in `new_archive_builder`, that seems like enough time to justify removing the older, unused `LlvmArchiveBuilder` implementation and its associated bindings.

Fixes rust-lang/rust#128955.
2025-08-19 14:18:25 +10:00
Zalathar
cf8ec6798f Remove LlvmArchiveBuilder and supporting code/bindings 2025-08-16 16:38:12 +10:00
Zalathar
e193b5342b Use LLVMGetTypeKind 2025-08-15 19:35:35 +10:00
Zalathar
c64c6d85e1 Use LLVMSetTailCallKind 2025-08-15 13:57:37 +10:00
Nikita Popov
ebef9d7f63 Set dead_on_return attribute for indirect arguments
Set the dead_on_return attribute (added in LLVM 21) for arguments
that are passed indirectly, but not byval.

This indicates that the value of the argument on return does not
matter, enabling additional dead store elimination.
2025-08-11 12:39:23 +02:00
Zalathar
81ed042c8c coverage: Remove all unstable support for MC/DC instrumentation 2025-08-06 22:38:52 +10:00
Stuart Cook
8628b78f24
Rollup merge of #144232 - xacrimon:explicit-tail-call, r=WaffleLapkin
Implement support for `become` and explicit tail call codegen for the LLVM backend

This PR implements codegen of explicit tail calls via `become` in `rustc_codegen_ssa` and support within the LLVM backend. Completes a task on (https://github.com/rust-lang/rust/issues/112788). This PR implements all the necessary bits to make explicit tail calls usable, other backends have received stubs for now and will ICE if you use `become` on them. I suspect there is some bikeshedding to be done on how we should go about implementing this for other backends, but it should be relatively straightforward for GCC after this is merged.

During development I also put together a POC bytecode VM based on tail call dispatch to test these changes out and analyze the codegen to make sure it generates expected assembly. That is available [here](https://github.com/xacrimon/tcvm).
2025-07-31 15:42:00 +10:00
Matthias Krüger
ec86930c1d
Rollup merge of #144503 - bjorn3:lto_refactors3, r=petrochenkov
Various refactors to the codegen coordinator code (part 3)

Continuing from https://github.com/rust-lang/rust/pull/144062 this removes an option without any known users, uses the object crate in favor of LLVM for getting the LTO bitcode and improves the coordinator channel handling.
2025-07-28 08:36:54 +02:00
Joel Wejdenstål
a448837045
Implement support for explicit tail calls in the MIR block builders and the LLVM codegen backend. 2025-07-26 01:02:29 +02:00
bjorn3
fe2eeabe27 Use the object crate rather than LLVM for extracting bitcode sections 2025-07-25 11:21:28 +00:00
WANG Rui
23fda6084b RustWrapper: Suppress getNextNonDebugInfoInstruction
Link: https://github.com/llvm/llvm-project/pull/144383
2025-07-23 22:09:16 +08:00
许杰友 Jieyou Xu (Joe)
5e3eb25125
Rollup merge of #142097 - ZuseZ4:offload-host1, r=oli-obk
gpu offload host code generation

r? ghost

This will generate most of the host side code to use llvm's offload feature.
The first PR will only handle automatic mem-transfers to and from the device.
So if a user calls a kernel, we will copy inputs back and forth, but we won't do the actual kernel launch.
Before merging, we will use LLVM's Info infrastructure to verify that the memcopies match what openmp offloa generates in C++. `LIBOMPTARGET_INFO=-1 ./my_rust_binary` should print that a memcpy to and later from the device is happening.

A follow-up PR will generate the actual device-side kernel which will then do computations on the GPU.
A third PR will implement manual host2device and device2host functionality, but the goal is to minimize cases where a user has to overwrite our default handling due to performance issues.

I'm trying to get a full MVP out first, so this just recognizes GPU functions based on magic names. The final frontend will obviously move this over to use proper macros, like I'm already doing it for the autodiff work.
This work will also be compatible with std::autodiff, so one can differentiate GPU kernels.

Tracking:
- https://github.com/rust-lang/rust/issues/131513
2025-07-22 00:54:24 +08:00
Matthias Krüger
d24684ef4f
Rollup merge of #144116 - nikic:llvm-21-fixes, r=dianqk
Fixes for LLVM 21

This fixes compatibility issues with LLVM 21 without performing the actual upgrade. Split out from https://github.com/rust-lang/rust/pull/143684.

This fixes three issues:
 * Updates the AMDGPU data layout for address space 8.
 * Makes emit-arity-indicator.rs a no_core test, so it doesn't fail on non-x86 hosts.
 * Explicitly sets the exception model for wasm, as this is no longer implied by `-wasm-enable-eh`.
2025-07-20 08:56:08 +02:00
Matthias Krüger
44ee51de0b
Rollup merge of #142444 - KMJ-007:autodiff-codegen-test, r=ZuseZ4
adding run-make test to autodiff

r? `@ZuseZ4`
2025-07-19 08:55:34 +02:00
Manuel Drehwald
5958ebe829 add various wrappers for gpu code generation 2025-07-18 16:24:12 -07:00
Nikita Popov
12b19be741 Pass wasm exception model to TargetOptions
This is no longer implied by -wasm-enable-eh.
2025-07-18 09:35:50 +02:00
наб
a0111ec7a1
awhile -> a while where appropriate 2025-07-02 20:17:29 +02:00
Karan Janthe
5773d38c7a fix: Fix TypePrintFn flag passing for autodiff codegen
Signed-off-by: Karan Janthe <karanjanthe@gmail.com>
2025-07-02 02:55:21 +00:00
Zalathar
d1bb310a7a Use LLVMGetInlineAsm
This LLVM-C binding replaces the existing `LLVMRustInlineAsm` function.
2025-05-11 14:37:54 +10:00
Erick Tryzelaar
951412e2f3 PassWrapper: adapt for llvm/llvm-project@f137c3d592
In LLVM 21 PR https://github.com/llvm/llvm-project/pull/130940
`TargetRegistry::createTargetMachine` was changed to take a `const
Triple&` and has deprecated the old `StringRef` method.

@rustbot label llvm-main
2025-05-01 17:59:00 +00:00
Trevor Gross
219f0b7082
Rollup merge of #140400 - durin42:llvm-21-getguid, r=cuviper
PassWrapper: adapt for llvm/llvm-project@d3d856ad84

LLVM 21 moves to making it more explicit what this function call is doing, but nothing has changed behaviorally, so for now we just adjust to using the new name of the function.

`@rustbot` label llvm-main
2025-04-29 12:28:24 -04:00
Augie Fackler
ff6dad436d PassWrapper: adapt for llvm/llvm-project@d3d856ad84
LLVM 21 moves to making it more explicit what this function call is
doing, but nothing has changed behaviorally, so for now we just adjust
to using the new name of the function.

@rustbot label llvm-main
2025-04-28 11:53:43 -04:00
bit-aloo
7018392337
remove noinline attribute and add alwaysinline after AD pass 2025-04-28 21:10:32 +05:30
bit-aloo
f319dd909e
add llvm wrappers and corresponding methods in attribute 2025-04-25 11:09:52 +05:30
Manuel Drehwald
75f86e6e2e fix LooseTypes flag and PrintMod behaviour, add debug helper 2025-04-12 01:36:44 -04:00
Josh Stone
12167d7064 Update the minimum external LLVM to 19 2025-04-05 11:44:38 -07:00
Stuart Cook
c6bf3a01ef
Rollup merge of #137880 - EnzymeAD:autodiff-batching, r=oli-obk
Autodiff batching

Enzyme supports batching, which is especially known from the ML side when training neural networks.
There we would normally have a training loop, where in each iteration we would pass in some data (e.g. an image), and a target vector. Based on how close we are with our prediction we compute our loss, and then use backpropagation to compute the gradients and update our weights.
That's quite inefficient, so what you normally do is passing in a batch of 8/16/.. images and targets, and compute the gradients for those all at once, allowing better optimizations.

Enzyme supports batching in two ways, the first one (which I implemented here) just accepts a Batch size,
and then each Dual/Duplicated argument has not one, but N shadow arguments.  So instead of
```rs
for i in 0..100 {
   df(x[i], y[i], 1234);
}
```
You can now do
```rs
for i in 0..100.step_by(4) {
   df(x[i+0],x[i+1],x[i+2],x[i+3], y[i+0], y[i+1], y[i+2], y[i+3], 1234);
}
```
which will give the same results, but allows better compiler optimizations. See the testcase for details.

There is a second variant, where we can mark certain arguments and instead of having to pass in N shadow arguments, Enzyme assumes that the argument is N times longer. I.e. instead of accepting 4 slices with 12 floats each, we would accept one slice with 48 floats. I'll implement this over the next days.

I will also add more tests for both modes.

For any one preferring some more interactive explanation, here's a video of Tim's llvm dev talk, where he presents his work. https://www.youtube.com/watch?v=edvaLAL5RqU
I'll also add some other docs to the dev guide and user docs in another PR.

r? ghost

Tracking:

- https://github.com/rust-lang/rust/issues/124509
- https://github.com/rust-lang/rust/issues/135283
2025-04-05 13:18:13 +11:00
Manuel Drehwald
b7c63a973f add autodiff batching backend 2025-04-04 14:24:23 -04:00
Augie Fackler
b14a0ce7f6 PassWrapper: adapt for llvm/llvm-project@94122d58fc
We also have to remove the LLVM argument in cast-target-abi.rs for LLVM
21. I'm not really sure what the best approach here is since that test
already uses revisions. We could also fork the test into a copy for LLVM
19-20 and another for LLVM 21, but what I did for now was drop the
lint-abort-on-error flag to LLVM figuring that some coverage was better
than none, but I'm happy to change this if that was a bad direction.

The above also applies for ffi-out-of-bounds-loads.rs.

r? dianqk
@rustbot label llvm-main
2025-03-31 15:47:26 -04:00
Zalathar
d07ef5b0e1 coverage: Add LLVM plumbing for expansion regions
This is currently unused, but paves the way for future work on expansion
regions without having to worry about the FFI parts.
2025-03-20 12:40:36 +11:00
Hans Wennborg
eb2e421e36 Adapt to LLVM dropping CfiFunctionIndex::begin()/end()
After https://github.com/llvm/llvm-project/pull/130382, RustWrapper
needs to call CfiFunctionIndex::symbols() instead.
2025-03-12 20:03:29 +01:00
Jacob Pratt
b16047780d
Rollup merge of #138137 - ZequanWu:fix-triple, r=cuviper
setTargetTriple now accepts Triple rather than string

https://github.com/llvm/llvm-project/pull/129868 updated `setTargetTriple`
2025-03-07 21:57:52 -05:00
Zequan Wu
8814679a54 rename Triple to Target 2025-03-06 22:52:20 -08:00
Zequan Wu
4e4bed8684 setTargetTriple now accepts Triple rather than string 2025-03-06 16:37:57 -08:00
Josh Stone
feae27945b [llvm/PassWrapper] use size_t when building arg strings 2025-03-06 10:09:19 -08:00
bors
0c72c0d11a Auto merge of #133250 - DianQK:embed-bitcode-pgo, r=nikic
The embedded bitcode should always be prepared for LTO/ThinLTO

Fixes #115344. Fixes #117220.

There are currently two methods for generating bitcode that used for LTO. One method involves using `-C linker-plugin-lto` to emit object files as bitcode, which is the typical setting used by cargo. The other method is through `-C embed-bitcode=yes`.

When using with `-C embed-bitcode=yes -C lto=no`, we run a complete non-LTO LLVM pipeline to obtain bitcode, then the bitcode is used for LTO. We run the Call Graph Profile Pass twice on the same module.

This PR is doing something similar to LLVM's `buildFatLTODefaultPipeline`, obtaining the bitcode for embedding after running `buildThinLTOPreLinkDefaultPipeline`.

r? nikic
2025-03-01 08:22:18 +00:00
bors
e0be1a0262 Auto merge of #137271 - nikic:gep-nuw-2, r=scottmcm
Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes https://github.com/rust-lang/rust/issues/137217.
2025-02-24 03:06:16 +00:00
DianQK
1a99ca8da9
The embedded bitcode should always be prepared for LTO/ThinLTO 2025-02-23 21:23:36 +08:00
Manuel Drehwald
161a4bf6ff update enzyme submodule and users 2025-02-21 21:49:46 -05:00
Nikita Popov
5e9d8a7d55 Switch to the LLVMBuildGEPWithNoWrapFlags API
This API allows us to set the nuw flag as well.
2025-02-19 11:32:32 +01:00
Nikita Popov
97f6e4d34b Quote embedded codeview command line arguments
The formatting of the command line arguments has been moved to the
frontend in:
e190d074a0

However, the Rust logic introduced in
ad0ecebf43
did not replicate the previous argument quoting behavior.
2025-02-14 11:02:28 +01:00
Matthias Krüger
78f5bddd57
Rollup merge of #136419 - EnzymeAD:autodiff-tests, r=onur-ozkan,jieyouxu
adding autodiff tests

I'd like to get started with upstreaming some tests, even though I'm still waiting for an answer on how to best integrate the enzyme pass. Can we therefore temporarily support the -Z llvm-plugins here without too much effort? And in that case, how would that work? I saw you can do remapping, e.g. `rust-src-base`, but I don't think that will give me the path to libEnzyme.so. Do you have another suggestion?

Other than that this test simply checks that the derivative of `x*x` is `2.0 * x`, which in this case is computed as
`%0 = fadd fast double %x.0.val, %x.0.val`
(I'll add a few more tests and move it to an autodiff folder if we can use the -Z flag)

r? ``@jieyouxu``

Locally at least `-Zllvm-plugins=${PWD}/build/x86_64-unknown-linux-gnu/enzyme/build/Enzyme/libEnzyme-19.so` seems to work if I copy the command I get from x.py test and run it manually. However, running x.py test itself fails.

Tracking:

- https://github.com/rust-lang/rust/issues/124509

Zulip discussion: https://rust-lang.zulipchat.com/#narrow/channel/326414-t-infra.2Fbootstrap/topic/Enzyme.20build.20changes
2025-02-10 16:38:23 +01:00
Manuel Drehwald
1221cff551 move second opt run to lto phase and cleanup code 2025-02-10 01:35:22 -05:00
Manuel Drehwald
21d096184e fix non-enzyme builds 2025-02-07 22:27:46 -05:00
Manuel Drehwald
8f87e8095e get tests to work without -Z/-C flags 2025-02-07 01:58:48 -05:00
Daniel Paoliello
2a6b27444a Remove dead code from rustc_codegen_llvm and the LLVM wrapper 2025-02-06 16:53:52 -08:00