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

[WIP] Move pointee_info_at to TyLayoutMethods. #57150

Closed

Conversation

wildarch
Copy link
Contributor

@wildarch wildarch commented Dec 27, 2018

Resolves #56166.

This moves the pointee_info_at from rustc_codegen_llvm::type_of::LayoutLlvmExt to rustc::ty::layout::TyLayoutMethods.

This pull request is far from ready to be merged, and is only meant for an intermediate review. It will be expanded to a completely resolve #56166.

r? @eddyb

The original implementation is still present at
librustc_codegen_llvm/abi.rs, should be removed later to prevent code
duplication.
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 27, 2018
@RalfJung
Copy link
Member

Uh I'm sorry don't think I can review this. I don't know this code.

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned RalfJung Dec 27, 2018
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Just a preliminary review.

Looks generally alright to me, can you start changing all the previous calls to pointee_info_at to this method and remove the old one?

src/librustc/ty/layout.rs Outdated Show resolved Hide resolved
src/librustc/ty/layout.rs Outdated Show resolved Hide resolved
src/librustc/ty/layout.rs Outdated Show resolved Hide resolved
src/librustc/ty/layout.rs Outdated Show resolved Hide resolved
src/librustc_target/abi/mod.rs Show resolved Hide resolved
oli-obk and others added 4 commits December 28, 2018 12:23
Co-Authored-By: wildarch <daandegraaf9@gmail.com>
An associated type ParamEnv has been added to TyLayoutMethods to
facilitate this.
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:1547a3e4:start=1546007169673648626,finish=1546007225209864498,duration=55536215872
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export IMAGE=x86_64-gnu-llvm-6.0
---

[00:03:44] travis_fold:start:tidy
travis_time:start:tidy
tidy check
[00:03:45] tidy error: /checkout/src/librustc/ty/layout.rs:1864: line longer than 100 chars
[00:03:46] some tidy checks failed
[00:03:46] 
[00:03:46] 
[00:03:46] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor" "--quiet"
[00:03:46] 
[00:03:46] 
[00:03:46] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:03:46] Build completed unsuccessfully in 0:00:45
[00:03:46] Build completed unsuccessfully in 0:00:45
[00:03:46] make: *** [tidy] Error 1
[00:03:46] Makefile:69: recipe for target 'tidy' failed
The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:0c21d938
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
Fri Dec 28 14:31:00 UTC 2018
---
travis_time:end:095ec6a1:start=1546007461009059277,finish=1546007461015277412,duration=6218135
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:00a0c460
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:01e3a6d6
travis_time:start:01e3a6d6
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:0260a7c8
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@wildarch
Copy link
Contributor Author

@eddyb I think the moving of pointee_info_at is now complete. I've been looking at your hints for further refactoring in #56166, which part of rustc_codegen_ssa are you referring to there?

@TimNN
Copy link
Contributor

TimNN commented Jan 29, 2019

Ping from triage @eddyb / @rust-lang/complier: This PR requires your review.

