Skip to content

Commit 6f6c527

Browse files
author
Luc Forget
committed
[MLIR][WASM] Make WasmSSA Importer signal program failure on error
Also contains non functional changes to use the `LDBG` macro.
1 parent 2cb8732 commit 6f6c527

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

mlir/lib/Target/Wasm/TranslateFromWasm.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/Target/Wasm/WasmImporter.h"
1616
#include "llvm/ADT/Statistic.h"
1717
#include "llvm/Support/Debug.h"
18+
#include "llvm/Support/DebugLog.h"
1819
#include "llvm/Support/FormatVariadic.h"
1920
#include "llvm/Support/LEB128.h"
2021

@@ -322,17 +323,16 @@ class ParserHead {
322323
}
323324

324325
FailureOr<StringRef> consumeNBytes(size_t nBytes) {
325-
LLVM_DEBUG(llvm::dbgs() << "Consume " << nBytes << " bytes\n");
326-
LLVM_DEBUG(llvm::dbgs() << " Bytes remaining: " << size() << "\n");
327-
LLVM_DEBUG(llvm::dbgs() << " Current offset: " << offset << "\n");
326+
LDBG() << "Consume " << nBytes << " bytes";
327+
LDBG() << " Bytes remaining: " << size();
328+
LDBG() << " Current offset: " << offset;
328329
if (nBytes > size())
329330
return emitError(getLocation(), "trying to extract ")
330331
<< nBytes << "bytes when only " << size() << "are avilables";
331332

332333
StringRef res = head.slice(offset, offset + nBytes);
333334
offset += nBytes;
334-
LLVM_DEBUG(llvm::dbgs()
335-
<< " Updated offset (+" << nBytes << "): " << offset << "\n");
335+
LDBG() << " Updated offset (+" << nBytes << "): " << offset;
336336
return res;
337337
}
338338

