Commit Graph

101 Commits

Author SHA1 Message Date
Noah Lev
e0bd01167e Re-enable ConstArgKind::Path lowering by default
...and remove the `const_arg_path` feature gate as a result. It was only
a stopgap measure to fix the regression that the new lowering introduced
(which should now be fixed by this PR).
2024-09-12 13:56:01 -04:00
Boxy
b8eedfa3d2 Retroactively feature gate ConstArgKind::Path 2024-08-19 01:14:22 +01:00
bors
7120fdac7a Auto merge of #126963 - runtimeverification:smir_serde_derive, r=celinval
Add basic Serde serialization capabilities to Stable MIR

This PR adds basic Serde serialization capabilities to Stable MIR. It is intentionally minimal (just wrapping all stable MIR types with a Serde `derive`), so that any important design decisions can be discussed before going further. A simple test is included with this PR to validate that JSON can actually be emitted.

## Notes

When I wrapped the Stable MIR error types in `compiler/stable_mir/src/error.rs`, it caused test failures (though I'm not sure why) so I backed those out.

## Future Work

So, this PR will support serializing basic stable MIR, but it _does not_ support serializing interned values beneath `Ty`s and `AllocId`s, etc... My current thinking about how to handle this is as follows:

1.  Add new `visited_X` fields to the `Tables` struct for each interned category of interest.

2.  As serialization is occuring, serialize interned values as usual _and_ also record the interned value we referenced in `visited_X`.

    (Possibly) In addition, if an interned value recursively references other interned values, record those interned values as well.

3.  Teach the stable MIR `Context` how to access the `visited_X` values and expose them with wrappers in `stable_mir/src/lib.rs` to users (e.g. to serialize and/or further analyze them).

### Pros

This approach does not commit to any specific serialization format regarding interned values or other more complex cases, which avoids us locking into any behaviors that may not be desired long-term.

### Cons

The user will need to manually handle serializing interned values.

### Alternatives

1.  We can directly provide access to the underlying `Tables` maps for interned values; the disadvantage of this approach is that it either requires extra processing for users to filter out to only use the values that they need _or_ users may serialize extra values that they don't need. The advantage is that the implementation is even simpler. The other pros/cons are similar to the above.

2.  We can directly serialize interned values by expanding them in-place. The pro is that this may make some basic inputs easier to consume. However, the cons are that there will need to be special provisions for dealing with cyclical values on both the producer and consumer _and_ global values will possibly need to be de-duplicated on the consumer side.
2024-07-25 20:27:51 +00:00
Noah Lev
37ed7a4438 Add ConstArgKind::Path and make ConstArg its own HIR node
This is a very large commit since a lot needs to be changed in order to
make the tests pass. The salient changes are:

- `ConstArgKind` gets a new `Path` variant, and all const params are now
  represented using it. Non-param paths still use `ConstArgKind::Anon`
  to prevent this change from getting too large, but they will soon use
  the `Path` variant too.

- `ConstArg` gets a distinct `hir_id` field and its own variant in
  `hir::Node`. This affected many parts of the compiler that expected
  the parent of an `AnonConst` to be the containing context (e.g., an
  array repeat expression). They have been changed to check the
  "grandparent" where necessary.

- Some `ast::AnonConst`s now have their `DefId`s created in
  rustc_ast_lowering rather than `DefCollector`. This is because in some
  cases they will end up becoming a `ConstArgKind::Path` instead, which
  has no `DefId`. We have to solve this in a hacky way where we guess
  whether the `AnonConst` could end up as a path const since we can't
  know for sure until after name resolution (`N` could refer to a free
  const or a nullary struct). If it has no chance as being a const
  param, then we create a `DefId` in `DefCollector` -- otherwise we
  decide during ast_lowering. This will have to be updated once all path
  consts use `ConstArgKind::Path`.

- We explicitly use `ConstArgHasType` for array lengths, rather than
  implicitly relying on anon const type feeding -- this is due to the
  addition of `ConstArgKind::Path`.

- Some tests have their outputs changed, but the changes are for the
  most part minor (including removing duplicate or almost-duplicate
  errors). One test now ICEs, but it is for an incomplete, unstable
  feature and is now tracked at #127009.
2024-07-16 19:27:28 -07:00
Adwin White
9387b0bad9 Add method to get all attributes on a definition 2024-06-28 13:24:41 +08:00
Adwin White
84071e2662 Support fetching Attribute of items. 2024-06-28 13:24:41 +08:00
Stephen Skeirik
414ebeaeea add serde derive Serialize to stable_mir 2024-06-26 11:56:01 -04:00
Adwin White
225796a2df Add method to get FnAbi of function pointer 2024-06-21 14:50:56 +08:00
Guillaume Gomez
be1d42776d
Rollup merge of #126410 - RalfJung:smir-const-operand, r=oli-obk
smir: merge identical Constant and ConstOperand types

The first commit renames the const operand visitor functions on regular MIR to match the type name, that was forgotten in the original rename.

The second commit changes stable MIR, fixing https://github.com/rust-lang/project-stable-mir/issues/71. Previously there were two different smir types for the MIR type `ConstOperand`, one used in `Operand` and one in `VarDebugInfoContents`.

Maybe we should have done this with https://github.com/rust-lang/rust/pull/125967, so there's only a single breaking change... but I saw that PR too late.

Fixes https://github.com/rust-lang/project-stable-mir/issues/71
2024-06-15 19:51:35 +02:00
Matthias Krüger
dad74aa67c
Rollup merge of #126361 - celinval:issue-0079-intrinsic, r=oli-obk
Unify intrinsics body handling in StableMIR

rust-lang/rust#120675 introduced a new mechanism to declare intrinsics which will potentially replace the rust-intrinsic ABI.

The new mechanism introduces a placeholder body and mark the intrinsic with `#[rustc_intrinsic_must_be_overridden]`.
In practice, this means that a backend should not generate code for the placeholder, and shim the intrinsic.
The new annotation is an internal compiler implementation, and it doesn't need to be exposed to StableMIR users.

In this PR, we unify the interface for intrinsics marked with `rustc_intrinsic_must_be_overridden` and intrinsics that do not have a body.

Fixes https://github.com/rust-lang/project-stable-mir/issues/79

r? ``@oli-obk``

cc: ``@momvart``
2024-06-15 14:40:48 +02:00
Ralf Jung
dcee529e5c smir: merge identical Constant and ConstOperand types 2024-06-13 16:11:40 +02:00
Celina G. Val
6d4a825714 Add a new trait to retrieve StableMir definition Ty
We implement the trait only for definitions that should have a type.
It's possible that I missed a few definitions, but we can add them later
if needed.
2024-06-12 17:47:49 -07:00
Celina G. Val
c8c6598f17 Unify intrinsics body handling in StableMIR
rust-lang/rust#120675 introduced a new mechanism to declare intrinsics
which will potentially replace the rust-intrinsic ABI.

The new mechanism introduces a placeholder body and mark the intrinsic
with #[rustc_intrinsic_must_be_overridden].
In practice, this means that backends should not generate code for the
placeholder, and shim the intrinsic.
The new annotation is an internal compiler implementation,
and it doesn't need to be exposed to StableMIR users.

In this PR, intrinsics marked with `rustc_intrinsic_must_be_overridden`
are handled the same way as intrinsics that do not have a body.
2024-06-12 16:01:38 -07:00
Oli Scherer
9461852363 Add regression test 2024-06-07 13:22:44 +00:00
Boxy
7e08f80b34 Split smir Const into TyConst and MirConst 2024-06-04 10:14:45 +01:00
Mohammad Omidvar
6743fc7704 Add conversion from IntrinsicDef to FnDef 2024-05-23 15:35:18 +00:00
Mohammad Omidvar
56ad5453f6 Extend tests for intrinsic definitions 2024-05-20 18:12:06 +00:00
Markus Reiter
bd8e565e16
Use generic NonZero. 2024-05-08 21:37:55 +02:00
Matthias Krüger
72e8fb4268
Rollup merge of #123050 - RalfJung:panic_str, r=m-ou-se
panic_str only exists for the migration to 2021 panic macros

The only caller is `expect_failed`, which is already a cold inline(never) function, so inlining into that function should be fine. (And indeed `panic_str` was `#[inline]` anyway.)

The existence of panic_str risks someone calling it when they should call `panic` instead, and I can't see a reason why this footgun should exist.

I also extended the comment in `panic` to explain why it needs a `'static` string -- I know I've wondered about this in the past and it took me quite a while to understand.
2024-04-23 12:10:25 +02:00
Ralf Jung
132921e187 try to fix unnecessarily fragile smir test 2024-04-23 11:54:53 +02:00
Guillaume Gomez
2b4c581ef9
Rollup merge of #123659 - celinval:smir-fix-intrinsic, r=oli-obk
Add support to intrinsics fallback body

Before this fix, the call to `body()` would crash, since `has_body()` would return true, but we would try to retrieve the body of an intrinsic which is not allowed.

Instead, the `Instance::body()` function will now convert an Intrinsic into an Item before retrieving its body.

Note: I also changed how we monomorphize the instance body. Unfortunately, the call still ICE for some shims.

r? `@oli-obk`
2024-04-10 16:15:23 +02:00
Celina G. Val
1512d06be9 Add support to intrinsics fallback body
Before this fix, the call to `body()` would crash, since `has_body()`
would return true, but we would try to retrieve the body of an intrinsic
which is not allowed.

Instead, the `Instance::body()` function will now convert an Intrinsic
into an Item before retrieving its body.
2024-04-08 17:07:29 -07:00
Celina G. Val
0a4f4a3e29 Remove unimplemented!() from BinOp::ty() function
To reduce redundancy, we now internalize the BinOp instead of
duplicating the `ty()` function body.
2024-04-08 15:47:37 -07:00
Celina G. Val
a325bce3cd Normalize the result of Fields::ty_with_args
We were only instantiating before, which would leak an AliasTy.
I added a test case that reproduce the issue seen here:

https://github.com/model-checking/kani/issues/3113
2024-03-28 13:22:10 -07:00
Matthias Krüger
bd53d1eee3
Rollup merge of #122405 - celinval:smir-new-const, r=oli-obk
Add methods to create StableMIR constant

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.

One question I have is whether we should create a global allocation instead for strings.

r? ``````@oli-obk``````
2024-03-14 11:09:58 +01:00
Adrian Palacios
68fc92242f Add intrinsic_name to get plain intrinsic name 2024-03-12 22:03:23 +00:00
Celina G. Val
893a9107b9 Add a test to SMIR body transformation 2024-03-12 12:55:18 -07:00
Celina G. Val
e3ac2c68b8 Implement missing ABI structures in StableMIR 2024-03-01 11:02:05 -08:00
许杰友 Jieyou Xu (Joe)
6e48b96692
[AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00: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
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
Celina G. Val
9aace67235 Ensure internal function is safe
The internal function was unsound, it could cause UB in rare cases where
the user inadvertly stored the returned object in a location that could
outlive the TyCtxt.

In order to make it safe, we now take a type context as an argument to
the internal fn, and we ensure that interned items are lifted using the
provided context.

Thus, this change ensures that the compiler can properly enforce
that the object does not outlive the type context it was lifted to.
2024-01-19 10:00:32 +00:00
Celina G. Val
6a573cbc60 Revert changes to internal method for now
- Move fix to a separate PR
2024-01-17 19:59:57 -08:00
Celina G. Val
2564811e7b Remove tcx function and make internal fn safer
I added `tcx` argument to `internal` to force 'tcx to be the same
lifetime as TyCtxt. The only other solution I could think is to change
this function to be `unsafe`.
2024-01-16 14:35:18 -08:00
Celina G. Val
f91ccf9ace Remove tcx from SMIR run macro and accept closures
Simplify the `run` macro to avoid sometimes unnecessary dependency
on `TyCtxt`. Instead, users can use the new internal method `tcx()`.
Additionally, extend the macro to accept closures that may capture
variables.

These are non-backward compatible changes, but they only affect
internal APIs which are provided today as helper functions until we
have a stable API to start the compiler.
2024-01-16 11:17:51 -08:00
Camille GILLOT
974b727874 Bless ui-fulldeps. 2024-01-11 09:58:22 +00: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
Celina G. Val
7ab38b80eb Add ItemKind::Ctor to stable mir 2023-12-20 10:53:40 -08:00
Celina G. Val
48c7cc2090 Add a small test for the case that was crashing 2023-12-20 10:50:40 -08: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
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
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
Celina G. Val
9e15c49ae3 Add a test to evaluate type intrinsic. 2023-12-07 17:01:29 -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
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