1 // Copyright (c) 2016 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/opt/instruction.h"
16
17 #include <initializer_list>
18
19 #include "OpenCLDebugInfo100.h"
20 #include "source/disassemble.h"
21 #include "source/opt/fold.h"
22 #include "source/opt/ir_context.h"
23 #include "source/opt/reflect.h"
24
25 namespace spvtools {
26 namespace opt {
27
28 namespace {
29 // Indices used to get particular operands out of instructions using InOperand.
30 const uint32_t kTypeImageDimIndex = 1;
31 const uint32_t kLoadBaseIndex = 0;
32 const uint32_t kPointerTypeStorageClassIndex = 0;
33 const uint32_t kTypeImageSampledIndex = 5;
34
35 // Constants for OpenCL.DebugInfo.100 extension instructions.
36 const uint32_t kExtInstSetIdInIdx = 0;
37 const uint32_t kExtInstInstructionInIdx = 1;
38 const uint32_t kDebugScopeNumWords = 7;
39 const uint32_t kDebugScopeNumWordsWithoutInlinedAt = 6;
40 const uint32_t kDebugNoScopeNumWords = 5;
41
42 // Number of operands of an OpBranchConditional instruction
43 // with weights.
44 const uint32_t kOpBranchConditionalWithWeightsNumOperands = 5;
45 } // namespace
46
Instruction(IRContext * c)47 Instruction::Instruction(IRContext* c)
48 : utils::IntrusiveNodeBase<Instruction>(),
49 context_(c),
50 opcode_(SpvOpNop),
51 has_type_id_(false),
52 has_result_id_(false),
53 unique_id_(c->TakeNextUniqueId()),
54 dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
55
Instruction(IRContext * c,SpvOp op)56 Instruction::Instruction(IRContext* c, SpvOp op)
57 : utils::IntrusiveNodeBase<Instruction>(),
58 context_(c),
59 opcode_(op),
60 has_type_id_(false),
61 has_result_id_(false),
62 unique_id_(c->TakeNextUniqueId()),
63 dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
64
Instruction(IRContext * c,const spv_parsed_instruction_t & inst,std::vector<Instruction> && dbg_line)65 Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
66 std::vector<Instruction>&& dbg_line)
67 : context_(c),
68 opcode_(static_cast<SpvOp>(inst.opcode)),
69 has_type_id_(inst.type_id != 0),
70 has_result_id_(inst.result_id != 0),
71 unique_id_(c->TakeNextUniqueId()),
72 dbg_line_insts_(std::move(dbg_line)),
73 dbg_scope_(kNoDebugScope, kNoInlinedAt) {
74 assert((!IsDebugLineInst(opcode_) || dbg_line.empty()) &&
75 "Op(No)Line attaching to Op(No)Line found");
76 for (uint32_t i = 0; i < inst.num_operands; ++i) {
77 const auto& current_payload = inst.operands[i];
78 std::vector<uint32_t> words(
79 inst.words + current_payload.offset,
80 inst.words + current_payload.offset + current_payload.num_words);
81 operands_.emplace_back(current_payload.type, std::move(words));
82 }
83 }
84
Instruction(IRContext * c,const spv_parsed_instruction_t & inst,const DebugScope & dbg_scope)85 Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
86 const DebugScope& dbg_scope)
87 : context_(c),
88 opcode_(static_cast<SpvOp>(inst.opcode)),
89 has_type_id_(inst.type_id != 0),
90 has_result_id_(inst.result_id != 0),
91 unique_id_(c->TakeNextUniqueId()),
92 dbg_scope_(dbg_scope) {
93 for (uint32_t i = 0; i < inst.num_operands; ++i) {
94 const auto& current_payload = inst.operands[i];
95 std::vector<uint32_t> words(
96 inst.words + current_payload.offset,
97 inst.words + current_payload.offset + current_payload.num_words);
98 operands_.emplace_back(current_payload.type, std::move(words));
99 }
100 }
101
Instruction(IRContext * c,SpvOp op,uint32_t ty_id,uint32_t res_id,const OperandList & in_operands)102 Instruction::Instruction(IRContext* c, SpvOp op, uint32_t ty_id,
103 uint32_t res_id, const OperandList& in_operands)
104 : utils::IntrusiveNodeBase<Instruction>(),
105 context_(c),
106 opcode_(op),
107 has_type_id_(ty_id != 0),
108 has_result_id_(res_id != 0),
109 unique_id_(c->TakeNextUniqueId()),
110 operands_(),
111 dbg_scope_(kNoDebugScope, kNoInlinedAt) {
112 if (has_type_id_) {
113 operands_.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_TYPE_ID,
114 std::initializer_list<uint32_t>{ty_id});
115 }
116 if (has_result_id_) {
117 operands_.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_RESULT_ID,
118 std::initializer_list<uint32_t>{res_id});
119 }
120 operands_.insert(operands_.end(), in_operands.begin(), in_operands.end());
121 }
122
Instruction(Instruction && that)123 Instruction::Instruction(Instruction&& that)
124 : utils::IntrusiveNodeBase<Instruction>(),
125 opcode_(that.opcode_),
126 has_type_id_(that.has_type_id_),
127 has_result_id_(that.has_result_id_),
128 unique_id_(that.unique_id_),
129 operands_(std::move(that.operands_)),
130 dbg_line_insts_(std::move(that.dbg_line_insts_)),
131 dbg_scope_(that.dbg_scope_) {
132 for (auto& i : dbg_line_insts_) {
133 i.dbg_scope_ = that.dbg_scope_;
134 }
135 }
136
operator =(Instruction && that)137 Instruction& Instruction::operator=(Instruction&& that) {
138 opcode_ = that.opcode_;
139 has_type_id_ = that.has_type_id_;
140 has_result_id_ = that.has_result_id_;
141 unique_id_ = that.unique_id_;
142 operands_ = std::move(that.operands_);
143 dbg_line_insts_ = std::move(that.dbg_line_insts_);
144 dbg_scope_ = that.dbg_scope_;
145 return *this;
146 }
147
Clone(IRContext * c) const148 Instruction* Instruction::Clone(IRContext* c) const {
149 Instruction* clone = new Instruction(c);
150 clone->opcode_ = opcode_;
151 clone->has_type_id_ = has_type_id_;
152 clone->has_result_id_ = has_result_id_;
153 clone->unique_id_ = c->TakeNextUniqueId();
154 clone->operands_ = operands_;
155 clone->dbg_line_insts_ = dbg_line_insts_;
156 clone->dbg_scope_ = dbg_scope_;
157 return clone;
158 }
159
GetSingleWordOperand(uint32_t index) const160 uint32_t Instruction::GetSingleWordOperand(uint32_t index) const {
161 const auto& words = GetOperand(index).words;
162 assert(words.size() == 1 && "expected the operand only taking one word");
163 return words.front();
164 }
165
NumInOperandWords() const166 uint32_t Instruction::NumInOperandWords() const {
167 uint32_t size = 0;
168 for (uint32_t i = TypeResultIdCount(); i < operands_.size(); ++i)
169 size += static_cast<uint32_t>(operands_[i].words.size());
170 return size;
171 }
172
HasBranchWeights() const173 bool Instruction::HasBranchWeights() const {
174 if (opcode_ == SpvOpBranchConditional &&
175 NumOperands() == kOpBranchConditionalWithWeightsNumOperands) {
176 return true;
177 }
178
179 return false;
180 }
181
ToBinaryWithoutAttachedDebugInsts(std::vector<uint32_t> * binary) const182 void Instruction::ToBinaryWithoutAttachedDebugInsts(
183 std::vector<uint32_t>* binary) const {
184 const uint32_t num_words = 1 + NumOperandWords();
185 binary->push_back((num_words << 16) | static_cast<uint16_t>(opcode_));
186 for (const auto& operand : operands_) {
187 binary->insert(binary->end(), operand.words.begin(), operand.words.end());
188 }
189 }
190
ReplaceOperands(const OperandList & new_operands)191 void Instruction::ReplaceOperands(const OperandList& new_operands) {
192 operands_.clear();
193 operands_.insert(operands_.begin(), new_operands.begin(), new_operands.end());
194 }
195
IsReadOnlyLoad() const196 bool Instruction::IsReadOnlyLoad() const {
197 if (IsLoad()) {
198 Instruction* address_def = GetBaseAddress();
199 if (!address_def) {
200 return false;
201 }
202
203 if (address_def->opcode() == SpvOpVariable) {
204 if (address_def->IsReadOnlyPointer()) {
205 return true;
206 }
207 }
208
209 if (address_def->opcode() == SpvOpLoad) {
210 const analysis::Type* address_type =
211 context()->get_type_mgr()->GetType(address_def->type_id());
212 if (address_type->AsSampledImage() != nullptr) {
213 const auto* image_type =
214 address_type->AsSampledImage()->image_type()->AsImage();
215 if (image_type->sampled() == 1) {
216 return true;
217 }
218 }
219 }
220 }
221 return false;
222 }
223
GetBaseAddress() const224 Instruction* Instruction::GetBaseAddress() const {
225 uint32_t base = GetSingleWordInOperand(kLoadBaseIndex);
226 Instruction* base_inst = context()->get_def_use_mgr()->GetDef(base);
227 bool done = false;
228 while (!done) {
229 switch (base_inst->opcode()) {
230 case SpvOpAccessChain:
231 case SpvOpInBoundsAccessChain:
232 case SpvOpPtrAccessChain:
233 case SpvOpInBoundsPtrAccessChain:
234 case SpvOpImageTexelPointer:
235 case SpvOpCopyObject:
236 // All of these instructions have the base pointer use a base pointer
237 // in in-operand 0.
238 base = base_inst->GetSingleWordInOperand(0);
239 base_inst = context()->get_def_use_mgr()->GetDef(base);
240 break;
241 default:
242 done = true;
243 break;
244 }
245 }
246 return base_inst;
247 }
248
IsReadOnlyPointer() const249 bool Instruction::IsReadOnlyPointer() const {
250 if (context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
251 return IsReadOnlyPointerShaders();
252 else
253 return IsReadOnlyPointerKernel();
254 }
255
IsVulkanStorageImage() const256 bool Instruction::IsVulkanStorageImage() const {
257 if (opcode() != SpvOpTypePointer) {
258 return false;
259 }
260
261 uint32_t storage_class =
262 GetSingleWordInOperand(kPointerTypeStorageClassIndex);
263 if (storage_class != SpvStorageClassUniformConstant) {
264 return false;
265 }
266
267 Instruction* base_type =
268 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(1));
269
270 // Unpack the optional layer of arraying.
271 if (base_type->opcode() == SpvOpTypeArray ||
272 base_type->opcode() == SpvOpTypeRuntimeArray) {
273 base_type = context()->get_def_use_mgr()->GetDef(
274 base_type->GetSingleWordInOperand(0));
275 }
276
277 if (base_type->opcode() != SpvOpTypeImage) {
278 return false;
279 }
280
281 if (base_type->GetSingleWordInOperand(kTypeImageDimIndex) == SpvDimBuffer) {
282 return false;
283 }
284
285 // Check if the image is sampled. If we do not know for sure that it is,
286 // then assume it is a storage image.
287 return base_type->GetSingleWordInOperand(kTypeImageSampledIndex) != 1;
288 }
289
IsVulkanSampledImage() const290 bool Instruction::IsVulkanSampledImage() const {
291 if (opcode() != SpvOpTypePointer) {
292 return false;
293 }
294
295 uint32_t storage_class =
296 GetSingleWordInOperand(kPointerTypeStorageClassIndex);
297 if (storage_class != SpvStorageClassUniformConstant) {
298 return false;
299 }
300
301 Instruction* base_type =
302 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(1));
303
304 // Unpack the optional layer of arraying.
305 if (base_type->opcode() == SpvOpTypeArray ||
306 base_type->opcode() == SpvOpTypeRuntimeArray) {
307 base_type = context()->get_def_use_mgr()->GetDef(
308 base_type->GetSingleWordInOperand(0));
309 }
310
311 if (base_type->opcode() != SpvOpTypeImage) {
312 return false;
313 }
314
315 if (base_type->GetSingleWordInOperand(kTypeImageDimIndex) == SpvDimBuffer) {
316 return false;
317 }
318
319 // Check if the image is sampled. If we know for sure that it is,
320 // then return true.
321 return base_type->GetSingleWordInOperand(kTypeImageSampledIndex) == 1;
322 }
323
IsVulkanStorageTexelBuffer() const324 bool Instruction::IsVulkanStorageTexelBuffer() const {
325 if (opcode() != SpvOpTypePointer) {
326 return false;
327 }
328
329 uint32_t storage_class =
330 GetSingleWordInOperand(kPointerTypeStorageClassIndex);
331 if (storage_class != SpvStorageClassUniformConstant) {
332 return false;
333 }
334
335 Instruction* base_type =
336 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(1));
337
338 // Unpack the optional layer of arraying.
339 if (base_type->opcode() == SpvOpTypeArray ||
340 base_type->opcode() == SpvOpTypeRuntimeArray) {
341 base_type = context()->get_def_use_mgr()->GetDef(
342 base_type->GetSingleWordInOperand(0));
343 }
344
345 if (base_type->opcode() != SpvOpTypeImage) {
346 return false;
347 }
348
349 if (base_type->GetSingleWordInOperand(kTypeImageDimIndex) != SpvDimBuffer) {
350 return false;
351 }
352
353 // Check if the image is sampled. If we do not know for sure that it is,
354 // then assume it is a storage texel buffer.
355 return base_type->GetSingleWordInOperand(kTypeImageSampledIndex) != 1;
356 }
357
IsVulkanStorageBuffer() const358 bool Instruction::IsVulkanStorageBuffer() const {
359 // Is there a difference between a "Storage buffer" and a "dynamic storage
360 // buffer" in SPIR-V and do we care about the difference?
361 if (opcode() != SpvOpTypePointer) {
362 return false;
363 }
364
365 Instruction* base_type =
366 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(1));
367
368 // Unpack the optional layer of arraying.
369 if (base_type->opcode() == SpvOpTypeArray ||
370 base_type->opcode() == SpvOpTypeRuntimeArray) {
371 base_type = context()->get_def_use_mgr()->GetDef(
372 base_type->GetSingleWordInOperand(0));
373 }
374
375 if (base_type->opcode() != SpvOpTypeStruct) {
376 return false;
377 }
378
379 uint32_t storage_class =
380 GetSingleWordInOperand(kPointerTypeStorageClassIndex);
381 if (storage_class == SpvStorageClassUniform) {
382 bool is_buffer_block = false;
383 context()->get_decoration_mgr()->ForEachDecoration(
384 base_type->result_id(), SpvDecorationBufferBlock,
385 [&is_buffer_block](const Instruction&) { is_buffer_block = true; });
386 return is_buffer_block;
387 } else if (storage_class == SpvStorageClassStorageBuffer) {
388 bool is_block = false;
389 context()->get_decoration_mgr()->ForEachDecoration(
390 base_type->result_id(), SpvDecorationBlock,
391 [&is_block](const Instruction&) { is_block = true; });
392 return is_block;
393 }
394 return false;
395 }
396
IsVulkanUniformBuffer() const397 bool Instruction::IsVulkanUniformBuffer() const {
398 if (opcode() != SpvOpTypePointer) {
399 return false;
400 }
401
402 uint32_t storage_class =
403 GetSingleWordInOperand(kPointerTypeStorageClassIndex);
404 if (storage_class != SpvStorageClassUniform) {
405 return false;
406 }
407
408 Instruction* base_type =
409 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(1));
410
411 // Unpack the optional layer of arraying.
412 if (base_type->opcode() == SpvOpTypeArray ||
413 base_type->opcode() == SpvOpTypeRuntimeArray) {
414 base_type = context()->get_def_use_mgr()->GetDef(
415 base_type->GetSingleWordInOperand(0));
416 }
417
418 if (base_type->opcode() != SpvOpTypeStruct) {
419 return false;
420 }
421
422 bool is_block = false;
423 context()->get_decoration_mgr()->ForEachDecoration(
424 base_type->result_id(), SpvDecorationBlock,
425 [&is_block](const Instruction&) { is_block = true; });
426 return is_block;
427 }
428
IsReadOnlyPointerShaders() const429 bool Instruction::IsReadOnlyPointerShaders() const {
430 if (type_id() == 0) {
431 return false;
432 }
433
434 Instruction* type_def = context()->get_def_use_mgr()->GetDef(type_id());
435 if (type_def->opcode() != SpvOpTypePointer) {
436 return false;
437 }
438
439 uint32_t storage_class =
440 type_def->GetSingleWordInOperand(kPointerTypeStorageClassIndex);
441
442 switch (storage_class) {
443 case SpvStorageClassUniformConstant:
444 if (!type_def->IsVulkanStorageImage() &&
445 !type_def->IsVulkanStorageTexelBuffer()) {
446 return true;
447 }
448 break;
449 case SpvStorageClassUniform:
450 if (!type_def->IsVulkanStorageBuffer()) {
451 return true;
452 }
453 break;
454 case SpvStorageClassPushConstant:
455 case SpvStorageClassInput:
456 return true;
457 default:
458 break;
459 }
460
461 bool is_nonwritable = false;
462 context()->get_decoration_mgr()->ForEachDecoration(
463 result_id(), SpvDecorationNonWritable,
464 [&is_nonwritable](const Instruction&) { is_nonwritable = true; });
465 return is_nonwritable;
466 }
467
IsReadOnlyPointerKernel() const468 bool Instruction::IsReadOnlyPointerKernel() const {
469 if (type_id() == 0) {
470 return false;
471 }
472
473 Instruction* type_def = context()->get_def_use_mgr()->GetDef(type_id());
474 if (type_def->opcode() != SpvOpTypePointer) {
475 return false;
476 }
477
478 uint32_t storage_class =
479 type_def->GetSingleWordInOperand(kPointerTypeStorageClassIndex);
480
481 return storage_class == SpvStorageClassUniformConstant;
482 }
483
GetTypeComponent(uint32_t element) const484 uint32_t Instruction::GetTypeComponent(uint32_t element) const {
485 uint32_t subtype = 0;
486 switch (opcode()) {
487 case SpvOpTypeStruct:
488 subtype = GetSingleWordInOperand(element);
489 break;
490 case SpvOpTypeArray:
491 case SpvOpTypeRuntimeArray:
492 case SpvOpTypeVector:
493 case SpvOpTypeMatrix:
494 // These types all have uniform subtypes.
495 subtype = GetSingleWordInOperand(0u);
496 break;
497 default:
498 break;
499 }
500
501 return subtype;
502 }
503
UpdateLexicalScope(uint32_t scope)504 void Instruction::UpdateLexicalScope(uint32_t scope) {
505 dbg_scope_.SetLexicalScope(scope);
506 for (auto& i : dbg_line_insts_) {
507 i.dbg_scope_.SetLexicalScope(scope);
508 }
509 if (!IsDebugLineInst(opcode()) &&
510 context()->AreAnalysesValid(IRContext::kAnalysisDebugInfo)) {
511 context()->get_debug_info_mgr()->AnalyzeDebugInst(this);
512 }
513 }
514
UpdateDebugInlinedAt(uint32_t new_inlined_at)515 void Instruction::UpdateDebugInlinedAt(uint32_t new_inlined_at) {
516 dbg_scope_.SetInlinedAt(new_inlined_at);
517 for (auto& i : dbg_line_insts_) {
518 i.dbg_scope_.SetInlinedAt(new_inlined_at);
519 }
520 if (!IsDebugLineInst(opcode()) &&
521 context()->AreAnalysesValid(IRContext::kAnalysisDebugInfo)) {
522 context()->get_debug_info_mgr()->AnalyzeDebugInst(this);
523 }
524 }
525
UpdateDebugInfoFrom(const Instruction * from)526 void Instruction::UpdateDebugInfoFrom(const Instruction* from) {
527 if (from == nullptr) return;
528 clear_dbg_line_insts();
529 if (!from->dbg_line_insts().empty())
530 dbg_line_insts().push_back(from->dbg_line_insts().back());
531 SetDebugScope(from->GetDebugScope());
532 if (!IsDebugLineInst(opcode()) &&
533 context()->AreAnalysesValid(IRContext::kAnalysisDebugInfo)) {
534 context()->get_debug_info_mgr()->AnalyzeDebugInst(this);
535 }
536 }
537
InsertBefore(std::unique_ptr<Instruction> && inst)538 Instruction* Instruction::InsertBefore(std::unique_ptr<Instruction>&& inst) {
539 inst.get()->InsertBefore(this);
540 return inst.release();
541 }
542
InsertBefore(std::vector<std::unique_ptr<Instruction>> && list)543 Instruction* Instruction::InsertBefore(
544 std::vector<std::unique_ptr<Instruction>>&& list) {
545 Instruction* first_node = list.front().get();
546 for (auto& inst : list) {
547 inst.release()->InsertBefore(this);
548 }
549 list.clear();
550 return first_node;
551 }
552
IsValidBasePointer() const553 bool Instruction::IsValidBasePointer() const {
554 uint32_t tid = type_id();
555 if (tid == 0) {
556 return false;
557 }
558
559 Instruction* type = context()->get_def_use_mgr()->GetDef(tid);
560 if (type->opcode() != SpvOpTypePointer) {
561 return false;
562 }
563
564 auto feature_mgr = context()->get_feature_mgr();
565 if (feature_mgr->HasCapability(SpvCapabilityAddresses)) {
566 // TODO: The rules here could be more restrictive.
567 return true;
568 }
569
570 if (opcode() == SpvOpVariable || opcode() == SpvOpFunctionParameter) {
571 return true;
572 }
573
574 // With variable pointers, there are more valid base pointer objects.
575 // Variable pointers implicitly declares Variable pointers storage buffer.
576 SpvStorageClass storage_class =
577 static_cast<SpvStorageClass>(type->GetSingleWordInOperand(0));
578 if ((feature_mgr->HasCapability(SpvCapabilityVariablePointersStorageBuffer) &&
579 storage_class == SpvStorageClassStorageBuffer) ||
580 (feature_mgr->HasCapability(SpvCapabilityVariablePointers) &&
581 storage_class == SpvStorageClassWorkgroup)) {
582 switch (opcode()) {
583 case SpvOpPhi:
584 case SpvOpSelect:
585 case SpvOpFunctionCall:
586 case SpvOpConstantNull:
587 return true;
588 default:
589 break;
590 }
591 }
592
593 uint32_t pointee_type_id = type->GetSingleWordInOperand(1);
594 Instruction* pointee_type_inst =
595 context()->get_def_use_mgr()->GetDef(pointee_type_id);
596
597 if (pointee_type_inst->IsOpaqueType()) {
598 return true;
599 }
600 return false;
601 }
602
GetOpenCL100DebugOpcode() const603 OpenCLDebugInfo100Instructions Instruction::GetOpenCL100DebugOpcode() const {
604 if (opcode() != SpvOpExtInst) {
605 return OpenCLDebugInfo100InstructionsMax;
606 }
607
608 if (!context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo()) {
609 return OpenCLDebugInfo100InstructionsMax;
610 }
611
612 if (GetSingleWordInOperand(kExtInstSetIdInIdx) !=
613 context()->get_feature_mgr()->GetExtInstImportId_OpenCL100DebugInfo()) {
614 return OpenCLDebugInfo100InstructionsMax;
615 }
616
617 return OpenCLDebugInfo100Instructions(
618 GetSingleWordInOperand(kExtInstInstructionInIdx));
619 }
620
IsValidBaseImage() const621 bool Instruction::IsValidBaseImage() const {
622 uint32_t tid = type_id();
623 if (tid == 0) {
624 return false;
625 }
626
627 Instruction* type = context()->get_def_use_mgr()->GetDef(tid);
628 return (type->opcode() == SpvOpTypeImage ||
629 type->opcode() == SpvOpTypeSampledImage);
630 }
631
IsOpaqueType() const632 bool Instruction::IsOpaqueType() const {
633 if (opcode() == SpvOpTypeStruct) {
634 bool is_opaque = false;
635 ForEachInOperand([&is_opaque, this](const uint32_t* op_id) {
636 Instruction* type_inst = context()->get_def_use_mgr()->GetDef(*op_id);
637 is_opaque |= type_inst->IsOpaqueType();
638 });
639 return is_opaque;
640 } else if (opcode() == SpvOpTypeArray) {
641 uint32_t sub_type_id = GetSingleWordInOperand(0);
642 Instruction* sub_type_inst =
643 context()->get_def_use_mgr()->GetDef(sub_type_id);
644 return sub_type_inst->IsOpaqueType();
645 } else {
646 return opcode() == SpvOpTypeRuntimeArray ||
647 spvOpcodeIsBaseOpaqueType(opcode());
648 }
649 }
650
IsFoldable() const651 bool Instruction::IsFoldable() const {
652 return IsFoldableByFoldScalar() ||
653 context()->get_instruction_folder().HasConstFoldingRule(this);
654 }
655
IsFoldableByFoldScalar() const656 bool Instruction::IsFoldableByFoldScalar() const {
657 const InstructionFolder& folder = context()->get_instruction_folder();
658 if (!folder.IsFoldableOpcode(opcode())) {
659 return false;
660 }
661
662 Instruction* type = context()->get_def_use_mgr()->GetDef(type_id());
663 if (!folder.IsFoldableType(type)) {
664 return false;
665 }
666
667 // Even if the type of the instruction is foldable, its operands may not be
668 // foldable (e.g., comparisons of 64bit types). Check that all operand types
669 // are foldable before accepting the instruction.
670 return WhileEachInOperand([&folder, this](const uint32_t* op_id) {
671 Instruction* def_inst = context()->get_def_use_mgr()->GetDef(*op_id);
672 Instruction* def_inst_type =
673 context()->get_def_use_mgr()->GetDef(def_inst->type_id());
674 return folder.IsFoldableType(def_inst_type);
675 });
676 }
677
IsFloatingPointFoldingAllowed() const678 bool Instruction::IsFloatingPointFoldingAllowed() const {
679 // TODO: Add the rules for kernels. For now it will be pessimistic.
680 // For now, do not support capabilities introduced by SPV_KHR_float_controls.
681 if (!context_->get_feature_mgr()->HasCapability(SpvCapabilityShader) ||
682 context_->get_feature_mgr()->HasCapability(SpvCapabilityDenormPreserve) ||
683 context_->get_feature_mgr()->HasCapability(
684 SpvCapabilityDenormFlushToZero) ||
685 context_->get_feature_mgr()->HasCapability(
686 SpvCapabilitySignedZeroInfNanPreserve) ||
687 context_->get_feature_mgr()->HasCapability(
688 SpvCapabilityRoundingModeRTZ) ||
689 context_->get_feature_mgr()->HasCapability(
690 SpvCapabilityRoundingModeRTE)) {
691 return false;
692 }
693
694 bool is_nocontract = false;
695 context_->get_decoration_mgr()->WhileEachDecoration(
696 result_id(), SpvDecorationNoContraction,
697 [&is_nocontract](const Instruction&) {
698 is_nocontract = true;
699 return false;
700 });
701 return !is_nocontract;
702 }
703
PrettyPrint(uint32_t options) const704 std::string Instruction::PrettyPrint(uint32_t options) const {
705 // Convert the module to binary.
706 std::vector<uint32_t> module_binary;
707 context()->module()->ToBinary(&module_binary, /* skip_nop = */ false);
708
709 // Convert the instruction to binary. This is used to identify the correct
710 // stream of words to output from the module.
711 std::vector<uint32_t> inst_binary;
712 ToBinaryWithoutAttachedDebugInsts(&inst_binary);
713
714 // Do not generate a header.
715 return spvInstructionBinaryToText(
716 context()->grammar().target_env(), inst_binary.data(), inst_binary.size(),
717 module_binary.data(), module_binary.size(),
718 options | SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
719 }
720
operator <<(std::ostream & str,const Instruction & inst)721 std::ostream& operator<<(std::ostream& str, const Instruction& inst) {
722 str << inst.PrettyPrint();
723 return str;
724 }
725
Dump() const726 void Instruction::Dump() const {
727 std::cerr << "Instruction #" << unique_id() << "\n" << *this << "\n";
728 }
729
IsOpcodeCodeMotionSafe() const730 bool Instruction::IsOpcodeCodeMotionSafe() const {
731 switch (opcode_) {
732 case SpvOpNop:
733 case SpvOpUndef:
734 case SpvOpLoad:
735 case SpvOpAccessChain:
736 case SpvOpInBoundsAccessChain:
737 case SpvOpArrayLength:
738 case SpvOpVectorExtractDynamic:
739 case SpvOpVectorInsertDynamic:
740 case SpvOpVectorShuffle:
741 case SpvOpCompositeConstruct:
742 case SpvOpCompositeExtract:
743 case SpvOpCompositeInsert:
744 case SpvOpCopyObject:
745 case SpvOpTranspose:
746 case SpvOpConvertFToU:
747 case SpvOpConvertFToS:
748 case SpvOpConvertSToF:
749 case SpvOpConvertUToF:
750 case SpvOpUConvert:
751 case SpvOpSConvert:
752 case SpvOpFConvert:
753 case SpvOpQuantizeToF16:
754 case SpvOpBitcast:
755 case SpvOpSNegate:
756 case SpvOpFNegate:
757 case SpvOpIAdd:
758 case SpvOpFAdd:
759 case SpvOpISub:
760 case SpvOpFSub:
761 case SpvOpIMul:
762 case SpvOpFMul:
763 case SpvOpUDiv:
764 case SpvOpSDiv:
765 case SpvOpFDiv:
766 case SpvOpUMod:
767 case SpvOpSRem:
768 case SpvOpSMod:
769 case SpvOpFRem:
770 case SpvOpFMod:
771 case SpvOpVectorTimesScalar:
772 case SpvOpMatrixTimesScalar:
773 case SpvOpVectorTimesMatrix:
774 case SpvOpMatrixTimesVector:
775 case SpvOpMatrixTimesMatrix:
776 case SpvOpOuterProduct:
777 case SpvOpDot:
778 case SpvOpIAddCarry:
779 case SpvOpISubBorrow:
780 case SpvOpUMulExtended:
781 case SpvOpSMulExtended:
782 case SpvOpAny:
783 case SpvOpAll:
784 case SpvOpIsNan:
785 case SpvOpIsInf:
786 case SpvOpLogicalEqual:
787 case SpvOpLogicalNotEqual:
788 case SpvOpLogicalOr:
789 case SpvOpLogicalAnd:
790 case SpvOpLogicalNot:
791 case SpvOpSelect:
792 case SpvOpIEqual:
793 case SpvOpINotEqual:
794 case SpvOpUGreaterThan:
795 case SpvOpSGreaterThan:
796 case SpvOpUGreaterThanEqual:
797 case SpvOpSGreaterThanEqual:
798 case SpvOpULessThan:
799 case SpvOpSLessThan:
800 case SpvOpULessThanEqual:
801 case SpvOpSLessThanEqual:
802 case SpvOpFOrdEqual:
803 case SpvOpFUnordEqual:
804 case SpvOpFOrdNotEqual:
805 case SpvOpFUnordNotEqual:
806 case SpvOpFOrdLessThan:
807 case SpvOpFUnordLessThan:
808 case SpvOpFOrdGreaterThan:
809 case SpvOpFUnordGreaterThan:
810 case SpvOpFOrdLessThanEqual:
811 case SpvOpFUnordLessThanEqual:
812 case SpvOpFOrdGreaterThanEqual:
813 case SpvOpFUnordGreaterThanEqual:
814 case SpvOpShiftRightLogical:
815 case SpvOpShiftRightArithmetic:
816 case SpvOpShiftLeftLogical:
817 case SpvOpBitwiseOr:
818 case SpvOpBitwiseXor:
819 case SpvOpBitwiseAnd:
820 case SpvOpNot:
821 case SpvOpBitFieldInsert:
822 case SpvOpBitFieldSExtract:
823 case SpvOpBitFieldUExtract:
824 case SpvOpBitReverse:
825 case SpvOpBitCount:
826 case SpvOpSizeOf:
827 return true;
828 default:
829 return false;
830 }
831 }
832
IsScalarizable() const833 bool Instruction::IsScalarizable() const {
834 if (spvOpcodeIsScalarizable(opcode())) {
835 return true;
836 }
837
838 if (opcode() == SpvOpExtInst) {
839 uint32_t instSetId =
840 context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
841
842 if (GetSingleWordInOperand(kExtInstSetIdInIdx) == instSetId) {
843 switch (GetSingleWordInOperand(kExtInstInstructionInIdx)) {
844 case GLSLstd450Round:
845 case GLSLstd450RoundEven:
846 case GLSLstd450Trunc:
847 case GLSLstd450FAbs:
848 case GLSLstd450SAbs:
849 case GLSLstd450FSign:
850 case GLSLstd450SSign:
851 case GLSLstd450Floor:
852 case GLSLstd450Ceil:
853 case GLSLstd450Fract:
854 case GLSLstd450Radians:
855 case GLSLstd450Degrees:
856 case GLSLstd450Sin:
857 case GLSLstd450Cos:
858 case GLSLstd450Tan:
859 case GLSLstd450Asin:
860 case GLSLstd450Acos:
861 case GLSLstd450Atan:
862 case GLSLstd450Sinh:
863 case GLSLstd450Cosh:
864 case GLSLstd450Tanh:
865 case GLSLstd450Asinh:
866 case GLSLstd450Acosh:
867 case GLSLstd450Atanh:
868 case GLSLstd450Atan2:
869 case GLSLstd450Pow:
870 case GLSLstd450Exp:
871 case GLSLstd450Log:
872 case GLSLstd450Exp2:
873 case GLSLstd450Log2:
874 case GLSLstd450Sqrt:
875 case GLSLstd450InverseSqrt:
876 case GLSLstd450Modf:
877 case GLSLstd450FMin:
878 case GLSLstd450UMin:
879 case GLSLstd450SMin:
880 case GLSLstd450FMax:
881 case GLSLstd450UMax:
882 case GLSLstd450SMax:
883 case GLSLstd450FClamp:
884 case GLSLstd450UClamp:
885 case GLSLstd450SClamp:
886 case GLSLstd450FMix:
887 case GLSLstd450Step:
888 case GLSLstd450SmoothStep:
889 case GLSLstd450Fma:
890 case GLSLstd450Frexp:
891 case GLSLstd450Ldexp:
892 case GLSLstd450FindILsb:
893 case GLSLstd450FindSMsb:
894 case GLSLstd450FindUMsb:
895 case GLSLstd450NMin:
896 case GLSLstd450NMax:
897 case GLSLstd450NClamp:
898 return true;
899 default:
900 return false;
901 }
902 }
903 }
904 return false;
905 }
906
IsOpcodeSafeToDelete() const907 bool Instruction::IsOpcodeSafeToDelete() const {
908 if (context()->IsCombinatorInstruction(this)) {
909 return true;
910 }
911
912 switch (opcode()) {
913 case SpvOpDPdx:
914 case SpvOpDPdy:
915 case SpvOpFwidth:
916 case SpvOpDPdxFine:
917 case SpvOpDPdyFine:
918 case SpvOpFwidthFine:
919 case SpvOpDPdxCoarse:
920 case SpvOpDPdyCoarse:
921 case SpvOpFwidthCoarse:
922 case SpvOpImageQueryLod:
923 return true;
924 default:
925 return false;
926 }
927 }
928
IsNonSemanticInstruction() const929 bool Instruction::IsNonSemanticInstruction() const {
930 if (!HasResultId()) return false;
931 if (opcode() != SpvOpExtInst) return false;
932
933 auto import_inst =
934 context()->get_def_use_mgr()->GetDef(GetSingleWordInOperand(0));
935 std::string import_name = import_inst->GetInOperand(0).AsString();
936 return import_name.find("NonSemantic.") == 0;
937 }
938
ToBinary(uint32_t type_id,uint32_t result_id,uint32_t ext_set,std::vector<uint32_t> * binary) const939 void DebugScope::ToBinary(uint32_t type_id, uint32_t result_id,
940 uint32_t ext_set,
941 std::vector<uint32_t>* binary) const {
942 uint32_t num_words = kDebugScopeNumWords;
943 OpenCLDebugInfo100Instructions dbg_opcode = OpenCLDebugInfo100DebugScope;
944 if (GetLexicalScope() == kNoDebugScope) {
945 num_words = kDebugNoScopeNumWords;
946 dbg_opcode = OpenCLDebugInfo100DebugNoScope;
947 } else if (GetInlinedAt() == kNoInlinedAt) {
948 num_words = kDebugScopeNumWordsWithoutInlinedAt;
949 }
950 std::vector<uint32_t> operands = {
951 (num_words << 16) | static_cast<uint16_t>(SpvOpExtInst),
952 type_id,
953 result_id,
954 ext_set,
955 static_cast<uint32_t>(dbg_opcode),
956 };
957 binary->insert(binary->end(), operands.begin(), operands.end());
958 if (GetLexicalScope() != kNoDebugScope) {
959 binary->push_back(GetLexicalScope());
960 if (GetInlinedAt() != kNoInlinedAt) binary->push_back(GetInlinedAt());
961 }
962 }
963
964 } // namespace opt
965 } // namespace spvtools
966