← Back to Pull requests

[AMDGPU] Don't select v_mad_f16/v_mac_f16 on GFX10+ #234544

Open
anjenner opened this pull request with 24 lines diff across 3 files
View on GitHub
anjenner anjenner commented on Oct 22, 2026 09:23 UTC
Author

V_MAD_F16 (v_mad_f16 on GFX8, renamed v_mad_legacy_f16 on GFX9) and V_MAC_F16 only exist on GFX8 and GFX9, but the f16 patterns selecting them for any_fmad were only predicated on Has16BitInsts. (The newer GFX9 v_mad_f16, V_MAD_F16_gfx9, is a different instruction and is not selected by these patterns.) ISD::FMAD for f16 is only formed when hasMadF16() is true, but llvm.amdgcn.fmad.ftz.f16 is selected through the same patterns directly. On GFX10 and later this selected a pseudo instruction with no encoding, and both SelectionDAG and GlobalISel crashed later with an "Invalid opcode" assertion in SIInstrInfo::getInstSizeInBytes.

Restrict the patterns to GFX8 and GFX9. llvm.amdgcn.fmad.ftz.f16 now fails with a selection error on GFX10+, as llvm.amdgcn.fmad.ftz.f32 already does on targets without v_mad_f32/v_mac_f32.

Assisted-by: AI

Diff: 24 lines (+23 / -1) across 3 files Reviewer: jayfoad Review duration: 56958m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.32) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.32 · 24 lines diff across 3 files
0.32
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
87%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.193
Real PR Conversation Timeline
7 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 22, 2026 09:23 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: anjenner

<details>
<summary>Changes</summary>

V_MAD_F16 (v_mad_f16 on GFX8, renamed v_mad_legacy_f16 on GFX9) and V_MAC_F16 only exist on GFX8 and GFX9, but the f16 patterns selecting them for any_fmad were only predicated on Has16BitInsts. (The newer GFX9 v_mad_f16, V_MAD_F16_gfx9, is a different instruction and is not selected by these patterns.) ISD::FMAD for f16 is only formed when hasMadF16() is true, but llvm.amdgcn.fmad.ftz.f16 is selected through the same patterns directly. On GFX10 and later this selected a pseudo instruction with no encoding, and both SelectionDAG and GlobalISel crashed later with an "Invalid opcode" assertion in SIInstrInfo::getInstSizeInBytes.

Restrict the patterns to GFX8 and GFX9. llvm.amdgcn.fmad.ftz.f16 now fails with a selection error on GFX10+, as llvm.amdgcn.fmad.ftz.f32 already does on targets without v_mad_f32/v_mac_f32.

Assisted-by: AI

---
Full diff: https://github.com/llvm/llvm-project/pull/234544.diff

