Skip to content

CPU features are not passed to clang when assembling #10411

Closed

Description

@silversquirl

Zig Version

0.10.0-dev.40+303bad998

Steps to Reproduce

Create an ARM assembly file named test.s with the following contents:

mov r1, #0

Compile that file to an object using zig build-obj --verbose-cc -target arm-freestanding -mcpu cortex_m0plus+thumb2 test.s

Expected Behavior

Zig passes the +thumb2 option to clang, which correctly assembles the thumb2 instruction (though this particular CPU feature won't work until llvm/llvm-project#52878 is fixed).

Actual Behavior

The +thumb2 feature is not passed to zig clang:

$ zig build-obj --verbose-cc -target arm-freestanding -mcpu cortex_m0plus+thumb2 test.s
/home/silver/.opt/zig/zig clang -fno-caret-diagnostics -target arm-unknown-unknown-eabi -mcpu=cortex-m0plus -ffreestanding -c -o /home/silver/.cache/zig/tmp/c60723f3c667529f-test.o test.s
error(compilation): clang failed with stderr: zig: warning: argument unused during compilation: '-fno-caret-diagnostics' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-ffreestanding' [-Wunused-command-line-argument]
test.s:1:1: error: invalid instruction, any one of the following would fix this:
mov r1, #0
^
test.s:1:9: note: operand must be a register in range [r0, r15]
mov r1, #0
        ^
test.s:1:1: note: instruction requires: thumb2
mov r1, #0
^

test.s:1:1: error: unable to build C object: clang exited with code 1

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Dec 26, 2021
  2. changed the title [-]CPU flags are not passed to clang[/-] [+]CPU features are not passed to clang[/+] on Dec 26, 2021
  3. akovaski commented on Dec 27, 2021

    @akovaski

    Just some info:
    This is already somewhat known (Not that there's a duplicate issue on github), see the .assembly part of addCCArgs in Compilation.zig:

    // The Clang assembler does not accept the list of CPU features like the
    // compiler frontend does. Therefore we must hard-code the -m flags for
    // all CPU features here.

    I'm not sure what exactly this comment is referring to, but I suspect it is due to clang's -Xassembler argument not passing -target-feature to the assembler [1] [2]. (Zig uses -Xclang -target-feature -Xclang -<feature> to pass target features for C files.)

    So manually adding exceptions is the current state. I expect Zig could pass -target-feature if calling the Clang assembly compiler directly with the -cc1as flag (.e.g clang-13 -cc1as -triple riscv64 ./os/entry.s -target-feature -relax). You'd also have to adjust how Zig passes all the other flags to Clang for assembly files, because clang -cc1as has different flags from clang.

    (P.S. There's also a corresponding -cc1 flag for Clang's C compiler)

  4. added
    enhancementSolving this issue will likely involve adding new logic or components to the codebase.
    zig ccZig as a drop-in C compiler feature
    on Dec 27, 2021
  5. added this to the 0.10.0 milestone on Dec 27, 2021
  6. added
    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.
    on Dec 27, 2021
  7. zheng commented on Jan 4, 2022

    @zheng

    Just some info: This is already somewhat known (Not that there's a duplicate issue on github), see the .assembly part of addCCArgs in Compilation.zig:

    // The Clang assembler does not accept the list of CPU features like the
    // compiler frontend does. Therefore we must hard-code the -m flags for
    // all CPU features here.

    I'm not sure what exactly this comment is referring to, but I suspect it is due to clang's -Xassembler argument not passing -target-feature to the assembler [1] [2]. (Zig uses -Xclang -target-feature -Xclang -<feature> to pass target features for C files.)

    So manually adding exceptions is the current state. I expect Zig could pass -target-feature if calling the Clang assembly compiler directly with the -cc1as flag (.e.g clang-13 -cc1as -triple riscv64 ./os/entry.s -target-feature -relax). You'd also have to adjust how Zig passes all the other flags to Clang for assembly files, because clang -cc1as has different flags from clang.

    (P.S. There's also a corresponding -cc1 flag for Clang's C compiler)

    Very helpful - thanks for all the context!

    I was able to run the clang compiler frontend and pass in the target feature in the example in the format you described. I'm going to try and get the various assembly flags right and put up a PR.

  8. modified the milestones: 0.10.0, 0.11.0 on Sep 14, 2022
  9. mikdusan commented on Jan 26, 2023

    @mikdusan
    Member

    found this stalled patch from '2014, though we'd need a few more passthru-args enabled: https://reviews.llvm.org/D6170

  10. modified the milestones: 0.11.0, 0.11.1 on Jul 20, 2023
  11. richarddd commented on Jan 19, 2024

    @richarddd

    Can we expect this in 0.11.1 or 0.12? :)

  12. 26 remaining items

  13. added this to the urgent milestone on Aug 1, 2025
  14. added 2 commits that reference this issue on Aug 3, 2025
    313a29c
    1bcfaed
  15. added a commit that references this issue on Aug 13, 2025
    cb4ed58
  16. added 7 commits that reference this issue on Aug 25, 2025
    60c5f6a
    73ba0c9
    768ed41
    b81c559
    5375a3e
    df245c3
    8c47dda
  17. removed their assignment
    on Sep 2, 2025
  18. modified the milestones: urgent, 0.16.0 on Mar 17, 2026
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

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.upstreamAn issue with a third party project that Zig uses.zig ccZig as a drop-in C compiler feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions