Skip to content

Conversation

@vitek-karas
Copy link
Member

This means the value of the fields is tracked as a const value instead of a field reference.
This is to support some additional code patterns as well as align the behavior with the linker. Compiler will inline the const fields into the IL effectively removing the field reference in these cases and linker only sees the constant. So ideally the analyzer should have a similar behavior.

This is currently blocking runtime integration: dotnet/runtime#68650

This means the value of the fields is tracked as a const value instead of a field reference.
This is to support some additional code patterns as well as align the behavior with the linker. Compiler will inline the const fields into the IL effectively removing the field refernce in these cases and linker only sees the constant. So ideally the analyzer should have a similar behavior.
return true;
} else if (operation.Type?.SpecialType == SpecialType.System_Boolean && constantValue is bool boolConstantValue) {
constValue = new ConstIntValue (boolConstantValue ? 1 : 0);
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only these types rather than any primitive that can be const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because only these are recognized by the rest of trimming analysis. And also because we only have value system representation for these (for example ConstInstValue... we would need to introduce similar types for other values like DateTime, double, ...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be tracking other integer types though - for example:

class Program {
	static void Main() {
		uint i = 0u;
		var t_arr = new Type[10];
		t_arr[i] = typeof(C);
		t_arr[i].GetMethods();
	}
}

class C {
	[RUC("RUC")]
	public void R() {}
}

Analyzer:
warning IL2065: Value passed to implicit 'this' parameter of method 'System.Type.GetMethods()' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.

Linker:
Trim analysis warning IL2026: Program.Main(): Using member 'C.R()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. RUC.

I think it would be fine to do as a separate change so we can unblock runtime though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into this in a followup change

Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM, thanks!

return true;
} else if (operation.Type?.SpecialType == SpecialType.System_Boolean && constantValue is bool boolConstantValue) {
constValue = new ConstIntValue (boolConstantValue ? 1 : 0);
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be tracking other integer types though - for example:

class Program {
	static void Main() {
		uint i = 0u;
		var t_arr = new Type[10];
		t_arr[i] = typeof(C);
		t_arr[i].GetMethods();
	}
}

class C {
	[RUC("RUC")]
	public void R() {}
}

Analyzer:
warning IL2065: Value passed to implicit 'this' parameter of method 'System.Type.GetMethods()' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements.

Linker:
Trim analysis warning IL2026: Program.Main(): Using member 'C.R()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. RUC.

I think it would be fine to do as a separate change so we can unblock runtime though.

@vitek-karas vitek-karas merged commit 05a3b65 into dotnet:main May 2, 2022
@vitek-karas vitek-karas deleted the RecognizeConstFields branch May 2, 2022 19:15
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…#2774)

This means the value of the fields is tracked as a const value instead of a field reference.
This is to support some additional code patterns as well as align the behavior with the linker. Compiler will inline the const fields into the IL effectively removing the field refernce in these cases and linker only sees the constant. So ideally the analyzer should have a similar behavior.

Commit migrated from dotnet/linker@05a3b65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants