Skip to content

Commit 2fbd478

Browse files
Working in closed world test
1 parent 7fd7418 commit 2fbd478

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/passes/GlobalEffects.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,16 @@ transitiveClosure(const Module& module,
165165
//
166166
// caller => called => called by called
167167
//
168-
auto& calledInfo = funcInfos.at(module.getFunction(called));
168+
auto it = funcInfos.find(module.getFunction(called));
169+
170+
// TODO: this should never be missing?
171+
if (it == funcInfos.end()) {
172+
std::cout << "missing key " << called << "\n";
173+
throw(1);
174+
// assert(false && ("missing key " + called));
175+
}
176+
auto& calledInfo = it->second;
177+
// auto& calledInfo = funcInfos.at(module.getFunction(called));
169178
for (auto calledByCalled : calledInfo.calledFunctions) {
170179
if (!callers[calledByCalled].contains(caller)) {
171180
work.push({caller, calledByCalled});
@@ -179,18 +188,17 @@ transitiveClosure(const Module& module,
179188
std::unordered_map<Name, std::unordered_set<Name>> transitiveClosure(
180189
const Module& module,
181190
const std::unordered_map<Name, std::unordered_set<Name>>& funcInfos) {
182-
std::map<Function*, FuncInfo> other;
183-
auto _ =
191+
auto transformed =
184192
funcInfos | std::views::transform(
185193
[&](const auto& pair) -> std::pair<Function*, FuncInfo> {
186194
auto& [k, v] = pair;
187195

188-
auto& func = module.getFunction(k);
196+
auto* func = module.getFunction(k);
189197
FuncInfo info;
190198
info.calledFunctions = v;
191-
return {func->name, info};
199+
return {func, info};
192200
});
193-
201+
std::map<Function*, FuncInfo> other(transformed.begin(), transformed.end());
194202
return transitiveClosure(module, other);
195203
}
196204

@@ -215,6 +223,7 @@ struct GenerateGlobalEffects : public Pass {
215223
std::unordered_map<Name, std::unordered_set<Name>>
216224
indirectCallersNonTransitive;
217225
for (auto& [func, info] : funcInfos) {
226+
indirectCallersNonTransitive[func->name];
218227
for (auto& calledType : info.indirectCalledTypes) {
219228
// auto asdf = functionsWithType.at(calledType);
220229
// auto foo = indirectCallersNonTransitive[func->name];
@@ -226,18 +235,23 @@ struct GenerateGlobalEffects : public Pass {
226235
indirectCallersNonTransitive[func->name].insert(it->second.begin(),
227236
it->second.end());
228237
}
229-
// indirectCallersNonTransitive[func->name].merge(functionsWithType.at(calledType));
230238
}
231-
// for (const auto& name : functionsWitType[])
232-
// for ()
233-
// info.indirectCalledTypes[func->name]
234239
}
235240

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

246+
std::cout << "indirectCallers\n";
247+
for (auto [callee, callers] : indirectCallers) {
248+
std::cout << callee << "\n";
249+
for (auto caller : callers) {
250+
std::cout << "\t" << caller << "\n";
251+
}
252+
std::cout << "\n";
253+
}
254+
241255
// Now that we have transitively propagated all static calls, apply that
242256
// information. First, apply infinite recursion: if a function can call
243257
// itself then it might recurse infinitely, which we consider an effect (a
@@ -303,6 +317,7 @@ struct GenerateGlobalEffects : public Pass {
303317
continue;
304318
}
305319

320+
std::cout << func->name << " has effects " << *info.effects << "\n";
306321
func->effects = std::make_shared<EffectAnalyzer>(*info.effects);
307322
}
308323
}

test/lit/passes/global-effects-closed-world.wast

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
)
7070

7171
;; CHECK: (func $g (type $5) (param $ref (ref $maybe-has-effects)) (result i32)
72+
;; CHECK-NEXT: (call $calls-effectful-function-via-ref
73+
;; CHECK-NEXT: (local.get $ref)
74+
;; CHECK-NEXT: )
7275
;; CHECK-NEXT: (i32.const 1)
7376
;; CHECK-NEXT: )
7477
(func $g (param $ref (ref $maybe-has-effects)) (result i32)

test/lit/passes/global-effects.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,19 @@
315315
;; INCLUDE-NEXT: (call $return-call-throw-and-catch)
316316
;; INCLUDE-NEXT: )
317317
;; INCLUDE-NEXT: )
318+
;; INCLUDE-NEXT: (block $tryend0
319+
;; INCLUDE-NEXT: (try_table (catch_all $tryend0)
320+
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
321+
;; INCLUDE-NEXT: )
322+
;; INCLUDE-NEXT: )
323+
;; INCLUDE-NEXT: (block $tryend1
324+
;; INCLUDE-NEXT: (try_table (catch_all $tryend1)
325+
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
326+
;; INCLUDE-NEXT: )
327+
;; INCLUDE-NEXT: )
318328
;; INCLUDE-NEXT: (call $return-call-throw-and-catch)
329+
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
330+
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
319331
;; INCLUDE-NEXT: )
320332
(func $call-return-call-throw-and-catch
321333
(block $tryend

0 commit comments

Comments
 (0)