Skip to content

Commit 3b7d9e5

Browse files
addressing pr comments
1 parent 2b22510 commit 3b7d9e5

File tree

10 files changed

+52
-70
lines changed

10 files changed

+52
-70
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static Value *handleHlslClip(const CallExpr *E, CodeGenFunction *CGF) {
104104

105105
Constant *FZeroConst = ConstantFP::getZero(CGF->FloatTy);
106106
Value *CMP;
107+
Value *LastInstr;
107108

108109
if (const auto *VecTy = E->getArg(0)->getType()->getAs<clang::VectorType>()) {
109110
FZeroConst = ConstantVector::getSplat(
@@ -116,23 +117,27 @@ static Value *handleHlslClip(const CallExpr *E, CodeGenFunction *CGF) {
116117
CMP = CGF->Builder.CreateFCmpOLT(Op0, FZeroConst);
117118

118119
if (CGF->CGM.getTarget().getTriple().isDXIL())
119-
return CGF->Builder.CreateIntrinsic(CGF->VoidTy, llvm::Intrinsic::dx_clip,
120-
{CMP}, nullptr);
120+
LastInstr = CGF->Builder.CreateIntrinsic(
121+
CGF->VoidTy, llvm::Intrinsic::dx_clip, {CMP}, nullptr);
122+
else if (CGF->CGM.getTarget().getTriple().isSPIRV()) {
123+
BasicBlock *LT0 = CGF->createBasicBlock("lt0", CGF->CurFn);
124+
BasicBlock *End = CGF->createBasicBlock("end", CGF->CurFn);
121125

122-
BasicBlock *LT0 = CGF->createBasicBlock("lt0", CGF->CurFn);
123-
BasicBlock *End = CGF->createBasicBlock("end", CGF->CurFn);
126+
CGF->Builder.CreateCondBr(CMP, LT0, End);
124127

125-
CGF->Builder.CreateCondBr(CMP, LT0, End);
128+
CGF->Builder.SetInsertPoint(LT0);
126129

127-
CGF->Builder.SetInsertPoint(LT0);
130+
CGF->Builder.CreateIntrinsic(CGF->VoidTy, llvm::Intrinsic::spv_clip, {},
131+
nullptr);
128132

129-
CGF->Builder.CreateIntrinsic(CGF->VoidTy, llvm::Intrinsic::spv_clip, {},
130-
nullptr);
133+
LastInstr = CGF->Builder.CreateBr(End);
131134

132-
auto *BrCall = CGF->Builder.CreateBr(End);
135+
CGF->Builder.SetInsertPoint(End);
136+
} else {
137+
llvm_unreachable("Backend Codegen not supported.");
138+
}
133139

134-
CGF->Builder.SetInsertPoint(End);
135-
return BrCall;
140+
return LastInstr;
136141
}
137142

138143
static Value *handleHlslSplitdouble(const CallExpr *E, CodeGenFunction *CGF) {

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class CGHLSLRuntime {
9494
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
9595
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
9696
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
97-
GENERATE_HLSL_INTRINSIC_FUNCTION(Clip, clip)
9897
GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
9998
GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
10099
GENERATE_HLSL_INTRINSIC_FUNCTION(NClamp, nclamp)

llvm/docs/SPIRVUsage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ list of supported SPIR-V extensions, sorted alphabetically by their extension na
149149
- Adds atomic min and max instruction on floating-point numbers.
150150
* - ``SPV_EXT_arithmetic_fence``
151151
- Adds an instruction that prevents fast-math optimizations between its argument and the expression that contains it.
152+
* - ``SPV_EXT_demote_to_helper_invocation``
153+
- Adds an instruction that demotes a fragment shader invocation to a helper invocation.
152154
* - ``SPV_INTEL_arbitrary_precision_integers``
153155
- Allows generating arbitrary width integer types.
154156
* - ``SPV_INTEL_bfloat16_conversion``

llvm/include/llvm/IR/IntrinsicsSPIRV.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let TargetPrefix = "spv" in {
9191
def int_spv_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
9292
def int_spv_radians : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty], [IntrNoMem]>;
9393
def int_spv_group_memory_barrier_with_group_sync : DefaultAttrsIntrinsic<[], [], []>;
94-
def int_spv_clip : DefaultAttrsIntrinsic<[], [llvm_i1_ty], [IntrNoMem]>;
94+
def int_spv_clip : DefaultAttrsIntrinsic<[], [], []>;
9595
def int_spv_uclamp : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
9696
def int_spv_sclamp : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
9797
def int_spv_nclamp : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -871,15 +871,6 @@ def WaveIsFirstLane : DXILOp<110, waveIsFirstLane> {
871871
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
872872
}
873873

874-
def WaveGetLaneIndex : DXILOp<111, waveGetLaneIndex> {
875-
let Doc = "returns the index of the current lane in the wave";
876-
let LLVMIntrinsic = int_dx_wave_getlaneindex;
877-
let arguments = [];
878-
let result = Int32Ty;
879-
let stages = [Stages<DXIL1_0, [all_stages]>];
880-
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
881-
}
882-
883874
def WaveReadLaneAt: DXILOp<117, waveReadLaneAt> {
884875
let Doc = "returns the value from the specified lane";
885876
let LLVMIntrinsic = int_dx_wave_readlane;
@@ -890,24 +881,10 @@ def WaveReadLaneAt: DXILOp<117, waveReadLaneAt> {
890881
let attributes = [Attributes<DXIL1_0, [ReadNone]>];
891882
}
892883

893-
def AnnotateHandle : DXILOp<217, annotateHandle> {
894-
let Doc = "annotate handle with resource properties";
895-
let arguments = [HandleTy, ResPropsTy];
896-
let result = HandleTy;
897-
let stages = [Stages<DXIL1_6, [all_stages]>];
898-
}
899-
900-
def CreateHandleFromBinding : DXILOp<218, createHandleFromBinding> {
901-
let Doc = "create resource handle from binding";
902-
let arguments = [ResBindTy, Int32Ty, Int1Ty];
903-
let result = HandleTy;
904-
let stages = [Stages<DXIL1_6, [all_stages]>];
905-
}
906-
907-
def WaveAllBitCount : DXILOp<135, waveAllOp> {
908-
let Doc = "returns the count of bits set to 1 across the wave";
909-
let LLVMIntrinsic = int_dx_wave_active_countbits;
910-
let arguments = [Int1Ty];
884+
def WaveGetLaneIndex : DXILOp<111, waveGetLaneIndex> {
885+
let Doc = "returns the index of the current lane in the wave";
886+
let LLVMIntrinsic = int_dx_wave_getlaneindex;
887+
let arguments = [];
911888
let result = Int32Ty;
912889
let stages = [Stages<DXIL1_0, [all_stages]>];
913890
let attributes = [Attributes<DXIL1_0, [ReadNone]>];

llvm/lib/Target/SPIRV/SPIRVCommandLine.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static const std::map<std::string, SPIRV::Extension::Extension>
3030
SPIRV::Extension::Extension::SPV_EXT_shader_atomic_float_min_max},
3131
{"SPV_EXT_arithmetic_fence",
3232
SPIRV::Extension::Extension::SPV_EXT_arithmetic_fence},
33+
{"SPV_EXT_demote_to_helper_invocation",
34+
SPIRV::Extension::Extension::SPV_EXT_demote_to_helper_invocation},
3335
{"SPV_INTEL_arbitrary_precision_integers",
3436
SPIRV::Extension::Extension::SPV_INTEL_arbitrary_precision_integers},
3537
{"SPV_INTEL_cache_controls",
@@ -75,9 +77,7 @@ static const std::map<std::string, SPIRV::Extension::Extension>
7577
{"SPV_KHR_cooperative_matrix",
7678
SPIRV::Extension::Extension::SPV_KHR_cooperative_matrix},
7779
{"SPV_KHR_non_semantic_info",
78-
SPIRV::Extension::Extension::SPV_KHR_non_semantic_info},
79-
{"SPV_EXT_demote_to_helper_invocation",
80-
SPIRV::Extension::Extension::SPV_EXT_demote_to_helper_invocation}};
80+
SPIRV::Extension::Extension::SPV_KHR_non_semantic_info}};
8181

8282
bool SPIRVExtensionsParser::parse(cl::Option &O, llvm::StringRef ArgName,
8383
llvm::StringRef ArgValue,

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,13 +2163,9 @@ bool SPIRVInstructionSelector::selectClip(Register ResVReg,
21632163

21642164
unsigned Opcode;
21652165

2166-
if (STI.isAtLeastSPIRVVer(VersionTuple(1, 6))) {
2167-
if (!STI.canUseExtension(
2168-
SPIRV::Extension::SPV_EXT_demote_to_helper_invocation))
2169-
report_fatal_error(
2170-
"llvm.spv.clip intrinsic: this instruction requires the following "
2171-
"SPIR-V extension: SPV_EXT_demote_to_helper_invocation",
2172-
false);
2166+
if (STI.canUseExtension(
2167+
SPIRV::Extension::SPV_EXT_demote_to_helper_invocation) ||
2168+
STI.isAtLeastSPIRVVer(llvm::VersionTuple(1, 6))) {
21732169
Opcode = SPIRV::OpDemoteToHelperInvocation;
21742170
} else {
21752171
Opcode = SPIRV::OpKill;

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ void RequirementHandler::initAvailableCapabilities(const SPIRVSubtarget &ST) {
633633
if (ST.isAtLeastSPIRVVer(VersionTuple(1, 6)))
634634
addAvailableCaps({Capability::DotProduct, Capability::DotProductInputAll,
635635
Capability::DotProductInput4x8Bit,
636-
Capability::DotProductInput4x8BitPacked});
636+
Capability::DotProductInput4x8BitPacked,
637+
Capability::DemoteToHelperInvocation});
637638

638639
// Add capabilities enabled by extensions.
639640
for (auto Extension : ST.getAllAvailableExtensions()) {
@@ -1407,21 +1408,23 @@ void addInstrRequirements(const MachineInstr &MI,
14071408
Reqs.addCapability(SPIRV::Capability::SplitBarrierINTEL);
14081409
}
14091410
break;
1410-
case SPIRV::OpSDot:
1411-
case SPIRV::OpUDot:
1412-
AddDotProductRequirements(MI, Reqs, ST);
1413-
break;
1414-
14151411
case SPIRV::OpKill: {
14161412
Reqs.addCapability(SPIRV::Capability::Shader);
14171413
} break;
14181414
case SPIRV::OpDemoteToHelperInvocation:
1415+
Reqs.addCapability(SPIRV::Capability::DemoteToHelperInvocation);
1416+
14191417
if (ST.canUseExtension(
14201418
SPIRV::Extension::SPV_EXT_demote_to_helper_invocation)) {
1421-
Reqs.addExtension(SPIRV::Extension::SPV_EXT_demote_to_helper_invocation);
1422-
Reqs.addCapability(SPIRV::Capability::DemoteToHelperInvocation);
1419+
if (!ST.isAtLeastSPIRVVer(llvm::VersionTuple(1, 6)))
1420+
Reqs.addExtension(
1421+
SPIRV::Extension::SPV_EXT_demote_to_helper_invocation);
14231422
}
14241423
break;
1424+
case SPIRV::OpSDot:
1425+
case SPIRV::OpUDot:
1426+
AddDotProductRequirements(MI, Reqs, ST);
1427+
break;
14251428
default:
14261429
break;
14271430
}

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ defm VulkanMemoryModelDeviceScopeKHR : CapabilityOperand<5346, 0, 0, [], []>;
456456
defm ImageFootprintNV : CapabilityOperand<5282, 0, 0, [], []>;
457457
defm FragmentBarycentricNV : CapabilityOperand<5284, 0, 0, [], []>;
458458
defm ComputeDerivativeGroupQuadsNV : CapabilityOperand<5288, 0, 0, [], []>;
459-
defm DemoteToHelperInvocation : CapabilityOperand<5379, 0, 0, [SPV_EXT_demote_to_helper_invocation], []>;
459+
defm DemoteToHelperInvocation : CapabilityOperand<5379, 0x10600, 0, [SPV_EXT_demote_to_helper_invocation], []>;
460460
defm ComputeDerivativeGroupLinearNV : CapabilityOperand<5350, 0, 0, [], []>;
461461
defm FragmentDensityEXT : CapabilityOperand<5291, 0, 0, [], [Shader]>;
462462
defm PhysicalStorageBufferAddressesEXT : CapabilityOperand<5347, 0, 0, [], [Shader]>;

llvm/test/CodeGen/SPIRV/hlsl-intrinsics/clip.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPIRV15
2-
; RUN: llc -verify-machineinstrs -spirv-ext=+SPV_EXT_demote_to_helper_invocation -O0 -mtriple=spirv32v1.6-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPIRV16
3-
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv1.5-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPIRV15
2+
; RUN: llc -verify-machineinstrs -spirv-ext=+SPV_EXT_demote_to_helper_invocation -O0 -mtriple=spirv1.5-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPIRV16,WITH-EXTENSION,WITH-CAPABILITY
3+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv1.6-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPIRV16,WITH-CAPABILITY
4+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.5-unknown-unknown %s -o - -filetype=obj | spirv-val %}
5+
; RUN: %if spirv-tools %{ llc -O0 -spirv-ext=+SPV_EXT_demote_to_helper_invocation -mtriple=spirv1.5-unknown-unknown %s -o - -filetype=obj | spirv-val %}
6+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-unknown-unknown %s -o - -filetype=obj | spirv-val %}
47

58

69
; Make sure lowering is correctly generating spirv code.
710

11+
; WITH-CAPABILITY-DAG: OpCapability DemoteToHelperInvocation
12+
; WITH-EXTENSION-DAG: OpExtension "SPV_EXT_demote_to_helper_invocation"
13+
814
; CHECK-DAG: %[[#float:]] = OpTypeFloat 32
915
; CHECK-DAG: %[[#void:]] = OpTypeVoid
1016
; CHECK-DAG: %[[#bool:]] = OpTypeBool
1117
; CHECK-DAG: %[[#v4bool:]] = OpTypeVector %[[#bool]] 4
1218
; CHECK-DAG: %[[#v4float:]] = OpTypeVector %[[#float]] 4
1319
; CHECK-DAG: %[[#fzero:]] = OpConstant %[[#float]] 0
1420
; CHECK-DAG: %[[#v4fzero:]] = OpConstantNull %[[#v4float]]
15-
; SPIRV16-DAG: %[[#vecfuncopptr:]] = OpTypePointer Function %[[#v4float]]
16-
; SPIRV16-DAG: %[[#funcopptr:]] = OpTypePointer Function %[[#float]]
1721

1822
define void @test_scalar(float noundef %Buf) {
1923
entry:
2024
; CHECK-LABEL: ; -- Begin function test_scalar
21-
; SPIRV16: %[[#param:]] = OpVariable %[[#funcopptr]] Function
22-
; SPIRV16: %[[#load:]] = OpLoad %[[#float]] %[[#param]] Aligned 4
23-
; SPIRV15: %[[#load:]] = OpFunctionParameter %[[#float]]
25+
; CHECK: %[[#load:]] = OpFunctionParameter %[[#float]]
2426
; CHECK: %[[#cmplt:]] = OpFOrdLessThan %[[#bool]] %[[#load]] %[[#fzero]]
2527
; CHECK: OpBranchConditional %[[#cmplt]] %[[#truel:]] %[[#endl:]]
2628
; CHECK: %[[#truel]] = OpLabel
@@ -48,9 +50,7 @@ declare void @llvm.spv.clip()
4850
define void @test_vector(<4 x float> noundef %Buf) {
4951
entry:
5052
; CHECK-LABEL: ; -- Begin function test_vector
51-
; SPIRV16: %[[#param:]] = OpVariable %[[#vecfuncopptr]] Function
52-
; SPIRV16: %[[#loadvec:]] = OpLoad %[[#v4float]] %[[#param]] Aligned 16
53-
; SPIRV15: %[[#loadvec:]] = OpFunctionParameter %[[#v4float]]
53+
; CHECK: %[[#loadvec:]] = OpFunctionParameter %[[#v4float]]
5454
; CHECK: %[[#cmplt:]] = OpFOrdLessThan %[[#v4bool]] %[[#loadvec]] %[[#v4fzero]]
5555
; CHECK: %[[#opany:]] = OpAny %[[#bool]] %[[#cmplt]]
5656
; CHECK: OpBranchConditional %[[#opany]] %[[#truel:]] %[[#endl:]]

0 commit comments

Comments
 (0)