[NVPTX] activated `sm_xx` subtarget features are not respected for ptx `.target` directive
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
LLVM models CUDA's compute capability as both: `-mcpu` values and subtarget features. While most decisions in the backend are based on the activated subtarget features (e.g. [here](https://github.com/llvm/llvm-project/blob/3b15d851b9f15b088b5a903aa8672f65c9ec06f8/llvm/lib/Target/NVPTX/NVPTXSubtarget.h#L101); `SmVersion` is derived from `FullSmVersion` which is [parsed from the activated subtarget features](https://github.com/llvm/llvm-project/blob/3c49429089efff48e3e1fce53682e6a954ee4cbd/llvm/lib/Target/NVPTX/NVPTX.td#L28)), the `.target` directive [only takes the `-mcpu` value into account](https://github.com/llvm/llvm-project/blob/3b15d851b9f15b088b5a903aa8672f65c9ec06f8/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp#L817).
This could lead to situations that the emitted instructions are unknown for the specified compute capability, when the compute capability specified by subtarget features is higher than specified by the subtarget features implied by the cpu.
Sample godbolt reproducer: https://godbolt.org/z/5W4jvxbr5
The resulting ptx is ill formed. `ptxas -arch=sm_80` complains with
```
ptxas testbf.ptx, line 20; error : Feature '.bf16' requires .target sm_80 or higher
ptxas testbf.ptx, line 20; error : Feature 'cvt.bf16.f32' requires .target sm_80 or higher
ptxas fatal : Ptx assembly aborted due to errors
```
Contributor guide
Research direction
Start by reading NVPTXAsmPrinter.cpp around the .target directive and compare its use of the -mcpu value with SmVersion in NVPTXSubtarget.h, which is derived from features defined in NVPTX.td. Run the linked reproducer and inspect the emitted PTX with ptxas -arch=sm_80. Done means the .target directive reflects the activated subtarget features and the resulting PTX is accepted for the requested capability.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 62/100