1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// Copyright (c) 2022 Google LLC
//
// 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.
#include "source/opt/spread_volatile_semantics.h"
#include "source/opt/decoration_manager.h"
#include "source/spirv_constant.h"
namespace spvtools {
namespace opt {
namespace {
constexpr uint32_t kOpDecorateInOperandBuiltinDecoration = 2u;
constexpr uint32_t kOpLoadInOperandMemoryOperands = 1u;
constexpr uint32_t kOpEntryPointInOperandEntryPoint = 1u;
constexpr uint32_t kOpEntryPointInOperandInterface = 3u;
bool HasBuiltinDecoration(analysis::DecorationManager* decoration_manager,
uint32_t var_id, uint32_t built_in) {
return decoration_manager->FindDecoration(
var_id, uint32_t(spv::Decoration::BuiltIn),
[built_in](const Instruction& inst) {
return built_in == inst.GetSingleWordInOperand(
kOpDecorateInOperandBuiltinDecoration);
});
}
bool IsBuiltInForRayTracingVolatileSemantics(spv::BuiltIn built_in) {
switch (built_in) {
case spv::BuiltIn::SMIDNV:
case spv::BuiltIn::WarpIDNV:
case spv::BuiltIn::SubgroupSize:
case spv::BuiltIn::SubgroupLocalInvocationId:
case spv::BuiltIn::SubgroupEqMask:
case spv::BuiltIn::SubgroupGeMask:
case spv::BuiltIn::SubgroupGtMask:
case spv::BuiltIn::SubgroupLeMask:
case spv::BuiltIn::SubgroupLtMask:
return true;
default:
return false;
}
}
bool HasBuiltinForRayTracingVolatileSemantics(
analysis::DecorationManager* decoration_manager, uint32_t var_id) {
return decoration_manager->FindDecoration(
var_id, uint32_t(spv::Decoration::BuiltIn), [](const Instruction& inst) {
spv::BuiltIn built_in = spv::BuiltIn(
inst.GetSingleWordInOperand(kOpDecorateInOperandBuiltinDecoration));
return IsBuiltInForRayTracingVolatileSemantics(built_in);
});
}
bool HasVolatileDecoration(analysis::DecorationManager* decoration_manager,
uint32_t var_id) {
return decoration_manager->HasDecoration(var_id,
uint32_t(spv::Decoration::Volatile));
}
} // namespace
Pass::Status SpreadVolatileSemantics::Process() {
if (HasNoExecutionModel()) {
return Status::SuccessWithoutChange;
}
const bool is_vk_memory_model_enabled =
context()->get_feature_mgr()->HasCapability(
spv::Capability::VulkanMemoryModel);
CollectTargetsForVolatileSemantics(is_vk_memory_model_enabled);
// If VulkanMemoryModel capability is not enabled, we have to set Volatile
// decoration for interface variables instead of setting Volatile for load
// instructions. If an interface (or pointers to it) is used by two load
// instructions in two entry points and one must be volatile while another
// is not, we have to report an error for the conflict.
if (!is_vk_memory_model_enabled &&
HasInterfaceInConflictOfVolatileSemantics()) {
return Status::Failure;
}
return SpreadVolatileSemanticsToVariables(is_vk_memory_model_enabled);
}
Pass::Status SpreadVolatileSemantics::SpreadVolatileSemanticsToVariables(
const bool is_vk_memory_model_enabled) {
Status status = Status::SuccessWithoutChange;
for (Instruction& var : context()->types_values()) {
auto entry_function_ids =
EntryFunctionsToSpreadVolatileSemanticsForVar(var.result_id());
if (entry_function_ids.empty()) {
continue;
}
if (is_vk_memory_model_enabled) {
SetVolatileForLoadsInEntries(&var, entry_function_ids);
} else {
DecorateVarWithVolatile(&var);
}
status = Status::SuccessWithChange;
}
return status;
}
bool SpreadVolatileSemantics::IsTargetUsedByNonVolatileLoadInEntryPoint(
uint32_t var_id, Instruction* entry_point) {
uint32_t entry_function_id =
entry_point->GetSingleWordInOperand(kOpEntryPointInOperandEntryPoint);
std::unordered_set<uint32_t> funcs;
context()->CollectCallTreeFromRoots(entry_function_id, &funcs);
return !VisitLoadsOfPointersToVariableInEntries(
var_id,
[](Instruction* load) {
// If it has a load without volatile memory operand, finish traversal
// and return false.
if (load->NumInOperands() <= kOpLoadInOperandMemoryOperands) {
return false;
}
uint32_t memory_operands =
load->GetSingleWordInOperand(kOpLoadInOperandMemoryOperands);
return (memory_operands & uint32_t(spv::MemoryAccessMask::Volatile)) !=
0;
},
funcs);
}
bool SpreadVolatileSemantics::HasInterfaceInConflictOfVolatileSemantics() {
for (Instruction& entry_point : get_module()->entry_points()) {
spv::ExecutionModel execution_model =
static_cast<spv::ExecutionModel>(entry_point.GetSingleWordInOperand(0));
for (uint32_t operand_index = kOpEntryPointInOperandInterface;
operand_index < entry_point.NumInOperands(); ++operand_index) {
uint32_t var_id = entry_point.GetSingleWordInOperand(operand_index);
if (!EntryFunctionsToSpreadVolatileSemanticsForVar(var_id).empty() &&
!IsTargetForVolatileSemantics(var_id, execution_model) &&
IsTargetUsedByNonVolatileLoadInEntryPoint(var_id, &entry_point)) {
Instruction* inst = context()->get_def_use_mgr()->GetDef(var_id);
context()->EmitErrorMessage(
"Variable is a target for Volatile semantics for an entry point, "
"but it is not for another entry point",
inst);
return true;
}
}
}
return false;
}
void SpreadVolatileSemantics::MarkVolatileSemanticsForVariable(
uint32_t var_id, Instruction* entry_point) {
uint32_t entry_function_id =
entry_point->GetSingleWordInOperand(kOpEntryPointInOperandEntryPoint);
auto itr = var_ids_to_entry_fn_for_volatile_semantics_.find(var_id);
if (itr == var_ids_to_entry_fn_for_volatile_semantics_.end()) {
var_ids_to_entry_fn_for_volatile_semantics_[var_id] = {entry_function_id};
return;
}
itr->second.insert(entry_function_id);
}
void SpreadVolatileSemantics::CollectTargetsForVolatileSemantics(
const bool is_vk_memory_model_enabled) {
for (Instruction& entry_point : get_module()->entry_points()) {
spv::ExecutionModel execution_model =
static_cast<spv::ExecutionModel>(entry_point.GetSingleWordInOperand(0));
for (uint32_t operand_index = kOpEntryPointInOperandInterface;
operand_index < entry_point.NumInOperands(); ++operand_index) {
uint32_t var_id = entry_point.GetSingleWordInOperand(operand_index);
if (!IsTargetForVolatileSemantics(var_id, execution_model)) {
continue;
}
if (is_vk_memory_model_enabled ||
IsTargetUsedByNonVolatileLoadInEntryPoint(var_id, &entry_point)) {
MarkVolatileSemanticsForVariable(var_id, &entry_point);
}
}
}
}
void SpreadVolatileSemantics::DecorateVarWithVolatile(Instruction* var) {
analysis::DecorationManager* decoration_manager =
context()->get_decoration_mgr();
uint32_t var_id = var->result_id();
if (HasVolatileDecoration(decoration_manager, var_id)) {
return;
}
get_decoration_mgr()->AddDecoration(
spv::Op::OpDecorate,
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {var_id}},
{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
{uint32_t(spv::Decoration::Volatile)}}});
}
bool SpreadVolatileSemantics::VisitLoadsOfPointersToVariableInEntries(
uint32_t var_id, const std::function<bool(Instruction*)>& handle_load,
const std::unordered_set<uint32_t>& function_ids) {
std::vector<uint32_t> worklist({var_id});
auto* def_use_mgr = context()->get_def_use_mgr();
while (!worklist.empty()) {
uint32_t ptr_id = worklist.back();
worklist.pop_back();
bool finish_traversal = !def_use_mgr->WhileEachUser(
ptr_id, [this, &worklist, &ptr_id, handle_load,
&function_ids](Instruction* user) {
BasicBlock* block = context()->get_instr_block(user);
if (block == nullptr ||
function_ids.find(block->GetParent()->result_id()) ==
function_ids.end()) {
return true;
}
if (user->opcode() == spv::Op::OpAccessChain ||
user->opcode() == spv::Op::OpInBoundsAccessChain ||
user->opcode() == spv::Op::OpPtrAccessChain ||
user->opcode() == spv::Op::OpInBoundsPtrAccessChain ||
user->opcode() == spv::Op::OpCopyObject) {
if (ptr_id == user->GetSingleWordInOperand(0))
worklist.push_back(user->result_id());
return true;
}
if (user->opcode() != spv::Op::OpLoad) {
return true;
}
return handle_load(user);
});
if (finish_traversal) return false;
}
return true;
}
void SpreadVolatileSemantics::SetVolatileForLoadsInEntries(
Instruction* var, const std::unordered_set<uint32_t>& entry_function_ids) {
// Set Volatile memory operand for all load instructions if they do not have
// it.
for (auto entry_id : entry_function_ids) {
std::unordered_set<uint32_t> funcs;
context()->CollectCallTreeFromRoots(entry_id, &funcs);
VisitLoadsOfPointersToVariableInEntries(
var->result_id(),
[](Instruction* load) {
if (load->NumInOperands() <= kOpLoadInOperandMemoryOperands) {
load->AddOperand({SPV_OPERAND_TYPE_MEMORY_ACCESS,
{uint32_t(spv::MemoryAccessMask::Volatile)}});
return true;
}
uint32_t memory_operands =
load->GetSingleWordInOperand(kOpLoadInOperandMemoryOperands);
memory_operands |= uint32_t(spv::MemoryAccessMask::Volatile);
load->SetInOperand(kOpLoadInOperandMemoryOperands, {memory_operands});
return true;
},
funcs);
}
}
bool SpreadVolatileSemantics::IsTargetForVolatileSemantics(
uint32_t var_id, spv::ExecutionModel execution_model) {
analysis::DecorationManager* decoration_manager =
context()->get_decoration_mgr();
if (execution_model == spv::ExecutionModel::Fragment) {
return get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 6) &&
HasBuiltinDecoration(decoration_manager, var_id,
uint32_t(spv::BuiltIn::HelperInvocation));
}
if (execution_model == spv::ExecutionModel::IntersectionKHR ||
execution_model == spv::ExecutionModel::IntersectionNV) {
if (HasBuiltinDecoration(decoration_manager, var_id,
uint32_t(spv::BuiltIn::RayTmaxKHR))) {
return true;
}
}
switch (execution_model) {
case spv::ExecutionModel::RayGenerationKHR:
case spv::ExecutionModel::ClosestHitKHR:
case spv::ExecutionModel::MissKHR:
case spv::ExecutionModel::CallableKHR:
case spv::ExecutionModel::IntersectionKHR:
return HasBuiltinForRayTracingVolatileSemantics(decoration_manager,
var_id);
default:
return false;
}
}
} // namespace opt
} // namespace spvtools
|