Coverage Report

Created: 2026-06-30 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/relax_float_ops_pass.cpp
Line
Count
Source
1
// Copyright (c) 2019 The Khronos Group Inc.
2
// Copyright (c) 2019 Valve Corporation
3
// Copyright (c) 2019 LunarG Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "relax_float_ops_pass.h"
18
19
#include "source/opt/ir_builder.h"
20
21
namespace spvtools {
22
namespace opt {
23
24
0
bool RelaxFloatOpsPass::IsRelaxable(Instruction* inst) {
25
0
  return target_ops_core_f_rslt_.count(inst->opcode()) != 0 ||
26
0
         target_ops_core_f_opnd_.count(inst->opcode()) != 0 ||
27
0
         sample_ops_.count(inst->opcode()) != 0 ||
28
0
         (inst->opcode() == spv::Op::OpExtInst &&
29
0
          inst->GetSingleWordInOperand(0) ==
30
0
              context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
31
0
          target_ops_450_.count(inst->GetSingleWordInOperand(1)) != 0);
32
0
}
33
34
0
bool RelaxFloatOpsPass::IsFloat32(Instruction* inst) {
35
0
  uint32_t ty_id;
36
0
  if (target_ops_core_f_opnd_.count(inst->opcode()) != 0) {
37
0
    uint32_t opnd_id = inst->GetSingleWordInOperand(0);
38
0
    Instruction* opnd_inst = get_def_use_mgr()->GetDef(opnd_id);
39
0
    ty_id = opnd_inst->type_id();
40
0
  } else {
41
0
    ty_id = inst->type_id();
42
0
    if (ty_id == 0) return false;
43
0
  }
44
0
  return IsFloat(ty_id, 32);
45
0
}
46
47
0
bool RelaxFloatOpsPass::IsRelaxed(uint32_t r_id) {
48
0
  for (auto r_inst : get_decoration_mgr()->GetDecorationsFor(r_id, false))
49
0
    if (r_inst->opcode() == spv::Op::OpDecorate &&
50
0
        spv::Decoration(r_inst->GetSingleWordInOperand(1)) ==
51
0
            spv::Decoration::RelaxedPrecision)
52
0
      return true;
53
0
  return false;
54
0
}
55
56
0
bool RelaxFloatOpsPass::ProcessInst(Instruction* r_inst) {
57
0
  uint32_t r_id = r_inst->result_id();
58
0
  if (r_id == 0) return false;
59
0
  if (!IsFloat32(r_inst)) return false;
60
0
  if (IsRelaxed(r_id)) return false;
61
0
  if (!IsRelaxable(r_inst)) return false;
62
0
  get_decoration_mgr()->AddDecoration(
63
0
      r_id, uint32_t(spv::Decoration::RelaxedPrecision));
64
0
  return true;
65
0
}
66
67
0
bool RelaxFloatOpsPass::ProcessFunction(Function* func) {
68
0
  bool modified = false;
69
0
  cfg()->ForEachBlockInReversePostOrder(
70
0
      func->entry().get(), [&modified, this](BasicBlock* bb) {
71
0
        for (auto ii = bb->begin(); ii != bb->end(); ++ii)
72
0
          modified |= ProcessInst(&*ii);
73
0
      });
74
0
  return modified;
75
0
}
76
77
0
Pass::Status RelaxFloatOpsPass::ProcessImpl() {
78
0
  Pass::ProcessFunction pfn = [this](Function* fp) {
79
0
    return ProcessFunction(fp);
80
0
  };
81
0
  bool modified = context()->ProcessReachableCallTree(pfn);
82
0
  return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
83
0
}
84
85
0
Pass::Status RelaxFloatOpsPass::Process() {
86
0
  Initialize();
87
0
  return ProcessImpl();
88
0
}
89
90
0
void RelaxFloatOpsPass::Initialize() {
91
0
  target_ops_core_f_rslt_ = {
92
0
      spv::Op::OpLoad,
93
0
      spv::Op::OpPhi,
94
0
      spv::Op::OpVectorExtractDynamic,
95
0
      spv::Op::OpVectorInsertDynamic,
96
0
      spv::Op::OpVectorShuffle,
97
0
      spv::Op::OpCompositeExtract,
98
0
      spv::Op::OpCompositeConstruct,
99
0
      spv::Op::OpCompositeInsert,
100
0
      spv::Op::OpCopyObject,
101
0
      spv::Op::OpTranspose,
102
0
      spv::Op::OpConvertSToF,
103
0
      spv::Op::OpConvertUToF,
104
0
      spv::Op::OpFConvert,
105
      // spv::Op::OpQuantizeToF16,
106
0
      spv::Op::OpFNegate,
107
0
      spv::Op::OpFAdd,
108
0
      spv::Op::OpFSub,
109
0
      spv::Op::OpFMul,
110
0
      spv::Op::OpFDiv,
111
0
      spv::Op::OpFMod,
112
0
      spv::Op::OpVectorTimesScalar,
113
0
      spv::Op::OpMatrixTimesScalar,
114
0
      spv::Op::OpVectorTimesMatrix,
115
0
      spv::Op::OpMatrixTimesVector,
116
0
      spv::Op::OpMatrixTimesMatrix,
117
0
      spv::Op::OpOuterProduct,
118
0
      spv::Op::OpDot,
119
0
      spv::Op::OpSelect,
120
0
  };
121
0
  target_ops_core_f_opnd_ = {
122
0
      spv::Op::OpFOrdEqual,
123
0
      spv::Op::OpFUnordEqual,
124
0
      spv::Op::OpFOrdNotEqual,
125
0
      spv::Op::OpFUnordNotEqual,
126
0
      spv::Op::OpFOrdLessThan,
127
0
      spv::Op::OpFUnordLessThan,
128
0
      spv::Op::OpFOrdGreaterThan,
129
0
      spv::Op::OpFUnordGreaterThan,
130
0
      spv::Op::OpFOrdLessThanEqual,
131
0
      spv::Op::OpFUnordLessThanEqual,
132
0
      spv::Op::OpFOrdGreaterThanEqual,
133
0
      spv::Op::OpFUnordGreaterThanEqual,
134
0
  };
135
0
  target_ops_450_ = {
136
0
      GLSLstd450Round, GLSLstd450RoundEven, GLSLstd450Trunc, GLSLstd450FAbs,
137
0
      GLSLstd450FSign, GLSLstd450Floor, GLSLstd450Ceil, GLSLstd450Fract,
138
0
      GLSLstd450Radians, GLSLstd450Degrees, GLSLstd450Sin, GLSLstd450Cos,
139
0
      GLSLstd450Tan, GLSLstd450Asin, GLSLstd450Acos, GLSLstd450Atan,
140
0
      GLSLstd450Sinh, GLSLstd450Cosh, GLSLstd450Tanh, GLSLstd450Asinh,
141
0
      GLSLstd450Acosh, GLSLstd450Atanh, GLSLstd450Atan2, GLSLstd450Pow,
142
0
      GLSLstd450Exp, GLSLstd450Log, GLSLstd450Exp2, GLSLstd450Log2,
143
0
      GLSLstd450Sqrt, GLSLstd450InverseSqrt, GLSLstd450Determinant,
144
0
      GLSLstd450MatrixInverse,
145
      // TODO(greg-lunarg): GLSLstd450ModfStruct,
146
0
      GLSLstd450FMin, GLSLstd450FMax, GLSLstd450FClamp, GLSLstd450FMix,
147
0
      GLSLstd450Step, GLSLstd450SmoothStep, GLSLstd450Fma,
148
      // TODO(greg-lunarg): GLSLstd450FrexpStruct,
149
0
      GLSLstd450Ldexp, GLSLstd450Length, GLSLstd450Distance, GLSLstd450Cross,
150
0
      GLSLstd450Normalize, GLSLstd450FaceForward, GLSLstd450Reflect,
151
0
      GLSLstd450Refract, GLSLstd450NMin, GLSLstd450NMax, GLSLstd450NClamp};
152
0
  sample_ops_ = {spv::Op::OpImageSampleImplicitLod,
153
0
                 spv::Op::OpImageSampleExplicitLod,
154
0
                 spv::Op::OpImageSampleDrefImplicitLod,
155
0
                 spv::Op::OpImageSampleDrefExplicitLod,
156
0
                 spv::Op::OpImageSampleProjImplicitLod,
157
0
                 spv::Op::OpImageSampleProjExplicitLod,
158
0
                 spv::Op::OpImageSampleProjDrefImplicitLod,
159
0
                 spv::Op::OpImageSampleProjDrefExplicitLod,
160
0
                 spv::Op::OpImageFetch,
161
0
                 spv::Op::OpImageGather,
162
0
                 spv::Op::OpImageDrefGather,
163
0
                 spv::Op::OpImageRead,
164
0
                 spv::Op::OpImageSparseSampleImplicitLod,
165
0
                 spv::Op::OpImageSparseSampleExplicitLod,
166
0
                 spv::Op::OpImageSparseSampleDrefImplicitLod,
167
0
                 spv::Op::OpImageSparseSampleDrefExplicitLod,
168
0
                 spv::Op::OpImageSparseSampleProjImplicitLod,
169
0
                 spv::Op::OpImageSparseSampleProjExplicitLod,
170
0
                 spv::Op::OpImageSparseSampleProjDrefImplicitLod,
171
0
                 spv::Op::OpImageSparseSampleProjDrefExplicitLod,
172
0
                 spv::Op::OpImageSparseFetch,
173
0
                 spv::Op::OpImageSparseGather,
174
0
                 spv::Op::OpImageSparseDrefGather,
175
0
                 spv::Op::OpImageSparseTexelsResident,
176
0
                 spv::Op::OpImageSparseRead};
177
0
}
178
179
}  // namespace opt
180
}  // namespace spvtools