Skip to content

Commit 7ecc77a

Browse files
authored
Rename isIntrinsicallyNondeterministic() to isGenerative() (#4092)
1 parent 0bc7195 commit 7ecc77a

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

src/ir/properties.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ namespace wasm {
2121

2222
namespace Properties {
2323

24-
bool isIntrinsicallyNondeterministic(Expression* curr, FeatureSet features) {
25-
// Practically no wasm instructions are intrinsically nondeterministic.
26-
// Exceptions occur only in GC atm.
24+
bool isGenerative(Expression* curr, FeatureSet features) {
25+
// Practically no wasm instructions are generative. Exceptions occur only in
26+
// GC atm.
2727
if (!features.hasGC()) {
2828
return false;
2929
}
3030

3131
struct Scanner : public PostWalker<Scanner> {
32-
bool deterministic = true;
33-
void visitStructNew(StructNew* curr) { deterministic = false; }
34-
void visitArrayNew(ArrayNew* curr) { deterministic = false; }
32+
bool generative = false;
33+
void visitStructNew(StructNew* curr) { generative = true; }
34+
void visitArrayNew(ArrayNew* curr) { generative = true; }
3535
} scanner;
3636
scanner.walk(curr);
37-
return !scanner.deterministic;
37+
return scanner.generative;
3838
}
3939

4040
} // namespace Properties

src/ir/properties.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,10 @@ inline bool canEmitSelectWithArms(Expression* ifTrue, Expression* ifFalse) {
372372
return ifTrue->type.isSingle() && ifFalse->type.isSingle();
373373
}
374374

375-
// An intrinsically-nondeterministic expression is one that can return different
376-
// results for the same inputs, and that difference is *not* explained by
377-
// other expressions that interact with this one. Hence the cause of
378-
// nondeterminism can be said to be "intrinsic" - it is internal and inherent in
379-
// the expression.
375+
// A "generative" expression is one that can generate different results for the
376+
// same inputs, and that difference is *not* explained by other expressions that
377+
// interact with this one. This is an intrinsic/internal property of the
378+
// expression.
380379
//
381380
// To see the issue more concretely, consider these:
382381
//
@@ -396,26 +395,26 @@ inline bool canEmitSelectWithArms(Expression* ifTrue, Expression* ifFalse) {
396395
// allocations, though, it doesn't matter what is in "..": there is nothing
397396
// in the wasm that we can check to find out if the results are the same or
398397
// not. (In fact, in this case they are always not the same.) So the
399-
// nondeterminism is "intrinsic."
398+
// generativity is "intrinsic" to the expression and it is because each call to
399+
// struct.new generates a new value.
400400
//
401-
// Thus, loads are nondeterministic but not intrinsically so, while GC
402-
// allocations are actual examples of intrinsically nondeterministic
403-
// instructions. If wasm were to add "get current time" or "get a random number"
404-
// instructions then those would also be intrinsically nondeterministic.
401+
// Thus, loads are nondeterministic but not generative, while GC allocations
402+
// are in fact generative. Note that "generative" need not mean "allocation" as
403+
// if wasm were to add "get current time" or "get a random number" instructions
404+
// then those would also be generative - generating a new current time value or
405+
// a new random number on each execution, respectively.
405406
//
406-
// * Note that NaN nondeterminism is ignored here. Technically that allows e.g.
407-
// an f32.add to be nondeterministic, but it is a valid wasm implementation
408-
// to have deterministic NaN behavior, and we optimize under that assumption.
409-
// So NaN nondeterminism does not cause anything to be intrinsically
410-
// nondeterministic.
407+
// * Note that NaN nondeterminism is ignored here. It is a valid wasm
408+
// implementation to have deterministic NaN behavior, and we optimize under
409+
// that simplifying assumption.
411410
// * Note that calls are ignored here. In theory this concept could be defined
412-
// either way for them (that is, we could potentially define them as either
413-
// intrinsically nondeterministic, or not, and each could make sense in its
414-
// own way). It is simpler to ignore them here, which means we only consider
415-
// the behavior of the expression provided here (including its chldren), and
416-
// not external code.
411+
// either way for them - that is, we could potentially define them as
412+
// generative, as they might contain such an instruction, or we could define
413+
// this property as only looking at code in the current function. We choose
414+
// the latter because calls are already handled best in other manners (using
415+
// EffectAnalyzer).
417416
//
418-
bool isIntrinsicallyNondeterministic(Expression* curr, FeatureSet features);
417+
bool isGenerative(Expression* curr, FeatureSet features);
419418

420419
} // namespace Properties
421420

src/passes/LocalCSE.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ struct Checker
436436
// nondeterministic: even if it has no side effects, if it may return a
437437
// different result each time, then we cannot optimize away repeats.
438438
if (effects.hasSideEffects() ||
439-
Properties::isIntrinsicallyNondeterministic(curr,
440-
getModule()->features)) {
439+
Properties::isGenerative(curr, getModule()->features)) {
441440
requestInfos.erase(curr);
442441
} else {
443442
activeOriginals.emplace(

src/passes/OptimizeInstructions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,8 +1583,7 @@ struct OptimizeInstructions
15831583
}
15841584
// To be equal, they must also be known to return the same result
15851585
// deterministically.
1586-
if (Properties::isIntrinsicallyNondeterministic(left,
1587-
getModule()->features)) {
1586+
if (Properties::isGenerative(left, getModule()->features)) {
15881587
return false;
15891588
}
15901589
return true;

src/passes/Precompute.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ struct Precompute
308308
// creates a new, unique struct, even if the data is equal), and so
309309
// PrecomputingExpressionRunner will return a nonconstant flow for all
310310
// GC heap operations. (We could also have used
311-
// Properties::isIntrinsicallyNondeterministic here, but that would be
312-
// less efficient to re-scan the entire expression.)
311+
// Properties::isGenerative here, but that would be less efficient to
312+
// re-scan the entire expression.)
313313
//
314314
// (Other side effects are fine; if an expression does a call and we
315315
// somehow know the entire expression precomputes to a 42, then we can

0 commit comments

Comments
 (0)