Commit graph

211 commits

Author SHA1 Message Date
Matthias Krüger 8d78f8ee0a
Rollup merge of #122203 - adpaco-aws:smir-intrinsic-name, r=celinval
Add `intrinsic_name` to get plain intrinsic name

Add an `intrinsic_name` API to retrieve the plain intrinsic name. The plain name does not include type arguments (as `trimmed_name` does), which is more convenient to match with intrinsic symbols.
2024-03-13 06:41:21 +01:00
Adrian Palacios 68fc92242f Add intrinsic_name to get plain intrinsic name 2024-03-12 22:03:23 +00:00
Celina G. Val c076509d8a Add methods to create constants
I've been experimenting with transforming the StableMIR to instrument
the code with potential UB checks. The modified body will only
be used by our analysis tool, however, constants in StableMIR must be
backed by rustc constants. Thus, I'm adding a few functions to build
constants, such as building string and other primitives.
2024-03-12 11:17:22 -07:00
Ben Kimock 5a93a59fd5 Distinguish between library and lang UB in assert_unsafe_precondition 2024-03-08 18:53:58 -05:00
Celina G. Val 0567162933 Add support to new float types 2024-03-01 11:16:35 -08:00
Celina G. Val e3ac2c68b8 Implement missing ABI structures in StableMIR 2024-03-01 11:02:05 -08:00
Ralf Jung cc3df0af7b remove platform-intrinsics ABI; make SIMD intrinsics be regular intrinsics 2024-02-25 08:14:52 +01:00
Pavel Grigorenko 613cb3262d
compiler: use addr_of! 2024-02-24 18:53:48 +03:00
Nicholas Nethercote e72e7e9ae3 Merge CompilerError::CompilationFailed and CompilerError::ICE.
`CompilerError` has `CompilationFailed` and `ICE` variants, which seems
reasonable at first. But the way it identifies them is flawed:
- If compilation errors out, i.e. `RunCompiler::run` returns an `Err`,
  it uses `CompilationFailed`, which is reasonable.
- If compilation panics with `FatalError`, it catches the panic and uses
  `ICE`. This is sometimes right, because ICEs do cause `FatalError`
  panics, but sometimes wrong, because certain compiler errors also
  cause `FatalError` panics. (The compiler/rustdoc/clippy/whatever just
  catches the `FatalError` with `catch_with_exit_code` in `main`.)

In other words, certain non-ICE compilation failures get miscategorized
as ICEs. It's not possible to reliably distinguish the two cases, so
this commit merges them. It also renames the combined variant as just
`Failed`, to better match the existing `Interrupted` and `Skipped`
variants.

Here is an example of a non-ICE failure that causes a `FatalError`
panic, from `tests/ui/recursion_limit/issue-105700.rs`:
```
 #![recursion_limit="4"]
 #![invalid_attribute]
 #![invalid_attribute]
 #![invalid_attribute]
 #![invalid_attribute]
 #![invalid_attribute]
 //~^ERROR recursion limit reached while expanding

 fn main() {{}}
```
2024-02-17 09:40:44 +11:00
Matthias Krüger 71466ca804
Rollup merge of #120982 - momvart:smir-61-foreign_kind, r=oli-obk
Add APIs for fetching foreign items

Closes https://github.com/rust-lang/project-stable-mir/issues/61
2024-02-15 09:20:18 +01:00
Mohammad Omidvar 2e691a5c12 Rewrite foreign item kind query using DefKind 2024-02-14 17:38:36 +00:00
Mohammad Omidvar 213748749e Add APIs for fetching foreign items including foreign modules, their ABIs, and their items 2024-02-12 19:44:35 +00:00
Shoyu Vanilla 3856df059e Dejargnonize subst 2024-02-12 15:46:35 +09:00
Ben Kimock 8836ac5758 Add a new debug_assertions instrinsic (compiler)
And in clippy
2024-02-08 11:49:08 -05:00
clubby789 f6b21e90d1 Remove the abi_amdgpu_kernel feature 2024-01-30 15:46:40 +00:00
clubby789 fd29f74ff8 Remove unused features 2024-01-25 14:01:33 +00:00
Oli Scherer 225f0b9fef Make the remaining "private" fields actually private 2024-01-19 16:37:50 +00:00
Guillaume Gomez 4d12817716
Rollup merge of #119877 - celinval:smir-visit-projection, r=oli-obk
Add more information to `visit_projection_elem`

Without the starting place, it's hard to retrieve any useful information from visiting a projection.

Note: I still need to add a test.
2024-01-12 15:16:57 +01:00
Celina G. Val efab0dcb25 Add more information to visit_projection_elem
Without the starting place, it's hard to retrieve any useful information
from visiting a projection.
2024-01-11 17:15:21 -08:00
Celina G. Val af3c2c9f6d Fix all_trait* methods to return all trait available
Also provide a mechanism to retrieve traits and implementations for a
given crate.
2024-01-09 15:45:03 -08:00
Michael Goulet 15ccf2e7bd Restore movability to SMIR 2023-12-28 16:35:01 +00:00
Michael Goulet fcb42b42d6 Remove movability from TyKind::Coroutine 2023-12-28 16:35:01 +00:00
Michael Goulet 3320c09eab Only regular coroutines have movability 2023-12-25 21:13:41 +00:00
Michael Goulet 004450506e Split coroutine desugaring kind from source 2023-12-22 23:58:29 +00:00
Matthias Krüger f6a04f693b
Rollup merge of #119141 - celinval:smir-instance-args, r=compiler-errors
Add method to get instance instantiation arguments

Add a method to get the instance instantiation arguments, and include that information in the instance debug.
2023-12-20 21:18:59 +01:00
Celina G. Val e0a4693294 Add method to get instance instantiation arguments 2023-12-20 11:07:06 -08:00
Celina G. Val 7ab38b80eb Add ItemKind::Ctor to stable mir 2023-12-20 10:53:40 -08:00
Matthias Krüger 8b3914ce48
Rollup merge of #119094 - celinval:smir-layout, r=compiler-errors
Add function ABI and type layout to StableMIR

This change introduces a new module to StableMIR named `abi` with information from `rustc_target::abi` and `rustc_abi`, that allow users to retrieve more low level information required to perform bit-precise analysis.

The layout of a type can be retrieved via `Ty::layout`, and the instance ABI can be retrieved via `Instance::fn_abi()`.

To properly handle errors while retrieve layout information, we had to implement a few layout related traits.

r? ```@compiler-errors```
2023-12-20 09:46:12 +01:00
Celina G. Val 76b3e6de55 Fix c_variadic flag and add opaque info to PassMode
We should expand the information in PassMode later.
2023-12-19 11:04:34 -08:00
Celina G. Val 36bb79fc5e Add the function body span to StableMIR 2023-12-18 17:13:49 -08:00
Celina G. Val 1a83c5b55b Add function ABI and type layout to StableMIR
This change introduces a new module to StableMIR named `abi` with
information from `rustc_target::abi` and `rustc_abi`, that allow users
to retrieve more low level information required to perform
bit-precise analysis.

The layout of a type can be retrieved via `Ty::layout`, and the instance
ABI can be retrieved via `Instance::fn_abi()`.

To properly handle errors while retrieve layout information, we had
to implement a few layout related traits.
2023-12-18 19:58:48 +00:00
Celina G. Val 86451badf1 Add a method to check if type is a CStr 2023-12-15 13:18:41 -08:00
Ali MJ Al-Nasrawy 578758ae04
Rollup merge of #118927 - celinval:smir-missing-info, r=compiler-errors
Erase late bound regions from `Instance::fn_sig()` and add a few more details to StableMIR APIs

The Instance `fn_sig()` still included a late bound regions which needed a new compiler function in order to be erased. I've also bundled the following small fixes in this PR, let me know if you want me to isolate any of them.

  - Add missing `CoroutineKind::AsyncGen`.
  - Add optional spread argument to function body which is needed to properly analyze compiler shims.
  - Add a utility method to iterate over all locals together with their declaration.
  - Add a method to get the description of `AssertMessage`*.

* For the last one, we could consider eventually calling the internal `AssertKind::description()` to avoid code duplication. However, we still don't have ways to convert `AssertMessage`, `Operand`, `Place` and others, in order to use that. The other downside of using the internal method is that it will panic for some of the variants.

r ? `@ouz-a`
2023-12-15 10:58:23 +03:00
Celina G. Val 600438679d Revert signature change for AssertMessage description 2023-12-14 22:04:14 -08:00
Celina G. Val c0f1207a12 Address PR comments
- Remove `fn_sig()` from Instance.
- Change return value of `AssertMessage::description` to `Cow<>`.
- Add assert to instance `ty()`.
- Generalize uint / int type creation.
2023-12-14 17:56:33 -08:00
ouz-a 82ee18c4ea add stable_mir output test 2023-12-14 18:10:38 +03:00
Celina G. Val a66cac92cc Add spread arg and missing CoroutineKind 2023-12-13 16:36:15 -08:00
Celina G. Val c3a2302fb0 Erase late bound regions from instance fn_sig()
Late bound regions were still part of the signature.
2023-12-13 16:36:14 -08:00
Matthias Krüger d67e80f12d
Rollup merge of #118846 - celinval:smir-ty-methods, r=compiler-errors
Fix BinOp `ty()` assertion and `fn_sig()` for closures

`BinOp::ty()` was asserting that the argument types were primitives. However, the primitive check doesn't include pointers, which can be used in a `BinaryOperation`. Thus extend the arguments to include them.

Since I had to add methods to check for pointers in TyKind, I just went ahead and added a bunch more utility checks that can be handy for our users and fixed the `fn_sig()` method to also include closures.

`@compiler-errors` just wanted to confirm that today no `BinaryOperation` accept SIMD types. Is that correct?

r? `@compiler-errors`
2023-12-12 06:52:51 +01:00
Celina G. Val 638b08ebdf Remove scalar fn and tighten the BiOp Ty assertions 2023-12-11 19:02:54 -08:00
Celina G. Val 3b97b5130a Fix doc comment 2023-12-11 16:37:33 -08:00
Celina G. Val 2f790af726 Fix BinOp ty assertion and fn_sig for closures
Also added a few more util methods to TyKind to check for specific types.
2023-12-11 16:06:48 -08:00
Matthias Krüger 0564de10b1 remove some redundant clones 2023-12-11 21:54:36 +01:00
surechen 40ae34194c remove redundant imports
detects redundant imports that can be eliminated.

for #117772 :

In order to facilitate review and modification, split the checking code and
removing redundant imports code into two PR.
2023-12-10 10:56:22 +08:00
Celina G. Val 0a0e7e6c0d Add tests to allocation methods and fix is_null() 2023-12-07 17:01:29 -08:00
Celina G. Val 9cb6463af7 Fix conversion to StaticDef and add test 2023-12-07 17:01:29 -08:00
Celina G. Val 4c9e842a09 Add instance evaluation and methods to read alloc
The instance evaluation is needed to handle intrinsics such as
`type_id` and `type_name`.

Since we now use Allocation to represent all evaluated constants,
provide a few methods to help process the data inside an allocation.
2023-12-07 17:01:29 -08:00
Matthias Krüger 20ed80523a
Rollup merge of #118688 - celinval:smir-rvalue-ty, r=compiler-errors
Add method to get type of an Rvalue in StableMIR

Provide a method to StableMIR users to retrieve the type of an Rvalue operation. There were two possible implementation:

1. Create the logic inside stable_mir to process the type according to the Rvalue semantics, which duplicates the logic of `rustc_middle::mir::Rvalue::ty()`.
2. Implement the Rvalue translation from StableMIR back to internal representation, invoke the `rustc_middle::mir::Rvalue::ty()`, and translate the return value to StableMIR.

I chose the first one for now since the duplication was fairly small, and the option 2 would require way more work to translate everything back to rustc internal representation. If we eventually add those translations, we could easily swap to the option 2.

```@compiler-errors``` / ```@ouz-a``` Please let me know if you have any strong opinion here.

