[AMDGPU] Don't select v_mad_f16/v_mac_f16 on GFX10+ #234544
<!--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:
````````````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>
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.
<!--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.
Nit: could just write:
let SubtargetPredicate = isGFX8GFX9 in
since that implies Has16BitInsts.
Nit: should normally use SubtargetPredicate for a subtarget predicate, unless there's some reason that doesn't work here.
LGTM, just optional nits inline
jayfoad
anjenner
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