summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-spirv-core-grammar.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/compiler-core/slang-spirv-core-grammar.cpp
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/compiler-core/slang-spirv-core-grammar.cpp')
-rw-r--r--source/compiler-core/slang-spirv-core-grammar.cpp150
1 files changed, 71 insertions, 79 deletions
diff --git a/source/compiler-core/slang-spirv-core-grammar.cpp b/source/compiler-core/slang-spirv-core-grammar.cpp
index 0660954fa..8b5e79071 100644
--- a/source/compiler-core/slang-spirv-core-grammar.cpp
+++ b/source/compiler-core/slang-spirv-core-grammar.cpp
@@ -2,8 +2,9 @@
#include "../core/slang-rtti-util.h"
#include "../core/slang-string-util.h"
-#include "slang-json-native.h"
#include "slang-core-diagnostics.h"
+#include "slang-json-native.h"
+
#include <limits>
namespace Slang
@@ -22,8 +23,7 @@ struct InstructionPrintingClass
SLANG_MAKE_STRUCT_RTTI_INFO(
InstructionPrintingClass,
SLANG_RTTI_FIELD(tag),
- SLANG_OPTIONAL_RTTI_FIELD(heading)
-);
+ SLANG_OPTIONAL_RTTI_FIELD(heading));
struct Operand
{
@@ -35,7 +35,7 @@ SLANG_MAKE_STRUCT_RTTI_INFO(
Operand,
SLANG_RTTI_FIELD(kind),
SLANG_OPTIONAL_RTTI_FIELD(quantifier)
- //SLANG_RTTI_FIELD(name),
+ // SLANG_RTTI_FIELD(name),
);
struct Instruction
@@ -52,8 +52,7 @@ SLANG_MAKE_STRUCT_RTTI_INFO(
SLANG_RTTI_FIELD_IMPL(class_, "class", 0),
SLANG_RTTI_FIELD(opcode),
SLANG_OPTIONAL_RTTI_FIELD(capabilities),
- SLANG_OPTIONAL_RTTI_FIELD(operands)
-);
+ SLANG_OPTIONAL_RTTI_FIELD(operands));
struct Enumerant
{
@@ -88,8 +87,7 @@ SLANG_MAKE_STRUCT_RTTI_INFO(
OperandKind,
SLANG_RTTI_FIELD(category),
SLANG_RTTI_FIELD(kind),
- SLANG_OPTIONAL_RTTI_FIELD(enumerants)
-);
+ SLANG_OPTIONAL_RTTI_FIELD(enumerants));
struct SPIRVSpec
{
@@ -111,47 +109,44 @@ SLANG_MAKE_STRUCT_RTTI_INFO(
// SLANG_RTTI_FIELD(revision)
SLANG_RTTI_FIELD(instruction_printing_class),
SLANG_RTTI_FIELD(instructions),
- SLANG_RTTI_FIELD(operand_kinds)
-);
+ SLANG_RTTI_FIELD(operand_kinds));
static Dictionary<UnownedStringSlice, SpvWord> operandKindToDict(
- JSONContainer& container,
- DiagnosticSink& sink,
- const OperandKind& k)
+ JSONContainer& container,
+ DiagnosticSink& sink,
+ const OperandKind& k)
{
Dictionary<UnownedStringSlice, SpvWord> dict;
dict.reserve(k.enumerants.getCount());
- for(const auto& e : k.enumerants)
+ for (const auto& e : k.enumerants)
{
SpvWord valueInt = 0;
- switch(e.value.getKind())
+ switch (e.value.getKind())
{
- case JSONValue::Kind::Integer:
+ case JSONValue::Kind::Integer:
{
// TODO: Range check here?
valueInt = SpvWord(container.asInteger(e.value));
break;
}
- case JSONValue::Kind::String:
+ case JSONValue::Kind::String:
{
Int i = 0;
const auto str = container.getString(e.value);
- if(SLANG_FAILED(StringUtil::parseInt(str, i)))
+ if (SLANG_FAILED(StringUtil::parseInt(str, i)))
sink.diagnose(
e.value.loc,
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "Expected an integer value"
- );
+ "Expected an integer value");
// TODO: Range check here?
valueInt = SpvWord(i);
break;
- }
- default:
- sink.diagnose(
- e.value.loc,
- MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "Expected an integer value (or a string with an integer inside)"
- );
+ }
+ default:
+ sink.diagnose(
+ e.value.loc,
+ MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
+ "Expected an integer value (or a string with an integer inside)");
}
dict.add(e.enumerant, valueInt);
@@ -166,7 +161,9 @@ static Dictionary<UnownedStringSlice, SpvWord> operandKindToDict(
//
//
//
-RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& source, DiagnosticSink& sink)
+RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(
+ SourceView& source,
+ DiagnosticSink& sink)
{
//
// Load the JSON
@@ -182,14 +179,13 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
SLANG_RETURN_NULL_ON_FAIL(parser.parse(&lexer, &source, &builder, &sink));
JSONToNativeConverter converter(&container, &typeMap, &sink);
SPIRVSpec spec;
- if(SLANG_FAILED(converter.convert(builder.getRootValue(), &spec)))
+ if (SLANG_FAILED(converter.convert(builder.getRootValue(), &spec)))
{
// TODO: not having a source loc here is not great...
sink.diagnoseWithoutSourceView(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "Failed to match SPIR-V grammar JSON to the expected schema"
- );
+ "Failed to match SPIR-V grammar JSON to the expected schema");
return nullptr;
}
@@ -200,42 +196,42 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
res->operandKinds.dict.reserve(spec.operand_kinds.getCount());
uint32_t operandKindIndex = 0;
- for(const auto& c : spec.operand_kinds)
+ for (const auto& c : spec.operand_kinds)
{
- if(operandKindIndex > std::numeric_limits<decltype(OperandKind::index)>::max())
+ if (operandKindIndex > std::numeric_limits<decltype(OperandKind::index)>::max())
{
sink.diagnoseWithoutSourceView(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "Too many enum categories, expected fewer than 256"
- );
+ "Too many enum categories, expected fewer than 256");
}
- res->operandKinds.dict.add(c.kind, {static_cast<decltype(OperandKind::index)>(operandKindIndex)});
+ res->operandKinds.dict.add(
+ c.kind,
+ {static_cast<decltype(OperandKind::index)>(operandKindIndex)});
operandKindIndex++;
}
// It's important we reserve the memory now, as we require the iterators to
// be stable, as references to them are maintained by the OpInfo structs.
Index totalNumOperands = 0;
- for(const auto& i : spec.instructions)
+ for (const auto& i : spec.instructions)
totalNumOperands += i.operands.getCapacity();
res->operandTypesStorage.reserve(totalNumOperands);
res->opcodes.dict.reserve(spec.instructions.getCount());
- for(const auto& i : spec.instructions)
+ for (const auto& i : spec.instructions)
{
res->opcodes.dict.add(i.opname, SpvOp(i.opcode));
- const auto class_ =
- i.class_ == "Type-Declaration" ? OpInfo::TypeDeclaration
- : i.class_ == "Constant-Creation" ? OpInfo::ConstantCreation
- : i.class_ == "Debug" ? OpInfo::Debug
- : OpInfo::Other;
+ const auto class_ = i.class_ == "Type-Declaration" ? OpInfo::TypeDeclaration
+ : i.class_ == "Constant-Creation" ? OpInfo::ConstantCreation
+ : i.class_ == "Debug" ? OpInfo::Debug
+ : OpInfo::Other;
- const auto resultTypeIndex
- = i.operands.findFirstIndex([](const auto& o){return o.kind == "IdResultType";});
- const auto resultIdIndex
- = i.operands.findFirstIndex([](const auto& o){return o.kind == "IdResult";});
+ const auto resultTypeIndex =
+ i.operands.findFirstIndex([](const auto& o) { return o.kind == "IdResultType"; });
+ const auto resultIdIndex =
+ i.operands.findFirstIndex([](const auto& o) { return o.kind == "IdResult"; });
SLANG_ASSERT(resultTypeIndex >= -1 || resultTypeIndex <= 0);
SLANG_ASSERT(resultIdIndex >= -1 || resultTypeIndex <= 1);
@@ -243,9 +239,9 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
uint16_t maxOperandCount = 0;
uint16_t numOperandTypes = 0;
const OperandKind* operandTypes = res->operandTypesStorage.end();
- for(const auto& o : i.operands)
+ for (const auto& o : i.operands)
{
- if(maxOperandCount == 0xffff)
+ if (maxOperandCount == 0xffff)
{
// We are about to overflow maxWordCount, either someone has
// put 2^16 operands in the json, or we have a "*" quantified
@@ -254,18 +250,16 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
sink.diagnoseWithoutSourceView(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "\"*\"-qualified operand wasn't the last operand"
- );
+ "\"*\"-qualified operand wasn't the last operand");
}
const auto catIndex = res->operandKinds.lookup(o.kind);
- if(!catIndex)
+ if (!catIndex)
{
sink.diagnoseWithoutSourceView(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "Operand references a kind which doesn't exist"
- );
+ "Operand references a kind which doesn't exist");
continue;
}
@@ -274,24 +268,23 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
// The number of "ImageOperands" is dependent on the bitmask
// operand, for our purposes treat them as unbounded
- if(o.quantifier == "*" || o.kind == "ImageOperands")
+ if (o.quantifier == "*" || o.kind == "ImageOperands")
{
maxOperandCount = 0xffff;
}
- else if(o.quantifier == "?")
+ else if (o.quantifier == "?")
{
maxOperandCount++;
}
- else if(o.quantifier == "")
+ else if (o.quantifier == "")
{
// This catches the case where an "?" or "*" qualified operand
// appears before any unqualified operands
- if(minOperandCount != maxOperandCount)
+ if (minOperandCount != maxOperandCount)
sink.diagnoseWithoutSourceView(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "\"*\" or \"?\" operand appeared before an unqualified operand"
- );
+ "\"*\" or \"?\" operand appeared before an unqualified operand");
minOperandCount++;
maxOperandCount++;
}
@@ -300,31 +293,30 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
sink.diagnose(
SourceLoc{},
MiscDiagnostics::spirvCoreGrammarJSONParseFailure,
- "quantifier wasn't empty, * or ?"
- );
+ "quantifier wasn't empty, * or ?");
}
}
// There are duplicate opcodes in the json (for renamed instructions,
// or the same instruction with different capabilities), for now just
// keep the first one.
- res->opInfos.dict.addIfNotExists(SpvOp(i.opcode), {
- class_,
- static_cast<int8_t>(resultTypeIndex),
- static_cast<int8_t>(resultIdIndex),
- minOperandCount,
- maxOperandCount,
- numOperandTypes,
- operandTypes
- });
+ res->opInfos.dict.addIfNotExists(
+ SpvOp(i.opcode),
+ {class_,
+ static_cast<int8_t>(resultTypeIndex),
+ static_cast<int8_t>(resultIdIndex),
+ minOperandCount,
+ maxOperandCount,
+ numOperandTypes,
+ operandTypes});
res->opNames.dict.addIfNotExists(SpvOp(i.opcode), i.opname);
}
- for(const auto& k : spec.operand_kinds)
+ for (const auto& k : spec.operand_kinds)
{
const auto kindIndex = res->operandKinds.dict.getValue(k.kind);
const auto d = operandKindToDict(container, sink, k);
- for(const auto& [n, v] : d)
+ for (const auto& [n, v] : d)
{
// Add the string to this slice pool as we'll be taking ownership
// of it shortly but don't want to invalidate it in the meantime.
@@ -336,17 +328,17 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
res->operandKindNames.dict.add(kindIndex, k.kind);
- if(k.kind == "Capability")
- for(const auto& [n, v] : d)
+ if (k.kind == "Capability")
+ for (const auto& [n, v] : d)
res->capabilities.dict.add(n, SpvCapability(v));
// If this starts with Id, and the suffix is also an operand kind,
// assume that this is an Id wrapper
- if(k.kind.startsWith("Id"))
+ if (k.kind.startsWith("Id"))
{
- const UnownedStringSlice underneathIdKind{k.kind.begin()+2, k.kind.end()};
+ const UnownedStringSlice underneathIdKind{k.kind.begin() + 2, k.kind.end()};
OperandKind targetIndex;
- if(res->operandKinds.dict.tryGetValue(underneathIdKind, targetIndex))
+ if (res->operandKinds.dict.tryGetValue(underneathIdKind, targetIndex))
res->operandKindUnderneathIds.dict.add(kindIndex, targetIndex);
}
}
@@ -354,4 +346,4 @@ RefPtr<SPIRVCoreGrammarInfo> SPIRVCoreGrammarInfo::loadFromJSON(SourceView& sour
res->strings.swapWith(container.getStringSlicePool());
return res;
}
-}
+} // namespace Slang