Skip to content

Commit

Permalink
[WASM] Add clinit to factories instead of ctors
Browse files Browse the repository at this point in the history
Currently we inject clinit calls to ctor functions however they are not the external entry points. They are either called from factories or child class (child class is already guaranteed to call clinit).

This patch generate these calls in factory methods instead. This also simplifies the optimization of clinit calls when cycles exists between constructors and static fields (like Enums).

PiperOrigin-RevId: 582048512
  • Loading branch information
gkdn authored and copybara-github committed Nov 13, 2023
1 parent 7f10820 commit 4721673
Show file tree
Hide file tree
Showing 164 changed files with 1,298 additions and 1,446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public ImmutableList<Supplier<NormalizationPass>> getPassFactories(BackendOption
NormalizeFieldInitialization::new,
ImplementInstanceInitialization::new,
NormalizeLabels::new,
NormalizeInstantiationThroughFactoryMethods::new,
ImplementStaticInitializationViaConditionChecks::new,
ImplementClassMetadataViaGetters::new,
ImplementStringCompileTimeConstants::new,
Expand Down Expand Up @@ -456,7 +457,6 @@ public ImmutableList<Supplier<NormalizationPass>> getPassFactories(BackendOption
// Needs to run at the end as the types in the ast will be invalid after the pass.
ImplementArraysAsClasses::new,
InsertExceptionConversionsWasm::new,
NormalizeInstantiationThroughFactoryMethods::new,
RemoveCustomIsInstanceMethods::new,
RemoveNoopStatements::new,
UpgradeInterfaceDispatch::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ public abstract class ImplementStaticInitializationBase extends NormalizationPas

private final Set<String> privateMembersCalledFromOtherClasses = new HashSet<>();

// TODO(b/187218486): Remove after adding a pass to convert constructors to static methods.
private final boolean triggerClinitInConstructors;

public ImplementStaticInitializationBase(boolean triggerClinitInConstructors) {
this.triggerClinitInConstructors = triggerClinitInConstructors;
}

@Override
public final void applyTo(CompilationUnit compilationUnit) {
collectPrivateMemberReferences(compilationUnit);
Expand Down Expand Up @@ -194,9 +187,6 @@ boolean triggersClinit(MemberDescriptor memberDescriptor, Type enclosingType) {

return memberDescriptor.isStatic()
|| memberDescriptor.isJsConstructor()
|| (triggerClinitInConstructors
&& memberDescriptor.isConstructor()
&& !enclosingType.isOptimizedEnum())
// non-private instance methods (except the synthetic ctor) of an optimized enum will
// trigger clinit, since the constructor will not.
|| (triggersClinitInInstanceMethods(enclosingType) && isInstanceMethod(memberDescriptor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,16 @@
public class ImplementStaticInitializationViaClinitFunctionRedirection
extends ImplementStaticInitializationBase {

public ImplementStaticInitializationViaClinitFunctionRedirection() {
super(/* triggerClinitInConstructors = */ false);
}

@Override
public void applyTo(Type type) {
checkState(!type.isNative());
checkState(!type.isJsFunctionInterface());
if (type.isJsEnum()) {
checkState(!type.isNative());
checkState(!type.isJsFunctionInterface());
if (type.isJsEnum()) {
return;
}
synthesizeSettersAndGetters(type);
synthesizeClinitMethod(type);
synthesizeStaticFieldDeclaration(type);
}
synthesizeSettersAndGetters(type);
synthesizeClinitMethod(type);
synthesizeStaticFieldDeclaration(type);
}

private static void synthesizeStaticFieldDeclaration(Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
public class ImplementStaticInitializationViaConditionChecks
extends ImplementStaticInitializationBase {

public ImplementStaticInitializationViaConditionChecks() {
super(/* triggerClinitInConstructors = */ true);
}

@Override
public void applyTo(Type type) {
synthesizeClinitCallsOnFieldAccess(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private static MethodDescriptor getFactoryDescriptorForConstructor(MethodDescrip
.getTypeParameterDescriptors())
.setOrigin(MethodOrigin.SYNTHETIC_FACTORY_FOR_CONSTRUCTOR)
.setOriginalJsInfo(JsInfo.NONE)
.setVisibility(Visibility.PUBLIC));
.setVisibility(constructor.getVisibility()));
}

private static Expression createThrowableInit(Expression newInstanceRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@
;;@ abstractinnerclass/InnerClasses.java:18:13
(local $$instance (ref null $abstractinnerclass.InnerClasses))
(block
;;@ abstractinnerclass/InnerClasses.java:18:13
(call [email protected] )
;;@ abstractinnerclass/InnerClasses.java:18:13
(local.set $$instance (struct.new $abstractinnerclass.InnerClasses (ref.as_non_null (global.get $abstractinnerclass.InnerClasses.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0)))
;;@ abstractinnerclass/InnerClasses.java:18:13
Expand All @@ -172,8 +174,6 @@
(param $this (ref null $abstractinnerclass.InnerClasses))
;;@ abstractinnerclass/InnerClasses.java:18:13
(block
;;@ abstractinnerclass/InnerClasses.java:18:13
(call [email protected] )
;;@ abstractinnerclass/InnerClasses.java:18:13
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -243,8 +243,6 @@
(param $this (ref null $abstractinnerclass.InnerClasses.B))
;;@ abstractinnerclass/InnerClasses.java:23:24
(block
;;@ abstractinnerclass/InnerClasses.java:23:24
(call [email protected] )
;;@ abstractinnerclass/InnerClasses.java:23:24
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -316,8 +314,6 @@
(param $$outer_this (ref null $abstractinnerclass.InnerClasses))
;;@ abstractinnerclass/InnerClasses.java:29:17
(block
;;@ abstractinnerclass/InnerClasses.java:29:17
(call [email protected] )
;;@ abstractinnerclass/InnerClasses.java:29:17
(struct.set $abstractinnerclass.InnerClasses.C $$outer_this__abstractinnerclass_InnerClasses_C (local.get $this) (local.get $$outer_this))
;;@ abstractinnerclass/InnerClasses.java:29:17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@
(param $this (ref null $abstractinterfaceimpl.AbstractInterfaceImpl))
;;@ abstractinterfaceimpl/AbstractInterfaceImpl.java:22:22
(block
;;@ abstractinterfaceimpl/AbstractInterfaceImpl.java:22:22
(call [email protected] )
;;@ abstractinterfaceimpl/AbstractInterfaceImpl.java:22:22
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(local $$instance (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault))
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(local.set $$instance (struct.new $abstractmethodoverridedefault.AbstractMethodOverrideDefault (ref.as_non_null (global.get $abstractmethodoverridedefault.AbstractMethodOverrideDefault.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0)))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
Expand All @@ -308,8 +310,6 @@
(param $this (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:18:13
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -379,8 +379,6 @@
(param $this (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault.A))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:31:24
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:31:24
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault.A )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:31:24
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -453,8 +451,6 @@
(param $this (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault.B))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:36:24
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:36:24
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault.B )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:36:24
(call $$ctor__void_$p_abstractmethodoverridedefault_AbstractMethodOverrideDefault_A@abstractmethodoverridedefault.AbstractMethodOverrideDefault.A (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -535,6 +531,8 @@
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(local $$instance (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault.C))
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault.C )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(local.set $$instance (struct.new $abstractmethodoverridedefault.AbstractMethodOverrideDefault.C (ref.as_non_null (global.get $abstractmethodoverridedefault.AbstractMethodOverrideDefault.C.vtable)) (ref.as_non_null (global.get $abstractmethodoverridedefault.AbstractMethodOverrideDefault.C.itable)) (i32.const 0)))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
Expand All @@ -549,8 +547,6 @@
(param $this (ref null $abstractmethodoverridedefault.AbstractMethodOverrideDefault.C))
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(block
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(call $$clinit__void@abstractmethodoverridedefault.AbstractMethodOverrideDefault.C )
;;@ abstractmethodoverridedefault/AbstractMethodOverrideDefault.java:46:15
(call $$ctor__void_$p_abstractmethodoverridedefault_AbstractMethodOverrideDefault_A@abstractmethodoverridedefault.AbstractMethodOverrideDefault.A (ref.as_non_null (local.get $this)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@
;;@ accidentaloverride/AccidentalOverride.java:18:6
(local $$instance (ref null $accidentaloverride.Parent))
(block
;;@ accidentaloverride/AccidentalOverride.java:18:6
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:18:6
(local.set $$instance (struct.new $accidentaloverride.Parent (ref.as_non_null (global.get $accidentaloverride.Parent.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0)))
;;@ accidentaloverride/AccidentalOverride.java:18:6
Expand All @@ -233,8 +235,6 @@
(param $this (ref null $accidentaloverride.Parent))
;;@ accidentaloverride/AccidentalOverride.java:18:6
(block
;;@ accidentaloverride/AccidentalOverride.java:18:6
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:18:6
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -318,6 +318,8 @@
;;@ accidentaloverride/AccidentalOverride.java:31:6
(local $$instance (ref null $accidentaloverride.AnotherAccidentalOverride))
(block
;;@ accidentaloverride/AccidentalOverride.java:31:6
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:31:6
(local.set $$instance (struct.new $accidentaloverride.AnotherAccidentalOverride (ref.as_non_null (global.get $accidentaloverride.AnotherAccidentalOverride.vtable)) (ref.as_non_null (global.get $accidentaloverride.AnotherAccidentalOverride.itable)) (i32.const 0)))
;;@ accidentaloverride/AccidentalOverride.java:31:6
Expand All @@ -332,8 +334,6 @@
(param $this (ref null $accidentaloverride.AnotherAccidentalOverride))
;;@ accidentaloverride/AccidentalOverride.java:31:6
(block
;;@ accidentaloverride/AccidentalOverride.java:31:6
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:31:6
(call $$ctor__void_$p_accidentaloverride_Parent@accidentaloverride.Parent (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -434,6 +434,8 @@
;;@ accidentaloverride/AccidentalOverride.java:41:13
(local $$instance (ref null $accidentaloverride.AccidentalOverride))
(block
;;@ accidentaloverride/AccidentalOverride.java:41:13
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:41:13
(local.set $$instance (struct.new $accidentaloverride.AccidentalOverride (ref.as_non_null (global.get $accidentaloverride.AccidentalOverride.vtable)) (ref.as_non_null (global.get $accidentaloverride.AccidentalOverride.itable)) (i32.const 0)))
;;@ accidentaloverride/AccidentalOverride.java:41:13
Expand All @@ -448,8 +450,6 @@
(param $this (ref null $accidentaloverride.AccidentalOverride))
;;@ accidentaloverride/AccidentalOverride.java:41:13
(block
;;@ accidentaloverride/AccidentalOverride.java:41:13
(call [email protected] )
;;@ accidentaloverride/AccidentalOverride.java:41:13
(call $$ctor__void_$p_accidentaloverride_Parent@accidentaloverride.Parent (ref.as_non_null (local.get $this)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@
;;@ annotation/Main.java:18:13
(local $$instance (ref null $annotation.Main))
(block
;;@ annotation/Main.java:18:13
(call [email protected] )
;;@ annotation/Main.java:18:13
(local.set $$instance (struct.new $annotation.Main (ref.as_non_null (global.get $annotation.Main.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0)))
;;@ annotation/Main.java:18:13
Expand All @@ -196,8 +198,6 @@
(param $this (ref null $annotation.Main))
;;@ annotation/Main.java:18:13
(block
;;@ annotation/Main.java:18:13
(call [email protected] )
;;@ annotation/Main.java:18:13
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -269,6 +269,8 @@
;;@ annotation/Main.java:30:8
(local $$instance (ref null $annotation.Main.Bar))
(block
;;@ annotation/Main.java:30:8
(call [email protected] )
;;@ annotation/Main.java:30:8
(local.set $$instance (struct.new $annotation.Main.Bar (ref.as_non_null (global.get $annotation.Main.Bar.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0) (ref.null $annotation.Main)))
;;@ annotation/Main.java:30:8
Expand All @@ -284,8 +286,6 @@
(param $$outer_this (ref null $annotation.Main))
;;@ annotation/Main.java:30:8
(block
;;@ annotation/Main.java:30:8
(call [email protected] )
;;@ annotation/Main.java:30:8
(struct.set $annotation.Main.Bar $$outer_this__annotation_Main_Bar (local.get $this) (local.get $$outer_this))
;;@ annotation/Main.java:30:8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@
(param $i i32)
;;@ anonymousclass/AnonymousClass.java:21:2
(block
;;@ anonymousclass/AnonymousClass.java:21:19
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:21:19
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -310,8 +308,6 @@
(param $i i32)
;;@ anonymousclass/AnonymousClass.java:27:2
(block
;;@ anonymousclass/AnonymousClass.java:27:36
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:27:36
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -378,6 +374,8 @@
;;@ anonymousclass/AnonymousClass.java:32:13
(local $$instance (ref null $anonymousclass.AnonymousClass))
(block
;;@ anonymousclass/AnonymousClass.java:32:13
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:32:13
(local.set $$instance (struct.new $anonymousclass.AnonymousClass (ref.as_non_null (global.get $anonymousclass.AnonymousClass.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0) (i32.const 0)))
;;@ anonymousclass/AnonymousClass.java:32:13
Expand All @@ -392,8 +390,6 @@
(param $this (ref null $anonymousclass.AnonymousClass))
;;@ anonymousclass/AnonymousClass.java:32:13
(block
;;@ anonymousclass/AnonymousClass.java:32:13
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:32:13
(call [email protected] (ref.as_non_null (local.get $this)))
)
Expand Down Expand Up @@ -484,6 +480,8 @@
;;@ anonymousclass/AnonymousClass.java:37:25
(local $$instance (ref null $anonymousclass.AnonymousClass.1))
(block
;;@ anonymousclass/AnonymousClass.java:37:25
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:37:25
(local.set $$instance (struct.new $anonymousclass.AnonymousClass.1 (ref.as_non_null (global.get $anonymousclass.AnonymousClass.1.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0) (ref.null $anonymousclass.AnonymousClass) (ref.null $java.lang.Object) (ref.null $java.lang.Object)))
;;@ anonymousclass/AnonymousClass.java:37:25
Expand All @@ -500,8 +498,6 @@
(param $$_0 i32)
;;@ anonymousclass/AnonymousClass.java:37:25
(block
;;@ anonymousclass/AnonymousClass.java:37:25
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:37:25
(struct.set $anonymousclass.AnonymousClass.1 $$outer_this__anonymousclass_AnonymousClass_1 (local.get $this) (local.get $$outer_this))
;;@ anonymousclass/AnonymousClass.java:37:25
Expand Down Expand Up @@ -618,6 +614,8 @@
;;@ anonymousclass/AnonymousClass.java:48:42
(local $$instance (ref null $anonymousclass.AnonymousClass.2))
(block
;;@ anonymousclass/AnonymousClass.java:48:42
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:48:42
(local.set $$instance (struct.new $anonymousclass.AnonymousClass.2 (ref.as_non_null (global.get $anonymousclass.AnonymousClass.2.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0) (ref.null $anonymousclass.AnonymousClass) (ref.null $java.lang.Object) (ref.null $java.lang.Object)))
;;@ anonymousclass/AnonymousClass.java:48:42
Expand All @@ -634,8 +632,6 @@
(param $$_0 i32)
;;@ anonymousclass/AnonymousClass.java:48:42
(block
;;@ anonymousclass/AnonymousClass.java:48:42
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:48:42
(struct.set $anonymousclass.AnonymousClass.2 $$outer_this__anonymousclass_AnonymousClass_2 (local.get $this) (local.get $$outer_this))
;;@ anonymousclass/AnonymousClass.java:48:42
Expand Down Expand Up @@ -738,6 +734,8 @@
;;@ anonymousclass/AnonymousClass.java:61:23
(local $$instance (ref null $anonymousclass.SomeInterface.1))
(block
;;@ anonymousclass/AnonymousClass.java:61:23
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:61:23
(local.set $$instance (struct.new $anonymousclass.SomeInterface.1 (ref.as_non_null (global.get $anonymousclass.SomeInterface.1.vtable)) (ref.as_non_null (global.get $itable.empty)) (i32.const 0)))
;;@ anonymousclass/AnonymousClass.java:61:23
Expand All @@ -753,8 +751,6 @@
(param $$_0 i32)
;;@ anonymousclass/AnonymousClass.java:61:23
(block
;;@ anonymousclass/AnonymousClass.java:61:23
(call [email protected] )
;;@ anonymousclass/AnonymousClass.java:61:23
(call $$ctor__int__void_$p_anonymousclass_SomeClass@anonymousclass.SomeClass (ref.as_non_null (local.get $this))(local.get $$_0))
)
Expand Down
Loading

0 comments on commit 4721673

Please sign in to comment.