Skip to content

Compiling for llvm-cpu without targeting a specific CPU is a bad experience #18561

Closed

Description

@stellaraccident

I've seen multiple people falling down this hole: they run iree-compile on their model, targeting CPU. Then they get performance that is 10x-100x off of any reasonable expectation. Then they either go away silently or report back about poor experiences (not always reporting flags and such).

There are good reasons why a compiler like IREE shouldn't make assumptions about what the CPU target is, but on the other hand, it will almost always produce a a grossly subpar experience to not specify a target CPU, since the generic target (at least on X86) lacks so many features as to be basically useless for any high performance numerics.

I've even fallen down this hole recently and had to go remember the incantation to select a specific CPU. In the case I was working on (an f16 CPU LLM), performance was 100x different between not specifying a target CPU and specifying "host". We need to guide people better than this.

Proposal

As mentioned, there are good reasons for a compiler to not make too many assumptions without being told what to do. But I think we can/should actively warn, possibly with a link to the documentation site when the compiler is invoked without the user specifying a target CPU. Whatever the warning is should be very explicit that the user should pass --iree-llvmcpu-target-cpu=host to target the precise CPU they are running on. We should possibly also accept "generic" or something like that for if the user really wants to target the default and not get the warning. I basically want to guard against the case where the user has not specified anything and the compiler just silently generates 100x too slow of code. In almost all cases, it will be better for the user to say something and we should guide them on a proper choice.

Activity

  1. ScottTodd commented on Sep 20, 2024

    @ScottTodd
    Member

    We need to guide people better than this.

    Yes please! #15487

  2. added
    documentation
    codegen/llvm
    on Sep 20, 2024
  3. stellaraccident commented on Sep 20, 2024

    @stellaraccident
    CollaboratorAuthor

    Let's stop over thinking this and do something simple like I suggest. Open to other options but would like to see this improved.

  4. ScottTodd commented on Sep 20, 2024

    @ScottTodd
    Member

    The suggested proposal SGTM. I might even want to default to having LLVM use the current host for the target CPU and available features (if those are different), then have users explicitly pass "generic" for the lowest common denominator.

    We could apply similar logic to the GPU backends - try to detect devices on the system (shell out to vulkaninfo / rocm-smi / nvidia-smi?) and default to what is available, but still support cross compilation with explicit device info and a "generic" target where possible.

  5. benvanik commented on Sep 20, 2024

    @benvanik
    Collaborator

    Yuck - that is not a cheap thing to do and has a high risk of flakes - I am still not sure why proper documentation is insufficient? You must specify your target device (--iree-hal-target-device=) when compiling so also specifying a "use my hardware info" is fine. Just change the documentation to include both flags and then a user has a choice and knows what to do if they want to change things. Anything automatic is going to have issues. Users aren't coming in to iree-compile command line invocations blind - if all the docs specify the flag and they choose not to copy/paste it that's on them.

  6. benvanik commented on Sep 20, 2024

    @benvanik
    Collaborator

    Note that this is also what clang does - https://clang.llvm.org/docs/HIPSupport.html - you must pass --offload-arch= to compile HIP code and if you want the native host target you must pass --offload-arch=native. nvcc does the same thing - you pass -arch=[some gpu] or -arch=native.

  7. benvanik commented on Sep 20, 2024

    @benvanik
    Collaborator

    (if there's such a big concern about documentation not fixing this issue then I'd be ok with making compilation fail if the user doesn't specify an arch for a backend - whether a particular arg, generic, native, etc - but guessing is bad)

  8. ScottTodd commented on Sep 20, 2024

    @ScottTodd
    Member

    We can certainly update the docs (https://iree.dev/guides/deployment-configurations/cpu/#compile-a-program) and start with a warning from the compiler if information is omitted and generic is used as the default.

    I'm seeing a proliferation of flags (mainly in rocm usage, but also cpu) and the documentation can't keep up. I want more of that to be captured somewhere - docs, samples, the compiler itself, etc.

    See one example here:

    ROCM_COMPILE_FLAGS = [
    "--iree-hal-target-backends=rocm",
    f"--iree-hip-target={rocm_chip}",
    "--iree-opt-const-eval=false",
    f"--iree-codegen-transform-dialect-library={iree_test_path_extension}/attention_and_matmul_spec.mlir",
    "--iree-global-opt-propagate-transposes=true",
    "--iree-dispatch-creation-enable-fuse-horizontal-contractions=true",
    "--iree-dispatch-creation-enable-aggressive-fusion=true",
    "--iree-opt-aggressively-propagate-transposes=true",
    "--iree-opt-outer-dim-concat=true",
    "--iree-vm-target-truncate-unsupported-floats",
    "--iree-llvmgpu-enable-prefetch=true",
    "--iree-opt-data-tiling=false",
    "--iree-codegen-gpu-native-math-precision=true",
    "--iree-codegen-llvmgpu-use-vector-distribution",
    "--iree-hip-waves-per-eu=2",
    "--iree-execution-model=async-external",
    "--iree-preprocessing-pass-pipeline=builtin.module(iree-preprocessing-transpose-convolution-pipeline,iree-preprocessing-pad-to-intrinsics)",
    "--iree-scheduling-dump-statistics-format=json",
    "--iree-scheduling-dump-statistics-file=compilation_info.json",
    ]

  9. benvanik commented on Sep 20, 2024

    @benvanik
    Collaborator

    That's insanity - besides the debug flags (dumping statistics/etc) if any of those are required that's a bug. I think Mahesh has said it before: a feature is not done until it's on by default and if all of those flags are needed to make the model compile or perform then the engineering was never completed. The only two flags required there should be --iree-hal-target-device=hip (that's using the old deprecated flag) and --iree-hip-target= (which could be native if we wanted to do what clang does and invoke amdgpu-arch if it's present).

  10. stellaraccident commented on Sep 20, 2024

    @stellaraccident
    CollaboratorAuthor

    I'm fine making --iree-llvmcpu-target-cpu=<something> required. Given the proliferation of things out there, I think that the way to get there may be to first do what I am suggesting: make it issue a warning if not specified and incorrectly/implicitly defaulting to a generic CPU (with a note that this flag will soon be required).

    Agreed on all of the other points. Need to burn down all of the other flags. I'm just starting with this one.

  11. ScottTodd commented on Sep 20, 2024

    @ScottTodd
    Member

    I can take a pass at this, unless someone else wants to.

    Plan:

  12. self-assigned this
    on Sep 20, 2024
  13. ScottTodd commented on Sep 20, 2024

    @ScottTodd
    Member

    Can someone clarify why we have all three of these flags?

    • --iree-llvmcpu-target-triple
    • --iree-llvmcpu-target-cpu
    • --iree-llvmcpu-target-cpu-features

    It seems like the triple could be a superset of the cpu? Is there some redundancy there? I see some riscv sample code setting both:

    RISCV_64=(
    --iree-llvmcpu-target-triple=riscv64-pc-linux-elf
    --iree-llvmcpu-target-cpu=generic-rv64
    --iree-llvmcpu-target-cpu-features=+m,+a,+f,+d,+c
    --iree-llvmcpu-target-abi=lp64d
    )

    but even our microkernels blog post (highlighting cpu performance work) only includes a few of the flags:

    Basic compilation command line:
    ```bash
    $ iree-compile matmul.mlir -o /tmp/matmul.vmfb \
    --iree-hal-target-backends=llvm-cpu \
    --iree-llvmcpu-target-cpu=znver4 \
    --iree-llvmcpu-enable-ukernels=all
    ```

    Oh, the linalg tutorial from @bjacob explains that target-cpu is for x86, but target-cpu-features is for other architectures?

    * To run on GPU or other non-CPU targets, explore other values for
    `--iree-hal-target-backends=`. You will then need to pass a matching
    `--device=` to `iree-run-module` below.
    * To cross-compile, explore `--iree-llvmcpu-target-triple=`.
    * To enable higher CPU performance by enabling CPU features:
    * On x86, explore `--iree-llvmcpu-target-cpu=` (e.g.
    `--iree-llvmcpu-target-cpu=znver4` to target AMD Zen4).
    * On other architectures, explore `--iree-llvmcpu-target-cpu-features=`.
    * To optimize for running on the same machine that the compilation ran
    on, pass `--iree-llvmcpu-target-cpu=host`. That works regardless of
    CPU architecture.

    There is some complicated logic and then a few calls into LLVM itself in https://github.com/iree-org/iree/blob/main/compiler/plugins/target/LLVMCPU/LLVMTargetOptions.cpp.

    (This is why I filed #15487 - I've wanted someone directly familiar with LLVM CPU to be driving this)

  14. 14 remaining items

  15. ScottTodd commented on Sep 23, 2024

    @ScottTodd
    Member

    That could work, yeah. When I say "test my changes" here, I'm still just referring to local development "testing", not automated CI testing - that would be a nice bonus.

  16. stellaraccident commented on Sep 23, 2024

    @stellaraccident
    CollaboratorAuthor

    Well, if you have the knobs to verify locally, then you're more than halfway to a lit test. That's how most of these things in llvm proper get tested.

  17. ScottTodd commented on Sep 23, 2024

    @ScottTodd
    Member

    Pushed an initial attempt at reworking how the target init is handled: #18587 . I could pass that off to someone else and context switch to other tasks 🤔

  18. bjacob commented on Sep 24, 2024

    @bjacob
    Collaborator

    Sorry, I had not kept up with the discussion here, was heads down in GPU data tiling.

    Here are the difficulties that I know of:

    1. Different concepts are more or less relevant on different CPU architectures:
      • On x86, people want to talk in terms of "CPU" (meaning microarchitecture) such as znver4 or cascadelake. People do not typically want to talk in terms of CPU features on x86 because that is very cumbersome. For example, just enabling the baseline AVX-512 feature set on x86 is a combination of 5 features, each with long names; a typical compilation relies on > 10 CPU features.
      • On RISC-V, people want to talk in terms of CPU features, and there are many, but they are not too cumbersome thanks to very short names, e.g. +z,+a,+m. The "CPU" string is not much used on RISC-V, according to RISC-V folks I asked back then, due to the very modular nature of the architecture.
      • On Arm, people typically specify baseline Arm architecture version plus a few CPU features, e.g. armv8.2-a+i8mm. The CPU names are also not much used on Arm; when targeting Android, fragmentation makes that hard anyway.
    2. To map a CPU name to CPU features, LLVM has a nice utility function doing that... on x86, but not on other architectures.
      • You know me, if that utility function had been available outside of x86, I would not have special-cased x86.
      • I tried implementing that on other architectures, but the ways I could see were either still architecture-specific in some way, or felt too heavy.

    Here is what I would do:

    1. When --iree-llvmcpu-target-triple is host (or unspecified, so defaults to host), default to --iree-llvmcpu-target-cpu=host.
      • This matches the behavior of hipcc, so I presume also nvcc.
    2. When --iree-llvmcpu-target-triple is not host, your Warn when --iree-llvmcpu-target-cpu defaults to "generic". #18587 sounds like a good way to go. Could dump a list of recognized CPU names for the specified target architecture.
  19. ScottTodd commented on Sep 24, 2024

    @ScottTodd
    Member

    Thanks for the context!


    When --iree-llvmcpu-target-triple is host (or unspecified, so defaults to host), default to --iree-llvmcpu-target-cpu=host.

    • This matches the behavior of hipcc, so I presume also nvcc.

    Defaulting to host (and not requiring it be set explicitly) goes against @benvanik 's suggestions up in the issue: #18561 (comment) (unless that was specifically referring to autodetection for gpu targets?)

    Note that this is also what clang does - https://clang.llvm.org/docs/HIPSupport.html - you must pass --offload-arch= to compile HIP code and if you want the native host target you must pass --offload-arch=native. nvcc does the same thing - you pass -arch=[some gpu] or -arch=native.


    2. Could dump a list of recognized CPU names for the specified target architecture.

    Oh, this sounds useful. Are there functions in LLVM that would help get such a list?

  20. benvanik commented on Sep 24, 2024

    @benvanik
    Collaborator

    Yeah mostly about GPU targets (where we have to launch other tools that fully load/initialize drivers and such) - CPU detection in LLVM is free.

  21. bjacob commented on Oct 3, 2024

    @bjacob
    Collaborator

    WDYT about #18682 ?

  22. bjacob commented on Oct 30, 2024

    @bjacob
    Collaborator

    @ScottTodd , should we close this as completed by #18682 or leave this open until further changes are made, such as promoting that warning into an error? That we shouldn't do before a full release cycle of whichever distribution channel users are getting their IREE from, so I wonder if leaving this open means having an open, essentially unactionable issue for half a year.

  23. ScottTodd commented on Oct 30, 2024

    @ScottTodd
    Member

    Let's keep this open until we at least update our docs (https://iree.dev/guides/deployment-configurations/cpu/) to include the recommended best practices (e.g. --iree-llvmcpu-target-cpu=host).

    We'll push a new stable release within a few weeks, and the next should then be 6-8 weeks later.

  24. hanhanW commented on May 8, 2026

    @hanhanW
    Contributor

    Let's keep this open until we at least update our docs (https://iree.dev/guides/deployment-configurations/cpu/) to include the recommended best practices (e.g. --iree-llvmcpu-target-cpu=host).

    We'll push a new stable release within a few weeks, and the next should then be 6-8 weeks later.

    Closing the issue because the docs already include the recommended flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen/llvmdocumentation

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions