Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miri: basic dyn* support #107728

Merged
merged 6 commits into from
Feb 21, 2023
Merged

Miri: basic dyn* support #107728

merged 6 commits into from
Feb 21, 2023

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Feb 6, 2023

As usual I am very unsure about the dynamic dispatch stuff, but it passes even the Pin<&mut dyn* Trait> test so that is something.

TBH I think it was a mistake to make dyn Trait and dyn* Trait part of the same TyKind variant. Almost everywhere in Miri this lead to the wrong default behavior, resulting in strange ICEs instead of nice "unimplemented" messages. The two types describe pretty different runtime data layout after all.

Strangely I did not need to do the equivalent of this diff in Miri. Maybe that is because the unsizing logic matches on ty::Dynamic(.., ty::Dyn) already? In unsized_info I don't think the target_dyn_kind can be DynStar, since then it wouldn't be unsized!

r? @oli-obk Cc @eholk (dyn-star) #102425

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 6, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 6, 2023

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@RalfJung
Copy link
Member Author

RalfJung commented Feb 6, 2023

Strangely I did not need to do the equivalent of #106532 (comment) in Miri. Maybe that is because the unsizing logic matches on ty::Dynamic(.., ty::Dyn) already? In unsized_info I don't think the target_dyn_kind can be DynStar, since then it wouldn't be unsized!

Indeed I confirmed if I weaken the match in cast.rs to ignore Dyn vs DynStar, I get a strange ICE. Yet another argument for why those shouldn't be conflated into a single TyKind.

@@ -193,7 +193,7 @@ fn layout_of_uncached<'tcx>(
}

ty::Dynamic(_, _, ty::DynStar) => {
let mut data = scalar_unit(Int(dl.ptr_sized_integer(), false));
let mut data = scalar_unit(Pointer(AddressSpace::DATA));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to cause issues.

Invalid InsertValueInst operands!
  %15 = insertvalue { ptr, ptr } undef, i64 %14, 0
in function _ZN3box13make_dyn_star17h0c15bb343f1925bdE
LLVM ERROR: Broken function found, compilation aborted!

Probably somewhere in the backend something still assumes an integer type here, and inserts an i64 into this { ptr, ptr }?

@@ -193,7 +193,7 @@ fn layout_of_uncached<'tcx>(
}

