Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions src/ir/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 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_ir_debug_h
#define wasm_ir_debug_h

#include <wasm-traversal.h>

namespace wasm {

namespace debug {

// Given an expression and a copy of it in another function, copy the debug
// info into the second function.
inline void copyDebugInfo(Expression* origin,
Expression* copy,
Function* originFunc,
Function* copyFunc) {
struct Lister : public PostWalker<Lister, UnifiedExpressionVisitor<Lister>> {
std::vector<Expression*> list;
void visitExpression(Expression* curr) { list.push_back(curr); }
};

Lister originList;
originList.walk(origin);
Lister copyList;
copyList.walk(copy);

auto& originDebug = originFunc->debugLocations;
auto& copyDebug = copyFunc->debugLocations;

assert(originList.list.size() == copyList.list.size());
for (Index i = 0; i < originList.list.size(); i++) {
auto iter = originDebug.find(originList.list[i]);
if (iter != originDebug.end()) {
auto location = iter->second;
copyDebug[copyList.list[i]] = location;
}
}
};

} // namespace debug

} // namespace wasm

#endif // wasm_ir_debug_h
17 changes: 11 additions & 6 deletions src/passes/Inlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <atomic>

#include "ir/debug.h"
#include "ir/literal-utils.h"
#include "ir/module-utils.h"
#include "ir/utils.h"
Expand Down Expand Up @@ -175,7 +176,7 @@ doInlining(Module* module, Function* into, InliningAction& action) {
auto* block = Builder(*module).makeBlock();
block->name = Name(std::string("__inlined_func$") + from->name.str);
*action.callSite = block;
// set up a locals mapping
// Prepare to update the inlined code's locals and other things.
struct Updater : public PostWalker<Updater> {
std::map<Index, Index> localMapping;
Name returnName;
Expand All @@ -193,33 +194,37 @@ doInlining(Module* module, Function* into, InliningAction& action) {
} updater;
updater.returnName = block->name;
updater.builder = &builder;
// Set up a locals mapping
for (Index i = 0; i < from->getNumLocals(); i++) {
updater.localMapping[i] = builder.addVar(into, from->getLocalType(i));
}
// assign the operands into the params
// Assign the operands into the params
for (Index i = 0; i < from->params.size(); i++) {
block->list.push_back(
builder.makeLocalSet(updater.localMapping[i], call->operands[i]));
}
// zero out the vars (as we may be in a loop, and may depend on their
// Zero out the vars (as we may be in a loop, and may depend on their
// zero-init value
for (Index i = 0; i < from->vars.size(); i++) {
block->list.push_back(
builder.makeLocalSet(updater.localMapping[from->getVarIndexBase() + i],
LiteralUtils::makeZero(from->vars[i], *module)));
}
// generate and update the inlined contents
// Generate and update the inlined contents
auto* contents = ExpressionManipulator::copy(from->body, *module);
if (!from->debugLocations.empty()) {
debug::copyDebugInfo(from->body, contents, from, into);
}
updater.walk(contents);
block->list.push_back(contents);
block->type = call->type;
// if the function returned a value, we just set the block containing the
// If the function returned a value, we just set the block containing the
// inlined code to have that type. or, if the function was void and
// contained void, that is fine too. a bad case is a void function in which
// we have unreachable code, so we would be replacing a void call with an
// unreachable; we need to handle
if (contents->type == unreachable && block->type == none) {
// make the block reachable by adding a break to it
// Make the block reachable by adding a break to it
block->list.push_back(builder.makeBreak(block->name));
}
return block;
Expand Down
15 changes: 14 additions & 1 deletion test/debugInfo.asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ function () {
function nofile() {
nofile(); //@line 1337
}
return { add: add, ret: ret, opts: opts, fib: fib, switch_reach: switch_reach, nofile: nofile };
function inlineMe(x, y) {
x = x | 0;
y = y | 0;
x = x + y | 0; //@line 120 "inline_me.c"
y = x + y | 0; //@line 121 "inline_me.c"
x = x + y | 0; //@line 122 "inline_me.c"
return x | 0; //@line 123 "inline_me.c"
}
function inlineInto(x, y) {
x = x | 0;
y = y | 0;
return inlineMe(x | 0, y | 0) | 0; //@line 125 "inline_me.c"
}
return { add: add, ret: ret, opts: opts, fib: fib, switch_reach: switch_reach, nofile: nofile, inlineInto: inlineInto };
}

17 changes: 17 additions & 0 deletions test/debugInfo.fromasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ tests/other_file.cpp:314159:0
(i32.add
Expand Down Expand Up @@ -169,4 +170,20 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineInto (; 6 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ inline_me.c:125:0
(i32.add
;;@ inline_me.c:120:0
(local.tee $0
(i32.add
(local.get $0)
(local.get $1)
)
)
(i32.add
(local.get $0)
(local.get $1)
)
)
)
)
17 changes: 17 additions & 0 deletions test/debugInfo.fromasm.clamp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ tests/other_file.cpp:314159:0
(i32.add
Expand Down Expand Up @@ -169,4 +170,20 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineInto (; 6 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ inline_me.c:125:0
(i32.add
;;@ inline_me.c:120:0
(local.tee $0
(i32.add
(local.get $0)
(local.get $1)
)
)
(i32.add
(local.get $0)
(local.get $1)
)
)
)
)
2 changes: 1 addition & 1 deletion test/debugInfo.fromasm.clamp.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions test/debugInfo.fromasm.clamp.no-opts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (param $x i32) (param $y i32) (result i32)
;;@ tests/hello_world.c:5:0
(local.set $x
Expand Down Expand Up @@ -287,4 +288,40 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineMe (; 7 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:120:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:121:0
(local.set $y
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:122:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:123:0
(return
(local.get $x)
)
)
(func $inlineInto (; 8 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:125:0
(return
(call $inlineMe
(local.get $x)
(local.get $y)
)
)
)
)
2 changes: 1 addition & 1 deletion test/debugInfo.fromasm.clamp.no-opts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/debugInfo.fromasm.imprecise
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ tests/other_file.cpp:314159:0
(i32.add
Expand Down Expand Up @@ -161,4 +162,20 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineInto (; 6 ;) (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
;;@ inline_me.c:125:0
(i32.add
;;@ inline_me.c:120:0
(local.tee $0
(i32.add
(local.get $0)
(local.get $1)
)
)
(i32.add
(local.get $0)
(local.get $1)
)
)
)
)
2 changes: 1 addition & 1 deletion test/debugInfo.fromasm.imprecise.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions test/debugInfo.fromasm.imprecise.no-opts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (param $x i32) (param $y i32) (result i32)
;;@ tests/hello_world.c:5:0
(local.set $x
Expand Down Expand Up @@ -275,4 +276,40 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineMe (; 6 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:120:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:121:0
(local.set $y
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:122:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:123:0
(return
(local.get $x)
)
)
(func $inlineInto (; 7 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:125:0
(return
(call $inlineMe
(local.get $x)
(local.get $y)
)
)
)
)
2 changes: 1 addition & 1 deletion test/debugInfo.fromasm.imprecise.no-opts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/debugInfo.fromasm.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions test/debugInfo.fromasm.no-opts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(export "fib" (func $fib))
(export "switch_reach" (func $switch_reach))
(export "nofile" (func $nofile))
(export "inlineInto" (func $inlineInto))
(func $add (; 0 ;) (param $x i32) (param $y i32) (result i32)
;;@ tests/hello_world.c:5:0
(local.set $x
Expand Down Expand Up @@ -287,4 +288,40 @@
;;@ (unknown):1337:0
(call $nofile)
)
(func $inlineMe (; 7 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:120:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:121:0
(local.set $y
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:122:0
(local.set $x
(i32.add
(local.get $x)
(local.get $y)
)
)
;;@ inline_me.c:123:0
(return
(local.get $x)
)
)
(func $inlineInto (; 8 ;) (param $x i32) (param $y i32) (result i32)
;;@ inline_me.c:125:0
(return
(call $inlineMe
(local.get $x)
(local.get $y)
)
)
)
)
Loading