@@ -660,10 +660,9 @@ void ValueStack::dump() const {
660660
#endif
661661

662662
parsed_inst_t ValueStack::popOperands(TypeRange operandTypes, Location *opLoc) {
663-
LLVM_DEBUG(llvm::dbgs() << "Popping from ValueStack\n");
664-
LLVM_DEBUG(llvm::dbgs() << " Elements(s) to pop: " << operandTypes.size()
665-
<< "\n");
666-
LLVM_DEBUG(llvm::dbgs() << " Current stack size: " << values.size() << "\n");
663+
LDBG() << "Popping from ValueStack\n"
664+
<< " Elements(s) to pop: " << operandTypes.size() << "\n"
665+
<< " Current stack size: " << values.size();
667666
if (operandTypes.size() > values.size())
668667
return emitError(*opLoc,
669668
"stack doesn't contain enough values. Trying to get ")
@@ -679,28 +678,27 @@ parsed_inst_t ValueStack::popOperands(TypeRange operandTypes, Location *opLoc) {
679678
return emitError(*opLoc, "invalid operand type on stack. Expecting ")
680679
<< operandTypes[i] << ", value on stack is of type " << stackType
681680
<< ".";
682-
LLVM_DEBUG(llvm::dbgs() << " POP: " << operand << "\n");
681+
LDBG() << " POP: " << operand;
683682
res.push_back(operand);
684683
}
685684
values.resize(values.size() - operandTypes.size());
686-
LLVM_DEBUG(llvm::dbgs() << " Updated stack size: " << values.size() << "\n");
685+
LDBG() << " Updated stack size: " << values.size();
687686
return res;
688687
}
689688

690689
LogicalResult ValueStack::pushResults(ValueRange results, Location *opLoc) {
691-
LLVM_DEBUG(llvm::dbgs() << "Pushing to ValueStack\n");
692-
LLVM_DEBUG(llvm::dbgs() << " Elements(s) to push: " << results.size()
693-
<< "\n");
694-
LLVM_DEBUG(llvm::dbgs() << " Current stack size: " << values.size() << "\n");
690+
LDBG() << "Pushing to ValueStack\n"
691+
<< " Elements(s) to push: " << results.size() << "\n"
692+
<< " Current stack size: " << values.size();
695693
for (Value val : results) {
696694
if (!isWasmValueType(val.getType()))
697695
return emitError(*opLoc, "invalid value type on stack: ")
698696
<< val.getType();
699-
LLVM_DEBUG(llvm::dbgs() << " PUSH: " << val << "\n");
697+
LDBG() << " PUSH: " << val;
700698
values.push_back(val);
701699
}
702700

703-
LLVM_DEBUG(llvm::dbgs() << " Updated stack size: " << values.size() << "\n");
701+
LDBG() << " Updated stack size: " << values.size();
704702
return success();
705703
}
706704

@@ -913,7 +911,7 @@ class WasmBinaryParser {
913911
};
914912
auto secContent = registry.getContentForSection<section>();
915913
if (!secContent) {
916-
LLVM_DEBUG(llvm::dbgs() << secName << " section is not present in file.");
914+
LDBG() << secName << " section is not present in file.";
917915
return success();
918916
}
919917

@@ -923,8 +921,8 @@ class WasmBinaryParser {
923921
if (failed(nElemsParsed))
924922
return failure();
925923
uint32_t nElems = *nElemsParsed;
926-
LLVM_DEBUG(llvm::dbgs() << "Starting to parse " << nElems
927-
<< " items for section " << secName << ".\n");
924+
LDBG() << "Starting to parse " << nElems << " items for section "
925+
<< secName;
928926
for (size_t i = 0; i < nElems; ++i) {
929927
if (failed(parseSectionItem<section>(ph, i)))
930928
return failure();
@@ -984,9 +982,18 @@ class WasmBinaryParser {
984982
return giOp.verify();
985983
}
986984

985+
// Detect occurence of errors
986+
LogicalResult peekDiag(Diagnostic &diag) {
987+
if (diag.getSeverity() == DiagnosticSeverity::Error)
988+
isValid = false;
989+
return failure();
990+
}
991+
987992
public:
988993
WasmBinaryParser(llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
989994
: builder{ctx}, ctx{ctx} {
995+
ctx->getDiagEngine().registerHandler(
996+
[this](Diagnostic &diag) { return peekDiag(diag); });
990997
ctx->loadAllAvailableDialects();
991998
if (sourceMgr.getNumBuffers() != 1) {
992999
emitError(UnknownLoc::get(ctx), "one source file should be provided");
@@ -1055,7 +1062,11 @@ class WasmBinaryParser {
10551062
numTableSectionItems = symbols.tableSymbols.size();
10561063
}
10571064

1058-
ModuleOp getModule() { return mOp; }
1065+
ModuleOp getModule() {
1066+
if (isValid)
1067+
return mOp;
1068+
return ModuleOp{};
1069+
}
10591070

10601071
private:
10611072
mlir::StringAttr srcName;
@@ -1065,6 +1076,7 @@ class WasmBinaryParser {
10651076
ModuleOp mOp;
10661077
SectionRegistry registry;
10671078
size_t firstInternalFuncID{0};
1079+
bool isValid{true};
10681080
};
10691081

10701082
template <>
@@ -1168,8 +1180,7 @@ WasmBinaryParser::parseSectionItem<WasmSectionType::TABLE>(ParserHead &ph,
11681180
FailureOr<TableType> tableType = ph.parseTableType(ctx);
11691181
if (failed(tableType))
11701182
return failure();
1171-
LLVM_DEBUG(llvm::dbgs() << " Parsed table description: " << *tableType
1172-
<< '\n');
1183+
LDBG() << " Parsed table description: " << *tableType;
11731184
StringAttr symbol = builder.getStringAttr(symbols.getNewTableSymbolName());
11741185
auto tableOp =
11751186
builder.create<TableOp>(opLocation, symbol.strref(), *tableType);
@@ -1209,7 +1220,7 @@ WasmBinaryParser::parseSectionItem<WasmSectionType::TYPE>(ParserHead &ph,
12091220
FailureOr<FunctionType> funcType = ph.parseFunctionType(ctx);
12101221
if (failed(funcType))
12111222
return failure();
1212-
LLVM_DEBUG(llvm::dbgs() << "Parsed function type " << *funcType << '\n');
1223+
LDBG() << "Parsed function type " << *funcType;
12131224
symbols.moduleFuncTypes.push_back(*funcType);
12141225
return success();
12151226
}
@@ -1223,7 +1234,7 @@ WasmBinaryParser::parseSectionItem<WasmSectionType::MEMORY>(ParserHead &ph,
12231234
if (failed(memory))
12241235
return failure();
12251236

1226-
LLVM_DEBUG(llvm::dbgs() << " Registering memory " << *memory << '\n');
1237+
LDBG() << " Registering memory " << *memory;
12271238
std::string symbol = symbols.getNewMemorySymbolName();
12281239
auto memOp = builder.create<MemOp>(opLocation, symbol, *memory);
12291240
symbols.memSymbols.push_back({SymbolRefAttr::get(memOp)});

mlir/test/Target/Wasm/function_export_out_of_scope.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# RUN: yaml2obj %s | mlir-translate --import-wasm -o - 2>&1 | FileCheck %s
2-
3-
# FIXME: The error code here should be nonzero.
1+
# RUN: yaml2obj %s | not mlir-translate --import-wasm -o - 2>&1 | FileCheck %s
42

53
# CHECK: trying to export function 42 which is undefined in this scope
64

mlir/test/Target/Wasm/invalid_function_type_index.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# RUN: yaml2obj %s | mlir-translate --import-wasm -o - 2>&1 | FileCheck %s
1+
# RUN: yaml2obj %s | not mlir-translate --import-wasm -o - 2>&1 | FileCheck %s
22
# CHECK: error: invalid type index: 2
33

4-
# FIXME: mlir-translate should not return 0 here.
5-
64
--- !WASM
75
FileHeader:
86
Version: 0x00000001

0 commit comments

Comments
 (0)