public void Trim(ILCompilerOptions options, ILogWriter logWriter) { ComputeDefaultOptions(out var targetOS, out var targetArchitecture); var targetDetails = new TargetDetails(targetArchitecture, targetOS, TargetAbi.NativeAot); CompilerTypeSystemContext typeSystemContext = new CompilerTypeSystemContext(targetDetails, SharedGenericsMode.CanonicalReferenceTypes, DelegateFeature.All); typeSystemContext.InputFilePaths = options.InputFilePaths; typeSystemContext.ReferenceFilePaths = options.ReferenceFilePaths; typeSystemContext.SetSystemModule(typeSystemContext.GetModuleForSimpleName(DefaultSystemModule)); List <EcmaModule> inputModules = new List <EcmaModule> (); foreach (var inputFile in typeSystemContext.InputFilePaths) { EcmaModule module = typeSystemContext.GetModuleFromPath(inputFile.Value); inputModules.Add(module); } CompilationModuleGroup compilationGroup = new TestInfraMultiFileSharedCompilationModuleGroup(typeSystemContext, inputModules); List <ICompilationRootProvider> compilationRoots = new List <ICompilationRootProvider> (); EcmaModule?entrypointModule = null; foreach (var inputFile in typeSystemContext.InputFilePaths) { EcmaModule module = typeSystemContext.GetModuleFromPath(inputFile.Value); if (module.PEReader.PEHeaders.IsExe) { if (entrypointModule != null) { throw new Exception("Multiple EXE modules"); } entrypointModule = module; } compilationRoots.Add(new ExportedMethodsRootProvider(module)); } compilationRoots.Add(new MainMethodRootProvider(entrypointModule, CreateInitializerList(typeSystemContext, options))); ILProvider ilProvider = new NativeAotILProvider(); ilProvider = new FeatureSwitchManager(ilProvider, options.FeatureSwitches); Logger logger = new Logger(logWriter, ilProvider, isVerbose: true); CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState(ilProvider, logger); UsageBasedMetadataManager metadataManager = new UsageBasedMetadataManager( compilationGroup, typeSystemContext, new NoMetadataBlockingPolicy(), new ManifestResourceBlockingPolicy(options.FeatureSwitches), logFile: null, new NoStackTraceEmissionPolicy(), new NoDynamicInvokeThunkGenerationPolicy(), new FlowAnnotations(logger, ilProvider, compilerGeneratedState), UsageBasedMetadataGenerationOptions.ReflectionILScanning, logger, Array.Empty <KeyValuePair <string, bool> > (), Array.Empty <string> (), options.TrimAssemblies.ToArray()); CompilationBuilder builder = new RyuJitCompilationBuilder(typeSystemContext, compilationGroup) .UseILProvider(ilProvider) .UseCompilationUnitPrefix(""); IILScanner scanner = builder.GetILScannerBuilder() .UseCompilationRoots(compilationRoots) .UseMetadataManager(metadataManager) .UseParallelism(System.Diagnostics.Debugger.IsAttached ? 1 : -1) .ToILScanner(); ILScanResults results = scanner.Scan(); }
public void TestDependencyGraphInvariants(EcmaMethod method) { // // Scan the input method // var context = (CompilerTypeSystemContext)method.Context; CompilationModuleGroup compilationGroup = new SingleFileCompilationModuleGroup(); NativeAotILProvider ilProvider = new NativeAotILProvider(); CompilerGeneratedState compilerGeneratedState = new CompilerGeneratedState(ilProvider, Logger.Null); UsageBasedMetadataManager metadataManager = new UsageBasedMetadataManager(compilationGroup, context, new FullyBlockedMetadataBlockingPolicy(), new FullyBlockedManifestResourceBlockingPolicy(), null, new NoStackTraceEmissionPolicy(), new NoDynamicInvokeThunkGenerationPolicy(), new ILLink.Shared.TrimAnalysis.FlowAnnotations(Logger.Null, ilProvider, compilerGeneratedState), UsageBasedMetadataGenerationOptions.None, Logger.Null, Array.Empty <KeyValuePair <string, bool> >(), Array.Empty <string>(), Array.Empty <string>()); CompilationBuilder builder = new RyuJitCompilationBuilder(context, compilationGroup) .UseILProvider(ilProvider); IILScanner scanner = builder.GetILScannerBuilder() .UseCompilationRoots(new ICompilationRootProvider[] { new SingleMethodRootProvider(method) }) .UseMetadataManager(metadataManager) .ToILScanner(); ILScanResults results = scanner.Scan(); // // Check invariants // const string assetsNamespace = "ILCompiler.Compiler.Tests.Assets"; bool foundSomethingToCheck = false; foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "GeneratesConstructedEETypeAttribute")) { foundSomethingToCheck = true; Assert.Contains((TypeDesc)attr.FixedArguments[0].Value, results.ConstructedEETypes); } foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "NoConstructedEETypeAttribute")) { foundSomethingToCheck = true; Assert.DoesNotContain((TypeDesc)attr.FixedArguments[0].Value, results.ConstructedEETypes); } foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "GeneratesMethodBodyAttribute")) { foundSomethingToCheck = true; MethodDesc methodToCheck = GetMethodFromAttribute(attr); Assert.Contains(methodToCheck.GetCanonMethodTarget(CanonicalFormKind.Specific), results.CompiledMethodBodies); } foreach (var attr in method.GetDecodedCustomAttributes(assetsNamespace, "NoMethodBodyAttribute")) { foundSomethingToCheck = true; MethodDesc methodToCheck = GetMethodFromAttribute(attr); Assert.DoesNotContain(methodToCheck.GetCanonMethodTarget(CanonicalFormKind.Specific), results.CompiledMethodBodies); } // // Make sure we checked something // Assert.True(foundSomethingToCheck, "No invariants to check?"); }