llvm / llvm/llvm-project

[NVPTX][TTI] v4i8 scalarization cost path is unreachable

Open
#224,808 0 comments 0 reactions 0 assignees View on GitHub
backend:NVPTX
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Problem

`NVPTXTTIImpl::getScalarizationOverhead()` has a dedicated cost model for constructing `v4i8`:

```cpp
if (Insert && VT == MVT::v4i8) {
InstructionCost Cost = 3; // 3 x PRMT
...
}
```

However, `v4i8` also matches the preceding generic packed 32-bit vector case:

```cpp
if (Insert && NVPTX::isPackedVectorTy(VT) && VT.is32BitVector()) {
Cost += 1;
Insert = false;
}
```

This clears `Insert`, making the `v4i8`-specific branch unreachable.

There is an additional bug in the `v4i8` branch: it declares a new local `Cost`, shadowing the outer accumulator, so its computed cost would not contribute to the returned value even if the branch were reached.

As a result, constructing a dynamic `<4 x i8>` vector is reported with aggregate cost `1`, while the dedicated implementation is intended to model three `PRMT` operations.

## Reproducer

```llvm
define <4 x i8> @pack(i8 %a, i8 %b, i8 %c, i8 %d) {
%v0 = insertelement <4 x i8> poison, i8 %a, i32 0
%v1 = insertelement <4 x i8> %v0, i8 %b, i32 1
%v2 = insertelement <4 x i8> %v1, i8 %c, i32 2
%v3 = insertelement <4 x i8> %v2, i8 %d, i32 3
ret <4 x i8> %v3
}
```

Tested with LLVM `0025dca`, NVPTX `sm_89`.

## Expected

The `v4i8`-specific cost path should be reachable, and its cost should be accumulated into the returned scalarization overhead.

Contributor guide

Open the contributing guide

Research direction

Start at NVPTXTTIImpl::getScalarizationOverhead() and reproduce the issue with the provided LLVM IR on NVPTX sm_89. Ensure the v4i8-specific cost path is reachable and its cost is accumulated into the returned scalarization overhead, with the dynamic vector construction reporting the intended cost.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.