File: validate_tensor_layout.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 (184 lines) | stat: -rw-r--r-- 6,351 bytes parent folder | download | duplicates (13)
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
// Copyright (c) 2024 NVIDIA Corporation
//
// 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.

// Validate instructions that manipulate tensor layout and view objects

#include "source/opcode.h"
#include "source/spirv_target_env.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validation_state.h"

namespace spvtools {
namespace val {
namespace {

spv_result_t ValidateTensorLayoutResultTypeNV(ValidationState_t& _,
                                              const Instruction* inst) {
  const auto result_type_index = 0;
  const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index);
  const auto result_type = _.FindDef(result_type_id);

  if (!result_type || spv::Op::OpTypeTensorLayoutNV != result_type->opcode()) {
    return _.diag(SPV_ERROR_INVALID_ID, inst)
           << spvOpcodeString(inst->opcode()) << " Result Type <id> "
           << _.getIdName(result_type_id) << " is not a tensor layout type.";
  }
  return SPV_SUCCESS;
}

spv_result_t ValidateTensorViewResultTypeNV(ValidationState_t& _,
                                            const Instruction* inst) {
  const auto result_type_index = 0;
  const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index);
  const auto result_type = _.FindDef(result_type_id);

  if (!result_type || spv::Op::OpTypeTensorViewNV != result_type->opcode()) {
    return _.diag(SPV_ERROR_INVALID_ID, inst)
           << spvOpcodeString(inst->opcode()) << " Result Type <id> "
           << _.getIdName(result_type_id) << " is not a tensor view type.";
  }
  return SPV_SUCCESS;
}

spv_result_t ValidateCreateTensorLayoutNV(ValidationState_t& _,
                                          const Instruction* inst) {
  if (auto error = ValidateTensorLayoutResultTypeNV(_, inst)) return error;

  return SPV_SUCCESS;
}

spv_result_t ValidateCreateTensorViewNV(ValidationState_t& _,
                                        const Instruction* inst) {
  if (auto error = ValidateTensorViewResultTypeNV(_, inst)) return error;

  return SPV_SUCCESS;
}

enum ExpectedNumValues {
  DIM,
  DIMx2,
  ONE,
  FOUR,
};

spv_result_t ValidateTensorTypeWithDimValuesNV(ValidationState_t& _,
                                               const Instruction* inst,
                                               ExpectedNumValues expected,
                                               bool is_view) {
  std::string type_str;
  if (is_view) {
    if (auto error = ValidateTensorViewResultTypeNV(_, inst)) return error;
    type_str = "TensorView";
  } else {
    if (auto error = ValidateTensorLayoutResultTypeNV(_, inst)) return error;
    type_str = "TensorLayout";
  }

  const auto result_type_id = inst->GetOperandAs<uint32_t>(0);
  const auto tensor_id = inst->GetOperandAs<uint32_t>(2);
  const auto tensor = _.FindDef(tensor_id);
  if (!tensor || result_type_id != tensor->type_id()) {
    return _.diag(SPV_ERROR_INVALID_ID, inst)
           << spvOpcodeString(inst->opcode()) << " Result Type <id> "
           << _.getIdName(result_type_id) << " does not match " << type_str
           << " type.";
  }

  const auto num_values = inst->operands().size() - 3;

  const auto result_type = _.FindDef(result_type_id);
  const auto dim_index = 1;
  const auto dim_id = result_type->GetOperandAs<uint32_t>(dim_index);
  uint64_t dim_value;
  if (_.EvalConstantValUint64(dim_id, &dim_value)) {
    uint64_t expected_num_values = 0;
    switch (expected) {
      case DIM:
        expected_num_values = dim_value;
        break;
      case DIMx2:
        expected_num_values = dim_value * 2;
        break;
      case ONE:
        expected_num_values = 1;
        break;
      case FOUR:
        expected_num_values = 4;
        break;
    }

    if (num_values != expected_num_values) {
      return _.diag(SPV_ERROR_INVALID_ID, inst)
             << spvOpcodeString(inst->opcode())
             << " unexpected number of operands.";
    }
  }

  for (uint32_t i = 0; i < num_values; ++i) {
    const auto val_id = inst->GetOperandAs<uint32_t>(i + 3);
    const auto val = _.FindDef(val_id);
    if (!val || !_.IsIntScalarType(val->type_id()) ||
        _.GetBitWidth(val->type_id()) != 32) {
      return _.diag(SPV_ERROR_INVALID_ID, inst)
             << spvOpcodeString(inst->opcode()) << " operand <id> "
             << _.getIdName(val_id) << " is not a 32-bit integer.";
    }
  }

  return SPV_SUCCESS;
}

}  // namespace

spv_result_t TensorLayoutPass(ValidationState_t& _, const Instruction* inst) {
  switch (inst->opcode()) {
    case spv::Op::OpCreateTensorLayoutNV:
      if (auto error = ValidateCreateTensorLayoutNV(_, inst)) return error;
      break;
    case spv::Op::OpCreateTensorViewNV:
      if (auto error = ValidateCreateTensorViewNV(_, inst)) return error;
      break;
    case spv::Op::OpTensorLayoutSetBlockSizeNV:
    case spv::Op::OpTensorLayoutSetDimensionNV:
    case spv::Op::OpTensorLayoutSetStrideNV:
      if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIM, false))
        return error;
      break;
    case spv::Op::OpTensorLayoutSliceNV:
      if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIMx2, false))
        return error;
      break;
    case spv::Op::OpTensorLayoutSetClampValueNV:
      if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, ONE, false))
        return error;
      break;
    case spv::Op::OpTensorViewSetDimensionNV:
    case spv::Op::OpTensorViewSetStrideNV:
      if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIM, true))
        return error;
      break;
    case spv::Op::OpTensorViewSetClipNV:
      if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, FOUR, true))
        return error;
      break;
    default:
      break;
  }

  return SPV_SUCCESS;
}

}  // namespace val
}  // namespace spvtools