1616#include " mlir/IR/BuiltinAttributeInterfaces.h"
1717#include " mlir/IR/BuiltinTypes.h"
1818#include " mlir/IR/Location.h"
19+ #include " mlir/Support/LLVM.h"
1920#include " mlir/Target/Wasm/WasmBinaryEncoding.h"
2021#include " mlir/Target/Wasm/WasmImporter.h"
21- #include " llvm/ADT/Statistic.h"
2222#include " llvm/Support/Debug.h"
2323#include " llvm/Support/DebugLog.h"
2424#include " llvm/Support/FormatVariadic.h"
2525#include " llvm/Support/LEB128.h"
26+ #include " llvm/Support/LogicalResult.h"
2627
27- #include < climits >
28+ #include < cstddef >
2829#include < cstdint>
2930#include < variant>
3031
@@ -148,22 +149,22 @@ struct WasmModuleSymbolTables {
148149 }
149150
150151 std::string getNewFuncSymbolName () const {
151- auto id = funcSymbols.size ();
152+ size_t id = funcSymbols.size ();
152153 return getNewSymbolName (" func_" , id);
153154 }
154155
155156 std::string getNewGlobalSymbolName () const {
156- auto id = globalSymbols.size ();
157+ size_t id = globalSymbols.size ();
157158 return getNewSymbolName (" global_" , id);
158159 }
159160
160161 std::string getNewMemorySymbolName () const {
161- auto id = memSymbols.size ();
162+ size_t id = memSymbols.size ();
162163 return getNewSymbolName (" mem_" , id);
163164 }
164165
165166 std::string getNewTableSymbolName () const {
166- auto id = tableSymbols.size ();
167+ size_t id = tableSymbols.size ();
167168 return getNewSymbolName (" table_" , id);
168169 }
169170};
@@ -342,7 +343,7 @@ class ParserHead {
342343 }
343344
344345 FailureOr<std::byte> consumeByte () {
345- auto res = consumeNBytes (1 );
346+ FailureOr<StringRef> res = consumeNBytes (1 );
346347 if (failed (res))
347348 return failure ();
348349 return std::byte{*res->bytes_begin ()};
@@ -502,7 +503,7 @@ class ParserHead {
502503 FileLineColLoc importLoc = getLocation ();
503504 FailureOr<std::byte> importType = consumeByte ();
504505 auto packager = [](auto parseResult) -> FailureOr<ImportDesc> {
505- if (llvm:: failed (parseResult))
506+ if (failed (parseResult))
506507 return failure ();
507508 return {*parseResult};
508509 };
@@ -530,11 +531,11 @@ class ParserHead {
530531 return eParser.parse (builder);
531532 }
532533
533- llvm:: LogicalResult parseCodeFor (FuncOp func,
534- WasmModuleSymbolTables const &symbols) {
535- llvm:: SmallVector<local_val_t > locals{};
534+ LogicalResult parseCodeFor (FuncOp func,
535+ WasmModuleSymbolTables const &symbols) {
536+ SmallVector<local_val_t > locals{};
536537 // Populating locals with function argument
537- auto &block = func.getBody ().front ();
538+ Block &block = func.getBody ().front ();
538539 // Delete temporary return argument which was only created for IR validity
539540 assert (func.getBody ().getBlocks ().size () == 1 &&
540541 " Function should only have its default created block at this point" );
@@ -543,42 +544,42 @@ class ParserHead {
543544 auto returnOp = cast<ReturnOp>(&block.back ());
544545 assert (returnOp);
545546
546- auto codeSizeInBytes = parseUI32 ();
547+ FailureOr< uint32_t > codeSizeInBytes = parseUI32 ();
547548 if (failed (codeSizeInBytes))
548549 return failure ();
549- auto codeContent = consumeNBytes (*codeSizeInBytes);
550+ FailureOr<StringRef> codeContent = consumeNBytes (*codeSizeInBytes);
550551 if (failed (codeContent))
551552 return failure ();
552553 auto name = StringAttr::get (func->getContext (),
553554 locName.str () + " ::" + func.getSymName ());
554555 auto cParser = ParserHead{*codeContent, name};
555- auto localVecSize = cParser.parseVectorSize ();
556+ FailureOr< uint32_t > localVecSize = cParser.parseVectorSize ();
556557 if (failed (localVecSize))
557558 return failure ();
558559 OpBuilder builder{&func.getBody ().front ().back ()};
559560 for (auto arg : block.getArguments ())
560561 locals.push_back (cast<TypedValue<LocalRefType>>(arg));
561562 // Declare the local ops
562- auto nVarVec = *localVecSize;
563+ uint32_t nVarVec = *localVecSize;
563564 for (size_t i = 0 ; i < nVarVec; ++i) {
564- auto varLoc = cParser.getLocation ();
565- auto nSubVar = cParser.parseUI32 ();
565+ FileLineColLoc varLoc = cParser.getLocation ();
566+ FailureOr< uint32_t > nSubVar = cParser.parseUI32 ();
566567 if (failed (nSubVar))
567568 return failure ();
568- auto varT = cParser.parseValueType (func->getContext ());
569+ FailureOr<Type> varT = cParser.parseValueType (func->getContext ());
569570 if (failed (varT))
570571 return failure ();
571572 for (size_t j = 0 ; j < *nSubVar; ++j) {
572573 auto local = builder.create <LocalOp>(varLoc, *varT);
573574 locals.push_back (local.getResult ());
574575 }
575576 }
576- auto res = cParser.parseExpression (builder, symbols, locals);
577+ parsed_inst_t res = cParser.parseExpression (builder, symbols, locals);
577578 if (failed (res))
578579 return failure ();
579580 if (!cParser.end ())
580581 return emitError (cParser.getLocation (),
581- " Unparsed garbage remaining at end of code block" );
582+ " unparsed garbage remaining at end of code block" );
582583 builder.create <ReturnOp>(func->getLoc (), *res);
583584 returnOp->erase ();
584585 return success ();
@@ -609,7 +610,7 @@ class ParserHead {
609610
610611template <>
611612FailureOr<float > ParserHead::parseLiteral<float >() {
612- auto bytes = consumeNBytes (4 );
613+ FailureOr<StringRef> bytes = consumeNBytes (4 );
613614 if (failed (bytes))
614615 return failure ();
615616 float result;
@@ -619,7 +620,7 @@ FailureOr<float> ParserHead::parseLiteral<float>() {
619620
620621template <>
621622FailureOr<double > ParserHead::parseLiteral<double >() {
622- auto bytes = consumeNBytes (8 );
623+ FailureOr<StringRef> bytes = consumeNBytes (8 );
623624 if (failed (bytes))
624625 return failure ();
625626 double result;
@@ -724,7 +725,7 @@ parsed_inst_t ValueStack::popOperands(TypeRange operandTypes, Location *opLoc) {
724725 << " Current stack size: " << values.size ();
725726 if (operandTypes.size () > values.size ())
726727 return emitError (*opLoc,
727- " stack doesn't contain enough values. Trying to get " )
728+ " stack doesn't contain enough values. trying to get " )
728729 << operandTypes.size () << " operands on a stack containing only "
729730 << values.size () << " values." ;
730731 size_t stackIdxOffset = values.size () - operandTypes.size ();
@@ -734,7 +735,7 @@ parsed_inst_t ValueStack::popOperands(TypeRange operandTypes, Location *opLoc) {
734735 Value operand = values[i + stackIdxOffset];
735736 Type stackType = operand.getType ();
736737 if (stackType != operandTypes[i])
737- return emitError (*opLoc, " invalid operand type on stack. Expecting " )
738+ return emitError (*opLoc, " invalid operand type on stack. expecting " )
738739 << operandTypes[i] << " , value on stack is of type " << stackType
739740 << " ." ;
740741 LDBG () << " POP: " << operand;
@@ -795,28 +796,28 @@ ExpressionParser::parse(OpBuilder &builder,
795796template <>
796797inline parsed_inst_t ExpressionParser::parseSpecificInstruction<
797798 WasmBinaryEncoding::OpCode::localGet>(OpBuilder &builder) {
798- auto id = parser.parseLiteral <uint32_t >();
799- auto instLoc = *currentOpLoc;
799+ FailureOr< uint32_t > id = parser.parseLiteral <uint32_t >();
800+ Location instLoc = *currentOpLoc;
800801 if (failed (id))
801802 return failure ();
802803 if (*id >= locals.size ())
803- return emitError (instLoc, " Invalid local index. Function has " )
804+ return emitError (instLoc, " invalid local index. function has " )
804805 << locals.size () << " accessible locals, received index " << *id;
805806 return {{builder.create <LocalGetOp>(instLoc, locals[*id]).getResult ()}};
806807}
807808
808809template <>
809810inline parsed_inst_t ExpressionParser::parseSpecificInstruction<
810811 WasmBinaryEncoding::OpCode::globalGet>(OpBuilder &builder) {
811- auto id = parser.parseLiteral <uint32_t >();
812- auto instLoc = *currentOpLoc;
812+ FailureOr< uint32_t > id = parser.parseLiteral <uint32_t >();
813+ Location instLoc = *currentOpLoc;
813814 if (failed (id))
814815 return failure ();
815816 if (*id >= symbols.globalSymbols .size ())
816- return emitError (instLoc, " Invalid global index. Function has " )
817+ return emitError (instLoc, " invalid global index. function has " )
817818 << symbols.globalSymbols .size ()
818819 << " accessible globals, received index " << *id;
819- auto globalVar = symbols.globalSymbols [*id];
820+ GlobalSymbolRefContainer globalVar = symbols.globalSymbols [*id];
820821 auto globalOp = builder.create <GlobalGetOp>(instLoc, globalVar.globalType ,
821822 globalVar.symbol );
822823
@@ -825,16 +826,16 @@ inline parsed_inst_t ExpressionParser::parseSpecificInstruction<
825826
826827template <typename OpToCreate>
827828parsed_inst_t ExpressionParser::parseSetOrTee (OpBuilder &builder) {
828- auto id = parser.parseLiteral <uint32_t >();
829+ FailureOr< uint32_t > id = parser.parseLiteral <uint32_t >();
829830 if (failed (id))
830831 return failure ();
831832 if (*id >= locals.size ())
832- return emitError (*currentOpLoc, " Invalid local index. Function has " )
833+ return emitError (*currentOpLoc, " invalid local index. function has " )
833834 << locals.size () << " accessible locals, received index " << *id;
834835 if (valueStack.empty ())
835836 return emitError (
836837 *currentOpLoc,
837- " Invalid stack access, trying to access a value on an empty stack." );
838+ " invalid stack access, trying to access a value on an empty stack." );
838839
839840 parsed_inst_t poppedOp = popOperands (locals[*id].getType ().getElementType ());
840841 if (failed (poppedOp))
@@ -953,7 +954,7 @@ inline parsed_inst_t ExpressionParser::buildNumericOp(
953954 auto ty = buildLiteralType<valueType>(builder);
954955 LLVM_DEBUG (llvm::dbgs () << " *** buildNumericOp: numOperands = " << numOperands
955956 << " , type = " << ty << " ***\n " );
956- auto tysToPop = llvm:: SmallVector<Type, numOperands>();
957+ auto tysToPop = SmallVector<Type, numOperands>();
957958 tysToPop.resize (numOperands);
958959 std::fill (tysToPop.begin (), tysToPop.end (), ty);
959960 auto operands = popOperands (tysToPop);
@@ -1133,7 +1134,7 @@ class WasmBinaryParser {
11331134 if (failed (nElemsParsed))
11341135 return failure ();
11351136 uint32_t nElems = *nElemsParsed;
1136- LDBG () << " Starting to parse " << nElems << " items for section "
1137+ LDBG () << " starting to parse " << nElems << " items for section "
11371138 << secName;
11381139 for (size_t i = 0 ; i < nElems; ++i) {
11391140 if (failed (parseSectionItem<section>(ph, i)))
@@ -1232,7 +1233,7 @@ class WasmBinaryParser {
12321233 return ;
12331234 if (version->compare (expectedVersionString)) {
12341235 emitError (versionLoc,
1235- " unsupported Wasm version. Only version 1 is supported. " );
1236+ " unsupported Wasm version. only version 1 is supported" );
12361237 return ;
12371238 }
12381239 LogicalResult fillRegistry = registry.populateFromBody (parser.copy ());
@@ -1263,11 +1264,11 @@ class WasmBinaryParser {
12631264 if (failed (parsingMems))
12641265 return ;
12651266
1266- auto parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
1267+ LogicalResult parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
12671268 if (failed (parsingGlobals))
12681269 return ;
12691270
1270- auto parsingCode = parseSection<WasmSectionType::CODE>();
1271+ LogicalResult parsingCode = parseSection<WasmSectionType::CODE>();
12711272 if (failed (parsingCode))
12721273 return ;
12731274
@@ -1469,21 +1470,21 @@ template <>
14691470LogicalResult
14701471WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
14711472 size_t ) {
1472- auto globalLocation = ph.getLocation ();
1473+ FileLineColLoc globalLocation = ph.getLocation ();
14731474 auto globalTypeParsed = ph.parseGlobalType (ctx);
14741475 if (failed (globalTypeParsed))
14751476 return failure ();
14761477
1477- auto globalType = *globalTypeParsed;
1478+ GlobalTypeRecord globalType = *globalTypeParsed;
14781479 auto symbol = builder.getStringAttr (symbols.getNewGlobalSymbolName ());
14791480 auto globalOp = builder.create <wasmssa::GlobalOp>(
14801481 globalLocation, symbol, globalType.type , globalType.isMutable );
14811482 symbols.globalSymbols .push_back (
14821483 {{FlatSymbolRefAttr::get (globalOp)}, globalOp.getType ()});
14831484 auto ip = builder.saveInsertionPoint ();
1484- auto *block = builder.createBlock (&globalOp.getInitializer ());
1485+ Block *block = builder.createBlock (&globalOp.getInitializer ());
14851486 builder.setInsertionPointToStart (block);
1486- auto expr = ph.parseExpression (builder, symbols);
1487+ parsed_inst_t expr = ph.parseExpression (builder, symbols);
14871488 if (failed (expr))
14881489 return failure ();
14891490 if (block->empty ())
@@ -1500,10 +1501,10 @@ WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
15001501template <>
15011502LogicalResult WasmBinaryParser::parseSectionItem<WasmSectionType::CODE>(
15021503 ParserHead &ph, size_t innerFunctionId) {
1503- auto funcId = innerFunctionId + firstInternalFuncID;
1504- auto symRef = symbols.funcSymbols [funcId];
1504+ unsigned long funcId = innerFunctionId + firstInternalFuncID;
1505+ FunctionSymbolRefContainer symRef = symbols.funcSymbols [funcId];
15051506 auto funcOp =
1506- llvm:: dyn_cast<FuncOp>(SymbolTable::lookupSymbolIn (mOp , symRef.symbol ));
1507+ dyn_cast<FuncOp>(SymbolTable::lookupSymbolIn (mOp , symRef.symbol ));
15071508 assert (funcOp);
15081509 if (failed (ph.parseCodeFor (funcOp, symbols)))
15091510 return failure ();
0 commit comments