| 1 | //===- Deserializer.cpp - MLIR SPIR-V Deserializer ------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the SPIR-V binary to MLIR SPIR-V module deserializer. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "Deserializer.h" |
| 14 | |
| 15 | #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h" |
| 16 | #include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h" |
| 17 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 18 | #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h" |
| 19 | #include "mlir/Dialect/SPIRV/IR/TargetAndABI.h" |
| 20 | #include "mlir/IR/Builders.h" |
| 21 | #include "mlir/IR/IRMapping.h" |
| 22 | #include "mlir/IR/Location.h" |
| 23 | #include "mlir/Target/SPIRV/SPIRVBinaryUtils.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
| 25 | #include "llvm/ADT/Sequence.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
| 28 | #include "llvm/ADT/bit.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/MathExtras.h" |
| 31 | #include "llvm/Support/SaveAndRestore.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include <optional> |
| 34 | #include <utility> |
| 35 | |
| 36 | using namespace mlir; |
| 37 | |
| 38 | #define DEBUG_TYPE "spirv-deserialization" |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Utility Functions |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | /// Returns true if the given `block` is a function entry block. |
| 45 | static inline bool isFnEntryBlock(Block *block) { |
| 46 | return block->isEntryBlock() && |
| 47 | isa_and_nonnull<spirv::FuncOp>(Val: block->getParentOp()); |
| 48 | } |
| 49 | |
| 50 | static void setInherentOrDiscardableAttr(Operation *op, StringAttr name, |
| 51 | Attribute value) { |
| 52 | if (op->getName().getInherentAttr(op, name).has_value()) { |
| 53 | op->getName().setInherentAttr(op, name, value); |
| 54 | return; |
| 55 | } |
| 56 | op->setDiscardableAttr(name, value); |
| 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // Deserializer Method Definitions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| 63 | spirv::Deserializer::Deserializer(ArrayRef<uint32_t> binary, |
| 64 | MLIRContext *context, |
| 65 | const spirv::DeserializationOptions &options) |
| 66 | : binary(binary), context(context), unknownLoc(UnknownLoc::get(context)), |
| 67 | module(createModuleOp()), opBuilder(module->getRegion()), options(options) |
| 68 | #ifndef NDEBUG |
| 69 | , |
| 70 | logger(llvm::dbgs()) |
| 71 | #endif |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | LogicalResult spirv::Deserializer::deserialize() { |
| 76 | LLVM_DEBUG({ |
| 77 | logger.resetIndent(); |
| 78 | logger.startLine() |
| 79 | << "//+++---------- start deserialization ----------+++//\n" ; |
| 80 | }); |
| 81 | |
| 82 | if (failed(Result: processHeader())) |
| 83 | return failure(); |
| 84 | |
| 85 | spirv::Opcode opcode = spirv::Opcode::OpNop; |
| 86 | ArrayRef<uint32_t> operands; |
| 87 | auto binarySize = binary.size(); |
| 88 | while (curOffset < binarySize) { |
| 89 | // Slice the next instruction out and populate `opcode` and `operands`. |
| 90 | // Internally this also updates `curOffset`. |
| 91 | if (failed(Result: sliceInstruction(opcode, operands))) |
| 92 | return failure(); |
| 93 | |
| 94 | if (failed(Result: processInstruction(opcode, operands))) |
| 95 | return failure(); |
| 96 | } |
| 97 | |
| 98 | assert(curOffset == binarySize && |
| 99 | "deserializer should never index beyond the binary end" ); |
| 100 | |
| 101 | for (auto &deferred : deferredInstructions) { |
| 102 | if (failed(Result: processInstruction(opcode: deferred.first, operands: deferred.second, deferInstructions: false))) { |
| 103 | return failure(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (failed(Result: resolveDeferredIdDecorations())) |
| 108 | return failure(); |
| 109 | |
| 110 | if (!functionParameterAttrs.empty()) |
| 111 | return emitError(loc: unknownLoc, message: "FuncParamAttr decoration on result <id> " ) |
| 112 | << functionParameterAttrs.begin()->first |
| 113 | << " is not supported: expected OpFunctionParameter or OpFunction" ; |
| 114 | |
| 115 | attachVCETriple(); |
| 116 | |
| 117 | LLVM_DEBUG(logger.startLine() |
| 118 | << "//+++-------- completed deserialization --------+++//\n" ); |
| 119 | return success(); |
| 120 | } |
| 121 | |
| 122 | OwningOpRef<spirv::ModuleOp> spirv::Deserializer::collect() { |
| 123 | return std::move(module); |
| 124 | } |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Module structure |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | OwningOpRef<spirv::ModuleOp> spirv::Deserializer::createModuleOp() { |
| 131 | OpBuilder builder(context); |
| 132 | OperationState state(unknownLoc, spirv::ModuleOp::getOperationName()); |
| 133 | spirv::ModuleOp::build(odsBuilder&: builder, odsState&: state); |
| 134 | return cast<spirv::ModuleOp>(Val: Operation::create(state)); |
| 135 | } |
| 136 | |
| 137 | LogicalResult spirv::Deserializer::() { |
| 138 | if (binary.size() < spirv::kHeaderWordCount) |
| 139 | return emitError(loc: unknownLoc, |
| 140 | message: "SPIR-V binary module must have a 5-word header" ); |
| 141 | |
| 142 | if (binary[0] != spirv::kMagicNumber) |
| 143 | return emitError(loc: unknownLoc, message: "incorrect magic number" ); |
| 144 | |
| 145 | // Version number bytes: 0 | major number | minor number | 0 |
| 146 | uint32_t majorVersion = (binary[1] << 8) >> 24; |
| 147 | uint32_t minorVersion = (binary[1] << 16) >> 24; |
| 148 | if (majorVersion == 1) { |
| 149 | switch (minorVersion) { |
| 150 | #define MIN_VERSION_CASE(v) \ |
| 151 | case v: \ |
| 152 | version = spirv::Version::V_1_##v; \ |
| 153 | break |
| 154 | |
| 155 | MIN_VERSION_CASE(0); |
| 156 | MIN_VERSION_CASE(1); |
| 157 | MIN_VERSION_CASE(2); |
| 158 | MIN_VERSION_CASE(3); |
| 159 | MIN_VERSION_CASE(4); |
| 160 | MIN_VERSION_CASE(5); |
| 161 | MIN_VERSION_CASE(6); |
| 162 | #undef MIN_VERSION_CASE |
| 163 | default: |
| 164 | return emitError(loc: unknownLoc, message: "unsupported SPIR-V minor version: " ) |
| 165 | << minorVersion; |
| 166 | } |
| 167 | } else { |
| 168 | return emitError(loc: unknownLoc, message: "unsupported SPIR-V major version: " ) |
| 169 | << majorVersion; |
| 170 | } |
| 171 | |
| 172 | // TODO: generator number, bound, schema |
| 173 | curOffset = spirv::kHeaderWordCount; |
| 174 | return success(); |
| 175 | } |
| 176 | |
| 177 | LogicalResult |
| 178 | spirv::Deserializer::processCapability(ArrayRef<uint32_t> operands) { |
| 179 | if (operands.size() != 1) |
| 180 | return emitError(loc: unknownLoc, message: "OpCapability must have one parameter" ); |
| 181 | |
| 182 | auto cap = spirv::symbolizeCapability(operands[0]); |
| 183 | if (!cap) |
| 184 | return emitError(loc: unknownLoc, message: "unknown capability: " ) << operands[0]; |
| 185 | |
| 186 | capabilities.insert(X: *cap); |
| 187 | return success(); |
| 188 | } |
| 189 | |
| 190 | LogicalResult spirv::Deserializer::processExtension(ArrayRef<uint32_t> words) { |
| 191 | if (words.empty()) { |
| 192 | return emitError( |
| 193 | loc: unknownLoc, |
| 194 | message: "OpExtension must have a literal string for the extension name" ); |
| 195 | } |
| 196 | |
| 197 | unsigned wordIndex = 0; |
| 198 | StringRef extName = decodeStringLiteral(words, wordIndex); |
| 199 | if (wordIndex != words.size()) |
| 200 | return emitError(loc: unknownLoc, |
| 201 | message: "unexpected trailing words in OpExtension instruction" ); |
| 202 | auto ext = spirv::symbolizeExtension(extName); |
| 203 | if (!ext) |
| 204 | return emitError(loc: unknownLoc, message: "unknown extension: " ) << extName; |
| 205 | |
| 206 | extensions.insert(X: *ext); |
| 207 | return success(); |
| 208 | } |
| 209 | |
| 210 | LogicalResult |
| 211 | spirv::Deserializer::processExtInstImport(ArrayRef<uint32_t> words) { |
| 212 | if (words.size() < 2) { |
| 213 | return emitError(loc: unknownLoc, |
| 214 | message: "OpExtInstImport must have a result <id> and a literal " |
| 215 | "string for the extended instruction set name" ); |
| 216 | } |
| 217 | |
| 218 | unsigned wordIndex = 1; |
| 219 | extendedInstSets[words[0]] = decodeStringLiteral(words, wordIndex); |
| 220 | if (wordIndex != words.size()) { |
| 221 | return emitError(loc: unknownLoc, |
| 222 | message: "unexpected trailing words in OpExtInstImport" ); |
| 223 | } |
| 224 | return success(); |
| 225 | } |
| 226 | |
| 227 | void spirv::Deserializer::attachVCETriple() { |
| 228 | module->setVceTripleAttr(spirv::VerCapExtAttr::get( |
| 229 | version, capabilities: capabilities.getArrayRef(), extensions: extensions.getArrayRef(), context)); |
| 230 | } |
| 231 | |
| 232 | LogicalResult |
| 233 | spirv::Deserializer::processMemoryModel(ArrayRef<uint32_t> operands) { |
| 234 | if (operands.size() != 2) |
| 235 | return emitError(loc: unknownLoc, message: "OpMemoryModel must have two operands" ); |
| 236 | |
| 237 | module->setAddressingModel( |
| 238 | static_cast<spirv::AddressingModel>(operands.front())); |
| 239 | |
| 240 | module->setMemoryModel(static_cast<spirv::MemoryModel>(operands.back())); |
| 241 | |
| 242 | return success(); |
| 243 | } |
| 244 | |
| 245 | template <typename AttrTy, typename EnumAttrTy, typename EnumTy> |
| 246 | static LogicalResult deserializeCacheControlDecoration( |
| 247 | Location loc, OpBuilder &opBuilder, |
| 248 | DenseMap<uint32_t, NamedAttrList> &decorations, ArrayRef<uint32_t> words, |
| 249 | StringAttr symbol, StringRef decorationName, StringRef cacheControlKind) { |
| 250 | if (words.size() != 4) { |
| 251 | return emitError(loc, message: "OpDecorate with " ) |
| 252 | << decorationName << " needs a cache control integer literal and a " |
| 253 | << cacheControlKind << " cache control literal" ; |
| 254 | } |
| 255 | unsigned cacheLevel = words[2]; |
| 256 | auto cacheControlAttr = static_cast<EnumTy>(words[3]); |
| 257 | auto value = opBuilder.getAttr<AttrTy>(cacheLevel, cacheControlAttr); |
| 258 | SmallVector<Attribute> attrs; |
| 259 | if (auto attrList = |
| 260 | dyn_cast_or_null<ArrayAttr>(Val: decorations[words[0]].get(name: symbol))) |
| 261 | llvm::append_range(C&: attrs, R&: attrList); |
| 262 | attrs.push_back(Elt: value); |
| 263 | decorations[words[0]].set(name: symbol, value: opBuilder.getArrayAttr(value: attrs)); |
| 264 | return success(); |
| 265 | } |
| 266 | |
| 267 | LogicalResult spirv::Deserializer::processDecoration(ArrayRef<uint32_t> words) { |
| 268 | // TODO: This function should also be auto-generated. For now, since only a |
| 269 | // few decorations are processed/handled in a meaningful manner, going with a |
| 270 | // manual implementation. |
| 271 | if (words.size() < 2) { |
| 272 | return emitError( |
| 273 | loc: unknownLoc, message: "OpDecorate must have at least result <id> and Decoration" ); |
| 274 | } |
| 275 | auto decorationName = |
| 276 | stringifyDecoration(static_cast<spirv::Decoration>(words[1])); |
| 277 | if (decorationName.empty()) { |
| 278 | return emitError(loc: unknownLoc, message: "invalid Decoration code : " ) << words[1]; |
| 279 | } |
| 280 | auto symbol = getSymbolDecoration(decorationName); |
| 281 | switch (static_cast<spirv::Decoration>(words[1])) { |
| 282 | case spirv::Decoration::FPFastMathMode: |
| 283 | if (words.size() != 3) { |
| 284 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 285 | << decorationName << " needs a single integer literal" ; |
| 286 | } |
| 287 | decorations[words[0]].set( |
| 288 | name: symbol, value: FPFastMathModeAttr::get(context: opBuilder.getContext(), |
| 289 | value: static_cast<FPFastMathMode>(words[2]))); |
| 290 | break; |
| 291 | case spirv::Decoration::FPRoundingMode: |
| 292 | if (words.size() != 3) { |
| 293 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 294 | << decorationName << " needs a single integer literal" ; |
| 295 | } |
| 296 | decorations[words[0]].set( |
| 297 | name: symbol, value: FPRoundingModeAttr::get(context: opBuilder.getContext(), |
| 298 | value: static_cast<FPRoundingMode>(words[2]))); |
| 299 | break; |
| 300 | case spirv::Decoration::FuncParamAttr: { |
| 301 | if (words.size() != 3) |
| 302 | return emitError(loc: unknownLoc, |
| 303 | message: "OpDecorate with FuncParamAttr needs a single " |
| 304 | "function parameter attribute" ); |
| 305 | auto value = spirv::symbolizeFunctionParameterAttribute(words[2]); |
| 306 | if (!value) |
| 307 | return emitError(loc: unknownLoc, message: "invalid FunctionParameterAttribute code: " ) |
| 308 | << words[2]; |
| 309 | SmallVector<Attribute> attrs; |
| 310 | if (auto previous = functionParameterAttrs.lookup(Val: words[0])) |
| 311 | llvm::append_range(C&: attrs, R&: previous); |
| 312 | attrs.push_back( |
| 313 | Elt: spirv::FunctionParameterAttributeAttr::get(context, value: *value)); |
| 314 | functionParameterAttrs[words[0]] = opBuilder.getArrayAttr(value: attrs); |
| 315 | break; |
| 316 | } |
| 317 | case spirv::Decoration::Alignment: |
| 318 | case spirv::Decoration::MaxByteOffset: |
| 319 | case spirv::Decoration::DescriptorSet: |
| 320 | case spirv::Decoration::Binding: |
| 321 | case spirv::Decoration::Location: |
| 322 | case spirv::Decoration::SpecId: |
| 323 | case spirv::Decoration::Index: |
| 324 | case spirv::Decoration::Offset: |
| 325 | case spirv::Decoration::XfbBuffer: |
| 326 | case spirv::Decoration::XfbStride: |
| 327 | if (words.size() != 3) { |
| 328 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 329 | << decorationName << " needs a single integer literal" ; |
| 330 | } |
| 331 | decorations[words[0]].set( |
| 332 | name: symbol, value: opBuilder.getI32IntegerAttr(value: static_cast<int32_t>(words[2]))); |
| 333 | break; |
| 334 | case spirv::Decoration::BuiltIn: |
| 335 | if (words.size() != 3) { |
| 336 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 337 | << decorationName << " needs a single integer literal" ; |
| 338 | } |
| 339 | decorations[words[0]].set( |
| 340 | name: symbol, value: opBuilder.getStringAttr( |
| 341 | bytes: stringifyBuiltIn(static_cast<spirv::BuiltIn>(words[2])))); |
| 342 | break; |
| 343 | case spirv::Decoration::ArrayStride: |
| 344 | if (words.size() != 3) { |
| 345 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 346 | << decorationName << " needs a single integer literal" ; |
| 347 | } |
| 348 | typeDecorations[words[0]] = words[2]; |
| 349 | break; |
| 350 | case spirv::Decoration::LinkageAttributes: { |
| 351 | if (words.size() < 4) { |
| 352 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 353 | << decorationName |
| 354 | << " needs at least 1 string and 1 integer literal" ; |
| 355 | } |
| 356 | // LinkageAttributes has two parameters ["linkageName", linkageType] |
| 357 | // e.g., OpDecorate %imported_func LinkageAttributes "outside.func" Import |
| 358 | // "linkageName" is a stringliteral encoded as uint32_t, |
| 359 | // hence the size of name is variable length which results in words.size() |
| 360 | // being variable length, words.size() = 3 + strlen(name)/4 + 1 or |
| 361 | // 3 + ceildiv(strlen(name), 4). |
| 362 | unsigned wordIndex = 2; |
| 363 | auto linkageName = spirv::decodeStringLiteral(words, wordIndex).str(); |
| 364 | auto linkageTypeAttr = opBuilder.getAttr<::mlir::spirv::LinkageTypeAttr>( |
| 365 | args: static_cast<::mlir::spirv::LinkageType>(words[wordIndex++])); |
| 366 | auto linkageAttr = opBuilder.getAttr<::mlir::spirv::LinkageAttributesAttr>( |
| 367 | args: StringAttr::get(context, bytes: linkageName), args&: linkageTypeAttr); |
| 368 | decorations[words[0]].set(name: symbol, value: dyn_cast<Attribute>(Val&: linkageAttr)); |
| 369 | break; |
| 370 | } |
| 371 | case spirv::Decoration::Aliased: |
| 372 | case spirv::Decoration::AliasedPointer: |
| 373 | case spirv::Decoration::Block: |
| 374 | case spirv::Decoration::BufferBlock: |
| 375 | case spirv::Decoration::CPacked: |
| 376 | case spirv::Decoration::Flat: |
| 377 | case spirv::Decoration::NonReadable: |
| 378 | case spirv::Decoration::NonWritable: |
| 379 | case spirv::Decoration::NoPerspective: |
| 380 | case spirv::Decoration::NoSignedWrap: |
| 381 | case spirv::Decoration::NoUnsignedWrap: |
| 382 | case spirv::Decoration::RelaxedPrecision: |
| 383 | case spirv::Decoration::Restrict: |
| 384 | case spirv::Decoration::RestrictPointer: |
| 385 | case spirv::Decoration::NoContraction: |
| 386 | case spirv::Decoration::Constant: |
| 387 | case spirv::Decoration::Invariant: |
| 388 | case spirv::Decoration::Patch: |
| 389 | case spirv::Decoration::Coherent: |
| 390 | case spirv::Decoration::Volatile: |
| 391 | case spirv::Decoration::SaturatedConversion: |
| 392 | if (words.size() != 2) { |
| 393 | return emitError(loc: unknownLoc, message: "OpDecorate with " ) |
| 394 | << decorationName << " needs a single target <id>" ; |
| 395 | } |
| 396 | decorations[words[0]].set(name: symbol, value: opBuilder.getUnitAttr()); |
| 397 | break; |
| 398 | case spirv::Decoration::CacheControlLoadINTEL: { |
| 399 | LogicalResult res = deserializeCacheControlDecoration< |
| 400 | CacheControlLoadINTELAttr, LoadCacheControlAttr, LoadCacheControl>( |
| 401 | loc: unknownLoc, opBuilder, decorations, words, symbol, decorationName, |
| 402 | cacheControlKind: "load" ); |
| 403 | if (failed(Result: res)) |
| 404 | return res; |
| 405 | break; |
| 406 | } |
| 407 | case spirv::Decoration::CacheControlStoreINTEL: { |
| 408 | LogicalResult res = deserializeCacheControlDecoration< |
| 409 | CacheControlStoreINTELAttr, StoreCacheControlAttr, StoreCacheControl>( |
| 410 | loc: unknownLoc, opBuilder, decorations, words, symbol, decorationName, |
| 411 | cacheControlKind: "store" ); |
| 412 | if (failed(Result: res)) |
| 413 | return res; |
| 414 | break; |
| 415 | } |
| 416 | case spirv::Decoration::AlignmentId: |
| 417 | case spirv::Decoration::MaxByteOffsetId: |
| 418 | case spirv::Decoration::CounterBuffer: |
| 419 | if (words.size() != 3) { |
| 420 | return emitError(loc: unknownLoc, message: "OpDecorateId with " ) |
| 421 | << decorationName << " needs a single <id> operand" ; |
| 422 | } |
| 423 | pendingIdDecorations.push_back(Elt: {.targetID: words[0], |
| 424 | .decoration: static_cast<spirv::Decoration>(words[1]), |
| 425 | .operandID: words[2], .loc: unknownLoc}); |
| 426 | break; |
| 427 | default: |
| 428 | return emitError(loc: unknownLoc, message: "unhandled Decoration : '" ) << decorationName; |
| 429 | } |
| 430 | return success(); |
| 431 | } |
| 432 | |
| 433 | LogicalResult spirv::Deserializer::resolveDeferredIdDecorations() { |
| 434 | for (const DeferredIdDecoration &entry : pendingIdDecorations) { |
| 435 | StringRef decorationName = stringifyDecoration(entry.decoration); |
| 436 | StringAttr symbol = getSymbolDecoration(decorationName); |
| 437 | |
| 438 | // Resolve the operand <id> to a symbol name. The operand must reference a |
| 439 | // module-scope symbol op (global variable or specialization constant). |
| 440 | StringRef operandSymName; |
| 441 | if (spirv::GlobalVariableOp varOp = |
| 442 | globalVariableMap.lookup(Val: entry.operandID)) |
| 443 | operandSymName = varOp.getSymName(); |
| 444 | else if (spirv::SpecConstantOp specOp = |
| 445 | specConstMap.lookup(Val: entry.operandID)) |
| 446 | operandSymName = specOp.getSymName(); |
| 447 | else |
| 448 | return emitError(loc: entry.loc, message: "OpDecorateId with " ) |
| 449 | << decorationName << " references <id> " << entry.operandID |
| 450 | << " which is not a global variable or specialization constant" ; |
| 451 | |
| 452 | auto symRef = FlatSymbolRefAttr::get(ctx: context, value: operandSymName); |
| 453 | |
| 454 | // Resolve the decoration target. By the time this method runs, all |
| 455 | // instructions have been processed, so every defined <id> must appear in |
| 456 | // one of these maps; an unresolved target indicates malformed input. |
| 457 | Operation *targetOp = nullptr; |
| 458 | if (spirv::GlobalVariableOp varOp = |
| 459 | globalVariableMap.lookup(Val: entry.targetID)) |
| 460 | targetOp = varOp; |
| 461 | else if (spirv::SpecConstantOp specOp = specConstMap.lookup(Val: entry.targetID)) |
| 462 | targetOp = specOp; |
| 463 | else if (spirv::FuncOp fnOp = funcMap.lookup(Val: entry.targetID)) |
| 464 | targetOp = fnOp; |
| 465 | else if (Value v = valueMap.lookup(Val: entry.targetID)) |
| 466 | targetOp = v.getDefiningOp(); |
| 467 | |
| 468 | if (!targetOp) |
| 469 | return emitError(loc: entry.loc, message: "OpDecorateId with " ) |
| 470 | << decorationName << " references unknown target <id> " |
| 471 | << entry.targetID; |
| 472 | |
| 473 | setInherentOrDiscardableAttr(op: targetOp, name: symbol, value: symRef); |
| 474 | } |
| 475 | return success(); |
| 476 | } |
| 477 | |
| 478 | LogicalResult |
| 479 | spirv::Deserializer::processMemberDecoration(ArrayRef<uint32_t> words) { |
| 480 | // The binary layout of OpMemberDecorate is different comparing to OpDecorate |
| 481 | if (words.size() < 3) { |
| 482 | return emitError(loc: unknownLoc, |
| 483 | message: "OpMemberDecorate must have at least 3 operands" ); |
| 484 | } |
| 485 | |
| 486 | auto decoration = static_cast<spirv::Decoration>(words[2]); |
| 487 | if (decoration == spirv::Decoration::Offset && words.size() != 4) { |
| 488 | return emitError(loc: unknownLoc, |
| 489 | message: " missing offset specification in OpMemberDecorate with " |
| 490 | "Offset decoration" ); |
| 491 | } |
| 492 | ArrayRef<uint32_t> decorationOperands; |
| 493 | if (words.size() > 3) { |
| 494 | decorationOperands = words.slice(N: 3); |
| 495 | } |
| 496 | memberDecorationMap[words[0]][words[1]][decoration] = decorationOperands; |
| 497 | return success(); |
| 498 | } |
| 499 | |
| 500 | LogicalResult spirv::Deserializer::processMemberName(ArrayRef<uint32_t> words) { |
| 501 | if (words.size() < 3) { |
| 502 | return emitError(loc: unknownLoc, message: "OpMemberName must have at least 3 operands" ); |
| 503 | } |
| 504 | unsigned wordIndex = 2; |
| 505 | auto name = decodeStringLiteral(words, wordIndex); |
| 506 | if (wordIndex != words.size()) { |
| 507 | return emitError(loc: unknownLoc, |
| 508 | message: "unexpected trailing words in OpMemberName instruction" ); |
| 509 | } |
| 510 | memberNameMap[words[0]][words[1]] = name; |
| 511 | return success(); |
| 512 | } |
| 513 | |
| 514 | LogicalResult spirv::Deserializer::setFunctionArgAttrs( |
| 515 | uint32_t argID, SmallVectorImpl<Attribute> &argAttrs, size_t argIndex) { |
| 516 | spirv::DecorationAttr foundDecorationAttr; |
| 517 | NamedAttrList attributes; |
| 518 | if (auto it = functionParameterAttrs.find(Val: argID); |
| 519 | it != functionParameterAttrs.end()) { |
| 520 | attributes.set(name: spirv::getFuncParamAttrName(), value: it->second); |
| 521 | functionParameterAttrs.erase(I: it); |
| 522 | } |
| 523 | for (NamedAttribute decAttr : decorations.lookup(Val: argID)) { |
| 524 | if (decAttr.getName() == getSymbolDecoration(decorationName: stringifyDecoration( |
| 525 | spirv::Decoration::Alignment))) { |
| 526 | attributes.set(name: spirv::getFuncParamAlignmentAttrName(), |
| 527 | value: decAttr.getValue()); |
| 528 | continue; |
| 529 | } |
| 530 | if (!isa<UnitAttr>(Val: decAttr.getValue())) |
| 531 | return emitError(loc: unknownLoc, message: "unhandled decoration '" ) |
| 532 | << decAttr.getName().getValue() |
| 533 | << "' on function argument with result <id> " << argID; |
| 534 | |
| 535 | // TODO: Support multiple decorations per function parameter. |
| 536 | if (foundDecorationAttr) |
| 537 | return emitError(loc: unknownLoc, message: "already found a decoration for function " |
| 538 | "argument with result <id> " ) |
| 539 | << argID; |
| 540 | |
| 541 | std::optional<spirv::Decoration> decoration = spirv::symbolizeDecoration( |
| 542 | llvm::convertToCamelFromSnakeCase(input: decAttr.getName(), capitalizeFirst: true)); |
| 543 | if (!decoration) |
| 544 | return emitError(loc: unknownLoc, message: "unknown decoration '" ) |
| 545 | << decAttr.getName().getValue() |
| 546 | << "' on function argument with result <id> " << argID; |
| 547 | foundDecorationAttr = spirv::DecorationAttr::get(context, value: *decoration); |
| 548 | } |
| 549 | |
| 550 | if (foundDecorationAttr) |
| 551 | attributes.set(name: spirv::DecorationAttr::name, value: foundDecorationAttr); |
| 552 | argAttrs[argIndex] = attributes.getDictionary(context); |
| 553 | return success(); |
| 554 | } |
| 555 | |
| 556 | LogicalResult |
| 557 | spirv::Deserializer::processFunction(ArrayRef<uint32_t> operands) { |
| 558 | if (curFunction) { |
| 559 | return emitError(loc: unknownLoc, message: "found function inside function" ); |
| 560 | } |
| 561 | |
| 562 | // Get the result type |
| 563 | if (operands.size() != 4) { |
| 564 | return emitError(loc: unknownLoc, message: "OpFunction must have 4 parameters" ); |
| 565 | } |
| 566 | Type resultType = getType(id: operands[0]); |
| 567 | if (!resultType) { |
| 568 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 569 | << operands[0]; |
| 570 | } |
| 571 | |
| 572 | uint32_t fnID = operands[1]; |
| 573 | if (funcMap.count(Val: fnID)) { |
| 574 | return emitError(loc: unknownLoc, message: "duplicate function definition/declaration" ); |
| 575 | } |
| 576 | |
| 577 | auto fnControl = spirv::symbolizeFunctionControl(operands[2]); |
| 578 | if (!fnControl) { |
| 579 | return emitError(loc: unknownLoc, message: "unknown Function Control: " ) << operands[2]; |
| 580 | } |
| 581 | |
| 582 | Type fnType = getType(id: operands[3]); |
| 583 | if (!fnType || !isa<FunctionType>(Val: fnType)) { |
| 584 | return emitError(loc: unknownLoc, message: "unknown function type from <id> " ) |
| 585 | << operands[3]; |
| 586 | } |
| 587 | auto functionType = cast<FunctionType>(Val&: fnType); |
| 588 | |
| 589 | if ((isVoidType(type: resultType) && functionType.getNumResults() != 0) || |
| 590 | (functionType.getNumResults() == 1 && |
| 591 | functionType.getResult(i: 0) != resultType)) { |
| 592 | return emitError(loc: unknownLoc, message: "mismatch in function type " ) |
| 593 | << functionType << " and return type " << resultType << " specified" ; |
| 594 | } |
| 595 | |
| 596 | std::string fnName = getFunctionSymbol(id: fnID); |
| 597 | auto funcOp = spirv::FuncOp::create(builder&: opBuilder, location: unknownLoc, name: fnName, |
| 598 | type: functionType, control: fnControl.value()); |
| 599 | if (auto it = functionParameterAttrs.find(Val: fnID); |
| 600 | it != functionParameterAttrs.end()) { |
| 601 | if (functionType.getNumResults() == 0) |
| 602 | return emitError(loc: unknownLoc, |
| 603 | message: "FuncParamAttr decoration requires a non-void function " |
| 604 | "result" ); |
| 605 | funcOp.setResultAttr(index: 0, name: spirv::getFuncParamAttrName(), value: it->second); |
| 606 | functionParameterAttrs.erase(I: it); |
| 607 | } |
| 608 | // Processing other function attributes. |
| 609 | if (decorations.count(Val: fnID)) { |
| 610 | for (auto attr : decorations[fnID].getAttrs()) { |
| 611 | setInherentOrDiscardableAttr(op: funcOp, name: attr.getName(), value: attr.getValue()); |
| 612 | } |
| 613 | } |
| 614 | curFunction = funcMap[fnID] = funcOp; |
| 615 | auto *entryBlock = funcOp.addEntryBlock(); |
| 616 | LLVM_DEBUG({ |
| 617 | logger.startLine() |
| 618 | << "//===-------------------------------------------===//\n" ; |
| 619 | logger.startLine() << "[fn] name: " << fnName << "\n" ; |
| 620 | logger.startLine() << "[fn] type: " << fnType << "\n" ; |
| 621 | logger.startLine() << "[fn] ID: " << fnID << "\n" ; |
| 622 | logger.startLine() << "[fn] entry block: " << entryBlock << "\n" ; |
| 623 | logger.indent(); |
| 624 | }); |
| 625 | |
| 626 | SmallVector<Attribute> argAttrs; |
| 627 | argAttrs.resize(N: functionType.getNumInputs()); |
| 628 | |
| 629 | // Parse the op argument instructions |
| 630 | if (functionType.getNumInputs()) { |
| 631 | for (size_t i = 0, e = functionType.getNumInputs(); i != e; ++i) { |
| 632 | auto argType = functionType.getInput(i); |
| 633 | spirv::Opcode opcode = spirv::Opcode::OpNop; |
| 634 | ArrayRef<uint32_t> operands; |
| 635 | if (failed(Result: sliceInstruction(opcode, operands, |
| 636 | expectedOpcode: spirv::Opcode::OpFunctionParameter))) { |
| 637 | return failure(); |
| 638 | } |
| 639 | if (opcode != spirv::Opcode::OpFunctionParameter) { |
| 640 | return emitError( |
| 641 | loc: unknownLoc, |
| 642 | message: "missing OpFunctionParameter instruction for argument " ) |
| 643 | << i; |
| 644 | } |
| 645 | if (operands.size() != 2) { |
| 646 | return emitError( |
| 647 | loc: unknownLoc, |
| 648 | message: "expected result type and result <id> for OpFunctionParameter" ); |
| 649 | } |
| 650 | auto argDefinedType = getType(id: operands[0]); |
| 651 | if (!argDefinedType || argDefinedType != argType) { |
| 652 | return emitError(loc: unknownLoc, |
| 653 | message: "mismatch in argument type between function type " |
| 654 | "definition " ) |
| 655 | << functionType << " and argument type definition " |
| 656 | << argDefinedType << " at argument " << i; |
| 657 | } |
| 658 | if (getValue(id: operands[1])) { |
| 659 | return emitError(loc: unknownLoc, message: "duplicate definition of result <id> " ) |
| 660 | << operands[1]; |
| 661 | } |
| 662 | if (failed(Result: setFunctionArgAttrs(argID: operands[1], argAttrs, argIndex: i))) { |
| 663 | return failure(); |
| 664 | } |
| 665 | |
| 666 | auto argValue = funcOp.getArgument(idx: i); |
| 667 | valueMap[operands[1]] = argValue; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (llvm::any_of(Range&: argAttrs, P: [](Attribute attr) { |
| 672 | auto argAttr = cast<DictionaryAttr>(Val&: attr); |
| 673 | return !argAttr.empty(); |
| 674 | })) |
| 675 | funcOp.setArgAttrsAttr(ArrayAttr::get(context, value: argAttrs)); |
| 676 | |
| 677 | // entryBlock is needed to access the arguments, Once that is done, we can |
| 678 | // erase the block for functions with 'Import' LinkageAttributes, since these |
| 679 | // are essentially function declarations, so they have no body. |
| 680 | auto linkageAttr = funcOp.getLinkageAttributes(); |
| 681 | auto hasImportLinkage = |
| 682 | linkageAttr && (linkageAttr.value().getLinkageType().getValue() == |
| 683 | spirv::LinkageType::Import); |
| 684 | if (hasImportLinkage) |
| 685 | funcOp.eraseBody(); |
| 686 | |
| 687 | // RAII guard to reset the insertion point to the module's region after |
| 688 | // deserializing the body of this function. |
| 689 | OpBuilder::InsertionGuard moduleInsertionGuard(opBuilder); |
| 690 | |
| 691 | spirv::Opcode opcode = spirv::Opcode::OpNop; |
| 692 | ArrayRef<uint32_t> instOperands; |
| 693 | |
| 694 | // Special handling for the entry block. We need to make sure it starts with |
| 695 | // an OpLabel instruction. The entry block takes the same parameters as the |
| 696 | // function. All other blocks do not take any parameter. We have already |
| 697 | // created the entry block, here we need to register it to the correct label |
| 698 | // <id>. |
| 699 | if (failed(Result: sliceInstruction(opcode, operands&: instOperands, |
| 700 | expectedOpcode: spirv::Opcode::OpFunctionEnd))) { |
| 701 | return failure(); |
| 702 | } |
| 703 | if (opcode == spirv::Opcode::OpFunctionEnd) { |
| 704 | return processFunctionEnd(operands: instOperands); |
| 705 | } |
| 706 | if (opcode != spirv::Opcode::OpLabel) { |
| 707 | return emitError(loc: unknownLoc, message: "a basic block must start with OpLabel" ); |
| 708 | } |
| 709 | if (instOperands.size() != 1) { |
| 710 | return emitError(loc: unknownLoc, message: "OpLabel should only have result <id>" ); |
| 711 | } |
| 712 | blockMap[instOperands[0]] = entryBlock; |
| 713 | if (failed(Result: processLabel(operands: instOperands))) { |
| 714 | return failure(); |
| 715 | } |
| 716 | |
| 717 | // Then process all the other instructions in the function until we hit |
| 718 | // OpFunctionEnd. |
| 719 | while (succeeded(Result: sliceInstruction(opcode, operands&: instOperands, |
| 720 | expectedOpcode: spirv::Opcode::OpFunctionEnd)) && |
| 721 | opcode != spirv::Opcode::OpFunctionEnd) { |
| 722 | if (failed(Result: processInstruction(opcode, operands: instOperands))) { |
| 723 | return failure(); |
| 724 | } |
| 725 | } |
| 726 | if (opcode != spirv::Opcode::OpFunctionEnd) { |
| 727 | return failure(); |
| 728 | } |
| 729 | |
| 730 | return processFunctionEnd(operands: instOperands); |
| 731 | } |
| 732 | |
| 733 | LogicalResult |
| 734 | spirv::Deserializer::processFunctionEnd(ArrayRef<uint32_t> operands) { |
| 735 | // Process OpFunctionEnd. |
| 736 | if (!operands.empty()) { |
| 737 | return emitError(loc: unknownLoc, message: "unexpected operands for OpFunctionEnd" ); |
| 738 | } |
| 739 | |
| 740 | // Wire up block arguments from OpPhi instructions. |
| 741 | // Put all structured control flow in spirv.mlir.selection/spirv.mlir.loop |
| 742 | // ops. |
| 743 | if (failed(Result: wireUpBlockArgument()) || failed(Result: structurizeControlFlow())) { |
| 744 | return failure(); |
| 745 | } |
| 746 | |
| 747 | blockConstantMap.clear(); |
| 748 | curBlock = nullptr; |
| 749 | curFunction = std::nullopt; |
| 750 | |
| 751 | LLVM_DEBUG({ |
| 752 | logger.unindent(); |
| 753 | logger.startLine() |
| 754 | << "//===-------------------------------------------===//\n" ; |
| 755 | }); |
| 756 | return success(); |
| 757 | } |
| 758 | |
| 759 | LogicalResult |
| 760 | spirv::Deserializer::processGraphEntryPointARM(ArrayRef<uint32_t> operands) { |
| 761 | if (operands.size() < 2) { |
| 762 | return emitError(loc: unknownLoc, |
| 763 | message: "missing graph defintion in OpGraphEntryPointARM" ); |
| 764 | } |
| 765 | |
| 766 | unsigned wordIndex = 0; |
| 767 | uint32_t graphID = operands[wordIndex++]; |
| 768 | if (!graphMap.contains(Val: graphID)) { |
| 769 | return emitError(loc: unknownLoc, |
| 770 | message: "missing graph definition/declaration with id " ) |
| 771 | << graphID; |
| 772 | } |
| 773 | |
| 774 | spirv::GraphARMOp graphARM = graphMap[graphID]; |
| 775 | StringRef name = decodeStringLiteral(words: operands, wordIndex); |
| 776 | graphARM.setSymName(name); |
| 777 | graphARM.setEntryPoint(true); |
| 778 | |
| 779 | SmallVector<Attribute, 4> interface; |
| 780 | for (int64_t size = operands.size(); wordIndex < size; ++wordIndex) { |
| 781 | if (spirv::GlobalVariableOp arg = getGlobalVariable(id: operands[wordIndex])) { |
| 782 | interface.push_back(Elt: SymbolRefAttr::get(symbol: arg.getOperation())); |
| 783 | } else { |
| 784 | return emitError(loc: unknownLoc, message: "undefined result <id> " ) |
| 785 | << operands[wordIndex] << " while decoding OpGraphEntryPoint" ; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | // RAII guard to reset the insertion point to previous value when done. |
| 790 | OpBuilder::InsertionGuard insertionGuard(opBuilder); |
| 791 | opBuilder.setInsertionPoint(graphARM); |
| 792 | spirv::GraphEntryPointARMOp::create( |
| 793 | builder&: opBuilder, location: unknownLoc, fn: SymbolRefAttr::get(ctx: opBuilder.getContext(), value: name), |
| 794 | interface: opBuilder.getArrayAttr(value: interface)); |
| 795 | |
| 796 | return success(); |
| 797 | } |
| 798 | |
| 799 | LogicalResult |
| 800 | spirv::Deserializer::processGraphARM(ArrayRef<uint32_t> operands) { |
| 801 | if (curGraph) { |
| 802 | return emitError(loc: unknownLoc, message: "found graph inside graph" ); |
| 803 | } |
| 804 | // Get the result type. |
| 805 | if (operands.size() < 2) { |
| 806 | return emitError(loc: unknownLoc, message: "OpGraphARM must have at least 2 parameters" ); |
| 807 | } |
| 808 | |
| 809 | Type type = getType(id: operands[0]); |
| 810 | if (!type || !isa<GraphType>(Val: type)) { |
| 811 | return emitError(loc: unknownLoc, message: "unknown graph type from <id> " ) |
| 812 | << operands[0]; |
| 813 | } |
| 814 | auto graphType = cast<GraphType>(Val&: type); |
| 815 | if (graphType.getNumResults() <= 0) { |
| 816 | return emitError(loc: unknownLoc, message: "expected at least one result" ); |
| 817 | } |
| 818 | |
| 819 | uint32_t graphID = operands[1]; |
| 820 | if (graphMap.count(Val: graphID)) { |
| 821 | return emitError(loc: unknownLoc, message: "duplicate graph definition/declaration" ); |
| 822 | } |
| 823 | |
| 824 | std::string graphName = getGraphSymbol(id: graphID); |
| 825 | auto graphOp = |
| 826 | spirv::GraphARMOp::create(builder&: opBuilder, location: unknownLoc, name: graphName, type: graphType); |
| 827 | curGraph = graphMap[graphID] = graphOp; |
| 828 | Block *entryBlock = graphOp.addEntryBlock(); |
| 829 | LLVM_DEBUG({ |
| 830 | logger.startLine() |
| 831 | << "//===-------------------------------------------===//\n" ; |
| 832 | logger.startLine() << "[graph] name: " << graphName << "\n" ; |
| 833 | logger.startLine() << "[graph] type: " << graphType << "\n" ; |
| 834 | logger.startLine() << "[graph] ID: " << graphID << "\n" ; |
| 835 | logger.startLine() << "[graph] entry block: " << entryBlock << "\n" ; |
| 836 | logger.indent(); |
| 837 | }); |
| 838 | |
| 839 | // Parse the op argument instructions. |
| 840 | for (auto [index, argType] : llvm::enumerate(First: graphType.getInputs())) { |
| 841 | spirv::Opcode opcode; |
| 842 | ArrayRef<uint32_t> operands; |
| 843 | if (failed(Result: sliceInstruction(opcode, operands, |
| 844 | expectedOpcode: spirv::Opcode::OpGraphInputARM))) { |
| 845 | return failure(); |
| 846 | } |
| 847 | if (operands.size() != 3) { |
| 848 | return emitError(loc: unknownLoc, message: "expected result type, result <id> and " |
| 849 | "input index for OpGraphInputARM" ); |
| 850 | } |
| 851 | |
| 852 | Type argDefinedType = getType(id: operands[0]); |
| 853 | if (!argDefinedType) { |
| 854 | return emitError(loc: unknownLoc, message: "unknown operand type <id> " ) << operands[0]; |
| 855 | } |
| 856 | |
| 857 | if (argDefinedType != argType) { |
| 858 | return emitError(loc: unknownLoc, |
| 859 | message: "mismatch in argument type between graph type " |
| 860 | "definition " ) |
| 861 | << graphType << " and argument type definition " << argDefinedType |
| 862 | << " at argument " << index; |
| 863 | } |
| 864 | if (getValue(id: operands[1])) { |
| 865 | return emitError(loc: unknownLoc, message: "duplicate definition of result <id> " ) |
| 866 | << operands[1]; |
| 867 | } |
| 868 | |
| 869 | IntegerAttr inputIndexAttr = getConstantInt(id: operands[2]); |
| 870 | if (!inputIndexAttr) { |
| 871 | return emitError(loc: unknownLoc, |
| 872 | message: "unable to read inputIndex value from constant op " ) |
| 873 | << operands[2]; |
| 874 | } |
| 875 | BlockArgument argValue = graphOp.getArgument(idx: inputIndexAttr.getInt()); |
| 876 | valueMap[operands[1]] = argValue; |
| 877 | } |
| 878 | |
| 879 | graphOutputs.resize(N: graphType.getNumResults()); |
| 880 | |
| 881 | // RAII guard to reset the insertion point to the module's region after |
| 882 | // deserializing the body of this function. |
| 883 | OpBuilder::InsertionGuard moduleInsertionGuard(opBuilder); |
| 884 | |
| 885 | blockMap[graphID] = entryBlock; |
| 886 | if (failed(Result: createGraphBlock(graphID))) { |
| 887 | return failure(); |
| 888 | } |
| 889 | |
| 890 | // Process all the instructions in the graph until and including |
| 891 | // OpGraphEndARM. |
| 892 | spirv::Opcode opcode; |
| 893 | ArrayRef<uint32_t> instOperands; |
| 894 | do { |
| 895 | if (failed(Result: sliceInstruction(opcode, operands&: instOperands, expectedOpcode: std::nullopt))) { |
| 896 | return failure(); |
| 897 | } |
| 898 | |
| 899 | if (failed(Result: processInstruction(opcode, operands: instOperands))) { |
| 900 | return failure(); |
| 901 | } |
| 902 | } while (opcode != spirv::Opcode::OpGraphEndARM); |
| 903 | |
| 904 | return success(); |
| 905 | } |
| 906 | |
| 907 | LogicalResult |
| 908 | spirv::Deserializer::processOpGraphSetOutputARM(ArrayRef<uint32_t> operands) { |
| 909 | if (operands.size() != 2) { |
| 910 | return emitError( |
| 911 | loc: unknownLoc, |
| 912 | message: "expected value id and output index for OpGraphSetOutputARM" ); |
| 913 | } |
| 914 | |
| 915 | uint32_t id = operands[0]; |
| 916 | Value value = getValue(id); |
| 917 | if (!value) { |
| 918 | return emitError(loc: unknownLoc, message: "could not find result <id> " ) << id; |
| 919 | } |
| 920 | |
| 921 | IntegerAttr outputIndexAttr = getConstantInt(id: operands[1]); |
| 922 | if (!outputIndexAttr) { |
| 923 | return emitError(loc: unknownLoc, |
| 924 | message: "unable to read outputIndex value from constant op " ) |
| 925 | << operands[1]; |
| 926 | } |
| 927 | graphOutputs[outputIndexAttr.getInt()] = value; |
| 928 | return success(); |
| 929 | } |
| 930 | |
| 931 | LogicalResult |
| 932 | spirv::Deserializer::processGraphEndARM(ArrayRef<uint32_t> operands) { |
| 933 | // Create GraphOutputsARM instruction. |
| 934 | spirv::GraphOutputsARMOp::create(builder&: opBuilder, location: unknownLoc, value: graphOutputs); |
| 935 | |
| 936 | // Process OpGraphEndARM. |
| 937 | if (!operands.empty()) { |
| 938 | return emitError(loc: unknownLoc, message: "unexpected operands for OpGraphEndARM" ); |
| 939 | } |
| 940 | |
| 941 | blockConstantMap.clear(); |
| 942 | curBlock = nullptr; |
| 943 | curGraph = std::nullopt; |
| 944 | graphOutputs.clear(); |
| 945 | |
| 946 | LLVM_DEBUG({ |
| 947 | logger.unindent(); |
| 948 | logger.startLine() |
| 949 | << "//===-------------------------------------------===//\n" ; |
| 950 | }); |
| 951 | return success(); |
| 952 | } |
| 953 | |
| 954 | std::optional<std::pair<Attribute, Type>> |
| 955 | spirv::Deserializer::getConstant(uint32_t id) { |
| 956 | auto constIt = constantMap.find(Val: id); |
| 957 | if (constIt != constantMap.end()) |
| 958 | return constIt->getSecond(); |
| 959 | |
| 960 | auto replicatedConstIt = constantCompositeReplicateMap.find(Val: id); |
| 961 | if (replicatedConstIt == constantCompositeReplicateMap.end()) |
| 962 | return std::nullopt; |
| 963 | |
| 964 | auto [value, type] = replicatedConstIt->getSecond(); |
| 965 | auto shapedType = dyn_cast<ShapedType>(Val&: type); |
| 966 | if (!shapedType) |
| 967 | return std::nullopt; |
| 968 | return std::make_pair(x: SplatElementsAttr::get(type: shapedType, values: value), y&: type); |
| 969 | } |
| 970 | |
| 971 | std::optional<std::pair<Attribute, Type>> |
| 972 | spirv::Deserializer::getConstantCompositeReplicate(uint32_t id) { |
| 973 | if (auto it = constantCompositeReplicateMap.find(Val: id); |
| 974 | it != constantCompositeReplicateMap.end()) |
| 975 | return it->second; |
| 976 | return std::nullopt; |
| 977 | } |
| 978 | |
| 979 | std::optional<spirv::SpecConstOperationMaterializationInfo> |
| 980 | spirv::Deserializer::getSpecConstantOperation(uint32_t id) { |
| 981 | auto constIt = specConstOperationMap.find(Val: id); |
| 982 | if (constIt == specConstOperationMap.end()) |
| 983 | return std::nullopt; |
| 984 | return constIt->getSecond(); |
| 985 | } |
| 986 | |
| 987 | std::string spirv::Deserializer::getFunctionSymbol(uint32_t id) { |
| 988 | auto funcName = nameMap.lookup(Val: id).str(); |
| 989 | if (funcName.empty()) { |
| 990 | funcName = "spirv_fn_" + std::to_string(val: id); |
| 991 | } |
| 992 | return funcName; |
| 993 | } |
| 994 | |
| 995 | std::string spirv::Deserializer::getGraphSymbol(uint32_t id) { |
| 996 | std::string graphName = nameMap.lookup(Val: id).str(); |
| 997 | if (graphName.empty()) { |
| 998 | graphName = "spirv_graph_" + std::to_string(val: id); |
| 999 | } |
| 1000 | return graphName; |
| 1001 | } |
| 1002 | |
| 1003 | std::string spirv::Deserializer::getSpecConstantSymbol(uint32_t id) { |
| 1004 | auto constName = nameMap.lookup(Val: id).str(); |
| 1005 | if (constName.empty()) { |
| 1006 | constName = "spirv_spec_const_" + std::to_string(val: id); |
| 1007 | } |
| 1008 | return constName; |
| 1009 | } |
| 1010 | |
| 1011 | spirv::SpecConstantOp |
| 1012 | spirv::Deserializer::createSpecConstant(Location loc, uint32_t resultID, |
| 1013 | TypedAttr defaultValue) { |
| 1014 | auto symName = opBuilder.getStringAttr(bytes: getSpecConstantSymbol(id: resultID)); |
| 1015 | auto op = spirv::SpecConstantOp::create(builder&: opBuilder, location: unknownLoc, sym_name: symName, |
| 1016 | default_value: defaultValue, |
| 1017 | /*sym_visibility=*/nullptr); |
| 1018 | if (decorations.count(Val: resultID)) { |
| 1019 | for (auto attr : decorations[resultID].getAttrs()) |
| 1020 | setInherentOrDiscardableAttr(op, name: attr.getName(), value: attr.getValue()); |
| 1021 | } |
| 1022 | specConstMap[resultID] = op; |
| 1023 | return op; |
| 1024 | } |
| 1025 | |
| 1026 | std::optional<spirv::GraphConstantARMOpMaterializationInfo> |
| 1027 | spirv::Deserializer::getGraphConstantARM(uint32_t id) { |
| 1028 | auto graphConstIt = graphConstantMap.find(Val: id); |
| 1029 | if (graphConstIt == graphConstantMap.end()) |
| 1030 | return std::nullopt; |
| 1031 | return graphConstIt->getSecond(); |
| 1032 | } |
| 1033 | |
| 1034 | LogicalResult |
| 1035 | spirv::Deserializer::processGlobalVariable(ArrayRef<uint32_t> operands) { |
| 1036 | unsigned wordIndex = 0; |
| 1037 | if (operands.size() < 3) { |
| 1038 | return emitError( |
| 1039 | loc: unknownLoc, |
| 1040 | message: "OpVariable needs at least 3 operands, type, <id> and storage class" ); |
| 1041 | } |
| 1042 | |
| 1043 | // Result Type. |
| 1044 | auto type = getType(id: operands[wordIndex]); |
| 1045 | if (!type) { |
| 1046 | return emitError(loc: unknownLoc, message: "unknown result type <id> : " ) |
| 1047 | << operands[wordIndex]; |
| 1048 | } |
| 1049 | auto ptrType = dyn_cast<spirv::PointerType>(Val&: type); |
| 1050 | if (!ptrType) { |
| 1051 | return emitError(loc: unknownLoc, |
| 1052 | message: "expected a result type <id> to be a spirv.ptr, found : " ) |
| 1053 | << type; |
| 1054 | } |
| 1055 | wordIndex++; |
| 1056 | |
| 1057 | // Result <id>. |
| 1058 | auto variableID = operands[wordIndex]; |
| 1059 | auto variableName = nameMap.lookup(Val: variableID).str(); |
| 1060 | if (variableName.empty()) { |
| 1061 | variableName = "spirv_var_" + std::to_string(val: variableID); |
| 1062 | } |
| 1063 | wordIndex++; |
| 1064 | |
| 1065 | // Storage class. |
| 1066 | auto storageClass = static_cast<spirv::StorageClass>(operands[wordIndex]); |
| 1067 | if (ptrType.getStorageClass() != storageClass) { |
| 1068 | return emitError(loc: unknownLoc, message: "mismatch in storage class of pointer type " ) |
| 1069 | << type << " and that specified in OpVariable instruction : " |
| 1070 | << stringifyStorageClass(storageClass); |
| 1071 | } |
| 1072 | wordIndex++; |
| 1073 | |
| 1074 | // Initializer. |
| 1075 | FlatSymbolRefAttr initializer = nullptr; |
| 1076 | |
| 1077 | if (wordIndex < operands.size()) { |
| 1078 | Operation *op = nullptr; |
| 1079 | |
| 1080 | if (auto initOp = getGlobalVariable(id: operands[wordIndex])) |
| 1081 | op = initOp; |
| 1082 | else if (auto initOp = getSpecConstant(id: operands[wordIndex])) |
| 1083 | op = initOp; |
| 1084 | else if (auto initOp = getSpecConstantComposite(id: operands[wordIndex])) |
| 1085 | op = initOp; |
| 1086 | else |
| 1087 | return emitError(loc: unknownLoc, message: "unknown <id> " ) |
| 1088 | << operands[wordIndex] << "used as initializer" ; |
| 1089 | |
| 1090 | initializer = SymbolRefAttr::get(symbol: op); |
| 1091 | wordIndex++; |
| 1092 | } |
| 1093 | if (wordIndex != operands.size()) { |
| 1094 | return emitError(loc: unknownLoc, |
| 1095 | message: "found more operands than expected when deserializing " |
| 1096 | "OpVariable instruction, only " ) |
| 1097 | << wordIndex << " of " << operands.size() << " processed" ; |
| 1098 | } |
| 1099 | auto loc = createFileLineColLoc(opBuilder); |
| 1100 | auto varOp = spirv::GlobalVariableOp::create( |
| 1101 | builder&: opBuilder, location: loc, type: TypeAttr::get(type), |
| 1102 | sym_name: opBuilder.getStringAttr(bytes: variableName), initializer); |
| 1103 | |
| 1104 | // Decorations. |
| 1105 | if (decorations.count(Val: variableID)) { |
| 1106 | for (auto attr : decorations[variableID].getAttrs()) |
| 1107 | setInherentOrDiscardableAttr(op: varOp, name: attr.getName(), value: attr.getValue()); |
| 1108 | } |
| 1109 | globalVariableMap[variableID] = varOp; |
| 1110 | return success(); |
| 1111 | } |
| 1112 | |
| 1113 | IntegerAttr spirv::Deserializer::getConstantInt(uint32_t id) { |
| 1114 | auto constInfo = getConstant(id); |
| 1115 | if (!constInfo) { |
| 1116 | return nullptr; |
| 1117 | } |
| 1118 | return dyn_cast<IntegerAttr>(Val&: constInfo->first); |
| 1119 | } |
| 1120 | |
| 1121 | LogicalResult spirv::Deserializer::processName(ArrayRef<uint32_t> operands) { |
| 1122 | if (operands.size() < 2) { |
| 1123 | return emitError(loc: unknownLoc, message: "OpName needs at least 2 operands" ); |
| 1124 | } |
| 1125 | |
| 1126 | unsigned wordIndex = 1; |
| 1127 | StringRef name = decodeStringLiteral(words: operands, wordIndex); |
| 1128 | if (wordIndex != operands.size()) { |
| 1129 | return emitError(loc: unknownLoc, |
| 1130 | message: "unexpected trailing words in OpName instruction" ); |
| 1131 | } |
| 1132 | |
| 1133 | // In SPIRV it's valid for multiple OpName instructions to refer to the same |
| 1134 | // <id>. Use a "last one wins" approach to resolve such cases. |
| 1135 | nameMap.emplace_or_assign(Key: operands[0], Args&: name); |
| 1136 | |
| 1137 | return success(); |
| 1138 | } |
| 1139 | |
| 1140 | //===----------------------------------------------------------------------===// |
| 1141 | // Type |
| 1142 | //===----------------------------------------------------------------------===// |
| 1143 | |
| 1144 | LogicalResult spirv::Deserializer::processType(spirv::Opcode opcode, |
| 1145 | ArrayRef<uint32_t> operands) { |
| 1146 | if (operands.empty()) { |
| 1147 | return emitError(loc: unknownLoc, message: "type instruction with opcode " ) |
| 1148 | << spirv::stringifyOpcode(opcode) << " needs at least one <id>" ; |
| 1149 | } |
| 1150 | |
| 1151 | /// TODO: Types might be forward declared in some instructions and need to be |
| 1152 | /// handled appropriately. |
| 1153 | if (typeMap.count(Val: operands[0])) { |
| 1154 | return emitError(loc: unknownLoc, message: "duplicate definition for result <id> " ) |
| 1155 | << operands[0]; |
| 1156 | } |
| 1157 | |
| 1158 | switch (opcode) { |
| 1159 | case spirv::Opcode::OpTypeVoid: |
| 1160 | if (operands.size() != 1) |
| 1161 | return emitError(loc: unknownLoc, message: "OpTypeVoid must have no parameters" ); |
| 1162 | typeMap[operands[0]] = opBuilder.getNoneType(); |
| 1163 | break; |
| 1164 | case spirv::Opcode::OpTypeBool: |
| 1165 | if (operands.size() != 1) |
| 1166 | return emitError(loc: unknownLoc, message: "OpTypeBool must have no parameters" ); |
| 1167 | typeMap[operands[0]] = opBuilder.getI1Type(); |
| 1168 | break; |
| 1169 | case spirv::Opcode::OpTypeInt: { |
| 1170 | if (operands.size() != 3) |
| 1171 | return emitError( |
| 1172 | loc: unknownLoc, message: "OpTypeInt must have bitwidth and signedness parameters" ); |
| 1173 | |
| 1174 | // SPIR-V OpTypeInt "Signedness specifies whether there are signed semantics |
| 1175 | // to preserve or validate. |
| 1176 | // 0 indicates unsigned, or no signedness semantics |
| 1177 | // 1 indicates signed semantics." |
| 1178 | // |
| 1179 | // So we cannot differentiate signless and unsigned integers; always use |
| 1180 | // signless semantics for such cases. |
| 1181 | auto sign = operands[2] == 1 ? IntegerType::SignednessSemantics::Signed |
| 1182 | : IntegerType::SignednessSemantics::Signless; |
| 1183 | typeMap[operands[0]] = IntegerType::get(context, width: operands[1], signedness: sign); |
| 1184 | } break; |
| 1185 | case spirv::Opcode::OpTypeFloat: { |
| 1186 | if (operands.size() != 2 && operands.size() != 3) |
| 1187 | return emitError(loc: unknownLoc, |
| 1188 | message: "OpTypeFloat expects either 2 operands (type, bitwidth) " |
| 1189 | "or 3 operands (type, bitwidth, encoding), but got " ) |
| 1190 | << operands.size(); |
| 1191 | uint32_t bitWidth = operands[1]; |
| 1192 | |
| 1193 | Type floatTy; |
| 1194 | if (operands.size() == 2) { |
| 1195 | switch (bitWidth) { |
| 1196 | case 16: |
| 1197 | floatTy = opBuilder.getF16Type(); |
| 1198 | break; |
| 1199 | case 32: |
| 1200 | floatTy = opBuilder.getF32Type(); |
| 1201 | break; |
| 1202 | case 64: |
| 1203 | floatTy = opBuilder.getF64Type(); |
| 1204 | break; |
| 1205 | default: |
| 1206 | return emitError(loc: unknownLoc, message: "unsupported OpTypeFloat bitwidth: " ) |
| 1207 | << bitWidth; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (operands.size() == 3) { |
| 1212 | if (spirv::FPEncoding(operands[2]) == spirv::FPEncoding::BFloat16KHR && |
| 1213 | bitWidth == 16) |
| 1214 | floatTy = opBuilder.getBF16Type(); |
| 1215 | else if (spirv::FPEncoding(operands[2]) == |
| 1216 | spirv::FPEncoding::Float8E4M3EXT && |
| 1217 | bitWidth == 8) |
| 1218 | floatTy = opBuilder.getF8E4M3FNType(); |
| 1219 | else if (spirv::FPEncoding(operands[2]) == |
| 1220 | spirv::FPEncoding::Float8E5M2EXT && |
| 1221 | bitWidth == 8) |
| 1222 | floatTy = opBuilder.getF8E5M2Type(); |
| 1223 | else |
| 1224 | return emitError(loc: unknownLoc, message: "unsupported OpTypeFloat FP encoding: " ) |
| 1225 | << operands[2] << " and bitWidth " << bitWidth; |
| 1226 | } |
| 1227 | |
| 1228 | typeMap[operands[0]] = floatTy; |
| 1229 | } break; |
| 1230 | case spirv::Opcode::OpTypeVector: { |
| 1231 | if (operands.size() != 3) { |
| 1232 | return emitError( |
| 1233 | loc: unknownLoc, |
| 1234 | message: "OpTypeVector must have element type and count parameters" ); |
| 1235 | } |
| 1236 | Type elementTy = getType(id: operands[1]); |
| 1237 | if (!elementTy) { |
| 1238 | return emitError(loc: unknownLoc, message: "OpTypeVector references undefined <id> " ) |
| 1239 | << operands[1]; |
| 1240 | } |
| 1241 | typeMap[operands[0]] = VectorType::get(shape: {operands[2]}, elementType: elementTy); |
| 1242 | } break; |
| 1243 | case spirv::Opcode::OpTypePointer: { |
| 1244 | return processOpTypePointer(operands); |
| 1245 | } break; |
| 1246 | case spirv::Opcode::OpTypeArray: |
| 1247 | return processArrayType(operands); |
| 1248 | case spirv::Opcode::OpTypeCooperativeMatrixKHR: |
| 1249 | return processCooperativeMatrixTypeKHR(operands); |
| 1250 | case spirv::Opcode::OpTypeFunction: |
| 1251 | return processFunctionType(operands); |
| 1252 | case spirv::Opcode::OpTypeImage: |
| 1253 | return processImageType(operands); |
| 1254 | case spirv::Opcode::OpTypeSampler: |
| 1255 | return processSamplerType(operands); |
| 1256 | case spirv::Opcode::OpTypeNamedBarrier: |
| 1257 | return processNamedBarrierType(operands); |
| 1258 | case spirv::Opcode::OpTypeSampledImage: |
| 1259 | return processSampledImageType(operands); |
| 1260 | case spirv::Opcode::OpTypeRuntimeArray: |
| 1261 | return processRuntimeArrayType(operands); |
| 1262 | case spirv::Opcode::OpTypeStruct: |
| 1263 | return processStructType(operands); |
| 1264 | case spirv::Opcode::OpTypeMatrix: |
| 1265 | return processMatrixType(operands); |
| 1266 | case spirv::Opcode::OpTypeTensorARM: |
| 1267 | return processTensorARMType(operands); |
| 1268 | case spirv::Opcode::OpTypeGraphARM: |
| 1269 | return processGraphTypeARM(operands); |
| 1270 | default: |
| 1271 | return emitError(loc: unknownLoc, message: "unhandled type instruction" ); |
| 1272 | } |
| 1273 | return success(); |
| 1274 | } |
| 1275 | |
| 1276 | LogicalResult |
| 1277 | spirv::Deserializer::processOpTypePointer(ArrayRef<uint32_t> operands) { |
| 1278 | if (operands.size() != 3) |
| 1279 | return emitError(loc: unknownLoc, message: "OpTypePointer must have two parameters" ); |
| 1280 | |
| 1281 | auto pointeeType = getType(id: operands[2]); |
| 1282 | if (!pointeeType) |
| 1283 | return emitError(loc: unknownLoc, message: "unknown OpTypePointer pointee type <id> " ) |
| 1284 | << operands[2]; |
| 1285 | |
| 1286 | uint32_t typePointerID = operands[0]; |
| 1287 | auto storageClass = static_cast<spirv::StorageClass>(operands[1]); |
| 1288 | typeMap[typePointerID] = spirv::PointerType::get(pointeeType, storageClass); |
| 1289 | |
| 1290 | for (auto *deferredStructIt = std::begin(cont&: deferredStructTypesInfos); |
| 1291 | deferredStructIt != std::end(cont&: deferredStructTypesInfos);) { |
| 1292 | for (auto *unresolvedMemberIt = |
| 1293 | std::begin(cont&: deferredStructIt->unresolvedMemberTypes); |
| 1294 | unresolvedMemberIt != |
| 1295 | std::end(cont&: deferredStructIt->unresolvedMemberTypes);) { |
| 1296 | if (unresolvedMemberIt->first == typePointerID) { |
| 1297 | // The newly constructed pointer type can resolve one of the |
| 1298 | // deferred struct type members; update the memberTypes list and |
| 1299 | // clean the unresolvedMemberTypes list accordingly. |
| 1300 | deferredStructIt->memberTypes[unresolvedMemberIt->second] = |
| 1301 | typeMap[typePointerID]; |
| 1302 | unresolvedMemberIt = |
| 1303 | deferredStructIt->unresolvedMemberTypes.erase(CI: unresolvedMemberIt); |
| 1304 | } else { |
| 1305 | ++unresolvedMemberIt; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | if (deferredStructIt->unresolvedMemberTypes.empty()) { |
| 1310 | // All deferred struct type members are now resolved, set the struct body. |
| 1311 | auto structType = deferredStructIt->deferredStructType; |
| 1312 | |
| 1313 | assert(structType && "expected a spirv::StructType" ); |
| 1314 | assert(structType.isIdentified() && "expected an indentified struct" ); |
| 1315 | |
| 1316 | if (failed(Result: structType.trySetBody( |
| 1317 | memberTypes: deferredStructIt->memberTypes, offsetInfo: deferredStructIt->offsetInfo, |
| 1318 | memberDecorations: deferredStructIt->memberDecorationsInfo, |
| 1319 | structDecorations: deferredStructIt->structDecorationsInfo))) |
| 1320 | return failure(); |
| 1321 | |
| 1322 | deferredStructIt = deferredStructTypesInfos.erase(CI: deferredStructIt); |
| 1323 | } else { |
| 1324 | ++deferredStructIt; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | return success(); |
| 1329 | } |
| 1330 | |
| 1331 | LogicalResult |
| 1332 | spirv::Deserializer::processArrayType(ArrayRef<uint32_t> operands) { |
| 1333 | if (operands.size() != 3) { |
| 1334 | return emitError(loc: unknownLoc, |
| 1335 | message: "OpTypeArray must have element type and count parameters" ); |
| 1336 | } |
| 1337 | |
| 1338 | Type elementTy = getType(id: operands[1]); |
| 1339 | if (!elementTy) { |
| 1340 | return emitError(loc: unknownLoc, message: "OpTypeArray references undefined <id> " ) |
| 1341 | << operands[1]; |
| 1342 | } |
| 1343 | |
| 1344 | unsigned count = 0; |
| 1345 | // TODO: The count can also come frome a specialization constant. |
| 1346 | auto countInfo = getConstant(id: operands[2]); |
| 1347 | if (!countInfo) { |
| 1348 | return emitError(loc: unknownLoc, message: "OpTypeArray count <id> " ) |
| 1349 | << operands[2] << "can only come from normal constant right now" ; |
| 1350 | } |
| 1351 | |
| 1352 | if (auto intVal = dyn_cast<IntegerAttr>(Val&: countInfo->first)) { |
| 1353 | count = intVal.getValue().getZExtValue(); |
| 1354 | } else { |
| 1355 | return emitError(loc: unknownLoc, message: "OpTypeArray count must come from a " |
| 1356 | "scalar integer constant instruction" ); |
| 1357 | } |
| 1358 | |
| 1359 | typeMap[operands[0]] = spirv::ArrayType::get( |
| 1360 | elementType: elementTy, elementCount: count, stride: typeDecorations.lookup(Val: operands[0])); |
| 1361 | return success(); |
| 1362 | } |
| 1363 | |
| 1364 | LogicalResult |
| 1365 | spirv::Deserializer::processFunctionType(ArrayRef<uint32_t> operands) { |
| 1366 | assert(!operands.empty() && "No operands for processing function type" ); |
| 1367 | if (operands.size() == 1) { |
| 1368 | return emitError(loc: unknownLoc, message: "missing return type for OpTypeFunction" ); |
| 1369 | } |
| 1370 | auto returnType = getType(id: operands[1]); |
| 1371 | if (!returnType) { |
| 1372 | return emitError(loc: unknownLoc, message: "unknown return type in OpTypeFunction" ); |
| 1373 | } |
| 1374 | SmallVector<Type, 1> argTypes; |
| 1375 | for (size_t i = 2, e = operands.size(); i < e; ++i) { |
| 1376 | auto ty = getType(id: operands[i]); |
| 1377 | if (!ty) { |
| 1378 | return emitError(loc: unknownLoc, message: "unknown argument type in OpTypeFunction" ); |
| 1379 | } |
| 1380 | argTypes.push_back(Elt: ty); |
| 1381 | } |
| 1382 | ArrayRef<Type> returnTypes; |
| 1383 | if (!isVoidType(type: returnType)) { |
| 1384 | returnTypes = llvm::ArrayRef(returnType); |
| 1385 | } |
| 1386 | typeMap[operands[0]] = FunctionType::get(context, inputs: argTypes, results: returnTypes); |
| 1387 | return success(); |
| 1388 | } |
| 1389 | |
| 1390 | LogicalResult spirv::Deserializer::processCooperativeMatrixTypeKHR( |
| 1391 | ArrayRef<uint32_t> operands) { |
| 1392 | if (operands.size() != 6) { |
| 1393 | return emitError(loc: unknownLoc, |
| 1394 | message: "OpTypeCooperativeMatrixKHR must have element type, " |
| 1395 | "scope, row and column parameters, and use" ); |
| 1396 | } |
| 1397 | |
| 1398 | Type elementTy = getType(id: operands[1]); |
| 1399 | if (!elementTy) { |
| 1400 | return emitError(loc: unknownLoc, |
| 1401 | message: "OpTypeCooperativeMatrixKHR references undefined <id> " ) |
| 1402 | << operands[1]; |
| 1403 | } |
| 1404 | |
| 1405 | std::optional<spirv::Scope> scope = |
| 1406 | spirv::symbolizeScope(getConstantInt(id: operands[2]).getInt()); |
| 1407 | if (!scope) { |
| 1408 | return emitError( |
| 1409 | loc: unknownLoc, |
| 1410 | message: "OpTypeCooperativeMatrixKHR references undefined scope <id> " ) |
| 1411 | << operands[2]; |
| 1412 | } |
| 1413 | |
| 1414 | IntegerAttr rowsAttr = getConstantInt(id: operands[3]); |
| 1415 | IntegerAttr columnsAttr = getConstantInt(id: operands[4]); |
| 1416 | IntegerAttr useAttr = getConstantInt(id: operands[5]); |
| 1417 | |
| 1418 | if (!rowsAttr) |
| 1419 | return emitError(loc: unknownLoc, message: "OpTypeCooperativeMatrixKHR `Rows` references " |
| 1420 | "undefined constant <id> " ) |
| 1421 | << operands[3]; |
| 1422 | |
| 1423 | if (!columnsAttr) |
| 1424 | return emitError(loc: unknownLoc, message: "OpTypeCooperativeMatrixKHR `Columns` " |
| 1425 | "references undefined constant <id> " ) |
| 1426 | << operands[4]; |
| 1427 | |
| 1428 | if (!useAttr) |
| 1429 | return emitError(loc: unknownLoc, message: "OpTypeCooperativeMatrixKHR `Use` references " |
| 1430 | "undefined constant <id> " ) |
| 1431 | << operands[5]; |
| 1432 | |
| 1433 | unsigned rows = rowsAttr.getInt(); |
| 1434 | unsigned columns = columnsAttr.getInt(); |
| 1435 | |
| 1436 | std::optional<spirv::CooperativeMatrixUseKHR> use = |
| 1437 | spirv::symbolizeCooperativeMatrixUseKHR(useAttr.getInt()); |
| 1438 | if (!use) { |
| 1439 | return emitError( |
| 1440 | loc: unknownLoc, |
| 1441 | message: "OpTypeCooperativeMatrixKHR references undefined use <id> " ) |
| 1442 | << operands[5]; |
| 1443 | } |
| 1444 | |
| 1445 | typeMap[operands[0]] = |
| 1446 | spirv::CooperativeMatrixType::get(elementType: elementTy, rows, columns, scope: *scope, use: *use); |
| 1447 | return success(); |
| 1448 | } |
| 1449 | |
| 1450 | LogicalResult |
| 1451 | spirv::Deserializer::processRuntimeArrayType(ArrayRef<uint32_t> operands) { |
| 1452 | if (operands.size() != 2) { |
| 1453 | return emitError(loc: unknownLoc, message: "OpTypeRuntimeArray must have two operands" ); |
| 1454 | } |
| 1455 | Type memberType = getType(id: operands[1]); |
| 1456 | if (!memberType) { |
| 1457 | return emitError(loc: unknownLoc, |
| 1458 | message: "OpTypeRuntimeArray references undefined <id> " ) |
| 1459 | << operands[1]; |
| 1460 | } |
| 1461 | typeMap[operands[0]] = spirv::RuntimeArrayType::get( |
| 1462 | elementType: memberType, stride: typeDecorations.lookup(Val: operands[0])); |
| 1463 | return success(); |
| 1464 | } |
| 1465 | |
| 1466 | LogicalResult |
| 1467 | spirv::Deserializer::processStructType(ArrayRef<uint32_t> operands) { |
| 1468 | // TODO: Find a way to handle identified structs when debug info is stripped. |
| 1469 | |
| 1470 | if (operands.empty()) { |
| 1471 | return emitError(loc: unknownLoc, message: "OpTypeStruct must have at least result <id>" ); |
| 1472 | } |
| 1473 | |
| 1474 | if (operands.size() == 1) { |
| 1475 | // Handle empty struct. |
| 1476 | typeMap[operands[0]] = |
| 1477 | spirv::StructType::getEmpty(context, identifier: nameMap.lookup(Val: operands[0]).str()); |
| 1478 | return success(); |
| 1479 | } |
| 1480 | |
| 1481 | // First element is operand ID, second element is member index in the struct. |
| 1482 | SmallVector<std::pair<uint32_t, unsigned>, 0> unresolvedMemberTypes; |
| 1483 | SmallVector<Type, 4> memberTypes; |
| 1484 | |
| 1485 | for (auto op : llvm::drop_begin(RangeOrContainer&: operands, N: 1)) { |
| 1486 | Type memberType = getType(id: op); |
| 1487 | bool typeForwardPtr = (typeForwardPointerIDs.count(key: op) != 0); |
| 1488 | |
| 1489 | if (!memberType && !typeForwardPtr) |
| 1490 | return emitError(loc: unknownLoc, message: "OpTypeStruct references undefined <id> " ) |
| 1491 | << op; |
| 1492 | |
| 1493 | if (!memberType) |
| 1494 | unresolvedMemberTypes.emplace_back(Args&: op, Args: memberTypes.size()); |
| 1495 | |
| 1496 | memberTypes.push_back(Elt: memberType); |
| 1497 | } |
| 1498 | |
| 1499 | SmallVector<spirv::StructType::OffsetInfo, 0> offsetInfo; |
| 1500 | SmallVector<spirv::StructType::MemberDecorationInfo, 0> memberDecorationsInfo; |
| 1501 | if (memberDecorationMap.count(Val: operands[0])) { |
| 1502 | auto &allMemberDecorations = memberDecorationMap[operands[0]]; |
| 1503 | for (auto memberIndex : llvm::seq<uint32_t>(Begin: 0, End: memberTypes.size())) { |
| 1504 | if (allMemberDecorations.count(Val: memberIndex)) { |
| 1505 | for (auto &memberDecoration : allMemberDecorations[memberIndex]) { |
| 1506 | // Check for offset. |
| 1507 | if (memberDecoration.first == spirv::Decoration::Offset) { |
| 1508 | // If offset info is empty, resize to the number of members; |
| 1509 | if (offsetInfo.empty()) { |
| 1510 | offsetInfo.resize(N: memberTypes.size()); |
| 1511 | } |
| 1512 | offsetInfo[memberIndex] = memberDecoration.second[0]; |
| 1513 | } else { |
| 1514 | auto intType = mlir::IntegerType::get(context, width: 32); |
| 1515 | if (!memberDecoration.second.empty()) { |
| 1516 | memberDecorationsInfo.emplace_back( |
| 1517 | Args&: memberIndex, Args&: memberDecoration.first, |
| 1518 | Args: IntegerAttr::get(type: intType, value: memberDecoration.second[0])); |
| 1519 | } else { |
| 1520 | memberDecorationsInfo.emplace_back( |
| 1521 | Args&: memberIndex, Args&: memberDecoration.first, Args: UnitAttr::get(context)); |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | SmallVector<spirv::StructType::StructDecorationInfo, 0> structDecorationsInfo; |
| 1530 | if (decorations.count(Val: operands[0])) { |
| 1531 | NamedAttrList &allDecorations = decorations[operands[0]]; |
| 1532 | for (NamedAttribute &decorationAttr : allDecorations) { |
| 1533 | std::optional<spirv::Decoration> decoration = spirv::symbolizeDecoration( |
| 1534 | llvm::convertToCamelFromSnakeCase(input: decorationAttr.getName(), capitalizeFirst: true)); |
| 1535 | assert(decoration.has_value()); |
| 1536 | structDecorationsInfo.emplace_back(Args&: decoration.value(), |
| 1537 | Args: decorationAttr.getValue()); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | uint32_t structID = operands[0]; |
| 1542 | std::string structIdentifier = nameMap.lookup(Val: structID).str(); |
| 1543 | |
| 1544 | if (structIdentifier.empty()) { |
| 1545 | assert(unresolvedMemberTypes.empty() && |
| 1546 | "didn't expect unresolved member types" ); |
| 1547 | typeMap[structID] = spirv::StructType::get( |
| 1548 | memberTypes, offsetInfo, memberDecorations: memberDecorationsInfo, structDecorations: structDecorationsInfo); |
| 1549 | } else { |
| 1550 | auto structTy = spirv::StructType::getIdentified(context, identifier: structIdentifier); |
| 1551 | typeMap[structID] = structTy; |
| 1552 | |
| 1553 | if (!unresolvedMemberTypes.empty()) |
| 1554 | deferredStructTypesInfos.push_back( |
| 1555 | Elt: {.deferredStructType: structTy, .unresolvedMemberTypes: std::move(unresolvedMemberTypes), .memberTypes: std::move(memberTypes), |
| 1556 | .offsetInfo: std::move(offsetInfo), .memberDecorationsInfo: std::move(memberDecorationsInfo), |
| 1557 | .structDecorationsInfo: std::move(structDecorationsInfo)}); |
| 1558 | else if (failed(Result: structTy.trySetBody(memberTypes, offsetInfo, |
| 1559 | memberDecorations: memberDecorationsInfo, |
| 1560 | structDecorations: structDecorationsInfo))) |
| 1561 | return failure(); |
| 1562 | } |
| 1563 | |
| 1564 | // TODO: Update StructType to have member name as attribute as |
| 1565 | // well. |
| 1566 | return success(); |
| 1567 | } |
| 1568 | |
| 1569 | LogicalResult |
| 1570 | spirv::Deserializer::processMatrixType(ArrayRef<uint32_t> operands) { |
| 1571 | if (operands.size() != 3) { |
| 1572 | // Three operands are needed: result_id, column_type, and column_count |
| 1573 | return emitError(loc: unknownLoc, message: "OpTypeMatrix must have 3 operands" |
| 1574 | " (result_id, column_type, and column_count)" ); |
| 1575 | } |
| 1576 | // Matrix columns must be of vector type |
| 1577 | Type elementTy = getType(id: operands[1]); |
| 1578 | if (!elementTy) { |
| 1579 | return emitError(loc: unknownLoc, |
| 1580 | message: "OpTypeMatrix references undefined column type." ) |
| 1581 | << operands[1]; |
| 1582 | } |
| 1583 | |
| 1584 | uint32_t colsCount = operands[2]; |
| 1585 | typeMap[operands[0]] = spirv::MatrixType::get(columnType: elementTy, columnCount: colsCount); |
| 1586 | return success(); |
| 1587 | } |
| 1588 | |
| 1589 | LogicalResult |
| 1590 | spirv::Deserializer::processTensorARMType(ArrayRef<uint32_t> operands) { |
| 1591 | unsigned size = operands.size(); |
| 1592 | if (size < 2 || size > 4) |
| 1593 | return emitError(loc: unknownLoc, message: "OpTypeTensorARM must have 2-4 operands " |
| 1594 | "(result_id, element_type, (rank), (shape)) " ) |
| 1595 | << size; |
| 1596 | |
| 1597 | Type elementTy = getType(id: operands[1]); |
| 1598 | if (!elementTy) |
| 1599 | return emitError(loc: unknownLoc, |
| 1600 | message: "OpTypeTensorARM references undefined element type " ) |
| 1601 | << operands[1]; |
| 1602 | |
| 1603 | if (size == 2) { |
| 1604 | typeMap[operands[0]] = TensorArmType::get(shape: {}, elementType: elementTy); |
| 1605 | return success(); |
| 1606 | } |
| 1607 | |
| 1608 | IntegerAttr rankAttr = getConstantInt(id: operands[2]); |
| 1609 | if (!rankAttr) |
| 1610 | return emitError(loc: unknownLoc, message: "OpTypeTensorARM rank must come from a " |
| 1611 | "scalar integer constant instruction" ); |
| 1612 | unsigned rank = rankAttr.getValue().getZExtValue(); |
| 1613 | if (size == 3) { |
| 1614 | SmallVector<int64_t, 4> shape(rank, ShapedType::kDynamic); |
| 1615 | typeMap[operands[0]] = TensorArmType::get(shape, elementType: elementTy); |
| 1616 | return success(); |
| 1617 | } |
| 1618 | |
| 1619 | std::optional<std::pair<Attribute, Type>> shapeInfo = |
| 1620 | getConstant(id: operands[3]); |
| 1621 | if (!shapeInfo) |
| 1622 | return emitError(loc: unknownLoc, message: "OpTypeTensorARM shape must come from a " |
| 1623 | "constant instruction of type OpTypeArray" ); |
| 1624 | |
| 1625 | ArrayAttr shapeArrayAttr = dyn_cast<ArrayAttr>(Val&: shapeInfo->first); |
| 1626 | SmallVector<int64_t, 1> shape; |
| 1627 | for (auto dimAttr : shapeArrayAttr.getValue()) { |
| 1628 | auto dimIntAttr = dyn_cast<IntegerAttr>(Val&: dimAttr); |
| 1629 | if (!dimIntAttr) |
| 1630 | return emitError(loc: unknownLoc, message: "OpTypeTensorARM shape has an invalid " |
| 1631 | "dimension size" ); |
| 1632 | shape.push_back(Elt: dimIntAttr.getValue().getSExtValue()); |
| 1633 | } |
| 1634 | typeMap[operands[0]] = TensorArmType::get(shape, elementType: elementTy); |
| 1635 | return success(); |
| 1636 | } |
| 1637 | |
| 1638 | LogicalResult |
| 1639 | spirv::Deserializer::processGraphTypeARM(ArrayRef<uint32_t> operands) { |
| 1640 | unsigned size = operands.size(); |
| 1641 | if (size < 2) { |
| 1642 | return emitError(loc: unknownLoc, message: "OpTypeGraphARM must have at least 2 operands " |
| 1643 | "(result_id, num_inputs, (inout0_type, " |
| 1644 | "inout1_type, ...))" ) |
| 1645 | << size; |
| 1646 | } |
| 1647 | uint32_t numInputs = operands[1]; |
| 1648 | SmallVector<Type, 1> argTypes; |
| 1649 | SmallVector<Type, 1> returnTypes; |
| 1650 | for (unsigned i = 2; i < size; ++i) { |
| 1651 | Type inOutTy = getType(id: operands[i]); |
| 1652 | if (!inOutTy) { |
| 1653 | return emitError(loc: unknownLoc, |
| 1654 | message: "OpTypeGraphARM references undefined element type." ) |
| 1655 | << operands[i]; |
| 1656 | } |
| 1657 | if (i - 2 >= numInputs) { |
| 1658 | returnTypes.push_back(Elt: inOutTy); |
| 1659 | } else { |
| 1660 | argTypes.push_back(Elt: inOutTy); |
| 1661 | } |
| 1662 | } |
| 1663 | typeMap[operands[0]] = GraphType::get(context, inputs: argTypes, results: returnTypes); |
| 1664 | return success(); |
| 1665 | } |
| 1666 | |
| 1667 | LogicalResult |
| 1668 | spirv::Deserializer::processTypeForwardPointer(ArrayRef<uint32_t> operands) { |
| 1669 | if (operands.size() != 2) |
| 1670 | return emitError(loc: unknownLoc, |
| 1671 | message: "OpTypeForwardPointer instruction must have two operands" ); |
| 1672 | |
| 1673 | typeForwardPointerIDs.insert(X: operands[0]); |
| 1674 | // TODO: Use the 2nd operand (Storage Class) to validate the OpTypePointer |
| 1675 | // instruction that defines the actual type. |
| 1676 | |
| 1677 | return success(); |
| 1678 | } |
| 1679 | |
| 1680 | LogicalResult |
| 1681 | spirv::Deserializer::processImageType(ArrayRef<uint32_t> operands) { |
| 1682 | // TODO: Add support for Access Qualifier. |
| 1683 | if (operands.size() != 8) |
| 1684 | return emitError( |
| 1685 | loc: unknownLoc, |
| 1686 | message: "OpTypeImage with non-eight operands are not supported yet" ); |
| 1687 | |
| 1688 | Type elementTy = getType(id: operands[1]); |
| 1689 | if (!elementTy) |
| 1690 | return emitError(loc: unknownLoc, message: "OpTypeImage references undefined <id>: " ) |
| 1691 | << operands[1]; |
| 1692 | |
| 1693 | auto dim = spirv::symbolizeDim(operands[2]); |
| 1694 | if (!dim) |
| 1695 | return emitError(loc: unknownLoc, message: "unknown Dim for OpTypeImage: " ) |
| 1696 | << operands[2]; |
| 1697 | |
| 1698 | auto depthInfo = spirv::symbolizeImageDepthInfo(operands[3]); |
| 1699 | if (!depthInfo) |
| 1700 | return emitError(loc: unknownLoc, message: "unknown Depth for OpTypeImage: " ) |
| 1701 | << operands[3]; |
| 1702 | |
| 1703 | auto arrayedInfo = spirv::symbolizeImageArrayedInfo(operands[4]); |
| 1704 | if (!arrayedInfo) |
| 1705 | return emitError(loc: unknownLoc, message: "unknown Arrayed for OpTypeImage: " ) |
| 1706 | << operands[4]; |
| 1707 | |
| 1708 | auto samplingInfo = spirv::symbolizeImageSamplingInfo(operands[5]); |
| 1709 | if (!samplingInfo) |
| 1710 | return emitError(loc: unknownLoc, message: "unknown MS for OpTypeImage: " ) << operands[5]; |
| 1711 | |
| 1712 | auto samplerUseInfo = spirv::symbolizeImageSamplerUseInfo(operands[6]); |
| 1713 | if (!samplerUseInfo) |
| 1714 | return emitError(loc: unknownLoc, message: "unknown Sampled for OpTypeImage: " ) |
| 1715 | << operands[6]; |
| 1716 | |
| 1717 | auto format = spirv::symbolizeImageFormat(operands[7]); |
| 1718 | if (!format) |
| 1719 | return emitError(loc: unknownLoc, message: "unknown Format for OpTypeImage: " ) |
| 1720 | << operands[7]; |
| 1721 | |
| 1722 | typeMap[operands[0]] = spirv::ImageType::get( |
| 1723 | elementType: elementTy, dim: dim.value(), depth: depthInfo.value(), arrayed: arrayedInfo.value(), |
| 1724 | samplingInfo: samplingInfo.value(), samplerUse: samplerUseInfo.value(), format: format.value()); |
| 1725 | return success(); |
| 1726 | } |
| 1727 | |
| 1728 | LogicalResult |
| 1729 | spirv::Deserializer::processSampledImageType(ArrayRef<uint32_t> operands) { |
| 1730 | if (operands.size() != 2) |
| 1731 | return emitError(loc: unknownLoc, message: "OpTypeSampledImage must have two operands" ); |
| 1732 | |
| 1733 | Type elementTy = getType(id: operands[1]); |
| 1734 | if (!elementTy) |
| 1735 | return emitError(loc: unknownLoc, |
| 1736 | message: "OpTypeSampledImage references undefined <id>: " ) |
| 1737 | << operands[1]; |
| 1738 | |
| 1739 | typeMap[operands[0]] = spirv::SampledImageType::get(imageType: elementTy); |
| 1740 | return success(); |
| 1741 | } |
| 1742 | |
| 1743 | LogicalResult |
| 1744 | spirv::Deserializer::processSamplerType(ArrayRef<uint32_t> operands) { |
| 1745 | if (operands.size() != 1) |
| 1746 | return emitError(loc: unknownLoc, message: "OpTypeSampler must have no parameters" ); |
| 1747 | |
| 1748 | typeMap[operands[0]] = spirv::SamplerType::get(context); |
| 1749 | return success(); |
| 1750 | } |
| 1751 | |
| 1752 | LogicalResult |
| 1753 | spirv::Deserializer::processNamedBarrierType(ArrayRef<uint32_t> operands) { |
| 1754 | if (operands.size() != 1) |
| 1755 | return emitError(loc: unknownLoc, message: "OpTypeNamedBarrier must have no parameters" ); |
| 1756 | |
| 1757 | typeMap[operands[0]] = spirv::NamedBarrierType::get(context); |
| 1758 | return success(); |
| 1759 | } |
| 1760 | |
| 1761 | //===----------------------------------------------------------------------===// |
| 1762 | // Constant |
| 1763 | //===----------------------------------------------------------------------===// |
| 1764 | |
| 1765 | LogicalResult spirv::Deserializer::processConstant(ArrayRef<uint32_t> operands, |
| 1766 | bool isSpec) { |
| 1767 | StringRef opname = isSpec ? "OpSpecConstant" : "OpConstant" ; |
| 1768 | |
| 1769 | if (operands.size() < 2) { |
| 1770 | return emitError(loc: unknownLoc) |
| 1771 | << opname << " must have type <id> and result <id>" ; |
| 1772 | } |
| 1773 | if (operands.size() < 3) { |
| 1774 | return emitError(loc: unknownLoc) |
| 1775 | << opname << " must have at least 1 more parameter" ; |
| 1776 | } |
| 1777 | |
| 1778 | Type resultType = getType(id: operands[0]); |
| 1779 | if (!resultType) { |
| 1780 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 1781 | << operands[0]; |
| 1782 | } |
| 1783 | |
| 1784 | auto checkOperandSizeForBitwidth = [&](unsigned bitwidth) -> LogicalResult { |
| 1785 | if (bitwidth == 64) { |
| 1786 | if (operands.size() == 4) { |
| 1787 | return success(); |
| 1788 | } |
| 1789 | return emitError(loc: unknownLoc) |
| 1790 | << opname << " should have 2 parameters for 64-bit values" ; |
| 1791 | } |
| 1792 | if (bitwidth <= 32) { |
| 1793 | if (operands.size() == 3) { |
| 1794 | return success(); |
| 1795 | } |
| 1796 | |
| 1797 | return emitError(loc: unknownLoc) |
| 1798 | << opname |
| 1799 | << " should have 1 parameter for values with no more than 32 bits" ; |
| 1800 | } |
| 1801 | return emitError(loc: unknownLoc, message: "unsupported OpConstant bitwidth: " ) |
| 1802 | << bitwidth; |
| 1803 | }; |
| 1804 | |
| 1805 | auto resultID = operands[1]; |
| 1806 | |
| 1807 | if (auto intType = dyn_cast<IntegerType>(Val&: resultType)) { |
| 1808 | auto bitwidth = intType.getWidth(); |
| 1809 | if (failed(Result: checkOperandSizeForBitwidth(bitwidth))) { |
| 1810 | return failure(); |
| 1811 | } |
| 1812 | |
| 1813 | APInt value; |
| 1814 | if (bitwidth == 64) { |
| 1815 | // 64-bit integers are represented with two SPIR-V words. According to |
| 1816 | // SPIR-V spec: "When the type’s bit width is larger than one word, the |
| 1817 | // literal’s low-order words appear first." |
| 1818 | struct DoubleWord { |
| 1819 | uint32_t word1; |
| 1820 | uint32_t word2; |
| 1821 | } words = {.word1: operands[2], .word2: operands[3]}; |
| 1822 | value = APInt(64, llvm::bit_cast<uint64_t>(from: words), /*isSigned=*/true); |
| 1823 | } else if (bitwidth <= 32) { |
| 1824 | value = APInt(bitwidth, operands[2], /*isSigned=*/true, |
| 1825 | /*implicitTrunc=*/true); |
| 1826 | } |
| 1827 | |
| 1828 | auto attr = opBuilder.getIntegerAttr(type: intType, value); |
| 1829 | |
| 1830 | if (isSpec) { |
| 1831 | createSpecConstant(loc: unknownLoc, resultID, defaultValue: attr); |
| 1832 | } else { |
| 1833 | // For normal constants, we just record the attribute (and its type) for |
| 1834 | // later materialization at use sites. |
| 1835 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: intType); |
| 1836 | } |
| 1837 | |
| 1838 | return success(); |
| 1839 | } |
| 1840 | |
| 1841 | if (auto floatType = dyn_cast<FloatType>(Val&: resultType)) { |
| 1842 | auto bitwidth = floatType.getWidth(); |
| 1843 | if (failed(Result: checkOperandSizeForBitwidth(bitwidth))) { |
| 1844 | return failure(); |
| 1845 | } |
| 1846 | |
| 1847 | APFloat value(0.f); |
| 1848 | if (floatType.isF64()) { |
| 1849 | // Double values are represented with two SPIR-V words. According to |
| 1850 | // SPIR-V spec: "When the type’s bit width is larger than one word, the |
| 1851 | // literal’s low-order words appear first." |
| 1852 | struct DoubleWord { |
| 1853 | uint32_t word1; |
| 1854 | uint32_t word2; |
| 1855 | } words = {.word1: operands[2], .word2: operands[3]}; |
| 1856 | value = APFloat(llvm::bit_cast<double>(from: words)); |
| 1857 | } else if (floatType.isF32()) { |
| 1858 | value = APFloat(llvm::bit_cast<float>(from: operands[2])); |
| 1859 | } else if (floatType.isF16()) { |
| 1860 | APInt data(16, operands[2]); |
| 1861 | value = APFloat(APFloat::IEEEhalf(), data); |
| 1862 | } else if (floatType.isBF16()) { |
| 1863 | APInt data(16, operands[2]); |
| 1864 | value = APFloat(APFloat::BFloat(), data); |
| 1865 | } else if (floatType.isF8E4M3FN()) { |
| 1866 | APInt data(8, operands[2]); |
| 1867 | value = APFloat(APFloat::Float8E4M3FN(), data); |
| 1868 | } else if (floatType.isF8E5M2()) { |
| 1869 | APInt data(8, operands[2]); |
| 1870 | value = APFloat(APFloat::Float8E5M2(), data); |
| 1871 | } |
| 1872 | |
| 1873 | auto attr = opBuilder.getFloatAttr(type: floatType, value); |
| 1874 | if (isSpec) { |
| 1875 | createSpecConstant(loc: unknownLoc, resultID, defaultValue: attr); |
| 1876 | } else { |
| 1877 | // For normal constants, we just record the attribute (and its type) for |
| 1878 | // later materialization at use sites. |
| 1879 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: floatType); |
| 1880 | } |
| 1881 | |
| 1882 | return success(); |
| 1883 | } |
| 1884 | |
| 1885 | return emitError(loc: unknownLoc, message: "OpConstant can only generate values of " |
| 1886 | "scalar integer or floating-point type" ); |
| 1887 | } |
| 1888 | |
| 1889 | LogicalResult spirv::Deserializer::processConstantBool( |
| 1890 | bool isTrue, ArrayRef<uint32_t> operands, bool isSpec) { |
| 1891 | if (operands.size() != 2) { |
| 1892 | return emitError(loc: unknownLoc, message: "Op" ) |
| 1893 | << (isSpec ? "Spec" : "" ) << "Constant" |
| 1894 | << (isTrue ? "True" : "False" ) |
| 1895 | << " must have type <id> and result <id>" ; |
| 1896 | } |
| 1897 | |
| 1898 | auto attr = opBuilder.getBoolAttr(value: isTrue); |
| 1899 | auto resultID = operands[1]; |
| 1900 | if (isSpec) { |
| 1901 | createSpecConstant(loc: unknownLoc, resultID, defaultValue: attr); |
| 1902 | } else { |
| 1903 | // For normal constants, we just record the attribute (and its type) for |
| 1904 | // later materialization at use sites. |
| 1905 | constantMap.try_emplace(Key: resultID, Args&: attr, Args: opBuilder.getI1Type()); |
| 1906 | } |
| 1907 | |
| 1908 | return success(); |
| 1909 | } |
| 1910 | |
| 1911 | LogicalResult |
| 1912 | spirv::Deserializer::processConstantComposite(ArrayRef<uint32_t> operands) { |
| 1913 | if (operands.size() < 2) { |
| 1914 | return emitError(loc: unknownLoc, |
| 1915 | message: "OpConstantComposite must have type <id> and result <id>" ); |
| 1916 | } |
| 1917 | Type resultType = getType(id: operands[0]); |
| 1918 | if (!resultType) { |
| 1919 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 1920 | << operands[0]; |
| 1921 | } |
| 1922 | |
| 1923 | // Unused constants are not materialized as ops, so their verifier cannot |
| 1924 | // diagnose missing constituents. Only empty structs may have none. |
| 1925 | auto structType = dyn_cast<spirv::StructType>(Val&: resultType); |
| 1926 | if (operands.size() < 3 && |
| 1927 | (!structType || structType.getNumElements() != 0)) { |
| 1928 | return emitError(loc: unknownLoc, |
| 1929 | message: "OpConstantComposite must have at least 1 parameter" ); |
| 1930 | } |
| 1931 | |
| 1932 | SmallVector<Attribute, 4> elements; |
| 1933 | elements.reserve(N: operands.size() - 2); |
| 1934 | for (auto [index, id] : llvm::enumerate(First: operands.drop_front(N: 2))) { |
| 1935 | std::optional<std::pair<Attribute, Type>> elementInfo = getConstant(id); |
| 1936 | if (!elementInfo) { |
| 1937 | // spirv.Constant cannot represent undefined constituents. |
| 1938 | if (getUndefType(id)) { |
| 1939 | return emitError(loc: unknownLoc, message: "OpConstantComposite component #" ) |
| 1940 | << index << " <id> " << id |
| 1941 | << " is an OpUndef, which cannot be represented in " |
| 1942 | "spirv.Constant" ; |
| 1943 | } |
| 1944 | return emitError(loc: unknownLoc, message: "OpConstantComposite component #" ) |
| 1945 | << index << " <id> " << id << " must come from a normal constant" ; |
| 1946 | } |
| 1947 | elements.push_back(Elt: elementInfo->first); |
| 1948 | } |
| 1949 | |
| 1950 | auto resultID = operands[1]; |
| 1951 | if (auto tensorType = dyn_cast<TensorArmType>(Val&: resultType)) { |
| 1952 | SmallVector<Attribute> flattenedElems; |
| 1953 | for (Attribute element : elements) { |
| 1954 | if (auto denseElemAttr = dyn_cast<DenseElementsAttr>(Val&: element)) { |
| 1955 | for (auto value : denseElemAttr.getValues<Attribute>()) |
| 1956 | flattenedElems.push_back(Elt: value); |
| 1957 | } else { |
| 1958 | flattenedElems.push_back(Elt: element); |
| 1959 | } |
| 1960 | } |
| 1961 | auto attr = DenseElementsAttr::get(type: tensorType, values: flattenedElems); |
| 1962 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: tensorType); |
| 1963 | } else if (auto shapedType = dyn_cast<ShapedType>(Val&: resultType)) { |
| 1964 | auto attr = DenseElementsAttr::get(type: shapedType, values: elements); |
| 1965 | // For normal constants, we just record the attribute (and its type) for |
| 1966 | // later materialization at use sites. |
| 1967 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: shapedType); |
| 1968 | } else if (isa<spirv::ArrayType, spirv::StructType>(Val: resultType)) { |
| 1969 | auto attr = opBuilder.getArrayAttr(value: elements); |
| 1970 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: resultType); |
| 1971 | } else { |
| 1972 | return emitError(loc: unknownLoc, message: "unsupported OpConstantComposite type: " ) |
| 1973 | << resultType; |
| 1974 | } |
| 1975 | |
| 1976 | return success(); |
| 1977 | } |
| 1978 | |
| 1979 | LogicalResult spirv::Deserializer::processConstantCompositeReplicateEXT( |
| 1980 | ArrayRef<uint32_t> operands) { |
| 1981 | if (operands.size() != 3) { |
| 1982 | return emitError( |
| 1983 | loc: unknownLoc, |
| 1984 | message: "OpConstantCompositeReplicateEXT expects 3 operands but found " ) |
| 1985 | << operands.size(); |
| 1986 | } |
| 1987 | |
| 1988 | Type resultType = getType(id: operands[0]); |
| 1989 | if (!resultType) { |
| 1990 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 1991 | << operands[0]; |
| 1992 | } |
| 1993 | |
| 1994 | auto compositeType = dyn_cast<CompositeType>(Val&: resultType); |
| 1995 | if (!compositeType) { |
| 1996 | return emitError(loc: unknownLoc, |
| 1997 | message: "result type from <id> is not a composite type" ) |
| 1998 | << operands[0]; |
| 1999 | } |
| 2000 | |
| 2001 | uint32_t resultID = operands[1]; |
| 2002 | uint32_t constantID = operands[2]; |
| 2003 | |
| 2004 | std::optional<std::pair<Attribute, Type>> replicatedConstantCompositeInfo = |
| 2005 | getConstantCompositeReplicate(id: constantID); |
| 2006 | if (replicatedConstantCompositeInfo.has_value()) { |
| 2007 | constantCompositeReplicateMap.try_emplace( |
| 2008 | Key: resultID, Args&: replicatedConstantCompositeInfo.value().first, Args&: resultType); |
| 2009 | return success(); |
| 2010 | } |
| 2011 | |
| 2012 | std::optional<std::pair<Attribute, Type>> constantInfo = |
| 2013 | getConstant(id: constantID); |
| 2014 | if (constantInfo.has_value()) { |
| 2015 | constantCompositeReplicateMap.try_emplace( |
| 2016 | Key: resultID, Args&: constantInfo.value().first, Args&: resultType); |
| 2017 | return success(); |
| 2018 | } |
| 2019 | |
| 2020 | return emitError(loc: unknownLoc, message: "OpConstantCompositeReplicateEXT operand <id> " ) |
| 2021 | << constantID |
| 2022 | << " must come from a normal constant or a " |
| 2023 | "OpConstantCompositeReplicateEXT" ; |
| 2024 | } |
| 2025 | |
| 2026 | LogicalResult |
| 2027 | spirv::Deserializer::processSpecConstantComposite(ArrayRef<uint32_t> operands) { |
| 2028 | if (operands.size() < 2) { |
| 2029 | return emitError( |
| 2030 | loc: unknownLoc, |
| 2031 | message: "OpSpecConstantComposite must have type <id> and result <id>" ); |
| 2032 | } |
| 2033 | if (operands.size() < 3) { |
| 2034 | return emitError(loc: unknownLoc, |
| 2035 | message: "OpSpecConstantComposite must have at least 1 parameter" ); |
| 2036 | } |
| 2037 | |
| 2038 | Type resultType = getType(id: operands[0]); |
| 2039 | if (!resultType) { |
| 2040 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 2041 | << operands[0]; |
| 2042 | } |
| 2043 | |
| 2044 | auto resultID = operands[1]; |
| 2045 | auto symName = opBuilder.getStringAttr(bytes: getSpecConstantSymbol(id: resultID)); |
| 2046 | |
| 2047 | SmallVector<Attribute, 4> elements; |
| 2048 | elements.reserve(N: operands.size() - 2); |
| 2049 | for (unsigned i = 2, e = operands.size(); i < e; ++i) { |
| 2050 | auto elementInfo = getSpecConstant(id: operands[i]); |
| 2051 | elements.push_back(Elt: SymbolRefAttr::get(symbol: elementInfo)); |
| 2052 | } |
| 2053 | |
| 2054 | auto op = spirv::SpecConstantCompositeOp::create( |
| 2055 | builder&: opBuilder, location: unknownLoc, type: TypeAttr::get(type: resultType), sym_name: symName, |
| 2056 | constituents: opBuilder.getArrayAttr(value: elements), /*sym_visibility=*/nullptr); |
| 2057 | specConstCompositeMap[resultID] = op; |
| 2058 | |
| 2059 | return success(); |
| 2060 | } |
| 2061 | |
| 2062 | LogicalResult spirv::Deserializer::processSpecConstantCompositeReplicateEXT( |
| 2063 | ArrayRef<uint32_t> operands) { |
| 2064 | if (operands.size() != 3) { |
| 2065 | return emitError(loc: unknownLoc, message: "OpSpecConstantCompositeReplicateEXT expects " |
| 2066 | "3 operands but found " ) |
| 2067 | << operands.size(); |
| 2068 | } |
| 2069 | |
| 2070 | Type resultType = getType(id: operands[0]); |
| 2071 | if (!resultType) { |
| 2072 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 2073 | << operands[0]; |
| 2074 | } |
| 2075 | |
| 2076 | auto compositeType = dyn_cast<CompositeType>(Val&: resultType); |
| 2077 | if (!compositeType) { |
| 2078 | return emitError(loc: unknownLoc, |
| 2079 | message: "result type from <id> is not a composite type" ) |
| 2080 | << operands[0]; |
| 2081 | } |
| 2082 | |
| 2083 | uint32_t resultID = operands[1]; |
| 2084 | |
| 2085 | auto symName = opBuilder.getStringAttr(bytes: getSpecConstantSymbol(id: resultID)); |
| 2086 | spirv::SpecConstantOp constituentSpecConstantOp = |
| 2087 | getSpecConstant(id: operands[2]); |
| 2088 | auto op = spirv::EXTSpecConstantCompositeReplicateOp::create( |
| 2089 | builder&: opBuilder, location: unknownLoc, type: TypeAttr::get(type: resultType), sym_name: symName, |
| 2090 | constituent: SymbolRefAttr::get(symbol: constituentSpecConstantOp), |
| 2091 | /*sym_visibility=*/nullptr); |
| 2092 | |
| 2093 | specConstCompositeReplicateMap[resultID] = op; |
| 2094 | |
| 2095 | return success(); |
| 2096 | } |
| 2097 | |
| 2098 | LogicalResult |
| 2099 | spirv::Deserializer::processSpecConstantOperation(ArrayRef<uint32_t> operands) { |
| 2100 | if (operands.size() < 3) |
| 2101 | return emitError(loc: unknownLoc, message: "OpConstantOperation must have type <id>, " |
| 2102 | "result <id>, and operand opcode" ); |
| 2103 | |
| 2104 | uint32_t resultTypeID = operands[0]; |
| 2105 | |
| 2106 | if (!getType(id: resultTypeID)) |
| 2107 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 2108 | << resultTypeID; |
| 2109 | |
| 2110 | uint32_t resultID = operands[1]; |
| 2111 | spirv::Opcode enclosedOpcode = static_cast<spirv::Opcode>(operands[2]); |
| 2112 | auto emplaceResult = specConstOperationMap.try_emplace( |
| 2113 | Key: resultID, |
| 2114 | Args: SpecConstOperationMaterializationInfo{ |
| 2115 | .enclodesOpcode: enclosedOpcode, .resultTypeID: resultTypeID, |
| 2116 | .enclosedOpOperands: SmallVector<uint32_t>{operands.begin() + 3, operands.end()}}); |
| 2117 | |
| 2118 | if (!emplaceResult.second) |
| 2119 | return emitError(loc: unknownLoc, message: "value with <id>: " ) |
| 2120 | << resultID << " is probably defined before." ; |
| 2121 | |
| 2122 | return success(); |
| 2123 | } |
| 2124 | |
| 2125 | Value spirv::Deserializer::materializeSpecConstantOperation( |
| 2126 | uint32_t resultID, spirv::Opcode enclosedOpcode, uint32_t resultTypeID, |
| 2127 | ArrayRef<uint32_t> enclosedOpOperands) { |
| 2128 | |
| 2129 | Type resultType = getType(id: resultTypeID); |
| 2130 | |
| 2131 | // Instructions wrapped by OpSpecConstantOp need an ID for their |
| 2132 | // Deserializer::processOp<op_name>(...) to emit the corresponding SPIR-V |
| 2133 | // dialect wrapped op. For that purpose, a new value map is created and "fake" |
| 2134 | // ID in that map is assigned to the result of the enclosed instruction. Note |
| 2135 | // that there is no need to update this fake ID since we only need to |
| 2136 | // reference the created Value for the enclosed op from the spv::YieldOp |
| 2137 | // created later in this method (both of which are the only values in their |
| 2138 | // region: the SpecConstantOperation's region). If we encounter another |
| 2139 | // SpecConstantOperation in the module, we simply re-use the fake ID since the |
| 2140 | // previous Value assigned to it isn't visible in the current scope anyway. |
| 2141 | DenseMap<uint32_t, Value> newValueMap; |
| 2142 | llvm::SaveAndRestore valueMapGuard(valueMap, newValueMap); |
| 2143 | constexpr uint32_t fakeID = static_cast<uint32_t>(-3); |
| 2144 | |
| 2145 | SmallVector<uint32_t, 4> enclosedOpResultTypeAndOperands; |
| 2146 | enclosedOpResultTypeAndOperands.push_back(Elt: resultTypeID); |
| 2147 | enclosedOpResultTypeAndOperands.push_back(Elt: fakeID); |
| 2148 | enclosedOpResultTypeAndOperands.append(in_start: enclosedOpOperands.begin(), |
| 2149 | in_end: enclosedOpOperands.end()); |
| 2150 | |
| 2151 | // Process enclosed instruction before creating the enclosing |
| 2152 | // specConstantOperation (and its region). This way, references to constants, |
| 2153 | // global variables, and spec constants will be materialized outside the new |
| 2154 | // op's region. For more info, see Deserializer::getValue's implementation. |
| 2155 | if (failed( |
| 2156 | Result: processInstruction(opcode: enclosedOpcode, operands: enclosedOpResultTypeAndOperands))) |
| 2157 | return Value(); |
| 2158 | |
| 2159 | // Since the enclosed op is emitted in the current block, split it in a |
| 2160 | // separate new block. |
| 2161 | Block *enclosedBlock = curBlock->splitBlock(splitBeforeOp: &curBlock->back()); |
| 2162 | |
| 2163 | auto loc = createFileLineColLoc(opBuilder); |
| 2164 | auto specConstOperationOp = |
| 2165 | spirv::SpecConstantOperationOp::create(builder&: opBuilder, location: loc, result: resultType); |
| 2166 | |
| 2167 | Region &body = specConstOperationOp.getBody(); |
| 2168 | // Move the new block into SpecConstantOperation's body. |
| 2169 | body.getBlocks().splice(where: body.end(), L2&: curBlock->getParent()->getBlocks(), |
| 2170 | first: Region::iterator(enclosedBlock)); |
| 2171 | Block &block = body.back(); |
| 2172 | |
| 2173 | // RAII guard to reset the insertion point to the module's region after |
| 2174 | // deserializing the body of the specConstantOperation. |
| 2175 | OpBuilder::InsertionGuard moduleInsertionGuard(opBuilder); |
| 2176 | opBuilder.setInsertionPointToEnd(&block); |
| 2177 | |
| 2178 | spirv::YieldOp::create(builder&: opBuilder, location: loc, operand: block.front().getResult(idx: 0)); |
| 2179 | return specConstOperationOp.getResult(); |
| 2180 | } |
| 2181 | |
| 2182 | LogicalResult |
| 2183 | spirv::Deserializer::processConstantNull(ArrayRef<uint32_t> operands) { |
| 2184 | if (operands.size() != 2) { |
| 2185 | return emitError(loc: unknownLoc, |
| 2186 | message: "OpConstantNull must only have type <id> and result <id>" ); |
| 2187 | } |
| 2188 | |
| 2189 | Type resultType = getType(id: operands[0]); |
| 2190 | if (!resultType) { |
| 2191 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 2192 | << operands[0]; |
| 2193 | } |
| 2194 | |
| 2195 | auto resultID = operands[1]; |
| 2196 | Attribute attr; |
| 2197 | if (resultType.isIntOrFloat() || isa<VectorType>(Val: resultType)) { |
| 2198 | attr = opBuilder.getZeroAttr(type: resultType); |
| 2199 | } else if (auto tensorType = dyn_cast<TensorArmType>(Val&: resultType)) { |
| 2200 | if (auto element = opBuilder.getZeroAttr(type: tensorType.getElementType())) |
| 2201 | attr = DenseElementsAttr::get(type: tensorType, values: element); |
| 2202 | } else if (isa<spirv::ArrayType, spirv::PointerType, spirv::StructType>( |
| 2203 | Val: resultType)) { |
| 2204 | attr = spirv::NullAttr::get(type: resultType); |
| 2205 | } |
| 2206 | |
| 2207 | if (attr) { |
| 2208 | // For normal constants, we just record the attribute (and its type) for |
| 2209 | // later materialization at use sites. |
| 2210 | constantMap.try_emplace(Key: resultID, Args&: attr, Args&: resultType); |
| 2211 | return success(); |
| 2212 | } |
| 2213 | |
| 2214 | return emitError(loc: unknownLoc, message: "unsupported OpConstantNull type: " ) |
| 2215 | << resultType; |
| 2216 | } |
| 2217 | |
| 2218 | LogicalResult |
| 2219 | spirv::Deserializer::processGraphConstantARM(ArrayRef<uint32_t> operands) { |
| 2220 | if (operands.size() < 3) { |
| 2221 | return emitError(loc: unknownLoc) |
| 2222 | << "OpGraphConstantARM must have at least 2 operands" ; |
| 2223 | } |
| 2224 | |
| 2225 | Type resultType = getType(id: operands[0]); |
| 2226 | if (!resultType) { |
| 2227 | return emitError(loc: unknownLoc, message: "undefined result type from <id> " ) |
| 2228 | << operands[0]; |
| 2229 | } |
| 2230 | |
| 2231 | uint32_t resultID = operands[1]; |
| 2232 | |
| 2233 | if (!dyn_cast<spirv::TensorArmType>(Val&: resultType)) { |
| 2234 | return emitError(loc: unknownLoc, message: "result must be of type OpTypeTensorARM" ); |
| 2235 | } |
| 2236 | |
| 2237 | APInt graph_constant_id = APInt(32, operands[2], /*isSigned=*/true); |
| 2238 | Type i32Ty = opBuilder.getIntegerType(width: 32); |
| 2239 | IntegerAttr attr = opBuilder.getIntegerAttr(type: i32Ty, value: graph_constant_id); |
| 2240 | graphConstantMap.try_emplace( |
| 2241 | Key: resultID, Args: GraphConstantARMOpMaterializationInfo{.resultType: resultType, .graphConstantID: attr}); |
| 2242 | |
| 2243 | return success(); |
| 2244 | } |
| 2245 | |
| 2246 | //===----------------------------------------------------------------------===// |
| 2247 | // Control flow |
| 2248 | //===----------------------------------------------------------------------===// |
| 2249 | |
| 2250 | Block *spirv::Deserializer::getOrCreateBlock(uint32_t id) { |
| 2251 | if (auto *block = getBlock(id)) { |
| 2252 | LLVM_DEBUG(logger.startLine() << "[block] got exiting block for id = " << id |
| 2253 | << " @ " << block << "\n" ); |
| 2254 | return block; |
| 2255 | } |
| 2256 | |
| 2257 | // We don't know where this block will be placed finally (in a |
| 2258 | // spirv.mlir.selection or spirv.mlir.loop or function). Create it into the |
| 2259 | // function for now and sort out the proper place later. |
| 2260 | auto *block = curFunction->addBlock(); |
| 2261 | LLVM_DEBUG(logger.startLine() << "[block] created block for id = " << id |
| 2262 | << " @ " << block << "\n" ); |
| 2263 | return blockMap[id] = block; |
| 2264 | } |
| 2265 | |
| 2266 | LogicalResult spirv::Deserializer::processBranch(ArrayRef<uint32_t> operands) { |
| 2267 | if (!curBlock) { |
| 2268 | return emitError(loc: unknownLoc, message: "OpBranch must appear inside a block" ); |
| 2269 | } |
| 2270 | |
| 2271 | if (operands.size() != 1) { |
| 2272 | return emitError(loc: unknownLoc, message: "OpBranch must take exactly one target label" ); |
| 2273 | } |
| 2274 | |
| 2275 | auto *target = getOrCreateBlock(id: operands[0]); |
| 2276 | auto loc = createFileLineColLoc(opBuilder); |
| 2277 | // The preceding instruction for the OpBranch instruction could be an |
| 2278 | // OpLoopMerge or an OpSelectionMerge instruction, in this case they will have |
| 2279 | // the same OpLine information. |
| 2280 | spirv::BranchOp::create(builder&: opBuilder, location: loc, successor: target); |
| 2281 | |
| 2282 | clearDebugLine(); |
| 2283 | return success(); |
| 2284 | } |
| 2285 | |
| 2286 | LogicalResult |
| 2287 | spirv::Deserializer::processBranchConditional(ArrayRef<uint32_t> operands) { |
| 2288 | if (!curBlock) { |
| 2289 | return emitError(loc: unknownLoc, |
| 2290 | message: "OpBranchConditional must appear inside a block" ); |
| 2291 | } |
| 2292 | |
| 2293 | if (operands.size() != 3 && operands.size() != 5) { |
| 2294 | return emitError(loc: unknownLoc, |
| 2295 | message: "OpBranchConditional must have condition, true label, " |
| 2296 | "false label, and optionally two branch weights" ); |
| 2297 | } |
| 2298 | |
| 2299 | auto condition = getValue(id: operands[0]); |
| 2300 | auto *trueBlock = getOrCreateBlock(id: operands[1]); |
| 2301 | auto *falseBlock = getOrCreateBlock(id: operands[2]); |
| 2302 | |
| 2303 | std::optional<std::pair<uint32_t, uint32_t>> weights; |
| 2304 | if (operands.size() == 5) { |
| 2305 | weights = std::make_pair(x: operands[3], y: operands[4]); |
| 2306 | } |
| 2307 | // The preceding instruction for the OpBranchConditional instruction could be |
| 2308 | // an OpSelectionMerge instruction, in this case they will have the same |
| 2309 | // OpLine information. |
| 2310 | auto loc = createFileLineColLoc(opBuilder); |
| 2311 | spirv::BranchConditionalOp::create( |
| 2312 | builder&: opBuilder, location: loc, condition, trueBlock, |
| 2313 | /*trueArguments=*/ArrayRef<Value>(), falseBlock, |
| 2314 | /*falseArguments=*/ArrayRef<Value>(), weights); |
| 2315 | |
| 2316 | clearDebugLine(); |
| 2317 | return success(); |
| 2318 | } |
| 2319 | |
| 2320 | LogicalResult spirv::Deserializer::processLabel(ArrayRef<uint32_t> operands) { |
| 2321 | if (!curFunction) { |
| 2322 | return emitError(loc: unknownLoc, message: "OpLabel must appear inside a function" ); |
| 2323 | } |
| 2324 | |
| 2325 | if (operands.size() != 1) { |
| 2326 | return emitError(loc: unknownLoc, message: "OpLabel should only have result <id>" ); |
| 2327 | } |
| 2328 | |
| 2329 | auto labelID = operands[0]; |
| 2330 | // We may have forward declared this block. |
| 2331 | auto *block = getOrCreateBlock(id: labelID); |
| 2332 | LLVM_DEBUG(logger.startLine() |
| 2333 | << "[block] populating block " << block << "\n" ); |
| 2334 | // If we have seen this block, make sure it was just a forward declaration. |
| 2335 | assert(block->empty() && "re-deserialize the same block!" ); |
| 2336 | |
| 2337 | opBuilder.setInsertionPointToStart(block); |
| 2338 | blockMap[labelID] = curBlock = block; |
| 2339 | |
| 2340 | return success(); |
| 2341 | } |
| 2342 | |
| 2343 | LogicalResult spirv::Deserializer::createGraphBlock(uint32_t graphID) { |
| 2344 | if (!curGraph) { |
| 2345 | return emitError(loc: unknownLoc, message: "a graph block must appear inside a graph" ); |
| 2346 | } |
| 2347 | |
| 2348 | // We may have forward declared this block. |
| 2349 | Block *block = getOrCreateBlock(id: graphID); |
| 2350 | LLVM_DEBUG(logger.startLine() |
| 2351 | << "[block] populating block " << block << "\n" ); |
| 2352 | // If we have seen this block, make sure it was just a forward declaration. |
| 2353 | assert(block->empty() && "re-deserialize the same block!" ); |
| 2354 | |
| 2355 | opBuilder.setInsertionPointToStart(block); |
| 2356 | blockMap[graphID] = curBlock = block; |
| 2357 | |
| 2358 | return success(); |
| 2359 | } |
| 2360 | |
| 2361 | LogicalResult |
| 2362 | spirv::Deserializer::processSelectionMerge(ArrayRef<uint32_t> operands) { |
| 2363 | if (!curBlock) { |
| 2364 | return emitError(loc: unknownLoc, message: "OpSelectionMerge must appear in a block" ); |
| 2365 | } |
| 2366 | |
| 2367 | if (operands.size() < 2) { |
| 2368 | return emitError( |
| 2369 | loc: unknownLoc, |
| 2370 | message: "OpSelectionMerge must specify merge target and selection control" ); |
| 2371 | } |
| 2372 | |
| 2373 | auto *mergeBlock = getOrCreateBlock(id: operands[0]); |
| 2374 | auto loc = createFileLineColLoc(opBuilder); |
| 2375 | auto selectionControl = operands[1]; |
| 2376 | |
| 2377 | if (!blockMergeInfo.try_emplace(Key: curBlock, Args&: loc, Args&: selectionControl, Args&: mergeBlock) |
| 2378 | .second) { |
| 2379 | return emitError( |
| 2380 | loc: unknownLoc, |
| 2381 | message: "a block cannot have more than one OpSelectionMerge instruction" ); |
| 2382 | } |
| 2383 | |
| 2384 | return success(); |
| 2385 | } |
| 2386 | |
| 2387 | LogicalResult |
| 2388 | spirv::Deserializer::processLoopMerge(ArrayRef<uint32_t> operands) { |
| 2389 | if (!curBlock) { |
| 2390 | return emitError(loc: unknownLoc, message: "OpLoopMerge must appear in a block" ); |
| 2391 | } |
| 2392 | |
| 2393 | if (operands.size() < 3) { |
| 2394 | return emitError(loc: unknownLoc, message: "OpLoopMerge must specify merge target, " |
| 2395 | "continue target and loop control" ); |
| 2396 | } |
| 2397 | |
| 2398 | auto *mergeBlock = getOrCreateBlock(id: operands[0]); |
| 2399 | auto *continueBlock = getOrCreateBlock(id: operands[1]); |
| 2400 | auto loc = createFileLineColLoc(opBuilder); |
| 2401 | uint32_t loopControl = operands[2]; |
| 2402 | |
| 2403 | if (!blockMergeInfo |
| 2404 | .try_emplace(Key: curBlock, Args&: loc, Args&: loopControl, Args&: mergeBlock, Args&: continueBlock) |
| 2405 | .second) { |
| 2406 | return emitError( |
| 2407 | loc: unknownLoc, |
| 2408 | message: "a block cannot have more than one OpLoopMerge instruction" ); |
| 2409 | } |
| 2410 | |
| 2411 | return success(); |
| 2412 | } |
| 2413 | |
| 2414 | LogicalResult spirv::Deserializer::processPhi(ArrayRef<uint32_t> operands) { |
| 2415 | if (!curBlock) { |
| 2416 | return emitError(loc: unknownLoc, message: "OpPhi must appear in a block" ); |
| 2417 | } |
| 2418 | |
| 2419 | if (operands.size() < 4) { |
| 2420 | return emitError(loc: unknownLoc, message: "OpPhi must specify result type, result <id>, " |
| 2421 | "and variable-parent pairs" ); |
| 2422 | } |
| 2423 | |
| 2424 | // Create a block argument for this OpPhi instruction. |
| 2425 | Type blockArgType = getType(id: operands[0]); |
| 2426 | BlockArgument blockArg = curBlock->addArgument(type: blockArgType, loc: unknownLoc); |
| 2427 | valueMap[operands[1]] = blockArg; |
| 2428 | LLVM_DEBUG(logger.startLine() |
| 2429 | << "[phi] created block argument " << blockArg |
| 2430 | << " id = " << operands[1] << " of type " << blockArgType << "\n" ); |
| 2431 | |
| 2432 | // For each (value, predecessor) pair, insert the value to the predecessor's |
| 2433 | // blockPhiInfo entry so later we can fix the block argument there. |
| 2434 | for (unsigned i = 2, e = operands.size(); i < e; i += 2) { |
| 2435 | uint32_t value = operands[i]; |
| 2436 | Block *predecessor = getOrCreateBlock(id: operands[i + 1]); |
| 2437 | std::pair<Block *, Block *> predecessorTargetPair{predecessor, curBlock}; |
| 2438 | blockPhiInfo[predecessorTargetPair].push_back(Elt: value); |
| 2439 | LLVM_DEBUG(logger.startLine() << "[phi] predecessor @ " << predecessor |
| 2440 | << " with arg id = " << value << "\n" ); |
| 2441 | } |
| 2442 | |
| 2443 | return success(); |
| 2444 | } |
| 2445 | |
| 2446 | LogicalResult spirv::Deserializer::processSwitch(ArrayRef<uint32_t> operands) { |
| 2447 | if (!curBlock) |
| 2448 | return emitError(loc: unknownLoc, message: "OpSwitch must appear in a block" ); |
| 2449 | |
| 2450 | if (operands.size() < 2) |
| 2451 | return emitError(loc: unknownLoc, message: "OpSwitch must at least specify selector and " |
| 2452 | "a default target" ); |
| 2453 | |
| 2454 | Value selector = getValue(id: operands[0]); |
| 2455 | if (!selector) |
| 2456 | return emitError(loc: unknownLoc, message: "unknown OpSwitch selector <id>: " ) |
| 2457 | << operands[0]; |
| 2458 | auto selectorType = dyn_cast<IntegerType>(Val: selector.getType()); |
| 2459 | if (!selectorType) |
| 2460 | return emitError(loc: unknownLoc, message: "OpSwitch selector must be an integer" ); |
| 2461 | |
| 2462 | // Each case literal has the width of the selector type. Literals wider than |
| 2463 | // one word span multiple words with the low-order word first. |
| 2464 | unsigned bitWidth = selectorType.getWidth(); |
| 2465 | unsigned literalWords = llvm::divideCeil(Numerator: bitWidth, Denominator: 32); |
| 2466 | unsigned wordsPerCase = literalWords + 1; |
| 2467 | if ((operands.size() - 2) % wordsPerCase != 0) |
| 2468 | return emitError(loc: unknownLoc, message: "OpSwitch must specify selector, default " |
| 2469 | "target and any number of literal and label " |
| 2470 | "<id> pairs, where each literal of a " ) |
| 2471 | << bitWidth << "-bit selector spans " << literalWords << " word(s)" ; |
| 2472 | |
| 2473 | Block *defaultBlock = getOrCreateBlock(id: operands[1]); |
| 2474 | Location loc = createFileLineColLoc(opBuilder); |
| 2475 | |
| 2476 | SmallVector<APInt> literals; |
| 2477 | SmallVector<Block *> blocks; |
| 2478 | for (unsigned i = 2, e = operands.size(); i < e; i += wordsPerCase) { |
| 2479 | APInt literal; |
| 2480 | if (bitWidth <= 32) { |
| 2481 | // Sub-word literals occupy the low-order bits of the word and are |
| 2482 | // sign-extended for signed selector types, so truncate to the selector |
| 2483 | // width. |
| 2484 | literal = APInt(bitWidth, operands[i], /*isSigned=*/true, |
| 2485 | /*implicitTrunc=*/true); |
| 2486 | } else { |
| 2487 | literal = APInt(bitWidth, 0); |
| 2488 | for (unsigned w = 0; w < literalWords; ++w) |
| 2489 | literal.insertBits(SubBits: APInt(32, operands[i + w]), bitPosition: w * 32); |
| 2490 | } |
| 2491 | literals.push_back(Elt: literal); |
| 2492 | blocks.push_back(Elt: getOrCreateBlock(id: operands[i + literalWords])); |
| 2493 | } |
| 2494 | |
| 2495 | SmallVector<ValueRange> targetOperands(blocks.size(), {}); |
| 2496 | spirv::SwitchOp::create(builder&: opBuilder, location: loc, selector, defaultTarget: defaultBlock, |
| 2497 | defaultOperands: ArrayRef<Value>(), literals: ArrayRef<APInt>(literals), targets: blocks, |
| 2498 | targetOperands); |
| 2499 | |
| 2500 | return success(); |
| 2501 | } |
| 2502 | |
| 2503 | namespace { |
| 2504 | /// A class for putting all blocks in a structured selection/loop in a |
| 2505 | /// spirv.mlir.selection/spirv.mlir.loop op. |
| 2506 | class ControlFlowStructurizer { |
| 2507 | public: |
| 2508 | #ifndef NDEBUG |
| 2509 | ControlFlowStructurizer(Location loc, uint32_t control, |
| 2510 | spirv::BlockMergeInfoMap &mergeInfo, Block *header, |
| 2511 | Block *merge, Block *cont, |
| 2512 | llvm::ScopedPrinter &logger) |
| 2513 | : location(loc), control(control), blockMergeInfo(mergeInfo), |
| 2514 | headerBlock(header), mergeBlock(merge), continueBlock(cont), |
| 2515 | logger(logger) {} |
| 2516 | #else |
| 2517 | ControlFlowStructurizer(Location loc, uint32_t control, |
| 2518 | spirv::BlockMergeInfoMap &mergeInfo, Block *, |
| 2519 | Block *merge, Block *cont) |
| 2520 | : location(loc), control(control), blockMergeInfo(mergeInfo), |
| 2521 | headerBlock(header), mergeBlock(merge), continueBlock(cont) {} |
| 2522 | #endif |
| 2523 | |
| 2524 | /// Structurizes the loop at the given `headerBlock`. |
| 2525 | /// |
| 2526 | /// This method will create an spirv.mlir.loop op in the `mergeBlock` and move |
| 2527 | /// all blocks in the structured loop into the spirv.mlir.loop's region. All |
| 2528 | /// branches to the `headerBlock` will be redirected to the `mergeBlock`. This |
| 2529 | /// method will also update `mergeInfo` by remapping all blocks inside to the |
| 2530 | /// newly cloned ones inside structured control flow op's regions. |
| 2531 | LogicalResult structurize(); |
| 2532 | |
| 2533 | private: |
| 2534 | /// Creates a new spirv.mlir.selection op at the beginning of the |
| 2535 | /// `mergeBlock`. |
| 2536 | spirv::SelectionOp createSelectionOp(uint32_t selectionControl); |
| 2537 | |
| 2538 | /// Creates a new spirv.mlir.loop op at the beginning of the `mergeBlock`. |
| 2539 | spirv::LoopOp createLoopOp(uint32_t loopControl); |
| 2540 | |
| 2541 | /// Collects all blocks reachable from `headerBlock` except `mergeBlock`. |
| 2542 | void collectBlocksInConstruct(); |
| 2543 | |
| 2544 | Location location; |
| 2545 | uint32_t control; |
| 2546 | |
| 2547 | spirv::BlockMergeInfoMap &blockMergeInfo; |
| 2548 | |
| 2549 | Block *; |
| 2550 | Block *mergeBlock; |
| 2551 | Block *continueBlock; // nullptr for spirv.mlir.selection |
| 2552 | |
| 2553 | SetVector<Block *> constructBlocks; |
| 2554 | |
| 2555 | #ifndef NDEBUG |
| 2556 | /// A logger used to emit information during the deserialzation process. |
| 2557 | llvm::ScopedPrinter &logger; |
| 2558 | #endif |
| 2559 | }; |
| 2560 | } // namespace |
| 2561 | |
| 2562 | spirv::SelectionOp |
| 2563 | ControlFlowStructurizer::createSelectionOp(uint32_t selectionControl) { |
| 2564 | // Create a builder and set the insertion point to the beginning of the |
| 2565 | // merge block so that the newly created SelectionOp will be inserted there. |
| 2566 | OpBuilder builder(&mergeBlock->front()); |
| 2567 | |
| 2568 | auto control = static_cast<spirv::SelectionControl>(selectionControl); |
| 2569 | auto selectionOp = spirv::SelectionOp::create(builder, location, selectionControl: control); |
| 2570 | selectionOp.addMergeBlock(builder); |
| 2571 | |
| 2572 | return selectionOp; |
| 2573 | } |
| 2574 | |
| 2575 | spirv::LoopOp ControlFlowStructurizer::createLoopOp(uint32_t loopControl) { |
| 2576 | // Create a builder and set the insertion point to the beginning of the |
| 2577 | // merge block so that the newly created LoopOp will be inserted there. |
| 2578 | OpBuilder builder(&mergeBlock->front()); |
| 2579 | |
| 2580 | auto control = static_cast<spirv::LoopControl>(loopControl); |
| 2581 | auto loopOp = spirv::LoopOp::create(builder, location, loopControl: control); |
| 2582 | loopOp.addEntryAndMergeBlock(builder); |
| 2583 | |
| 2584 | return loopOp; |
| 2585 | } |
| 2586 | |
| 2587 | void ControlFlowStructurizer::collectBlocksInConstruct() { |
| 2588 | assert(constructBlocks.empty() && "expected empty constructBlocks" ); |
| 2589 | |
| 2590 | // Put the header block in the work list first. |
| 2591 | constructBlocks.insert(X: headerBlock); |
| 2592 | |
| 2593 | // For each item in the work list, add its successors excluding the merge |
| 2594 | // block. |
| 2595 | for (unsigned i = 0; i < constructBlocks.size(); ++i) { |
| 2596 | for (auto *successor : constructBlocks[i]->getSuccessors()) |
| 2597 | if (successor != mergeBlock) |
| 2598 | constructBlocks.insert(X: successor); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | LogicalResult ControlFlowStructurizer::structurize() { |
| 2603 | Operation *op = nullptr; |
| 2604 | bool isLoop = continueBlock != nullptr; |
| 2605 | if (isLoop) { |
| 2606 | if (auto loopOp = createLoopOp(loopControl: control)) |
| 2607 | op = loopOp.getOperation(); |
| 2608 | } else { |
| 2609 | if (auto selectionOp = createSelectionOp(selectionControl: control)) |
| 2610 | op = selectionOp.getOperation(); |
| 2611 | } |
| 2612 | if (!op) |
| 2613 | return failure(); |
| 2614 | Region &body = op->getRegion(index: 0); |
| 2615 | |
| 2616 | IRMapping mapper; |
| 2617 | // All references to the old merge block should be directed to the |
| 2618 | // selection/loop merge block in the SelectionOp/LoopOp's region. |
| 2619 | mapper.map(from: mergeBlock, to: &body.back()); |
| 2620 | |
| 2621 | collectBlocksInConstruct(); |
| 2622 | |
| 2623 | // We've identified all blocks belonging to the selection/loop's region. Now |
| 2624 | // need to "move" them into the selection/loop. Instead of really moving the |
| 2625 | // blocks, in the following we copy them and remap all values and branches. |
| 2626 | // This is because: |
| 2627 | // * Inserting a block into a region requires the block not in any region |
| 2628 | // before. But selections/loops can nest so we can create selection/loop ops |
| 2629 | // in a nested manner, which means some blocks may already be in a |
| 2630 | // selection/loop region when to be moved again. |
| 2631 | // * It's much trickier to fix up the branches into and out of the loop's |
| 2632 | // region: we need to treat not-moved blocks and moved blocks differently: |
| 2633 | // Not-moved blocks jumping to the loop header block need to jump to the |
| 2634 | // merge point containing the new loop op but not the loop continue block's |
| 2635 | // back edge. Moved blocks jumping out of the loop need to jump to the |
| 2636 | // merge block inside the loop region but not other not-moved blocks. |
| 2637 | // We cannot use replaceAllUsesWith clearly and it's harder to follow the |
| 2638 | // logic. |
| 2639 | |
| 2640 | // Create a corresponding block in the SelectionOp/LoopOp's region for each |
| 2641 | // block in this loop construct. |
| 2642 | OpBuilder builder(body); |
| 2643 | for (auto *block : constructBlocks) { |
| 2644 | // Create a block and insert it before the selection/loop merge block in the |
| 2645 | // SelectionOp/LoopOp's region. |
| 2646 | auto *newBlock = builder.createBlock(insertBefore: &body.back()); |
| 2647 | mapper.map(from: block, to: newBlock); |
| 2648 | LLVM_DEBUG(logger.startLine() << "[cf] cloned block " << newBlock |
| 2649 | << " from block " << block << "\n" ); |
| 2650 | if (!isFnEntryBlock(block)) { |
| 2651 | for (BlockArgument blockArg : block->getArguments()) { |
| 2652 | auto newArg = |
| 2653 | newBlock->addArgument(type: blockArg.getType(), loc: blockArg.getLoc()); |
| 2654 | mapper.map(from: blockArg, to: newArg); |
| 2655 | LLVM_DEBUG(logger.startLine() << "[cf] remapped block argument " |
| 2656 | << blockArg << " to " << newArg << "\n" ); |
| 2657 | } |
| 2658 | } else { |
| 2659 | LLVM_DEBUG(logger.startLine() |
| 2660 | << "[cf] block " << block << " is a function entry block\n" ); |
| 2661 | } |
| 2662 | |
| 2663 | for (auto &op : *block) |
| 2664 | newBlock->push_back(op: op.clone(mapper)); |
| 2665 | } |
| 2666 | |
| 2667 | // Go through all ops and remap the operands. |
| 2668 | auto remapOperands = [&](Operation *op) { |
| 2669 | for (auto &operand : op->getOpOperands()) |
| 2670 | if (Value mappedOp = mapper.lookupOrNull(from: operand.get())) |
| 2671 | operand.set(mappedOp); |
| 2672 | for (auto &succOp : op->getBlockOperands()) |
| 2673 | if (Block *mappedOp = mapper.lookupOrNull(from: succOp.get())) |
| 2674 | succOp.set(mappedOp); |
| 2675 | }; |
| 2676 | for (auto &block : body) |
| 2677 | block.walk(callback&: remapOperands); |
| 2678 | |
| 2679 | // We have created the SelectionOp/LoopOp and "moved" all blocks belonging to |
| 2680 | // the selection/loop construct into its region. Next we need to fix the |
| 2681 | // connections between this new SelectionOp/LoopOp with existing blocks. |
| 2682 | |
| 2683 | // All existing incoming branches should go to the merge block, where the |
| 2684 | // SelectionOp/LoopOp resides right now. |
| 2685 | headerBlock->replaceAllUsesWith(newValue&: mergeBlock); |
| 2686 | |
| 2687 | LLVM_DEBUG({ |
| 2688 | logger.startLine() << "[cf] after cloning and fixing references:\n" ; |
| 2689 | headerBlock->getParentOp()->print(logger.getOStream()); |
| 2690 | logger.startLine() << "\n" ; |
| 2691 | }); |
| 2692 | |
| 2693 | if (isLoop) { |
| 2694 | if (!mergeBlock->args_empty()) { |
| 2695 | return mergeBlock->getParentOp()->emitError( |
| 2696 | message: "OpPhi in loop merge block unsupported" ); |
| 2697 | } |
| 2698 | |
| 2699 | // The loop header block may have block arguments. Since now we place the |
| 2700 | // loop op inside the old merge block, we need to make sure the old merge |
| 2701 | // block has the same block argument list. |
| 2702 | for (BlockArgument blockArg : headerBlock->getArguments()) |
| 2703 | mergeBlock->addArgument(type: blockArg.getType(), loc: blockArg.getLoc()); |
| 2704 | |
| 2705 | // If the loop header block has block arguments, make sure the spirv.Branch |
| 2706 | // op matches. |
| 2707 | SmallVector<Value, 4> blockArgs; |
| 2708 | if (!headerBlock->args_empty()) |
| 2709 | blockArgs = {mergeBlock->args_begin(), mergeBlock->args_end()}; |
| 2710 | |
| 2711 | // The loop entry block should have a unconditional branch jumping to the |
| 2712 | // loop header block. |
| 2713 | builder.setInsertionPointToEnd(&body.front()); |
| 2714 | spirv::BranchOp::create(builder, location, successor: mapper.lookupOrNull(from: headerBlock), |
| 2715 | arguments: ArrayRef<Value>(blockArgs)); |
| 2716 | } |
| 2717 | |
| 2718 | // Values defined inside the selection region that need to be yielded outside |
| 2719 | // the region. |
| 2720 | SmallVector<Value> valuesToYield; |
| 2721 | // Outside uses of values that were sunk into the selection region. Those uses |
| 2722 | // will be replaced with values returned by the SelectionOp. |
| 2723 | SmallVector<Value> outsideUses; |
| 2724 | |
| 2725 | // Move block arguments of the original block (`mergeBlock`) into the merge |
| 2726 | // block inside the selection (`body.back()`). Values produced by block |
| 2727 | // arguments will be yielded by the selection region. We do not update uses or |
| 2728 | // erase original block arguments yet. It will be done later in the code. |
| 2729 | // |
| 2730 | // Code below is not executed for loops as it would interfere with the logic |
| 2731 | // above. Currently block arguments in the merge block are not supported, but |
| 2732 | // instead, the code above copies those arguments from the header block into |
| 2733 | // the merge block. As such, running the code would yield those copied |
| 2734 | // arguments that is most likely not a desired behaviour. This may need to be |
| 2735 | // revisited in the future. |
| 2736 | if (!isLoop) |
| 2737 | for (BlockArgument blockArg : mergeBlock->getArguments()) { |
| 2738 | // Create new block arguments in the last block ("merge block") of the |
| 2739 | // selection region. We create one argument for each argument in |
| 2740 | // `mergeBlock`. This new value will need to be yielded, and the original |
| 2741 | // value replaced, so add them to appropriate vectors. |
| 2742 | body.back().addArgument(type: blockArg.getType(), loc: blockArg.getLoc()); |
| 2743 | valuesToYield.push_back(Elt: body.back().getArguments().back()); |
| 2744 | outsideUses.push_back(Elt: blockArg); |
| 2745 | } |
| 2746 | |
| 2747 | // All the blocks cloned into the SelectionOp/LoopOp's region can now be |
| 2748 | // cleaned up. |
| 2749 | LLVM_DEBUG(logger.startLine() << "[cf] cleaning up blocks after clone\n" ); |
| 2750 | // First we need to drop all operands' references inside all blocks. This is |
| 2751 | // needed because we can have blocks referencing SSA values from one another. |
| 2752 | for (auto *block : constructBlocks) |
| 2753 | block->dropAllReferences(); |
| 2754 | |
| 2755 | // All internal uses should be removed from original blocks by now, so |
| 2756 | // whatever is left is an outside use and will need to be yielded from |
| 2757 | // the newly created selection / loop region. |
| 2758 | for (Block *block : constructBlocks) { |
| 2759 | for (Operation &op : *block) { |
| 2760 | if (!op.use_empty()) |
| 2761 | for (Value result : op.getResults()) { |
| 2762 | valuesToYield.push_back(Elt: mapper.lookupOrNull(from: result)); |
| 2763 | outsideUses.push_back(Elt: result); |
| 2764 | } |
| 2765 | } |
| 2766 | for (BlockArgument &arg : block->getArguments()) { |
| 2767 | if (!arg.use_empty()) { |
| 2768 | valuesToYield.push_back(Elt: mapper.lookupOrNull(from: arg)); |
| 2769 | outsideUses.push_back(Elt: arg); |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | assert(valuesToYield.size() == outsideUses.size()); |
| 2775 | |
| 2776 | // If we need to yield any values from the selection / loop region we will |
| 2777 | // take care of it here. |
| 2778 | if (!valuesToYield.empty()) { |
| 2779 | LLVM_DEBUG(logger.startLine() |
| 2780 | << "[cf] yielding values from the selection / loop region\n" ); |
| 2781 | |
| 2782 | // Update `mlir.merge` with values to be yield. |
| 2783 | auto mergeOps = body.back().getOps<spirv::MergeOp>(); |
| 2784 | Operation *merge = llvm::getSingleElement(C&: mergeOps); |
| 2785 | assert(merge); |
| 2786 | merge->setOperands(valuesToYield); |
| 2787 | |
| 2788 | // MLIR does not allow changing the number of results of an operation, so |
| 2789 | // we create a new SelectionOp / LoopOp with required list of results and |
| 2790 | // move the region from the initial SelectionOp / LoopOp. The initial |
| 2791 | // operation is then removed. Since we move the region to the new op all |
| 2792 | // links between blocks and remapping we have previously done should be |
| 2793 | // preserved. |
| 2794 | builder.setInsertionPoint(&mergeBlock->front()); |
| 2795 | |
| 2796 | Operation *newOp = nullptr; |
| 2797 | |
| 2798 | if (isLoop) |
| 2799 | newOp = spirv::LoopOp::create(builder, location, |
| 2800 | results: TypeRange(ValueRange(outsideUses)), |
| 2801 | loop_control: static_cast<spirv::LoopControl>(control)); |
| 2802 | else |
| 2803 | newOp = spirv::SelectionOp::create( |
| 2804 | builder, location, results: TypeRange(ValueRange(outsideUses)), |
| 2805 | selection_control: static_cast<spirv::SelectionControl>(control)); |
| 2806 | |
| 2807 | newOp->getRegion(index: 0).takeBody(other&: body); |
| 2808 | |
| 2809 | // Remove initial op and swap the pointer to the newly created one. |
| 2810 | op->erase(); |
| 2811 | op = newOp; |
| 2812 | |
| 2813 | // Update all outside uses to use results of the SelectionOp / LoopOp and |
| 2814 | // remove block arguments from the original merge block. |
| 2815 | for (unsigned i = 0, e = outsideUses.size(); i != e; ++i) |
| 2816 | outsideUses[i].replaceAllUsesWith(newValue: op->getResult(idx: i)); |
| 2817 | |
| 2818 | // We do not support block arguments in loop merge block. Also running this |
| 2819 | // function with loop would break some of the loop specific code above |
| 2820 | // dealing with block arguments. |
| 2821 | if (!isLoop) |
| 2822 | mergeBlock->eraseArguments(start: 0, num: mergeBlock->getNumArguments()); |
| 2823 | } |
| 2824 | |
| 2825 | // Check that whether some op in the to-be-erased blocks still has uses. Those |
| 2826 | // uses come from blocks that won't be sinked into the SelectionOp/LoopOp's |
| 2827 | // region. We cannot handle such cases given that once a value is sinked into |
| 2828 | // the SelectionOp/LoopOp's region, there is no escape for it. |
| 2829 | for (auto *block : constructBlocks) { |
| 2830 | if (!block->use_empty()) |
| 2831 | return emitError(loc: block->getParent()->getLoc(), |
| 2832 | message: "failed control flow structurization: " |
| 2833 | "block has uses outside of the " |
| 2834 | "enclosing selection/loop construct" ); |
| 2835 | for (Operation &op : *block) |
| 2836 | if (!op.use_empty()) |
| 2837 | return op.emitOpError(message: "failed control flow structurization: value has " |
| 2838 | "uses outside of the " |
| 2839 | "enclosing selection/loop construct" ); |
| 2840 | for (BlockArgument &arg : block->getArguments()) |
| 2841 | if (!arg.use_empty()) |
| 2842 | return emitError(loc: arg.getLoc(), message: "failed control flow structurization: " |
| 2843 | "block argument has uses outside of the " |
| 2844 | "enclosing selection/loop construct" ); |
| 2845 | } |
| 2846 | |
| 2847 | // Then erase all old blocks. |
| 2848 | for (auto *block : constructBlocks) { |
| 2849 | // We've cloned all blocks belonging to this construct into the structured |
| 2850 | // control flow op's region. Among these blocks, some may compose another |
| 2851 | // selection/loop. If so, they will be recorded within blockMergeInfo. |
| 2852 | // We need to update the pointers there to the newly remapped ones so we can |
| 2853 | // continue structurizing them later. |
| 2854 | // |
| 2855 | // We need to walk each block as constructBlocks do not include blocks |
| 2856 | // internal to ops already structured within those blocks. It is not |
| 2857 | // fully clear to me why the mergeInfo of blocks (yet to be structured) |
| 2858 | // inside already structured selections/loops get invalidated and needs |
| 2859 | // updating, however the following example code can cause a crash (depending |
| 2860 | // on the structuring order), when the most inner selection is being |
| 2861 | // structured after the outer selection and loop have been already |
| 2862 | // structured: |
| 2863 | // |
| 2864 | // spirv.mlir.for { |
| 2865 | // // ... |
| 2866 | // spirv.mlir.selection { |
| 2867 | // // .. |
| 2868 | // // A selection region that hasn't been yet structured! |
| 2869 | // // .. |
| 2870 | // } |
| 2871 | // // ... |
| 2872 | // } |
| 2873 | // |
| 2874 | // If the loop gets structured after the outer selection, but before the |
| 2875 | // inner selection. Moving the already structured selection inside the loop |
| 2876 | // will invalidate the mergeInfo of the region that is not yet structured. |
| 2877 | // Just going over constructBlocks will not check and updated header blocks |
| 2878 | // inside the already structured selection region. Walking block fixes that. |
| 2879 | // |
| 2880 | // TODO: If structuring was done in a fixed order starting with inner |
| 2881 | // most constructs this most likely not be an issue and the whole code |
| 2882 | // section could be removed. However, with the current non-deterministic |
| 2883 | // order this is not possible. |
| 2884 | // |
| 2885 | // TODO: The asserts in the following assumes input SPIR-V blob forms |
| 2886 | // correctly nested selection/loop constructs. We should relax this and |
| 2887 | // support error cases better. |
| 2888 | auto updateMergeInfo = [&](Block *block) -> WalkResult { |
| 2889 | auto it = blockMergeInfo.find(Key: block); |
| 2890 | if (it != blockMergeInfo.end()) { |
| 2891 | // Use the original location for nested selection/loop ops. |
| 2892 | Location loc = it->second.loc; |
| 2893 | // Copy the control before the erase below invalidates `it`. |
| 2894 | uint32_t nestedControl = it->second.control; |
| 2895 | |
| 2896 | Block * = mapper.lookupOrNull(from: block); |
| 2897 | if (!newHeader) |
| 2898 | return emitError(loc, message: "failed control flow structurization: nested " |
| 2899 | "loop header block should be remapped!" ); |
| 2900 | |
| 2901 | Block *newContinue = it->second.continueBlock; |
| 2902 | if (newContinue) { |
| 2903 | newContinue = mapper.lookupOrNull(from: newContinue); |
| 2904 | if (!newContinue) |
| 2905 | return emitError(loc, message: "failed control flow structurization: nested " |
| 2906 | "loop continue block should be remapped!" ); |
| 2907 | } |
| 2908 | |
| 2909 | Block *newMerge = it->second.mergeBlock; |
| 2910 | if (Block *mappedTo = mapper.lookupOrNull(from: newMerge)) |
| 2911 | newMerge = mappedTo; |
| 2912 | |
| 2913 | // Erase the stale entry before inserting the remapped one. |
| 2914 | blockMergeInfo.erase(Iterator: it); |
| 2915 | blockMergeInfo.try_emplace(Key: newHeader, Args&: loc, Args&: nestedControl, Args&: newMerge, |
| 2916 | Args&: newContinue); |
| 2917 | } |
| 2918 | |
| 2919 | return WalkResult::advance(); |
| 2920 | }; |
| 2921 | |
| 2922 | if (block->walk(callback&: updateMergeInfo).wasInterrupted()) |
| 2923 | return failure(); |
| 2924 | |
| 2925 | // The structured selection/loop's entry block does not have arguments. |
| 2926 | // If the function's header block is also part of the structured control |
| 2927 | // flow, we cannot just simply erase it because it may contain arguments |
| 2928 | // matching the function signature and used by the cloned blocks. |
| 2929 | if (isFnEntryBlock(block)) { |
| 2930 | LLVM_DEBUG(logger.startLine() << "[cf] changing entry block " << block |
| 2931 | << " to only contain a spirv.Branch op\n" ); |
| 2932 | // Still keep the function entry block for the potential block arguments, |
| 2933 | // but replace all ops inside with a branch to the merge block. |
| 2934 | block->clear(); |
| 2935 | builder.setInsertionPointToEnd(block); |
| 2936 | spirv::BranchOp::create(builder, location, successor: mergeBlock); |
| 2937 | } else { |
| 2938 | LLVM_DEBUG(logger.startLine() << "[cf] erasing block " << block << "\n" ); |
| 2939 | block->erase(); |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | LLVM_DEBUG(logger.startLine() |
| 2944 | << "[cf] after structurizing construct with header block " |
| 2945 | << headerBlock << ":\n" |
| 2946 | << *op << "\n" ); |
| 2947 | |
| 2948 | return success(); |
| 2949 | } |
| 2950 | |
| 2951 | LogicalResult spirv::Deserializer::wireUpBlockArgument() { |
| 2952 | LLVM_DEBUG({ |
| 2953 | logger.startLine() |
| 2954 | << "//----- [phi] start wiring up block arguments -----//\n" ; |
| 2955 | logger.indent(); |
| 2956 | }); |
| 2957 | |
| 2958 | OpBuilder::InsertionGuard guard(opBuilder); |
| 2959 | |
| 2960 | for (const auto &info : blockPhiInfo) { |
| 2961 | Block *block = info.first.first; |
| 2962 | Block *target = info.first.second; |
| 2963 | const BlockPhiInfo &phiInfo = info.second; |
| 2964 | LLVM_DEBUG({ |
| 2965 | logger.startLine() << "[phi] block " << block << "\n" ; |
| 2966 | logger.startLine() << "[phi] before creating block argument:\n" ; |
| 2967 | block->getParentOp()->print(logger.getOStream()); |
| 2968 | logger.startLine() << "\n" ; |
| 2969 | }); |
| 2970 | |
| 2971 | // Set insertion point to before this block's terminator early because we |
| 2972 | // may materialize ops via getValue() call. |
| 2973 | auto *op = block->getTerminator(); |
| 2974 | opBuilder.setInsertionPoint(op); |
| 2975 | |
| 2976 | SmallVector<Value, 4> blockArgs; |
| 2977 | blockArgs.reserve(N: phiInfo.size()); |
| 2978 | for (uint32_t valueId : phiInfo) { |
| 2979 | if (Value value = getValue(id: valueId)) { |
| 2980 | blockArgs.push_back(Elt: value); |
| 2981 | LLVM_DEBUG(logger.startLine() << "[phi] block argument " << value |
| 2982 | << " id = " << valueId << "\n" ); |
| 2983 | } else { |
| 2984 | return emitError(loc: unknownLoc, message: "OpPhi references undefined value!" ); |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | if (auto branchOp = dyn_cast<spirv::BranchOp>(Val: op)) { |
| 2989 | // Replace the previous branch op with a new one with block arguments. |
| 2990 | spirv::BranchOp::create(builder&: opBuilder, location: branchOp.getLoc(), |
| 2991 | successor: branchOp.getTarget(), arguments: blockArgs); |
| 2992 | branchOp.erase(); |
| 2993 | } else if (auto branchCondOp = dyn_cast<spirv::BranchConditionalOp>(Val: op)) { |
| 2994 | assert((branchCondOp.getTrueBlock() == target || |
| 2995 | branchCondOp.getFalseBlock() == target) && |
| 2996 | "expected target to be either the true or false target" ); |
| 2997 | if (target == branchCondOp.getTrueTarget()) |
| 2998 | spirv::BranchConditionalOp::create( |
| 2999 | builder&: opBuilder, location: branchCondOp.getLoc(), condition: branchCondOp.getCondition(), |
| 3000 | trueTargetOperands: blockArgs, falseTargetOperands: branchCondOp.getFalseBlockArguments(), |
| 3001 | branch_weights: branchCondOp.getBranchWeightsAttr(), trueTarget: branchCondOp.getTrueTarget(), |
| 3002 | falseTarget: branchCondOp.getFalseTarget()); |
| 3003 | else |
| 3004 | spirv::BranchConditionalOp::create( |
| 3005 | builder&: opBuilder, location: branchCondOp.getLoc(), condition: branchCondOp.getCondition(), |
| 3006 | trueTargetOperands: branchCondOp.getTrueBlockArguments(), falseTargetOperands: blockArgs, |
| 3007 | branch_weights: branchCondOp.getBranchWeightsAttr(), trueTarget: branchCondOp.getTrueBlock(), |
| 3008 | falseTarget: branchCondOp.getFalseBlock()); |
| 3009 | |
| 3010 | branchCondOp.erase(); |
| 3011 | } else if (auto switchOp = dyn_cast<spirv::SwitchOp>(Val: op)) { |
| 3012 | if (target == switchOp.getDefaultTarget()) { |
| 3013 | SmallVector<ValueRange> targetOperands(switchOp.getTargetOperands()); |
| 3014 | DenseIntElementsAttr literals = |
| 3015 | switchOp.getLiterals().value_or(u: DenseIntElementsAttr()); |
| 3016 | spirv::SwitchOp::create( |
| 3017 | builder&: opBuilder, location: switchOp.getLoc(), selector: switchOp.getSelector(), |
| 3018 | defaultTarget: switchOp.getDefaultTarget(), defaultOperands: blockArgs, literals, |
| 3019 | targets: switchOp.getTargets(), targetOperands); |
| 3020 | switchOp.erase(); |
| 3021 | } else { |
| 3022 | SuccessorRange targets = switchOp.getTargets(); |
| 3023 | auto it = llvm::find(Range&: targets, Val: target); |
| 3024 | assert(it != targets.end()); |
| 3025 | size_t index = std::distance(first: targets.begin(), last: it); |
| 3026 | switchOp.getTargetOperandsMutable(index).assign(values: blockArgs); |
| 3027 | } |
| 3028 | } else { |
| 3029 | return emitError(loc: unknownLoc, message: "unimplemented terminator for Phi creation" ); |
| 3030 | } |
| 3031 | |
| 3032 | LLVM_DEBUG({ |
| 3033 | logger.startLine() << "[phi] after creating block argument:\n" ; |
| 3034 | block->getParentOp()->print(logger.getOStream()); |
| 3035 | logger.startLine() << "\n" ; |
| 3036 | }); |
| 3037 | } |
| 3038 | blockPhiInfo.clear(); |
| 3039 | |
| 3040 | LLVM_DEBUG({ |
| 3041 | logger.unindent(); |
| 3042 | logger.startLine() |
| 3043 | << "//--- [phi] completed wiring up block arguments ---//\n" ; |
| 3044 | }); |
| 3045 | return success(); |
| 3046 | } |
| 3047 | |
| 3048 | LogicalResult spirv::Deserializer::() { |
| 3049 | // Create a copy, so we can modify keys in the original. |
| 3050 | BlockMergeInfoMap blockMergeInfoCopy = blockMergeInfo; |
| 3051 | for (auto [block, mergeInfo] : blockMergeInfoCopy) { |
| 3052 | // Skip processing loop regions. For loop regions continueBlock is non-null. |
| 3053 | if (mergeInfo.continueBlock) |
| 3054 | continue; |
| 3055 | |
| 3056 | if (!block->mightHaveTerminator()) |
| 3057 | continue; |
| 3058 | |
| 3059 | Operation *terminator = block->getTerminator(); |
| 3060 | assert(terminator); |
| 3061 | |
| 3062 | if (!isa<spirv::BranchConditionalOp, spirv::SwitchOp>(Val: terminator)) |
| 3063 | continue; |
| 3064 | |
| 3065 | // Check if the current header block is a merge block of another construct. |
| 3066 | bool = false; |
| 3067 | for (const auto &[_, mergeInfo] : blockMergeInfo) { |
| 3068 | if (mergeInfo.mergeBlock == block) |
| 3069 | splitHeaderMergeBlock = true; |
| 3070 | } |
| 3071 | |
| 3072 | // Do not split a block that only contains a conditional branch / switch, |
| 3073 | // unless it is also a merge block of another construct - in that case we |
| 3074 | // want to split the block. We do not want two constructs to share header / |
| 3075 | // merge block. The entry block cannot join a region, so split it too. |
| 3076 | if (!llvm::hasSingleElement(C&: *block) || splitHeaderMergeBlock || |
| 3077 | isFnEntryBlock(block)) { |
| 3078 | Block *newBlock = block->splitBlock(splitBeforeOp: terminator); |
| 3079 | OpBuilder builder(block, block->end()); |
| 3080 | spirv::BranchOp::create(builder, location: block->getParent()->getLoc(), successor: newBlock); |
| 3081 | |
| 3082 | // After splitting we need to update the map to use the new block as a |
| 3083 | // header. |
| 3084 | blockMergeInfo.erase(Key: block); |
| 3085 | blockMergeInfo.try_emplace(Key: newBlock, Args&: mergeInfo); |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | return success(); |
| 3090 | } |
| 3091 | |
| 3092 | LogicalResult spirv::Deserializer::structurizeControlFlow() { |
| 3093 | if (!options.enableControlFlowStructurization) { |
| 3094 | LLVM_DEBUG( |
| 3095 | { |
| 3096 | logger.startLine() |
| 3097 | << "//----- [cf] skip structurizing control flow -----//\n" ; |
| 3098 | logger.indent(); |
| 3099 | }); |
| 3100 | return success(); |
| 3101 | } |
| 3102 | |
| 3103 | LLVM_DEBUG({ |
| 3104 | logger.startLine() |
| 3105 | << "//----- [cf] start structurizing control flow -----//\n" ; |
| 3106 | logger.indent(); |
| 3107 | }); |
| 3108 | |
| 3109 | LLVM_DEBUG({ |
| 3110 | logger.startLine() << "[cf] split conditional blocks\n" ; |
| 3111 | logger.startLine() << "\n" ; |
| 3112 | }); |
| 3113 | |
| 3114 | if (failed(Result: splitSelectionHeader())) { |
| 3115 | return failure(); |
| 3116 | } |
| 3117 | |
| 3118 | while (!blockMergeInfo.empty()) { |
| 3119 | Block * = blockMergeInfo.begin()->first; |
| 3120 | BlockMergeInfo mergeInfo = blockMergeInfo.begin()->second; |
| 3121 | |
| 3122 | LLVM_DEBUG({ |
| 3123 | logger.startLine() << "[cf] header block " << headerBlock << ":\n" ; |
| 3124 | headerBlock->print(logger.getOStream()); |
| 3125 | logger.startLine() << "\n" ; |
| 3126 | }); |
| 3127 | |
| 3128 | auto *mergeBlock = mergeInfo.mergeBlock; |
| 3129 | assert(mergeBlock && "merge block cannot be nullptr" ); |
| 3130 | if (mergeInfo.continueBlock && !mergeBlock->args_empty()) |
| 3131 | return emitError(loc: unknownLoc, message: "OpPhi in loop merge block unimplemented" ); |
| 3132 | LLVM_DEBUG({ |
| 3133 | logger.startLine() << "[cf] merge block " << mergeBlock << ":\n" ; |
| 3134 | mergeBlock->print(logger.getOStream()); |
| 3135 | logger.startLine() << "\n" ; |
| 3136 | }); |
| 3137 | |
| 3138 | auto *continueBlock = mergeInfo.continueBlock; |
| 3139 | LLVM_DEBUG(if (continueBlock) { |
| 3140 | logger.startLine() << "[cf] continue block " << continueBlock << ":\n" ; |
| 3141 | continueBlock->print(logger.getOStream()); |
| 3142 | logger.startLine() << "\n" ; |
| 3143 | }); |
| 3144 | // Erase this case before calling into structurizer, who will update |
| 3145 | // blockMergeInfo. |
| 3146 | blockMergeInfo.erase(Iterator: blockMergeInfo.begin()); |
| 3147 | ControlFlowStructurizer structurizer(mergeInfo.loc, mergeInfo.control, |
| 3148 | blockMergeInfo, headerBlock, |
| 3149 | mergeBlock, continueBlock |
| 3150 | #ifndef NDEBUG |
| 3151 | , |
| 3152 | logger |
| 3153 | #endif |
| 3154 | ); |
| 3155 | if (failed(Result: structurizer.structurize())) |
| 3156 | return failure(); |
| 3157 | } |
| 3158 | |
| 3159 | LLVM_DEBUG({ |
| 3160 | logger.unindent(); |
| 3161 | logger.startLine() |
| 3162 | << "//--- [cf] completed structurizing control flow ---//\n" ; |
| 3163 | }); |
| 3164 | return success(); |
| 3165 | } |
| 3166 | |
| 3167 | //===----------------------------------------------------------------------===// |
| 3168 | // Debug |
| 3169 | //===----------------------------------------------------------------------===// |
| 3170 | |
| 3171 | Location spirv::Deserializer::createFileLineColLoc(OpBuilder opBuilder) { |
| 3172 | if (!debugLine) |
| 3173 | return unknownLoc; |
| 3174 | |
| 3175 | auto fileName = debugInfoMap.lookup(Val: debugLine->fileID).str(); |
| 3176 | if (fileName.empty()) |
| 3177 | fileName = "<unknown>" ; |
| 3178 | return FileLineColLoc::get(filename: opBuilder.getStringAttr(bytes: fileName), line: debugLine->line, |
| 3179 | column: debugLine->column); |
| 3180 | } |
| 3181 | |
| 3182 | LogicalResult |
| 3183 | spirv::Deserializer::processDebugLine(ArrayRef<uint32_t> operands) { |
| 3184 | // According to SPIR-V spec: |
| 3185 | // "This location information applies to the instructions physically |
| 3186 | // following this instruction, up to the first occurrence of any of the |
| 3187 | // following: the next end of block, the next OpLine instruction, or the next |
| 3188 | // OpNoLine instruction." |
| 3189 | if (operands.size() != 3) |
| 3190 | return emitError(loc: unknownLoc, message: "OpLine must have 3 operands" ); |
| 3191 | debugLine = DebugLine{.fileID: operands[0], .line: operands[1], .column: operands[2]}; |
| 3192 | return success(); |
| 3193 | } |
| 3194 | |
| 3195 | void spirv::Deserializer::clearDebugLine() { debugLine = std::nullopt; } |
| 3196 | |
| 3197 | LogicalResult |
| 3198 | spirv::Deserializer::processDebugString(ArrayRef<uint32_t> operands) { |
| 3199 | if (operands.size() < 2) |
| 3200 | return emitError(loc: unknownLoc, message: "OpString needs at least 2 operands" ); |
| 3201 | |
| 3202 | if (!debugInfoMap.lookup(Val: operands[0]).empty()) |
| 3203 | return emitError(loc: unknownLoc, |
| 3204 | message: "duplicate debug string found for result <id> " ) |
| 3205 | << operands[0]; |
| 3206 | |
| 3207 | unsigned wordIndex = 1; |
| 3208 | StringRef debugString = decodeStringLiteral(words: operands, wordIndex); |
| 3209 | if (wordIndex != operands.size()) |
| 3210 | return emitError(loc: unknownLoc, |
| 3211 | message: "unexpected trailing words in OpString instruction" ); |
| 3212 | |
| 3213 | debugInfoMap[operands[0]] = debugString; |
| 3214 | return success(); |
| 3215 | } |
| 3216 | |