3 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInstructions.td (+4-1)

  • (modified) llvm/lib/Target/AMDGPU/VOP3Instructions.td (+3)

  • (added) llvm/test/CodeGen/AMDGPU/llvm.amdgcn.fmad.ftz.f16.err.ll (+16)
  • ``````

    diff --git a/llvm/lib/Target/AMDGPU/SIInstructions.td b/llvm/lib/Target/AMDGPU/SIInstructions.td
    index 87e1769d0ccd1..671a0859e91dc 100644
    --- a/llvm/lib/Target/AMDGPU/SIInstructions.td
    +++ b/llvm/lib/Target/AMDGPU/SIInstructions.td
    @@ -1560,7 +1560,10 @@ def : GCNPat <
                                  SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
     >;
     
    -let SubtargetPredicate = Has16BitInsts in
    +// V_MAD_F16 (v_mad_f16 on GFX8, v_mad_legacy_f16 on GFX9) and V_MAC_F16
    +// only exist on GFX8 and GFX9. The newer v_mad_f16 (V_MAD_F16_gfx9) is
    +// GFX9-only and not selected by these patterns.
    +let SubtargetPredicate = Has16BitInsts, OtherPredicates = [isGFX8GFX9] in
     def : FMADPat <f16, V_MAC_F16_e64>;
     } // AddedComplexity = 9
     
    diff --git a/llvm/lib/Target/AMDGPU/VOP3Instructions.td b/llvm/lib/Target/AMDGPU/VOP3Instructions.td
    index 42db01dd28cf1..fffbeb674415b 100644
    --- a/llvm/lib/Target/AMDGPU/VOP3Instructions.td
    +++ b/llvm/lib/Target/AMDGPU/VOP3Instructions.td
    @@ -472,6 +472,9 @@ let SubtargetPredicate = Has16BitInsts, isCommutable = 1 in {
     defm V_MAD_U16 : VOP3Inst <"v_mad_u16", VOP3_Profile<VOP_I16_I16_I16_I16, VOP3_CLAMP>>;
     defm V_MAD_I16 : VOP3Inst <"v_mad_i16", VOP3_Profile<VOP_I16_I16_I16_I16, VOP3_CLAMP>>;
     let FPDPRounding = 1 in {
    +  // V_MAD_F16 (v_mad_f16 on GFX8, v_mad_legacy_f16 on GFX9) only exists on
    +  // GFX8 and GFX9. The newer v_mad_f16 is V_MAD_F16_gfx9 below.
    +  let OtherPredicates = [isGFX8GFX9] in
       defm V_MAD_F16 : VOP3Inst <"v_mad_f16", VOP3_Profile<VOP_F16_F16_F16_F16>, any_fmad>;
       let Uses = [MODE, M0, EXEC] in {
       let OtherPredicates = [isNotGFX90APlus] in
    diff --git a/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.fmad.ftz.f16.err.ll b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.fmad.ftz.f16.err.ll
    new file mode 100644
    index 0000000000000..3cb55ba581222
    --- /dev/null
    +++ b/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.fmad.ftz.f16.err.ll
    @@ -0,0 +1,16 @@
    +; RUN: not --crash llc -global-isel=0 -mtriple=amdgpu10.10 < %s 2>&1 | FileCheck -check-prefix=SDAG-ERR %s
    +; RUN: not llc -global-isel=1 -mtriple=amdgpu10.10 < %s 2>&1 | FileCheck -check-prefix=GISEL-ERR %s
    +; RUN: not --crash llc -global-isel=0 -mtriple=amdgpu11.00 < %s 2>&1 | FileCheck -check-prefix=SDAG-ERR %s
    +; RUN: not llc -global-isel=1 -mtriple=amdgpu11.00 < %s 2>&1 | FileCheck -check-prefix=GISEL-ERR %s
    +
    +; v_mad_f16 and v_mac_f16 only exist on GFX8 and GFX9, so
    +; llvm.amdgcn.fmad.ftz.f16 cannot be selected on GFX10+. This used to select
    +; a pseudo instruction with no encoding and crash later in the pipeline.
    +
    +; SDAG-ERR: LLVM ERROR: Cannot select: {{.*}} = AMDGPUISD::FMAD_FTZ
    +; GISEL-ERR: LLVM ERROR: cannot select: {{.*}} = G_INTRINSIC intrinsic(@llvm.amdgcn.fmad.ftz)
    +
    +define half @fmad_ftz_f16(half %a, half %b, half %c) {
    +  %r = call half @llvm.amdgcn.fmad.ftz.f16(half %a, half %b, half %c)
    +  ret half %r
    +}
    
    
    ``````

    </details>

    anjenner anjenner commented on Oct 22, 2026 09:28 UTC
    Author

    AI found this while investigating something else. Nothing generates this so it's not important to fix but given that we're fixing fuzzer-found errors it seems reasonable to turn this case from a crash to a "cannot select" error.

    github-actions[bot] github-actions[bot] commented on Oct 22, 2026 13:59 UTC
    Bot

    <!--PREMERGE ADVISOR COMMENT: Linux-->

    :penguin: Linux x64 Test Results

    154305 tests passed
    1799 tests skipped

    All executed tests passed, but another part of the build failed. Click on a failure below to see the details.

    <details>
    <summary>[code=1] libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o</summary>

    FAILED: [code=1] libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang++ --target=x86_64-unknown-linux-gnu -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION=2 -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/x86_64-unknown-linux-gnu/c++/v1 -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1 -I/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxxabi/include -isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/cmake/Modules/../../libc -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Xclang -fno-pch-timestamp -O3 -DNDEBUG -fPIC -UNDEBUG -std=c++26 -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-nullability-completeness -Wmissing-prototypes -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -fdebug-prefix-map=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1=/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/include -MD -MT libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o -MF libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o.d -o libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o -c /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp:12:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/codecvt:64:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/__locale_dir/codecvt.h:17:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/__locale_dir/locale.h:27:
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:2941:96: error: too few arguments to function call, expected 2, have 1
    2941 |     __long __buffer = __allocate_long_buffer(__alloc_, __n, __get_amortized_growth_capacity(__n));
    |                                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:1492:7: note: in instantiation of function template specialization 'std::basic_string<wchar_t>::__assign_trivial<wchar_t *, wchar_t *>' requested here
    1492 |       __assign_trivial(__first, __last, __n);
    |       ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp:4621:17: note: in instantiation of function template specialization 'std::basic_string<wchar_t>::assign<wchar_t *, 0>' requested here
    4621 |     __weeks_[i].assign(wbuf, wbe);
    |                 ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:2309:3: note: '__get_amortized_growth_capacity' declared here
    2309 |   __get_amortized_growth_capacity(size_type __old_cap, size_type __additional_characters) {
    |   ^                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    

    </details>
    <details>
    <summary>[code=1] libcxx/src/CMakeFiles/cxx_static.dir/locale.cpp.o</summary>

    FAILED: [code=1] libcxx/src/CMakeFiles/cxx_static.dir/locale.cpp.o
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang++ --target=x86_64-unknown-linux-gnu -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_LIBCPP_AVAILABILITY_MINIMUM_HEADER_VERSION=2 -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/x86_64-unknown-linux-gnu/c++/v1 -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1 -I/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxxabi/include -isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/cmake/Modules/../../libc -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -Xclang -fno-pch-timestamp -O3 -DNDEBUG -UNDEBUG -std=c++26 -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-nullability-completeness -Wmissing-prototypes -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -fdebug-prefix-map=/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1=/home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/include -MD -MT libcxx/src/CMakeFiles/cxx_static.dir/locale.cpp.o -MF libcxx/src/CMakeFiles/cxx_static.dir/locale.cpp.o.d -o libcxx/src/CMakeFiles/cxx_static.dir/locale.cpp.o -c /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp:12:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/codecvt:64:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/__locale_dir/codecvt.h:17:
    In file included from /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/__locale_dir/locale.h:27:
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:2941:96: error: too few arguments to function call, expected 2, have 1
    2941 |     __long __buffer = __allocate_long_buffer(__alloc_, __n, __get_amortized_growth_capacity(__n));
    |                                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:1492:7: note: in instantiation of function template specialization 'std::basic_string<wchar_t>::__assign_trivial<wchar_t *, wchar_t *>' requested here
    1492 |       __assign_trivial(__first, __last, __n);
    |       ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/libcxx/src/locale.cpp:4621:17: note: in instantiation of function template specialization 'std::basic_string<wchar_t>::assign<wchar_t *, 0>' requested here
    4621 |     __weeks_[i].assign(wbuf, wbe);
    |                 ^
    /home/gha/actions-runner/_work/llvm-project/llvm-project/build/include/c++/v1/string:2309:3: note: '__get_amortized_growth_capacity' declared here
    2309 |   __get_amortized_growth_capacity(size_type __old_cap, size_type __additional_characters) {
    |   ^                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    

    </details>
    <details>
    <summary>[code=1] runtimes/runtimes-stamps/runtimes-build /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-stamps/runtimes-build</summary>

    FAILED: [code=1] runtimes/runtimes-stamps/runtimes-build /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-stamps/runtimes-build
    cd /home/gha/actions-runner/_work/llvm-project/llvm-project/build/runtimes/runtimes-bins && /usr/bin/cmake --build .
    

    </details>

    If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

    jayfoad jayfoad commented on code on Oct 26, 2026 13:36 UTC
    Code Review
    llvm/lib/Target/AMDGPU/SIInstructions.td:1
    @@ -1560,7 +1560,10 @@ def : GCNPat <
                                  SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
     >;
     
    -let SubtargetPredicate = Has16BitInsts in
    +// V_MAD_F16 (v_mad_f16 on GFX8, v_mad_legacy_f16 on GFX9) and V_MAC_F16
    +// only exist on GFX8 and GFX9. The newer v_mad_f16 (V_MAD_F16_gfx9) is
    +// GFX9-only and not selected by these patterns.
    +let SubtargetPredicate = Has16BitInsts, OtherPredicates = [isGFX8GFX9] in

    Nit: could just write:

    let SubtargetPredicate = isGFX8GFX9 in
    

    since that implies Has16BitInsts.

    jayfoad jayfoad commented on code on Oct 26, 2026 13:37 UTC
    Code Review
    llvm/lib/Target/AMDGPU/VOP3Instructions.td:1
    @@ -472,6 +472,9 @@ let SubtargetPredicate = Has16BitInsts, isCommutable = 1 in {
     defm V_MAD_U16 : VOP3Inst <"v_mad_u16", VOP3_Profile<VOP_I16_I16_I16_I16, VOP3_CLAMP>>;
     defm V_MAD_I16 : VOP3Inst <"v_mad_i16", VOP3_Profile<VOP_I16_I16_I16_I16, VOP3_CLAMP>>;
     let FPDPRounding = 1 in {
    +  // V_MAD_F16 (v_mad_f16 on GFX8, v_mad_legacy_f16 on GFX9) only exists on
    +  // GFX8 and GFX9. The newer v_mad_f16 is V_MAD_F16_gfx9 below.
    +  let OtherPredicates = [isGFX8GFX9] in

    Nit: should normally use SubtargetPredicate for a subtarget predicate, unless there's some reason that doesn't work here.

    jayfoad jayfoad approved these changes on Oct 26, 2026 13:37 UTC
    Approved Reviewer

    LGTM, just optional nits inline

    Reviewers
    Assignees
    anjenner anjenner
    Labels
    risk: low confidence: 87.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.2% (LOW) · Higher = larger blast radius
    32.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.8% · Lower = rushed or shallow review
    87.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.9% · Safe to merge
    3.9%