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

Const eval does alignment checks #61952

Closed
oli-obk opened this issue Jun 19, 2019 · 1 comment · Fixed by #63079
Closed

Const eval does alignment checks #61952

oli-obk opened this issue Jun 19, 2019 · 1 comment · Fixed by #63079
Labels
A-const-eval Area: constant evaluation (mir interpretation) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2019

The const eval engine takes quite a few steps to ensure that it bails out of evaluation if any steps would cause unaligned reads. E.g.

#[repr(packed)]
struct Foo {
    a: i32,
}

static A: Foo = Foo { a: 42 };
static B: i32 = {
    let x: &i32 = unsafe { &A.a };
    *x
};

will fail to compile with

error[E0080]: could not evaluate static initializer
 --> src/main.rs:9:5
  |
9 |     *x
  |     ^^ tried to access memory with alignment 1, but alignment 4 is required

The question is, do we need to be this restrictive during const eval. The checks are everywhere, so we may even see perf gains from removing them, but the main gain would be that we can remove the additional code that just exists to ensure we fiddle the right alignment through the pattern-matching-on-constants logic (ConstValue::ByRef's Align field is unused for anything else).

I think there's little use in keeping this restriction in const eval, since it only exists in miri because some hardware platforms actually do UB on unaligned reads, while other platforms are simply slower on unaligned reads.

cc @RalfJung

@jonas-schievink jonas-schievink added A-const-eval Area: constant evaluation (mir interpretation) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jun 19, 2019
@RalfJung
Copy link
Member

RalfJung commented Jun 19, 2019

it only exists in miri because some hardware platforms actually do UB on unaligned reads, while other platforms are simply slower on unaligned reads.

That is not correct. It exists in Miri because it is UB in MIR to do unaligned accesses, and we optimize and generate LLVM IR under that assumption. It is wrong and can cause incorrect codegen to do unaligned accesses even on platforms like x86 where they do not trap.

But CTFE does not have to go out of its way to detect UB, IMO.

bors added a commit that referenced this issue Aug 5, 2019
CTFE: simplify ConstValue by not checking for alignment

I hope the test suite actually covers the problematic cases here?

r? @oli-obk

Fixes #61952
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants