https://github.com/michaelselehov updated https://github.com/llvm/llvm-project/pull/238412
>From cd7a2036bb084409610945288755a14d790ec637 Mon Sep 17 00:00:00 2001 From: Michael Selehov <[email protected]> Date: Tue, 3 Nov 2026 12:47:36 -0600 Subject: [PATCH] [AMDGPU] Treat a bitcast between legal types as free The generic cost model treats a bitcast as free only when both types are integer types or both are not. AMDGPU keeps integer and floating-point values in the same registers, so a bitcast between two legal types is a no-op. A scalar bitcast from i32 to float cost 1, but the vector bitcast from <2 x i32> to <2 x float> cost 0. The SLP vectorizer counted this difference as a saving. A bitcast that involves a type the target legalizes, such as a vector of i8, keeps its cost, because the lanes may need repacking. Assisted-by: Claude Opus --- .../AMDGPU/AMDGPUTargetTransformInfo.cpp | 6 ++ .../CostModel/AMDGPU/bitcast-int-fp.ll | 70 +++++++++---------- .../SLPVectorizer/AMDGPU/bitcast-int-fp.ll | 7 +- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp index db9b5c6c1ef7f7..8adbf9ffa49f29 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp @@ -1066,6 +1066,12 @@ InstructionCost GCNTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Elts && Src->isIntegerTy(*Elts)) return InstructionCost(MaskUnpackCostPerElt) * *Elts * getFullRateInstrCost(); + // Integer and floating-point values share the same registers, so a bitcast + // between legal types is free. A legalized type, such as a vector of i8, + // may need its lanes repacked. + if (TLI->isTypeLegal(TLI->getValueType(DL, Src)) && + TLI->isTypeLegal(TLI->getValueType(DL, Dst))) + return 0; } // Each f32 lane is rounded and the halves are packed in pairs. A packed diff --git a/llvm/test/Analysis/CostModel/AMDGPU/bitcast-int-fp.ll b/llvm/test/Analysis/CostModel/AMDGPU/bitcast-int-fp.ll index 694789aa63099c..a0b364bd47e8bf 100644 --- a/llvm/test/Analysis/CostModel/AMDGPU/bitcast-int-fp.ll +++ b/llvm/test/Analysis/CostModel/AMDGPU/bitcast-int-fp.ll @@ -20,32 +20,32 @@ define void @scalar() { ; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F16toI16 = bitcast half poison to i16 ; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I16toBF16 = bitcast i16 poison to bfloat ; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %BF16toI16 = bitcast bfloat poison to i16 -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toF32 = bitcast i32 poison to float -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F32toI32 = bitcast float poison to i32 -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toF64 = bitcast i64 poison to double -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F64toI64 = bitcast double poison to i64 +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toF32 = bitcast i32 poison to float +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32toI32 = bitcast float poison to i32 +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toF64 = bitcast i64 poison to double +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toI64 = bitcast double poison to i64 ; GFX7-NEXT: Cost Model: Found an estimated cost of 10 for instruction: ret void ; ; ALL-LABEL: 'scalar' -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I16toF16 = bitcast i16 poison to half -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F16toI16 = bitcast half poison to i16 -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I16toBF16 = bitcast i16 poison to bfloat -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %BF16toI16 = bitcast bfloat poison to i16 -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toF32 = bitcast i32 poison to float -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F32toI32 = bitcast float poison to i32 -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toF64 = bitcast i64 poison to double -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F64toI64 = bitcast double poison to i64 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I16toF16 = bitcast i16 poison to half +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F16toI16 = bitcast half poison to i16 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I16toBF16 = bitcast i16 poison to bfloat +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %BF16toI16 = bitcast bfloat poison to i16 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toF32 = bitcast i32 poison to float +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32toI32 = bitcast float poison to i32 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toF64 = bitcast i64 poison to double +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toI64 = bitcast double poison to i64 ; ALL-NEXT: Cost Model: Found an estimated cost of 10 for instruction: ret void ; ; ALL-SIZE-LABEL: 'scalar' -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I16toF16 = bitcast i16 poison to half -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F16toI16 = bitcast half poison to i16 -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I16toBF16 = bitcast i16 poison to bfloat -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %BF16toI16 = bitcast bfloat poison to i16 -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toF32 = bitcast i32 poison to float -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F32toI32 = bitcast float poison to i32 -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toF64 = bitcast i64 poison to double -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F64toI64 = bitcast double poison to i64 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I16toF16 = bitcast i16 poison to half +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F16toI16 = bitcast half poison to i16 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I16toBF16 = bitcast i16 poison to bfloat +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %BF16toI16 = bitcast bfloat poison to i16 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toF32 = bitcast i32 poison to float +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32toI32 = bitcast float poison to i32 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toF64 = bitcast i64 poison to double +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toI64 = bitcast double poison to i64 ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void ; %I16toF16 = bitcast i16 poison to half @@ -68,35 +68,35 @@ define void @scalar_vector() { ; GFX7-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F16toI32 = bitcast <2 x half> poison to i32 ; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V4I8toI32 = bitcast <4 x i8> poison to i32 ; GFX7-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F32toV2I16 = bitcast float poison to <2 x i16> -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 -; GFX7-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 +; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> ; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toV2F32 = bitcast double poison to <2 x float> ; GFX7-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I128toV4F32 = bitcast i128 poison to <4 x float> ; GFX7-NEXT: Cost Model: Found an estimated cost of 10 for instruction: ret void ; ; ALL-LABEL: 'scalar_vector' -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toV2I16 = bitcast i32 poison to <2 x i16> -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toV2F16 = bitcast i32 poison to <2 x half> -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F16toI32 = bitcast <2 x half> poison to i32 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toV2I16 = bitcast i32 poison to <2 x i16> +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toV2F16 = bitcast i32 poison to <2 x half> +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V2F16toI32 = bitcast <2 x half> poison to i32 ; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V4I8toI32 = bitcast <4 x i8> poison to i32 ; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32toV2I16 = bitcast float poison to <2 x i16> -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 -; ALL-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 +; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> ; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toV2F32 = bitcast double poison to <2 x float> ; ALL-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I128toV4F32 = bitcast i128 poison to <4 x float> ; ALL-NEXT: Cost Model: Found an estimated cost of 10 for instruction: ret void ; ; ALL-SIZE-LABEL: 'scalar_vector' -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toV2I16 = bitcast i32 poison to <2 x i16> -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I32toV2F16 = bitcast i32 poison to <2 x half> -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F16toI32 = bitcast <2 x half> poison to i32 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toV2I16 = bitcast i32 poison to <2 x i16> +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I32toV2F16 = bitcast i32 poison to <2 x half> +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V2F16toI32 = bitcast <2 x half> poison to i32 ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V4I8toI32 = bitcast <4 x i8> poison to i32 ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32toV2I16 = bitcast float poison to <2 x i16> -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 -; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2F32 = bitcast i64 poison to <2 x float> +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %V2F32toI64 = bitcast <2 x float> poison to i64 +; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I64toV2I32 = bitcast i64 poison to <2 x i32> ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64toV2F32 = bitcast double poison to <2 x float> ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %I128toV4F32 = bitcast i128 poison to <4 x float> ; ALL-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void diff --git a/llvm/test/Transforms/SLPVectorizer/AMDGPU/bitcast-int-fp.ll b/llvm/test/Transforms/SLPVectorizer/AMDGPU/bitcast-int-fp.ll index 2799b1d0b9529e..1d6457b9ef59bc 100644 --- a/llvm/test/Transforms/SLPVectorizer/AMDGPU/bitcast-int-fp.ll +++ b/llvm/test/Transforms/SLPVectorizer/AMDGPU/bitcast-int-fp.ll @@ -20,9 +20,10 @@ define void @bitcast_only(i32 %a, i32 %b, ptr addrspace(3) %out) { ; ; PACKED-LABEL: define void @bitcast_only( ; PACKED-SAME: i32 [[A:%.*]], i32 [[B:%.*]], ptr addrspace(3) [[OUT:%.*]]) #[[ATTR0:[0-9]+]] { -; PACKED-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[A]], i64 0 -; PACKED-NEXT: [[TMP2:%.*]] = insertelement <2 x i32> [[TMP1]], i32 [[B]], i64 1 -; PACKED-NEXT: [[TMP3:%.*]] = bitcast <2 x i32> [[TMP2]] to <2 x float> +; PACKED-NEXT: [[AF:%.*]] = bitcast i32 [[A]] to float +; PACKED-NEXT: [[BF:%.*]] = bitcast i32 [[B]] to float +; PACKED-NEXT: [[R0:%.*]] = insertelement <2 x float> poison, float [[AF]], i64 0 +; PACKED-NEXT: [[TMP3:%.*]] = insertelement <2 x float> [[R0]], float [[BF]], i64 1 ; PACKED-NEXT: store <2 x float> [[TMP3]], ptr addrspace(3) [[OUT]], align 8 ; PACKED-NEXT: ret void ; _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
