Skip to content

Commit aa1e9a0

Browse files
committed
[SPIRV] Set hasSideEffects flag to false on type and constant opcodes
This change sets the hasSideEffects flag to false on type and constant opcodes so that they can be considered trivially dead if their result is unused. This means that instruction selection will now be able to remove them.
1 parent 6bee6b2 commit aa1e9a0

File tree

4 files changed

+130
-83
lines changed

4 files changed

+130
-83
lines changed

llvm/lib/Target/SPIRV/SPIRVInstrFormats.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class Op<bits<16> Opcode, dag outs, dag ins, string asmstr, list<dag> pattern =
2525
let Pattern = pattern;
2626
}
2727

28+
class PureOp<bits<16> Opcode, dag outs, dag ins, string asmstr,
29+
list<dag> pattern = []> : Op<Opcode, outs, ins, asmstr, pattern> {
30+
let hasSideEffects = 0;
31+
}
32+
2833
class UnknownOp<dag outs, dag ins, string asmstr, list<dag> pattern = []>
2934
: Op<0, outs, ins, asmstr, pattern> {
3035
let isPseudo = 1;

llvm/lib/Target/SPIRV/SPIRVInstrInfo.td

Lines changed: 108 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -163,52 +163,74 @@ def OpExecutionModeId: Op<331, (outs), (ins ID:$entry, ExecutionMode:$mode, vari
163163

164164
// 3.42.6 Type-Declaration Instructions
165165

166-
def OpTypeVoid: Op<19, (outs TYPE:$type), (ins), "$type = OpTypeVoid">;
167-
def OpTypeBool: Op<20, (outs TYPE:$type), (ins), "$type = OpTypeBool">;
168-
def OpTypeInt: Op<21, (outs TYPE:$type), (ins i32imm:$width, i32imm:$signedness),
169-
"$type = OpTypeInt $width $signedness">;
170-
def OpTypeFloat: Op<22, (outs TYPE:$type), (ins i32imm:$width, variable_ops),
171-
"$type = OpTypeFloat $width">;
172-
def OpTypeVector: Op<23, (outs TYPE:$type), (ins TYPE:$compType, i32imm:$compCount),
173-
"$type = OpTypeVector $compType $compCount">;
174-
def OpTypeMatrix: Op<24, (outs TYPE:$type), (ins TYPE:$colType, i32imm:$colCount),
175-
"$type = OpTypeMatrix $colType $colCount">;
176-
def OpTypeImage: Op<25, (outs TYPE:$res), (ins TYPE:$sampTy, Dim:$dim, i32imm:$depth,
177-
i32imm:$arrayed, i32imm:$MS, i32imm:$sampled, ImageFormat:$imFormat, variable_ops),
178-
"$res = OpTypeImage $sampTy $dim $depth $arrayed $MS $sampled $imFormat">;
179-
def OpTypeSampler: Op<26, (outs TYPE:$res), (ins), "$res = OpTypeSampler">;
180-
def OpTypeSampledImage: Op<27, (outs TYPE:$res), (ins TYPE:$imageType),
181-
"$res = OpTypeSampledImage $imageType">;
182-
def OpTypeArray: Op<28, (outs TYPE:$type), (ins TYPE:$elementType, ID:$length),
183-
"$type = OpTypeArray $elementType $length">;
184-
def OpTypeRuntimeArray: Op<29, (outs TYPE:$type), (ins TYPE:$elementType),
185-
"$type = OpTypeRuntimeArray $elementType">;
186-
def OpTypeStruct: Op<30, (outs TYPE:$res), (ins variable_ops), "$res = OpTypeStruct">;
187-
def OpTypeStructContinuedINTEL: Op<6090, (outs), (ins variable_ops),
188-
"OpTypeStructContinuedINTEL">;
189-
def OpTypeOpaque: Op<31, (outs TYPE:$res), (ins StringImm:$name, variable_ops),
190-
"$res = OpTypeOpaque $name">;
191-
def OpTypePointer: Op<32, (outs TYPE:$res), (ins StorageClass:$storage, TYPE:$type),
192-
"$res = OpTypePointer $storage $type">;
193-
def OpTypeFunction: Op<33, (outs TYPE:$funcType), (ins TYPE:$returnType, variable_ops),
194-
"$funcType = OpTypeFunction $returnType">;
195-
def OpTypeEvent: Op<34, (outs TYPE:$res), (ins), "$res = OpTypeEvent">;
196-
def OpTypeDeviceEvent: Op<35, (outs TYPE:$res), (ins), "$res = OpTypeDeviceEvent">;
197-
def OpTypeReserveId: Op<36, (outs TYPE:$res), (ins), "$res = OpTypeReserveId">;
198-
def OpTypeQueue: Op<37, (outs TYPE:$res), (ins), "$res = OpTypeQueue">;
199-
def OpTypePipe: Op<38, (outs TYPE:$res), (ins AccessQualifier:$a), "$res = OpTypePipe $a">;
200-
def OpTypeForwardPointer: Op<39, (outs), (ins TYPE:$ptrType, StorageClass:$storageClass),
201-
"OpTypeForwardPointer $ptrType $storageClass">;
202-
def OpTypePipeStorage: Op<322, (outs TYPE:$res), (ins), "$res = OpTypePipeStorage">;
203-
def OpTypeNamedBarrier: Op<327, (outs TYPE:$res), (ins), "$res = OpTypeNamedBarrier">;
204-
def OpTypeAccelerationStructureNV: Op<5341, (outs TYPE:$res), (ins),
205-
"$res = OpTypeAccelerationStructureNV">;
206-
def OpTypeCooperativeMatrixNV: Op<5358, (outs TYPE:$res),
207-
(ins TYPE:$compType, ID:$scope, ID:$rows, ID:$cols),
208-
"$res = OpTypeCooperativeMatrixNV $compType $scope $rows $cols">;
209-
def OpTypeCooperativeMatrixKHR: Op<4456, (outs TYPE:$res),
210-
(ins TYPE:$compType, ID:$scope, ID:$rows, ID:$cols, ID:$use),
211-
"$res = OpTypeCooperativeMatrixKHR $compType $scope $rows $cols $use">;
166+
def OpTypeVoid : PureOp<19, (outs TYPE:$type), (ins), "$type = OpTypeVoid">;
167+
def OpTypeBool : PureOp<20, (outs TYPE:$type), (ins), "$type = OpTypeBool">;
168+
def OpTypeInt
169+
: PureOp<21, (outs TYPE:$type), (ins i32imm:$width, i32imm:$signedness),
170+
"$type = OpTypeInt $width $signedness">;
171+
def OpTypeFloat
172+
: PureOp<22, (outs TYPE:$type), (ins i32imm:$width, variable_ops),
173+
"$type = OpTypeFloat $width">;
174+
def OpTypeVector
175+
: PureOp<23, (outs TYPE:$type), (ins TYPE:$compType, i32imm:$compCount),
176+
"$type = OpTypeVector $compType $compCount">;
177+
def OpTypeMatrix
178+
: PureOp<24, (outs TYPE:$type), (ins TYPE:$colType, i32imm:$colCount),
179+
"$type = OpTypeMatrix $colType $colCount">;
180+
def OpTypeImage : PureOp<25, (outs TYPE:$res),
181+
(ins TYPE:$sampTy, Dim:$dim, i32imm:$depth,
182+
i32imm:$arrayed, i32imm:$MS, i32imm:$sampled,
183+
ImageFormat:$imFormat, variable_ops),
184+
"$res = OpTypeImage $sampTy $dim $depth $arrayed $MS "
185+
"$sampled $imFormat">;
186+
def OpTypeSampler : PureOp<26, (outs TYPE:$res), (ins), "$res = OpTypeSampler">;
187+
def OpTypeSampledImage : PureOp<27, (outs TYPE:$res), (ins TYPE:$imageType),
188+
"$res = OpTypeSampledImage $imageType">;
189+
def OpTypeArray
190+
: PureOp<28, (outs TYPE:$type), (ins TYPE:$elementType, ID:$length),
191+
"$type = OpTypeArray $elementType $length">;
192+
def OpTypeRuntimeArray : PureOp<29, (outs TYPE:$type), (ins TYPE:$elementType),
193+
"$type = OpTypeRuntimeArray $elementType">;
194+
def OpTypeStruct
195+
: PureOp<30, (outs TYPE:$res), (ins variable_ops), "$res = OpTypeStruct">;
196+
def OpTypeStructContinuedINTEL
197+
: PureOp<6090, (outs), (ins variable_ops), "OpTypeStructContinuedINTEL">;
198+
def OpTypeOpaque
199+
: PureOp<31, (outs TYPE:$res), (ins StringImm:$name, variable_ops),
200+
"$res = OpTypeOpaque $name">;
201+
def OpTypePointer
202+
: PureOp<32, (outs TYPE:$res), (ins StorageClass:$storage, TYPE:$type),
203+
"$res = OpTypePointer $storage $type">;
204+
def OpTypeFunction
205+
: PureOp<33, (outs TYPE:$funcType), (ins TYPE:$returnType, variable_ops),
206+
"$funcType = OpTypeFunction $returnType">;
207+
def OpTypeEvent : PureOp<34, (outs TYPE:$res), (ins), "$res = OpTypeEvent">;
208+
def OpTypeDeviceEvent
209+
: PureOp<35, (outs TYPE:$res), (ins), "$res = OpTypeDeviceEvent">;
210+
def OpTypeReserveId
211+
: PureOp<36, (outs TYPE:$res), (ins), "$res = OpTypeReserveId">;
212+
def OpTypeQueue : PureOp<37, (outs TYPE:$res), (ins), "$res = OpTypeQueue">;
213+
def OpTypePipe : PureOp<38, (outs TYPE:$res), (ins AccessQualifier:$a),
214+
"$res = OpTypePipe $a">;
215+
def OpTypeForwardPointer
216+
: PureOp<39, (outs), (ins TYPE:$ptrType, StorageClass:$storageClass),
217+
"OpTypeForwardPointer $ptrType $storageClass">;
218+
def OpTypePipeStorage
219+
: PureOp<322, (outs TYPE:$res), (ins), "$res = OpTypePipeStorage">;
220+
def OpTypeNamedBarrier
221+
: PureOp<327, (outs TYPE:$res), (ins), "$res = OpTypeNamedBarrier">;
222+
def OpTypeAccelerationStructureNV
223+
: PureOp<5341, (outs TYPE:$res), (ins),
224+
"$res = OpTypeAccelerationStructureNV">;
225+
def OpTypeCooperativeMatrixNV
226+
: PureOp<5358, (outs TYPE:$res),
227+
(ins TYPE:$compType, ID:$scope, ID:$rows, ID:$cols),
228+
"$res = OpTypeCooperativeMatrixNV $compType $scope $rows $cols">;
229+
def OpTypeCooperativeMatrixKHR
230+
: PureOp<4456, (outs TYPE:$res),
231+
(ins TYPE:$compType, ID:$scope, ID:$rows, ID:$cols, ID:$use),
232+
"$res = OpTypeCooperativeMatrixKHR $compType $scope $rows $cols "
233+
"$use">;
212234

213235
// 3.42.7 Constant-Creation Instructions
214236

@@ -222,31 +244,46 @@ defm OpConstant: IntFPImm<43, "OpConstant">;
222244

223245
def ConstPseudoTrue: IntImmLeaf<i64, [{ return Imm.getBitWidth() == 1 && Imm.getZExtValue() == 1; }]>;
224246
def ConstPseudoFalse: IntImmLeaf<i64, [{ return Imm.getBitWidth() == 1 && Imm.getZExtValue() == 0; }]>;
225-
def OpConstantTrue: Op<41, (outs iID:$dst), (ins TYPE:$src_ty), "$dst = OpConstantTrue $src_ty",
226-
[(set iID:$dst, (assigntype ConstPseudoTrue, TYPE:$src_ty))]>;
227-
def OpConstantFalse: Op<42, (outs iID:$dst), (ins TYPE:$src_ty), "$dst = OpConstantFalse $src_ty",
228-
[(set iID:$dst, (assigntype ConstPseudoFalse, TYPE:$src_ty))]>;
229-
230-
def OpConstantComposite: Op<44, (outs ID:$res), (ins TYPE:$type, variable_ops),
231-
"$res = OpConstantComposite $type">;
232-
def OpConstantCompositeContinuedINTEL: Op<6091, (outs), (ins variable_ops),
233-
"OpConstantCompositeContinuedINTEL">;
234-
235-
def OpConstantSampler: Op<45, (outs ID:$res),
236-
(ins TYPE:$t, SamplerAddressingMode:$s, i32imm:$p, SamplerFilterMode:$f),
237-
"$res = OpConstantSampler $t $s $p $f">;
238-
def OpConstantNull: Op<46, (outs ID:$dst), (ins TYPE:$src_ty), "$dst = OpConstantNull $src_ty">;
239-
240-
def OpSpecConstantTrue: Op<48, (outs ID:$r), (ins TYPE:$t), "$r = OpSpecConstantTrue $t">;
241-
def OpSpecConstantFalse: Op<49, (outs ID:$r), (ins TYPE:$t), "$r = OpSpecConstantFalse $t">;
242-
def OpSpecConstant: Op<50, (outs ID:$res), (ins TYPE:$type, i32imm:$imm, variable_ops),
243-
"$res = OpSpecConstant $type $imm">;
244-
def OpSpecConstantComposite: Op<51, (outs ID:$res), (ins TYPE:$type, variable_ops),
245-
"$res = OpSpecConstantComposite $type">;
246-
def OpSpecConstantCompositeContinuedINTEL: Op<6092, (outs), (ins variable_ops),
247-
"OpSpecConstantCompositeContinuedINTEL">;
248-
def OpSpecConstantOp: Op<52, (outs ID:$res), (ins TYPE:$t, SpecConstantOpOperands:$c, ID:$o, variable_ops),
249-
"$res = OpSpecConstantOp $t $c $o">;
247+
def OpConstantTrue
248+
: PureOp<41, (outs iID:$dst), (ins TYPE:$src_ty),
249+
"$dst = OpConstantTrue $src_ty",
250+
[(set iID:$dst, (assigntype ConstPseudoTrue, TYPE:$src_ty))]>;
251+
def OpConstantFalse
252+
: PureOp<42, (outs iID:$dst), (ins TYPE:$src_ty),
253+
"$dst = OpConstantFalse $src_ty",
254+
[(set iID:$dst, (assigntype ConstPseudoFalse, TYPE:$src_ty))]>;
255+
256+
def OpConstantComposite
257+
: PureOp<44, (outs ID:$res), (ins TYPE:$type, variable_ops),
258+
"$res = OpConstantComposite $type">;
259+
def OpConstantCompositeContinuedINTEL
260+
: PureOp<6091, (outs), (ins variable_ops),
261+
"OpConstantCompositeContinuedINTEL">;
262+
263+
def OpConstantSampler : PureOp<45, (outs ID:$res),
264+
(ins TYPE:$t, SamplerAddressingMode:$s,
265+
i32imm:$p, SamplerFilterMode:$f),
266+
"$res = OpConstantSampler $t $s $p $f">;
267+
def OpConstantNull : PureOp<46, (outs ID:$dst), (ins TYPE:$src_ty),
268+
"$dst = OpConstantNull $src_ty">;
269+
270+
def OpSpecConstantTrue
271+
: PureOp<48, (outs ID:$r), (ins TYPE:$t), "$r = OpSpecConstantTrue $t">;
272+
def OpSpecConstantFalse
273+
: PureOp<49, (outs ID:$r), (ins TYPE:$t), "$r = OpSpecConstantFalse $t">;
274+
def OpSpecConstant
275+
: PureOp<50, (outs ID:$res), (ins TYPE:$type, i32imm:$imm, variable_ops),
276+
"$res = OpSpecConstant $type $imm">;
277+
def OpSpecConstantComposite
278+
: PureOp<51, (outs ID:$res), (ins TYPE:$type, variable_ops),
279+
"$res = OpSpecConstantComposite $type">;
280+
def OpSpecConstantCompositeContinuedINTEL
281+
: PureOp<6092, (outs), (ins variable_ops),
282+
"OpSpecConstantCompositeContinuedINTEL">;
283+
def OpSpecConstantOp
284+
: PureOp<52, (outs ID:$res),
285+
(ins TYPE:$t, SpecConstantOpOperands:$c, ID:$o, variable_ops),
286+
"$res = OpSpecConstantOp $t $c $o">;
250287

251288
// 3.42.8 Memory Instructions
252289

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ entry:
6363
; CHECK: %[[#a_high:]] = OpVectorShuffle %[[#vec2_int_32]] %[[#a]] %[[#undef_v4i32]] 1 3
6464
; CHECK: %[[#b_low:]] = OpVectorShuffle %[[#vec2_int_32]] %[[#b]] %[[#undef_v4i32]] 0 2
6565
; CHECK: %[[#b_high:]] = OpVectorShuffle %[[#vec2_int_32]] %[[#b]] %[[#undef_v4i32]] 1 3
66-
; CHECK: %[[#iaddcarry:]] = OpIAddCarry %[[#struct_v2i32_v2i32]] %[[#a_low]] %[[#vec2_int_32]]
66+
; CHECK: %[[#iaddcarry:]] = OpIAddCarry %[[#struct_v2i32_v2i32]] %[[#a_low]] %[[#b_low]]
6767
; CHECK: %[[#lowsum:]] = OpCompositeExtract %[[#vec2_int_32]] %[[#iaddcarry]] 0
6868
; CHECK: %[[#carry:]] = OpCompositeExtract %[[#vec2_int_32]] %[[#iaddcarry]] 1
6969
; CHECK: %[[#carry_ne0:]] = OpINotEqual %[[#vec2_bool]] %[[#carry]] %[[#const_v2i32_0_0]]

llvm/test/CodeGen/SPIRV/pointers/resource-vector-load-store.ll

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
@.str = private unnamed_addr constant [7 x i8] c"buffer\00", align 1
66

7+
; The i64 values in the extracts will be turned
8+
; into immidiate values. There should be no 64-bit
9+
; integers in the module.
10+
; CHECK-NOT: OpTypeInt 64 0
11+
712
define void @main() "hlsl.shader"="pixel" {
8-
; CHECK: %24 = OpFunction %2 None %3 ; -- Begin function main
9-
; CHECK-NEXT: %1 = OpLabel
10-
; CHECK-NEXT: %25 = OpVariable %13 Function %22
11-
; CHECK-NEXT: %26 = OpLoad %7 %23
12-
; CHECK-NEXT: %27 = OpImageRead %5 %26 %15
13-
; CHECK-NEXT: %28 = OpCompositeExtract %4 %27 0
14-
; CHECK-NEXT: %29 = OpCompositeExtract %4 %27 1
15-
; CHECK-NEXT: %30 = OpFAdd %4 %29 %28
16-
; CHECK-NEXT: %31 = OpCompositeInsert %5 %30 %27 0
17-
; CHECK-NEXT: %32 = OpLoad %7 %23
18-
; CHECK-NEXT: OpImageWrite %32 %15 %31
13+
; CHECK: %[[FUNC:[0-9]+]] = OpFunction %[[VOID:[0-9]+]] None %[[FNTYPE:[0-9]+]] ; -- Begin function main
14+
; CHECK-NEXT: %[[LABEL:[0-9]+]] = OpLabel
15+
; CHECK-NEXT: %[[VAR:[0-9]+]] = OpVariable %[[PTR_FN:[a-zA-Z0-9_]+]] Function %[[INIT:[a-zA-Z0-9_]+]]
16+
; CHECK-NEXT: %[[LOAD1:[0-9]+]] = OpLoad %[[IMG_TYPE:[a-zA-Z0-9_]+]] %[[IMG_VAR:[a-zA-Z0-9_]+]]
17+
; CHECK-NEXT: %[[READ:[0-9]+]] = OpImageRead %[[VEC4:[a-zA-Z0-9_]+]] %[[LOAD1]] %[[COORD:[a-zA-Z0-9_]+]]
18+
; CHECK-NEXT: %[[EXTRACT1:[0-9]+]] = OpCompositeExtract %[[FLOAT:[a-zA-Z0-9_]+]] %[[READ]] 0
19+
; CHECK-NEXT: %[[EXTRACT2:[0-9]+]] = OpCompositeExtract %[[FLOAT]] %[[READ]] 1
20+
; CHECK-NEXT: %[[ADD:[0-9]+]] = OpFAdd %[[FLOAT]] %[[EXTRACT2]] %[[EXTRACT1]]
21+
; CHECK-NEXT: %[[INSERT:[0-9]+]] = OpCompositeInsert %[[VEC4]] %[[ADD]] %[[READ]] 0
22+
; CHECK-NEXT: %[[LOAD2:[0-9]+]] = OpLoad %[[IMG_TYPE]] %[[IMG_VAR]]
23+
; CHECK-NEXT: OpImageWrite %[[LOAD2]] %[[COORD]] %[[INSERT]]
1924
; CHECK-NEXT: OpReturn
2025
; CHECK-NEXT: OpFunctionEnd
2126
entry:

0 commit comments

Comments
 (0)