r? ```@compiler-errors```
2023-12-07 21:38:08 +01:00
Celina G. Val 4616b9fd1d Add sanity check to BinOp::ty() 2023-12-06 22:15:36 -08:00
Celina G. Val 77d7e44573
Update compiler/stable_mir/src/mir/body.rs
Co-authored-by: Michael Goulet <michael@errs.io>
2023-12-06 17:48:38 -08:00
Celina G. Val e16ebdbb06 Simplify StaticDef to Instance conversion 2023-12-06 15:08:08 -08:00
Celina G. Val 281b65aad9 Add method to get type of an Rvalue in StableMIR 2023-12-06 14:26:52 -08:00
Matthias Krüger 3c1357ca6b
Rollup merge of #118681 - celinval:fix-foreign-item, r=ouz-a
Fix is_foreign_item for StableMIR instance

Change the implementation of `Instance::is_foreign_item` to directly query the compiler for the instance `def_id` instead of incorrectly relying on the conversion to `CrateItem`. I also added a method to check if the instance has body, since the function already existed and it just wasn't exposed via public APIs. This makes it much cheaper for the user to check if the instance has body.

## Background:

- In pull https://github.com/rust-lang/rust/pull/118524, I fixed the conversion from Instance to CrateItem to avoid the conversion if the instance didn't have a body available. This broke the `is_foreign_item`.

r? `@ouz-a`
2023-12-06 21:52:33 +01:00
Celina G. Val 4a75d1893e Also add an API to check if an instance has body
This is much cheaper than building a body just for the purpose of
checking if the body exists.
2023-12-06 11:02:13 -08:00
Celina G. Val 1bcd162465 Fix is_foreign_item for StableMIR instance
Change the implementation of `Instance::is_foreign_item` to directly
query the compiler for the instance `def_id` instead of incorrectly
relying on the conversion to `CrateItem`.

Background:

- In pull https://github.com/rust-lang/rust/pull/118524, I fixed the
  conversion from Instance to CrateItem to avoid the conversion if the
  instance didn't have a body available. This broke the `is_foreign_item`.
2023-12-06 10:48:18 -08:00
Celina G. Val 326fea0fb8 Change ty_with_args to return Ty instead of Result
Although, we would like to avoid crashes whenever
possible, and that's why I wanted to make this API fallible. It's
looking pretty hard to do proper validation.

I think many of our APIs will unfortunately depend on the user doing
the correct thing since at the MIR level we are working on,
we expect types to have been checked already.
2023-12-05 12:03:44 -08:00
Celina G. Val 1720b108f7 Add FieldDef to StableMIR and methods to get type 2023-12-04 20:08:25 -08:00
Celina G. Val e19c7cd159 Finish implementing RustcInternal for TyKind
This will allow us to provide methods to create `Ty` inside the stable
MIR, which can be helpful while handling pointers and other stuff.
2023-12-04 11:05:29 -08:00
Celina G. Val efaf4258ba Add Variant and a few more APIs to stable_mir 2023-12-04 11:03:52 -08:00
Celina G. Val c997c6d822 Add more information to stable Instance
- Retrieve `FnSig`.
  - Implement CrateDef for InstanceDef.
  - Add VTable index for Virtual instances.
2023-12-01 16:22:06 -08:00
Celina G. Val 9d2c92377d Fix SwitchTarget pretty print
We currently rely on the order of successors to be conditional branches
first, followed by the otherwise target.
2023-11-30 11:45:34 -08:00
Celina G. Val 3e0b2fac5d Change SwitchTarget representation
The new structure encodes its invariant, which reduces the likelihood
of having an inconsistent representation. It is also more intuitive and
user friendly.

I encapsulated the structure for now in case we decide to change it back.
2023-11-30 11:45:34 -08:00
Michael Goulet 7aa513b861
Rollup merge of #118172 - ouz-a:improve_emit_stable1, r=celinval
Add `pretty_terminator` to pretty stable-mir

~Because we don't have successors in `stable_mir` this is somewhat lacking but it's better than nothing~, also fixed bug(?) with `Opaque` which printed extra `"` when we try to print opaqued `String`.

**Edit**: Added successors so this covers Terminators as a whole.
r? `@celinval`
2023-11-27 19:06:47 -05:00
ouz-a 15f9bab7ba add otherwise into targets 2023-11-27 21:26:11 +03:00
ouz-a de2779077a add successors and their formatter 2023-11-26 11:23:28 +03:00
ouz-a 879c7f91ab add pretty_terminator 2023-11-25 22:28:41 +03:00
Michael Goulet 079a2e865f is_{some,ok}_and 2023-11-25 18:47:16 +00:00
León Orell Valerian Liehr 20d243e854
Rollup merge of #118274 - celinval:smir-fix-pretty, r=ouz-a
Fix smir's `Ty::Ref` pretty printing

Add `&` or `&mut` to reference when generating a string for `TyKind::Ref`.

r? `@ouz-a`
2023-11-25 10:21:09 +01:00
Celina G. Val 60817e6863 Fix smir's Ty::Ref pretty printing 2023-11-24 18:43:35 -08:00
Celina G. Val f8c2478b6b Reorder imports and remove re-export 2023-11-24 15:09:26 -08:00
Celina G. Val d7c7236845 Move the compiler interface defs to its own module
Separate items that are exposed in the `stable_mir` crate to be used
by the compiler from items that we expect to be used by tool developers.
2023-11-24 13:49:31 -08:00
Michael Goulet 8294352b2d
Rollup merge of #118215 - celinval:smir-def-paths, r=ouz-a
Add common trait for crate definitions

In stable mir, we specialize DefId, however some functionality is the same for every definition, such as def paths, and getting their crate. Use a trait to implement those.
2023-11-24 07:29:12 -08:00
Celina G. Val b6e977243f Improve documentation and fix the fixme comment 2023-11-23 12:29:20 -08:00
bors 1e9dda77b5 Auto merge of #118120 - compiler-errors:closure-kind, r=lcnr
Remove `PredicateKind::ClosureKind`

We don't need the `ClosureKind` predicate kind -- instead, `Fn`-family trait goals are left as ambiguous, and we only need to make progress on `FnOnce` projection goals for inference purposes.

This is similar to how we do confirmation of `Fn`-family trait and projection goals in the new trait solver, which also doesn't use the `ClosureKind` predicate.

Some hacky logic is added in the second commit so that we can keep the error messages the same.
2023-11-22 21:09:01 +00:00
Celina G. Val d5afea51dc Add CrateDef trait and methods to get def names 2023-11-22 07:32:37 -08:00
Celina G. Val c07a6d5c9a Add allocation test and a bit more documentation 2023-11-21 19:16:58 -08:00
Celina G. Val 5b3cf6610b Add support to get virtual table allocation 2023-11-21 19:16:58 -08:00
Celina G. Val fa5ff859e6 Add support to global allocation to stable-mir 2023-11-21 19:16:53 -08:00
bors ed10a53025 Auto merge of #118152 - matthiaskrgr:rollup-bqcck4w, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #117972 (Add VarDebugInfo to Stable MIR)
 - #118109 (rustdoc-search: simplify `checkPath` and `sortResults`)
 - #118110 (Document `DefiningAnchor` a bit more)
 - #118112 (Don't ICE when ambiguity is found when selecting `Index` implementation in typeck)
 - #118135 (Remove quotation from filename in stable_mir)

Failed merges:

 - #118012 (Add support for global allocation in smir)

r? `@ghost`
`@rustbot` modify labels: rollup
2023-11-22 00:30:56 +00:00
Matthias Krüger 914891fc58
Rollup merge of #118135 - ouz-a:fix_stable_span, r=celinval
Remove quotation from filename in stable_mir

Previously we had quotation marks in filenames which is obviously wrong this fixes that.

r? ```@celinval```
2023-11-21 23:46:20 +01:00
Nilstrieb 21a870515b Fix clippy::needless_borrow in the compiler
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`.

Then I had to remove a few unnecessary parens and muts that were exposed
now.
2023-11-21 20:13:40 +01:00
Michael Goulet 93298ee0dd Remove ClosureKind predicate kind 2023-11-21 18:35:45 +00:00
ouz-a 0b25415559 remove quotation from filename 2023-11-21 16:56:19 +03:00
ouz-a d0dd19a6c9 de-structure variable and add stables 2023-11-21 15:38:45 +03:00
ouz-a 018b85986d Add VarDebugInfo to Stable MIR 2023-11-21 15:38:45 +03:00
Celina G. Val d94df62398 Improve code per PR comments
- Simplified DefTy::internal
 - Break down place::ty() method
2023-11-20 12:46:14 -08:00
Celina G. Val d3fa6a0e35 Add place.ty() and Ty build from a kind to smir 2023-11-20 12:43:39 -08:00
ouz-a 92657f163a use new apis and add new function 2023-11-17 23:28:17 +03:00
Oğuz Ağcayazı 71c990470a move pretty into stable_mir 2023-11-17 13:28:43 +03:00
Oğuz Ağcayazı ebd9c145f6 better formatting for statements 2023-11-17 13:28:07 +03:00
Oğuz Ağcayazı ae179a04b6 emit basic smir 2023-11-17 13:28:07 +03:00
Celina G. Val 8e81fc0087 Fix has_body() and change resolve_drop_in_place() sig
Fixed the `has_body()` function operator. Before that, this function was
returning false for all shims.

Change resolve_drop_in_place() to also return an instance for empty
shims, since they may still be required for vtable construction.
2023-11-16 12:04:25 -08:00
Celina G. Val 08036a8005 Address PR comments 2023-11-16 12:01:18 -08:00
Celina G. Val 6515ac9d3f Add more APIs and fix Instance::body
Add more APIs to retrieve information about types, and add more instance
resolution options.

Make `Instance::body()` return an Option<Body>, since not every instance
might have an available body. For example, foreign instances, virtual
instances, dependencies.
2023-11-16 12:01:10 -08:00
Celina G. Val 3f87dac9a2 Fix bug on MIRVisitor
We were not iterating over all local variables due to a typo.
2023-11-16 11:05:36 -08:00
Celina G. Val e70839ac84 Add more SMIR internal impl and callback return value
In cases like Kani, we will invoke the rustc_internal run command
directly for now. It would be handly to be able to have a callback
that can return a value.

We also need extra methods to convert stable constructs into internal
ones, so we can break down the transition into finer grain commits.
2023-11-16 11:05:36 -08:00
bors 1500db7314 Auto merge of #117908 - lcnr:region-kind-rename, r=BoxyUwU
finish `RegionKind` renaming

second step of https://github.com/rust-lang/types-team/issues/95

continues the work from #117876. While working on this and I encountered a bunch of further cleanup which I'll either open a tracking issue for or will do in a separate PR:
- rewrite the `RegionKind` docs, they still talk about `ReEmpty` and are generally out of date
- rename `DescriptionCtx` to `DescriptionCtxt`
- what is `CheckRegions::Bound`?
- `collect_late_bound_regions` et al
- `erase_late_bound_regions` -> `instantiate_bound_regions_with_erased`?
- `EraseEarlyRegions` visitor should be removed, feels duplicate

r? `@BoxyUwU`
2023-11-15 12:55:42 +00:00
bors 698fcc8219 Auto merge of #117517 - klinvill:smir-projections, r=ouz-a
Add richer structure for Stable MIR Projections

Resolves https://github.com/rust-lang/project-stable-mir/issues/49.

Projections in Stable MIR are currently just strings. This PR replaces that representation with a richer structure, namely projections become vectors of `ProjectionElem`s, just as in MIR. The `ProjectionElem` enum is heavily based off of the MIR `ProjectionElem`.

This PR is a draft since there are several outstanding issues to resolve, including:

- How should `UserTypeProjection`s be represented in Stable MIR? In MIR, the projections are just a vector of `ProjectionElem<(),()>`, meaning `ProjectionElem`s that don't have Local or Type arguments (for `Index`, `Field`, etc. objects). Should `UserTypeProjection`s be represented this way in Stable MIR as well? Or is there a more user-friendly representation that wouldn't drag along all the `ProjectionElem` variants that presumably can't appear?
- What is the expected behavior of a `Place`'s `ty` function? Should it resolve down the chain of projections so that something like `*_1.f` would return the type referenced by field `f`?
- Tests should be added for `UserTypeProjection`
2023-11-15 06:05:54 +00:00
Kirby Linvill c036a10ed5
Make UserTypeProjection projections Opaque
Also shifts comments explaining why Stable MIR drops an optional variant
name field, for `Downcast` projection elements, to the `Place::stable`
function.
2023-11-14 19:19:35 -07:00
lcnr 15f21562e6 finish RegionKind rename
- `ReFree` -> `ReLateParam`
- `ReEarlyBound` -> `ReEarlyParam`
2023-11-14 13:13:27 +00:00