| 1 | //===- WasmSSAOps.cpp - WasmSSA dialect operations ----------------===// |
| 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 | #include "mlir/Dialect/WasmSSA/IR/WasmSSA.h" |
| 10 | #include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.h" |
| 11 | |
| 12 | #include "mlir/IR/Attributes.h" |
| 13 | #include "mlir/IR/Builders.h" |
| 14 | #include "mlir/IR/BuiltinAttributes.h" |
| 15 | #include "mlir/IR/Diagnostics.h" |
| 16 | #include "mlir/IR/Dialect.h" |
| 17 | #include "mlir/IR/Region.h" |
| 18 | #include "mlir/IR/SymbolTable.h" |
| 19 | #include "mlir/Interfaces/FunctionImplementation.h" |
| 20 | #include "llvm/Support/Casting.h" |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // TableGen'd op method definitions |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | using namespace mlir; |
| 27 | namespace { |
| 28 | ParseResult parseElseRegion(OpAsmParser &opParser, Region &elseRegion) { |
| 29 | std::string keyword; |
| 30 | std::ignore = opParser.parseOptionalKeywordOrString(result: &keyword); |
| 31 | if (keyword == "else" ) |
| 32 | return opParser.parseRegion(region&: elseRegion); |
| 33 | return ParseResult::success(); |
| 34 | } |
| 35 | |
| 36 | void printElseRegion(OpAsmPrinter &opPrinter, Operation *op, |
| 37 | Region &elseRegion) { |
| 38 | if (elseRegion.empty()) |
| 39 | return; |
| 40 | opPrinter << "else " ; |
| 41 | opPrinter.printRegion(blocks&: elseRegion); |
| 42 | } |
| 43 | } // namespace |
| 44 | |
| 45 | #define GET_OP_CLASSES |
| 46 | #include "mlir/Dialect/WasmSSA/IR/WasmSSAOps.cpp.inc" |
| 47 | |
| 48 | #include "mlir/IR/OpImplementation.h" |
| 49 | #include "mlir/IR/Types.h" |
| 50 | #include "llvm/Support/LogicalResult.h" |
| 51 | |
| 52 | using namespace wasmssa; |
| 53 | |
| 54 | namespace { |
| 55 | inline LogicalResult |
| 56 | inferTeeGetResType(ValueRange operands, |
| 57 | SmallVectorImpl<Type> &inferredReturnTypes) { |
| 58 | if (operands.empty()) |
| 59 | return failure(); |
| 60 | auto opType = dyn_cast<LocalRefType>(Val: operands.front().getType()); |
| 61 | if (!opType) |
| 62 | return failure(); |
| 63 | inferredReturnTypes.push_back(Elt: opType.getElementType()); |
| 64 | return success(); |
| 65 | } |
| 66 | |
| 67 | ParseResult parseImportOp(OpAsmParser &parser, OperationState &result) { |
| 68 | std::string importName; |
| 69 | auto *ctx = parser.getContext(); |
| 70 | ParseResult res = parser.parseString(string: &importName); |
| 71 | result.addAttribute(name: "importName" , attr: StringAttr::get(context: ctx, bytes: importName)); |
| 72 | |
| 73 | std::string fromStr; |
| 74 | res = parser.parseKeywordOrString(result: &fromStr); |
| 75 | if (failed(Result: res) || fromStr != "from" ) |
| 76 | return failure(); |
| 77 | |
| 78 | std::string moduleName; |
| 79 | res = parser.parseString(string: &moduleName); |
| 80 | if (failed(Result: res)) |
| 81 | return failure(); |
| 82 | result.addAttribute(name: "moduleName" , attr: StringAttr::get(context: ctx, bytes: moduleName)); |
| 83 | |
| 84 | std::string asStr; |
| 85 | res = parser.parseKeywordOrString(result: &asStr); |
| 86 | if (failed(Result: res) || asStr != "as" ) |
| 87 | return failure(); |
| 88 | |
| 89 | StringAttr symbolName; |
| 90 | res = parser.parseSymbolName(result&: symbolName); |
| 91 | if (succeeded(Result: res)) |
| 92 | result.getOrAddProperties<GlobalImportOp::Properties>().sym_name = |
| 93 | symbolName; |
| 94 | return res; |
| 95 | } |
| 96 | } // namespace |
| 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | // BlockOp |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
| 102 | Block *BlockOp::getLabelTarget() { return getTarget(); } |
| 103 | |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | // BlockReturnOp |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | std::size_t BlockReturnOp::getExitLevel() { return 0; } |
| 109 | |
| 110 | Block *BlockReturnOp::getTarget() { |
| 111 | return cast<LabelBranchingOpInterface>(Val: getOperation()) |
| 112 | .getTargetOp() |
| 113 | .getOperation() |
| 114 | ->getSuccessor(index: 0); |
| 115 | } |
| 116 | |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | // ExtendLowBitsSOp |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
| 121 | LogicalResult ExtendLowBitsSOp::verify() { |
| 122 | auto bitsToTake = getBitsToTake().getValue().getLimitedValue(); |
| 123 | if (bitsToTake != 32 && bitsToTake != 16 && bitsToTake != 8) |
| 124 | return emitError(message: "extend op can only take 8, 16 or 32 bits. Got " ) |
| 125 | << bitsToTake; |
| 126 | |
| 127 | if (bitsToTake >= getInput().getType().getIntOrFloatBitWidth()) |
| 128 | return emitError(message: "trying to extend the " ) |
| 129 | << bitsToTake << " low bits from a " << getInput().getType() |
| 130 | << " value is illegal" ; |
| 131 | return success(); |
| 132 | } |
| 133 | |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | // FuncOp |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | |
| 138 | Block *FuncOp::addEntryBlock() { |
| 139 | if (!getBody().empty()) { |
| 140 | emitError(message: "adding entry block to a FuncOp which already has one" ); |
| 141 | return &getBody().front(); |
| 142 | } |
| 143 | Block &block = getBody().emplaceBlock(); |
| 144 | for (auto argType : getFunctionType().getInputs()) |
| 145 | block.addArgument(type: LocalRefType::get(typeParam: argType), loc: getLoc()); |
| 146 | return █ |
| 147 | } |
| 148 | |
| 149 | void FuncOp::build(OpBuilder &odsBuilder, OperationState &odsState, |
| 150 | StringRef symbol, FunctionType funcType) { |
| 151 | FuncOp::build(odsBuilder, odsState, sym_name: symbol, functionType: funcType, arg_attrs: {}, res_attrs: {}); |
| 152 | } |
| 153 | |
| 154 | ParseResult FuncOp::parse(OpAsmParser &parser, OperationState &result) { |
| 155 | auto *ctx = parser.getContext(); |
| 156 | std::string visibilityString; |
| 157 | auto loc = parser.getNameLoc(); |
| 158 | ParseResult res = parser.parseOptionalKeywordOrString(result: &visibilityString); |
| 159 | bool exported{false}; |
| 160 | if (res.succeeded()) { |
| 161 | if (visibilityString != "exported" ) |
| 162 | return parser.emitError( |
| 163 | loc, message: "expecting either `exported` or symbol name. got " ) |
| 164 | << visibilityString; |
| 165 | exported = true; |
| 166 | } |
| 167 | |
| 168 | auto buildFuncType = [&parser](Builder &builder, ArrayRef<Type> argTypes, |
| 169 | ArrayRef<Type> results, |
| 170 | function_interface_impl::VariadicFlag, |
| 171 | std::string &) { |
| 172 | SmallVector<Type> argTypesWithoutLocal{}; |
| 173 | argTypesWithoutLocal.reserve(N: argTypes.size()); |
| 174 | llvm::for_each(Range&: argTypes, F: [&parser, &argTypesWithoutLocal](Type argType) { |
| 175 | auto refType = dyn_cast<LocalRefType>(Val&: argType); |
| 176 | auto loc = parser.getEncodedSourceLoc(loc: parser.getCurrentLocation()); |
| 177 | if (!refType) { |
| 178 | mlir::emitError(loc, message: "invalid type for wasm.func argument. Expecting " |
| 179 | "!wasm<local T>, got " ) |
| 180 | << argType; |
| 181 | return; |
| 182 | } |
| 183 | argTypesWithoutLocal.push_back(Elt: refType.getElementType()); |
| 184 | }); |
| 185 | |
| 186 | return builder.getFunctionType(inputs: argTypesWithoutLocal, results); |
| 187 | }; |
| 188 | auto funcParseRes = function_interface_impl::parseFunctionOp( |
| 189 | parser, result, /*allowVariadic=*/false, |
| 190 | typeAttrName: getFunctionTypeAttrName(name: result.name), funcTypeBuilder: buildFuncType, |
| 191 | argAttrsName: getArgAttrsAttrName(name: result.name), resAttrsName: getResAttrsAttrName(name: result.name)); |
| 192 | if (exported) |
| 193 | result.addAttribute(name: getExportedAttrName(name: result.name), attr: UnitAttr::get(context: ctx)); |
| 194 | return funcParseRes; |
| 195 | } |
| 196 | |
| 197 | LogicalResult FuncOp::verifyBody() { |
| 198 | if (getBody().empty()) |
| 199 | return success(); |
| 200 | Block &entry = getBody().front(); |
| 201 | if (entry.getNumArguments() != getFunctionType().getNumInputs()) |
| 202 | return emitError(message: "entry block should have same number of arguments as " |
| 203 | "function type. Function type has " ) |
| 204 | << getFunctionType().getNumInputs() << ", entry block has " |
| 205 | << entry.getNumArguments(); |
| 206 | |
| 207 | for (auto [argNo, funcSignatureType, blockType] : llvm::enumerate( |
| 208 | First: getFunctionType().getInputs(), Rest: entry.getArgumentTypes())) { |
| 209 | auto blockLocalRefType = dyn_cast<LocalRefType>(Val&: blockType); |
| 210 | if (!blockLocalRefType) |
| 211 | return emitError(message: "entry block argument type should be LocalRefType, got " ) |
| 212 | << blockType << " for block argument " << argNo; |
| 213 | if (blockLocalRefType.getElementType() != funcSignatureType) |
| 214 | return emitError(message: "func argument type #" ) |
| 215 | << argNo << "(" << funcSignatureType |
| 216 | << ") doesn't match entry block referenced type (" |
| 217 | << blockLocalRefType.getElementType() << ")" ; |
| 218 | } |
| 219 | return success(); |
| 220 | } |
| 221 | |
| 222 | void FuncOp::print(OpAsmPrinter &p) { |
| 223 | /// If exported, print it before and mask it before printing |
| 224 | /// using generic interface. |
| 225 | auto exported = getExported(); |
| 226 | if (exported) { |
| 227 | p << " exported" ; |
| 228 | removeExportedAttr(); |
| 229 | } |
| 230 | function_interface_impl::printFunctionOp( |
| 231 | p, op: *this, /*isVariadic=*/false, typeAttrName: getFunctionTypeAttrName(), |
| 232 | argAttrsName: getArgAttrsAttrName(), resAttrsName: getResAttrsAttrName()); |
| 233 | if (exported) |
| 234 | setExported(true); |
| 235 | } |
| 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // FuncImportOp |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | |
| 241 | void FuncImportOp::build(OpBuilder &odsBuilder, OperationState &odsState, |
| 242 | StringRef symbol, StringRef moduleName, |
| 243 | StringRef importName, FunctionType type) { |
| 244 | FuncImportOp::build(odsBuilder, odsState, sym_name: symbol, moduleName, importName, |
| 245 | type, arg_attrs: {}, res_attrs: {}); |
| 246 | } |
| 247 | |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | // GlobalOp |
| 250 | //===----------------------------------------------------------------------===// |
| 251 | namespace { |
| 252 | Operation *getGlobalOpTerminatorOp(GlobalOp gop) { |
| 253 | return gop.getInitializer().begin()->getTerminator(); |
| 254 | } |
| 255 | } // namespace |
| 256 | |
| 257 | ReturnOp GlobalOp::getInitTerminator() { |
| 258 | return llvm::cast<wasmssa::ReturnOp>(Val: getGlobalOpTerminatorOp(gop: *this)); |
| 259 | } |
| 260 | |
| 261 | // Custom formats |
| 262 | ParseResult GlobalOp::parse(OpAsmParser &parser, OperationState &result) { |
| 263 | StringAttr symbolName; |
| 264 | Type globalType; |
| 265 | auto *ctx = parser.getContext(); |
| 266 | std::string visibilityString; |
| 267 | auto loc = parser.getNameLoc(); |
| 268 | ParseResult res = parser.parseOptionalKeywordOrString(result: &visibilityString); |
| 269 | if (res.succeeded()) { |
| 270 | if (visibilityString != "exported" ) |
| 271 | return parser.emitError( |
| 272 | loc, message: "expecting either `exported` or symbol name. got " ) |
| 273 | << visibilityString; |
| 274 | result.addAttribute(name: getExportedAttrName(name: result.name), attr: UnitAttr::get(context: ctx)); |
| 275 | } |
| 276 | |
| 277 | res = parser.parseSymbolName(result&: symbolName, attrName: getSymNameAttrName(name: result.name), |
| 278 | attrs&: result.attributes); |
| 279 | res = parser.parseType(result&: globalType); |
| 280 | result.addAttribute(name: getTypeAttrName(name: result.name), attr: TypeAttr::get(type: globalType)); |
| 281 | std::string mutableString; |
| 282 | res = parser.parseOptionalKeywordOrString(result: &mutableString); |
| 283 | if (res.succeeded() && mutableString == "mutable" ) |
| 284 | result.addAttribute(name: "isMutable" , attr: UnitAttr::get(context: ctx)); |
| 285 | |
| 286 | res = parser.parseColon(); |
| 287 | Region *globalInitRegion = result.addRegion(); |
| 288 | res = parser.parseRegion(region&: *globalInitRegion); |
| 289 | return res; |
| 290 | } |
| 291 | |
| 292 | void GlobalOp::print(OpAsmPrinter &printer) { |
| 293 | if (getExported()) |
| 294 | printer << " exported" ; |
| 295 | printer << " @" << getSymName().str() << " " << getType(); |
| 296 | if (getIsMutable()) |
| 297 | printer << " mutable" ; |
| 298 | printer << " :" ; |
| 299 | Region &body = getRegion(); |
| 300 | if (!body.empty()) { |
| 301 | printer << ' '; |
| 302 | printer.printRegion(blocks&: body, /*printEntryBlockArgs=*/false, |
| 303 | /*printBlockTerminators=*/true); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | LogicalResult GlobalOp::verify() { |
| 308 | return success(IsSuccess: llvm::isa<ReturnOp>(Val: getGlobalOpTerminatorOp(gop: *this))); |
| 309 | } |
| 310 | |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | // GlobalGetOp |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | |
| 315 | LogicalResult |
| 316 | GlobalGetOp::verifySymbolUses(SymbolTableCollection &symbolTable) { |
| 317 | // If the parent requires a constant context, verify that global.get is a |
| 318 | // constant as defined per the wasm standard. |
| 319 | if (!this->getOperation() |
| 320 | ->getParentWithTrait<ConstantExpressionInitializerOpTrait>()) |
| 321 | return success(); |
| 322 | Operation *symTabOp = SymbolTable::getNearestSymbolTable(from: *this); |
| 323 | StringRef referencedSymbol = getGlobal(); |
| 324 | Operation *definitionOp = symbolTable.lookupSymbolIn( |
| 325 | symbolTableOp: symTabOp, symbol: StringAttr::get(context: this->getContext(), bytes: referencedSymbol)); |
| 326 | if (!definitionOp) |
| 327 | return emitError() << "symbol @" << referencedSymbol << " is undefined" ; |
| 328 | auto definitionImport = dyn_cast<GlobalImportOp>(Val: definitionOp); |
| 329 | if (!definitionImport || definitionImport.getIsMutable()) { |
| 330 | return emitError(message: "global.get op is considered constant if it's referring " |
| 331 | "to a import.global symbol marked non-mutable" ); |
| 332 | } |
| 333 | return success(); |
| 334 | } |
| 335 | |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | // GlobalSetOp |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
| 340 | LogicalResult |
| 341 | GlobalSetOp::verifySymbolUses(SymbolTableCollection &symbolTable) { |
| 342 | Operation *symTabOp = SymbolTable::getNearestSymbolTable(from: *this); |
| 343 | StringRef referencedSymbol = getGlobal(); |
| 344 | Operation *definitionOp = symbolTable.lookupSymbolIn( |
| 345 | symbolTableOp: symTabOp, symbol: StringAttr::get(context: this->getContext(), bytes: referencedSymbol)); |
| 346 | if (!definitionOp) |
| 347 | return emitError() << "symbol @" << referencedSymbol << " is undefined" ; |
| 348 | |
| 349 | Type globalType; |
| 350 | bool isMutable = false; |
| 351 | if (auto global = dyn_cast<GlobalOp>(Val: definitionOp)) { |
| 352 | globalType = global.getType(); |
| 353 | isMutable = global.getIsMutable(); |
| 354 | } else if (auto globalImport = dyn_cast<GlobalImportOp>(Val: definitionOp)) { |
| 355 | globalType = globalImport.getType(); |
| 356 | isMutable = globalImport.getIsMutable(); |
| 357 | } else { |
| 358 | return emitError() << "symbol @" << referencedSymbol |
| 359 | << " is not a global symbol" ; |
| 360 | } |
| 361 | |
| 362 | if (!isMutable) |
| 363 | return emitError(message: "global.set target must be mutable" ); |
| 364 | |
| 365 | Type valueType = getValue().getType(); |
| 366 | if (globalType != valueType) |
| 367 | return emitError(message: "global.set value type does not match target global " |
| 368 | "type: expected " ) |
| 369 | << globalType << " but got " << valueType; |
| 370 | |
| 371 | return success(); |
| 372 | } |
| 373 | |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | // GlobalImportOp |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | ParseResult GlobalImportOp::parse(OpAsmParser &parser, OperationState &result) { |
| 379 | auto *ctx = parser.getContext(); |
| 380 | ParseResult res = parseImportOp(parser, result); |
| 381 | if (res.failed()) |
| 382 | return failure(); |
| 383 | std::string mutableOrSymVisString; |
| 384 | res = parser.parseOptionalKeywordOrString(result: &mutableOrSymVisString); |
| 385 | if (res.succeeded() && mutableOrSymVisString == "mutable" ) { |
| 386 | result.addAttribute(name: "isMutable" , attr: UnitAttr::get(context: ctx)); |
| 387 | } |
| 388 | |
| 389 | res = parser.parseColon(); |
| 390 | |
| 391 | Type importedType; |
| 392 | res = parser.parseType(result&: importedType); |
| 393 | if (res.succeeded()) |
| 394 | result.addAttribute(name: getTypeAttrName(name: result.name), |
| 395 | attr: TypeAttr::get(type: importedType)); |
| 396 | return res; |
| 397 | } |
| 398 | |
| 399 | void GlobalImportOp::print(OpAsmPrinter &printer) { |
| 400 | printer << " \"" << getImportName() << "\" from \"" << getModuleName() |
| 401 | << "\" as @" << getSymName(); |
| 402 | if (getIsMutable()) |
| 403 | printer << " mutable" ; |
| 404 | printer << " : " << getType(); |
| 405 | } |
| 406 | |
| 407 | //===----------------------------------------------------------------------===// |
| 408 | // IfOp |
| 409 | //===----------------------------------------------------------------------===// |
| 410 | |
| 411 | Block *IfOp::getLabelTarget() { return getTarget(); } |
| 412 | |
| 413 | //===----------------------------------------------------------------------===// |
| 414 | // LocalOp |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
| 417 | LogicalResult LocalOp::inferReturnTypes( |
| 418 | MLIRContext *context, ::std::optional<Location> location, |
| 419 | ValueRange operands, DictionaryAttr attributes, PropertyRef properties, |
| 420 | RegionRange regions, SmallVectorImpl<Type> &inferredReturnTypes) { |
| 421 | LocalOp::GenericAdaptor<ValueRange> adaptor{operands, attributes, properties, |
| 422 | regions}; |
| 423 | auto type = adaptor.getTypeAttr(); |
| 424 | if (!type) |
| 425 | return failure(); |
| 426 | auto resType = LocalRefType::get(context: type.getContext(), elementType: type.getValue()); |
| 427 | inferredReturnTypes.push_back(Elt: resType); |
| 428 | return success(); |
| 429 | } |
| 430 | |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | // LocalGetOp |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | |
| 435 | LogicalResult LocalGetOp::inferReturnTypes( |
| 436 | MLIRContext *context, ::std::optional<Location> location, |
| 437 | ValueRange operands, DictionaryAttr attributes, PropertyRef properties, |
| 438 | RegionRange regions, SmallVectorImpl<Type> &inferredReturnTypes) { |
| 439 | return inferTeeGetResType(operands, inferredReturnTypes); |
| 440 | } |
| 441 | |
| 442 | //===----------------------------------------------------------------------===// |
| 443 | // LocalSetOp |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
| 446 | LogicalResult LocalSetOp::verify() { |
| 447 | if (getLocalVar().getType().getElementType() != getValue().getType()) |
| 448 | return emitError(message: "input type and result type of local.set do not match" ); |
| 449 | return success(); |
| 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | // LocalTeeOp |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
| 456 | LogicalResult LocalTeeOp::inferReturnTypes( |
| 457 | MLIRContext *context, ::std::optional<Location> location, |
| 458 | ValueRange operands, DictionaryAttr attributes, PropertyRef properties, |
| 459 | RegionRange regions, SmallVectorImpl<Type> &inferredReturnTypes) { |
| 460 | return inferTeeGetResType(operands, inferredReturnTypes); |
| 461 | } |
| 462 | |
| 463 | LogicalResult LocalTeeOp::verify() { |
| 464 | if (getLocalVar().getType().getElementType() != getValue().getType() || |
| 465 | getValue().getType() != getResult().getType()) |
| 466 | return emitError(message: "input type and output type of local.tee do not match" ); |
| 467 | return success(); |
| 468 | } |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | // LoopOp |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | |
| 474 | Block *LoopOp::getLabelTarget() { return &getBody().front(); } |
| 475 | |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | // ReinterpretOp |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
| 480 | LogicalResult ReinterpretOp::verify() { |
| 481 | auto inT = getInput().getType(); |
| 482 | auto resT = getResult().getType(); |
| 483 | if (inT == resT) |
| 484 | return emitError(message: "reinterpret input and output type should be distinct" ); |
| 485 | if (inT.getIntOrFloatBitWidth() != resT.getIntOrFloatBitWidth()) |
| 486 | return emitError() << "input type (" << inT << ") and output type (" << resT |
| 487 | << ") have incompatible bit widths" ; |
| 488 | return success(); |
| 489 | } |
| 490 | |
| 491 | //===----------------------------------------------------------------------===// |
| 492 | // ReturnOp |
| 493 | //===----------------------------------------------------------------------===// |
| 494 | |
| 495 | void ReturnOp::build(OpBuilder &odsBuilder, OperationState &odsState) {} |
| 496 | |
| 497 | LogicalResult ReturnOp::verify() { |
| 498 | auto funcOp = (*this)->getParentOfType<FuncOp>(); |
| 499 | if (!funcOp) |
| 500 | return success(); |
| 501 | ArrayRef<Type> resultTypes = funcOp.getFunctionType().getResults(); |
| 502 | if (getOperands().size() != resultTypes.size()) |
| 503 | return emitOpError(message: "has " ) << getOperands().size() |
| 504 | << " operands, but enclosing function returns " |
| 505 | << resultTypes.size(); |
| 506 | for (auto [idx, resultType, operandType] : |
| 507 | llvm::enumerate(First&: resultTypes, Rest: getOperands().getTypes())) |
| 508 | if (resultType != operandType) |
| 509 | return emitOpError(message: "type of return operand #" ) |
| 510 | << idx << " (" << operandType |
| 511 | << ") doesn't match function result type (" << resultType << ")" ; |
| 512 | return success(); |
| 513 | } |
| 514 | |