Skip to content

Custom target: data-layout changed #31367

Closed

Description

@phil-opp

I use a custom target file to build code for ARM. I copied the file from somewhere and it worked without problems. But now, after an update to the latest nightly, the build fails with a strange LLVM error:

rustc:
/buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/llvm/lib/CodeGen/MachineFunction.cpp:108:
llvm::MachineFunction::MachineFunction(const llvm::Function*, const
llvm::TargetMachine&, unsigned int, llvm::MachineModuleInfo&): Assertion
`TM.isCompatibleDataLayout(getDataLayout()) && "Can't create a
MachineFunction using a Module with a " "Target-incompatible DataLayout
attached\n"' failed.

I don't understand any of this but it seems like the data-layout is suddently “Target-incompatible”. My target json looks like this:

{
    "arch": "arm",
    "cpu": "cortex-m4",
    "data-layout": "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i64:64-v128:64:128-a:0:32-n32-S64",
    "disable-redzone": true,
    "executables": true,
    "llvm-target": "thumbv7em-none-eabihf",
    "morestack": false,
    "os": "none",
    "relocation-model": "static",
    "target-endian": "little",
    "target-pointer-width": "32",
    "no-compiler-rt": true,
    "pre-link-args": [
        "-mcpu=cortex-m7", "-mthumb",
        "-Tlayout.ld"
    ],
    "post-link-args": [
        "-lm", "-lgcc", "-lnosys"
    ]
}

Removing the data-layout line fixes it and everything works again. So I think this issue is already solved :). I opened it anyway as I wasn't sure if this was intended (and maybe it helps someone).

If someone wants to investigate: The travis builds for nightly libcore started failing on rustc 1.8.0-nightly (9a07087bc 2016-01-30).

Activity

  1. alexcrichton commented on Feb 2, 2016

    @alexcrichton
    Member

    This might be related to how LLVM was upgraded recently?

  2. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    on Feb 2, 2016
  3. dpc commented on Feb 3, 2016

    @dpc
    Contributor

    This also happend to me on travis where LLVM is in version 3.4. https://travis-ci.org/dpc/titanos/jobs/106659993 when compiling for ARMv8 aarch64 target. It works (well, fails but in unrelated way) on my local machine where llvm is in version 3.7.

    Removing data-layout also helped. Is it OK to skip this line?

  4. tari commented on Feb 4, 2016

    @tari
    Contributor

    I'm seeing this issue as well, with JSON targets for x86_64 that use a datalayout copied from rustc's x86_64-unknown-linux-gnu target. The LLVM assert does not trigger on the same code when using the built-in linux target.

    Trying some nightlies, I still see this crash with the build from January 30 (303892e) and all subsequent ones. Testing further back will require downgrading the copy of libcore I'm building.


    Removing datalayout from the target specification prevents the failure, but it's not clear to me what the implications of that are; either rustc or LLVM is quietly choosing a default, where if it's the former everything should be fine and if it's the latter there's a chance of hard-to-debug miscompilation.

  5. added a commit that references this issue on Feb 19, 2016
  6. added a commit that references this issue on Feb 19, 2016
  7. added a commit that references this issue on Feb 22, 2016
  8. added a commit that references this issue on Mar 20, 2016
  9. thejpster commented on Apr 22, 2016

    @thejpster
    Contributor

    So I tried 1.10.0-nightly (b5ba592 2016-04-21) with my bare metal ARM project and it says it "failed to run rustc to learn about target-specific information". In verbose mode it complained that "Field data-layout in target specification is required".

    Oh, well, I'll put it back in then. But if you put it back in, you still get this issue.

    I note the O/Ps Travis build is also broken, so it's probably not just me.

  10. phil-opp commented on Apr 22, 2016

    @phil-opp
    ContributorAuthor

    The data-layout field was made manditory again in #32939. The correct values for the built-in target triples can be found in the commit diff. The field layout is specified in the LLVM documentation.

    I note the O/Ps Travis build is also broken

    Thanks for the hint :).

  11. japaric commented on Apr 22, 2016

    @japaric
    Contributor

    FWIW, I have found two ways to make llvm generate a (seemingly) correct data layout for you:

    Using clang

    $ touch empty.c
    $ clang --target=thumbv7m-none-eabi -S -emit-llvm empty.c -o empty.ll
    $ head empty.ll
    ; ModuleID = 'empty.c'
    target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
    target triple = "thumbv7m-none--eabi"
    

    Where thumbv7m-none-eabi is the llvm-target field in your target specification file. And clang should be recent-ish, probably 3.8+ or you'll get the old, longer data-layout format.

    Using an old rustc

    The other way is to use a rustc nightly from 2016-04-20 or older:

    $ cat empty.rs
    #![feature(no_core)]
    #![crate_type = "rlib"]
    #![no_core]
    
    $ rustup run nightly-2016-04-20 rustc --target stm32f100 --emit=llvm-ir empty.rs
    $ head empty.ll
    ; ModuleID = 'empty.0.rs'
    target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
    target triple = "thumbv7m-none--eabi"
    

    Where stm32f100 is your custom target, your target specification file must be in $PWD.

  12. added a commit that references this issue on Apr 22, 2016
  13. thejpster commented on Apr 23, 2016

    @thejpster
    Contributor

    I tried the clang method with clang-3.5, but the value it produced did not work. On a whim, I added a:0:32 and then it compiled OK. Haven't actually tested the binary though.

  14. pravic commented on Apr 25, 2016

    @pravic
    Contributor

    The other way is to use a rustc nightly from 2016-04-20 or older

    You can use stable as well, I guess:

    $ touch empty.rs
    $ rustup run stable-i686   rustc --emit=llvm-ir --crate-type rlib -o x86.ll empty.rs
    $ rustup run stable-x86_64 rustc --emit=llvm-ir --crate-type rlib -o x64.ll empty.rs
    
  15. Mark-Simulacrum commented on May 3, 2017

    @Mark-Simulacrum
    Member

    Is there something in particular that is left to do here? I can't quite see any "bug" in this issue, though it would be great to document what @japaric says about getting a seemingly correct data layout somewhere.

  16. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Jun 22, 2017
  17. alexcrichton commented on Aug 25, 2017

    @alexcrichton
    Member

    AFAIK there's not much left to do here, so closing.

  18. added a commit that references this issue on Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions