Skip to content

Commit bac6ca7

Browse files
Support optimization of OpCopyLogical (#6016)
* Adds support for OpCopyLogical * Adds tests for OpCopyLogical --------- Co-authored-by: Zackery Mason-Blaug <zackery.mason-blaug@ntd.nintendo.com>
1 parent 7e3efaf commit bac6ca7

File tree

4 files changed

+261
-0
lines changed

4 files changed

+261
-0
lines changed

source/opt/copy_prop_arrays.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ CopyPropagateArrays::GetSourceObjectIfAny(uint32_t result) {
276276
case spv::Op::OpCompositeConstruct:
277277
return BuildMemoryObjectFromCompositeConstruct(result_inst);
278278
case spv::Op::OpCopyObject:
279+
case spv::Op::OpCopyLogical:
279280
return GetSourceObjectIfAny(result_inst->GetSingleWordInOperand(0));
280281
case spv::Op::OpCompositeInsert:
281282
return BuildMemoryObjectFromInsert(result_inst);

source/opt/ir_context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ void IRContext::AddCombinatorsForCapability(uint32_t capability) {
564564
(uint32_t)spv::Op::OpCompositeConstruct,
565565
(uint32_t)spv::Op::OpCompositeExtract,
566566
(uint32_t)spv::Op::OpCompositeInsert,
567+
(uint32_t)spv::Op::OpCopyLogical,
567568
(uint32_t)spv::Op::OpCopyObject,
568569
(uint32_t)spv::Op::OpTranspose,
569570
(uint32_t)spv::Op::OpSampledImage,

test/opt/aggressive_dead_code_elim_test.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8297,6 +8297,185 @@ OpFunctionEnd
82978297
HasSubstr("OpSource HLSL 600 %5 \"#define UBER_TYPE(x) x ## Type"));
82988298
}
82998299

8300+
TEST_F(AggressiveDCETest, EliminateCopyLogical) {
8301+
const std::string before = R"(
8302+
; CHECK: [[float32:%\w+]] = OpTypeFloat 32
8303+
; CHECK: [[v4float:%\w+]] = OpTypeVector [[float32]] 4
8304+
; CHECK-NOT: %10 = OpTypeArray [[v4float]] %9
8305+
; CHECK-NOT: %11 = OpTypeStruct %10 %10
8306+
; CHECK-NOT: %22 = OpTypePointer Uniform %16
8307+
; CHECK-NOT: %38 = OpTypePointer Function [[v4float]]
8308+
; CHECK-NOT: %43 = OpTypePointer Function %10
8309+
; CHECK-NOT: %44 = OpVariable %42 Function
8310+
; CHECK-NOT: %23 = OpAccessChain %22 %19 %21
8311+
; CHECK-NOT: %24 = OpLoad %16 %23
8312+
; CHECK-NOT: %25 = OpCopyLogical %11 %24
8313+
; CHECK-NOT: %46 = OpCompositeExtract %10 %25 0
8314+
; CHECK-NOT: OpStore %44 %46
8315+
OpCapability Shader
8316+
%1 = OpExtInstImport "GLSL.std.450"
8317+
OpMemoryModel Logical GLSL450
8318+
OpEntryPoint Vertex %4 "main" %19 %30 %32
8319+
OpSource GLSL 430
8320+
OpName %4 "main"
8321+
OpDecorate %14 ArrayStride 16
8322+
OpDecorate %15 ArrayStride 16
8323+
OpMemberDecorate %16 0 Offset 0
8324+
OpMemberDecorate %16 1 Offset 32
8325+
OpDecorate %17 Block
8326+
OpMemberDecorate %17 0 Offset 0
8327+
OpDecorate %19 Binding 0
8328+
OpDecorate %19 DescriptorSet 0
8329+
OpDecorate %28 Block
8330+
OpMemberDecorate %28 0 BuiltIn Position
8331+
OpMemberDecorate %28 1 BuiltIn PointSize
8332+
OpMemberDecorate %28 2 BuiltIn ClipDistance
8333+
OpDecorate %32 Location 0
8334+
%2 = OpTypeVoid
8335+
%3 = OpTypeFunction %2
8336+
%6 = OpTypeFloat 32
8337+
%7 = OpTypeVector %6 4
8338+
%8 = OpTypeInt 32 0
8339+
%9 = OpConstant %8 2
8340+
%10 = OpTypeArray %7 %9
8341+
%11 = OpTypeStruct %10 %10
8342+
%14 = OpTypeArray %7 %9
8343+
%15 = OpTypeArray %7 %9
8344+
%16 = OpTypeStruct %14 %15
8345+
%17 = OpTypeStruct %16
8346+
%18 = OpTypePointer Uniform %17
8347+
%19 = OpVariable %18 Uniform
8348+
%20 = OpTypeInt 32 1
8349+
%21 = OpConstant %20 0
8350+
%22 = OpTypePointer Uniform %16
8351+
%26 = OpConstant %8 1
8352+
%27 = OpTypeArray %6 %26
8353+
%28 = OpTypeStruct %7 %6 %27
8354+
%29 = OpTypePointer Output %28
8355+
%30 = OpVariable %29 Output
8356+
%31 = OpTypePointer Input %7
8357+
%32 = OpVariable %31 Input
8358+
%33 = OpConstant %8 0
8359+
%34 = OpTypePointer Input %6
8360+
%38 = OpTypePointer Function %7
8361+
%41 = OpTypePointer Output %7
8362+
%43 = OpTypePointer Function %10
8363+
%48 = OpTypePointer Uniform %14
8364+
%49 = OpTypePointer Uniform %7
8365+
%4 = OpFunction %2 None %3
8366+
%5 = OpLabel
8367+
%44 = OpVariable %43 Function
8368+
%23 = OpAccessChain %22 %19 %21
8369+
%24 = OpLoad %16 %23
8370+
%25 = OpCopyLogical %11 %24
8371+
%46 = OpCompositeExtract %10 %25 0
8372+
%50 = OpAccessChain %48 %19 %21 %33
8373+
OpStore %44 %46
8374+
%35 = OpAccessChain %34 %32 %33
8375+
%36 = OpLoad %6 %35
8376+
%37 = OpConvertFToS %20 %36
8377+
%47 = OpAccessChain %49 %50 %37
8378+
%40 = OpLoad %7 %47
8379+
%42 = OpAccessChain %41 %30 %21
8380+
OpStore %42 %40
8381+
OpReturn
8382+
OpFunctionEnd
8383+
)";
8384+
8385+
SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8386+
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8387+
SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
8388+
SinglePassRunAndMatch<AggressiveDCEPass>(before, true);
8389+
}
8390+
8391+
TEST_F(AggressiveDCETest, KeepCopyLogical) {
8392+
const std::string before = R"(
8393+
; CHECK: OpCopyLogical
8394+
OpCapability Shader
8395+
%1 = OpExtInstImport "GLSL.std.450"
8396+
OpMemoryModel Logical GLSL450
8397+
OpEntryPoint GLCompute %4 "main" %15 %23 %38
8398+
OpExecutionMode %4 LocalSize 32 32 1
8399+
OpSource GLSL 430
8400+
OpName %4 "main"
8401+
OpDecorate %10 ArrayStride 16
8402+
OpDecorate %11 ArrayStride 16
8403+
OpMemberDecorate %12 0 Offset 0
8404+
OpMemberDecorate %12 1 Offset 2048
8405+
OpDecorate %13 Block
8406+
OpMemberDecorate %13 0 NonReadable
8407+
OpMemberDecorate %13 0 Offset 0
8408+
OpDecorate %15 NonReadable
8409+
OpDecorate %15 Binding 1
8410+
OpDecorate %15 DescriptorSet 0
8411+
OpDecorate %18 ArrayStride 16
8412+
OpDecorate %19 ArrayStride 16
8413+
OpMemberDecorate %20 0 Offset 0
8414+
OpMemberDecorate %20 1 Offset 2048
8415+
OpDecorate %21 Block
8416+
OpMemberDecorate %21 0 NonWritable
8417+
OpMemberDecorate %21 0 Offset 0
8418+
OpDecorate %23 NonWritable
8419+
OpDecorate %23 Binding 0
8420+
OpDecorate %23 DescriptorSet 0
8421+
OpDecorate %30 ArrayStride 16
8422+
OpDecorate %31 ArrayStride 16
8423+
OpMemberDecorate %32 0 Offset 0
8424+
OpMemberDecorate %32 1 Offset 2048
8425+
OpDecorate %34 ArrayStride 4096
8426+
OpMemberDecorate %35 0 Offset 0
8427+
OpDecorate %36 Block
8428+
OpMemberDecorate %36 0 Offset 0
8429+
OpDecorate %38 Binding 0
8430+
OpDecorate %38 DescriptorSet 0
8431+
%2 = OpTypeVoid
8432+
%3 = OpTypeFunction %2
8433+
%6 = OpTypeFloat 32
8434+
%7 = OpTypeVector %6 4
8435+
%8 = OpTypeInt 32 0
8436+
%9 = OpConstant %8 128
8437+
%10 = OpTypeArray %7 %9
8438+
%11 = OpTypeArray %7 %9
8439+
%12 = OpTypeStruct %10 %11
8440+
%13 = OpTypeStruct %12
8441+
%14 = OpTypePointer StorageBuffer %13
8442+
%15 = OpVariable %14 StorageBuffer
8443+
%16 = OpTypeInt 32 1
8444+
%17 = OpConstant %16 0
8445+
%18 = OpTypeArray %7 %9
8446+
%19 = OpTypeArray %7 %9
8447+
%20 = OpTypeStruct %18 %19
8448+
%21 = OpTypeStruct %20
8449+
%22 = OpTypePointer StorageBuffer %21
8450+
%23 = OpVariable %22 StorageBuffer
8451+
%24 = OpTypePointer StorageBuffer %20
8452+
%27 = OpTypePointer StorageBuffer %12
8453+
%30 = OpTypeArray %7 %9
8454+
%31 = OpTypeArray %7 %9
8455+
%32 = OpTypeStruct %30 %31
8456+
%33 = OpConstant %8 8
8457+
%34 = OpTypeArray %32 %33
8458+
%35 = OpTypeStruct %34
8459+
%36 = OpTypeStruct %35
8460+
%37 = OpTypePointer Uniform %36
8461+
%38 = OpVariable %37 Uniform
8462+
%4 = OpFunction %2 None %3
8463+
%5 = OpLabel
8464+
%25 = OpAccessChain %24 %23 %17
8465+
%26 = OpLoad %20 %25
8466+
%28 = OpAccessChain %27 %15 %17
8467+
%29 = OpCopyLogical %12 %26
8468+
OpStore %28 %29
8469+
OpReturn
8470+
OpFunctionEnd
8471+
)";
8472+
8473+
SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
8474+
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8475+
SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
8476+
SinglePassRunAndMatch<AggressiveDCEPass>(before, true);
8477+
}
8478+
83008479
} // namespace
83018480
} // namespace opt
83028481
} // namespace spvtools

test/opt/copy_prop_array_test.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,86 @@ OpFunctionEnd
21462146
SetTargetEnv(SPV_ENV_UNIVERSAL_1_4);
21472147
SinglePassRunAndMatch<CopyPropagateArrays>(before, true);
21482148
}
2149+
2150+
TEST_F(CopyPropArrayPassTest, PropCopyLogical) {
2151+
const std::string before = R"(
2152+
; CHECK: [[v4array_ptr:%\w+]] = OpTypePointer Uniform %14
2153+
; CHECK: [[v4_ptr:%\w+]] = OpTypePointer Uniform %7
2154+
; CHECK: [[ac:%\w+]] = OpAccessChain [[v4array_ptr]] %19 %21 %33
2155+
; CHECK: %47 = OpAccessChain [[v4_ptr]] [[ac]] %37
2156+
OpCapability Shader
2157+
%1 = OpExtInstImport "GLSL.std.450"
2158+
OpMemoryModel Logical GLSL450
2159+
OpEntryPoint Vertex %4 "main" %19 %30 %32
2160+
OpSource GLSL 430
2161+
OpName %4 "main"
2162+
OpDecorate %14 ArrayStride 16
2163+
OpDecorate %15 ArrayStride 16
2164+
OpMemberDecorate %16 0 Offset 0
2165+
OpMemberDecorate %16 1 Offset 32
2166+
OpDecorate %17 Block
2167+
OpMemberDecorate %17 0 Offset 0
2168+
OpDecorate %19 Binding 0
2169+
OpDecorate %19 DescriptorSet 0
2170+
OpDecorate %28 Block
2171+
OpMemberDecorate %28 0 BuiltIn Position
2172+
OpMemberDecorate %28 1 BuiltIn PointSize
2173+
OpMemberDecorate %28 2 BuiltIn ClipDistance
2174+
OpDecorate %32 Location 0
2175+
%2 = OpTypeVoid
2176+
%3 = OpTypeFunction %2
2177+
%6 = OpTypeFloat 32
2178+
%7 = OpTypeVector %6 4
2179+
%8 = OpTypeInt 32 0
2180+
%9 = OpConstant %8 2
2181+
%10 = OpTypeArray %7 %9
2182+
%11 = OpTypeStruct %10 %10
2183+
%14 = OpTypeArray %7 %9
2184+
%15 = OpTypeArray %7 %9
2185+
%16 = OpTypeStruct %14 %15
2186+
%17 = OpTypeStruct %16
2187+
%18 = OpTypePointer Uniform %17
2188+
%19 = OpVariable %18 Uniform
2189+
%20 = OpTypeInt 32 1
2190+
%21 = OpConstant %20 0
2191+
%22 = OpTypePointer Uniform %16
2192+
%26 = OpConstant %8 1
2193+
%27 = OpTypeArray %6 %26
2194+
%28 = OpTypeStruct %7 %6 %27
2195+
%29 = OpTypePointer Output %28
2196+
%30 = OpVariable %29 Output
2197+
%31 = OpTypePointer Input %7
2198+
%32 = OpVariable %31 Input
2199+
%33 = OpConstant %8 0
2200+
%34 = OpTypePointer Input %6
2201+
%38 = OpTypePointer Function %7
2202+
%41 = OpTypePointer Output %7
2203+
%43 = OpTypePointer Function %10
2204+
%4 = OpFunction %2 None %3
2205+
%5 = OpLabel
2206+
%44 = OpVariable %43 Function
2207+
%23 = OpAccessChain %22 %19 %21
2208+
%24 = OpLoad %16 %23
2209+
%25 = OpCopyLogical %11 %24
2210+
%46 = OpCompositeExtract %10 %25 0
2211+
OpStore %44 %46
2212+
%35 = OpAccessChain %34 %32 %33
2213+
%36 = OpLoad %6 %35
2214+
%37 = OpConvertFToS %20 %36
2215+
%47 = OpAccessChain %38 %44 %37
2216+
%40 = OpLoad %7 %47
2217+
%42 = OpAccessChain %41 %30 %21
2218+
OpStore %42 %40
2219+
OpReturn
2220+
OpFunctionEnd
2221+
)";
2222+
2223+
SetTargetEnv(SPV_ENV_UNIVERSAL_1_6);
2224+
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2225+
SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
2226+
SinglePassRunAndMatch<CopyPropagateArrays>(before, true);
2227+
}
2228+
21492229
} // namespace
21502230
} // namespace opt
21512231
} // namespace spvtools

0 commit comments

Comments
 (0)