fn for_variant(
this: TyLayout<'a, Self>,
cx: &C,
variant_index: VariantIdx,
) -> TyLayout<'a, Self>;
fn field(this: TyLayout<'a, Self>, cx: &C, i: usize) -> C::TyLayout;
fn pointee_info_at(
Copy link
Member

Choose a reason for hiding this comment

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

Each method in this trait needs a "forwarding" wrapper in the impl just below, the trait should never be used otherwise, only implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that should be easy to add 👍. As far as I could tell I am never actually using the trait directly, so that should be alright. Maybe you could give me some more details on the design decision here, so that I can leave it as a comment for the TyLayoutMethods trait?

Copy link
Member

Choose a reason for hiding this comment

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

Sure. We have to implement the trait on the Ty (e.g. rustc::ty::Ty<'tcx>), but the functionality should be available as method syntax on TyLayout<Ty>.

If we make TyLayout<Ty> Deref to Ty, we can use self instead of this and have the trait directly callable.
But it already Derefs to LayoutDetails, sadly.

@eddyb
Copy link
Member

eddyb commented Jan 31, 2019

My immediate reaction is that PointeeInfo is really about information LLVM wants, but I did suggest moving it, so it's probably fine (also, it's defined in broad Rust terms, not LLVM-specific distinctions).


@oli-obk Note that we never pass ParamEnv separately in layout code! It's always in the context, e.g.:

pub struct LayoutCx<'tcx, C> {
pub tcx: C,
pub param_env: ty::ParamEnv<'tcx>,
}

or:
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
self.tcx.layout_of(self.param_env.and(ty))

So I think what is needed is to add a trait that contains is_freeze (or a way to get the ty::ParamEnv) as a bound on C here:

rust/src/librustc/ty/layout.rs

Lines 1599 to 1602 in f40aaa6

impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
where C: LayoutOf<Ty = Ty<'tcx>> + HasTyCtxt<'tcx>,
C::TyLayout: MaybeResult<TyLayout<'tcx>>
{

@bors
Copy link
Contributor

bors commented Feb 7, 2019

☔ The latest upstream changes (presumably #58254) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC-zz
Copy link

ping from triage @wildarch you need to reply to the review and make necessary changes. Also you have conflicts to attend to.

@wildarch
Copy link
Contributor Author

wildarch commented Mar 7, 2019

@Dylan-DPC I have been a bit preoccupied as of late 😅, but I should have some time this weekend to attend to this. Thanks for the heads-up! 😄

@wildarch
Copy link
Contributor Author

My immediate reaction is that PointeeInfo is really about information LLVM wants, but I did suggest moving it, so it's probably fine (also, it's defined in broad Rust terms, not LLVM-specific distinctions).

@oli-obk Note that we never pass ParamEnv separately in layout code! It's always in the context, e.g.:

rust/src/librustc/ty/layout.rs

Lines 203 to 206 in f40aaa6
pub struct LayoutCx<'tcx, C> {
pub tcx: C,
pub param_env: ty::ParamEnv<'tcx>,
}

or:

rust/src/librustc_mir/interpret/eval_context.rs

Lines 168 to 169 in f40aaa6
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
self.tcx.layout_of(self.param_env.and(ty))

So I think what is needed is to add a trait that contains is_freeze (or a way to get the ty::ParamEnv) as a bound on C here:

rust/src/librustc/ty/layout.rs

Lines 1599 to 1602 in f40aaa6
impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
where C: LayoutOf<Ty = Ty<'tcx>> + HasTyCtxt<'tcx>,
C::TyLayout: MaybeResult<TyLayout<'tcx>>
{

I feel like a trait just for is_freeze might be a little too specific, I think it would be nice to have a trait like:

pub trait HasParamEnv {
    fn param_env(&self) -> ParamEnv
}

And then put that as an additional bound on C, which I think is what you suggested with ty::ParamEnv?

Then I think I should be able to remove the ParamEnv as a parameter from the pointee_info_at function and use the available context to retrieve it. Does that sound alright to you @eddyb?

@@ -1497,6 +1499,7 @@ impl<'gcx, 'tcx, T: HasTyCtxt<'gcx>> HasTyCtxt<'gcx> for LayoutCx<'tcx, T> {
pub trait MaybeResult<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I realize now that this is actually an isomorphism with Result<T, Self::Err>.
In the case of impl<T> MaybeResult<T> for T, that Err associated type would be !, which would make both conversions total.

So the existing API could be replaced with:

  • MaybeResult::from_ok(x) -> MaybeResult::from(Ok(x))
  • x.map_same(f) -> MaybeResult::from(x.to_result().map(f))
  • x.ok() -> x.to_result().ok()

Copy link
Contributor

@saleemjaffer saleemjaffer Apr 25, 2019

Choose a reason for hiding this comment

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

@eddyb I am a bit confused here. Does this mean I have to create two methods from and to_result for MaybeResult?

And in

x.map_same(f) -> MaybeResult::from(x.to_result().map(f))

In this x should have a to_result method. But in our case TyLayout has no such method.

Copy link
Contributor

Choose a reason for hiding this comment

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

from should be a std::convert::From impl.

to_result should be a method, yes.

x.map_same(f) -> MaybeResult::from(x.to_result().map(f))
In this x should have a to_result method. But in our case TyLayout has no such method.

x is a MaybeResult, so it will have the to_result method.

Copy link
Contributor

Choose a reason for hiding this comment

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

@oli-obk I am trying to implement std::convert::From for MaybeResult like this:

impl From<TyLayout<'tcx>> for dyn MaybeResult<TyLayout<'tcx>, Item=!> {
    fn from(ty: TyLayout<'tcx>) -> dyn MaybeResult<TyLayout<'tcx>, Item=!> {
        ty
    }
}

Getting some issues:

error[E0277]: the size for values of type `(dyn ty::layout::MaybeResult<rustc_target::abi::TyLayout<'tcx, &'tcx ty::TyS<'tcx>>, Item = !> + 'static)` cannot be known at compilation time
    --> src/librustc/ty/layout.rs:1495:6
     |
1495 | impl From<TyLayout<'tcx>> for dyn MaybeResult<TyLayout<'tcx>, Item=!> {
     |      ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `std::marker::Sized` is not implemented for `(dyn ty::layout::MaybeResult<rustc_target::abi::TyLayout<'tcx, &'tcx ty::TyS<'tcx>>, Item = !> + 'static)`
     = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0038]: the trait `ty::layout::MaybeResult` cannot be made into an object
    --> src/librustc/ty/layout.rs:1495:6
     |
1495 | impl From<TyLayout<'tcx>> for dyn MaybeResult<TyLayout<'tcx>, Item=!> {
     |      ^^^^^^^^^^^^^^^^^^^^ the trait `ty::layout::MaybeResult` cannot be made into an object
     |
     = note: method `from_ok` has no receiver
     = note: method `map_same` references the `Self` type in its arguments or return type

error[E0038]: the trait `ty::layout::MaybeResult` cannot be made into an object
    --> src/librustc/ty/layout.rs:1496:5
     |
1496 |     fn from(ty: TyLayout<'tcx>) -> dyn MaybeResult<TyLayout<'tcx>, Item=!> {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ty::layout::MaybeResult` cannot be made into an object
     |
     = note: method `from_ok` has no receiver
     = note: method `map_same` references the `Self` type in its arguments or return type

error: aborting due to 3 previous errors

Copy link
Contributor

Choose a reason for hiding this comment

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

oh... that was stupid of me. Sorry about that. It should suffice to add a : From<TyLayout<'tcx>> bound on the definition of MaybeResult.

This will end up requiring that all implementors of MaybeResult also implement From, not sure how well that works with the T impl

Copy link
Contributor

@saleemjaffer saleemjaffer Apr 28, 2019

Choose a reason for hiding this comment

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

@oli-obk

I am trying something like this:

pub trait MaybeResult<'tcx, T>: From<TyLayout<'tcx>> {
    type Item;

    fn from_ok(x: T) -> Self;
    fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self;
    fn to_result(self) -> Result<T, Self::Item>;
}

impl<'tcx, T: From<TyLayout<'tcx>>> MaybeResult<'tcx, T> for T {
    type Item = !;

    fn from_ok(x: T) -> Self {
        x
    }
    fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
        f(self)
    }
    fn to_result(self) -> Result<T, !> {
        Ok(self)
    }
}

impl<'tcx, T, E> MaybeResult<'tcx, T> for Result<T, E> {
    type Item = E;

    fn from_ok(x: T) -> Self {
        Ok(x)
    }
    fn map_same<F: FnOnce(T) -> T>(self, f: F) -> Self {
        self.map(f)
    }
    fn to_result(self) -> Result<T, E> {
        self
    }
}

This is the fallout:

error[E0277]: the trait bound `std::result::Result<T, E>: std::convert::From<rustc_target::abi::TyLayout<'tcx, &'tcx ty::TyS<'tcx>>>` is not satisfied                      
    --> src/librustc/ty/layout.rs:1517:18
     |
1517 | impl<'tcx, T, E> MaybeResult<'tcx, T> for Result<T, E> {
     |                  ^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<rustc_target::abi::TyLayout<'tcx, &'tcx ty::TyS<'tcx>>>` is not implemented for `std::result::Result<T, E>`

error: aborting due to previous error

Should we implement From for Result? Does not seem right.

Copy link
Member

@eddyb eddyb Apr 30, 2019

Choose a reason for hiding this comment

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

Sorry for not giving a full signature, I think I meant this:

pub trait MaybeResult<T> {
    type Err;

    fn from(r: Result<T, Self::Err>) -> Self;
    fn to_result(self) -> Result<T, Self::Err>;
}

We can't use the From trait because T doesn't implement From<Result<T, !>> (nor does Result<T, !> implement Into<T>).

@eddyb
Copy link
Member

eddyb commented Mar 13, 2019

@wildarch Sure, either way should work fine. Note that is_freeze could be added to TyLayoutMethods, which might be the nicest way to make this work.

@TimNN TimNN added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 2019
@Centril
Copy link
Contributor

Centril commented Mar 30, 2019

Ping from triage @wildarch, any progress?

@wildarch
Copy link
Contributor Author

wildarch commented Apr 2, 2019

@Centril I have been really busy lately, so I have not had much time to look at this. I will try to find more time to continue the PR, but honestly it might be better if someone else would take over the responsibility of finishing this. Sorry to keep you waiting 😢

@saleemjaffer
Copy link
Contributor

@oli-obk @eddyb I can take this up!

bors added a commit that referenced this pull request May 5, 2019
…ng, r=eddyb

Move pointee_info_at from rustc_codegen_llvm to rustc_target.

Makes progress towards #56166.

This is a continuation of #57150.

@oli-obk Should I close the older PR?
@Centril
Copy link
Contributor

Centril commented May 18, 2019

I believe #60237 implemented this, so we can close the PR. Please reopen if I am wrong.

@Centril Centril closed this May 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

miri function argument passing should use FnAbi
10 participants