From 2049ee82d56f8cb1bb0107c9e260663e97bc4c17 Mon Sep 17 00:00:00 2001 From: Ashley Nelson Date: Thu, 7 Dec 2023 02:15:40 -0800 Subject: [PATCH] [Outlining] Add loop instruction support Adds support for the loop instruction to be outlined and a test showing a repeat loop being outlined. Reviewers: tlively Reviewed By: tlively Pull Request: https://github.com/WebAssembly/binaryen/pull/6141 --- src/passes/Outlining.cpp | 3 +++ test/lit/passes/outlining.wast | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/passes/Outlining.cpp b/src/passes/Outlining.cpp index 433889d3772..345b3f9a2da 100644 --- a/src/passes/Outlining.cpp +++ b/src/passes/Outlining.cpp @@ -103,6 +103,9 @@ struct ReconstructStringifyWalker } else if (reason.getElseStart()) { ASSERT_OK(existingBuilder.visitElse()); DBG(desc = "Else Start at "); + } else if (auto curr = reason.getLoopStart()) { + ASSERT_OK(existingBuilder.visitLoopStart(curr->loop)); + DBG(desc = "Loop Start at "); } else if (reason.getEnd()) { ASSERT_OK(existingBuilder.visitEnd()); // Outlining performs an unnested walk of the Wasm module, visiting diff --git a/test/lit/passes/outlining.wast b/test/lit/passes/outlining.wast index 98cb5a31670..1c5ec06bdc3 100644 --- a/test/lit/passes/outlining.wast +++ b/test/lit/passes/outlining.wast @@ -726,3 +726,40 @@ ;; CHECK-NEXT: (call $outline$) ;; CHECK-NEXT: (call $outline$) ;; CHECK-NEXT: ) + +;; Outline a loop +;; TODO: Ideally, a loop (like any control flow) repeated within a program can +;; be outlined by itself. Right now this is not possible since a control flow +;; is represented by a single symbol and only sequences of symbols >= 2 are +;; candidates for outlining. +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (func $outline$ (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (loop $loop-in + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + + ;; CHECK: (func $a (type $0) + ;; CHECK-NEXT: (call $outline$) + ;; CHECK-NEXT: ) + (func $a + (drop + (i32.const 0) + ) + (loop (nop)) + ) + ;; CHECK: (func $b (type $0) + ;; CHECK-NEXT: (call $outline$) + ;; CHECK-NEXT: ) + (func $b + (drop + (i32.const 0) + ) + (loop (nop)) + ) +)