|
16 | 16 |
|
17 | 17 | #include "convert_to_half_pass.h" |
18 | 18 |
|
| 19 | +#include <cstdint> |
| 20 | + |
19 | 21 | #include "source/opt/ir_builder.h" |
20 | 22 |
|
21 | 23 | namespace spvtools { |
@@ -195,6 +197,117 @@ bool ConvertToHalfPass::MatConvertCleanup(Instruction* inst) { |
195 | 197 | return true; |
196 | 198 | } |
197 | 199 |
|
| 200 | +// Return operand position of Image Operands <ID> or zero if there is none |
| 201 | +static constexpr uint32_t GetImageOperandsPosition(spv::Op opcode) { |
| 202 | + uint32_t position = 0; |
| 203 | + switch (opcode) { |
| 204 | + case spv::Op::OpImageWrite: |
| 205 | + return 3; |
| 206 | + case spv::Op::OpImageSampleImplicitLod: |
| 207 | + case spv::Op::OpImageSampleExplicitLod: |
| 208 | + case spv::Op::OpImageSampleProjImplicitLod: |
| 209 | + case spv::Op::OpImageSampleProjExplicitLod: |
| 210 | + case spv::Op::OpImageFetch: |
| 211 | + case spv::Op::OpImageRead: |
| 212 | + case spv::Op::OpImageSparseSampleImplicitLod: |
| 213 | + case spv::Op::OpImageSparseSampleExplicitLod: |
| 214 | + case spv::Op::OpImageSparseSampleProjImplicitLod: |
| 215 | + case spv::Op::OpImageSparseSampleProjExplicitLod: |
| 216 | + case spv::Op::OpImageSparseFetch: |
| 217 | + case spv::Op::OpImageSparseRead: |
| 218 | + return 4; |
| 219 | + case spv::Op::OpImageSampleDrefImplicitLod: |
| 220 | + case spv::Op::OpImageSampleDrefExplicitLod: |
| 221 | + case spv::Op::OpImageSampleProjDrefImplicitLod: |
| 222 | + case spv::Op::OpImageSampleProjDrefExplicitLod: |
| 223 | + case spv::Op::OpImageGather: |
| 224 | + case spv::Op::OpImageDrefGather: |
| 225 | + case spv::Op::OpImageSparseSampleDrefImplicitLod: |
| 226 | + case spv::Op::OpImageSparseSampleDrefExplicitLod: |
| 227 | + case spv::Op::OpImageSparseSampleProjDrefImplicitLod: |
| 228 | + case spv::Op::OpImageSparseSampleProjDrefExplicitLod: |
| 229 | + case spv::Op::OpImageSparseGather: |
| 230 | + case spv::Op::OpImageSparseDrefGather: |
| 231 | + return 5; |
| 232 | + case spv::Op::OpImageSampleFootprintNV: |
| 233 | + return 6; |
| 234 | + default: |
| 235 | + break; |
| 236 | + } |
| 237 | + return position; |
| 238 | +} |
| 239 | + |
| 240 | +bool ConvertToHalfPass::ImageOperandCleanup(Instruction* inst) { |
| 241 | + if (image_ops_.count(inst->opcode()) == 0) return false; |
| 242 | + |
| 243 | + const uint32_t image_operand_position = |
| 244 | + GetImageOperandsPosition(inst->opcode()); |
| 245 | + // Image operands can be optional, also some things like |SignExtend| don't |
| 246 | + // have additonal operands |
| 247 | + if (image_operand_position == 0 || |
| 248 | + (image_operand_position + 1) >= inst->NumOperandWords()) { |
| 249 | + return false; |
| 250 | + } |
| 251 | + |
| 252 | + bool modified = false; |
| 253 | + const uint32_t mask = inst->GetSingleWordOperand(image_operand_position); |
| 254 | + uint32_t next_operand = image_operand_position + 1; |
| 255 | + |
| 256 | + InstructionBuilder builder( |
| 257 | + context(), inst, |
| 258 | + IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
| 259 | + |
| 260 | + uint32_t float32_type = |
| 261 | + context()->get_type_mgr()->GetTypeInstruction(FloatScalarType(32)); |
| 262 | + |
| 263 | + // All mask after don't have a 32-bit restriction |
| 264 | + for (uint32_t i = 1; i <= (uint32_t)spv::ImageOperandsMask::MinLod; i <<= 1) { |
| 265 | + if ((mask & i) == 0) { |
| 266 | + continue; |
| 267 | + } |
| 268 | + |
| 269 | + uint32_t update_operand = next_operand++; |
| 270 | + const uint32_t ref_id = inst->GetSingleWordOperand(update_operand); |
| 271 | + Instruction* ref_inst = get_def_use_mgr()->GetDef(ref_id); |
| 272 | + Instruction* type_inst = get_def_use_mgr()->GetDef(ref_inst->type_id()); |
| 273 | + assert(type_inst->opcode() == spv::Op::OpTypeFloat); |
| 274 | + if (type_inst->GetSingleWordInOperand(0) != 32) { |
| 275 | + Instruction* cvt_inst = |
| 276 | + builder.AddUnaryOp(float32_type, spv::Op::OpFConvert, ref_id); |
| 277 | + if (cvt_inst == nullptr) { |
| 278 | + status_ = Status::Failure; |
| 279 | + return false; |
| 280 | + } |
| 281 | + inst->SetOperand(update_operand, {cvt_inst->result_id()}); |
| 282 | + get_def_use_mgr()->AnalyzeInstUse(inst); |
| 283 | + modified = true; |
| 284 | + } |
| 285 | + |
| 286 | + // All masks have 1 operand, except grad has two |
| 287 | + if (mask & uint32_t(spv::ImageOperandsMask::Grad)) { |
| 288 | + uint32_t dy_update_operand = next_operand++; |
| 289 | + const uint32_t dy_ref_id = inst->GetSingleWordOperand(dy_update_operand); |
| 290 | + Instruction* dy_ref_inst = get_def_use_mgr()->GetDef(dy_ref_id); |
| 291 | + Instruction* dy_type_inst = |
| 292 | + get_def_use_mgr()->GetDef(dy_ref_inst->type_id()); |
| 293 | + assert(dy_type_inst->opcode() == spv::Op::OpTypeFloat); |
| 294 | + if (dy_type_inst->GetSingleWordInOperand(0) != 32) { |
| 295 | + Instruction* dy_cvt_inst = |
| 296 | + builder.AddUnaryOp(float32_type, spv::Op::OpFConvert, ref_id); |
| 297 | + if (dy_cvt_inst == nullptr) { |
| 298 | + status_ = Status::Failure; |
| 299 | + return false; |
| 300 | + } |
| 301 | + inst->SetOperand(dy_update_operand, {dy_cvt_inst->result_id()}); |
| 302 | + get_def_use_mgr()->AnalyzeInstUse(inst); |
| 303 | + modified = true; |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + return modified; |
| 309 | +} |
| 310 | + |
198 | 311 | bool ConvertToHalfPass::RemoveRelaxedDecoration(uint32_t id) { |
199 | 312 | return context()->get_decoration_mgr()->RemoveDecorationsFrom( |
200 | 313 | id, [](const Instruction& dec) { |
@@ -452,6 +565,7 @@ bool ConvertToHalfPass::ProcessFunction(Function* func) { |
452 | 565 | } |
453 | 566 | for (auto ii = bb->begin(); ii != bb->end(); ++ii) { |
454 | 567 | bool Mmodified = MatConvertCleanup(&*ii); |
| 568 | + Mmodified |= ImageOperandCleanup(&*ii); |
455 | 569 | if (status_ == Status::Failure) { |
456 | 570 | ok = false; |
457 | 571 | break; |
|
0 commit comments