Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Working in closed world test
  • Loading branch information
stevenfontanella committed Apr 8, 2026
commit 2fbd478ed900fbb8050c0053c6854867eb57d03a
35 changes: 25 additions & 10 deletions src/passes/GlobalEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@ transitiveClosure(const Module& module,
//
// caller => called => called by called
//
auto& calledInfo = funcInfos.at(module.getFunction(called));
auto it = funcInfos.find(module.getFunction(called));

// TODO: this should never be missing?
if (it == funcInfos.end()) {
std::cout << "missing key " << called << "\n";
throw(1);
// assert(false && ("missing key " + called));
}
auto& calledInfo = it->second;
// auto& calledInfo = funcInfos.at(module.getFunction(called));
for (auto calledByCalled : calledInfo.calledFunctions) {
if (!callers[calledByCalled].contains(caller)) {
work.push({caller, calledByCalled});
Expand All @@ -179,18 +188,17 @@ transitiveClosure(const Module& module,
std::unordered_map<Name, std::unordered_set<Name>> transitiveClosure(
const Module& module,
const std::unordered_map<Name, std::unordered_set<Name>>& funcInfos) {
std::map<Function*, FuncInfo> other;
auto _ =
auto transformed =
funcInfos | std::views::transform(
[&](const auto& pair) -> std::pair<Function*, FuncInfo> {
auto& [k, v] = pair;

auto& func = module.getFunction(k);
auto* func = module.getFunction(k);
FuncInfo info;
info.calledFunctions = v;
return {func->name, info};
return {func, info};
});

std::map<Function*, FuncInfo> other(transformed.begin(), transformed.end());
return transitiveClosure(module, other);
}

Expand All @@ -215,6 +223,7 @@ struct GenerateGlobalEffects : public Pass {
std::unordered_map<Name, std::unordered_set<Name>>
indirectCallersNonTransitive;
for (auto& [func, info] : funcInfos) {
indirectCallersNonTransitive[func->name];
for (auto& calledType : info.indirectCalledTypes) {
// auto asdf = functionsWithType.at(calledType);
// auto foo = indirectCallersNonTransitive[func->name];
Expand All @@ -226,18 +235,23 @@ struct GenerateGlobalEffects : public Pass {
indirectCallersNonTransitive[func->name].insert(it->second.begin(),
it->second.end());
}
// indirectCallersNonTransitive[func->name].merge(functionsWithType.at(calledType));
}
// for (const auto& name : functionsWitType[])
// for ()
// info.indirectCalledTypes[func->name]
}

// indirectCallers[foo] = [func that indirect calls something with the same
// type as foo, ..]
const std::unordered_map<Name, std::unordered_set<Name>> indirectCallers =
transitiveClosure(*module, indirectCallersNonTransitive);

std::cout << "indirectCallers\n";
for (auto [callee, callers] : indirectCallers) {
std::cout << callee << "\n";
for (auto caller : callers) {
std::cout << "\t" << caller << "\n";
}
std::cout << "\n";
}

// Now that we have transitively propagated all static calls, apply that
// information. First, apply infinite recursion: if a function can call
// itself then it might recurse infinitely, which we consider an effect (a
Expand Down Expand Up @@ -303,6 +317,7 @@ struct GenerateGlobalEffects : public Pass {
continue;
}

std::cout << func->name << " has effects " << *info.effects << "\n";
func->effects = std::make_shared<EffectAnalyzer>(*info.effects);
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/lit/passes/global-effects-closed-world.wast
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
)

;; CHECK: (func $g (type $5) (param $ref (ref $maybe-has-effects)) (result i32)
;; CHECK-NEXT: (call $calls-effectful-function-via-ref
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
(func $g (param $ref (ref $maybe-has-effects)) (result i32)
Expand Down
12 changes: 12 additions & 0 deletions test/lit/passes/global-effects.wast
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,19 @@
;; INCLUDE-NEXT: (call $return-call-throw-and-catch)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (block $tryend0
;; INCLUDE-NEXT: (try_table (catch_all $tryend0)
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (block $tryend1
;; INCLUDE-NEXT: (try_table (catch_all $tryend1)
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (call $return-call-throw-and-catch)
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
;; INCLUDE-NEXT: )
(func $call-return-call-throw-and-catch
(block $tryend
Expand Down