ty::Dynamic(_, _, ty::DynStar) => {
let mut data = scalar_unit(Int(dl.ptr_sized_integer(), false));
let mut data = scalar_unit(Pointer(AddressSpace::DATA));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to cause issues.

---- [ui] tests/ui/dyn-star/box.rs stdout ----

error: test compilation failed although it shouldn't!
status: exit status: 101
command: "/home/r/src/rust/rustc/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" "/home/r/src/rust/rustc/tests/ui/dyn-star/box.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--remap-path-prefix=/home/r/src/rust/rustc/tests/ui=fake-test-src-base" "-C" "prefer-dynamic" "-o" "/home/r/src/rust/rustc/build/x86_64-unknown-linux-gnu/test/ui/dyn-star/box/a" "-Crpath" "-Cdebuginfo=0" "-Lnative=/home/r/src/rust/rustc/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/home/r/src/rust/rustc/build/x86_64-unknown-linux-gnu/test/ui/dyn-star/box/auxiliary" "-C" "opt-level=0"
stdout: none
--- stderr -------------------------------
Invalid InsertValueInst operands!
  %15 = insertvalue { ptr, ptr } undef, i64 %14, 0
in function _ZN3box13make_dyn_star17h0c15bb343f1925bdE
LLVM ERROR: Broken function found, compilation aborted!

Probably somewhere in the backend something still assumes an integer type here, and inserts an i64 into this { ptr, ptr }? This actually casts a Box to dyn*, so the input value is a pointer -- no idea where the i64 comes from.

Copy link
Member Author

@RalfJung RalfJung Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the LLVM IR on current nightly

  %10 = ptrtoint ptr %_4.i to i64, !dbg !1309
  %11 = insertvalue { i64, ptr } undef, i64 %10, 0, !dbg !1310
  %12 = insertvalue { i64, ptr } %11, ptr @vtable.1, 1, !dbg !1310

We are generating a ptrtoint?!? That doesn't sound right at all. We shouldn't be generating such a cursed operation unless the user actually asked for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just found #102641...

We should be doing the opposite: if the input is an integer, we should cast/transmute/bit/cast/whatever it to a pointer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely just removing that pointer type hack seems to work? I would have expected errors from where we cast usize to dyn* (I think we are now inserting an i64 where a ptr should go) but strangely, there are no such errors...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, on CI it does fail. CI uses an older, pre-ptr version of LLVM.

@eholk
Copy link
Contributor

eholk commented Feb 7, 2023

TBH I think it was a mistake to make dyn Trait and dyn* Trait part of the same TyKind variant. Almost everywhere in Miri this lead to the wrong default behavior, resulting in strange ICEs instead of nice "unimplemented" messages. The two types describe pretty different runtime data layout after all.

This totally makes sense, and I'd be up for rewriting it to split them apart.

The reason I added the layout as a field within the same variant originally was that, at least in the front parts of the compiler, they generally are pretty similar. For a lot of the type system, the only difference between dyn Trait and dyn* Trait is that dyn* is Sized while dyn isn't.

But yeah, later on the layouts are quite different, and it's not just too hard to do Dynamic(..) | DynStar(..) in the parts of the compiler where they really are the same.

@RalfJung
Copy link
Member Author

RalfJung commented Feb 7, 2023

--- stderr -------------------------------
Invalid bitcast
  %2 = bitcast i64 %0 to i8*
LLVM ERROR: Broken module found, compilation aborted!
------------------------------------------

So looks like something is inserting bitcasts, and that works fine on recent LLVM but old LLVM doesn't like them?

Probably the change to using a pointer ABI should be its own PR, a prerequisite to this one. @eholk is that something you could help with?

@compiler-errors
Copy link
Member

@RalfJung I think I can do it unless @eholk already planned to -- I'll cherry-pick d64cb07d1d5a57117de2f1680f20aa14755a4f29 and fix the LLVM bugs on top of it. Seems like we just need to insert an inttoptr instead of ptrtoint when doing the usize -> *const () during codegen.

@RalfJung
Copy link
Member Author

RalfJung commented Feb 7, 2023

Seems like we just need to insert an inttoptr instead of ptrtoint when doing the usize -> *const () during codegen.

Yeah, probably. Ideally we'd insert the equivalent of ptr::invalid_mut but I don't think LLVM has a convenient way of expressing that.

There is also a bitcast coming from somewhere that we do not want... or maybe that's some automatic thing the codegen backend is doing when an immediate of one type is used in a situation that expects another type?

@eholk
Copy link
Contributor

eholk commented Feb 7, 2023

@RalfJung I think I can do it unless @eholk already planned to -- I'll cherry-pick d64cb07 and fix the LLVM bugs on top of it. Seems like we just need to insert an inttoptr instead of ptrtoint when doing the usize -> *const () during codegen.

@compiler-errors - go for it!

I was going to spend some time this afternoon splitting dyn* out from the ty::Dynamic variant to make it less easy to accidentally do the wrong thing with `dyn*.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 12, 2023
…s-ptr, r=eholk

Make `dyn*`'s value backend type a pointer

One tweak on top of Ralf's commit should fix using `usize` as a `dyn*`-coercible type, and should fix when we're using various other pointer types when LLVM opaque pointers is disabled.

r? `@eholk` but feel free to reassign
cc rust-lang#107728 (comment) `@RalfJung`
@oli-obk
Copy link
Contributor

oli-obk commented Feb 18, 2023

@rustbot blocked #107772

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2023
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Feb 19, 2023
@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2023

I blessed some tests and refactored the main dyn/* condition, lmk what you think

edit: race condition, you pushed faster lol

@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2023

ok, another try :)

@RalfJung
Copy link
Member Author

r=me on your commits, and I think this is good to go.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2023

@bors r=RalfJung,oli-obk

@bors
Copy link
Contributor

bors commented Feb 20, 2023

📌 Commit 054c76d has been approved by RalfJung,oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 20, 2023
@bors
Copy link
Contributor

bors commented Feb 21, 2023

⌛ Testing commit 054c76d with merge f715e43...

@bors
Copy link
Contributor

bors commented Feb 21, 2023

☀️ Test successful - checks-actions
Approved by: RalfJung,oli-obk
Pushing f715e43 to master...

1 similar comment
@bors
Copy link
Contributor

bors commented Feb 21, 2023

☀️ Test successful - checks-actions
Approved by: RalfJung,oli-obk
Pushing f715e43 to master...

@bors bors added merged-by-bors This PR was explicitly merged by bors. labels Feb 21, 2023
@bors bors merged commit f715e43 into rust-lang:master Feb 21, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 21, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f715e43): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.9% [1.0%, 2.4%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.8% [-1.8%, -1.8%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

@RalfJung RalfJung deleted the miri-dyn-star branch February 26, 2023 18:09
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Apr 5, 2023
…tic, r=eholk

Don't ICE when encountering `dyn*` in statics or consts

Since we have properly implemented `dyn*` support in CTFE (rust-lang#107728), let's not ICE here anymore.

Fixes rust-lang#105777

r? `@eholk`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants