1 // Copyright (c) 2019 Google LLC.
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/val/validate.h"
16 
17 #include "source/val/instruction.h"
18 #include "source/val/validation_state.h"
19 
20 namespace spvtools {
21 namespace val {
22 
ValidateSmallTypeUses(ValidationState_t & _,const Instruction * inst)23 spv_result_t ValidateSmallTypeUses(ValidationState_t& _,
24                                    const Instruction* inst) {
25   if (!_.HasCapability(SpvCapabilityShader) || inst->type_id() == 0 ||
26       !_.ContainsLimitedUseIntOrFloatType(inst->type_id())) {
27     return SPV_SUCCESS;
28   }
29 
30   if (_.IsPointerType(inst->type_id())) return SPV_SUCCESS;
31 
32   // The validator should previously have checked ways to generate 8- or 16-bit
33   // types. So we only need to considervalid paths from source to sink.
34   // When restricted, uses of 8- or 16-bit types can only be stores,
35   // width-only conversions, decorations and copy object.
36   for (auto use : inst->uses()) {
37     const auto* user = use.first;
38     switch (user->opcode()) {
39       case SpvOpDecorate:
40       case SpvOpDecorateId:
41       case SpvOpCopyObject:
42       case SpvOpStore:
43       case SpvOpFConvert:
44       case SpvOpUConvert:
45       case SpvOpSConvert:
46         break;
47       default:
48         return _.diag(SPV_ERROR_INVALID_ID, user)
49                << "Invalid use of 8- or 16-bit result";
50     }
51   }
52 
53   return SPV_SUCCESS;
54 }
55 
56 }  // namespace val
57 }  // namespace spvtools
58