File: remove_struct_member_reduction_opportunity.h

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 (84 lines) | stat: -rw-r--r-- 3,252 bytes parent folder | download | duplicates (32)
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
// Copyright (c) 2020 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.

#ifndef SOURCE_REDUCE_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_

#include "source/reduce/reduction_opportunity.h"

#include "source/opt/instruction.h"

namespace spvtools {
namespace reduce {

// An opportunity for removing a member from a struct type, adjusting all uses
// of the struct accordingly.
class RemoveStructMemberReductionOpportunity : public ReductionOpportunity {
 public:
  // Constructs a reduction opportunity from the struct type |struct_type|, for
  // removal of member |member_index|.
  RemoveStructMemberReductionOpportunity(opt::Instruction* struct_type,
                                         uint32_t member_index)
      : struct_type_(struct_type),
        member_index_(member_index),
        original_number_of_members_(struct_type->NumInOperands()) {}

  // Opportunities to remove fields from a common struct type mutually
  // invalidate each other.  We guard against this by requiring that the struct
  // still has the number of members it had when the opportunity was created.
  bool PreconditionHolds() override;

 protected:
  void Apply() override;

 private:
  // |composite_access_instruction| is an instruction that accesses a composite
  // id using either a series of literal indices (e.g. in the case of
  // OpCompositeInsert) or a series of index ids (e.g. in the case of
  // OpAccessChain).
  //
  // This function adjusts the indices that are used by
  // |composite_access_instruction| to that whenever an index is accessing a
  // member of |struct_type_|, it is decremented if the member is beyond
  // |member_index_|, to account for the removal of the |member_index_|-th
  // member.
  //
  // |composite_type_id| is the id of the composite type that the series of
  // indices is to be applied to.
  //
  // |first_index_input_operand| specifies the first input operand that is an
  // index.
  //
  // |literal_indices| specifies whether indices are given as literals (true),
  // or as ids (false).
  //
  // If id-based indexing is used, this function will add a constant for
  // |member_index_| - 1 to the module if needed.
  void AdjustAccessedIndices(
      uint32_t composite_type_id, uint32_t first_index_input_operand,
      bool literal_indices, opt::IRContext* context,
      opt::Instruction* composite_access_instruction) const;

  // The struct type from which a member is to be removed.
  opt::Instruction* struct_type_;

  uint32_t member_index_;

  uint32_t original_number_of_members_;
};

}  // namespace reduce
}  // namespace spvtools

#endif  //   SOURCE_REDUCE_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_