Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure illink task to trim unreachable platform-specific code #31785

Closed
jkotas opened this issue Feb 5, 2020 · 16 comments
Closed

Configure illink task to trim unreachable platform-specific code #31785

jkotas opened this issue Feb 5, 2020 · 16 comments
Assignees
Labels
area-Infrastructure-libraries size-reduction Issues impacting final app size primary for size sensitive workloads
Milestone

Comments

@jkotas
Copy link
Member

jkotas commented Feb 5, 2020

IL linker is capable of constant propagation and dead code removal (see dotnet/linker#848).

We should configure linking for the runtime package to eliminate platform-specific unreachable code that is under conditions like:

  • IntPtr.Size, sizeof(IntPtr)
  • BitConverter.IsLittleEndian
  • hardware intrinsics IsSupported methods
  • System.Runtime.InteropService.OSPlatform
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Feb 5, 2020
@jkotas
Copy link
Member Author

jkotas commented Feb 5, 2020

Related #31712

@am11
Copy link
Member

am11 commented Feb 5, 2020

hardware intrinsics IsSupported methods

If the build machine has different hardware capabilities than that of consumers', wouldn't it produce unexpected result? For instance, if software fallback is removed by this optimization, and some consumer required it.

@Gnbrkm41
Copy link
Contributor

Gnbrkm41 commented Feb 5, 2020

Aren't HWIntrinsics configurable by environment variable before the execution of the runtime? https://github.com/dotnet/runtime/blob/master/src/coreclr/src/jit/jitconfigvalues.h#L223-L245
cc @tannergooding

@MichalStrehovsky
Copy link
Member

Aren't HWIntrinsics configurable by environment variable before the execution of the runtime?

I think this is about e.g. all x64 intrinsics never returning IsSupported == true on ARM.

@tannergooding
Copy link
Member

Aren't HWIntrinsics configurable by environment variable before the execution of the runtime?

I don't believe we currently support this for the R2R code. We would have to determine if it is appropriate to support for AOT code in general.

@ericstj
Copy link
Member

ericstj commented Mar 14, 2020

My understanding is that this issue is calling for us to add configuration such as the following to our ILLink invokation.

    <type fullname="System.IntPtr">
      <method signature="System.Int32 get_Size()" body="stub" value="4" />
    </type>

And similar for each case @jkotas mentions above, and maintain a different set of values for each architecture and platform we build for.

Do we really think this is the right way to control this? Can we instead do so with metadata on the methods? That way anyone running the linker can benefit with a simple flag (eg: --honor-platform-specific-metadata-for-platform:plat-arch or something)

@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Mar 14, 2020
@ericstj ericstj added this to the 5.0 milestone Mar 14, 2020
@marek-safar
Copy link
Contributor

What metadata would you add the the method? The configuration for each RID which will be used during libraries build will be different and for some platforms (e.g. webassembly) much longer than what Jan listed in initial set. Perhaps a better example would be to use RuntimeInformation.OSArchitecture.

We could also embed such configuration file into System.Private.CoreLib for linker to use the data also when linking user apps for specific RID.

@ericstj
Copy link
Member

ericstj commented Mar 16, 2020

What metadata would you add the the method

I was thinking something like

LinkAssumeReturnValue("xyz")

or

LinkAssumeReturnValueForRuntime("xyz", "rid")

I think it makes the most sense for this sort of information to be maintained parallel to the API which implements it (ideally in the same source file). Also we should have tests that make sure that whatever information is checked in agrees with the actual API behavior on that platform.

@tannergooding
Copy link
Member

Some of the metadata, such as hardware intrinsic IsSupported checks, won't be doable in that manner as they are dependent on the underlying or target hardware.

@marek-safar
Copy link
Contributor

Some of the metadata, such as hardware intrinsic IsSupported checks

It still might be useful, for example for platforms where the intrinsics set is restricted (e.g. wasm)

I was thinking something like

LinkAssumeReturnValueForRuntime("xyz", "rid")

How would that work for e.g. IntPtr.Size. Would you list every possible 32 bit rid in the values? Perhaps that could work for our build.

Note, the xml file/syntax will still be used to let users control features like System.Globalization.Invariant

@eerhardt
Copy link
Member

eerhardt commented Jun 8, 2020

I've opened #37615 which covers:

  • IntPtr.Size, sizeof(IntPtr)
  • BitConverter.IsLittleEndian
  • hardware intrinsics IsSupported methods

sizeof(IntPtr) is supported by setting IntPtr.Size given this linker code:

https://github.com/mono/linker/blob/29fea6aa0778e7b4e2af0e276c18f968f407a050/src/linker/Linker.Steps/RemoveUnreachableBlocksStep.cs#L203-L223

The original proposed constants above that I don't know how to support with the ILLinker are:

  • System.Runtime.InteropService.OSPlatform

The OSPlatform API is tricky because you always pass a parameter into the method, and I don't think the ILLinker supports trimming branches on methods that have parameters:

https://github.com/mono/linker/blob/29fea6aa0778e7b4e2af0e276c18f968f407a050/src/linker/Linker.Steps/RemoveUnreachableBlocksStep.cs#L181-L182

@tannergooding
Copy link
Member

sizeof(IntPtr) is supported by setting IntPtr.Size given this linker code:

Should this also cover sizeof(void*) (and pointers in general)? Does this catch Unsafe.SizeOf<T> (which internally uses sizeof, iirc)?

@marek-safar
Copy link
Contributor

Should this also cover sizeof(void*)

I don't think it should. It's rare to see this code used in conditional comparison

eerhardt added a commit to eerhardt/runtime that referenced this issue Jun 10, 2020
Ported the settings from https://github.com/mono/mono/blob/eaa32d13659f0a6b6b5e62ddb49af68b1f9efb6c/sdks/wasm/src/linker-subs.xml and split them out as appropriate to reduce duplication across the different platform builds.

Contributes to dotnet#31785
eerhardt added a commit that referenced this issue Jun 10, 2020
* Add ILLink.Substitutions.xml files for System.Private.CoreLib.

Ported the settings from https://github.com/mono/mono/blob/eaa32d13659f0a6b6b5e62ddb49af68b1f9efb6c/sdks/wasm/src/linker-subs.xml and split them out as appropriate to reduce duplication across the different platform builds.

Contributes to #31785

* Remove all Platforms other than wasm to workaround linker bug.

See dotnet/linker#1260
@ViktorHofer ViktorHofer added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 20, 2020
@ViktorHofer
Copy link
Member

@eerhardt assigning you. Is the tracking work already done or is there something else necessary to be done for 5.0?

@eerhardt
Copy link
Member

It is tracking filling out this TODO:

<!-- TODO: Enable these substitutions files for more platforms once https://github.com/mono/linker/issues/1260 is fixed -->

Which is blocked on the underlying linker issue.

@eerhardt eerhardt added the blocked Issue/PR is blocked on something - see comments label Jul 20, 2020
@eerhardt eerhardt modified the milestones: 5.0.0, 6.0.0 Aug 7, 2020
@marek-safar marek-safar removed the blocked Issue/PR is blocked on something - see comments label Sep 22, 2020
@marek-safar marek-safar added size-reduction Issues impacting final app size primary for size sensitive workloads and removed linkable-framework Issues associated with delivering a linker friendly framework labels Dec 9, 2020
@eerhardt
Copy link
Member

This was addressed in #42578.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Infrastructure-libraries size-reduction Issues impacting final app size primary for size sensitive workloads
Projects
None yet
Development

No branches or pull requests

10 participants