[Mlir-commits] [mlir] [mlir][wasm] Support for saturating FP truncations (PR #212709)

Max Graey llvmlistbot at llvm.org
Sat Aug 1 05:26:47 PDT 2026


================
@@ -415,98 +361,117 @@ class ExpressionParser {
   template <typename OpToCreate>
   parsed_inst_t parseSetOrTee(OpBuilder &);
 
-  /// Blocks and Loops have a similar format and differ only in how their exit
-  /// is handled which doesn´t matter at parsing time. Factorizes in one
-  /// function.
-  template <typename OpToCreate>
-  parsed_inst_t parseBlockLikeOp(OpBuilder &);
-
   Location getCurrentOpLoc() {
     assert(currentOpLoc.has_value() &&
            "expects current opcode location to be set");
     return *currentOpLoc;
   }
 
-  class TopLevelInstParserRegistry {
+  struct ExprParserProxy {
   public:
-    template <std::byte opCode>
-    static constexpr bool hasParserForOpcode = false;
+    friend ExpressionParser;
+    inline auto parseBlockFuncType(OpBuilder &builder) {
+      return exprParser.parseBlockFuncType(builder);
+    }
 
-    template <std::byte opCode>
-    static parsed_inst_t parseInstrWithOpCode(OpBuilder &,
-                                              ExpressionParser &) = delete;
-  };
+    template <typename FilterT = ByteSequence<WasmBinaryEncoding::endByte>>
+    /// @param blockToFill: the block which content will be populated
+    /// @param resType: the type that this block is supposed to return
+    llvm::FailureOr<std::byte>
+    parseBlockContent(OpBuilder &builder, Block *blockToFill,
+                      TypeRange resTypes, Location opLoc,
+                      LabelLevelOpInterface levelOp,
+                      FilterT parseEndBytes = {}) {
+      OpBuilder::InsertionGuard guard{builder};
+      builder.setInsertionPointToStart(blockToFill);
+      LDBG() << "parsing a block of type "
+             << builder.getFunctionType(blockToFill->getArgumentTypes(),
+                                        resTypes);
+      auto nC = exprParser.addNesting(levelOp);
----------------
MaxGraey wrote:

I think the cleanest approach would be to model label nesting and the operand stack frame as a single RAII object:

```cpp
auto frame =
    exprParser.enterBlock(levelOp, block->getArguments(), resultTypes);
```

The frame could record the initial stack height, register the label, track the unreachable state, prevent underflow past the block boundary, validate the result types and final stack height on `finish()`, and restore the parser state on early exit.

This would avoid manually coordinating `addNesting()`, `pushResults()`, and `popOperands()`. These operations are really one logical transaction, but the current API makes it possible to perform only part of it by accident.


https://github.com/llvm/llvm-project/pull/212709


More information about the Mlir-commits mailing list