2016-03-09 10:01:14 +08:00
|
|
|
//=- WebAssemblyFixIrreducibleControlFlow.cpp - Fix irreducible control flow -//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-03-09 10:01:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// This file implements a pass that removes irreducible control flow.
|
|
|
|
/// Irreducible control flow means multiple-entry loops, which this pass
|
|
|
|
/// transforms to have a single entry.
|
2016-03-09 10:01:14 +08:00
|
|
|
///
|
2016-03-09 12:17:36 +08:00
|
|
|
/// Note that LLVM has a generic pass that lowers irreducible control flow, but
|
|
|
|
/// it linearizes control flow, turning diamonds into two triangles, which is
|
|
|
|
/// both unnecessary and undesirable for WebAssembly.
|
|
|
|
///
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// The big picture: We recursively process each "region", defined as a group
|
|
|
|
/// of blocks with a single entry and no branches back to that entry. A region
|
|
|
|
/// may be the entire function body, or the inner part of a loop, i.e., the
|
|
|
|
/// loop's body without branches back to the loop entry. In each region we fix
|
|
|
|
/// up multi-entry loops by adding a new block that can dispatch to each of the
|
|
|
|
/// loop entries, based on the value of a label "helper" variable, and we
|
|
|
|
/// replace direct branches to the entries with assignments to the label
|
|
|
|
/// variable and a branch to the dispatch block. Then the dispatch block is the
|
|
|
|
/// single entry in the loop containing the previous multiple entries. After
|
|
|
|
/// ensuring all the loops in a region are reducible, we recurse into them. The
|
|
|
|
/// total time complexity of this pass is:
|
2019-03-28 04:12:42 +08:00
|
|
|
///
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// O(NumBlocks * NumNestedLoops * NumIrreducibleLoops +
|
|
|
|
/// NumLoops * NumLoops)
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
///
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// This pass is similar to what the Relooper [1] does. Both identify looping
|
|
|
|
/// code that requires multiple entries, and resolve it in a similar way (in
|
|
|
|
/// Relooper terminology, we implement a Multiple shape in a Loop shape). Note
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
/// also that like the Relooper, we implement a "minimal" intervention: we only
|
|
|
|
/// use the "label" helper for the blocks we absolutely must and no others. We
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// also prioritize code size and do not duplicate code in order to resolve
|
|
|
|
/// irreducibility. The graph algorithms for finding loops and entries and so
|
|
|
|
/// forth are also similar to the Relooper. The main differences between this
|
|
|
|
/// pass and the Relooper are:
|
2019-03-28 04:12:42 +08:00
|
|
|
///
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
/// * We just care about irreducibility, so we just look at loops.
|
|
|
|
/// * The Relooper emits structured control flow (with ifs etc.), while we
|
|
|
|
/// emit a CFG.
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
///
|
|
|
|
/// [1] Alon Zakai. 2011. Emscripten: an LLVM-to-JavaScript compiler. In
|
|
|
|
/// Proceedings of the ACM international conference companion on Object oriented
|
|
|
|
/// programming systems languages and applications companion (SPLASH '11). ACM,
|
|
|
|
/// New York, NY, USA, 301-312. DOI=10.1145/2048147.2048224
|
|
|
|
/// http://doi.acm.org/10.1145/2048147.2048224
|
2016-03-09 10:01:14 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2016-03-09 10:01:14 +08:00
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2019-10-19 09:31:09 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-03-09 10:01:14 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-fix-irreducible-control-flow"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
using BlockVector = SmallVector<MachineBasicBlock *, 4>;
|
|
|
|
using BlockSet = SmallPtrSet<MachineBasicBlock *, 4>;
|
|
|
|
|
[WebAssembly] Fix a non-determinism problem in FixIrreducibleControlFlow
Summary:
We already sorted the blocks when fixing up a set of mutual
loop entries, however, there can be multiple sets of such
mutual loop entries, and the order we encounter them
should not be random, so sort them too.
Fixes https://bugs.llvm.org/show_bug.cgi?id=44982
Patch by Alon Zakai (kripken)
Reviewers: aheejin, sbc100, dschuff
Subscribers: mgrang, sunfish, hiraditya, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74999
2020-02-22 09:01:52 +08:00
|
|
|
static BlockVector getSortedEntries(const BlockSet &Entries) {
|
|
|
|
BlockVector SortedEntries(Entries.begin(), Entries.end());
|
|
|
|
llvm::sort(SortedEntries,
|
|
|
|
[](const MachineBasicBlock *A, const MachineBasicBlock *B) {
|
|
|
|
auto ANum = A->getNumber();
|
|
|
|
auto BNum = B->getNumber();
|
|
|
|
return ANum < BNum;
|
|
|
|
});
|
|
|
|
return SortedEntries;
|
|
|
|
}
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Calculates reachability in a region. Ignores branches to blocks outside of
|
|
|
|
// the region, and ignores branches to the region entry (for the case where
|
|
|
|
// the region is the inner part of a loop).
|
|
|
|
class ReachabilityGraph {
|
2016-03-09 10:01:14 +08:00
|
|
|
public:
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
ReachabilityGraph(MachineBasicBlock *Entry, const BlockSet &Blocks)
|
|
|
|
: Entry(Entry), Blocks(Blocks) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// The region must have a single entry.
|
|
|
|
for (auto *MBB : Blocks) {
|
|
|
|
if (MBB != Entry) {
|
|
|
|
for (auto *Pred : MBB->predecessors()) {
|
|
|
|
assert(inRegion(Pred));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
calculate();
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:26:33 +08:00
|
|
|
bool canReach(MachineBasicBlock *From, MachineBasicBlock *To) const {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
assert(inRegion(From) && inRegion(To));
|
2019-03-19 13:26:33 +08:00
|
|
|
auto I = Reachable.find(From);
|
|
|
|
if (I == Reachable.end())
|
|
|
|
return false;
|
|
|
|
return I->second.count(To);
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// "Loopers" are blocks that are in a loop. We detect these by finding blocks
|
|
|
|
// that can reach themselves.
|
2019-03-19 13:26:33 +08:00
|
|
|
const BlockSet &getLoopers() const { return Loopers; }
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
|
|
|
|
// Get all blocks that are loop entries.
|
2019-03-19 13:26:33 +08:00
|
|
|
const BlockSet &getLoopEntries() const { return LoopEntries; }
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
|
|
|
|
// Get all blocks that enter a particular loop from outside.
|
2019-03-19 13:26:33 +08:00
|
|
|
const BlockSet &getLoopEnterers(MachineBasicBlock *LoopEntry) const {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
assert(inRegion(LoopEntry));
|
2019-03-19 13:26:33 +08:00
|
|
|
auto I = LoopEnterers.find(LoopEntry);
|
|
|
|
assert(I != LoopEnterers.end());
|
|
|
|
return I->second;
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
|
|
|
|
private:
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
MachineBasicBlock *Entry;
|
|
|
|
const BlockSet &Blocks;
|
|
|
|
|
|
|
|
BlockSet Loopers, LoopEntries;
|
|
|
|
DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
|
2019-03-19 13:26:33 +08:00
|
|
|
bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Maps a block to all the other blocks it can reach.
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
DenseMap<MachineBasicBlock *, BlockSet> Reachable;
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
void calculate() {
|
|
|
|
// Reachability computation work list. Contains pairs of recent additions
|
|
|
|
// (A, B) where we just added a link A => B.
|
|
|
|
using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
|
|
|
|
SmallVector<BlockPair, 4> WorkList;
|
|
|
|
|
|
|
|
// Add all relevant direct branches.
|
|
|
|
for (auto *MBB : Blocks) {
|
|
|
|
for (auto *Succ : MBB->successors()) {
|
|
|
|
if (Succ != Entry && inRegion(Succ)) {
|
|
|
|
Reachable[MBB].insert(Succ);
|
|
|
|
WorkList.emplace_back(MBB, Succ);
|
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
while (!WorkList.empty()) {
|
|
|
|
MachineBasicBlock *MBB, *Succ;
|
|
|
|
std::tie(MBB, Succ) = WorkList.pop_back_val();
|
|
|
|
assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
|
|
|
|
if (MBB != Entry) {
|
|
|
|
// We recently added MBB => Succ, and that means we may have enabled
|
|
|
|
// Pred => MBB => Succ.
|
|
|
|
for (auto *Pred : MBB->predecessors()) {
|
|
|
|
if (Reachable[Pred].insert(Succ).second) {
|
|
|
|
WorkList.emplace_back(Pred, Succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Blocks that can return to themselves are in a loop.
|
|
|
|
for (auto *MBB : Blocks) {
|
|
|
|
if (canReach(MBB, MBB)) {
|
|
|
|
Loopers.insert(MBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!Loopers.count(Entry));
|
|
|
|
|
|
|
|
// Find the loop entries - loopers reachable from blocks not in that loop -
|
|
|
|
// and those outside blocks that reach them, the "loop enterers".
|
|
|
|
for (auto *Looper : Loopers) {
|
|
|
|
for (auto *Pred : Looper->predecessors()) {
|
|
|
|
// Pred can reach Looper. If Looper can reach Pred, it is in the loop;
|
|
|
|
// otherwise, it is a block that enters into the loop.
|
|
|
|
if (!canReach(Looper, Pred)) {
|
|
|
|
LoopEntries.insert(Looper);
|
|
|
|
LoopEnterers[Looper].insert(Pred);
|
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
};
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Finds the blocks in a single-entry loop, given the loop entry and the
|
|
|
|
// list of blocks that enter the loop.
|
|
|
|
class LoopBlocks {
|
|
|
|
public:
|
|
|
|
LoopBlocks(MachineBasicBlock *Entry, const BlockSet &Enterers)
|
|
|
|
: Entry(Entry), Enterers(Enterers) {
|
|
|
|
calculate();
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
BlockSet &getBlocks() { return Blocks; }
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
private:
|
|
|
|
MachineBasicBlock *Entry;
|
|
|
|
const BlockSet &Enterers;
|
|
|
|
|
|
|
|
BlockSet Blocks;
|
|
|
|
|
|
|
|
void calculate() {
|
|
|
|
// Going backwards from the loop entry, if we ignore the blocks entering
|
|
|
|
// from outside, we will traverse all the blocks in the loop.
|
|
|
|
BlockVector WorkList;
|
|
|
|
BlockSet AddedToWorkList;
|
|
|
|
Blocks.insert(Entry);
|
|
|
|
for (auto *Pred : Entry->predecessors()) {
|
|
|
|
if (!Enterers.count(Pred)) {
|
|
|
|
WorkList.push_back(Pred);
|
|
|
|
AddedToWorkList.insert(Pred);
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
while (!WorkList.empty()) {
|
|
|
|
auto *MBB = WorkList.pop_back_val();
|
|
|
|
assert(!Enterers.count(MBB));
|
|
|
|
if (Blocks.insert(MBB).second) {
|
|
|
|
for (auto *Pred : MBB->predecessors()) {
|
|
|
|
if (!AddedToWorkList.count(Pred)) {
|
|
|
|
WorkList.push_back(Pred);
|
|
|
|
AddedToWorkList.insert(Pred);
|
|
|
|
}
|
|
|
|
}
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
};
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
class WebAssemblyFixIrreducibleControlFlow final : public MachineFunctionPass {
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "WebAssembly Fix Irreducible Control Flow";
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
bool processRegion(MachineBasicBlock *Entry, BlockSet &Blocks,
|
|
|
|
MachineFunction &MF);
|
|
|
|
|
|
|
|
void makeSingleEntryLoop(BlockSet &Entries, BlockSet &Blocks,
|
2019-03-30 09:31:11 +08:00
|
|
|
MachineFunction &MF, const ReachabilityGraph &Graph);
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyFixIrreducibleControlFlow() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool WebAssemblyFixIrreducibleControlFlow::processRegion(
|
|
|
|
MachineBasicBlock *Entry, BlockSet &Blocks, MachineFunction &MF) {
|
|
|
|
bool Changed = false;
|
|
|
|
// Remove irreducibility before processing child loops, which may take
|
|
|
|
// multiple iterations.
|
|
|
|
while (true) {
|
|
|
|
ReachabilityGraph Graph(Entry, Blocks);
|
|
|
|
|
|
|
|
bool FoundIrreducibility = false;
|
|
|
|
|
[WebAssembly] Fix a non-determinism problem in FixIrreducibleControlFlow
Summary:
We already sorted the blocks when fixing up a set of mutual
loop entries, however, there can be multiple sets of such
mutual loop entries, and the order we encounter them
should not be random, so sort them too.
Fixes https://bugs.llvm.org/show_bug.cgi?id=44982
Patch by Alon Zakai (kripken)
Reviewers: aheejin, sbc100, dschuff
Subscribers: mgrang, sunfish, hiraditya, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74999
2020-02-22 09:01:52 +08:00
|
|
|
for (auto *LoopEntry : getSortedEntries(Graph.getLoopEntries())) {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Find mutual entries - all entries which can reach this one, and
|
|
|
|
// are reached by it (that always includes LoopEntry itself). All mutual
|
|
|
|
// entries must be in the same loop, so if we have more than one, then we
|
|
|
|
// have irreducible control flow.
|
|
|
|
//
|
[WebAssembly] Fix a non-determinism problem in FixIrreducibleControlFlow
Summary:
We already sorted the blocks when fixing up a set of mutual
loop entries, however, there can be multiple sets of such
mutual loop entries, and the order we encounter them
should not be random, so sort them too.
Fixes https://bugs.llvm.org/show_bug.cgi?id=44982
Patch by Alon Zakai (kripken)
Reviewers: aheejin, sbc100, dschuff
Subscribers: mgrang, sunfish, hiraditya, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74999
2020-02-22 09:01:52 +08:00
|
|
|
// (Note that we need to sort the entries here, as otherwise the order can
|
|
|
|
// matter: being mutual is a symmetric relationship, and each set of
|
|
|
|
// mutuals will be handled properly no matter which we see first. However,
|
|
|
|
// there can be multiple disjoint sets of mutuals, and which we process
|
|
|
|
// first changes the output.)
|
|
|
|
//
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Note that irreducibility may involve inner loops, e.g. imagine A
|
|
|
|
// starts one loop, and it has B inside it which starts an inner loop.
|
|
|
|
// If we add a branch from all the way on the outside to B, then in a
|
|
|
|
// sense B is no longer an "inner" loop, semantically speaking. We will
|
|
|
|
// fix that irreducibility by adding a block that dispatches to either
|
|
|
|
// either A or B, so B will no longer be an inner loop in our output.
|
|
|
|
// (A fancier approach might try to keep it as such.)
|
|
|
|
//
|
|
|
|
// Note that we still need to recurse into inner loops later, to handle
|
|
|
|
// the case where the irreducibility is entirely nested - we would not
|
|
|
|
// be able to identify that at this point, since the enclosing loop is
|
|
|
|
// a group of blocks all of whom can reach each other. (We'll see the
|
|
|
|
// irreducibility after removing branches to the top of that enclosing
|
|
|
|
// loop.)
|
|
|
|
BlockSet MutualLoopEntries;
|
|
|
|
MutualLoopEntries.insert(LoopEntry);
|
|
|
|
for (auto *OtherLoopEntry : Graph.getLoopEntries()) {
|
|
|
|
if (OtherLoopEntry != LoopEntry &&
|
|
|
|
Graph.canReach(LoopEntry, OtherLoopEntry) &&
|
|
|
|
Graph.canReach(OtherLoopEntry, LoopEntry)) {
|
|
|
|
MutualLoopEntries.insert(OtherLoopEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MutualLoopEntries.size() > 1) {
|
2019-03-30 09:31:11 +08:00
|
|
|
makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
FoundIrreducibility = true;
|
|
|
|
Changed = true;
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
break;
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
|
|
|
}
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Only go on to actually process the inner loops when we are done
|
|
|
|
// removing irreducible control flow and changing the graph. Modifying
|
|
|
|
// the graph as we go is possible, and that might let us avoid looking at
|
|
|
|
// the already-fixed loops again if we are careful, but all that is
|
|
|
|
// complex and bug-prone. Since irreducible loops are rare, just starting
|
|
|
|
// another iteration is best.
|
|
|
|
if (FoundIrreducibility) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *LoopEntry : Graph.getLoopEntries()) {
|
|
|
|
LoopBlocks InnerBlocks(LoopEntry, Graph.getLoopEnterers(LoopEntry));
|
|
|
|
// Each of these calls to processRegion may change the graph, but are
|
|
|
|
// guaranteed not to interfere with each other. The only changes we make
|
|
|
|
// to the graph are to add blocks on the way to a loop entry. As the
|
|
|
|
// loops are disjoint, that means we may only alter branches that exit
|
|
|
|
// another loop, which are ignored when recursing into that other loop
|
|
|
|
// anyhow.
|
|
|
|
if (processRegion(LoopEntry, InnerBlocks.getBlocks(), MF)) {
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Given a set of entries to a single loop, create a single entry for that
|
|
|
|
// loop by creating a dispatch block for them, routing control flow using
|
|
|
|
// a helper variable. Also updates Blocks with any new blocks created, so
|
2019-03-30 09:31:11 +08:00
|
|
|
// that we properly track all the blocks in the region. But this does not update
|
|
|
|
// ReachabilityGraph; this will be updated in the caller of this function as
|
|
|
|
// needed.
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
|
2019-03-30 09:31:11 +08:00
|
|
|
BlockSet &Entries, BlockSet &Blocks, MachineFunction &MF,
|
|
|
|
const ReachabilityGraph &Graph) {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
assert(Entries.size() >= 2);
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
// Sort the entries to ensure a deterministic build.
|
[WebAssembly] Fix a non-determinism problem in FixIrreducibleControlFlow
Summary:
We already sorted the blocks when fixing up a set of mutual
loop entries, however, there can be multiple sets of such
mutual loop entries, and the order we encounter them
should not be random, so sort them too.
Fixes https://bugs.llvm.org/show_bug.cgi?id=44982
Patch by Alon Zakai (kripken)
Reviewers: aheejin, sbc100, dschuff
Subscribers: mgrang, sunfish, hiraditya, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74999
2020-02-22 09:01:52 +08:00
|
|
|
BlockVector SortedEntries = getSortedEntries(Entries);
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
|
2019-01-04 14:49:24 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
for (auto Block : SortedEntries)
|
|
|
|
assert(Block->getNumber() != -1);
|
|
|
|
if (SortedEntries.size() > 1) {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
for (auto I = SortedEntries.begin(), E = SortedEntries.end() - 1; I != E;
|
|
|
|
++I) {
|
2019-01-04 14:49:24 +08:00
|
|
|
auto ANum = (*I)->getNumber();
|
|
|
|
auto BNum = (*(std::next(I)))->getNumber();
|
|
|
|
assert(ANum != BNum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
// Create a dispatch block which will contain a jump table to the entries.
|
2016-03-09 10:01:14 +08:00
|
|
|
MachineBasicBlock *Dispatch = MF.CreateMachineBasicBlock();
|
|
|
|
MF.insert(MF.end(), Dispatch);
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
Blocks.insert(Dispatch);
|
2016-03-09 10:01:14 +08:00
|
|
|
|
|
|
|
// Add the jump table.
|
|
|
|
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
2019-03-19 13:26:33 +08:00
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(Dispatch, DebugLoc(), TII.get(WebAssembly::BR_TABLE_I32));
|
2016-03-09 10:01:14 +08:00
|
|
|
|
|
|
|
// Add the register which will be used to tell the jump table which block to
|
|
|
|
// jump to.
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 06:40:45 +08:00
|
|
|
Register Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
2016-03-09 10:01:14 +08:00
|
|
|
MIB.addReg(Reg);
|
|
|
|
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
// Compute the indices in the superheader, one for each bad block, and
|
|
|
|
// add them as successors.
|
2016-03-09 10:01:14 +08:00
|
|
|
DenseMap<MachineBasicBlock *, unsigned> Indices;
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
for (auto *Entry : SortedEntries) {
|
|
|
|
auto Pair = Indices.insert(std::make_pair(Entry, 0));
|
|
|
|
assert(Pair.second);
|
2016-03-09 10:01:14 +08:00
|
|
|
|
2016-04-26 09:40:56 +08:00
|
|
|
unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;
|
2016-03-09 10:01:14 +08:00
|
|
|
Pair.first->second = Index;
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
MIB.addMBB(Entry);
|
|
|
|
Dispatch->addSuccessor(Entry);
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Rewrite the problematic successors for every block that wants to reach
|
|
|
|
// the bad blocks. For simplicity, we just introduce a new block for every
|
|
|
|
// edge we need to rewrite. (Fancier things are possible.)
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
BlockVector AllPreds;
|
|
|
|
for (auto *Entry : SortedEntries) {
|
|
|
|
for (auto *Pred : Entry->predecessors()) {
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
if (Pred != Dispatch) {
|
|
|
|
AllPreds.push_back(Pred);
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 09:31:11 +08:00
|
|
|
// This set stores predecessors within this loop.
|
|
|
|
DenseSet<MachineBasicBlock *> InLoop;
|
|
|
|
for (auto *Pred : AllPreds) {
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
for (auto *Entry : Pred->successors()) {
|
2019-03-30 09:31:11 +08:00
|
|
|
if (!Entries.count(Entry))
|
2016-03-09 10:01:14 +08:00
|
|
|
continue;
|
2019-03-30 09:31:11 +08:00
|
|
|
if (Graph.canReach(Entry, Pred)) {
|
|
|
|
InLoop.insert(Pred);
|
|
|
|
break;
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
}
|
2019-03-30 09:31:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record if each entry has a layout predecessor. This map stores
|
|
|
|
// <<Predecessor is within the loop?, loop entry>, layout predecessor>
|
|
|
|
std::map<std::pair<bool, MachineBasicBlock *>, MachineBasicBlock *>
|
|
|
|
EntryToLayoutPred;
|
|
|
|
for (auto *Pred : AllPreds)
|
|
|
|
for (auto *Entry : Pred->successors())
|
|
|
|
if (Entries.count(Entry) && Pred->isLayoutSuccessor(Entry))
|
|
|
|
EntryToLayoutPred[std::make_pair(InLoop.count(Pred), Entry)] = Pred;
|
|
|
|
|
|
|
|
// We need to create at most two routing blocks per entry: one for
|
|
|
|
// predecessors outside the loop and one for predecessors inside the loop.
|
|
|
|
// This map stores
|
|
|
|
// <<Predecessor is within the loop?, loop entry>, routing block>
|
|
|
|
std::map<std::pair<bool, MachineBasicBlock *>, MachineBasicBlock *> Map;
|
|
|
|
for (auto *Pred : AllPreds) {
|
|
|
|
bool PredInLoop = InLoop.count(Pred);
|
|
|
|
for (auto *Entry : Pred->successors()) {
|
|
|
|
if (!Entries.count(Entry) ||
|
|
|
|
Map.count(std::make_pair(InLoop.count(Pred), Entry)))
|
|
|
|
continue;
|
|
|
|
// If there exists a layout predecessor of this entry and this predecessor
|
|
|
|
// is not that, we rather create a routing block after that layout
|
|
|
|
// predecessor to save a branch.
|
|
|
|
if (EntryToLayoutPred.count(std::make_pair(PredInLoop, Entry)) &&
|
|
|
|
EntryToLayoutPred[std::make_pair(PredInLoop, Entry)] != Pred)
|
|
|
|
continue;
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
// This is a successor we need to rewrite.
|
2019-03-30 09:31:11 +08:00
|
|
|
MachineBasicBlock *Routing = MF.CreateMachineBasicBlock();
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
MF.insert(Pred->isLayoutSuccessor(Entry)
|
|
|
|
? MachineFunction::iterator(Entry)
|
|
|
|
: MF.end(),
|
2019-03-30 09:31:11 +08:00
|
|
|
Routing);
|
|
|
|
Blocks.insert(Routing);
|
2016-03-09 10:01:14 +08:00
|
|
|
|
|
|
|
// Set the jump table's register of the index of the block we wish to
|
|
|
|
// jump to, and jump to the jump table.
|
2019-03-30 09:31:11 +08:00
|
|
|
BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::CONST_I32), Reg)
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
.addImm(Indices[Entry]);
|
2019-03-30 09:31:11 +08:00
|
|
|
BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::BR)).addMBB(Dispatch);
|
|
|
|
Routing->addSuccessor(Dispatch);
|
|
|
|
Map[std::make_pair(PredInLoop, Entry)] = Routing;
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
2019-03-30 09:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *Pred : AllPreds) {
|
|
|
|
bool PredInLoop = InLoop.count(Pred);
|
2016-03-09 10:01:14 +08:00
|
|
|
// Remap the terminator operands and the successor list.
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
for (MachineInstr &Term : Pred->terminators())
|
2016-03-09 10:01:14 +08:00
|
|
|
for (auto &Op : Term.explicit_uses())
|
|
|
|
if (Op.isMBB() && Indices.count(Op.getMBB()))
|
2019-03-30 09:31:11 +08:00
|
|
|
Op.setMBB(Map[std::make_pair(PredInLoop, Op.getMBB())]);
|
|
|
|
|
|
|
|
for (auto *Succ : Pred->successors()) {
|
|
|
|
if (!Entries.count(Succ))
|
|
|
|
continue;
|
|
|
|
auto *Routing = Map[std::make_pair(PredInLoop, Succ)];
|
|
|
|
Pred->replaceSuccessor(Succ, Routing);
|
|
|
|
}
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a fake default label, because br_table requires one.
|
|
|
|
MIB.addMBB(MIB.getInstr()
|
|
|
|
->getOperand(MIB.getInstr()->getNumExplicitOperands() - 1)
|
|
|
|
.getMBB());
|
|
|
|
}
|
|
|
|
|
[WebAssembly] Optimize Irreducible Control Flow
Summary:
Irreducible control flow is not that rare, e.g. it happens in malloc and
3 other places in the libc portions linked in to a hello world program.
This patch improves how we handle that code: it emits a br_table to
dispatch to only the minimal necessary number of blocks. This reduces
the size of malloc by 33%, and makes it comparable in size to asm2wasm's
malloc output.
Added some tests, and verified this passes the emscripten-wasm tests run
on the waterfall (binaryen2, wasmobj2, other).
Reviewers: aheejin, sunfish
Subscribers: mgrang, jgravelle-google, sbc100, dschuff, llvm-commits
Differential Revision: https://reviews.llvm.org/D55467
Patch by Alon Zakai (kripken)
llvm-svn: 350367
2019-01-04 07:10:11 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyFixIrreducibleControlFlow::ID = 0;
|
|
|
|
INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE,
|
|
|
|
"Removes irreducible control flow", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createWebAssemblyFixIrreducibleControlFlow() {
|
|
|
|
return new WebAssemblyFixIrreducibleControlFlow();
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:01:14 +08:00
|
|
|
bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
|
|
|
|
MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** Fixing Irreducible Control Flow **********\n"
|
|
|
|
"********** Function: "
|
|
|
|
<< MF.getName() << '\n');
|
2016-03-09 10:01:14 +08:00
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
// Start the recursive process on the entire function body.
|
|
|
|
BlockSet AllBlocks;
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
AllBlocks.insert(&MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LLVM_UNLIKELY(processRegion(&*MF.begin(), AllBlocks, MF))) {
|
|
|
|
// We rewrote part of the function; recompute relevant things.
|
2016-03-09 10:01:14 +08:00
|
|
|
MF.getRegInfo().invalidateLiveness();
|
|
|
|
MF.RenumberBlocks();
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
return true;
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|
|
|
|
|
[WebAssembly] Irreducible control flow rewrite
Summary:
Rewrite WebAssemblyFixIrreducibleControlFlow to a simpler and cleaner
design, which directly computes reachability and other properties
itself. This avoids previous complexity and bugs. (The new graph
analyses are very similar to how the Relooper algorithm would find loop
entries and so forth.)
This fixes a few bugs, including where we had a false positive and
thought fannkuch was irreducible when it was not, which made us much
larger and slower there, and a reverse bug where we missed
irreducibility. On fannkuch, we used to be 44% slower than asm2wasm and
are now 4% faster.
Reviewers: aheejin
Subscribers: jdoerfert, mgrang, dschuff, sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D58919
Patch by Alon Zakai (kripken)
llvm-svn: 356313
2019-03-16 11:00:19 +08:00
|
|
|
return false;
|
2016-03-09 10:01:14 +08:00
|
|
|
}
|