about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/llvm
AgeCommit message (Collapse)AuthorLines
2025-09-28Rollup merge of #144197 - KMJ-007:type-tree, r=ZuseZ4Matthias Krüger-1/+205
TypeTree support in autodiff # TypeTrees for Autodiff ## What are TypeTrees? Memory layout descriptors for Enzyme. Tell Enzyme exactly how types are structured in memory so it can compute derivatives efficiently. ## Structure ```rust TypeTree(Vec<Type>) Type { offset: isize, // byte offset (-1 = everywhere) size: usize, // size in bytes kind: Kind, // Float, Integer, Pointer, etc. child: TypeTree // nested structure } ``` ## Example: `fn compute(x: &f32, data: &[f32]) -> f32` **Input 0: `x: &f32`** ```rust TypeTree(vec![Type { offset: -1, size: 8, kind: Pointer, child: TypeTree(vec![Type { offset: -1, size: 4, kind: Float, child: TypeTree::new() }]) }]) ``` **Input 1: `data: &[f32]`** ```rust TypeTree(vec![Type { offset: -1, size: 8, kind: Pointer, child: TypeTree(vec![Type { offset: -1, size: 4, kind: Float, // -1 = all elements child: TypeTree::new() }]) }]) ``` **Output: `f32`** ```rust TypeTree(vec![Type { offset: -1, size: 4, kind: Float, child: TypeTree::new() }]) ``` ## Why Needed? - Enzyme can't deduce complex type layouts from LLVM IR - Prevents slow memory pattern analysis - Enables correct derivative computation for nested structures - Tells Enzyme which bytes are differentiable vs metadata ## What Enzyme Does With This Information: Without TypeTrees (current state): ```llvm ; Enzyme sees generic LLVM IR: define float ``@distance(ptr*`` %p1, ptr* %p2) { ; Has to guess what these pointers point to ; Slow analysis of all memory operations ; May miss optimization opportunities } ``` With TypeTrees (our implementation): ```llvm define "enzyme_type"="{[]:Float@float}" float ``@distance(`` ptr "enzyme_type"="{[]:Pointer}" %p1, ptr "enzyme_type"="{[]:Pointer}" %p2 ) { ; Enzyme knows exact type layout ; Can generate efficient derivative code directly } ``` # TypeTrees - Offset and -1 Explained ## Type Structure ```rust Type { offset: isize, // WHERE this type starts size: usize, // HOW BIG this type is kind: Kind, // WHAT KIND of data (Float, Int, Pointer) child: TypeTree // WHAT'S INSIDE (for pointers/containers) } ``` ## Offset Values ### Regular Offset (0, 4, 8, etc.) **Specific byte position within a structure** ```rust struct Point { x: f32, // offset 0, size 4 y: f32, // offset 4, size 4 id: i32, // offset 8, size 4 } ``` TypeTree for `&Point` (internal representation): ```rust TypeTree(vec![ Type { offset: 0, size: 4, kind: Float }, // x at byte 0 Type { offset: 4, size: 4, kind: Float }, // y at byte 4 Type { offset: 8, size: 4, kind: Integer } // id at byte 8 ]) ``` Generates LLVM: ```llvm "enzyme_type"="{[]:Float@float}" ``` ### Offset -1 (Special: "Everywhere") **Means "this pattern repeats for ALL elements"** #### Example 1: Array `[f32; 100]` ```rust TypeTree(vec![Type { offset: -1, // ALL positions size: 4, // each f32 is 4 bytes kind: Float, // every element is float }]) ``` Instead of listing 100 separate Types with offsets `0,4,8,12...396` #### Example 2: Slice `&[i32]` ```rust // Pointer to slice data TypeTree(vec![Type { offset: -1, size: 8, kind: Pointer, child: TypeTree(vec![Type { offset: -1, // ALL slice elements size: 4, // each i32 is 4 bytes kind: Integer }]) }]) ``` #### Example 3: Mixed Structure ```rust struct Container { header: i64, // offset 0 data: [f32; 1000], // offset 8, but elements use -1 } ``` ```rust TypeTree(vec![ Type { offset: 0, size: 8, kind: Integer }, // header Type { offset: 8, size: 4000, kind: Pointer, child: TypeTree(vec![Type { offset: -1, size: 4, kind: Float // ALL array elements }]) } ]) ```
2025-09-28Rollup merge of #146763 - Zalathar:di-builder, r=jdonszelmannMatthias Krüger-40/+57
cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 5) - Part of rust-lang/rust#134001 - Follow-up to rust-lang/rust#146673 --- This is another batch of LLVMDIBuilder binding migrations, replacing some our own LLVMRust bindings with bindings to upstream LLVM-C APIs. Some of these are a little more complex than most of the previous migrations, because they split one LLVMRust binding into multiple LLVM bindings, but nothing too fancy. This appears to be the last of the low-hanging fruit. As noted in https://github.com/rust-lang/rust/issues/134001#issuecomment-2524979268, the remaining bindings are difficult or impossible to migrate at present.
2025-09-25Use `LLVMDisposeTargetMachine`Zalathar-1/+2
2025-09-21emit attribute for readonly non-pure inline assemblyFolkert de Vries-0/+1
2025-09-19Auto merge of #146700 - Zalathar:quoted-args, r=nikicbors-2/+4
cg_llvm: Move target machine command-line quoting from C++ to Rust When this code was introduced in rust-lang/rust#130446 and rust-lang/rust#131805, it was complicated by the need to maintain compatibility with earlier versions of LLVM. Now that LLVM 20 is the baseline (rust-lang/rust#145071), we can do all of the quoting in pure Rust code, and pass two flat strings to LLVM to be used as-is. --- In this PR, my priority has been to preserve the existing behaviour as much as possible, without worrying too much about what the behaviour *should* be. (Though I did avoid a leading space before the first argument.)
2025-09-19Use `LLVMDIBuilderCreate(Auto|Parameter)Variable`Zalathar-16/+27
2025-09-19Use `LLVMDIBuilder(CreateExpression|InsertDeclareRecordAtEnd)`Zalathar-10/+17
2025-09-19autodiff: typetree recursive depth query from enzyme with fallbackKaran Janthe-1/+0
Signed-off-by: Karan Janthe <karanjanthe@gmail.com>
2025-09-19Use `LLVMDIBuilderGetOrCreateArray`Zalathar-6/+6
2025-09-19Use `LLVMDIBuilderGetOrCreateSubrange`Zalathar-8/+7
2025-09-19autodiff: recurion added for typetreeKaran Janthe-0/+1
2025-09-19autodiff: fixed test to be more precise for type tree checkingKaran Janthe-0/+23
2025-09-19autodiff: f128 support added for typetreeKaran Janthe-1/+1
2025-09-19added typetree support for memcpyKaran Janthe-0/+1
2025-09-19Add TypeTree metadata attachment for autodiffKaran Janthe-1/+181
- Add F128 support to TypeTree Kind enum - Implement TypeTree FFI bindings and conversion functions - Add typetree.rs module for metadata attachment to LLVM functions - Integrate TypeTree generation with autodiff intrinsic pipeline - Support scalar types: f32, f64, integers, f16, f128 - Attach enzyme_type attributes as LLVM string metadata for Enzyme Signed-off-by: Karan Janthe <karanjanthe@gmail.com>
2025-09-18Move target machine command-line quoting from C++ to RustZalathar-2/+4
2025-09-18Rollup merge of #146673 - Zalathar:di-builder, r=nnethercoteStuart Cook-44/+47
cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 4) - Part of rust-lang/rust#134001 - Follow-up to rust-lang/rust#146631 --- This is another batch of LLVMDIBuilder binding migrations, replacing some our own LLVMRust bindings with bindings to upstream LLVM-C APIs.
2025-09-17Rollup merge of #146598 - bjorn3:feature_llvm_enzyme, r=davidtwcoJana Dönszelmann-4/+4
Make llvm_enzyme a regular cargo feature This makes it clearer that it is set by the build system rather than by the rustc that compiles the current rustc. It also avoids bootstrap needing to pass `--check-cfg llvm_enzyme` to rustc.
2025-09-17Use `LLVMDIBuilderCreateTypedef`Zalathar-11/+12
2025-09-17Use `LLVMDIBuilderCreateQualifiedType`Zalathar-6/+8
2025-09-17Use `LLVMDIBuilderCreateStaticMemberType`Zalathar-13/+13
2025-09-17Use `LLVMDIBuilderCreateMemberType`Zalathar-14/+14
2025-09-17Rollup merge of #146631 - Zalathar:di-builder, r=nnethercoteStuart Cook-68/+75
cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 3) - Part of rust-lang/rust#134001 - Follow-up to rust-lang/rust#136375 - Follow-up to rust-lang/rust#136632 --- This is another batch of LLVMDIBuilder binding migrations, replacing some our own LLVMRust bindings with bindings to upstream LLVM-C APIs. This PR migrates all of the bindings that were touched by rust-lang/rust#136632, plus `LLVMDIBuilderCreateStructType`.
2025-09-17Use `LLVMDIBuilderCreateStructType`Zalathar-21/+22
2025-09-17Use `LLVMDIBuilderCreatePointerType`Zalathar-10/+10
2025-09-17Use `LLVMDIBuilderCreateBasicType`Zalathar-8/+9
2025-09-17Use `LLVMDIBuilderCreateArrayType`Zalathar-8/+9
2025-09-17Use `LLVMDIBuilderCreateUnionType`Zalathar-16/+17
2025-09-17Use `LLVMDIBuilderCreateSubroutineType`Zalathar-5/+8
2025-09-15Make llvm_enzyme a regular cargo featurebjorn3-4/+4
This makes it clearer that it is set by the build system rather than by the rustc that compiles the current rustc. It also avoids bootstrap needing to pass --check-cfg llvm_enzyme to rustc.
2025-09-13initial implementation of the darwin_objc unstable featureJo Bates-0/+5
2025-09-06Remove want_summary argument from prepare_thinbjorn3-1/+0
It is always false nowadays. ThinLTO summary writing is instead done by llvm_optimize.
2025-08-29Update to ar_archive_writer 0.5.1Daniel Paoliello-0/+2
2025-08-28Auto merge of #145877 - nikic:capture-address, r=tmiaskobors-1/+1
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-26Use captures(address) instead of captures(none) for indirect argsNikita Popov-1/+1
While provenance cannot be captured through these arguments, the address / object identity can.
2025-08-26Assert that LLVM range-attribute values don't exceed 128 bitsZalathar-11/+27
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-24Rename `llvm::Bool` aliases to standard const caseZalathar-3/+2
This avoids the need for `#![allow(non_upper_case_globals)]`.
2025-08-24Replace the `llvm::Bool` typedef with a proper newtypeZalathar-6/+55
2025-08-20Tell LLVM about read-only capturesNikita Popov-0/+1
`&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-19Rollup merge of #145484 - Zalathar:archive-builder, r=bjorn3Stuart Cook-157/+0
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-19Rollup merge of #145432 - Zalathar:target-machine, r=wesleywiserStuart Cook-1/+1
cg_llvm: Small cleanups to `owned_target_machine` This PR contains a few tiny cleanups to the `owned_target_machine` code. Each individual commit should be fairly straightforward.
2025-08-18Rollup merge of #145420 - Zalathar:llvm-c, r=WaffleLapkinStuart Cook-7/+12
cg_llvm: Use LLVM-C bindings for `LLVMSetTailCallKind`, `LLVMGetTypeKind` This PR replaces two existing `LLVMRust` bindings with equivalent calls to the LLVM-C API. For `LLVMGetTypeKind`, we avoid the UB hazard by declaring the foreign function to return `RawEnum<TypeKind>` (which is a wrapper around `u32`), and then perform checked conversion from `u32` to `TypeKind`.
2025-08-16Remove `LlvmArchiveBuilder` and supporting code/bindingsZalathar-157/+0
2025-08-15Avoid an explicit cast from `*const c_uchar` to `*const c_char`Zalathar-1/+1
As noted in the `ffi` module docs, passing pointer/length byte strings from Rust to C++ is easier if we declare them as `*const c_uchar` on the Rust side, but `const char *` (possibly signed) on the C++ side. This is allowed because both pointer types are ABI-compatible, regardless of char signedness.
2025-08-15Use `LLVMGetTypeKind`Zalathar-6/+10
2025-08-15Use `LLVMSetTailCallKind`Zalathar-1/+2
2025-08-14Remove lto inline logicMarcelo Domínguez-26/+0
2025-08-11Set dead_on_return attribute for indirect argumentsNikita Popov-0/+1
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-06coverage: Remove all unstable support for MC/DC instrumentationZalathar-4/+0
2025-07-31Rollup merge of #144232 - xacrimon:explicit-tail-call, r=WaffleLapkinStuart Cook-0/+11
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).