24#include "llvm/Support/Debug.h"
25#include "llvm/Support/DebugLog.h"
26#include "llvm/Support/Endian.h"
27#include "llvm/Support/FormatVariadic.h"
28#include "llvm/Support/LEB128.h"
29#include "llvm/Support/LogicalResult.h"
37#define DEBUG_TYPE "wasm-translate"
39static_assert(CHAR_BIT == 8,
40 "This code expects std::byte to be exactly 8 bits");
47using section_id_t = uint8_t;
48enum struct WasmSectionType : section_id_t {
64constexpr section_id_t highestWasmSectionID{
65 static_cast<section_id_t
>(WasmSectionType::DATACOUNT)};
67#define APPLY_WASM_SEC_TRANSFORM \
68 WASM_SEC_TRANSFORM(CUSTOM) \
69 WASM_SEC_TRANSFORM(TYPE) \
70 WASM_SEC_TRANSFORM(IMPORT) \
71 WASM_SEC_TRANSFORM(FUNCTION) \
72 WASM_SEC_TRANSFORM(TABLE) \
73 WASM_SEC_TRANSFORM(MEMORY) \
74 WASM_SEC_TRANSFORM(GLOBAL) \
75 WASM_SEC_TRANSFORM(EXPORT) \
76 WASM_SEC_TRANSFORM(START) \
77 WASM_SEC_TRANSFORM(ELEMENT) \
78 WASM_SEC_TRANSFORM(CODE) \
79 WASM_SEC_TRANSFORM(DATA) \
80 WASM_SEC_TRANSFORM(DATACOUNT)
82template <WasmSectionType>
83constexpr const char *wasmSectionName =
"";
85#define WASM_SEC_TRANSFORM(section) \
87 [[maybe_unused]] constexpr const char \
88 *wasmSectionName<WasmSectionType::section> = #section;
90#undef WASM_SEC_TRANSFORM
92constexpr bool sectionShouldBeUnique(WasmSectionType secType) {
93 return secType != WasmSectionType::CUSTOM;
96template <std::byte... Bytes>
97struct ByteSequence {};
100template <std::
byte Byte>
101struct UniqueByte : ByteSequence<Byte> {};
103[[maybe_unused]]
constexpr ByteSequence<
108template <std::byte... allowedFlags>
109constexpr bool isValueOneOf(std::byte value,
110 ByteSequence<allowedFlags...> = {}) {
111 return ((value == allowedFlags) | ... |
false);
114template <std::byte... flags>
115constexpr bool isNotIn(std::byte value, ByteSequence<flags...> = {}) {
116 return !isValueOneOf<flags...>(value);
119struct GlobalTypeRecord {
124struct TypeIdxRecord {
128struct SymbolRefContainer {
129 FlatSymbolRefAttr symbol;
132struct GlobalSymbolRefContainer : SymbolRefContainer {
136struct FunctionSymbolRefContainer : SymbolRefContainer {
137 FunctionType functionType;
141 std::variant<TypeIdxRecord, TableType, LimitType, GlobalTypeRecord>;
143using parsed_inst_t = FailureOr<SmallVector<Value>>;
145struct EmptyBlockMarker {};
146using BlockTypeParseResult =
147 std::variant<EmptyBlockMarker, TypeIdxRecord, Type>;
149struct WasmModuleSymbolTables {
150 SmallVector<FunctionSymbolRefContainer> funcSymbols;
151 SmallVector<GlobalSymbolRefContainer> globalSymbols;
152 SmallVector<SymbolRefContainer> memSymbols;
153 SmallVector<SymbolRefContainer> tableSymbols;
154 SmallVector<FunctionType> moduleFuncTypes;
156 std::string getNewSymbolName(StringRef prefix,
size_t id)
const {
157 return (prefix + Twine{
id}).str();
160 std::string getNewFuncSymbolName()
const {
161 size_t id = funcSymbols.size();
162 return getNewSymbolName(
"func_",
id);
165 std::string getNewGlobalSymbolName()
const {
166 size_t id = globalSymbols.size();
167 return getNewSymbolName(
"global_",
id);
170 std::string getNewMemorySymbolName()
const {
171 size_t id = memSymbols.size();
172 return getNewSymbolName(
"mem_",
id);
175 std::string getNewTableSymbolName()
const {
176 size_t id = tableSymbols.size();
177 return getNewSymbolName(
"table_",
id);
193 LabelLevelOpInterface levelOp;
197 bool empty()
const {
return values.empty(); }
199 size_t size()
const {
return values.size(); }
209 FailureOr<SmallVector<Value>> popOperands(
TypeRange operandTypes,
218 LogicalResult pushResults(
ValueRange results, Location *opLoc);
220 void addLabelLevel(LabelLevelOpInterface levelOp) {
221 labelLevel.push_back({values.size(), levelOp});
222 LDBG() <<
"Adding a new frame context to ValueStack";
225 void dropLabelLevel() {
226 assert(!labelLevel.empty() &&
"Trying to drop a frame from empty context");
227 auto newSize = labelLevel.pop_back_val().stackIdx;
228 values.truncate(newSize);
230#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
233 LLVM_DUMP_METHOD
void dump()
const;
237 SmallVector<Value> values;
238 SmallVector<LabelLevel> labelLevel;
246constexpr std::byte byteAt =
static_cast<std::byte
>(I);
248template <
size_t... IS>
249constexpr ByteSequence<byteAt<IS>...>
250castIndexSequenceToBytes(std::index_sequence<IS...>) {
254constexpr auto all8bitsBytes =
255 castIndexSequenceToBytes(std::make_index_sequence<256>());
260class ExpressionParser {
262 using locals_t = SmallVector<local_val_t>;
263 ExpressionParser(ParserHead &parser, WasmModuleSymbolTables
const &symbols,
264 ArrayRef<local_val_t> initLocal)
265 : parser{parser}, symbols{symbols}, locals{initLocal} {}
268 inline parsed_inst_t dispatchToInstParser(std::byte opCode,
273 struct NestingContextGuard {
274 NestingContextGuard(ExpressionParser &parser, LabelLevelOpInterface levelOp)
276 parser.addNestingContextLevel(levelOp);
278 NestingContextGuard(NestingContextGuard &&other) : parser{other.parser} {
279 other.shouldDropOnDestruct =
false;
281 NestingContextGuard(NestingContextGuard
const &) =
delete;
282 ~NestingContextGuard() {
283 if (shouldDropOnDestruct)
284 parser.dropNestingContextLevel();
286 ExpressionParser &parser;
287 bool shouldDropOnDestruct =
true;
290 void addNestingContextLevel(LabelLevelOpInterface levelOp) {
291 valueStack.addLabelLevel(levelOp);
294 void dropNestingContextLevel() {
297 valueStack.dropLabelLevel();
300 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
305 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
306 TypeIdxRecord type) {
307 if (type.id >= symbols.moduleFuncTypes.size())
309 "type index references nonexistent type (")
310 << type.id <<
"). Only " << symbols.moduleFuncTypes.size()
311 <<
" types are registered";
312 return symbols.moduleFuncTypes[type.id];
315 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
320 llvm::FailureOr<FunctionType>
321 getFuncTypeFor(OpBuilder &builder, BlockTypeParseResult parseResult) {
323 [
this, &builder](
auto value) {
return getFuncTypeFor(builder, value); },
327 llvm::FailureOr<FunctionType>
328 getFuncTypeFor(OpBuilder &builder,
329 llvm::FailureOr<BlockTypeParseResult> parseResult) {
330 if (llvm::failed(parseResult))
332 return getFuncTypeFor(builder, *parseResult);
335 struct ParseResultWithInfo {
336 SmallVector<Value> opResults;
337 std::byte endingByte;
340 llvm::FailureOr<FunctionType> parseBlockFuncType(OpBuilder &builder);
343 template <std::
byte ParseEndByte = WasmBinaryEncoding::endByte>
344 parsed_inst_t
parse(OpBuilder &builder, UniqueByte<ParseEndByte> = {});
346 template <std::byte... ExpressionParseEnd>
347 FailureOr<ParseResultWithInfo>
348 parse(OpBuilder &builder,
349 ByteSequence<ExpressionParseEnd...> parsingEndFilters);
351 NestingContextGuard addNesting(LabelLevelOpInterface levelOp) {
352 return NestingContextGuard{*
this, levelOp};
355 FailureOr<llvm::SmallVector<Value>> popOperands(
TypeRange operandTypes) {
356 return valueStack.popOperands(operandTypes, ¤tOpLoc.value());
359 LogicalResult pushResults(
ValueRange results) {
360 return valueStack.pushResults(results, ¤tOpLoc.value());
366 template <
typename OpToCreate>
367 parsed_inst_t parseSetOrTee(OpBuilder &);
369 Location getCurrentOpLoc() {
370 assert(currentOpLoc.has_value() &&
371 "expects current opcode location to be set");
372 return *currentOpLoc;
375 class ExprParserProxy {
377 friend ExpressionParser;
378 inline auto parseBlockFuncType(OpBuilder &builder) {
379 return exprParser.parseBlockFuncType(builder);
384 template <
typename FilterT = ByteSequence<WasmBinaryEncoding::endByte>>
385 llvm::FailureOr<std::byte>
386 parseBlockContent(OpBuilder &builder,
Block *blockToFill,
388 LabelLevelOpInterface levelOp,
389 FilterT parseEndBytes = {}) {
390 OpBuilder::InsertionGuard guard(builder);
391 assert(blockToFill && blockToFill->
empty() &&
"expected an empty block");
393 LDBG() <<
"parsing a block of type "
396 auto nestingGuard = exprParser.addNesting(levelOp);
400 auto bodyParsingRes = exprParser.parse(builder, parseEndBytes);
401 if (
failed(bodyParsingRes))
403 auto returnOperands = exprParser.popOperands(resTypes);
404 if (
failed(returnOperands))
406 BlockReturnOp::create(builder, opLoc, *returnOperands);
407 LDBG() <<
"end of parsing of a block";
408 return bodyParsingRes->endingByte;
411 inline ParserHead &parser() {
return exprParser.parser; }
416 template <
typename OpToCreate>
417 parsed_inst_t parseBlockLikeOp(OpBuilder &);
419 inline auto getCurrentOpLoc() {
return exprParser.getCurrentOpLoc(); }
421 inline auto popOperands(
TypeRange operandTypes) {
422 return exprParser.popOperands(operandTypes);
425 inline auto &symbols() {
return exprParser.symbols; }
427 inline auto &locals() {
return exprParser.locals; }
429 template <
typename OpToCreate>
430 parsed_inst_t parseSetOrTee(OpBuilder &builder) {
431 return exprParser.parseSetOrTee<OpToCreate>(builder);
434 template <
typename valueT>
436 parseConstInst(OpBuilder &builder,
437 std::enable_if_t<std::is_arithmetic_v<valueT>> * =
nullptr);
448 template <
typename opcode,
typename valueType,
unsigned int numOperands>
449 inline parsed_inst_t buildNumericOp(
451 std::enable_if_t<std::is_arithmetic_v<valueType>> * =
nullptr);
463 template <
typename opType,
typename inputType,
typename outputType,
464 typename... extraArgsT>
465 inline parsed_inst_t buildConvertOp(OpBuilder &builder, extraArgsT...);
468 explicit ExprParserProxy(ExpressionParser &exprParser)
469 : exprParser{exprParser} {};
472 ExpressionParser &exprParser;
476 std::optional<Location> currentOpLoc;
478 WasmModuleSymbolTables
const &symbols;
480 ValueStack valueStack;
485 ParserHead(StringRef src, StringAttr name) : head{src}, locName{name} {}
486 ParserHead(ParserHead &&) =
default;
489 ParserHead(ParserHead
const &other) =
default;
492 auto getLocation()
const {
496 FailureOr<StringRef> consumeNBytes(
size_t nBytes) {
497 LDBG() <<
"Consume " << nBytes <<
" bytes";
498 LDBG() <<
" Bytes remaining: " << size();
499 LDBG() <<
" Current offset: " << offset;
501 return emitError(getLocation(),
"trying to extract ")
502 << nBytes <<
"bytes when only " << size() <<
"are available";
504 StringRef res = head.slice(offset, offset + nBytes);
506 LDBG() <<
" Updated offset (+" << nBytes <<
"): " << offset;
510 FailureOr<std::byte> consumeByte() {
511 FailureOr<StringRef> res = consumeNBytes(1);
514 return std::byte{*res->bytes_begin()};
517 template <
typename T>
518 FailureOr<T> parseLiteral();
520 FailureOr<uint32_t> parseVectorSize();
526 inline FailureOr<uint32_t> parseUI32();
527 inline FailureOr<int64_t> parseI64();
530 FailureOr<StringRef> parseName() {
531 FailureOr<uint32_t> size = parseVectorSize();
535 return consumeNBytes(*size);
538 FailureOr<WasmSectionType> parseWasmSectionType() {
539 FailureOr<std::byte>
id = consumeByte();
542 if (std::to_integer<unsigned>(*
id) > highestWasmSectionID)
543 return emitError(getLocation(),
"invalid section ID: ")
544 <<
static_cast<int>(*id);
545 return static_cast<WasmSectionType
>(*id);
548 FailureOr<LimitType> parseLimit(MLIRContext *ctx) {
549 using WasmLimits = WasmBinaryEncoding::LimitHeader;
550 FileLineColLoc limitLocation = getLocation();
551 FailureOr<std::byte> limitHeader = consumeByte();
555 if (isNotIn<WasmLimits::bothLimits, WasmLimits::lowLimitOnly>(*limitHeader))
556 return emitError(limitLocation,
"invalid limit header: ")
557 <<
static_cast<int>(*limitHeader);
558 FailureOr<uint32_t> minParse = parseUI32();
561 std::optional<uint32_t>
max{std::nullopt};
562 if (*limitHeader == WasmLimits::bothLimits) {
563 FailureOr<uint32_t> maxParse = parseUI32();
568 return LimitType::get(ctx, *minParse,
max);
571 FailureOr<Type> parseValueType(MLIRContext *ctx) {
572 FileLineColLoc typeLoc = getLocation();
573 FailureOr<std::byte> typeEncoding = consumeByte();
576 switch (*typeEncoding) {
578 return IntegerType::get(ctx, 32);
580 return IntegerType::get(ctx, 64);
582 return Float32Type::get(ctx);
584 return Float64Type::get(ctx);
586 return IntegerType::get(ctx, 128);
588 return wasmssa::FuncRefType::get(ctx);
590 return wasmssa::ExternRefType::get(ctx);
592 return emitError(typeLoc,
"invalid value type encoding: ")
593 <<
static_cast<int>(*typeEncoding);
597 FailureOr<GlobalTypeRecord> parseGlobalType(MLIRContext *ctx) {
598 using WasmGlobalMut = WasmBinaryEncoding::GlobalMutability;
599 FailureOr<Type> typeParsed = parseValueType(ctx);
602 FileLineColLoc mutLoc = getLocation();
603 FailureOr<std::byte> mutSpec = consumeByte();
606 if (isNotIn<WasmGlobalMut::isConst, WasmGlobalMut::isMutable>(*mutSpec))
607 return emitError(mutLoc,
"invalid global mutability specifier: ")
608 <<
static_cast<int>(*mutSpec);
609 return GlobalTypeRecord{*typeParsed, *mutSpec == WasmGlobalMut::isMutable};
612 FailureOr<TupleType> parseResultType(MLIRContext *ctx) {
613 FailureOr<uint32_t> nParamsParsed = parseVectorSize();
614 if (
failed(nParamsParsed))
616 uint32_t nParams = *nParamsParsed;
617 SmallVector<Type> res{};
618 res.reserve(nParams);
619 for (
size_t i = 0; i < nParams; ++i) {
620 FailureOr<Type> parsedType = parseValueType(ctx);
623 res.push_back(*parsedType);
625 return TupleType::get(ctx, res);
628 FailureOr<FunctionType> parseFunctionType(MLIRContext *ctx) {
629 FileLineColLoc typeLoc = getLocation();
630 FailureOr<std::byte> funcTypeHeader = consumeByte();
631 if (
failed(funcTypeHeader))
634 return emitError(typeLoc,
"invalid function type header byte. Expecting ")
636 <<
" got " << std::to_integer<unsigned>(*funcTypeHeader);
637 FailureOr<TupleType> inputTypes = parseResultType(ctx);
641 FailureOr<TupleType> resTypes = parseResultType(ctx);
645 return FunctionType::get(ctx, inputTypes->getTypes(), resTypes->getTypes());
648 FailureOr<TypeIdxRecord> parseTypeIndex() {
649 FailureOr<uint32_t> res = parseUI32();
652 return TypeIdxRecord{*res};
655 FailureOr<TableType> parseTableType(MLIRContext *ctx) {
656 FailureOr<Type> elmTypeParse = parseValueType(ctx);
660 return emitError(getLocation(),
"invalid element type for table");
661 FailureOr<LimitType> limitParse = parseLimit(ctx);
664 return TableType::get(ctx, *elmTypeParse, *limitParse);
667 FailureOr<ImportDesc> parseImportDesc(MLIRContext *ctx) {
668 FileLineColLoc importLoc = getLocation();
669 FailureOr<std::byte> importType = consumeByte();
670 auto packager = [](
auto parseResult) -> FailureOr<ImportDesc> {
673 return {*parseResult};
677 switch (*importType) {
679 return packager(parseTypeIndex());
681 return packager(parseTableType(ctx));
683 return packager(parseLimit(ctx));
685 return packager(parseGlobalType(ctx));
687 return emitError(importLoc,
"invalid import type descriptor: ")
688 <<
static_cast<int>(*importType);
692 parsed_inst_t parseExpression(OpBuilder &builder,
693 WasmModuleSymbolTables
const &symbols,
694 ArrayRef<local_val_t> locals = {}) {
695 auto eParser = ExpressionParser{*
this, symbols, locals};
696 return eParser.parse(builder);
699 LogicalResult parseCodeFor(FuncOp func,
700 WasmModuleSymbolTables
const &symbols) {
701 SmallVector<local_val_t> locals{};
703 Block &block = func.getBody().front();
705 assert(func.getBody().getBlocks().size() == 1 &&
706 "Function should only have its default created block at this point");
708 "Only the placeholder return op should be present at this point");
709 auto returnOp = cast<ReturnOp>(&block.
back());
712 FailureOr<uint32_t> codeSizeInBytes = parseUI32();
713 if (
failed(codeSizeInBytes))
715 FailureOr<StringRef> codeContent = consumeNBytes(*codeSizeInBytes);
718 auto name = StringAttr::get(func->getContext(),
719 locName.str() +
"::" + func.getSymName());
720 auto cParser = ParserHead{*codeContent, name};
721 FailureOr<uint32_t> localVecSize = cParser.parseVectorSize();
724 OpBuilder builder{&func.getBody().front().back()};
728 uint32_t nVarVec = *localVecSize;
729 for (
size_t i = 0; i < nVarVec; ++i) {
730 FileLineColLoc varLoc = cParser.getLocation();
731 FailureOr<uint32_t> nSubVar = cParser.parseUI32();
734 FailureOr<Type> varT = cParser.parseValueType(func->getContext());
737 for (
size_t j = 0; j < *nSubVar; ++j) {
738 auto local = LocalOp::create(builder, varLoc, *varT);
739 locals.push_back(local.getResult());
742 parsed_inst_t res = cParser.parseExpression(builder, symbols, locals);
747 "unparsed garbage remaining at end of code block");
748 ReturnOp::create(builder, func->getLoc(), *res);
753 llvm::FailureOr<BlockTypeParseResult> parseBlockType(MLIRContext *ctx) {
754 auto loc = getLocation();
755 auto blockIndicator = peek();
756 if (
failed(blockIndicator))
760 return {EmptyBlockMarker{}};
762 if (isValueOneOf(*blockIndicator, valueTypesEncodings))
763 return parseValueType(ctx);
766 auto typeIdx = parseI64();
769 if (*typeIdx < 0 || *typeIdx > std::numeric_limits<uint32_t>::max())
770 return emitError(loc,
"type ID should be representable with an unsigned "
771 "32 bits integer. Got ")
773 return {TypeIdxRecord{
static_cast<uint32_t
>(*typeIdx)}};
776 bool end()
const {
return curHead().empty(); }
778 ParserHead
copy()
const {
return *
this; }
781 StringRef curHead()
const {
return head.drop_front(offset); }
783 FailureOr<std::byte> peek()
const {
787 "trying to peek at next byte, but input stream is empty");
788 return static_cast<std::byte
>(curHead().front());
791 size_t size()
const {
return head.size() - offset; }
795 unsigned anchorOffset{0};
800FailureOr<float> ParserHead::parseLiteral<float>() {
801 FailureOr<StringRef> bytes = consumeNBytes(4);
804 return llvm::support::endian::read<float>(bytes->bytes_begin(),
805 llvm::endianness::little);
809FailureOr<double> ParserHead::parseLiteral<double>() {
810 FailureOr<StringRef> bytes = consumeNBytes(8);
813 return llvm::support::endian::read<double>(bytes->bytes_begin(),
814 llvm::endianness::little);
818FailureOr<uint32_t> ParserHead::parseLiteral<uint32_t>() {
819 char const *error =
nullptr;
821 unsigned encodingSize{0};
822 StringRef src = curHead();
823 uint64_t decoded = llvm::decodeULEB128(src.bytes_begin(), &encodingSize,
824 src.bytes_end(), &error);
828 if (std::isgreater(decoded, std::numeric_limits<uint32_t>::max()))
829 return emitError(getLocation()) <<
"literal does not fit on 32 bits";
831 res =
static_cast<uint32_t
>(decoded);
832 offset += encodingSize;
837FailureOr<int32_t> ParserHead::parseLiteral<int32_t>() {
838 char const *error =
nullptr;
840 unsigned encodingSize{0};
841 StringRef src = curHead();
842 int64_t decoded = llvm::decodeSLEB128(src.bytes_begin(), &encodingSize,
843 src.bytes_end(), &error);
846 if (std::isgreater(decoded, std::numeric_limits<int32_t>::max()) ||
847 std::isgreater(std::numeric_limits<int32_t>::min(), decoded))
848 return emitError(getLocation()) <<
"literal does not fit on 32 bits";
850 res =
static_cast<int32_t
>(decoded);
851 offset += encodingSize;
856FailureOr<int64_t> ParserHead::parseLiteral<int64_t>() {
857 char const *error =
nullptr;
858 unsigned encodingSize{0};
859 StringRef src = curHead();
860 int64_t res = llvm::decodeSLEB128(src.bytes_begin(), &encodingSize,
861 src.bytes_end(), &error);
865 offset += encodingSize;
869FailureOr<uint32_t> ParserHead::parseVectorSize() {
870 return parseLiteral<uint32_t>();
873inline FailureOr<uint32_t> ParserHead::parseUI32() {
874 return parseLiteral<uint32_t>();
877inline FailureOr<int64_t> ParserHead::parseI64() {
878 return parseLiteral<int64_t>();
881#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
882void ValueStack::dump()
const {
883 llvm::dbgs() <<
"================= Wasm ValueStack =======================\n";
884 llvm::dbgs() <<
"size: " << size() <<
"\n";
885 llvm::dbgs() <<
"nbFrames: " << labelLevel.size() <<
'\n';
886 llvm::dbgs() <<
"<Top>"
891 auto indexGetter = [
this]() {
892 size_t idx = labelLevel.size();
893 return [
this, idx]()
mutable -> std::optional<std::pair<size_t, size_t>> {
894 llvm::dbgs() <<
"IDX: " << idx <<
'\n';
897 auto frameId = idx - 1;
898 auto frameLimit = labelLevel[frameId].stackIdx;
900 return {{frameId, frameLimit}};
903 auto getNextFrameIndex = indexGetter();
904 auto nextFrameIdx = getNextFrameIndex();
905 size_t stackSize = size();
906 for (
size_t idx = 0; idx < stackSize; ++idx) {
907 size_t actualIdx = stackSize - 1 - idx;
908 while (nextFrameIdx && (nextFrameIdx->second > actualIdx)) {
909 llvm::dbgs() <<
" --------------- Frame (" << nextFrameIdx->first
911 nextFrameIdx = getNextFrameIndex();
914 values[actualIdx].dump();
916 while (nextFrameIdx) {
917 llvm::dbgs() <<
" --------------- Frame (" << nextFrameIdx->first <<
")\n";
918 nextFrameIdx = getNextFrameIndex();
920 llvm::dbgs() <<
"<Bottom>"
922 llvm::dbgs() <<
"=========================================================\n";
926parsed_inst_t ValueStack::popOperands(
TypeRange operandTypes, Location *opLoc) {
928 values.size() - (labelLevel.empty() ? 0 : labelLevel.back().stackIdx);
929 LDBG() <<
"Popping from ValueStack\n"
930 <<
" Elements to pop: " << operandTypes.size() <<
"\n"
931 <<
" Current stack size: " << values.size() <<
"\n"
932 <<
" Current frame size: " << frameSize;
933 if (operandTypes.size() > frameSize)
934 return emitError(*opLoc,
"operand stack underflow: expected ")
935 << operandTypes.size() <<
", but the current frame contains "
937 size_t stackIdxOffset = values.size() - operandTypes.size();
938 SmallVector<Value> res{};
939 res.reserve(operandTypes.size());
940 for (
size_t i{0}; i < operandTypes.size(); ++i) {
941 Value operand = values[i + stackIdxOffset];
942 Type stackType = operand.
getType();
943 if (stackType != operandTypes[i])
944 return emitError(*opLoc,
"invalid operand type on stack. expecting ")
945 << operandTypes[i] <<
", value on stack is of type " << stackType;
946 LDBG() <<
" POP: " << operand;
947 res.push_back(operand);
949 values.resize(values.size() - operandTypes.size());
950 LDBG() <<
" Updated stack size: " << values.size() <<
"\n"
951 <<
" Updated frame size: " << frameSize - operandTypes.size();
955LogicalResult ValueStack::pushResults(
ValueRange results, Location *opLoc) {
956 LDBG() <<
"Pushing to ValueStack\n"
957 <<
" Elements(s) to push: " << results.size() <<
"\n"
958 <<
" Current stack size: " << values.size();
959 for (Value val : results) {
961 return emitError(*opLoc,
"invalid value type on stack: ")
963 LDBG() <<
" PUSH: " << val;
964 values.push_back(val);
967 LDBG() <<
" Updated stack size: " << values.size();
971template <std::
byte EndParseByte>
972parsed_inst_t ExpressionParser::parse(OpBuilder &builder,
973 UniqueByte<EndParseByte> endByte) {
974 auto res =
parse(builder, ByteSequence<EndParseByte>{});
977 return res->opResults;
980template <std::byte... ExpressionParseEnd>
981FailureOr<ExpressionParser::ParseResultWithInfo>
982ExpressionParser::parse(OpBuilder &builder,
983 ByteSequence<ExpressionParseEnd...> parsingEndFilters) {
984 SmallVector<Value> res;
986 currentOpLoc = parser.getLocation();
987 FailureOr<std::byte> opCode = parser.consumeByte();
990 if (isValueOneOf(*opCode, parsingEndFilters))
991 return {{res, *opCode}};
992 parsed_inst_t resParsed;
993 resParsed = dispatchToInstParser(*opCode, builder);
996 std::swap(res, *resParsed);
997 if (
failed(pushResults(res)))
1002llvm::FailureOr<FunctionType>
1003ExpressionParser::parseBlockFuncType(OpBuilder &builder) {
1004 return getFuncTypeFor(builder, parser.parseBlockType(builder.
getContext()));
1007template <
typename OpToCreate>
1009ExpressionParser::ExprParserProxy::parseBlockLikeOp(OpBuilder &builder) {
1010 auto opLoc = getCurrentOpLoc();
1011 auto funcType = parseBlockFuncType(builder);
1015 auto inputTypes = funcType->getInputs();
1016 auto inputOps = popOperands(inputTypes);
1021 Region *curRegion = curBlock->
getParent();
1022 auto resTypes = funcType->getResults();
1023 llvm::SmallVector<Location> locations{};
1024 locations.resize(resTypes.size(), getCurrentOpLoc());
1026 builder.
createBlock(curRegion, curRegion->
end(), resTypes, locations);
1029 OpToCreate::create(builder, getCurrentOpLoc(), *inputOps, successor);
1030 auto *blockBody = blockOp.createBlock();
1031 if (
failed(parseBlockContent(builder, blockBody, resTypes, opLoc, blockOp)))
1034 return {
ValueRange{successor->getArguments()}};
1037parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::block>,
1039 ExpressionParser::ExprParserProxy &exprParser) {
1040 return exprParser.parseBlockLikeOp<BlockOp>(builder);
1043parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::loop>,
1045 ExpressionParser::ExprParserProxy &exprParser) {
1046 return exprParser.parseBlockLikeOp<LoopOp>(builder);
1049parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::ifOpCode>,
1051 ExpressionParser::ExprParserProxy &exprParser) {
1052 auto opLoc = exprParser.getCurrentOpLoc();
1053 auto funcType = exprParser.parseBlockFuncType(builder);
1057 LDBG() <<
"Parsing an if instruction of type " << *funcType;
1058 auto inputTypes = funcType->getInputs();
1059 auto conditionValue = exprParser.popOperands(builder.
getI32Type());
1060 if (
failed(conditionValue))
1062 auto inputOps = exprParser.popOperands(inputTypes);
1067 Region *curRegion = curBlock->
getParent();
1068 auto resTypes = funcType->getResults();
1069 llvm::SmallVector<Location> locations{};
1070 locations.resize(resTypes.size(), exprParser.getCurrentOpLoc());
1072 builder.
createBlock(curRegion, curRegion->
end(), resTypes, locations);
1074 auto ifOp = IfOp::create(builder, exprParser.getCurrentOpLoc(),
1075 conditionValue->front(), *inputOps, successor);
1076 auto *ifEntryBlock = ifOp.createIfBlock();
1077 constexpr auto ifElseFilter =
1080 auto parseIfRes = exprParser.parseBlockContent(
1081 builder, ifEntryBlock, resTypes, opLoc, ifOp, ifElseFilter);
1085 LDBG() <<
" else block is present.";
1086 Block *elseEntryBlock = ifOp.createElseBlock();
1087 auto parseElseRes = exprParser.parseBlockContent(builder, elseEntryBlock,
1088 resTypes, opLoc, ifOp);
1089 if (
failed(parseElseRes))
1093 return {
ValueRange{successor->getArguments()}};
1096parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::branchIf>,
1098 ExpressionParser::ExprParserProxy &exprParser) {
1099 auto level = exprParser.parser().parseLiteral<uint32_t>();
1103 Region *curRegion = curBlock->
getParent();
1106 auto condition = exprParser.popOperands(builder.
getI32Type());
1111 LabelBranchingOpInterface::getTargetOpFromBlock(curBlock, *level);
1114 auto inputTypes = targetOp->getLabelTarget()->getArgumentTypes();
1115 auto branchArgs = exprParser.popOperands(inputTypes);
1118 BranchIfOp::create(builder, exprParser.getCurrentOpLoc(), condition->front(),
1122 return {*branchArgs};
1125parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::call>,
1127 ExpressionParser::ExprParserProxy &exprParser) {
1128 auto loc = exprParser.getCurrentOpLoc();
1129 auto funcIdx = exprParser.parser().parseLiteral<uint32_t>();
1132 if (*funcIdx >= exprParser.symbols().funcSymbols.size())
1133 return emitError(loc,
"Invalid function index: ") << *funcIdx;
1134 auto callee = exprParser.symbols().funcSymbols[*funcIdx];
1135 llvm::ArrayRef<Type> inTypes = callee.functionType.getInputs();
1136 llvm::ArrayRef<Type> resTypes = callee.functionType.getResults();
1137 parsed_inst_t inOperands = exprParser.popOperands(inTypes);
1141 FuncCallOp::create(builder, loc, resTypes, callee.symbol, *inOperands);
1142 return {callOp.getResults()};
1145parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localGet>,
1147 ExpressionParser::ExprParserProxy &exprParser) {
1148 FailureOr<uint32_t>
id = exprParser.parser().parseLiteral<uint32_t>();
1149 Location instLoc = exprParser.getCurrentOpLoc();
1152 if (*
id >= exprParser.locals().size())
1153 return emitError(instLoc,
"invalid local index. function has ")
1154 << exprParser.locals().size()
1155 <<
" accessible locals, received index " << *id;
1156 return {{LocalGetOp::create(builder, instLoc, exprParser.locals()[*
id])
1160parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::globalGet>,
1162 ExpressionParser::ExprParserProxy &exprParser) {
1163 FailureOr<uint32_t>
id = exprParser.parser().parseLiteral<uint32_t>();
1164 Location instLoc = exprParser.getCurrentOpLoc();
1167 if (*
id >= exprParser.symbols().globalSymbols.size())
1168 return emitError(instLoc,
"invalid global index. function has ")
1169 << exprParser.symbols().globalSymbols.size()
1170 <<
" accessible globals, received index " << *id;
1171 GlobalSymbolRefContainer globalVar = exprParser.symbols().globalSymbols[*id];
1172 auto globalOp = GlobalGetOp::create(builder, instLoc, globalVar.globalType,
1175 return {{globalOp.getResult()}};
1178template <
typename OpToCreate>
1179parsed_inst_t ExpressionParser::parseSetOrTee(OpBuilder &builder) {
1180 FailureOr<uint32_t>
id = parser.parseLiteral<uint32_t>();
1183 if (*
id >= locals.size())
1184 return emitError(*currentOpLoc,
"invalid local index. function has ")
1185 << locals.size() <<
" accessible locals, received index " << *id;
1186 if (valueStack.empty())
1189 "invalid stack access, trying to access a value on an empty stack");
1195 OpToCreate::create(builder, *currentOpLoc, locals[*
id], poppedOp->front())
1199parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localSet>,
1201 ExpressionParser::ExprParserProxy &exprParser) {
1202 return exprParser.parseSetOrTee<LocalSetOp>(builder);
1205parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localTee>,
1207 ExpressionParser::ExprParserProxy &exprParser) {
1208 return exprParser.parseSetOrTee<LocalTeeOp>(builder);
1211template <
typename T>
1212inline Type buildLiteralType(OpBuilder &);
1215inline Type buildLiteralType<int32_t>(OpBuilder &builder) {
1220inline Type buildLiteralType<int64_t>(OpBuilder &builder) {
1225[[maybe_unused]]
inline Type buildLiteralType<uint32_t>(OpBuilder &builder) {
1230[[maybe_unused]]
inline Type buildLiteralType<uint64_t>(OpBuilder &builder) {
1235inline Type buildLiteralType<float>(OpBuilder &builder) {
1240inline Type buildLiteralType<double>(OpBuilder &builder) {
1244template <
typename ValT,
1245 typename E = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1248template <
typename ValT>
1249struct AttrHolder<ValT, std::enable_if_t<std::is_integral_v<ValT>>> {
1250 using type = IntegerAttr;
1253template <
typename ValT>
1254struct AttrHolder<ValT, std::enable_if_t<std::is_floating_point_v<ValT>>> {
1255 using type = FloatAttr;
1258template <
typename ValT>
1259using attr_holder_t =
typename AttrHolder<ValT>::type;
1261template <
typename ValT,
1262 typename EnableT = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1263attr_holder_t<ValT> buildLiteralAttr(OpBuilder &builder, ValT val) {
1264 return attr_holder_t<ValT>::get(buildLiteralType<ValT>(builder), val);
1267template <
typename valueT>
1268parsed_inst_t ExpressionParser::ExprParserProxy::parseConstInst(
1269 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueT>> *) {
1270 auto parsedConstant = parser().parseLiteral<valueT>();
1271 if (
failed(parsedConstant))
1274 ConstOp::create(builder, getCurrentOpLoc(),
1275 buildLiteralAttr<valueT>(builder, *parsedConstant));
1276 return {{constOp.getResult()}};
1279parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constI32>,
1281 ExpressionParser::ExprParserProxy &exprParser) {
1282 return exprParser.parseConstInst<int32_t>(builder);
1285parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constI64>,
1287 ExpressionParser::ExprParserProxy &exprParser) {
1288 return exprParser.parseConstInst<int64_t>(builder);
1291parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constFP32>,
1293 ExpressionParser::ExprParserProxy &exprParser) {
1294 return exprParser.parseConstInst<
float>(builder);
1297parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constFP64>,
1299 ExpressionParser::ExprParserProxy &exprParser) {
1300 return exprParser.parseConstInst<
double>(builder);
1303template <
typename opcode,
typename valueType,
unsigned int numOperands>
1304inline parsed_inst_t ExpressionParser::ExprParserProxy::buildNumericOp(
1305 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueType>> *) {
1306 auto ty = buildLiteralType<valueType>(builder);
1307 LDBG() <<
"*** buildNumericOp: numOperands = " << numOperands
1308 <<
", type = " << ty <<
" ***";
1309 auto tysToPop = SmallVector<Type, numOperands>();
1310 tysToPop.resize(numOperands);
1311 llvm::fill(tysToPop, ty);
1312 auto operands = popOperands(tysToPop);
1315 auto op = opcode::create(builder, getCurrentOpLoc(), *operands).getResult();
1316 LDBG() <<
"Built operation: " << op;
1321#define BUILD_NUMERIC_OP(OP_NAME, N_ARGS, PREFIX, SUFFIX, TYPE) \
1322 inline parsed_inst_t parse( \
1323 OpCode<WasmBinaryEncoding::OpCode::PREFIX##SUFFIX>, OpBuilder &builder, \
1324 ExpressionParser::ExprParserProxy &exprParser) { \
1325 return exprParser.buildNumericOp<OP_NAME, TYPE, N_ARGS>(builder); \
1329#define BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1330 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I32, int32_t) \
1331 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I64, int64_t)
1334#define BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX) \
1335 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F32, float) \
1336 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F64, double)
1339#define BUILD_NUMERIC_BINOP_INTFP(OP_NAME, PREFIX) \
1340 BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1341 BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX)
1344#define BUILD_NUMERIC_UNARY_OP_INT(OP_NAME, PREFIX) \
1345 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I32, int32_t) \
1346 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I64, int64_t)
1349#define BUILD_NUMERIC_UNARY_OP_FP(OP_NAME, PREFIX) \
1350 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F32, float) \
1351 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F64, double)
1399#undef BUILD_NUMERIC_BINOP_FP
1400#undef BUILD_NUMERIC_BINOP_INT
1401#undef BUILD_NUMERIC_BINOP_INTFP
1402#undef BUILD_NUMERIC_UNARY_OP_FP
1403#undef BUILD_NUMERIC_UNARY_OP_INT
1404#undef BUILD_NUMERIC_OP
1405#undef BUILD_NUMERIC_CAST_OP
1407template <
typename opType,
typename inputType,
typename outputType,
1408 typename... extraArgsT>
1410ExpressionParser::ExprParserProxy::buildConvertOp(
OpBuilder &builder,
1411 extraArgsT... extraArgs) {
1412 static_assert(std::is_arithmetic_v<inputType>,
1413 "InputType should be an arithmetic type");
1414 static_assert(std::is_arithmetic_v<outputType>,
1415 "OutputType should be an arithmetic type");
1416 auto intype = buildLiteralType<inputType>(builder);
1417 auto outType = buildLiteralType<outputType>(builder);
1418 auto operand = popOperands(intype);
1419 if (failed(operand))
1421 auto op = opType::create(builder, getCurrentOpLoc(), outType,
1422 operand->front(), extraArgs...);
1423 LDBG() <<
"Built operation: " << op;
1424 return {{op.getResult()}};
1427parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::demoteF64ToF32>,
1429 ExpressionParser::ExprParserProxy &exprParser) {
1430 return exprParser.buildConvertOp<DemoteOp, double,
float>(builder);
1433parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::wrap>,
1435 ExpressionParser::ExprParserProxy &exprParser) {
1436 return exprParser.buildConvertOp<WrapOp, int64_t, int32_t>(builder);
1439#define BUILD_CONVERSION_OP(IN_T, OUT_T, SOURCE_OP, TARGET_OP) \
1440 inline parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::SOURCE_OP>, \
1441 OpBuilder &builder, \
1442 ExpressionParser::ExprParserProxy &exprParser) { \
1443 return exprParser.buildConvertOp<TARGET_OP, IN_T, OUT_T>(builder); \
1446#define BUILD_CONVERT_OP_FOR(DEST_T, WIDTH) \
1447 BUILD_CONVERSION_OP(uint32_t, DEST_T, convertUI32F##WIDTH, ConvertUOp) \
1448 BUILD_CONVERSION_OP(int32_t, DEST_T, convertSI32F##WIDTH, ConvertSOp) \
1449 BUILD_CONVERSION_OP(uint64_t, DEST_T, convertUI64F##WIDTH, ConvertUOp) \
1450 BUILD_CONVERSION_OP(int64_t, DEST_T, convertSI64F##WIDTH, ConvertSOp)
1455#undef BUILD_CONVERT_OP_FOR
1457#define BUILD_TRUNC_OP_FOR(SRC_T, WIDTH) \
1458 BUILD_CONVERSION_OP(SRC_T, int32_t, truncSI32F##WIDTH, TruncSIOp) \
1459 BUILD_CONVERSION_OP(SRC_T, uint32_t, truncUI32F##WIDTH, TruncUIOp) \
1460 BUILD_CONVERSION_OP(SRC_T, int64_t, truncSI64F##WIDTH, TruncSIOp) \
1461 BUILD_CONVERSION_OP(SRC_T, uint64_t, truncUI64F##WIDTH, TruncUIOp)
1466#undef BUILD_TRUNC_OP_FOR
1471#undef BUILD_CONVERSION_OP
1473parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::saturatedTruncate>,
1475 ExpressionParser::ExprParserProxy &parser,
1476 std::uint32_t subOpCode) {
1478 return emitError(parser.getCurrentOpLoc())
1479 <<
"invalid sub-opcode for trunc_saturate: " << subOpCode;
1480 LDBG() <<
"Sub subOpcode for operation: " << subOpCode;
1481 bool isDestUnsigned = (subOpCode & 1) != 0;
1482 bool isSrcF64 = (subOpCode & 2) != 0;
1483 bool isDestI64 = (subOpCode & 4) != 0;
1487 auto srcOp = parser.popOperands(srcType);
1491 ? TruncSatUIOp::create(builder, parser.getCurrentOpLoc(),
1492 destType, srcOp->front())
1493 : TruncSatSIOp::create(builder, parser.getCurrentOpLoc(),
1494 destType, srcOp->front());
1495 LDBG() <<
"Built operation: " << op;
1499#define BUILD_SLICE_EXTEND_PARSER(IT_WIDTH, EXTRACT_WIDTH) \
1500 parsed_inst_t parse( \
1501 OpCode<WasmBinaryEncoding::OpCode::extendI##IT_WIDTH##EXTRACT_WIDTH##S>, \
1502 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1503 using inout_t = int##IT_WIDTH##_t; \
1504 auto attr = builder.getUI32IntegerAttr(EXTRACT_WIDTH); \
1505 return exprParser.buildConvertOp<ExtendLowBitsSOp, inout_t, inout_t>( \
1515#undef BUILD_SLICE_EXTEND_PARSER
1517parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::promoteF32ToF64>,
1519 ExpressionParser::ExprParserProxy &exprParser) {
1520 return exprParser.buildConvertOp<PromoteOp, float,
double>(builder);
1523#define BUILD_REINTERPRET_PARSER(WIDTH, FP_TYPE) \
1524 inline parsed_inst_t parse( \
1525 OpCode<WasmBinaryEncoding::OpCode::reinterpretF##WIDTH##AsI##WIDTH>, \
1526 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1527 return exprParser.buildConvertOp<ReinterpretOp, FP_TYPE, int##WIDTH##_t>( \
1531 inline parsed_inst_t parse( \
1532 OpCode<WasmBinaryEncoding::OpCode::reinterpretI##WIDTH##AsF##WIDTH>, \
1533 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1534 return exprParser.buildConvertOp<ReinterpretOp, int##WIDTH##_t, FP_TYPE>( \
1541#undef BUILD_REINTERPRET_PARSER
1543class InstDispatcher {
1545 template <std::
byte OpCode,
typename =
void>
1546 struct HasParserRegistered : std::false_type {};
1548 template <std::
byte opCode>
1549 struct HasParserRegistered<
1550 opCode, std::void_t<decltype(parse(
1551 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1552 std::declval<ExpressionParser::ExprParserProxy &>()))>>
1553 : std::true_type {};
1555 template <std::
byte opCode>
1556 static constexpr bool hasParseredRegistered =
1557 HasParserRegistered<opCode>::value;
1559 template <std::
byte OpCode,
typename =
void>
1560 struct HasParserWthSubOpCodeRegistered : std::false_type {};
1562 template <std::
byte opCode>
1563 struct HasParserWthSubOpCodeRegistered<
1564 opCode, std::void_t<decltype(parse(
1565 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1566 std::declval<ExpressionParser::ExprParserProxy &>(),
1567 std::declval<std::uint32_t>()))>> : std::true_type {
1568 static_assert(!hasParseredRegistered<opCode>,
1569 "plain parser and parser with sub-opcode can't be registered "
1570 "for the same opcode");
1573 template <std::
byte opCode>
1574 static constexpr bool hasParseredWithSubOpCodeRegistered =
1575 HasParserWthSubOpCodeRegistered<opCode>::value;
1577 using dispatch_t = parsed_inst_t (*)(
OpBuilder &,
1578 ExpressionParser::ExprParserProxy &);
1580 static inline parsed_inst_t
1582 ExpressionParser::ExprParserProxy &expressionParser) {
1583 llvm_unreachable(
"Failure in opcode parser dispatch logic.");
1584 return mlir::failure();
1587 template <std::
byte opCode>
1588 static parsed_inst_t
1590 ExpressionParser::ExprParserProxy &exprParser) {
1591 return parse(OpCode<opCode>{}, builder, exprParser);
1594 template <std::
byte opCode>
1595 static parsed_inst_t
1596 forwardToParserWithSubOpCode(
OpBuilder &builder,
1597 ExpressionParser::ExprParserProxy &exprParser) {
1598 auto loc = exprParser.getCurrentOpLoc();
1599 auto subOpCode = exprParser.parser().parseLiteral<std::uint32_t>();
1600 if (failed(subOpCode))
1601 return emitError(loc) <<
"expecting sub opcode for opcode "
1602 <<
static_cast<uint8_t
>(opCode);
1603 return parse(OpCode<opCode>{}, builder, exprParser, subOpCode.value());
1606 template <std::
byte opCode>
1607 static constexpr dispatch_t getHandlerForOpCode() {
1608 if constexpr (hasParseredRegistered<opCode>)
1609 return forwardToParser<opCode>;
1610 else if constexpr (hasParseredWithSubOpCodeRegistered<opCode>)
1611 return forwardToParserWithSubOpCode<opCode>;
1613 return unreachableHandler;
1617 static inline parsed_inst_t
1619 ExpressionParser::ExprParserProxy &expressionParser,
1621 return emitError(expressionParser.getCurrentOpLoc(),
1622 "unknown instruction opcode: ")
1623 <<
static_cast<int>(opCode);
1626 template <std::byte... opCodes>
1627 static inline parsed_inst_t
1628 dispatchImpl(std::byte opCode,
OpBuilder &builder,
1629 ExpressionParser::ExprParserProxy &exprParser,
1630 ByteSequence<opCodes...>) {
1631 static constexpr std::array<bool, 256> opcodeValidityMap{
1632 (hasParseredRegistered<opCodes> ||
1633 hasParseredWithSubOpCodeRegistered<opCodes>)...};
1634 static constexpr std::array<dispatch_t, 256> dispatchTable{
1635 getHandlerForOpCode<opCodes>()...};
1636 if (opcodeValidityMap[
static_cast<size_t>(opCode)]) {
1637 return dispatchTable[
static_cast<size_t>(opCode)](builder, exprParser);
1639 return invalidOpcodeDiag(builder, exprParser, opCode);
1653 static parsed_inst_t dispatch(std::byte opCode,
OpBuilder &builder,
1654 ExpressionParser::ExprParserProxy &exprParser) {
1655 return dispatchImpl(opCode, builder, exprParser, all8bitsBytes);
1660ExpressionParser::dispatchToInstParser(std::byte opCode, OpBuilder &builder) {
1661 ExpressionParser::ExprParserProxy exprParser{*
this};
1662 return InstDispatcher::dispatch(opCode, builder, exprParser);
1664class WasmBinaryParser {
1666 struct SectionRegistry {
1667 using section_location_t = StringRef;
1669 std::array<SmallVector<section_location_t>, highestWasmSectionID + 1>
1672 template <WasmSectionType SecType>
1673 std::conditional_t<sectionShouldBeUnique(SecType),
1674 std::optional<section_location_t>,
1675 ArrayRef<section_location_t>>
1676 getContentForSection()
const {
1677 constexpr auto idx =
static_cast<size_t>(SecType);
1678 if constexpr (sectionShouldBeUnique(SecType)) {
1679 return registry[idx].empty() ? std::nullopt
1680 : std::make_optional(registry[idx][0]);
1682 return registry[idx];
1686 bool hasSection(WasmSectionType secType)
const {
1687 return !registry[
static_cast<size_t>(secType)].empty();
1695 LogicalResult registerSection(WasmSectionType secType,
1696 section_location_t location, Location loc) {
1697 if (sectionShouldBeUnique(secType) && hasSection(secType))
1699 "trying to add a second instance of unique section");
1701 registry[
static_cast<size_t>(secType)].
push_back(location);
1702 emitRemark(loc,
"Adding section with section ID ")
1703 <<
static_cast<uint8_t
>(secType);
1707 LogicalResult populateFromBody(ParserHead ph) {
1709 FileLineColLoc sectionLoc = ph.getLocation();
1710 FailureOr<WasmSectionType> secType = ph.parseWasmSectionType();
1714 FailureOr<uint32_t> secSizeParsed = ph.parseLiteral<uint32_t>();
1715 if (
failed(secSizeParsed))
1718 uint32_t secSize = *secSizeParsed;
1719 FailureOr<StringRef> sectionContent = ph.consumeNBytes(secSize);
1720 if (
failed(sectionContent))
1723 LogicalResult registration =
1724 registerSection(*secType, *sectionContent, sectionLoc);
1726 if (
failed(registration))
1733 auto getLocation(
int offset = 0)
const {
1737 template <WasmSectionType>
1738 LogicalResult parseSectionItem(ParserHead &,
size_t);
1740 template <WasmSectionType section>
1741 LogicalResult parseSection() {
1742 auto secName = std::string{wasmSectionName<section>};
1743 auto sectionNameAttr =
1744 StringAttr::get(ctx, srcName.strref() +
":" + secName +
"-SECTION");
1745 unsigned offset = 0;
1746 auto getLocation = [sectionNameAttr, &offset]() {
1749 auto secContent = registry.getContentForSection<section>();
1751 LDBG() << secName <<
" section is not present in file.";
1755 auto secSrc = secContent.value();
1756 ParserHead ph{secSrc, sectionNameAttr};
1757 FailureOr<uint32_t> nElemsParsed = ph.parseVectorSize();
1758 if (
failed(nElemsParsed))
1760 uint32_t nElems = *nElemsParsed;
1761 LDBG() <<
"starting to parse " << nElems <<
" items for section "
1763 for (
size_t i = 0; i < nElems; ++i) {
1764 if (
failed(parseSectionItem<section>(ph, i)))
1769 return emitError(getLocation(),
"unparsed garbage at end of section ")
1775 LogicalResult visitImport(Location loc, StringRef moduleName,
1776 StringRef importName, TypeIdxRecord tid) {
1778 if (tid.id >= symbols.moduleFuncTypes.size())
1779 return emitError(loc,
"invalid type id: ")
1780 << tid.id <<
". Only " << symbols.moduleFuncTypes.size()
1781 <<
" type registrations";
1782 FunctionType type = symbols.moduleFuncTypes[tid.id];
1783 std::string symbol = symbols.getNewFuncSymbolName();
1784 auto funcOp = FuncImportOp::create(builder, loc, symbol, moduleName,
1787 return funcOp.verify();
1791 LogicalResult visitImport(Location loc, StringRef moduleName,
1792 StringRef importName, LimitType limitType) {
1793 std::string symbol = symbols.getNewMemorySymbolName();
1794 auto memOp = MemImportOp::create(builder, loc, symbol, moduleName,
1795 importName, limitType);
1797 return memOp.verify();
1801 LogicalResult visitImport(Location loc, StringRef moduleName,
1802 StringRef importName, TableType tableType) {
1803 std::string symbol = symbols.getNewTableSymbolName();
1804 auto tableOp = TableImportOp::create(builder, loc, symbol, moduleName,
1805 importName, tableType);
1807 return tableOp.verify();
1811 LogicalResult visitImport(Location loc, StringRef moduleName,
1812 StringRef importName, GlobalTypeRecord globalType) {
1813 std::string symbol = symbols.getNewGlobalSymbolName();
1815 GlobalImportOp::create(builder, loc, symbol, moduleName, importName,
1816 globalType.type, globalType.isMutable);
1817 symbols.globalSymbols.push_back(
1819 return giOp.verify();
1823 LogicalResult peekDiag(Diagnostic &diag) {
1824 if (diag.
getSeverity() == DiagnosticSeverity::Error)
1830 WasmBinaryParser(llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
1831 : builder{ctx}, ctx{ctx} {
1833 [
this](Diagnostic &diag) {
return peekDiag(diag); });
1835 if (sourceMgr.getNumBuffers() != 1) {
1836 emitError(UnknownLoc::get(ctx),
"one source file should be provided");
1839 uint32_t sourceBufId = sourceMgr.getMainFileID();
1840 StringRef source = sourceMgr.getMemoryBuffer(sourceBufId)->getBuffer();
1841 srcName = StringAttr::get(
1842 ctx, sourceMgr.getMemoryBuffer(sourceBufId)->getBufferIdentifier());
1844 auto parser = ParserHead{source, srcName};
1845 auto const wasmHeader = StringRef{
"\0asm", 4};
1846 FileLineColLoc magicLoc = parser.getLocation();
1847 FailureOr<StringRef> magic = parser.consumeNBytes(wasmHeader.size());
1848 if (
failed(magic) || magic->compare(wasmHeader)) {
1849 emitError(magicLoc,
"source file does not contain valid Wasm header");
1852 auto const expectedVersionString = StringRef{
"\1\0\0\0", 4};
1853 FileLineColLoc versionLoc = parser.getLocation();
1854 FailureOr<StringRef> version =
1855 parser.consumeNBytes(expectedVersionString.size());
1858 if (version->compare(expectedVersionString)) {
1860 "unsupported Wasm version. only version 1 is supported");
1863 LogicalResult fillRegistry = registry.populateFromBody(parser.copy());
1864 if (
failed(fillRegistry))
1867 mOp = ModuleOp::create(builder, getLocation());
1869 LogicalResult parsingTypes = parseSection<WasmSectionType::TYPE>();
1870 if (
failed(parsingTypes))
1873 LogicalResult parsingImports = parseSection<WasmSectionType::IMPORT>();
1874 if (
failed(parsingImports))
1877 firstInternalFuncID = symbols.funcSymbols.size();
1879 LogicalResult parsingFunctions = parseSection<WasmSectionType::FUNCTION>();
1880 if (
failed(parsingFunctions))
1883 LogicalResult parsingTables = parseSection<WasmSectionType::TABLE>();
1884 if (
failed(parsingTables))
1887 LogicalResult parsingMems = parseSection<WasmSectionType::MEMORY>();
1891 LogicalResult parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
1892 if (
failed(parsingGlobals))
1895 LogicalResult parsingCode = parseSection<WasmSectionType::CODE>();
1899 LogicalResult parsingExports = parseSection<WasmSectionType::EXPORT>();
1900 if (
failed(parsingExports))
1904 LDBG() <<
"WASM Imports:"
1906 <<
" - Num functions: " << symbols.funcSymbols.size() <<
"\n"
1907 <<
" - Num globals: " << symbols.globalSymbols.size() <<
"\n"
1908 <<
" - Num memories: " << symbols.memSymbols.size() <<
"\n"
1909 <<
" - Num tables: " << symbols.tableSymbols.size();
1912 ModuleOp getModule() {
1921 mlir::StringAttr srcName;
1923 WasmModuleSymbolTables symbols;
1926 SectionRegistry registry;
1927 size_t firstInternalFuncID{0};
1933WasmBinaryParser::parseSectionItem<WasmSectionType::IMPORT>(ParserHead &ph,
1935 FileLineColLoc importLoc = ph.getLocation();
1936 auto moduleName = ph.parseName();
1940 auto importName = ph.parseName();
1944 FailureOr<ImportDesc>
import = ph.parseImportDesc(ctx);
1949 [
this, importLoc, &moduleName, &importName](
auto import) {
1950 return visitImport(importLoc, *moduleName, *importName,
import);
1957WasmBinaryParser::parseSectionItem<WasmSectionType::EXPORT>(ParserHead &ph,
1959 FileLineColLoc exportLoc = ph.getLocation();
1961 auto exportName = ph.parseName();
1965 FailureOr<std::byte> opcode = ph.consumeByte();
1969 FailureOr<uint32_t> idx = ph.parseLiteral<uint32_t>();
1973 using SymbolRefDesc = std::variant<SmallVector<SymbolRefContainer>,
1974 SmallVector<GlobalSymbolRefContainer>,
1975 SmallVector<FunctionSymbolRefContainer>>;
1977 SymbolRefDesc currentSymbolList;
1978 std::string symbolType =
"";
1981 symbolType =
"function";
1982 currentSymbolList = symbols.funcSymbols;
1985 symbolType =
"table";
1986 currentSymbolList = symbols.tableSymbols;
1989 symbolType =
"memory";
1990 currentSymbolList = symbols.memSymbols;
1993 symbolType =
"global";
1994 currentSymbolList = symbols.globalSymbols;
1997 return emitError(exportLoc,
"invalid value for export type: ")
1998 << std::to_integer<unsigned>(*opcode);
2001 auto currentSymbol = std::visit(
2002 [&](
const auto &list) -> FailureOr<FlatSymbolRefAttr> {
2003 if (*idx > list.size()) {
2007 "trying to export {0} {1} which is undefined in this scope",
2011 return list[*idx].symbol;
2015 if (
failed(currentSymbol))
2021 return SymbolTable{mOp}.rename(symName, *exportName);
2026WasmBinaryParser::parseSectionItem<WasmSectionType::TABLE>(ParserHead &ph,
2028 FileLineColLoc opLocation = ph.getLocation();
2029 FailureOr<TableType> tableType = ph.parseTableType(ctx);
2032 LDBG() <<
" Parsed table description: " << *tableType;
2033 StringAttr symbol = builder.
getStringAttr(symbols.getNewTableSymbolName());
2035 TableOp::create(builder, opLocation, symbol.strref(), *tableType);
2036 symbols.tableSymbols.push_back({SymbolRefAttr::get(tableOp)});
2042WasmBinaryParser::parseSectionItem<WasmSectionType::FUNCTION>(ParserHead &ph,
2044 FileLineColLoc opLoc = ph.getLocation();
2045 auto typeIdxParsed = ph.parseLiteral<uint32_t>();
2046 if (
failed(typeIdxParsed))
2048 uint32_t typeIdx = *typeIdxParsed;
2049 if (typeIdx >= symbols.moduleFuncTypes.size())
2050 return emitError(getLocation(),
"invalid type index: ") << typeIdx;
2051 std::string symbol = symbols.getNewFuncSymbolName();
2053 FuncOp::create(builder, opLoc, symbol, symbols.moduleFuncTypes[typeIdx]);
2054 Block *block = funcOp.addEntryBlock();
2055 OpBuilder::InsertionGuard guard{builder};
2057 ReturnOp::create(builder, opLoc);
2058 symbols.funcSymbols.push_back(
2060 symbols.moduleFuncTypes[typeIdx]});
2061 return funcOp.verify();
2066WasmBinaryParser::parseSectionItem<WasmSectionType::TYPE>(ParserHead &ph,
2068 FailureOr<FunctionType> funcType = ph.parseFunctionType(ctx);
2071 LDBG() <<
"Parsed function type " << *funcType;
2072 symbols.moduleFuncTypes.push_back(*funcType);
2078WasmBinaryParser::parseSectionItem<WasmSectionType::MEMORY>(ParserHead &ph,
2080 FileLineColLoc opLocation = ph.getLocation();
2081 FailureOr<LimitType> memory = ph.parseLimit(ctx);
2085 LDBG() <<
" Registering memory " << *memory;
2086 std::string symbol = symbols.getNewMemorySymbolName();
2087 auto memOp = MemOp::create(builder, opLocation, symbol, *memory);
2088 symbols.memSymbols.push_back({SymbolRefAttr::get(memOp)});
2094WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
2096 FileLineColLoc globalLocation = ph.getLocation();
2097 auto globalTypeParsed = ph.parseGlobalType(ctx);
2098 if (
failed(globalTypeParsed))
2101 GlobalTypeRecord globalType = *globalTypeParsed;
2102 auto symbol = builder.
getStringAttr(symbols.getNewGlobalSymbolName());
2103 auto globalOp = wasmssa::GlobalOp::create(
2104 builder, globalLocation, symbol, globalType.type, globalType.isMutable);
2105 symbols.globalSymbols.push_back(
2107 OpBuilder::InsertionGuard guard{builder};
2110 parsed_inst_t expr = ph.parseExpression(builder, symbols);
2114 return emitError(globalLocation,
"global with empty initializer");
2115 if (expr->size() != 1 && (*expr)[0].getType() != globalType.type)
2118 "initializer result type does not match global declaration type");
2119 ReturnOp::create(builder, globalLocation, *expr);
2124LogicalResult WasmBinaryParser::parseSectionItem<WasmSectionType::CODE>(
2125 ParserHead &ph,
size_t innerFunctionId) {
2126 unsigned long funcId = innerFunctionId + firstInternalFuncID;
2127 FunctionSymbolRefContainer symRef = symbols.funcSymbols[funcId];
2131 if (
failed(ph.parseCodeFor(funcOp, symbols)))
2137namespace mlir::wasm {
2140 WasmBinaryParser wBN{source, context};
2141 ModuleOp mOp = wBN.getModule();
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
memberIdxs push_back(ArrayAttr::get(parser.getContext(), values))
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
#define BUILD_NUMERIC_BINOP_INTFP(OP_NAME, PREFIX)
#define BUILD_CONVERT_OP_FOR(DEST_T, WIDTH)
#define APPLY_WASM_SEC_TRANSFORM
#define BUILD_REINTERPRET_PARSER(WIDTH, FP_TYPE)
#define BUILD_TRUNC_OP_FOR(SRC_T, WIDTH)
#define BUILD_CONVERSION_OP(IN_T, OUT_T, SOURCE_OP, TARGET_OP)
#define BUILD_SLICE_EXTEND_PARSER(IT_WIDTH, EXTRACT_WIDTH)
#define BUILD_NUMERIC_UNARY_OP_INT(OP_NAME, PREFIX)
#define BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX)
#define BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX)
#define BUILD_NUMERIC_UNARY_OP_FP(OP_NAME, PREFIX)
ValueTypeRange< BlockArgListType > getArgumentTypes()
Return a range containing the types of the arguments for this block.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
OpListType & getOperations()
BlockArgListType getArguments()
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
StringAttr getStringAttr(const Twine &bytes)
MLIRContext * getContext() const
IntegerAttr getUI32IntegerAttr(uint32_t value)
HandlerID registerHandler(HandlerTy handler)
Register a new handler for diagnostics to the engine.
DiagnosticSeverity getSeverity() const
Returns the severity of this diagnostic.
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
MLIRContext is the top-level object for a collection of MLIR operations.
DiagnosticEngine & getDiagEngine()
Returns the diagnostic engine for this context.
void loadAllAvailableDialects()
Load all dialects available in the registry in this context.
This class helps build Operations.
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Block * getBlock() const
Returns the current block of the builder.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
MLIRContext * getContext()
Return the context this operation is associated with.
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
static StringAttr getSymbolName(Operation *symbol)
Returns the name of the given symbol operation, aborting if no symbol is present.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Type getType() const
Return the type of this value.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
OwningOpRef< ModuleOp > importWebAssemblyToModule(llvm::SourceMgr &source, MLIRContext *context)
If source contains a valid Wasm binary file, this function returns a a ModuleOp containing the repres...
bool isWasmValueType(::mlir::Type type)
bool isWasmRefType(::mlir::Type type)
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
InFlightDiagnostic emitRemark(Location loc)
Utility method to emit a remark message using this location.
static constexpr std::byte memory
static constexpr std::byte table
static constexpr std::byte global
static constexpr std::byte function
static constexpr std::byte memType
static constexpr std::byte typeID
static constexpr std::byte tableType
static constexpr std::byte globalType
static constexpr std::byte elseOpCode
static constexpr std::byte externRef
static constexpr std::byte i32
static constexpr std::byte funcType
static constexpr std::byte i64
static constexpr std::byte emptyBlockType
static constexpr std::byte funcRef
static constexpr std::byte v128
static constexpr std::byte f64
static constexpr std::byte f32
static constexpr std::byte endByte