Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d1979b2
start ssa pass
kripken May 23, 2017
9563cd2
wip
kripken May 23, 2017
8061320
more
kripken May 23, 2017
d4a94f2
fixes
kripken May 24, 2017
0b217e7
buid
kripken May 24, 2017
91dc4a7
start test
kripken May 24, 2017
9d9dd7c
more
kripken May 24, 2017
84e6dbb
more
kripken May 24, 2017
64346e9
more
kripken May 24, 2017
65039dc
debug
kripken May 24, 2017
0a80c1a
fix
kripken May 24, 2017
97e16e2
zero out preinits of locals
kripken May 24, 2017
9de1e59
test
kripken May 24, 2017
eb3d9c8
test
kripken May 24, 2017
829065d
test
kripken May 24, 2017
5593858
test
kripken May 24, 2017
8292fe1
more
kripken May 25, 2017
8789b3f
test
kripken May 25, 2017
b684a3d
fix
kripken May 25, 2017
c701932
more
kripken May 25, 2017
f83b830
loop
kripken May 25, 2017
682ebd6
loop fix
kripken May 25, 2017
0382f19
more
kripken May 25, 2017
5b3ee85
more
kripken May 25, 2017
493fd4f
Merge remote-tracking branch 'origin/master' into ssa2
kripken May 25, 2017
da9773e
fast
kripken May 25, 2017
c92c7a7
optimize
kripken May 26, 2017
98a8602
testcase
kripken May 26, 2017
d91d395
rework wip
kripken May 27, 2017
13b15a5
write up
kripken May 27, 2017
06dfc19
builds
kripken May 28, 2017
25a2717
fix
kripken May 29, 2017
400d5a8
Merge branch 'master' into ssa2
kripken Jun 7, 2017
7099cac
default output names in wasm-as/dis are the input name with modified …
kripken Jun 7, 2017
97e5645
add InstrumentLocals pass
kripken Jun 7, 2017
8f245f8
refactor
kripken Jun 8, 2017
5a7347c
add testcase showing a problem
kripken Jun 8, 2017
1b4f92a
merge loop join into loop fallthrough
kripken Jun 8, 2017
23dad7d
add testcase
kripken Jun 8, 2017
e6dbb10
add failing testcase
kripken Jun 9, 2017
9dbbca3
add another bad testcase
kripken Jun 9, 2017
6e47df8
fix ssa break loop updating
kripken Jun 9, 2017
d24ef3f
add function name validation to asm2wasm
kripken Jun 9, 2017
6678b59
do not optimize tees in pick-load-signs, it is invalid to do so
kripken Jun 10, 2017
4995ccb
Merge remote-tracking branch 'origin/master' into ssa2
kripken Jun 10, 2017
00c6fd0
add include
kripken Jun 10, 2017
67bc5c3
comment
kripken Jun 10, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/asm2wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,9 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
} else if (curr[0] == DEFUN) {
// function
auto* func = processFunction(curr);
if (wasm.getFunctionOrNull(func->name)) {
Fatal() << "duplicate function: " << func->name;
}
if (runOptimizationPasses) {
optimizingBuilder->addFunction(func);
} else {
Expand Down
46 changes: 46 additions & 0 deletions src/ast/literal-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef wasm_ast_literl_utils_h
#define wasm_ast_literl_utils_h

#include "wasm.h"

namespace wasm {

namespace LiteralUtils {

inline Expression* makeZero(WasmType type, Module& wasm) {
Literal value;
switch (type) {
case i32: value = Literal(int32_t(0)); break;
case i64: value = Literal(int64_t(0)); break;
case f32: value = Literal(float(0)); break;
case f64: value = Literal(double(0)); break;
default: WASM_UNREACHABLE();
}
auto* ret = wasm.allocator.alloc<Const>();
ret->value = value;
ret->type = value.type;
return ret;
}

} // namespace LiteralUtils

} // namespace wasm

#endif // wasm_ast_literl_utils_h

2 changes: 2 additions & 0 deletions src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SET(passes_SOURCES
LegalizeJSInterface.cpp
LocalCSE.cpp
LogExecution.cpp
InstrumentLocals.cpp
InstrumentMemory.cpp
MemoryPacking.cpp
MergeBlocks.cpp
Expand All @@ -32,6 +33,7 @@ SET(passes_SOURCES
ReorderLocals.cpp
ReorderFunctions.cpp
SimplifyLocals.cpp
SSAify.cpp
Vacuum.cpp
)
ADD_LIBRARY(passes STATIC ${passes_SOURCES})
30 changes: 1 addition & 29 deletions src/passes/CoalesceLocals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "cfg/cfg-traversal.h"
#include "wasm-builder.h"
#include "support/learning.h"
#include "support/permutations.h"
#ifdef CFG_PROFILE
#include "support/timing.h"
#endif
Expand Down Expand Up @@ -533,35 +534,6 @@ void CoalesceLocals::pickIndicesFromOrder(std::vector<Index>& order, std::vector
}
}

// Utilities for operating on permutation vectors

static std::vector<Index> makeIdentity(Index num) {
std::vector<Index> ret;
ret.resize(num);
for (Index i = 0; i < num; i++) {
ret[i] = i;
}
return ret;
}

static void setIdentity(std::vector<Index>& ret) {
auto num = ret.size();
assert(num > 0); // must already be of the right size
for (Index i = 0; i < num; i++) {
ret[i] = i;
}
}

static std::vector<Index> makeReversed(std::vector<Index>& original) {
std::vector<Index> ret;
auto num = original.size();
ret.resize(num);
for (Index i = 0; i < num; i++) {
ret[original[i]] = i;
}
return ret;
}

// given a baseline order, adjust it based on an important order of priorities (higher values
// are higher priority). The priorities take precedence, unless they are equal and then
// the original order should be kept.
Expand Down
140 changes: 140 additions & 0 deletions src/passes/InstrumentLocals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2017 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//
// Instruments the build with code to intercept all local reads and writes.
//
// gets:
//
// Before:
// (get_local $x)
//
// After:
// (call $get_TYPE
// (i32.const n) // call id
// (i32.const n) // local id
// (get_local $x)
// )
//
// sets:
//
// Before:
// (set_local $x (i32.const 1))
//
// After:
// (set_local $x
// (call $set_TYPE
// (i32.const n) // call id
// (i32.const n) // local id
// (i32.const 1) // value
// )
// )

#include <wasm.h>
#include <wasm-builder.h>
#include <pass.h>
#include "shared-constants.h"
#include "asmjs/shared-constants.h"
#include "asm_v_wasm.h"

namespace wasm {

Name get_i32("get_i32");
Name get_i64("get_i64");
Name get_f32("get_f32");
Name get_f64("get_f64");

Name set_i32("set_i32");
Name set_i64("set_i64");
Name set_f32("set_f32");
Name set_f64("set_f64");

struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
void visitGetLocal(GetLocal* curr) {
Builder builder(*getModule());
Name import;
switch (curr->type) {
case i32: import = get_i32; break;
case i64: return; // TODO
case f32: import = get_f32; break;
case f64: import = get_f64; break;
default: WASM_UNREACHABLE();
}
replaceCurrent(
builder.makeCallImport(
import,
{
builder.makeConst(Literal(int32_t(id++))),
builder.makeConst(Literal(int32_t(curr->index))),
curr
},
curr->type
)
);
}

void visitSetLocal(SetLocal* curr) {
Builder builder(*getModule());
Name import;
switch (curr->value->type) {
case i32: import = set_i32; break;
case i64: return; // TODO
case f32: import = set_f32; break;
case f64: import = set_f64; break;
case unreachable: return; // nothing to do here
default: WASM_UNREACHABLE();
}
curr->value = builder.makeCallImport(
import,
{
builder.makeConst(Literal(int32_t(id++))),
builder.makeConst(Literal(int32_t(curr->index))),
curr->value
},
curr->value->type
);
}

void visitModule(Module* curr) {
addImport(curr, get_i32, "iiii");
addImport(curr, get_i64, "jiij");
addImport(curr, get_f32, "fiif");
addImport(curr, get_f64, "diid");
addImport(curr, set_i32, "iiii");
addImport(curr, set_i64, "jiij");
addImport(curr, set_f32, "fiif");
addImport(curr, set_f64, "diid");
}

private:
Index id = 0;

void addImport(Module* wasm, Name name, std::string sig) {
auto import = new Import;
import->name = name;
import->module = INSTRUMENT;
import->base = name;
import->functionType = ensureFunctionType(sig, wasm)->name;
import->kind = ExternalKind::Function;
wasm->addImport(import);
}
};

Pass* createInstrumentLocalsPass() {
return new InstrumentLocals();
}

} // namespace wasm
4 changes: 4 additions & 0 deletions src/passes/PickLoadSigns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ struct PickLoadSigns : public WalkerPass<ExpressionStackWalker<PickLoadSigns>> {
}

void visitSetLocal(SetLocal* curr) {
if (curr->isTee()) {
// we can't modify a tee, the value is used elsewhere
return;
}
if (auto* load = curr->value->dynCast<Load>()) {
loads[load] = curr->index;
}
Expand Down
Loading