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
28 changes: 15 additions & 13 deletions dev/src/driver/toolchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ void input_language(Invocation& invocation)
: Input::Kind::Source;
}

uint32_t parse_wasm_stack_size(const std::string& size)
{
uint64_t bytes = 0;
for (char digit : size)
{
if (digit < '0' || digit > '9' || bytes > 0x7fffffffu / 10)
throw InvocationError("invalid Wasm stack size");
bytes = bytes * 10 + static_cast<unsigned>(digit - '0');
}
if (bytes == 0 || bytes > 0x7fffffffu || bytes % 16 != 0)
throw InvocationError("Wasm stack size must be a positive multiple of 16 below 2 GiB");
return static_cast<uint32_t>(bytes);
}

Invocation parse(const std::vector<std::string>& args, const PreprocessorOptions& base)
{
Invocation invocation;
Expand Down Expand Up @@ -373,19 +387,7 @@ Invocation parse(const std::vector<std::string>& args, const PreprocessorOptions
else if (starts_with(arg, "--gcc-install-dir="))
gcc_install_dir = arg.substr(arg.find('=') + 1);
else if (starts_with(arg, "-Wl,-z,stack-size="))
{
const std::string size = arg.substr(18);
uint64_t bytes = 0;
for (char digit : size)
{
if (digit < '0' || digit > '9' || bytes > 0x7fffffffu / 10)
throw InvocationError("invalid Wasm stack size");
bytes = bytes * 10 + static_cast<unsigned>(digit - '0');
}
if (bytes == 0 || bytes > 0x7fffffffu || bytes % 16 != 0)
throw InvocationError("Wasm stack size must be a positive multiple of 16 below 2 GiB");
invocation.wasm_stack_size = static_cast<uint32_t>(bytes);
}
invocation.wasm_stack_size = parse_wasm_stack_size(arg.substr(18));
else if (arg == "--sysroot" || starts_with(arg, "--sysroot=") || arg == "-resource-dir" ||
starts_with(arg, "-resource-dir="))
{
Expand Down
4 changes: 4 additions & 0 deletions dev/src/opt/dead_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ bool remove_dead_code(FunctionContext& context)
}
if (unknown == 0)
return false;
const bool mapped = context.block_of_version == context.version &&
context.block_of_size == body.instructions.size();
bool changed = false;
for (BlockId b : body.layout)
{
Expand All @@ -90,6 +92,8 @@ bool remove_dead_code(FunctionContext& context)
if (live[i] != 1)
{
unlink_after(body, b, prev, i);
if (mapped)
context.block_of[i] = kNoId;
++context.stats.dead;
changed = true;
}
Expand Down
42 changes: 12 additions & 30 deletions dev/src/opt/function_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ void build_edges(const Program& program, const FunctionBody& body, Cfg& cfg, boo
void build_order(const FunctionBody& body, Cfg& cfg)
{
const uint32_t n = cfg.block_count;
cfg.dfs_index.assign(n, kNoId);
cfg.dfs_vertex.clear();
cfg.dfs_parent.clear();
if (body.layout.empty())
return;
// Iterative DFS: ordinary successors, then handler targets (the
Expand All @@ -314,6 +317,9 @@ void build_order(const FunctionBody& body, Cfg& cfg)
post.clear();
const BlockId entry = body.layout[0];
cfg.flags[entry] |= BF_REACHABLE;
cfg.dfs_index[entry] = 0;
cfg.dfs_vertex.push_back(entry);
cfg.dfs_parent.push_back(kNoId);
stack.push_back(std::make_pair(entry, 0u));
while (!stack.empty())
{
Expand All @@ -331,6 +337,9 @@ void build_order(const FunctionBody& body, Cfg& cfg)
if ((cfg.flags[s] & BF_PLACED) != 0 && (cfg.flags[s] & BF_REACHABLE) == 0)
{
cfg.flags[s] |= BF_REACHABLE;
cfg.dfs_index[s] = static_cast<uint32_t>(cfg.dfs_vertex.size());
cfg.dfs_vertex.push_back(s);
cfg.dfs_parent.push_back(cfg.dfs_index[b]);
next = s;
}
}
Expand Down Expand Up @@ -426,36 +435,9 @@ void build_dominators(Cfg& cfg)
// iterative intersection walk is quadratic on a long chain feeding one
// merge): a depth-first preorder over both edge kinds, semidominators
// by EVAL over the linked forest, then immediate dominators.
static std::vector<uint32_t> number;
static std::vector<BlockId> vertex, parent;
static std::vector<std::pair<BlockId, uint32_t>> stack;
number.assign(n, kNoId);
vertex.clear();
parent.clear();
stack.clear();
number[entry] = 0;
vertex.push_back(entry);
parent.push_back(kNoId);
stack.push_back(std::make_pair(entry, 0u));
while (!stack.empty())
{
const BlockId b = stack.back().first;
const uint32_t k = stack.back().second++;
const uint32_t succs = cfg.succ_first[b + 1] - cfg.succ_first[b];
const uint32_t total = succs + cfg.handler_first[b + 1] - cfg.handler_first[b];
if (k >= total)
{
stack.pop_back();
continue;
}
const BlockId s = k < succs ? cfg.succs[cfg.succ_first[b] + k] : cfg.handlers[cfg.handler_first[b] + k - succs];
if (!cfg.reachable(s) || number[s] != kNoId)
continue;
number[s] = static_cast<uint32_t>(vertex.size());
vertex.push_back(s);
parent.push_back(number[b]);
stack.push_back(std::make_pair(s, 0u));
}
const std::vector<uint32_t>& number = cfg.dfs_index;
const std::vector<BlockId>& vertex = cfg.dfs_vertex;
const std::vector<uint32_t>& parent = cfg.dfs_parent;
const uint32_t count = static_cast<uint32_t>(vertex.size());
static std::vector<uint32_t> semi, label, ancestor, idom, bucket, next, chain;
semi.assign(count, 0);
Expand Down
3 changes: 3 additions & 0 deletions dev/src/opt/function_ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ struct Cfg
std::vector<uint32_t> handler_first, handlers;
std::vector<uint8_t> flags;
std::vector<lowir::BlockId> rpo;
// The same DFS supplies RPO and Lengauer-Tarjan preorder/parents.
std::vector<uint32_t> dfs_index, dfs_parent;
std::vector<lowir::BlockId> dfs_vertex;
std::vector<uint32_t> rpo_index; // kNoId when unreachable
std::vector<lowir::BlockId> idom; // kNoId for the entry and unreachable blocks
std::vector<uint32_t> dom_in, dom_out;
Expand Down
30 changes: 21 additions & 9 deletions dev/src/opt/jump_threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,15 @@ void JumpThreading::find_use_dominators()
use_dom_[v.index] = b;
return;
}
// Most additional uses are below the common dominator already
// found. The intervals answer that without climbing the tree again.
if (a == b || cfg_.dominates(a, b))
return;
if (cfg_.dominates(b, a))
{
use_dom_[v.index] = b;
return;
}
while (a != b)
{
while (cfg_.rpo_index[a] > cfg_.rpo_index[b])
Expand Down Expand Up @@ -783,6 +792,8 @@ void JumpThreading::find_use_dominators()
BlockId JumpThreading::successor_holding(BlockId m, TempId t) const
{
BlockId x = t < use_dom_.size() ? use_dom_[t] : kNoId;
if (x == kNoId || x == m || !cfg_.dominates(m, x))
return kNoId;
while (x != kNoId && x != m && cfg_.idom[x] != m)
x = cfg_.idom[x];
return x == kNoId || x == m || cfg_.pred_count(x) != 1 ? kNoId : x;
Expand Down Expand Up @@ -1305,7 +1316,11 @@ bool JumpThreading::known_compares()
// comes between the phis and it is copied for a threaded
// predecessor (at most kThreadCopy plain instructions).
bool def_here = false;
for (InstId i = first; i != kNoId && i != last && !def_here; i = body_.instructions[i].next)
uint32_t examined = 0;
// Beyond the copy budget this block cannot be threaded, whether
// its compare is here or in another block.
for (InstId i = first; i != kNoId && i != last && !def_here && examined++ <= kThreadCopy;
i = body_.instructions[i].next)
def_here = i == def;
if (def_here)
{
Expand Down Expand Up @@ -1516,11 +1531,10 @@ bool JumpThreading::known_compares()
for (const std::pair<BlockId, BlockId>& r : redirects)
if (bypass(r.first, s, r.second, cresult))
changed = true;
if (!redirects.empty() && changed)
{
if (changed)
c_.invalidate_cfg();
c_.ensure_cfg();
}
// Demotion may split an edge even if every bypass is rejected.
c_.ensure_cfg();
}
counted_ = false;
return changed;
Expand Down Expand Up @@ -2341,12 +2355,10 @@ bool JumpThreading::demote_phis(BlockId s, std::vector<std::pair<BlockId, BlockI
}
unlink_after(body_, s, kNoId, i);
}
// The stores and loads leave the graph as it was; new edge blocks do not.
// The caller redirects these edges next without reading CFG facts.
// Invalidate for new edge blocks, then rebuild after the redirects.
if (split)
{
c_.invalidate_cfg();
c_.ensure_cfg();
}
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions dev/src/opt/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,17 @@ bool run_pass(FunctionContext& context, bool (*pass)(FunctionContext&), const ch
verify(context, name);
if (changed && context.kept_body != pass)
{
// Constant propagation only replaces operands; dead-code removal
// clears the entries it unlinks. Both leave this block map current.
if ((pass == propagate_constants || pass == remove_dead_code) && context.block_of_version == context.version)
++context.block_of_version;
++context.version;
// Known in order: the pass ended with restore_definition_order, or
// only removed instructions or replaced operands with literals.
if (context.left_ordered || (was_ordered && (pass == remove_dead_code || pass == propagate_constants)))
context.ordered_version = context.version;
if (context.verify)
verify_kept_analyses(context, name);
return true;
}
if (context.verify)
Expand Down
12 changes: 10 additions & 2 deletions dev/src/sema/lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace sema {

namespace {

// The bit of Scope::name_bits a name sets: six bits of its spelling's hash.
uint64_t NameBit(const Identifier* name)
{
return uint64_t(1) << (name->hash >> 26);
}

// A 64-bit mixer (a multiplicative hash, its high half folded down): every
// key bit reaches the low bits the tables index by, so names declared in
// many scopes spread out.
Expand Down Expand Up @@ -194,7 +200,9 @@ void NameLookup::reserve(uint32_t names)

NameEntry* NameLookup::entry(const Scope* scope, const Identifier* name)
{
scope->indexed_names = true;
const uint64_t bit = NameBit(name);
if ((scope->name_bits & bit) == 0)
scope->name_bits |= bit;
if ((count_ + 1) * 2 > slots_.size())
grow();
const uint64_t key = key_of(scope, name);
Expand All @@ -215,7 +223,7 @@ NameEntry* NameLookup::entry(const Scope* scope, const Identifier* name)

const NameEntry* NameLookup::find(const Scope* scope, const Identifier* name) const
{
if (!scope->indexed_names)
if ((scope->name_bits & NameBit(name)) == 0)
return nullptr;
const uint64_t key = key_of(scope, name);
const uint64_t mask = slot_mask_;
Expand Down
10 changes: 6 additions & 4 deletions dev/src/sema/semantic_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ struct Scope
// its concrete class, including bases dependent in the original
// template (an explicit member specialization, 14.7.3p16).
bool concrete_members : 1;
// Monotonic membership hint for the name index. Setting it through a
// const scope records an index entry, independently of name visibility
// and the printed bindings list (which can omit indexed declarations).
mutable bool indexed_names : 1;
// A frozen prefix of a default argument's function parameter clause.
// Lookup sees these bindings; declarations (including closures) belong
// to the surrounding declaration scope.
Expand All @@ -173,6 +169,12 @@ struct Scope
// scopes already count).
uint8_t lists;
uint32_t id;
// Monotonic membership hint for the name index: one bit per name hash
// (see NameLookup::entry) of the names indexed here, so a clear bit
// answers a search without probing the index. Setting it through a
// const scope records an index entry, independently of name visibility
// and the printed bindings list (which can omit indexed declarations).
mutable uint64_t name_bits;
// Where unqualified lookup continues: the lexical parent, or for an
// out-of-class definition the scope of its class or namespace.
Scope* parent;
Expand Down
10 changes: 10 additions & 0 deletions dev/src/syntax/name_scopes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace {

const uint64_t kEmptyKey = ~0ull;

// The bit of Scope::name_bits a name sets: six bits of its spelling's hash.
uint64_t NameBit(const Identifier* name)
{
return uint64_t(1) << (name->hash >> 26);
}

// Orders NameScopes::pending_ as a max-heap by the depth of `at`.
bool Shallower(const ScopeEdge* a, const ScopeEdge* b)
{
Expand Down Expand Up @@ -134,6 +140,7 @@ Scope* NameScopes::push(ScopeKind kind, const Identifier* name)
scope->fixed_bases = 0;
scope->id = next_scope_id_++;
scope->specialization_visit = 0;
scope->name_bits = 0;
scope->parent = current_;
scope->depth = current_ != nullptr ? current_->depth + 1 : 0;
scope->name = name;
Expand Down Expand Up @@ -185,6 +192,7 @@ void NameScopes::declare(Scope* scope, const Identifier* name, NameBinding bindi
marks_[name->id] |= kNamespaceTemplateValue;
if (member)
touch_name(name->id);
scope->name_bits |= NameBit(name);
const uint64_t key = key_of(scope, name);
uint32_t index = find_slot(key);
if (slots_[index].key == kEmptyKey)
Expand Down Expand Up @@ -553,6 +561,8 @@ void NameScopes::reattach(const std::vector<Change>& changes, size_t begin, size

NameBinding NameScopes::own(Scope* scope, const Identifier* name) const
{
if ((scope->name_bits & NameBit(name)) == 0)
return NameBinding{ NameCategory::None, false, nullptr };
const Slot& slot = slots_[find_slot(key_of(scope, name))];
if (slot.key == kEmptyKey)
return NameBinding{ NameCategory::None, false, nullptr };
Expand Down
3 changes: 3 additions & 0 deletions dev/src/syntax/name_scopes.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct Scope
uint32_t id;
// A second visit is needed when a lookup first missed without dependent bases.
uint32_t specialization_visit;
// One bit per name hash of the names declared here (see
// NameScopes::declare): a clear bit answers a search without the table.
uint64_t name_bits;
Scope* parent;
const Identifier* name;
// Namespace: nominated namespaces (using-directives, inline and unnamed
Expand Down
Loading
Loading