2017-06-16 21:37:31 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/local_ssa_elim_pass.h"
|
2017-06-16 21:37:31 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/cfa.h"
|
|
|
|
#include "source/opt/iterator.h"
|
|
|
|
#include "source/opt/ssa_rewrite_pass.h"
|
2017-06-16 21:37:31 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
bool LocalMultiStoreElimPass::AllExtensionsSupported() const {
|
2017-07-19 00:57:26 +00:00
|
|
|
// If any extension not in whitelist, return false
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ei : get_module()->extensions()) {
|
2017-11-08 17:40:02 +00:00
|
|
|
const char* extName =
|
|
|
|
reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
|
2017-07-19 00:57:26 +00:00
|
|
|
if (extensions_whitelist_.find(extName) == extensions_whitelist_.end())
|
|
|
|
return false;
|
2017-06-16 21:37:31 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass::Status LocalMultiStoreElimPass::ProcessImpl() {
|
2017-12-11 18:10:24 +00:00
|
|
|
// Assumes relaxed logical addressing only (see instruction.h)
|
2017-06-16 21:37:31 +00:00
|
|
|
// TODO(greg-lunarg): Add support for physical addressing
|
2017-12-19 19:18:13 +00:00
|
|
|
if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
|
2017-06-16 21:37:31 +00:00
|
|
|
return Status::SuccessWithoutChange;
|
|
|
|
// Do not process if module contains OpGroupDecorate. Additional
|
|
|
|
// support required in KillNamesAndDecorates().
|
|
|
|
// TODO(greg-lunarg): Add support for OpGroupDecorate
|
2017-11-08 17:40:02 +00:00
|
|
|
for (auto& ai : get_module()->annotations())
|
|
|
|
if (ai.opcode() == SpvOpGroupDecorate) return Status::SuccessWithoutChange;
|
2017-06-16 21:37:31 +00:00
|
|
|
// Do not process if any disallowed extensions are enabled
|
2017-11-08 17:40:02 +00:00
|
|
|
if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
|
2017-06-16 21:37:31 +00:00
|
|
|
// Process functions
|
2018-07-12 19:14:43 +00:00
|
|
|
ProcessFunction pfn = [this](Function* fp) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
return SSARewriter(this).RewriteFunctionIntoSSA(fp);
|
2017-08-10 22:42:16 +00:00
|
|
|
};
|
2018-11-29 19:24:58 +00:00
|
|
|
bool modified = context()->ProcessEntryPointCallTree(pfn);
|
2017-06-16 21:37:31 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
LocalMultiStoreElimPass::LocalMultiStoreElimPass() = default;
|
2017-06-16 21:37:31 +00:00
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status LocalMultiStoreElimPass::Process() {
|
|
|
|
// Initialize extension whitelist
|
|
|
|
InitExtensions();
|
2017-06-16 21:37:31 +00:00
|
|
|
return ProcessImpl();
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
void LocalMultiStoreElimPass::InitExtensions() {
|
|
|
|
extensions_whitelist_.clear();
|
|
|
|
extensions_whitelist_.insert({
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_AMD_shader_explicit_vertex_parameter",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_AMD_shader_trinary_minmax",
|
|
|
|
"SPV_AMD_gcn_shader",
|
|
|
|
"SPV_KHR_shader_ballot",
|
|
|
|
"SPV_AMD_shader_ballot",
|
|
|
|
"SPV_AMD_gpu_shader_half_float",
|
|
|
|
"SPV_KHR_shader_draw_parameters",
|
|
|
|
"SPV_KHR_subgroup_vote",
|
|
|
|
"SPV_KHR_16bit_storage",
|
|
|
|
"SPV_KHR_device_group",
|
|
|
|
"SPV_KHR_multiview",
|
|
|
|
"SPV_NVX_multiview_per_view_attributes",
|
|
|
|
"SPV_NV_viewport_array2",
|
|
|
|
"SPV_NV_stereo_view_rendering",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_NV_sample_mask_override_coverage",
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_NV_geometry_shader_passthrough",
|
|
|
|
"SPV_AMD_texture_gather_bias_lod",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_storage_buffer_storage_class",
|
|
|
|
// SPV_KHR_variable_pointers
|
|
|
|
// Currently do not support extended pointer expressions
|
2017-11-27 15:16:41 +00:00
|
|
|
"SPV_AMD_gpu_shader_int16",
|
|
|
|
"SPV_KHR_post_depth_coverage",
|
2017-11-08 17:40:02 +00:00
|
|
|
"SPV_KHR_shader_atomic_counter_ops",
|
2018-03-08 13:54:00 +00:00
|
|
|
"SPV_EXT_shader_stencil_export",
|
|
|
|
"SPV_EXT_shader_viewport_index_layer",
|
|
|
|
"SPV_AMD_shader_image_load_store_lod",
|
|
|
|
"SPV_AMD_shader_fragment_mask",
|
|
|
|
"SPV_EXT_fragment_fully_covered",
|
|
|
|
"SPV_AMD_gpu_shader_half_float_fetch",
|
2018-03-08 20:33:28 +00:00
|
|
|
"SPV_GOOGLE_decorate_string",
|
|
|
|
"SPV_GOOGLE_hlsl_functionality1",
|
2018-04-06 14:18:34 +00:00
|
|
|
"SPV_NV_shader_subgroup_partitioned",
|
|
|
|
"SPV_EXT_descriptor_indexing",
|
2018-09-19 18:53:33 +00:00
|
|
|
"SPV_NV_fragment_shader_barycentric",
|
|
|
|
"SPV_NV_compute_shader_derivatives",
|
|
|
|
"SPV_NV_shader_image_footprint",
|
|
|
|
"SPV_NV_shading_rate",
|
|
|
|
"SPV_NV_mesh_shader",
|
2018-10-25 18:07:46 +00:00
|
|
|
"SPV_NV_ray_tracing",
|
2018-11-23 15:21:19 +00:00
|
|
|
"SPV_EXT_fragment_invocation_density",
|
2017-07-19 00:57:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|