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

Added Generated Files For SPIR-V Builder #841

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ ModelManifest.xml

# Rider
.idea/
Src/.idea/

# macOS
.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Tools/SPIRVGenerationTool/SPIRV-Headers"]
path = Tools/SPIRVGenerationTool/SPIRV-Headers
url = https://github.com/KhronosGroup/SPIRV-Headers
10,439 changes: 10,439 additions & 0 deletions Src/ILGPU/Backends/SPIRV/BinarySPIRVBuilder.cs

Large diffs are not rendered by default.

1,483 changes: 1,483 additions & 0 deletions Src/ILGPU/Backends/SPIRV/ISPIRVBuilder.cs

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions Src/ILGPU/Backends/SPIRV/SPIRVBuilderUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: SPIRVBuilderUtils.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

namespace ILGPU.Backends.SPIRV
{
internal static class SPIRVBuilderUtils
{
public static uint JoinOpCodeWordCount(ushort opCode, ushort wordCount)
{
uint opCodeUint = opCode;
uint wordCountUint = wordCount;

uint shiftedWordCount = wordCountUint << 16;

return shiftedWordCount | opCodeUint;
}
}
}
65 changes: 65 additions & 0 deletions Src/ILGPU/Backends/SPIRV/SPIRVWord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: SPIRVWord.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;

namespace ILGPU.Backends.SPIRV
{
internal struct SPIRVWord
{
public uint Data { get; }
private const int BytesPerWord = sizeof(uint);

public SPIRVWord(uint value)
{
Data = value;
}

public static SPIRVWord FromBytes(ReadOnlySpan<byte> bytes)
{
if (bytes.Length > BytesPerWord)
{
throw new ArgumentException(
"The provided span must be at most 4 bytes long.",
nameof(bytes));
}

return new SPIRVWord(BitConverter.ToUInt32(bytes.ToArray(), 0));
}

public static SPIRVWord[] ManyFromBytes(ReadOnlySpan<byte> bytes)
{
// Round up bytes.Length / BytesPerWord
var words = new SPIRVWord[(bytes.Length - 1) / BytesPerWord + 1];

for (int i = 0; i < words.Length; i++)
{
int bytesIndex = i * BytesPerWord;
// Check if we can take a word more bytes,
// if we can't then just take what's left
if (bytesIndex + BytesPerWord > bytes.Length)
{
words[i] = FromBytes(bytes.Slice(bytesIndex));
}
else
{
words[i] = FromBytes(bytes.Slice(bytesIndex, BytesPerWord));
}
}

return words;
}

public override string ToString() => Data.ToString();

public static implicit operator SPIRVWord(uint u) => new SPIRVWord(u);
}
}
20 changes: 20 additions & 0 deletions Src/ILGPU/Backends/SPIRV/Types/ISPIRVType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: ISPIRVType.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

namespace ILGPU.Backends.SPIRV.Types
{
internal interface ISPIRVType
{
SPIRVWord[] ToWords();

string ToRepr();
}
}
114 changes: 114 additions & 0 deletions Src/ILGPU/Backends/SPIRV/Types/SPIRVLiteralTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2023 ILGPU Project
// www.ilgpu.net
//
// File: SPIRVLiteralTypes.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Text;

namespace ILGPU.Backends.SPIRV.Types
{
internal readonly struct LiteralInteger : ISPIRVType
{
private readonly int _value;

public LiteralInteger(int val)
{
_value = val;
}

public SPIRVWord[] ToWords() =>
new[] {SPIRVWord.FromBytes(BitConverter.GetBytes(_value))};

public string ToRepr() => _value.ToString();
}

internal readonly struct LiteralFloat : ISPIRVType
{
private readonly float _value;

public LiteralFloat(float val)
{
_value = val;
}

public SPIRVWord[] ToWords() =>
new[] {SPIRVWord.FromBytes(BitConverter.GetBytes(_value))};

public string ToRepr() => _value.ToString(CultureInfo.InvariantCulture);
}

internal readonly struct LiteralString : ISPIRVType
{
private readonly string _value;

public LiteralString(string val)
{
_value = val + "\000";
}

public SPIRVWord[] ToWords() =>
SPIRVWord.ManyFromBytes(Encoding.UTF8.GetBytes(_value));

public string ToRepr() => _value;
}

internal readonly struct LiteralContextDependentNumber : ISPIRVType
{
private readonly LiteralFloat? _floatValue;
private readonly LiteralInteger? _intValue;

public LiteralContextDependentNumber(LiteralFloat val)
{
_floatValue = val;
_intValue = null;
}

public LiteralContextDependentNumber(LiteralInteger val)
{
_intValue = val;
_floatValue = null;
}

public SPIRVWord[] ToWords() => _floatValue?.ToWords() ?? _intValue?.ToWords()!;

public string ToRepr() => _floatValue?.ToRepr() ?? _intValue?.ToRepr()!;
}

internal readonly struct LiteralExtInstInteger
{
private readonly uint _value;

public LiteralExtInstInteger(uint val)
{
_value = val;
}

public SPIRVWord[] ToWords() =>
new[] {SPIRVWord.FromBytes(BitConverter.GetBytes(_value))};

public string ToRepr() => _value.ToString();
}

internal readonly struct LiteralSpecConstantOpInteger
{
private readonly uint _value;

public LiteralSpecConstantOpInteger(uint val)
{
_value = val;
}

public SPIRVWord[] ToWords() =>
new[] {SPIRVWord.FromBytes(BitConverter.GetBytes(_value))};

public string ToRepr() => _value.ToString();
}
}
Loading
Loading