Skip to content

Commit

Permalink
[spirv] Move descriptor scalar replacement to legalization stage. (#3261
Browse files Browse the repository at this point in the history
)

* [spirv] `-fspv-flatten-resource-arrays` is not needed for flattening global struct of resources.

* [spirv] Move descriptor scalar replacement to legalization stage.
  • Loading branch information
ehsannas committed Nov 16, 2020
1 parent f1f6064 commit 78515c2
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 18 deletions.
17 changes: 8 additions & 9 deletions tools/clang/lib/SPIRV/SpirvEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11937,15 +11937,6 @@ bool SpirvEmitter::spirvToolsOptimize(std::vector<uint32_t> *mod,
// Add performance passes.
optimizer.RegisterPerformancePasses();

// Add flattening of resources if needed.
if (spirvOptions.flattenResourceArrays ||
declIdMapper.requiresFlatteningCompositeResources()) {
optimizer.RegisterPass(spvtools::CreateDescriptorScalarReplacementPass());
// ADCE should be run after desc_sroa in order to remove potentially
// illegal types such as structures containing opaque types.
optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
}

// Add compact ID pass.
optimizer.RegisterPass(spvtools::CreateCompactIdsPass());
} else {
Expand All @@ -11972,6 +11963,14 @@ bool SpirvEmitter::spirvToolsLegalize(std::vector<uint32_t> *mod,
spvtools::OptimizerOptions options;
options.set_run_validator(false);
optimizer.RegisterLegalizationPasses();
// Add flattening of resources if needed.
if (spirvOptions.flattenResourceArrays ||
declIdMapper.requiresFlatteningCompositeResources()) {
optimizer.RegisterPass(spvtools::CreateDescriptorScalarReplacementPass());
// ADCE should be run after desc_sroa in order to remove potentially
// illegal types such as structures containing opaque types.
optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
}
optimizer.RegisterPass(spvtools::CreateReplaceInvalidOpcodePass());
optimizer.RegisterPass(spvtools::CreateCompactIdsPass());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

struct S {
Texture2D t[2];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

struct T {
float2x3 a[4];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

// globalS.t should take binding #0.
// globalS.s should take binding #1.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

// globalS[0][0].t should take binding #0.
// globalS[0][0].s should take binding #1.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

// globalS.t should take binding #0.
// globalS.s should take binding #1.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

// globalS[0].t[0] should take binding #0.
// globalS[0].t[1] should take binding #1.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays
// Run: %dxc -T ps_6_0 -E main

struct S {
Texture2D t[2];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run: %dxc -T ps_6_0 -E main -fspv-flatten-resource-arrays -O3
// Run: %dxc -T ps_6_0 -E main -O3

// Check the names
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Run: %dxc -T ps_6_0 -E main -O0

// CHECK: OpDecorate %g_CombinedTextureSampler_t DescriptorSet 0
// CHECK: OpDecorate %g_CombinedTextureSampler_t Binding 0
// CHECK: OpDecorate %g_CombinedTextureSampler_s DescriptorSet 0
// CHECK: OpDecorate %g_CombinedTextureSampler_s Binding 1

struct sampler2D {
Texture2D t;
SamplerState s;
};
float4 tex2D(sampler2D x, float2 v) { return x.t.Sample(x.s, v); }

struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};

sampler2D g_CombinedTextureSampler;

float4 main(v2f i) : SV_Target {
return tex2D(g_CombinedTextureSampler, i.uv);
}
5 changes: 5 additions & 0 deletions tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,11 @@ TEST_F(FileTest, BindingStructureOfResourcesContainsBufferError) {
"vk.binding.global-struct-of-resources.contains-buffer-error.hlsl",
Expect::Failure);
}
TEST_F(FileTest, BindingStructureOfResourcesPassLegalization) {
runFileTest("vk.binding.global-struct-of-resources.pass-legalization.hlsl",
Expect::Success,
/*runValidation*/ true);
}

TEST_F(FileTest, VulkanPushConstant) { runFileTest("vk.push-constant.hlsl"); }
TEST_F(FileTest, VulkanPushConstantOffset) {
Expand Down
2 changes: 1 addition & 1 deletion tools/clang/unittests/SPIRV/FileTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ bool runCompilerWithSpirvGeneration(const llvm::StringRef inputFilePath,

bool requires_opt = false;
for (const auto &arg : rest)
if (arg == L"-O3" || arg.substr(0, 8) == L"-Oconfig")
if (arg == L"-O3" || arg == L"-O0" || arg.substr(0, 8) == L"-Oconfig")
requires_opt = true;

std::vector<LPCWSTR> flags;
Expand Down

0 comments on commit 78515c2

Please sign in to comment.