# Codegen on mac vs linux

**URL:** <https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745>\
**Category:** compiler\
**Created:** [May 20, 2021, 6:45pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745 "2021-05-20T18:45:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![petar-dambovaliev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petar-dambovaliev/32/7394_2.png) [@petar-dambovaliev](https://internals.rust-lang.org/u/petar-dambovaliev)\
**Post date:** [May 20, 2021, 6:45pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/1 "2021-05-20T18:45:53Z")

</div>

```
stable-x86_64-apple-darwin (default)
rustc 1.52.1 (9bc8c42bb 2021-05-09)

```

Hey there folks. I would like to draw attention of those above my pay grade, who can shed some light on something i can't understand as of now. I was doing some benchmarks on different crates and i noticed that a crate [https://crates.io/crates/matchit](https://crates.io/crates/matchit) when compiled for `stable-x86_64-apple-darwin` does not have any SIMD instructions but when compiled for `x86_64-unknown-linux-gnu` it does have SIMD instructions and is much faster as a result.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [May 20, 2021, 7:09pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/2 "2021-05-20T19:09:05Z")

</div>

That's surprising, because at the compiler level, macOS should have the advantage in target features. The `x86_64-unknown-linux-gnu` target is very generic with only `fxsr`, `sse`, and `sse2`, while `x86_64-apple-darwin` uses a "core2" base CPU adding `cmpxchg16b`, `sse3`, and `ssse3`.

Maybe something in the crate itself (or its dependencies) is using explicit SIMD gated for Linux only?

---

<div class="post-metadata">

**Author:** ![petar-dambovaliev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petar-dambovaliev/32/7394_2.png) [@petar-dambovaliev](https://internals.rust-lang.org/u/petar-dambovaliev)\
**Post date:** [May 20, 2021, 7:41pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/3 "2021-05-20T19:41:11Z")

</div>

The same results can be observed when I use my Linux machine as well. The crate is using SIMD UTF-8 validation. However, I have removed it and it's not using any other crates. There are still SIMD instructions which are not present on Mac. Several people, including me, the author of the crate and others from the Rust discord server have tried to understand what's happening here but couldn't.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 20, 2021, 9:37pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/4 "2021-05-20T21:37:55Z")

</div>

Do you have `.cargo/config` in the project directory **or any parent directory**? It could be enabling target CPU features.

---

<div class="post-metadata">

**Author:** ![petar-dambovaliev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petar-dambovaliev/32/7394_2.png) [@petar-dambovaliev](https://internals.rust-lang.org/u/petar-dambovaliev)\
**Post date:** [May 20, 2021, 10:01pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/5 "2021-05-20T22:01:41Z")

</div>

No. Everything is up on the repo. There is no config.

---

<div class="post-metadata">

**Author:** ![illicitonion](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/illicitonion/32/3886_2.png) [@illicitonion](https://internals.rust-lang.org/u/illicitonion)\
**Post date:** [May 20, 2021, 10:38pm UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/6 "2021-05-20T22:38:25Z")

</div>

I believe the default target on darwin is to target very general target environments for maximum compatibility:

```rust
% rustc +nightly -Z unstable-options --print target-spec-json | jq '."llvm-target"'
"x86_64-apple-macosx10.7.0"

```

See [this thread](https://internals.rust-lang.org/t/policies-around-default-cpu-architecture/9142) for information about how you can fix this for your builds, and discussion about changing the default.

---

<div class="post-metadata">

**Author:** ![petar-dambovaliev](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petar-dambovaliev/32/7394_2.png) [@petar-dambovaliev](https://internals.rust-lang.org/u/petar-dambovaliev)\
**Post date:** [May 21, 2021, 6:11am UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/7 "2021-05-21T06:11:01Z")

</div>

That solved the mystery. Thank you! I added a `.cargo/config.toml`

```rust
[target.x86_64-unknown-linux-gnu]
rustflags = ["-Ctarget-cpu=native"]

[target.x86_64-apple-darwin]
rustflags = ["-Ctarget-cpu=native"]

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 19, 2021, 6:11am UTC](https://internals.rust-lang.org/t/codegen-on-mac-vs-linux/14745/8 "2021-08-19T06:11:25Z")

</div>

Closed on August 19, 2021, 6:11am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
