11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . IO ;
45using Xunit ;
56using Xunit . Abstractions ;
67
@@ -16,25 +17,114 @@ public WasmSIMDTests(ITestOutputHelper output, SharedBuildPerTestClassFixture bu
1617 }
1718
1819 [ Theory ]
19- [ MemberData ( nameof ( MainMethodTestData ) , parameters : new object [ ] { /*aot*/ true , RunHost . All } ) ]
20- public void BuildWithSIMD ( BuildArgs buildArgs , RunHost host , string id )
21- => TestMain ( "main_simd_aot" ,
22- @"
23- using System;
24- using System.Runtime.Intrinsics;
25-
26- public class TestClass {
27- public static int Main()
28- {
29- var v1 = Vector128.Create(0x12345678);
30- var v2 = Vector128.Create(0x23456789);
31- var v3 = v1*v2;
32- Console.WriteLine(v3);
33- Console.WriteLine(""Hello, World!"");
34-
35- return 42;
36- }
37- }" ,
38- buildArgs , host , id , extraProperties : "<WasmSIMD>true</WasmSIMD>" ) ;
20+ [ MemberData ( nameof ( MainMethodTestData ) , parameters : new object [ ] { /*aot*/ false , RunHost . All } ) ]
21+ public void BuildWithSIMD_NoAOT_ShouldRelink ( BuildArgs buildArgs , RunHost host , string id )
22+ {
23+ string projectName = $ "sim_with_workload_no_aot";
24+ buildArgs = buildArgs with { ProjectName = projectName } ;
25+ buildArgs = ExpandBuildArgs ( buildArgs , "<WasmEnableSIMD>true</WasmEnableSIMD>" ) ;
26+
27+ ( _ , string output ) = BuildProject ( buildArgs ,
28+ id : id ,
29+ new BuildProjectOptions (
30+ InitProject : ( ) => File . WriteAllText ( Path . Combine ( _projectDir ! , "Program.cs" ) , s_simdProgramText ) ,
31+ Publish : false ,
32+ DotnetWasmFromRuntimePack : false ) ) ;
33+
34+ if ( ! _buildContext . TryGetBuildFor ( buildArgs , out _ ) )
35+ {
36+ // Check if this is not a cached build
37+ Assert . Contains ( "Compiling native assets with excc" , output ) ;
38+ }
39+
40+ RunAndTestWasmApp ( buildArgs ,
41+ extraXHarnessArgs : host == RunHost . NodeJS ? "--engine-arg=--experimental-wasm-simd" : "" ,
42+ expectedExitCode : 42 ,
43+ test : output =>
44+ {
45+ Assert . Contains ( "<-2094756296, -2094756296, -2094756296, -2094756296>" , output ) ;
46+ Assert . Contains ( "Hello, World!" , output ) ;
47+ } , host : host , id : id ) ;
48+ }
49+
50+ [ Theory ]
51+ // https://github.com/dotnet/runtime/issues/75044 - disabled for V8, and NodeJS
52+ //[MemberData(nameof(MainMethodTestData), parameters: new object[] { /*aot*/ true, RunHost.All })]
53+ [ MemberData ( nameof ( MainMethodTestData ) , parameters : new object [ ] { /*aot*/ true , RunHost . Chrome } ) ]
54+ [ MemberData ( nameof ( MainMethodTestData ) , parameters : new object [ ] { /*aot*/ false , RunHost . All } ) ]
55+ public void PublishWithSIMD_AOT ( BuildArgs buildArgs , RunHost host , string id )
56+ {
57+ string projectName = $ "sim_with_workload_aot";
58+ buildArgs = buildArgs with { ProjectName = projectName } ;
59+ buildArgs = ExpandBuildArgs ( buildArgs , "<WasmEnableSIMD>true</WasmEnableSIMD>" ) ;
60+
61+ BuildProject ( buildArgs ,
62+ id : id ,
63+ new BuildProjectOptions (
64+ InitProject : ( ) => File . WriteAllText ( Path . Combine ( _projectDir ! , "Program.cs" ) , s_simdProgramText ) ,
65+ DotnetWasmFromRuntimePack : false ) ) ;
66+
67+ RunAndTestWasmApp ( buildArgs ,
68+ extraXHarnessArgs : host == RunHost . NodeJS ? "--engine-arg=--experimental-wasm-simd" : "" ,
69+ expectedExitCode : 42 ,
70+ test : output =>
71+ {
72+ Assert . Contains ( "<-2094756296, -2094756296, -2094756296, -2094756296>" , output ) ;
73+ Assert . Contains ( "Hello, World!" , output ) ;
74+ } , host : host , id : id ) ;
75+ }
76+
77+ [ Theory , TestCategory ( "no-workload" ) ]
78+ [ InlineData ( "Debug" , /*aot*/ true , /*publish*/ true ) ]
79+ [ InlineData ( "Debug" , /*aot*/ false , /*publish*/ false ) ]
80+ [ InlineData ( "Debug" , /*aot*/ false , /*publish*/ true ) ]
81+ [ InlineData ( "Release" , /*aot*/ true , /*publish*/ true ) ]
82+ [ InlineData ( "Release" , /*aot*/ false , /*publish*/ false ) ]
83+ [ InlineData ( "Release" , /*aot*/ false , /*publish*/ true ) ]
84+ public void BuildWithSIMDNeedsWorkload ( string config , bool aot , bool publish )
85+ {
86+ string id = Path . GetRandomFileName ( ) ;
87+ string projectName = $ "simd_no_workload_{ config } _aot_{ aot } ";
88+ BuildArgs buildArgs = new
89+ (
90+ ProjectName : projectName ,
91+ Config : config ,
92+ AOT : aot ,
93+ ProjectFileContents : "placeholder" ,
94+ ExtraBuildArgs : string . Empty
95+ ) ;
96+
97+ string extraProperties = """
98+ <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
99+ <WasmEnableSIMD>true</WasmEnableSIMD>
100+ """ ;
101+ buildArgs = ExpandBuildArgs ( buildArgs , extraProperties ) ;
102+
103+ ( _ , string output ) = BuildProject ( buildArgs ,
104+ id : id ,
105+ new BuildProjectOptions (
106+ InitProject : ( ) => File . WriteAllText ( Path . Combine ( _projectDir ! , "Program.cs" ) , s_simdProgramText ) ,
107+ Publish : publish ,
108+ ExpectSuccess : false ,
109+ UseCache : false ) ) ;
110+ Assert . Contains ( "following workloads must be installed: wasm-tools" , output ) ;
111+ }
112+
113+ private static string s_simdProgramText = @"
114+ using System;
115+ using System.Runtime.Intrinsics;
116+
117+ public class TestClass {
118+ public static int Main()
119+ {
120+ var v1 = Vector128.Create(0x12345678);
121+ var v2 = Vector128.Create(0x23456789);
122+ var v3 = v1*v2;
123+ Console.WriteLine(v3);
124+ Console.WriteLine(""Hello, World!"");
125+
126+ return 42;
127+ }
128+ }" ;
39129 }
40130}
0 commit comments