File: transformation_set_function_control.cpp

package info (click to toggle)
spirv-tools 2025.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,588 kB
  • sloc: cpp: 470,407; javascript: 5,893; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (107 lines) | stat: -rw-r--r-- 4,123 bytes parent folder | download | duplicates (15)
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
// Copyright (c) 2019 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/fuzz/transformation_set_function_control.h"

namespace spvtools {
namespace fuzz {

TransformationSetFunctionControl::TransformationSetFunctionControl(
    protobufs::TransformationSetFunctionControl message)
    : message_(std::move(message)) {}

TransformationSetFunctionControl::TransformationSetFunctionControl(
    uint32_t function_id, uint32_t function_control) {
  message_.set_function_id(function_id);
  message_.set_function_control(function_control);
}

bool TransformationSetFunctionControl::IsApplicable(
    opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  opt::Instruction* function_def_instruction =
      FindFunctionDefInstruction(ir_context);
  if (!function_def_instruction) {
    // The given function id does not correspond to any function.
    return false;
  }
  uint32_t existing_function_control_mask =
      function_def_instruction->GetSingleWordInOperand(0);

  // Check (via an assertion) that function control mask doesn't have any bad
  // bits set.
  uint32_t acceptable_function_control_bits = uint32_t(
      spv::FunctionControlMask::Inline | spv::FunctionControlMask::DontInline |
      spv::FunctionControlMask::Pure | spv::FunctionControlMask::Const);
  // The following is to keep release-mode compilers happy as this variable is
  // only used in an assertion.
  (void)(acceptable_function_control_bits);
  assert(!(message_.function_control() & ~acceptable_function_control_bits) &&
         "Nonsensical loop control bits were found.");

  // Check (via an assertion) that function control mask does not have both
  // Inline and DontInline bits set.
  assert(!((message_.function_control() &
            (uint32_t)spv::FunctionControlMask::Inline) &&
           (message_.function_control() &
            (uint32_t)spv::FunctionControlMask::DontInline)) &&
         "It is not OK to set both the 'Inline' and 'DontInline' bits of a "
         "function control mask");

  // Check that Const and Pure are only present if they were present on the
  // original function
  for (auto mask_bit :
       {spv::FunctionControlMask::Pure, spv::FunctionControlMask::Const}) {
    if ((message_.function_control() & uint32_t(mask_bit)) &&
        !(existing_function_control_mask & uint32_t(mask_bit))) {
      return false;
    }
  }

  return true;
}

void TransformationSetFunctionControl::Apply(
    opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  opt::Instruction* function_def_instruction =
      FindFunctionDefInstruction(ir_context);
  function_def_instruction->SetInOperand(0, {message_.function_control()});
}

protobufs::Transformation TransformationSetFunctionControl::ToMessage() const {
  protobufs::Transformation result;
  *result.mutable_set_function_control() = message_;
  return result;
}

opt::Instruction* TransformationSetFunctionControl ::FindFunctionDefInstruction(
    opt::IRContext* ir_context) const {
  // Look through all functions for a function whose defining instruction's
  // result id matches |message_.function_id|, returning the defining
  // instruction if found.
  for (auto& function : *ir_context->module()) {
    if (function.DefInst().result_id() == message_.function_id()) {
      return &function.DefInst();
    }
  }
  // A nullptr result indicates that no match was found.
  return nullptr;
}

std::unordered_set<uint32_t> TransformationSetFunctionControl::GetFreshIds()
    const {
  return std::unordered_set<uint32_t>();
}

}  // namespace fuzz
}  // namespace spvtools