about summary refs log tree commit diff
path: root/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
AgeCommit message (Collapse)AuthorLines
2022-07-27Rollup merge of #99759 - bjorn3:remove_llvm_dead_code, r=nikicYuki Okushi-9/+0
Remove dead code from cg_llvm Found while working on https://github.com/rust-lang/rust/pull/97485
2022-07-26codegen: use new {re,de,}allocator annotations in llvmAugie Fackler-0/+69
This obviates the patch that teaches LLVM internals about _rust_{re,de}alloc functions by putting annotations directly in the IR for the optimizer. The sole test change is required to anchor FileCheck to the body of the `box_uninitialized` method, so it doesn't see the `allocalign` on `__rust_alloc` and get mad about the string `alloca` showing up. Since I was there anyway, I added some checks on the attributes to prove the right attributes got set. While we're here, we also emit allocator attributes on __rust_alloc_zeroed. This should allow LLVM to perform more optimizations for zeroed blocks, and probably fixes #90032. [This comment](https://github.com/rust-lang/rust/issues/24194#issuecomment-308791157) mentions "weird UB-like behaviour with bitvec iterators in rustc_data_structures" so we may need to back this change out if things go wrong. The new test cases require LLVM 15, so we copy them into LLVM 14-supporting versions, which we can delete when we drop LLVM 14.
2022-07-26Remove dead code from cg_llvmbjorn3-9/+0
2022-07-20Add ShadowCallStack SupportIvan Lozano-0/+2
Adds support for the LLVM ShadowCallStack sanitizer.
2022-07-18Rollup merge of #98998 - ↵Dylan DPC-0/+2
workingjubilee:naked-means-no-clothes-enforcement-technology, r=Amanieu Remove branch target prologues from `#[naked] fn` This patch hacks around rust-lang/rust#98768 for now via injecting appropriate attributes into the LLVMIR we emit for naked functions. I intend to pursue this upstream so that these attributes can be removed in general, but it's slow going wading through C++ for me.
2022-07-12llvm-wrapper: adapt for LLVM API changeKrasimir Georgiev-0/+6
2022-07-06Stop emitting CET prologues for naked functionsJubilee Young-0/+2
We can apply nocf_check as a hack for now.
2022-06-30llvm-wrapper: adapt for LLVMConstExtractValue removalKrasimir Georgiev-0/+8
2022-06-14Add metadata generation for vtables when using VFEflip1995-0/+5
This adds the typeid and `vcall_visibility` metadata to vtables when the -Cvirtual-function-elimination flag is set. The typeid is generated in the same way as for the `llvm.type.checked.load` intrinsic from the trait_ref. The offset that is added to the typeid is always 0. This is because LLVM assumes that vtables are constructed according to the definition in the Itanium ABI. This includes an "address point" of the vtable. In C++ this is the offset in the vtable where information for RTTI is placed. Since there is no RTTI information in Rust's vtables, this "address point" is always 0. This "address point" in combination with the offset passed to the `llvm.type.checked.load` intrinsic determines the final function that should be loaded from the vtable in the `WholeProgramDevirtualization` pass in LLVM. That's why the `llvm.type.checked.load` intrinsics are generated with the typeid of the trait, rather than with that of the function that is called. This matches what `clang` does for C++. The vcall_visibility metadata depends on three factors: 1. LTO level: Currently this is always fat LTO, because LLVM only supports this optimization with fat LTO. 2. Visibility of the trait: If the trait is publicly visible, VFE can only act on its vtables after linking. 3. Number of CGUs: if there is more than one CGU, also vtables with restricted visibility could be seen outside of the CGU, so VFE can only act on them after linking. To reflect this, there are three visibility levels: Public, LinkageUnit, and TranslationUnit.
2022-06-14Add LLVM module flags required for the VFE optflip1995-0/+5
To apply the optimization the `Virtual Function Elim` module flag has to be set. To apply this optimization post-link the `LTOPostLink` module flag has to be set.
2022-06-07RustWrapper: adapt to APInt API changes in LLVM 15Augie Fackler-0/+8
In https://reviews.llvm.org/D125556 upstream changed sext() and zext() to allow some no-op cases, which previously required use of the *OrSelf() methods, which I assume is what was going on here. The *OrSelf() methods got removed in https://reviews.llvm.org/D125559 after two weeks of deprecation because they came with some bonus (probably-undesired) behavior. Since the behavior of sext() and zext() changed slightly, I kept the old *OrSelf() calls in LLVM 14 and earlier, and only use the new version in LLVM 15. r? @nikic
2022-04-28RustWrapper: explicitly don't handle DXILPointerTyIDAugie Fackler-0/+5
This new enum entry was introduced in https://reviews.llvm.org/D122268, and if I'm reading correctly there's no case where we'd ever encounter it in our uses of LLVM. To preserve the ability to compile this file with -Werror -Wswitch we add an explicit case for this entry.
2022-04-20Add missing includeNikita Popov-0/+1
2022-04-15Add codegen for global_asm! sym operandsAmanieu d'Antras-0/+6
2022-03-10RustWrapper: add missing includeAugie Fackler-0/+1
This is required after LLVM change 3c4410dfcaaf (aka https://reviews.llvm.org/D121168) did some includes cleanup.
2022-03-03Pass LLVM string attributes as string slicesTomasz Miąsko-6/+0
2022-03-02Auto merge of #94229 - erikdesjardins:rem2, r=nikicbors-26/+0
Remove LLVM attribute removal This was necessary before, because `declare_raw_fn` would always apply the default optimization attributes to every declared function. Then `attributes::from_fn_attrs` would have to remove the default attributes in the case of, e.g. `#[optimize(speed)]` in a `-Os` build. (see [`src/test/codegen/optimize-attr-1.rs`](https://github.com/rust-lang/rust/blob/03a8cc7df1d65554a4d40825b0490c93ac0f0236/src/test/codegen/optimize-attr-1.rs#L33)) However, every relevant callsite of `declare_raw_fn` (i.e. where we actually generate code for the function, and not e.g. a call to an intrinsic, where optimization attributes don't [?] matter) calls `from_fn_attrs`, so we can remove the attribute setting from `declare_raw_fn`, and rely on `from_fn_attrs` to apply the correct attributes all at once. r? `@ghost` (blocked on #94221) `@rustbot` label S-blocked
2022-02-28Remove LLVM attribute removalErik Desjardins-26/+0
This was necessary before, because `declare_raw_fn` would always apply the default optimization attributes to every declared function, and then `attributes::from_fn_attrs` would have to remove the default attributes in the case of, e.g. `#[optimize(speed)]` in a `-Os` build. However, every relevant callsite of `declare_raw_fn` (i.e. where we actually generate code for the function, and not e.g. a call to an intrinsic, where optimization attributes don't [?] matter) calls `from_fn_attrs`, so we can simply remove the attribute setting from `declare_raw_fn`, and rely on `from_fn_attrs` to apply the correct attributes all at once.
2022-02-27Revert "Auto merge of #92419 - erikdesjardins:coldland, r=nagisa"Erik Desjardins-8/+0
This reverts commit 4f49627c6fe2a32d1fed6310466bb0e1c535c0c0, reversing changes made to 028c6f1454787c068ff5117e9000a1de4fd98374.
2022-02-26AttrBuilder doesn't take a context in old LLVMErik Desjardins-4/+7
2022-02-26use attrbuilder to remove attrs in old LLVMErik Desjardins-4/+7
2022-02-26Add LLVM attributes in batches instead of individuallyErik Desjardins-105/+60
This should improve performance.
2022-02-18Rollup merge of #91675 - ivanloz:memtagsan, r=nagisaMatthias Krüger-0/+2
Add MemTagSanitizer Support Add support for the LLVM [MemTagSanitizer](https://llvm.org/docs/MemTagSanitizer.html). On hardware which supports it (see caveats below), the MemTagSanitizer can catch bugs similar to AddressSanitizer and HardwareAddressSanitizer, but with lower overhead. On a tag mismatch, a SIGSEGV is signaled with code SEGV_MTESERR / SEGV_MTEAERR. # Usage `-Zsanitizer=memtag -C target-feature="+mte"` # Comments/Caveats * MemTagSanitizer is only supported on AArch64 targets with hardware support * Requires `-C target-feature="+mte"` * LLVM MemTagSanitizer currently only performs stack tagging. # TODO * Tests * Example
2022-02-16MemTagSanitizer SupportIvan Lozano-0/+2
Adds support for the LLVM MemTagSanitizer.
2022-02-14llvm: migrate to new parameter-bearing uwtable attrAugie Fackler-0/+11
In https://reviews.llvm.org/D114543 the uwtable attribute gained a flag so that we can ask for sync uwtables instead of async, as the former are much cheaper. The default is async, so that's what I've done here, but I left a TODO that we might be able to do better. While in here I went ahead and dropped support for removing uwtable attributes in rustc: we never did it, so I didn't write the extra C++ bridge code to make it work. Maybe I should have done the same thing with the `sync|async` parameter but we'll see.
2022-02-05Apply noundef attribute to &T, &mut T, Box<T>, boolErik Desjardins-0/+2
This doesn't handle `char` because it's a bit awkward to distinguish it from u32 at this point in codegen. Note that for some types (like `&Struct` and `&mut Struct`), we already apply `dereferenceable`, which implies `noundef`, so the IR does not change.
2022-01-27Windows: Disable LLVM crash dialog boxes.Eric Huss-0/+4
2022-01-24Use error-on-mismatch policy for PAuth module flags.Jacob Bramley-3/+6
This agrees with Clang, and avoids an error when using LTO with mixed C/Rust. LLVM considers different behaviour flags to be a mismatch, even when the flag value itself is the same. This also makes the flag setting explicit for all uses of LLVMRustAddModuleFlag.
2022-01-06Rollup merge of #92559 - durin42:llvm-14-attributemask, r=nikicMatthias Krüger-5/+3
RustWrapper: adapt to new AttributeMask API Upstream LLVM change 9290ccc3c1a1 migrated attribute removal to use AttributeMask instead of AttrBuilder, so we need to follow suit here. r? ``@nagisa`` cc ``@nikic``
2022-01-05RustWrapper: simplify removing attributesAugie Fackler-7/+3
Avoids some extra conversions. Spotted by nikic during review.
2022-01-04RustWrapper: adapt to new AttributeMask APIAugie Fackler-2/+4
Upstream LLVM change 9290ccc3c1a1 migrated attribute removal to use AttributeMask instead of AttrBuilder, so we need to follow suit here.
2022-01-03RustWrapper: adapt for an LLVM API changeKrasimir Georgiev-4/+4
No functional changes intended. The LLVM commit https://github.com/llvm/llvm-project/commit/ec501f15a8b8ace2b283732740d6d65d40d82e09 removed the signed version of `createExpression`. This adapts the Rust LLVM wrappers accordingly.
2021-12-30keep noinline for system llvm < 14Erik Desjardins-0/+8
2021-12-03LLVM codgen support for unwinding inline assemblycynecx-0/+8
2021-12-03Adjust llvm wrapper for unwinding support for inlineasmcynecx-2/+3
2021-11-28Rollup merge of #90833 - tmiasko:optimization-remarks, r=nikicMatthias Krüger-0/+93
Emit LLVM optimization remarks when enabled with `-Cremark` The default diagnostic handler considers all remarks to be disabled by default unless configured otherwise through LLVM internal flags: `-pass-remarks`, `-pass-remarks-missed`, and `-pass-remarks-analysis`. This behaviour makes `-Cremark` ineffective on its own. Fix this by configuring a custom diagnostic handler that enables optimization remarks based on the value of `-Cremark` option. With `-Cremark=all` enabling all remarks. Fixes #90924. r? `@nikic`
2021-11-22add rustc option for using LLVM stack smash protectionBenjamin A. Bjørnseth-0/+6
LLVM has built-in heuristics for adding stack canaries to functions. These heuristics can be selected with LLVM function attributes. This patch adds a rustc option `-Z stack-protector={none,basic,strong,all}` which controls the use of these attributes. This gives rustc the same stack smash protection support as clang offers through options `-fno-stack-protector`, `-fstack-protector`, `-fstack-protector-strong`, and `-fstack-protector-all`. The protection this can offer is demonstrated in test/ui/abi/stack-protector.rs. This fills a gap in the current list of rustc exploit mitigations (https://doc.rust-lang.org/rustc/exploit-mitigations.html), originally discussed in #15179. Stack smash protection adds runtime overhead and is therefore still off by default, but now users have the option to trade performance for security as they see fit. An example use case is adding Rust code in an existing C/C++ code base compiled with stack smash protection. Without the ability to add stack smash protection to the Rust code, the code base artifacts could be exploitable in ways not possible if the code base remained pure C/C++. Stack smash protection support is present in LLVM for almost all the current tier 1/tier 2 targets: see test/assembly/stack-protector/stack-protector-target-support.rs. The one exception is nvptx64-nvidia-cuda. This patch follows clang's example, and adds a warning message printed if stack smash protection is used with this target (see test/ui/stack-protector/warn-stack-protector-unsupported.rs). Support for tier 3 targets has not been checked. Since the heuristics are applied at the LLVM level, the heuristics are expected to add stack smash protection to a fraction of functions comparable to C/C++. Some experiments demonstrating how Rust code is affected by the different heuristics can be found in test/assembly/stack-protector/stack-protector-heuristics-effect.rs. There is potential for better heuristics using Rust-specific safety information. For example it might be reasonable to skip stack smash protection in functions which transitively only use safe Rust code, or which uses only a subset of functions the user declares safe (such as anything under `std.*`). Such alternative heuristics could be added at a later point. LLVM also offers a "safestack" sanitizer as an alternative way to guard against stack smashing (see #26612). This could possibly also be included as a stack-protection heuristic. An alternative is to add it as a sanitizer (#39699). This is what clang does: safestack is exposed with option `-fsanitize=safe-stack`. The options are only supported by the LLVM backend, but as with other codegen options it is visible in the main codegen option help menu. The heuristic names "basic", "strong", and "all" are hopefully sufficiently generic to be usable in other backends as well. Reviewed-by: Nikita Popov <nikic@php.net> Extra commits during review: - [address-review] make the stack-protector option unstable - [address-review] reduce detail level of stack-protector option help text - [address-review] correct grammar in comment - [address-review] use compiler flag to avoid merging functions in test - [address-review] specify min LLVM version in fortanix stack-protector test Only for Fortanix test, since this target specifically requests the `--x86-experimental-lvi-inline-asm-hardening` flag. - [address-review] specify required LLVM components in stack-protector tests - move stack protector option enum closer to other similar option enums - rustc_interface/tests: sort debug option list in tracking hash test - add an explicit `none` stack-protector option Revert "set LLVM requirements for all stack protector support test revisions" This reverts commit a49b74f92a4e7d701d6f6cf63d207a8aff2e0f68.
2021-11-19Make `LLVMRustGetOrInsertGlobal` always return a `GlobalVariable`Josh Stone-1/+11
`Module::getOrInsertGlobal` returns a `Constant*`, which is a super class of `GlobalVariable`, but if the given type doesn't match an existing declaration, it returns a bitcast of that global instead. This causes UB when we pass that to `LLVMGetVisibility` which unconditionally casts the opaque argument to a `GlobalValue*`. Instead, we can do our own get-or-insert without worrying whether existing types match exactly. It's not relevant when we're just trying to get/set the linkage and visibility, and if types are needed we can bitcast or error nicely from `rustc_codegen_llvm` instead.
2021-11-16Recognize machine optimization remarksTomasz Miąsko-0/+3
2021-11-16Emit LLVM optimization remarks when enabled with `-Cremark`Tomasz Miąsko-0/+90
The default diagnostic handler considers all remarks to be disabled by default unless configured otherwise through LLVM internal flags: `-pass-remarks`, `-pass-remarks-missed`, and `-pass-remarks-analysis`. This behaviour makes `-Cremark` ineffective on its own. Fix this by configuring a custom diagnostic handler that enables optimization remarks based on the value of `-Cremark` option. With `-Cremark=all` enabling all remarks.
2021-10-22Update the minimum external LLVM to 12Josh Stone-24/+0
2021-10-22Update the minimum external LLVM to 11Josh Stone-14/+0
2021-10-07Rollup merge of #89025 - ricobbe:raw-dylib-link-ordinal, r=michaelwoeristerJubilee-4/+7
Implement `#[link_ordinal(n)]` Allows the use of `#[link_ordinal(n)]` with `#[link(kind = "raw-dylib")]`, allowing Rust to link against DLLs that export symbols by ordinal rather than by name. As long as the ordinal matches, the name of the function in Rust is not required to match the name of the corresponding function in the exporting DLL. Part of #58713.
2021-10-05RustWrapper: adapt for LLVM API change of fatal_error_handler_tKrasimir Georgiev-0/+4
No functional changes intended. The LLVM commit https://github.com/llvm/llvm-project/commit/e463b69736da8b0a950ecd937cf990401bdfcdeb changed an argument of fatal_error_handler_t from std::string to char*. This adapts RustWrapper accordingly.
2021-09-20Implement #[link_ordinal] attribute in the context of #[link(kind = ↵Richard Cobbe-4/+7
"raw-dylib")].
2021-09-08RustWrapper: remove some uses of AttrBuilderAugie Fackler-19/+6
Turns out we can also use Attribute::get*() methods here, and avoid the AttrBuilder and an extra helper method here.
2021-09-07RustWrapper: just use the *AtIndex funcs directlyAugie Fackler-36/+3
Otherwise we're kind of reimplementing the inverse of the well-named methods, and that's not a direction we want to go.
2021-09-07RustWrapper: avoid deleted unclear attribute methodsAugie Fackler-18/+70
These were deleted in https://reviews.llvm.org/D108614, and in C++ I definitely see the argument for their removal. I didn't try and propagate the changes up into higher layers of rustc in this change because my initial goal was to get rustc working against LLVM HEAD promptly, but I'm happy to follow up with some refactoring to make the API on the Rust side match the LLVM API more directly (though the way the enum works in Rust makes the API less scary IMO). r? @nagisa cc @nikic
2021-08-26RustWrapper: adapt to LLVM change 0f45c16f2caaAugie Fackler-14/+9
The above-mentioned commit (part of the LLVM 14 development cycle) removes a method that rustc uses somewhat extensively. We mostly switch to lower-level methods that exist in all versions of LLVM we use, so no new ifdef logic is required in most cases.
2021-08-16Handle SrcMgr diagnosticsNikita Popov-3/+17
This is how InlineAsm diagnostics with source information are reported now. Previously a separate InlineAsm diagnostic handler was used.