From c9b0c9420a51079d963ae7eadd8c1dbf9eaf856b Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Tue, 18 Aug 2026 11:49:53 +0200 Subject: [PATCH 1/4] [AMDGPU] Fix crash folding cos/sin table lookups on poison/undef vector lanes TDOFold cast the lane directly to ConstantFP, which aborts under assertions when a lane is poison or undef instead of a table lookup failure --- llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp | 8 +++++-- .../AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp b/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp index 7291fdaca0d69..52bb6feb94229 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp @@ -844,8 +844,12 @@ bool AMDGPULibCalls::TDOFold(CallInst *CI, const FuncInfo &FInfo) { SmallVector Values; Values.reserve(vecSize); for (int eltNo = 0; eltNo < vecSize; ++eltNo) { - ConstantFP *eltval = - cast(CV->getAggregateElement((unsigned)eltNo)); + // A lane may be undef or poison, in which case there is nothing to + // look up in the table. + ConstantFP *eltval = dyn_cast_or_null( + CV->getAggregateElement((unsigned)eltNo)); + if (!eltval) + return false; auto MatchingRow = llvm::find_if(tr, [eltval](const TableEntry &entry) { return eltval->isExactlyValue(entry.input); }); diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll index 936471bac4ca9..4f784d4a40046 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll @@ -61,6 +61,29 @@ entry: ret <2 x double> %c } +; A lane that is not a ConstantFP has nothing to look up in the table. +define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { +; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: [[C:%.*]] = call <2 x float> @_Z3cosDv2_f(<2 x float> ) +; CHECK-NEXT: ret <2 x float> [[C]] +; +entry: + %c = call <2 x float> @_Z3cosDv2_f(<2 x float> ) + ret <2 x float> %c +} + +define <2 x float> @test_tdo_v2_f32_cos_undef_lane() { +; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_undef_lane() { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: [[C:%.*]] = call <2 x float> @_Z3cosDv2_f(<2 x float> ) +; CHECK-NEXT: ret <2 x float> [[C]] +; +entry: + %c = call <2 x float> @_Z3cosDv2_f(<2 x float> ) + ret <2 x float> %c +} + declare float @_Z3cosf(float) declare <2 x float> @_Z3cosDv2_f(<2 x float>) declare half @_Z3cosDh(half) From d8cc0b343c18df7ee22b9787d76719d980a84205 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Tue, 18 Aug 2026 12:08:54 +0200 Subject: [PATCH 2/4] fix undef deprecator remark --- .../AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll index 4f784d4a40046..14ec399a18808 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll @@ -61,7 +61,8 @@ entry: ret <2 x double> %c } -; A lane that is not a ConstantFP has nothing to look up in the table. +; A lane that is not a ConstantFP (e.g. poison or undef) has nothing to look +; up in the table. define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-NEXT: [[ENTRY:.*:]] @@ -73,17 +74,6 @@ entry: ret <2 x float> %c } -define <2 x float> @test_tdo_v2_f32_cos_undef_lane() { -; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_undef_lane() { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[C:%.*]] = call <2 x float> @_Z3cosDv2_f(<2 x float> ) -; CHECK-NEXT: ret <2 x float> [[C]] -; -entry: - %c = call <2 x float> @_Z3cosDv2_f(<2 x float> ) - ret <2 x float> %c -} - declare float @_Z3cosf(float) declare <2 x float> @_Z3cosDv2_f(<2 x float>) declare half @_Z3cosDh(half) From 96ee16e3a8074180df8e70d031b9869828b2a4e7 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Tue, 18 Aug 2026 12:51:09 +0200 Subject: [PATCH 3/4] comment --- llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll index 14ec399a18808..e66af5e6cdafa 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll @@ -61,8 +61,7 @@ entry: ret <2 x double> %c } -; A lane that is not a ConstantFP (e.g. poison or undef) has nothing to look -; up in the table. +; A lane that is not a ConstantFP has nothing to look up in the table. define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-NEXT: [[ENTRY:.*:]] From 3bcea7bf75b70f4ca615b2e6fcf3a4c587902f44 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Tue, 18 Aug 2026 12:54:51 +0200 Subject: [PATCH 4/4] Revert "comment" This reverts commit 96ee16e3a8074180df8e70d031b9869828b2a4e7. --- llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll index e66af5e6cdafa..14ec399a18808 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-simplify-libcall-tdo-cos.ll @@ -61,7 +61,8 @@ entry: ret <2 x double> %c } -; A lane that is not a ConstantFP has nothing to look up in the table. +; A lane that is not a ConstantFP (e.g. poison or undef) has nothing to look +; up in the table. define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-LABEL: define <2 x float> @test_tdo_v2_f32_cos_poison_lane() { ; CHECK-NEXT: [[ENTRY:.*:]]