Opt passes should apply to the exported functions (#4554)

This is follow-up to the commit
bd3a271ce3
This commit is contained in:
JiaoluAMD 2021-10-19 01:18:16 +08:00 committed by GitHub
parent 35fd0e17d0
commit 387cae472e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 673 additions and 8 deletions

View File

@ -44,7 +44,7 @@ bool BlockMergePass::MergeBlocks(Function* func) {
Pass::Status BlockMergePass::Process() {
// Process all entry point functions.
ProcessFunction pfn = [this](Function* fp) { return MergeBlocks(fp); };
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}

View File

@ -340,7 +340,7 @@ Pass::Status ConvertToHalfPass::ProcessImpl() {
Pass::ProcessFunction pfn = [this](Function* fp) {
return ProcessFunction(fp);
};
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
// If modified, make sure module has Float16 capability
if (modified) context()->AddCapability(SpvCapabilityFloat16);
// Remove all RelaxedPrecision decorations from instructions and globals

View File

@ -256,7 +256,7 @@ Pass::Status DeadInsertElimPass::Process() {
ProcessFunction pfn = [this](Function* fp) {
return EliminateDeadInserts(fp);
};
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}

View File

@ -65,7 +65,7 @@ Pass::Status InlineExhaustivePass::ProcessImpl() {
status = CombineStatus(status, InlineExhaustive(fp));
return false;
};
context()->ProcessEntryPointCallTree(pfn);
context()->ProcessReachableCallTree(pfn);
return status;
}

View File

@ -105,7 +105,7 @@ Pass::Status InlineOpaquePass::ProcessImpl() {
status = CombineStatus(status, InlineOpaque(fp));
return false;
};
context()->ProcessEntryPointCallTree(pfn);
context()->ProcessReachableCallTree(pfn);
return status;
}

View File

@ -223,7 +223,7 @@ Pass::Status LocalSingleBlockLoadStoreElimPass::ProcessImpl() {
return LocalSingleBlockLoadStoreElim(fp);
};
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}

View File

@ -81,7 +81,7 @@ Pass::Status LocalSingleStoreElimPass::ProcessImpl() {
ProcessFunction pfn = [this](Function* fp) {
return LocalSingleStoreElim(fp);
};
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}

View File

@ -76,7 +76,7 @@ Pass::Status RelaxFloatOpsPass::ProcessImpl() {
Pass::ProcessFunction pfn = [this](Function* fp) {
return ProcessFunction(fp);
};
bool modified = context()->ProcessEntryPointCallTree(pfn);
bool modified = context()->ProcessReachableCallTree(pfn);
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
}

View File

@ -90,6 +90,63 @@ OpFunctionEnd
true);
}
TEST_F(BlockMergeTest, BlockMergeForLinkage) {
const std::string before =
R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%8 = OpTypeFunction %v4float %_ptr_Function_v4float
%main = OpFunction %v4float None %8
%BaseColor = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%9 = OpLoad %v4float %BaseColor
OpStore %v %9
OpBranch %10
%10 = OpLabel
%11 = OpLoad %v4float %v
OpBranch %12
%12 = OpLabel
OpReturnValue %11
OpFunctionEnd
)";
const std::string after =
R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%8 = OpTypeFunction %v4float %_ptr_Function_v4float
%main = OpFunction %v4float None %8
%BaseColor = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%9 = OpLoad %v4float %BaseColor
OpStore %v %9
%11 = OpLoad %v4float %v
OpReturnValue %11
OpFunctionEnd
)";
SinglePassRunAndCheck<BlockMergePass>(before, after, true, true);
}
TEST_F(BlockMergeTest, EmptyBlock) {
// Note: SPIR-V hand edited to insert empty block
// after two statements in main.

View File

@ -204,6 +204,98 @@ OpFunctionEnd
defs_after + func_after, true, true);
}
TEST_F(ConvertToHalfTest, ConvertToHalfForLinkage) {
const std::string before =
R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %type_cbuff "type.cbuff"
OpMemberName %type_cbuff 0 "c"
OpName %cbuff "cbuff"
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
OpDecorate %cbuff DescriptorSet 0
OpDecorate %cbuff Binding 0
OpMemberDecorate %type_cbuff 0 Offset 0
OpDecorate %type_cbuff Block
OpDecorate %18 RelaxedPrecision
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%float = OpTypeFloat 32
%type_cbuff = OpTypeStruct %float
%_ptr_Uniform_type_cbuff = OpTypePointer Uniform %type_cbuff
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%9 = OpTypeFunction %v4float %_ptr_Function_v4float
%_ptr_Uniform_float = OpTypePointer Uniform %float
%cbuff = OpVariable %_ptr_Uniform_type_cbuff Uniform
%main = OpFunction %v4float None %9
%BaseColor = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%14 = OpLoad %v4float %BaseColor
%16 = OpAccessChain %_ptr_Uniform_float %cbuff %int_0
%17 = OpLoad %float %16
%18 = OpVectorTimesScalar %v4float %14 %17
OpStore %v %18
%19 = OpLoad %v4float %v
OpReturnValue %19
OpFunctionEnd
)";
const std::string after =
R"(OpCapability Shader
OpCapability Linkage
OpCapability Float16
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %type_cbuff "type.cbuff"
OpMemberName %type_cbuff 0 "c"
OpName %cbuff "cbuff"
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
OpDecorate %cbuff DescriptorSet 0
OpDecorate %cbuff Binding 0
OpMemberDecorate %type_cbuff 0 Offset 0
OpDecorate %type_cbuff Block
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%float = OpTypeFloat 32
%type_cbuff = OpTypeStruct %float
%_ptr_Uniform_type_cbuff = OpTypePointer Uniform %type_cbuff
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%14 = OpTypeFunction %v4float %_ptr_Function_v4float
%_ptr_Uniform_float = OpTypePointer Uniform %float
%cbuff = OpVariable %_ptr_Uniform_type_cbuff Uniform
%half = OpTypeFloat 16
%v4half = OpTypeVector %half 4
%main = OpFunction %v4float None %14
%BaseColor = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%16 = OpLoad %v4float %BaseColor
%17 = OpAccessChain %_ptr_Uniform_float %cbuff %int_0
%18 = OpLoad %float %17
%22 = OpFConvert %v4half %16
%23 = OpFConvert %half %18
%7 = OpVectorTimesScalar %v4half %22 %23
%24 = OpFConvert %v4float %7
OpStore %v %24
%19 = OpLoad %v4float %v
OpReturnValue %19
OpFunctionEnd
)";
SinglePassRunAndCheck<ConvertToHalfPass>(before, after, true, true);
}
TEST_F(ConvertToHalfTest, ConvertToHalfWithDrefSample) {
// The resulting SPIR-V was processed with --relax-float-ops.
//

View File

@ -170,6 +170,72 @@ OpFunctionEnd
after_predefs + after, true, true);
}
TEST_F(DeadInsertElimTest, DeadInsertForLinkage) {
const std::string before =
R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
%int = OpTypeInt 32 1
%int_1 = OpConstant %int 1
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%int_0 = OpConstant %int 0
%float = OpTypeFloat 32
%float_0 = OpConstant %float 0
%v2float = OpTypeVector %float 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%14 = OpTypeFunction %v2float %_ptr_Function_v2float
%_ptr_Function_float = OpTypePointer Function %float
%main = OpFunction %v2float None %14
%BaseColor = OpFunctionParameter %_ptr_Function_v2float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v2float Function
%16 = OpLoad %v2float %v
%17 = OpAccessChain %_ptr_Function_float %BaseColor %int_1
%18 = OpLoad %float %17
%19 = OpCompositeInsert %v2float %18 %16 0
%20 = OpCompositeInsert %v2float %float_0 %19 0
OpReturnValue %20
OpFunctionEnd
)";
const std::string after =
R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %BaseColor "BaseColor"
OpName %bb_entry "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
%int = OpTypeInt 32 1
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%int_0 = OpConstant %int 0
%float = OpTypeFloat 32
%float_0 = OpConstant %float 0
%v2float = OpTypeVector %float 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%14 = OpTypeFunction %v2float %_ptr_Function_v2float
%_ptr_Function_float = OpTypePointer Function %float
%main = OpFunction %v2float None %14
%BaseColor = OpFunctionParameter %_ptr_Function_v2float
%bb_entry = OpLabel
%v = OpVariable %_ptr_Function_v2float Function
%16 = OpLoad %v2float %v
%20 = OpCompositeInsert %v2float %float_0 %16 0
OpReturnValue %20
OpFunctionEnd
)";
SinglePassRunAndCheck<DeadInsertElimPass>(before, after, true, true);
}
TEST_F(DeadInsertElimTest, DeadInsertInChainWithPhi) {
// Dead insert eliminated with phi in insertion chain.
//

View File

@ -226,6 +226,115 @@ OpFunctionEnd
predefs + before + post_defs, predefs + after + post_defs, true, true);
}
TEST_F(InlineOpaqueTest, InlineOpaqueForLinkage) {
const std::string predefs_1 =
R"(OpCapability Shader
OpCapability Linkage
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %S_t "S_t"
OpMemberName %S_t 0 "v0"
OpMemberName %S_t 1 "v1"
OpMemberName %S_t 2 "smp"
OpName %foo_struct_S_t_vf2_vf21_ "foo(struct-S_t-vf2-vf21;"
OpName %s "s"
OpName %outColor "outColor"
OpName %sampler15 "sampler15"
OpName %s0 "s0"
OpName %texCoords "texCoords"
OpName %param "param"
OpDecorate %main LinkageAttributes "main" Export
)";
const std::string name = R"(OpName %return_value "return_value"
)";
const std::string predefs_2 = R"(OpDecorate %sampler15 DescriptorSet 0
%void = OpTypeVoid
%13 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%outColor = OpVariable %_ptr_Output_v4float Output
%18 = OpTypeImage %float 2D 0 0 0 1 Unknown
%19 = OpTypeSampledImage %18
%S_t = OpTypeStruct %v2float %v2float %19
%_ptr_Function_S_t = OpTypePointer Function %S_t
%21 = OpTypeFunction %void %_ptr_Function_S_t
%_ptr_UniformConstant_19 = OpTypePointer UniformConstant %19
%_ptr_Function_19 = OpTypePointer Function %19
%sampler15 = OpVariable %_ptr_UniformConstant_19 UniformConstant
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%int_2 = OpConstant %int 2
%_ptr_Function_v2float = OpTypePointer Function %v2float
%_ptr_Input_v2float = OpTypePointer Input %v2float
%texCoords = OpVariable %_ptr_Input_v2float Input
)";
const std::string before =
R"(%main = OpFunction %void None %13
%29 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%param = OpVariable %_ptr_Function_S_t Function
%30 = OpLoad %v2float %texCoords
%31 = OpAccessChain %_ptr_Function_v2float %s0 %int_0
OpStore %31 %30
%32 = OpLoad %19 %sampler15
%33 = OpAccessChain %_ptr_Function_19 %s0 %int_2
OpStore %33 %32
%34 = OpLoad %S_t %s0
OpStore %param %34
%return_value = OpFunctionCall %void %foo_struct_S_t_vf2_vf21_ %param
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %13
%29 = OpLabel
%s0 = OpVariable %_ptr_Function_S_t Function
%param = OpVariable %_ptr_Function_S_t Function
%30 = OpLoad %v2float %texCoords
%31 = OpAccessChain %_ptr_Function_v2float %s0 %int_0
OpStore %31 %30
%32 = OpLoad %19 %sampler15
%33 = OpAccessChain %_ptr_Function_19 %s0 %int_2
OpStore %33 %32
%34 = OpLoad %S_t %s0
OpStore %param %34
%42 = OpAccessChain %_ptr_Function_19 %param %int_2
%43 = OpLoad %19 %42
%44 = OpAccessChain %_ptr_Function_v2float %param %int_0
%45 = OpLoad %v2float %44
%46 = OpImageSampleImplicitLod %v4float %43 %45
OpStore %outColor %46
OpReturn
OpFunctionEnd
)";
const std::string post_defs =
R"(%foo_struct_S_t_vf2_vf21_ = OpFunction %void None %21
%s = OpFunctionParameter %_ptr_Function_S_t
%35 = OpLabel
%36 = OpAccessChain %_ptr_Function_19 %s %int_2
%37 = OpLoad %19 %36
%38 = OpAccessChain %_ptr_Function_v2float %s %int_0
%39 = OpLoad %v2float %38
%40 = OpImageSampleImplicitLod %v4float %37 %39
OpStore %outColor %40
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<InlineOpaquePass>(
predefs_1 + name + predefs_2 + before + post_defs,
predefs_1 + predefs_2 + after + post_defs, true, true);
}
TEST_F(InlineOpaqueTest, InlineInNonEntryPointFunction) {
// This demonstrates opaque inlining in a function that is not
// an entry point function (main2) but is in the call tree of an

View File

@ -2581,6 +2581,132 @@ OpFunctionEnd
SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
}
TEST_F(InlineTest, InlineForLinkage) {
const std::string before =
R"(OpCapability SampledBuffer
OpCapability ImageBuffer
OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %type_buffer_image "type.buffer.image"
OpName %output "output"
OpName %main "main"
OpName %color "color"
OpName %bb_entry "bb.entry"
OpName %param_var_color "param.var.color"
OpName %fn "fn"
OpName %color_0 "color"
OpName %bb_entry_0 "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
OpDecorate %output DescriptorSet 0
OpDecorate %output Binding 1
%float = OpTypeFloat 32
%float_0_200000003 = OpConstant %float 0.200000003
%v4float = OpTypeVector %float 4
%6 = OpConstantComposite %v4float %float_0_200000003 %float_0_200000003 %float_0_200000003 %float_0_200000003
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%type_buffer_image = OpTypeImage %float Buffer 2 0 0 2 Rgba32f
%_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
%_ptr_Function_v4float = OpTypePointer Function %v4float
%11 = OpTypeFunction %float %_ptr_Function_v4float
%_ptr_Function_float = OpTypePointer Function %float
%output = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
%main = OpFunction %float None %11
%color = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%param_var_color = OpVariable %_ptr_Function_v4float Function
%16 = OpLoad %v4float %color
OpStore %param_var_color %16
%17 = OpFunctionCall %float %fn %param_var_color
OpReturnValue %17
OpFunctionEnd
%fn = OpFunction %float None %11
%color_0 = OpFunctionParameter %_ptr_Function_v4float
%bb_entry_0 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%22 = OpLoad %v4float %color_0
OpStore %v %22
%23 = OpLoad %v4float %v
%24 = OpFMul %v4float %23 %6
OpStore %v %24
%26 = OpAccessChain %_ptr_Function_float %v %int_0
%27 = OpLoad %float %26
OpReturnValue %27
OpFunctionEnd
)";
const std::string after =
R"(OpCapability SampledBuffer
OpCapability ImageBuffer
OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %type_buffer_image "type.buffer.image"
OpName %output "output"
OpName %main "main"
OpName %color "color"
OpName %bb_entry "bb.entry"
OpName %param_var_color "param.var.color"
OpName %fn "fn"
OpName %color_0 "color"
OpName %bb_entry_0 "bb.entry"
OpName %v "v"
OpDecorate %main LinkageAttributes "main" Export
OpDecorate %output DescriptorSet 0
OpDecorate %output Binding 1
%float = OpTypeFloat 32
%float_0_200000003 = OpConstant %float 0.200000003
%v4float = OpTypeVector %float 4
%6 = OpConstantComposite %v4float %float_0_200000003 %float_0_200000003 %float_0_200000003 %float_0_200000003
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%type_buffer_image = OpTypeImage %float Buffer 2 0 0 2 Rgba32f
%_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
%_ptr_Function_v4float = OpTypePointer Function %v4float
%11 = OpTypeFunction %float %_ptr_Function_v4float
%_ptr_Function_float = OpTypePointer Function %float
%output = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
%main = OpFunction %float None %11
%color = OpFunctionParameter %_ptr_Function_v4float
%bb_entry = OpLabel
%28 = OpVariable %_ptr_Function_v4float Function
%29 = OpVariable %_ptr_Function_float Function
%param_var_color = OpVariable %_ptr_Function_v4float Function
%16 = OpLoad %v4float %color
OpStore %param_var_color %16
%31 = OpLoad %v4float %param_var_color
OpStore %28 %31
%32 = OpLoad %v4float %28
%33 = OpFMul %v4float %32 %6
OpStore %28 %33
%34 = OpAccessChain %_ptr_Function_float %28 %int_0
%35 = OpLoad %float %34
OpStore %29 %35
%17 = OpLoad %float %29
OpReturnValue %17
OpFunctionEnd
%fn = OpFunction %float None %11
%color_0 = OpFunctionParameter %_ptr_Function_v4float
%bb_entry_0 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%22 = OpLoad %v4float %color_0
OpStore %v %22
%23 = OpLoad %v4float %v
%24 = OpFMul %v4float %23 %6
OpStore %v %24
%26 = OpAccessChain %_ptr_Function_float %v %int_0
%27 = OpLoad %float %26
OpReturnValue %27
OpFunctionEnd
)";
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
}
TEST_F(InlineTest, InlineFuncWithOpTerminateRayNotInContinue) {
const std::string text =
R"(

View File

@ -84,6 +84,56 @@ OpFunctionEnd
predefs_before + before, predefs_before + after, true, true);
}
TEST_F(LocalSingleBlockLoadStoreElimTest, LSBElimForLinkage) {
const std::string predefs_before =
R"(OpCapability Shader
OpCapability Linkage
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %v "v"
OpName %BaseColor "BaseColor"
OpName %gl_FragColor "gl_FragColor"
OpDecorate %main LinkageAttributes "main" Export
%void = OpTypeVoid
%7 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
)";
const std::string before =
R"(%main = OpFunction %void None %7
%13 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%14 = OpLoad %v4float %BaseColor
OpStore %v %14
%15 = OpLoad %v4float %v
OpStore %gl_FragColor %15
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %7
%13 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%14 = OpLoad %v4float %BaseColor
OpStore %v %14
OpStore %gl_FragColor %14
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<LocalSingleBlockLoadStoreElimPass>(
predefs_before + before, predefs_before + after, true, true);
}
TEST_F(LocalSingleBlockLoadStoreElimTest, SimpleLoadLoadElim) {
// #version 140
//

View File

@ -126,6 +126,91 @@ OpFunctionEnd
predefs + after, true, true);
}
TEST_F(LocalSingleStoreElimTest, LSSElimForLinkage) {
const std::string predefs =
R"(OpCapability Shader
OpCapability Linkage
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %v "v"
OpName %BaseColor "BaseColor"
OpName %f "f"
OpName %fi "fi"
OpName %gl_FragColor "gl_FragColor"
OpDecorate %main LinkageAttributes "main" Export
%void = OpTypeVoid
%9 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Function_v4float = OpTypePointer Function %v4float
%_ptr_Input_v4float = OpTypePointer Input %v4float
%BaseColor = OpVariable %_ptr_Input_v4float Input
%_ptr_Function_float = OpTypePointer Function %float
%_ptr_Input_float = OpTypePointer Input %float
%fi = OpVariable %_ptr_Input_float Input
%float_0 = OpConstant %float 0
%bool = OpTypeBool
%_ptr_Output_v4float = OpTypePointer Output %v4float
%gl_FragColor = OpVariable %_ptr_Output_v4float Output
)";
const std::string before =
R"(%main = OpFunction %void None %9
%19 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%f = OpVariable %_ptr_Function_float Function
%20 = OpLoad %v4float %BaseColor
OpStore %v %20
%21 = OpLoad %float %fi
OpStore %f %21
%22 = OpLoad %float %f
%23 = OpFOrdLessThan %bool %22 %float_0
OpSelectionMerge %24 None
OpBranchConditional %23 %25 %24
%25 = OpLabel
OpStore %f %float_0
OpBranch %24
%24 = OpLabel
%26 = OpLoad %v4float %v
%27 = OpLoad %float %f
%28 = OpCompositeConstruct %v4float %27 %27 %27 %27
%29 = OpFAdd %v4float %26 %28
OpStore %gl_FragColor %29
OpReturn
OpFunctionEnd
)";
const std::string after =
R"(%main = OpFunction %void None %9
%19 = OpLabel
%v = OpVariable %_ptr_Function_v4float Function
%f = OpVariable %_ptr_Function_float Function
%20 = OpLoad %v4float %BaseColor
OpStore %v %20
%21 = OpLoad %float %fi
OpStore %f %21
%22 = OpLoad %float %f
%23 = OpFOrdLessThan %bool %22 %float_0
OpSelectionMerge %24 None
OpBranchConditional %23 %25 %24
%25 = OpLabel
OpStore %f %float_0
OpBranch %24
%24 = OpLabel
%27 = OpLoad %float %f
%28 = OpCompositeConstruct %v4float %27 %27 %27 %27
%29 = OpFAdd %v4float %20 %28
OpStore %gl_FragColor %29
OpReturn
OpFunctionEnd
)";
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
predefs + after, true, true);
}
TEST_F(LocalSingleStoreElimTest, ThreeStores) {
// Three stores to multiple loads of v is not optimized.

View File

@ -137,6 +137,86 @@ OpFunctionEnd
true);
}
TEST_F(RelaxFloatOpsTest, RelaxFloatOpsForLinkage) {
const std::string defs0 =
R"(OpCapability Shader
OpCapability Linkage
OpCapability Sampled1D
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpSource HLSL 630
OpName %main "main"
OpName %g_tTex1df4 "g_tTex1df4"
OpName %g_sSamp "g_sSamp"
OpName %i_Tex0 "i.Tex0"
OpName %i_Tex1 "i.Tex1"
OpName %_entryPointOutput_Color "@entryPointOutput.Color"
OpDecorate %main LinkageAttributes "main" Export
OpDecorate %g_tTex1df4 DescriptorSet 0
OpDecorate %g_tTex1df4 Binding 0
OpDecorate %g_sSamp DescriptorSet 0
OpDecorate %g_sSamp Binding 0
OpDecorate %i_Tex0 Location 0
OpDecorate %i_Tex1 Location 1
OpDecorate %_entryPointOutput_Color Location 0
)";
const std::string defs1 =
R"(%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%17 = OpTypeImage %float 1D 0 0 0 1 Unknown
%_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
%g_tTex1df4 = OpVariable %_ptr_UniformConstant_17 UniformConstant
%21 = OpTypeSampler
%_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
%g_sSamp = OpVariable %_ptr_UniformConstant_21 UniformConstant
%25 = OpTypeSampledImage %17
%_ptr_Input_float = OpTypePointer Input %float
%i_Tex0 = OpVariable %_ptr_Input_float Input
%i_Tex1 = OpVariable %_ptr_Input_float Input
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_entryPointOutput_Color = OpVariable %_ptr_Output_v4float Output
%float_0_5 = OpConstant %float 0.5
%116 = OpConstantComposite %v4float %float_0_5 %float_0_5 %float_0_5 %float_0_5
)";
const std::string relax_decos =
R"(OpDecorate %60 RelaxedPrecision
OpDecorate %63 RelaxedPrecision
OpDecorate %82 RelaxedPrecision
OpDecorate %88 RelaxedPrecision
OpDecorate %91 RelaxedPrecision
OpDecorate %94 RelaxedPrecision
)";
const std::string func_orig =
R"(%main = OpFunction %void None %3
%5 = OpLabel
%60 = OpLoad %float %i_Tex0
%63 = OpLoad %float %i_Tex1
%77 = OpLoad %17 %g_tTex1df4
%78 = OpLoad %21 %g_sSamp
%79 = OpSampledImage %25 %77 %78
%82 = OpImageSampleImplicitLod %v4float %79 %60
%83 = OpLoad %17 %g_tTex1df4
%84 = OpLoad %21 %g_sSamp
%85 = OpSampledImage %25 %83 %84
%88 = OpImageSampleImplicitLod %v4float %85 %63
%91 = OpFAdd %v4float %82 %88
%94 = OpFMul %v4float %91 %116
OpStore %_entryPointOutput_Color %94
OpReturn
OpFunctionEnd
)";
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
SinglePassRunAndCheck<RelaxFloatOpsPass>(
defs0 + defs1 + func_orig, defs0 + relax_decos + defs1 + func_orig, true,
true);
}
} // namespace
} // namespace opt
} // namespace spvtools