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) {
927 LDBG() <<
"Popping from ValueStack\n"
928 <<
" Elements(s) to pop: " << operandTypes.size() <<
"\n"
929 <<
" Current stack size: " << values.size();
930 if (operandTypes.size() > values.size())
932 "stack doesn't contain enough values. trying to get ")
933 << operandTypes.size() <<
" operands on a stack containing only "
934 << values.size() <<
" values";
935 size_t stackIdxOffset = values.size() - operandTypes.size();
936 SmallVector<Value> res{};
937 res.reserve(operandTypes.size());
938 for (
size_t i{0}; i < operandTypes.size(); ++i) {
939 Value operand = values[i + stackIdxOffset];
940 Type stackType = operand.
getType();
941 if (stackType != operandTypes[i])
942 return emitError(*opLoc,
"invalid operand type on stack. expecting ")
943 << operandTypes[i] <<
", value on stack is of type " << stackType;
944 LDBG() <<
" POP: " << operand;
945 res.push_back(operand);
947 values.resize(values.size() - operandTypes.size());
948 LDBG() <<
" Updated stack size: " << values.size();
952LogicalResult ValueStack::pushResults(
ValueRange results, Location *opLoc) {
953 LDBG() <<
"Pushing to ValueStack\n"
954 <<
" Elements(s) to push: " << results.size() <<
"\n"
955 <<
" Current stack size: " << values.size();
956 for (Value val : results) {
958 return emitError(*opLoc,
"invalid value type on stack: ")
960 LDBG() <<
" PUSH: " << val;
961 values.push_back(val);
964 LDBG() <<
" Updated stack size: " << values.size();
968template <std::
byte EndParseByte>
969parsed_inst_t ExpressionParser::parse(OpBuilder &builder,
970 UniqueByte<EndParseByte> endByte) {
971 auto res =
parse(builder, ByteSequence<EndParseByte>{});
974 return res->opResults;
977template <std::byte... ExpressionParseEnd>
978FailureOr<ExpressionParser::ParseResultWithInfo>
979ExpressionParser::parse(OpBuilder &builder,
980 ByteSequence<ExpressionParseEnd...> parsingEndFilters) {
981 SmallVector<Value> res;
983 currentOpLoc = parser.getLocation();
984 FailureOr<std::byte> opCode = parser.consumeByte();
987 if (isValueOneOf(*opCode, parsingEndFilters))
988 return {{res, *opCode}};
989 parsed_inst_t resParsed;
990 resParsed = dispatchToInstParser(*opCode, builder);
993 std::swap(res, *resParsed);
994 if (
failed(pushResults(res)))
999llvm::FailureOr<FunctionType>
1000ExpressionParser::parseBlockFuncType(OpBuilder &builder) {
1001 return getFuncTypeFor(builder, parser.parseBlockType(builder.
getContext()));
1004template <
typename OpToCreate>
1006ExpressionParser::ExprParserProxy::parseBlockLikeOp(OpBuilder &builder) {
1007 auto opLoc = getCurrentOpLoc();
1008 auto funcType = parseBlockFuncType(builder);
1012 auto inputTypes = funcType->getInputs();
1013 auto inputOps = popOperands(inputTypes);
1018 Region *curRegion = curBlock->
getParent();
1019 auto resTypes = funcType->getResults();
1020 llvm::SmallVector<Location> locations{};
1021 locations.resize(resTypes.size(), getCurrentOpLoc());
1023 builder.
createBlock(curRegion, curRegion->
end(), resTypes, locations);
1026 OpToCreate::create(builder, getCurrentOpLoc(), *inputOps, successor);
1027 auto *blockBody = blockOp.createBlock();
1028 if (
failed(parseBlockContent(builder, blockBody, resTypes, opLoc, blockOp)))
1031 return {
ValueRange{successor->getArguments()}};
1034parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::block>,
1036 ExpressionParser::ExprParserProxy &exprParser) {
1037 return exprParser.parseBlockLikeOp<BlockOp>(builder);
1040parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::loop>,
1042 ExpressionParser::ExprParserProxy &exprParser) {
1043 return exprParser.parseBlockLikeOp<LoopOp>(builder);
1046parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::ifOpCode>,
1048 ExpressionParser::ExprParserProxy &exprParser) {
1049 auto opLoc = exprParser.getCurrentOpLoc();
1050 auto funcType = exprParser.parseBlockFuncType(builder);
1054 LDBG() <<
"Parsing an if instruction of type " << *funcType;
1055 auto inputTypes = funcType->getInputs();
1056 auto conditionValue = exprParser.popOperands(builder.
getI32Type());
1057 if (
failed(conditionValue))
1059 auto inputOps = exprParser.popOperands(inputTypes);
1064 Region *curRegion = curBlock->
getParent();
1065 auto resTypes = funcType->getResults();
1066 llvm::SmallVector<Location> locations{};
1067 locations.resize(resTypes.size(), exprParser.getCurrentOpLoc());
1069 builder.
createBlock(curRegion, curRegion->
end(), resTypes, locations);
1071 auto ifOp = IfOp::create(builder, exprParser.getCurrentOpLoc(),
1072 conditionValue->front(), *inputOps, successor);
1073 auto *ifEntryBlock = ifOp.createIfBlock();
1074 constexpr auto ifElseFilter =
1077 auto parseIfRes = exprParser.parseBlockContent(
1078 builder, ifEntryBlock, resTypes, opLoc, ifOp, ifElseFilter);
1082 LDBG() <<
" else block is present.";
1083 Block *elseEntryBlock = ifOp.createElseBlock();
1084 auto parseElseRes = exprParser.parseBlockContent(builder, elseEntryBlock,
1085 resTypes, opLoc, ifOp);
1086 if (
failed(parseElseRes))
1090 return {
ValueRange{successor->getArguments()}};
1093parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::branchIf>,
1095 ExpressionParser::ExprParserProxy &exprParser) {
1096 auto level = exprParser.parser().parseLiteral<uint32_t>();
1100 Region *curRegion = curBlock->
getParent();
1103 auto condition = exprParser.popOperands(builder.
getI32Type());
1108 LabelBranchingOpInterface::getTargetOpFromBlock(curBlock, *level);
1111 auto inputTypes = targetOp->getLabelTarget()->getArgumentTypes();
1112 auto branchArgs = exprParser.popOperands(inputTypes);
1115 BranchIfOp::create(builder, exprParser.getCurrentOpLoc(), condition->front(),
1119 return {*branchArgs};
1122parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::call>,
1124 ExpressionParser::ExprParserProxy &exprParser) {
1125 auto loc = exprParser.getCurrentOpLoc();
1126 auto funcIdx = exprParser.parser().parseLiteral<uint32_t>();
1129 if (*funcIdx >= exprParser.symbols().funcSymbols.size())
1130 return emitError(loc,
"Invalid function index: ") << *funcIdx;
1131 auto callee = exprParser.symbols().funcSymbols[*funcIdx];
1132 llvm::ArrayRef<Type> inTypes = callee.functionType.getInputs();
1133 llvm::ArrayRef<Type> resTypes = callee.functionType.getResults();
1134 parsed_inst_t inOperands = exprParser.popOperands(inTypes);
1138 FuncCallOp::create(builder, loc, resTypes, callee.symbol, *inOperands);
1139 return {callOp.getResults()};
1142parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localGet>,
1144 ExpressionParser::ExprParserProxy &exprParser) {
1145 FailureOr<uint32_t>
id = exprParser.parser().parseLiteral<uint32_t>();
1146 Location instLoc = exprParser.getCurrentOpLoc();
1149 if (*
id >= exprParser.locals().size())
1150 return emitError(instLoc,
"invalid local index. function has ")
1151 << exprParser.locals().size()
1152 <<
" accessible locals, received index " << *id;
1153 return {{LocalGetOp::create(builder, instLoc, exprParser.locals()[*
id])
1157parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::globalGet>,
1159 ExpressionParser::ExprParserProxy &exprParser) {
1160 FailureOr<uint32_t>
id = exprParser.parser().parseLiteral<uint32_t>();
1161 Location instLoc = exprParser.getCurrentOpLoc();
1164 if (*
id >= exprParser.symbols().globalSymbols.size())
1165 return emitError(instLoc,
"invalid global index. function has ")
1166 << exprParser.symbols().globalSymbols.size()
1167 <<
" accessible globals, received index " << *id;
1168 GlobalSymbolRefContainer globalVar = exprParser.symbols().globalSymbols[*id];
1169 auto globalOp = GlobalGetOp::create(builder, instLoc, globalVar.globalType,
1172 return {{globalOp.getResult()}};
1175template <
typename OpToCreate>
1176parsed_inst_t ExpressionParser::parseSetOrTee(OpBuilder &builder) {
1177 FailureOr<uint32_t>
id = parser.parseLiteral<uint32_t>();
1180 if (*
id >= locals.size())
1181 return emitError(*currentOpLoc,
"invalid local index. function has ")
1182 << locals.size() <<
" accessible locals, received index " << *id;
1183 if (valueStack.empty())
1186 "invalid stack access, trying to access a value on an empty stack");
1192 OpToCreate::create(builder, *currentOpLoc, locals[*
id], poppedOp->front())
1196parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localSet>,
1198 ExpressionParser::ExprParserProxy &exprParser) {
1199 return exprParser.parseSetOrTee<LocalSetOp>(builder);
1202parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::localTee>,
1204 ExpressionParser::ExprParserProxy &exprParser) {
1205 return exprParser.parseSetOrTee<LocalTeeOp>(builder);
1208template <
typename T>
1209inline Type buildLiteralType(OpBuilder &);
1212inline Type buildLiteralType<int32_t>(OpBuilder &builder) {
1217inline Type buildLiteralType<int64_t>(OpBuilder &builder) {
1222[[maybe_unused]]
inline Type buildLiteralType<uint32_t>(OpBuilder &builder) {
1227[[maybe_unused]]
inline Type buildLiteralType<uint64_t>(OpBuilder &builder) {
1232inline Type buildLiteralType<float>(OpBuilder &builder) {
1237inline Type buildLiteralType<double>(OpBuilder &builder) {
1241template <
typename ValT,
1242 typename E = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1245template <
typename ValT>
1246struct AttrHolder<ValT, std::enable_if_t<std::is_integral_v<ValT>>> {
1247 using type = IntegerAttr;
1250template <
typename ValT>
1251struct AttrHolder<ValT, std::enable_if_t<std::is_floating_point_v<ValT>>> {
1252 using type = FloatAttr;
1255template <
typename ValT>
1256using attr_holder_t =
typename AttrHolder<ValT>::type;
1258template <
typename ValT,
1259 typename EnableT = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1260attr_holder_t<ValT> buildLiteralAttr(OpBuilder &builder, ValT val) {
1261 return attr_holder_t<ValT>::get(buildLiteralType<ValT>(builder), val);
1264template <
typename valueT>
1265parsed_inst_t ExpressionParser::ExprParserProxy::parseConstInst(
1266 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueT>> *) {
1267 auto parsedConstant = parser().parseLiteral<valueT>();
1268 if (
failed(parsedConstant))
1271 ConstOp::create(builder, getCurrentOpLoc(),
1272 buildLiteralAttr<valueT>(builder, *parsedConstant));
1273 return {{constOp.getResult()}};
1276parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constI32>,
1278 ExpressionParser::ExprParserProxy &exprParser) {
1279 return exprParser.parseConstInst<int32_t>(builder);
1282parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constI64>,
1284 ExpressionParser::ExprParserProxy &exprParser) {
1285 return exprParser.parseConstInst<int64_t>(builder);
1288parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constFP32>,
1290 ExpressionParser::ExprParserProxy &exprParser) {
1291 return exprParser.parseConstInst<
float>(builder);
1294parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::constFP64>,
1296 ExpressionParser::ExprParserProxy &exprParser) {
1297 return exprParser.parseConstInst<
double>(builder);
1300template <
typename opcode,
typename valueType,
unsigned int numOperands>
1301inline parsed_inst_t ExpressionParser::ExprParserProxy::buildNumericOp(
1302 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueType>> *) {
1303 auto ty = buildLiteralType<valueType>(builder);
1304 LDBG() <<
"*** buildNumericOp: numOperands = " << numOperands
1305 <<
", type = " << ty <<
" ***";
1306 auto tysToPop = SmallVector<Type, numOperands>();
1307 tysToPop.resize(numOperands);
1308 llvm::fill(tysToPop, ty);
1309 auto operands = popOperands(tysToPop);
1312 auto op = opcode::create(builder, getCurrentOpLoc(), *operands).getResult();
1313 LDBG() <<
"Built operation: " << op;
1318#define BUILD_NUMERIC_OP(OP_NAME, N_ARGS, PREFIX, SUFFIX, TYPE) \
1319 inline parsed_inst_t parse( \
1320 OpCode<WasmBinaryEncoding::OpCode::PREFIX##SUFFIX>, OpBuilder &builder, \
1321 ExpressionParser::ExprParserProxy &exprParser) { \
1322 return exprParser.buildNumericOp<OP_NAME, TYPE, N_ARGS>(builder); \
1326#define BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1327 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I32, int32_t) \
1328 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I64, int64_t)
1331#define BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX) \
1332 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F32, float) \
1333 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F64, double)
1336#define BUILD_NUMERIC_BINOP_INTFP(OP_NAME, PREFIX) \
1337 BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1338 BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX)
1341#define BUILD_NUMERIC_UNARY_OP_INT(OP_NAME, PREFIX) \
1342 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I32, int32_t) \
1343 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I64, int64_t)
1346#define BUILD_NUMERIC_UNARY_OP_FP(OP_NAME, PREFIX) \
1347 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F32, float) \
1348 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F64, double)
1396#undef BUILD_NUMERIC_BINOP_FP
1397#undef BUILD_NUMERIC_BINOP_INT
1398#undef BUILD_NUMERIC_BINOP_INTFP
1399#undef BUILD_NUMERIC_UNARY_OP_FP
1400#undef BUILD_NUMERIC_UNARY_OP_INT
1401#undef BUILD_NUMERIC_OP
1402#undef BUILD_NUMERIC_CAST_OP
1404template <
typename opType,
typename inputType,
typename outputType,
1405 typename... extraArgsT>
1407ExpressionParser::ExprParserProxy::buildConvertOp(
OpBuilder &builder,
1408 extraArgsT... extraArgs) {
1409 static_assert(std::is_arithmetic_v<inputType>,
1410 "InputType should be an arithmetic type");
1411 static_assert(std::is_arithmetic_v<outputType>,
1412 "OutputType should be an arithmetic type");
1413 auto intype = buildLiteralType<inputType>(builder);
1414 auto outType = buildLiteralType<outputType>(builder);
1415 auto operand = popOperands(intype);
1416 if (failed(operand))
1418 auto op = opType::create(builder, getCurrentOpLoc(), outType,
1419 operand->front(), extraArgs...);
1420 LDBG() <<
"Built operation: " << op;
1421 return {{op.getResult()}};
1424parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::demoteF64ToF32>,
1426 ExpressionParser::ExprParserProxy &exprParser) {
1427 return exprParser.buildConvertOp<DemoteOp, double,
float>(builder);
1430parsed_inst_t
parse(OpCode<WasmBinaryEncoding::OpCode::wrap>,
1432 ExpressionParser::ExprParserProxy &exprParser) {
1433 return exprParser.buildConvertOp<WrapOp, int64_t, int32_t>(builder);
1436#define BUILD_CONVERSION_OP(IN_T, OUT_T, SOURCE_OP, TARGET_OP) \
1437 inline parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::SOURCE_OP>, \
1438 OpBuilder &builder, \
1439 ExpressionParser::ExprParserProxy &exprParser) { \
1440 return exprParser.buildConvertOp<TARGET_OP, IN_T, OUT_T>(builder); \
1443#define BUILD_CONVERT_OP_FOR(DEST_T, WIDTH) \
1444 BUILD_CONVERSION_OP(uint32_t, DEST_T, convertUI32F##WIDTH, ConvertUOp) \
1445 BUILD_CONVERSION_OP(int32_t, DEST_T, convertSI32F##WIDTH, ConvertSOp) \
1446 BUILD_CONVERSION_OP(uint64_t, DEST_T, convertUI64F##WIDTH, ConvertUOp) \
1447 BUILD_CONVERSION_OP(int64_t, DEST_T, convertSI64F##WIDTH, ConvertSOp)
1452#undef BUILD_CONVERT_OP_FOR
1454#define BUILD_TRUNC_OP_FOR(SRC_T, WIDTH) \
1455 BUILD_CONVERSION_OP(SRC_T, int32_t, truncSI32F##WIDTH, TruncSIOp) \
1456 BUILD_CONVERSION_OP(SRC_T, uint32_t, truncUI32F##WIDTH, TruncUIOp) \
1457 BUILD_CONVERSION_OP(SRC_T, int64_t, truncSI64F##WIDTH, TruncSIOp) \
1458 BUILD_CONVERSION_OP(SRC_T, uint64_t, truncUI64F##WIDTH, TruncUIOp)
1463#undef BUILD_TRUNC_OP_FOR
1468#undef BUILD_CONVERSION_OP
1470parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::saturatedTruncate>,
1472 ExpressionParser::ExprParserProxy &parser,
1473 std::uint32_t subOpCode) {
1475 return emitError(parser.getCurrentOpLoc())
1476 <<
"invalid sub-opcode for trunc_saturate: " << subOpCode;
1477 LDBG() <<
"Sub subOpcode for operation: " << subOpCode;
1478 bool isDestUnsigned = (subOpCode & 1) != 0;
1479 bool isSrcF64 = (subOpCode & 2) != 0;
1480 bool isDestI64 = (subOpCode & 4) != 0;
1484 auto srcOp = parser.popOperands(srcType);
1488 ? TruncSatUIOp::create(builder, parser.getCurrentOpLoc(),
1489 destType, srcOp->front())
1490 : TruncSatSIOp::create(builder, parser.getCurrentOpLoc(),
1491 destType, srcOp->front());
1492 LDBG() <<
"Built operation: " << op;
1496#define BUILD_SLICE_EXTEND_PARSER(IT_WIDTH, EXTRACT_WIDTH) \
1497 parsed_inst_t parse( \
1498 OpCode<WasmBinaryEncoding::OpCode::extendI##IT_WIDTH##EXTRACT_WIDTH##S>, \
1499 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1500 using inout_t = int##IT_WIDTH##_t; \
1501 auto attr = builder.getUI32IntegerAttr(EXTRACT_WIDTH); \
1502 return exprParser.buildConvertOp<ExtendLowBitsSOp, inout_t, inout_t>( \
1512#undef BUILD_SLICE_EXTEND_PARSER
1514parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::promoteF32ToF64>,
1516 ExpressionParser::ExprParserProxy &exprParser) {
1517 return exprParser.buildConvertOp<PromoteOp, float,
double>(builder);
1520#define BUILD_REINTERPRET_PARSER(WIDTH, FP_TYPE) \
1521 inline parsed_inst_t parse( \
1522 OpCode<WasmBinaryEncoding::OpCode::reinterpretF##WIDTH##AsI##WIDTH>, \
1523 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1524 return exprParser.buildConvertOp<ReinterpretOp, FP_TYPE, int##WIDTH##_t>( \
1528 inline parsed_inst_t parse( \
1529 OpCode<WasmBinaryEncoding::OpCode::reinterpretI##WIDTH##AsF##WIDTH>, \
1530 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1531 return exprParser.buildConvertOp<ReinterpretOp, int##WIDTH##_t, FP_TYPE>( \
1538#undef BUILD_REINTERPRET_PARSER
1540class InstDispatcher {
1542 template <std::
byte OpCode,
typename =
void>
1543 struct HasParserRegistered : std::false_type {};
1545 template <std::
byte opCode>
1546 struct HasParserRegistered<
1547 opCode, std::void_t<decltype(parse(
1548 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1549 std::declval<ExpressionParser::ExprParserProxy &>()))>>
1550 : std::true_type {};
1552 template <std::
byte opCode>
1553 static constexpr bool hasParseredRegistered =
1554 HasParserRegistered<opCode>::value;
1556 template <std::
byte OpCode,
typename =
void>
1557 struct HasParserWthSubOpCodeRegistered : std::false_type {};
1559 template <std::
byte opCode>
1560 struct HasParserWthSubOpCodeRegistered<
1561 opCode, std::void_t<decltype(parse(
1562 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1563 std::declval<ExpressionParser::ExprParserProxy &>(),
1564 std::declval<std::uint32_t>()))>> : std::true_type {
1565 static_assert(!hasParseredRegistered<opCode>,
1566 "plain parser and parser with sub-opcode can't be registered "
1567 "for the same opcode");
1570 template <std::
byte opCode>
1571 static constexpr bool hasParseredWithSubOpCodeRegistered =
1572 HasParserWthSubOpCodeRegistered<opCode>::value;
1574 using dispatch_t = parsed_inst_t (*)(
OpBuilder &,
1575 ExpressionParser::ExprParserProxy &);
1577 static inline parsed_inst_t
1579 ExpressionParser::ExprParserProxy &expressionParser) {
1580 llvm_unreachable(
"Failure in opcode parser dispatch logic.");
1581 return mlir::failure();
1584 template <std::
byte opCode>
1585 static parsed_inst_t
1587 ExpressionParser::ExprParserProxy &exprParser) {
1588 return parse(OpCode<opCode>{}, builder, exprParser);
1591 template <std::
byte opCode>
1592 static parsed_inst_t
1593 forwardToParserWithSubOpCode(
OpBuilder &builder,
1594 ExpressionParser::ExprParserProxy &exprParser) {
1595 auto loc = exprParser.getCurrentOpLoc();
1596 auto subOpCode = exprParser.parser().parseLiteral<std::uint32_t>();
1597 if (failed(subOpCode))
1598 return emitError(loc) <<
"expecting sub opcode for opcode "
1599 <<
static_cast<uint8_t
>(opCode);
1600 return parse(OpCode<opCode>{}, builder, exprParser, subOpCode.value());
1603 template <std::
byte opCode>
1604 static constexpr dispatch_t getHandlerForOpCode() {
1605 if constexpr (hasParseredRegistered<opCode>)
1606 return forwardToParser<opCode>;
1607 else if constexpr (hasParseredWithSubOpCodeRegistered<opCode>)
1608 return forwardToParserWithSubOpCode<opCode>;
1610 return unreachableHandler;
1614 static inline parsed_inst_t
1616 ExpressionParser::ExprParserProxy &expressionParser,
1618 return emitError(expressionParser.getCurrentOpLoc(),
1619 "unknown instruction opcode: ")
1620 <<
static_cast<int>(opCode);
1623 template <std::byte... opCodes>
1624 static inline parsed_inst_t
1625 dispatchImpl(std::byte opCode,
OpBuilder &builder,
1626 ExpressionParser::ExprParserProxy &exprParser,
1627 ByteSequence<opCodes...>) {
1628 static constexpr std::array<bool, 256> opcodeValidityMap{
1629 (hasParseredRegistered<opCodes> ||
1630 hasParseredWithSubOpCodeRegistered<opCodes>)...};
1631 static constexpr std::array<dispatch_t, 256> dispatchTable{
1632 getHandlerForOpCode<opCodes>()...};
1633 if (opcodeValidityMap[
static_cast<size_t>(opCode)]) {
1634 return dispatchTable[
static_cast<size_t>(opCode)](builder, exprParser);
1636 return invalidOpcodeDiag(builder, exprParser, opCode);
1650 static parsed_inst_t dispatch(std::byte opCode,
OpBuilder &builder,
1651 ExpressionParser::ExprParserProxy &exprParser) {
1652 return dispatchImpl(opCode, builder, exprParser, all8bitsBytes);
1657ExpressionParser::dispatchToInstParser(std::byte opCode, OpBuilder &builder) {
1658 ExpressionParser::ExprParserProxy exprParser{*
this};
1659 return InstDispatcher::dispatch(opCode, builder, exprParser);
1661class WasmBinaryParser {
1663 struct SectionRegistry {
1664 using section_location_t = StringRef;
1666 std::array<SmallVector<section_location_t>, highestWasmSectionID + 1>
1669 template <WasmSectionType SecType>
1670 std::conditional_t<sectionShouldBeUnique(SecType),
1671 std::optional<section_location_t>,
1672 ArrayRef<section_location_t>>
1673 getContentForSection()
const {
1674 constexpr auto idx =
static_cast<size_t>(SecType);
1675 if constexpr (sectionShouldBeUnique(SecType)) {
1676 return registry[idx].empty() ? std::nullopt
1677 : std::make_optional(registry[idx][0]);
1679 return registry[idx];
1683 bool hasSection(WasmSectionType secType)
const {
1684 return !registry[
static_cast<size_t>(secType)].empty();
1692 LogicalResult registerSection(WasmSectionType secType,
1693 section_location_t location, Location loc) {
1694 if (sectionShouldBeUnique(secType) && hasSection(secType))
1696 "trying to add a second instance of unique section");
1698 registry[
static_cast<size_t>(secType)].
push_back(location);
1699 emitRemark(loc,
"Adding section with section ID ")
1700 <<
static_cast<uint8_t
>(secType);
1704 LogicalResult populateFromBody(ParserHead ph) {
1706 FileLineColLoc sectionLoc = ph.getLocation();
1707 FailureOr<WasmSectionType> secType = ph.parseWasmSectionType();
1711 FailureOr<uint32_t> secSizeParsed = ph.parseLiteral<uint32_t>();
1712 if (
failed(secSizeParsed))
1715 uint32_t secSize = *secSizeParsed;
1716 FailureOr<StringRef> sectionContent = ph.consumeNBytes(secSize);
1717 if (
failed(sectionContent))
1720 LogicalResult registration =
1721 registerSection(*secType, *sectionContent, sectionLoc);
1723 if (
failed(registration))
1730 auto getLocation(
int offset = 0)
const {
1734 template <WasmSectionType>
1735 LogicalResult parseSectionItem(ParserHead &,
size_t);
1737 template <WasmSectionType section>
1738 LogicalResult parseSection() {
1739 auto secName = std::string{wasmSectionName<section>};
1740 auto sectionNameAttr =
1741 StringAttr::get(ctx, srcName.strref() +
":" + secName +
"-SECTION");
1742 unsigned offset = 0;
1743 auto getLocation = [sectionNameAttr, &offset]() {
1746 auto secContent = registry.getContentForSection<section>();
1748 LDBG() << secName <<
" section is not present in file.";
1752 auto secSrc = secContent.value();
1753 ParserHead ph{secSrc, sectionNameAttr};
1754 FailureOr<uint32_t> nElemsParsed = ph.parseVectorSize();
1755 if (
failed(nElemsParsed))
1757 uint32_t nElems = *nElemsParsed;
1758 LDBG() <<
"starting to parse " << nElems <<
" items for section "
1760 for (
size_t i = 0; i < nElems; ++i) {
1761 if (
failed(parseSectionItem<section>(ph, i)))
1766 return emitError(getLocation(),
"unparsed garbage at end of section ")
1772 LogicalResult visitImport(Location loc, StringRef moduleName,
1773 StringRef importName, TypeIdxRecord tid) {
1775 if (tid.id >= symbols.moduleFuncTypes.size())
1776 return emitError(loc,
"invalid type id: ")
1777 << tid.id <<
". Only " << symbols.moduleFuncTypes.size()
1778 <<
" type registrations";
1779 FunctionType type = symbols.moduleFuncTypes[tid.id];
1780 std::string symbol = symbols.getNewFuncSymbolName();
1781 auto funcOp = FuncImportOp::create(builder, loc, symbol, moduleName,
1784 return funcOp.verify();
1788 LogicalResult visitImport(Location loc, StringRef moduleName,
1789 StringRef importName, LimitType limitType) {
1790 std::string symbol = symbols.getNewMemorySymbolName();
1791 auto memOp = MemImportOp::create(builder, loc, symbol, moduleName,
1792 importName, limitType);
1794 return memOp.verify();
1798 LogicalResult visitImport(Location loc, StringRef moduleName,
1799 StringRef importName, TableType tableType) {
1800 std::string symbol = symbols.getNewTableSymbolName();
1801 auto tableOp = TableImportOp::create(builder, loc, symbol, moduleName,
1802 importName, tableType);
1804 return tableOp.verify();
1808 LogicalResult visitImport(Location loc, StringRef moduleName,
1809 StringRef importName, GlobalTypeRecord globalType) {
1810 std::string symbol = symbols.getNewGlobalSymbolName();
1812 GlobalImportOp::create(builder, loc, symbol, moduleName, importName,
1813 globalType.type, globalType.isMutable);
1814 symbols.globalSymbols.push_back(
1816 return giOp.verify();
1820 LogicalResult peekDiag(Diagnostic &diag) {
1821 if (diag.
getSeverity() == DiagnosticSeverity::Error)
1827 WasmBinaryParser(llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
1828 : builder{ctx}, ctx{ctx} {
1830 [
this](Diagnostic &diag) {
return peekDiag(diag); });
1832 if (sourceMgr.getNumBuffers() != 1) {
1833 emitError(UnknownLoc::get(ctx),
"one source file should be provided");
1836 uint32_t sourceBufId = sourceMgr.getMainFileID();
1837 StringRef source = sourceMgr.getMemoryBuffer(sourceBufId)->getBuffer();
1838 srcName = StringAttr::get(
1839 ctx, sourceMgr.getMemoryBuffer(sourceBufId)->getBufferIdentifier());
1841 auto parser = ParserHead{source, srcName};
1842 auto const wasmHeader = StringRef{
"\0asm", 4};
1843 FileLineColLoc magicLoc = parser.getLocation();
1844 FailureOr<StringRef> magic = parser.consumeNBytes(wasmHeader.size());
1845 if (
failed(magic) || magic->compare(wasmHeader)) {
1846 emitError(magicLoc,
"source file does not contain valid Wasm header");
1849 auto const expectedVersionString = StringRef{
"\1\0\0\0", 4};
1850 FileLineColLoc versionLoc = parser.getLocation();
1851 FailureOr<StringRef> version =
1852 parser.consumeNBytes(expectedVersionString.size());
1855 if (version->compare(expectedVersionString)) {
1857 "unsupported Wasm version. only version 1 is supported");
1860 LogicalResult fillRegistry = registry.populateFromBody(parser.copy());
1861 if (
failed(fillRegistry))
1864 mOp = ModuleOp::create(builder, getLocation());
1866 LogicalResult parsingTypes = parseSection<WasmSectionType::TYPE>();
1867 if (
failed(parsingTypes))
1870 LogicalResult parsingImports = parseSection<WasmSectionType::IMPORT>();
1871 if (
failed(parsingImports))
1874 firstInternalFuncID = symbols.funcSymbols.size();
1876 LogicalResult parsingFunctions = parseSection<WasmSectionType::FUNCTION>();
1877 if (
failed(parsingFunctions))
1880 LogicalResult parsingTables = parseSection<WasmSectionType::TABLE>();
1881 if (
failed(parsingTables))
1884 LogicalResult parsingMems = parseSection<WasmSectionType::MEMORY>();
1888 LogicalResult parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
1889 if (
failed(parsingGlobals))
1892 LogicalResult parsingCode = parseSection<WasmSectionType::CODE>();
1896 LogicalResult parsingExports = parseSection<WasmSectionType::EXPORT>();
1897 if (
failed(parsingExports))
1901 LDBG() <<
"WASM Imports:"
1903 <<
" - Num functions: " << symbols.funcSymbols.size() <<
"\n"
1904 <<
" - Num globals: " << symbols.globalSymbols.size() <<
"\n"
1905 <<
" - Num memories: " << symbols.memSymbols.size() <<
"\n"
1906 <<
" - Num tables: " << symbols.tableSymbols.size();
1909 ModuleOp getModule() {
1918 mlir::StringAttr srcName;
1920 WasmModuleSymbolTables symbols;
1923 SectionRegistry registry;
1924 size_t firstInternalFuncID{0};
1930WasmBinaryParser::parseSectionItem<WasmSectionType::IMPORT>(ParserHead &ph,
1932 FileLineColLoc importLoc = ph.getLocation();
1933 auto moduleName = ph.parseName();
1937 auto importName = ph.parseName();
1941 FailureOr<ImportDesc>
import = ph.parseImportDesc(ctx);
1946 [
this, importLoc, &moduleName, &importName](
auto import) {
1947 return visitImport(importLoc, *moduleName, *importName,
import);
1954WasmBinaryParser::parseSectionItem<WasmSectionType::EXPORT>(ParserHead &ph,
1956 FileLineColLoc exportLoc = ph.getLocation();
1958 auto exportName = ph.parseName();
1962 FailureOr<std::byte> opcode = ph.consumeByte();
1966 FailureOr<uint32_t> idx = ph.parseLiteral<uint32_t>();
1970 using SymbolRefDesc = std::variant<SmallVector<SymbolRefContainer>,
1971 SmallVector<GlobalSymbolRefContainer>,
1972 SmallVector<FunctionSymbolRefContainer>>;
1974 SymbolRefDesc currentSymbolList;
1975 std::string symbolType =
"";
1978 symbolType =
"function";
1979 currentSymbolList = symbols.funcSymbols;
1982 symbolType =
"table";
1983 currentSymbolList = symbols.tableSymbols;
1986 symbolType =
"memory";
1987 currentSymbolList = symbols.memSymbols;
1990 symbolType =
"global";
1991 currentSymbolList = symbols.globalSymbols;
1994 return emitError(exportLoc,
"invalid value for export type: ")
1995 << std::to_integer<unsigned>(*opcode);
1998 auto currentSymbol = std::visit(
1999 [&](
const auto &list) -> FailureOr<FlatSymbolRefAttr> {
2000 if (*idx > list.size()) {
2004 "trying to export {0} {1} which is undefined in this scope",
2008 return list[*idx].symbol;
2012 if (
failed(currentSymbol))
2018 return SymbolTable{mOp}.rename(symName, *exportName);
2023WasmBinaryParser::parseSectionItem<WasmSectionType::TABLE>(ParserHead &ph,
2025 FileLineColLoc opLocation = ph.getLocation();
2026 FailureOr<TableType> tableType = ph.parseTableType(ctx);
2029 LDBG() <<
" Parsed table description: " << *tableType;
2030 StringAttr symbol = builder.
getStringAttr(symbols.getNewTableSymbolName());
2032 TableOp::create(builder, opLocation, symbol.strref(), *tableType);
2033 symbols.tableSymbols.push_back({SymbolRefAttr::get(tableOp)});
2039WasmBinaryParser::parseSectionItem<WasmSectionType::FUNCTION>(ParserHead &ph,
2041 FileLineColLoc opLoc = ph.getLocation();
2042 auto typeIdxParsed = ph.parseLiteral<uint32_t>();
2043 if (
failed(typeIdxParsed))
2045 uint32_t typeIdx = *typeIdxParsed;
2046 if (typeIdx >= symbols.moduleFuncTypes.size())
2047 return emitError(getLocation(),
"invalid type index: ") << typeIdx;
2048 std::string symbol = symbols.getNewFuncSymbolName();
2050 FuncOp::create(builder, opLoc, symbol, symbols.moduleFuncTypes[typeIdx]);
2051 Block *block = funcOp.addEntryBlock();
2052 OpBuilder::InsertionGuard guard{builder};
2054 ReturnOp::create(builder, opLoc);
2055 symbols.funcSymbols.push_back(
2057 symbols.moduleFuncTypes[typeIdx]});
2058 return funcOp.verify();
2063WasmBinaryParser::parseSectionItem<WasmSectionType::TYPE>(ParserHead &ph,
2065 FailureOr<FunctionType> funcType = ph.parseFunctionType(ctx);
2068 LDBG() <<
"Parsed function type " << *funcType;
2069 symbols.moduleFuncTypes.push_back(*funcType);
2075WasmBinaryParser::parseSectionItem<WasmSectionType::MEMORY>(ParserHead &ph,
2077 FileLineColLoc opLocation = ph.getLocation();
2078 FailureOr<LimitType> memory = ph.parseLimit(ctx);
2082 LDBG() <<
" Registering memory " << *memory;
2083 std::string symbol = symbols.getNewMemorySymbolName();
2084 auto memOp = MemOp::create(builder, opLocation, symbol, *memory);
2085 symbols.memSymbols.push_back({SymbolRefAttr::get(memOp)});
2091WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
2093 FileLineColLoc globalLocation = ph.getLocation();
2094 auto globalTypeParsed = ph.parseGlobalType(ctx);
2095 if (
failed(globalTypeParsed))
2098 GlobalTypeRecord globalType = *globalTypeParsed;
2099 auto symbol = builder.
getStringAttr(symbols.getNewGlobalSymbolName());
2100 auto globalOp = wasmssa::GlobalOp::create(
2101 builder, globalLocation, symbol, globalType.type, globalType.isMutable);
2102 symbols.globalSymbols.push_back(
2104 OpBuilder::InsertionGuard guard{builder};
2107 parsed_inst_t expr = ph.parseExpression(builder, symbols);
2111 return emitError(globalLocation,
"global with empty initializer");
2112 if (expr->size() != 1 && (*expr)[0].getType() != globalType.type)
2115 "initializer result type does not match global declaration type");
2116 ReturnOp::create(builder, globalLocation, *expr);
2121LogicalResult WasmBinaryParser::parseSectionItem<WasmSectionType::CODE>(
2122 ParserHead &ph,
size_t innerFunctionId) {
2123 unsigned long funcId = innerFunctionId + firstInternalFuncID;
2124 FunctionSymbolRefContainer symRef = symbols.funcSymbols[funcId];
2128 if (
failed(ph.parseCodeFor(funcOp, symbols)))
2134namespace mlir::wasm {
2137 WasmBinaryParser wBN{source, context};
2138 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