From 132d8afe7ac5eae64b7597f120319fd2ccca5d5a Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Sat, 26 Oct 2019 08:20:15 +0200 Subject: Add new optimization steps to make Mark step more effective Two new steps have been introduced BodySubstituterStep This step removes any conditional blocks when the condition or conditions are evaluated as constants. This step does not do any inlining. The conditional logic is kept but based on the known values only one branch is always taken. A simple example which can be detected by linker. ```c# class C { static void Main () { if (Result) Console.WriteLine (); // <- this call will be marked and removed } static bool Result () => false; } ``` RemoveUnreachableBlocksStep A new command-line option called `--substitutions` allow external customization of any methoda for assemblies which are linked. The syntax is same as for existing linker descriptor XML files but it add way to control body modifications. An example of XML file ```xml ``` The `value` attribute is optional and only required when the method stub should not fallback to default behaviour. Additional to `stub` value also `remove` value is supported to forcibly remove body of the method when the method is marked. This is useful when the conditional logic cannot be evaluated by linker and the method will be marked but never actually reached. Applicability Both steps can be combined to achieve the effect of externally customizing which conditional branches can be removed during the linking. I can illustrate that on IntPtr.Size property. With following substitution any code that has compiled in conditional logic for 64 bits handling by checking IntPtr.Size will be removed during linking. ```xml ``` Implements #752 --- .../UnreachableBlock/ComplexConditions.cs | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs (limited to 'test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs') diff --git a/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs new file mode 100644 index 000000000..9a92ab742 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditions.cs @@ -0,0 +1,35 @@ +using System; +using System.Reflection.Emit; +using Mono.Linker.Tests.Cases.Expectations.Assertions; + +namespace Mono.Linker.Tests.Cases.UnreachableBlock +{ + public class ComplexConditions + { + public static void Main() + { + Test_1 (null); + } + + [Kept] + [ExpectBodyModified] + static void Test_1 (object type) + { + if (type is Type || (IsDynamicCodeSupported && type is TypeBuilder)) + Reached_1 (); + } + + [Kept] + static bool IsDynamicCodeSupported { + [Kept] + get { + return true; + } + } + + [Kept] + static void Reached_1 () + { + } + } +} \ No newline at end of file -- cgit v1.2.3