Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions examples/QIR/Development/Development.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<!-- The following references need to be removed once the Sdk version number is updated. -->
<PackageReference Condition="$([MSBuild]::IsOsPlatform('Windows'))"
Include="libLLVM.runtime.win-x64" Version="11.0.0"
PrivateAssets="All" GeneratePathProperty="true" />
<PackageReference Condition="$([MSBuild]::IsOsPlatform('OSX'))"
Include="libLLVM.runtime.osx-x64" Version="11.0.0"
PrivateAssets="All" GeneratePathProperty="true" />
<PackageReference Condition="$([MSBuild]::IsOsPlatform('Linux')) And '$(UbuntuVersion)' == '18.04'"
Include="libLLVM.runtime.ubuntu.18.04-x64" Version="11.0.0"
PrivateAssets="All" GeneratePathProperty="true" />
<PackageReference Condition="$([MSBuild]::IsOsPlatform('Linux')) And '$(UbuntuVersion)' != '18.04'"
Include="libLLVM.runtime.ubuntu.20.04-x64" Version="11.0.0"
PrivateAssets="All" GeneratePathProperty="true" />
</ItemGroup>

<PropertyGroup>
<CSharpGeneration>false</CSharpGeneration>
<QscExe>dotnet $(MSBuildThisFileDirectory)../../../src/QsCompiler/CommandLineTool/bin/$(Configuration)/netcoreapp3.1/qsc.dll</QscExe>
</PropertyGroup>

<ItemGroup>
<None Include="qir\**" />
<UpToDateCheckInput Include="@(None)" />
<None Include="$(QirOutputPath)**" />
<Compile Remove="Main.cs" />
</ItemGroup>

Expand All @@ -28,18 +45,28 @@
and files with the generated QIR are deleted when the project is cleaned
despite that we added them in a way that displays them as part of the project.
-->
<Target Name="CleanAll" BeforeTargets="QSharpClean">

<Target Name="CleanQir" AfterTargets="QSharpClean" DependsOnTargets="Restore">
<ItemGroup>
<_FilesToClean Include="$(GeneratedFilesOutputPath)**" />
<_FilesToClean Include="$(QirOutputPath)**/*.ll" />
<Compile Remove="$(_FilesToClean)**/*.g.cs" />
<_QirFilesToClean Include="$(QirOutputPath)**" />
<None Remove="@(_QirFilesToClean)" />
</ItemGroup>
<Delete Files="@(_FilesToClean)" />
<Delete Files="@(_QirFilesToClean)" />
<RemoveDir Directories="$(QirOutputPath)" />
</Target>

<Target Name="BeforeQSharpCompile" DependsOnTargets="CleanAll">
<Target Name="BeforeQSharpCompile" DependsOnTargets="QSharpClean">
<Message Text="Removed files from prior compilation." Importance="High" />
<!-- The following lines need to be removed once the Sdk version number is updated. -->
<PropertyGroup Condition="'$(LlvmLibsPath)' == ''">
<LlvmLibsPath Condition="$([MSBuild]::IsOsPlatform('OSX'))">$(PkglibLLVM_runtime_osx-x64)</LlvmLibsPath>
<LlvmLibsPath Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(PkglibLLVM_runtime_win-x64)</LlvmLibsPath>
<LlvmLibsPath Condition="$([MSBuild]::IsOsPlatform('Linux')) And '$(UbuntuVersion)' == '18.04'">$(PkglibLLVM_runtime_ubuntu_18_04-x64)</LlvmLibsPath>
<LlvmLibsPath Condition="$([MSBuild]::IsOsPlatform('Linux')) And '$(UbuntuVersion)' != '18.04'">$(PkglibLLVM_runtime_ubuntu_20_04-x64)</LlvmLibsPath>
</PropertyGroup>
<PropertyGroup>
<AdditionalQscArguments>--llvm-libs $(LlvmLibsPath)/</AdditionalQscArguments>
</PropertyGroup>
</Target>

<Target Name="BeforeCSharpCompile">
Expand Down
4 changes: 0 additions & 4 deletions src/QsCompiler/CommandLineTool/CommandLineTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0" />
<PackageReference Include="libLLVM.runtime.win-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.osx-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.ubuntu.20.04-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.ubuntu.18.04-x64" Version="11.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/QsCompiler/CommandLineTool/Commands/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ internal static bool IncorporateResponseFiles(
BuildOptions options, [NotNullWhen(true)] out BuildOptions? incorporated, ILogger? logger = null)
{
incorporated = null;
var responseFiles = options.ResponseFiles;
while (options.ResponseFiles != null && options.ResponseFiles.Any())
{
try
Expand All @@ -110,6 +111,7 @@ internal static bool IncorporateResponseFiles(
}
}
incorporated = options;
incorporated.ResponseFiles = responseFiles;
return true;
}
}
Expand Down Expand Up @@ -203,11 +205,15 @@ public static int Run(BuildOptions options, ConsoleLogger logger)
{
if (!BuildOptions.IncorporateResponseFiles(options, out var incorporated))
{
options.Print(logger);
logger.Log(ErrorCode.InvalidCommandLineArgsInResponseFiles, Array.Empty<string>());
return ReturnCode.InvalidArguments;
}

options = incorporated;
options.Print(logger);
options.SetupLoadingContext();

var usesPlugins = options.Plugins != null && options.Plugins.Any();
if (!options.ParseAssemblyProperties(out var assemblyConstants))
{
Expand Down
2 changes: 2 additions & 0 deletions src/QsCompiler/CommandLineTool/Commands/Diagnose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public static IEnumerable<QsStatement> StripSnippetWrapping(IEnumerable<QsNamesp
/// </summary>
public static int Run(DiagnoseOptions options, ConsoleLogger logger)
{
options.Print(logger);
options.SetupLoadingContext();
if (!options.ParseAssemblyProperties(out var assemblyConstants))
{
logger.Log(WarningCode.InvalidAssemblyProperties, Array.Empty<string>());
Expand Down
5 changes: 3 additions & 2 deletions src/QsCompiler/CommandLineTool/Commands/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ ImmutableDictionary<Uri, string> LoadSources(SourceFileLoader loadFromDisk) =>
.ToImmutableDictionary(entry => entry.Key, entry => UpdateArrayLiterals(entry.Value)); // manually replace array literals

// no rewrite steps, no generation
var loaded =
new CompilationLoader(LoadSources, options.References ?? Enumerable.Empty<string>(), logger: logger);
options.Print(logger);
var loaded = new CompilationLoader(LoadSources, options.References ?? Enumerable.Empty<string>(), logger: logger);

if (ReturnCode.Status(loaded) == ReturnCode.UnresolvedFiles)
{
return ReturnCode.UnresolvedFiles; // ignore compilation errors
Expand Down
26 changes: 19 additions & 7 deletions src/QsCompiler/CommandLineTool/LoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace Microsoft.Quantum.QsCompiler
/// </summary>
public class LoadContext : AssemblyLoadContext
{
internal static HashSet<string> ManagedDllPaths = new HashSet<string>();
internal static HashSet<string> UnmanagedDllPaths = new HashSet<string>();

public readonly string PathToParentAssembly;
private readonly AssemblyDependencyResolver resolver;
private readonly HashSet<string> fallbackPaths;
Expand Down Expand Up @@ -50,22 +53,22 @@ private LoadContext(string parentAssembly)
protected override IntPtr LoadUnmanagedDll(string name)
{
var path = this.resolver.ResolveUnmanagedDllToPath(name);
path ??= this.ResolveFromFallbackPaths(name);
path ??= ResolveFromPaths(name, this.fallbackPaths);
return path == null ? IntPtr.Zero : this.LoadUnmanagedDllFromPath(path);
}

/// <summary>
/// Search all fallback paths for a suitable .dll, .dylib, or .so file, ignoring all exceptions.
/// Returns the full path to the file if such a file was found.
/// </summary>
private string? ResolveFromFallbackPaths(string name)
internal static string? ResolveFromPaths(string name, IEnumerable<string> paths)
{
bool MatchByName(string file) =>
Path.GetFileNameWithoutExtension(file)
.Equals(name, StringComparison.InvariantCultureIgnoreCase);

var found = new List<string>();
foreach (var dir in this.fallbackPaths)
foreach (var dir in paths)
{
try
{
Expand Down Expand Up @@ -108,14 +111,14 @@ bool MatchByName(string file) =>
/// Search all fallback paths for a suitable .dll ignoring all exceptions.
/// Returns the full path to the dll if a suitable assembly was found.
/// </summary>
private string? ResolveFromFallbackPaths(AssemblyName name)
internal static string? ResolveFromPaths(AssemblyName name, string pathToParentAssembly, IEnumerable<string> paths)
{
bool MatchByName(string file) =>
Path.GetFileNameWithoutExtension(file)
.Equals(name.Name, StringComparison.InvariantCultureIgnoreCase);

var found = new List<string>();
foreach (var dir in this.fallbackPaths)
foreach (var dir in paths)
{
try
{
Expand All @@ -132,7 +135,7 @@ bool MatchByName(string file) =>
return found.FirstOrDefault();
}

var tempContext = new LoadContext(this.PathToParentAssembly);
var tempContext = new LoadContext(pathToParentAssembly);
var versions = new List<(string, Version?)>();
foreach (var file in found)
{
Expand Down Expand Up @@ -172,7 +175,7 @@ bool MatchByName(string file) =>
/// </summary>
private Assembly? OnResolving(AssemblyLoadContext context, AssemblyName name)
{
var path = this.ResolveFromFallbackPaths(name);
var path = ResolveFromPaths(name, this.PathToParentAssembly, this.fallbackPaths);
return path == null ? null : this.LoadFromAssemblyPath(path);
}

Expand Down Expand Up @@ -210,6 +213,15 @@ public static Assembly LoadAssembly(string path, string[]? fallbackPaths = null)
{
context.AddToPath(fallbackPaths);
}
foreach (var preload in UnmanagedDllPaths)
{
context.LoadUnmanagedDllFromPath(preload);
}
foreach (var preload in ManagedDllPaths)
{
context.LoadFromAssemblyPath(preload);
}

Loaded.Add(context);
var assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(path));
return context.LoadFromAssemblyName(assemblyName);
Expand Down
44 changes: 34 additions & 10 deletions src/QsCompiler/CommandLineTool/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ public class CompilationOptions : Options
HelpText = "Specifies whether to build a Q# command line application.")]
public bool MakeExecutable { get; set; }

[Option(
"package-load-fallback-folders",
Required = false,
SetName = CodeMode,
HelpText = "Specifies the directories the compiler will search when a compiler dependency could not be found.")]
public IEnumerable<string>? PackageLoadFallbackFolders { get; set; }

[Option(
"llvm-libs",
Required = false,
SetName = CodeMode,
HelpText = "Specifies the folder where the llvm libraries can be found.")]
public string? LlvmLibs { get; set; }

/// <summary>
/// Returns a dictionary with the specified assembly properties as out parameter.
/// Returns a boolean indicating whether all specified properties were successfully added.
Expand All @@ -117,6 +131,25 @@ internal bool ParseAssemblyProperties(out Dictionary<string, string> parsed)
}
return success;
}

/// <summary>
/// Configures the LoadingContext for this assembly depending on the defined options.
/// </summary>
internal void SetupLoadingContext()
{
var current = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var fallbackFolders = this.PackageLoadFallbackFolders.Prepend(current);
CompilationLoader.LoadAssembly = path =>
LoadContext.LoadAssembly(path, fallbackFolders.ToArray());

var llvmLibs = this.LlvmLibs != null
? LoadContext.ResolveFromPaths("libLLVM", new[] { this.LlvmLibs })
: null;
if (llvmLibs != null)
{
LoadContext.UnmanagedDllPaths.Add(llvmLibs);
}
}
}

public class Options
Expand Down Expand Up @@ -188,13 +221,6 @@ public enum LogFormat
HelpText = "Warnings with the given code(s) will be ignored.")]
public IEnumerable<int>? NoWarn { get; set; }

[Option(
"package-load-fallback-folders",
Required = false,
SetName = CodeMode,
HelpText = "Specifies the directories the compiler will search when a compiler dependency could not be found.")]
public IEnumerable<string>? PackageLoadFallbackFolders { get; set; }

/// <summary>
/// Updates the settings that can be used independent on the other arguments according to the setting in the given options.
/// Already specified non-default values are prioritized over the values in the given options,
Expand Down Expand Up @@ -241,8 +267,7 @@ internal static Func<Diagnostic, string> LoggingFormat(LogFormat format) =>
};

/// <summary>
/// Creates a suitable logger for the given command line options,
/// logging the given arguments if the verbosity is high enough.
/// Creates a suitable logger for the given command line options.
/// </summary>
public ConsoleLogger GetLogger(DiagnosticSeverity defaultVerbosity = DiagnosticSeverity.Warning)
{
Expand All @@ -261,7 +286,6 @@ public ConsoleLogger GetLogger(DiagnosticSeverity defaultVerbosity = DiagnosticS
verbosity,
this.NoWarn,
this.CodeSnippet != null ? -2 : 0);
this.Print(logger);
return logger;
}

Expand Down
5 changes: 0 additions & 5 deletions src/QsCompiler/CommandLineTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ private static int Run<T>(Func<T, ConsoleLogger, int> compile, T options)
var logger = options.GetLogger();
try
{
var current = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var fallbackFolders = options.PackageLoadFallbackFolders.Prepend(current);
CompilationLoader.LoadAssembly = path =>
LoadContext.LoadAssembly(path, fallbackFolders.ToArray());

var result = compile(options, logger);
logger.ReportSummary(result);
return result;
Expand Down
6 changes: 1 addition & 5 deletions src/QsCompiler/LlvmBindings/LlvmBindings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LLVMSharp" Version="11.0.0-beta" />
<PackageReference Include="libLLVM.runtime.win-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.osx-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.ubuntu.20.04-x64" Version="11.0.0" />
<PackageReference Include="libLLVM.runtime.ubuntu.18.04-x64" Version="11.0.0" />
<PackageReference Include="LLVMSharp" Version="11.0.0-beta" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions src/QsCompiler/Tests.Compiler/Tests.Compiler.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@
<None Include="TestCases\LinkingTests\ReferenceLinking.qs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestCases\LinkingTests\SyntaxTreeTrim.qs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestCases\LinkingTests\SyntaxTreeTrim.qs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="TestCases\LinkingTests\ValidEntryPoints.qs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -536,6 +536,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="libLLVM.runtime.win-x64" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="libLLVM.runtime.osx-x64" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="libLLVM.runtime.ubuntu.20.04-x64" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="libLLVM.runtime.ubuntu.18.04-x64" Version="11.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
Expand Down
8 changes: 4 additions & 4 deletions src/QuantumSdk/DefaultItems/DefaultItems.targets
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
<ValidExecutionTargets Condition="'$(ResolvedQSharpOutputType)' == 'QSharpLibrary'">The execution target for a Q# library needs to be 'Any'.</ValidExecutionTargets>
</PropertyGroup>

<!-- Resolving the QuantumIntrinsics to either OpenQASM, ExtendedQASM, or Default.-->
<!-- Resolving the QuantumIntrinsics to either Type1, Type2, Type3, or Default.-->
<PropertyGroup>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'HoneywellProcessor'">OpenQASM</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'IonQProcessor'">ExtendedQASM</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'QCIProcessor'">OpenQASM</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'HoneywellProcessor'">Type1</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'IonQProcessor'">Type2</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' == 'QCIProcessor'">Type3</ResolvedQuantumIntrinsics>
<ResolvedQuantumIntrinsics Condition="'$(ResolvedProcessorArchitecture)' != 'HoneywellProcessor' And '$(ResolvedProcessorArchitecture)' != 'IonQProcessor' And '$(ResolvedProcessorArchitecture)' != 'QCIProcessor'">Default</ResolvedQuantumIntrinsics>
</PropertyGroup>

Expand Down
Loading