Skip to content

[DirectX][ShaderFlags] Add analysis for WaveOps flag #118140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@
using namespace llvm;
using namespace llvm::dxil;

static bool checkWaveOps(Intrinsic::ID IID) {
switch (IID) {
default:
return false;
case Intrinsic::dx_wave_is_first_lane:
case Intrinsic::dx_wave_getlaneindex:
case Intrinsic::dx_wave_any:
case Intrinsic::dx_wave_all:
case Intrinsic::dx_wave_readlane:
case Intrinsic::dx_wave_active_countbits:
// Wave Active Op Variants
case Intrinsic::dx_wave_reduce_sum:
case Intrinsic::dx_wave_reduce_usum:
case Intrinsic::dx_wave_reduce_max:
case Intrinsic::dx_wave_reduce_umax:

// Currently unsupported intrinsics
// case Intrinsic::dx_WaveGetLaneCount:
// case Intrinsic::dx_WaveActiveAllEqual:
// case Intrinsic::dx_WaveActiveBallot:
// case Intrinsic::dx_WaveReadLaneFirst:
// case Intrinsic::dx_WaveActiveBit:
// case Intrinsic::dx_WavePrefixOp:
// case Intrinsic::dx_QuadReadLaneAt:
// case Intrinsic::dx_QuadOp:
// case Intrinsic::dx_WavePrefixBitCount:
// case Intrinsic::dx_WaveMatch:
// case Intrinsic::dx_WaveMultiPrefixOp:
// case Intrinsic::dx_WaveMultiPrefixBitCount:
// case Intrinsic::dx_QuadVote:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have proposed spellings for all of these, so you could name these to match those if you like: https://github.com/llvm/wg-hlsl/blob/main/proposals/0014-consistent-naming-for-dx-intrinsics.md#wave-ops

Also clang-format does something kind of awkward here - might be better to put this comment outside of the switch.

return true;
}
}

/// Update the shader flags mask based on the given instruction.
/// \param CSF Shader flags mask to update.
/// \param I Instruction to check.
Expand Down Expand Up @@ -92,6 +126,8 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,

// TODO: Set DX11_1_DoubleExtensions if I is a call to DXIL intrinsic
// DXIL::Opcode::Fma https://github.com/llvm/llvm-project/issues/114554

CSF.WaveOps |= checkWaveOps(CI->getIntrinsicID());
}
}

Expand Down
84 changes: 84 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/wave-ops.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
;
; Test that we have the correct shader flags to indicate that there are wave
; ops set at the module level
;
; CHECK: ; Shader Flags Value: [[WAVE_FLAG:0x00080000]]
; CHECK: ; Note: shader requires additional functionality:
; CHECK-NEXT: ; Wave level operations
; CHECK-NEXT: ; Note: extra DXIL module flags:

target triple = "dxil-pc-shadermodel6.7-library"

; Test the indiviual ops that they have the same Shader Wave flag at the
; function level to ensure that each op is setting it accordingly

define noundef i1 @wave_is_first_lane() {
entry:
; CHECK: Function wave_is_first_lane : [[WAVE_FLAG]]
%ret = call i1 @llvm.dx.wave.is.first.lane()
ret i1 %ret
}

define noundef i32 @wave_getlaneindex() {
entry:
; CHECK: Function wave_getlaneindex : [[WAVE_FLAG]]
%ret = call i32 @llvm.dx.wave.getlaneindex()
ret i32 %ret
}

define noundef i1 @wave_any(i1 %x) {
entry:
; CHECK: Function wave_any : [[WAVE_FLAG]]
%ret = call i1 @llvm.dx.wave.any(i1 %x)
ret i1 %ret
}

define noundef i1 @wave_all(i1 %x) {
entry:
; CHECK: Function wave_all : [[WAVE_FLAG]]
%ret = call i1 @llvm.dx.wave.all(i1 %x)
ret i1 %ret
}

define noundef i1 @wave_readlane(i1 %x, i32 %idx) {
entry:
; CHECK: Function wave_readlane : [[WAVE_FLAG]]
%ret = call i1 @llvm.dx.wave.readlane.i1(i1 %x, i32 %idx)
ret i1 %ret
}

define noundef i32 @wave_reduce_sum(i32 noundef %x) {
entry:
; CHECK: Function wave_reduce_sum : [[WAVE_FLAG]]
%ret = call i32 @llvm.dx.wave.reduce.sum.i32(i32 %x)
ret i32 %ret
}

define noundef i32 @wave_reduce_usum(i32 noundef %x) {
entry:
; CHECK: Function wave_reduce_usum : [[WAVE_FLAG]]
%ret = call i32 @llvm.dx.wave.reduce.usum.i32(i32 %x)
ret i32 %ret
}

define noundef i32 @wave_reduce_max(i32 noundef %x) {
entry:
; CHECK: Function wave_reduce_max : [[WAVE_FLAG]]
%ret = call i32 @llvm.dx.wave.reduce.max.i32(i32 %x)
ret i32 %ret
}

define noundef i32 @wave_reduce_umax(i32 noundef %x) {
entry:
; CHECK: Function wave_reduce_umax : [[WAVE_FLAG]]
%ret = call i32 @llvm.dx.wave.reduce.umax.i32(i32 %x)
ret i32 %ret
}

define void @wave_active_countbits(i1 %expr) {
entry:
; CHECK: Function wave_active_countbits : [[WAVE_FLAG]]
%0 = call i32 @llvm.dx.wave.active.countbits(i1 %expr)
ret void
}