Skip to content

Commit

Permalink
Header generator: Check enumerant ordering
Browse files Browse the repository at this point in the history
In the grammar, enforce ordering rules:
- Instructions must appear in order of their opcode
- Non-instructions: each successive enumerant within a single kind must
  appear in order

- Reorder enumerants Subgroup*MaskKHR enums to satisfy the rule.
  • Loading branch information
dneto0 committed Jan 27, 2021
1 parent faa570a commit e50154d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
36 changes: 18 additions & 18 deletions include/spirv/unified1/spirv.core.grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -10407,55 +10407,55 @@
"version" : "1.3"
},
{
"enumerant" : "SubgroupGeMask",
"value" : 4417,
"enumerant" : "SubgroupEqMaskKHR",
"value" : 4416,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupGtMask",
"value" : 4418,
"enumerant" : "SubgroupGeMask",
"value" : 4417,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupLeMask",
"value" : 4419,
"enumerant" : "SubgroupGeMaskKHR",
"value" : 4417,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupLtMask",
"value" : 4420,
"enumerant" : "SubgroupGtMask",
"value" : 4418,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupEqMaskKHR",
"value" : 4416,
"enumerant" : "SubgroupGtMaskKHR",
"value" : 4418,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupGeMaskKHR",
"value" : 4417,
"enumerant" : "SubgroupLeMask",
"value" : 4419,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupGtMaskKHR",
"value" : 4418,
"enumerant" : "SubgroupLeMaskKHR",
"value" : 4419,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
"enumerant" : "SubgroupLeMaskKHR",
"value" : 4419,
"enumerant" : "SubgroupLtMask",
"value" : 4420,
"capabilities" : [ "SubgroupBallotKHR", "GroupNonUniformBallot" ],
"extensions" : [ "SPV_KHR_shader_ballot" ],
"version" : "1.3"
},
{
Expand Down
14 changes: 8 additions & 6 deletions include/spirv/unified1/spirv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,14 +1106,16 @@ public enum FragmentShadingRateMask
Horizontal4Pixels = 0x00000008,
}

public enum FPDenormMode {
Preserve = 0,
FlushToZero = 1,
public enum FPDenormMode
{
Preserve = 0,
FlushToZero = 1,
}

public enum FPOperationMode {
IEEE = 0,
ALT = 1,
public enum FPOperationMode
{
IEEE = 0,
ALT = 1,
}

public enum Op
Expand Down
12 changes: 6 additions & 6 deletions include/spirv/unified1/spirv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,15 +1106,15 @@ typedef enum SpvFragmentShadingRateMask_ {
} SpvFragmentShadingRateMask;

typedef enum SpvFPDenormMode_ {
SpvFPDenormModePreserve = 0,
SpvFPDenormModeFlushToZero = 1,
SpvFPDenormModeMax = 0x7fffffff,
SpvFPDenormModePreserve = 0,
SpvFPDenormModeFlushToZero = 1,
SpvFPDenormModeMax = 0x7fffffff,
} SpvFPDenormMode;

typedef enum SpvFPOperationMode_ {
SpvFPOperationModeIEEE = 0,
SpvFPOperationModeALT = 1,
SpvFPOperationModeMax = 0x7fffffff,
SpvFPOperationModeIEEE = 0,
SpvFPOperationModeALT = 1,
SpvFPOperationModeMax = 0x7fffffff,
} SpvFPOperationMode;

typedef enum SpvOp_ {
Expand Down
12 changes: 6 additions & 6 deletions include/spirv/unified1/spirv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,15 +1102,15 @@ enum FragmentShadingRateMask {
};

enum FPDenormMode {
FPDenormModePreserve = 0,
FPDenormModeFlushToZero = 1,
FPDenormModeMax = 0x7fffffff,
FPDenormModePreserve = 0,
FPDenormModeFlushToZero = 1,
FPDenormModeMax = 0x7fffffff,
};

enum FPOperationMode {
FPOperationModeIEEE = 0,
FPOperationModeALT = 1,
FPOperationModeMax = 0x7fffffff,
FPOperationModeIEEE = 0,
FPOperationModeALT = 1,
FPOperationModeMax = 0x7fffffff,
};

enum Op {
Expand Down
31 changes: 31 additions & 0 deletions tools/buildHeaders/jsonToSpirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -326,6 +327,8 @@ void jsonToSpirv(const std::string& jsonPath, bool buildingHeaders)

// process the instructions
const Json::Value insts = root["instructions"];
unsigned maxOpcode = 0;
bool firstOpcode = true;
for (const auto& inst : insts) {
const auto printingClass = inst["class"].asString();
if (printingClass.size() == 0) {
Expand All @@ -341,6 +344,19 @@ void jsonToSpirv(const std::string& jsonPath, bool buildingHeaders)
}
const auto opcode = inst["opcode"].asUInt();
const std::string name = inst["opname"].asString();
if (firstOpcode) {
maxOpcode = opcode;
firstOpcode = false;
} else {
if (maxOpcode > opcode) {
std::cerr << "Error: " << name
<< " is out of order. It follows the instruction with opcode " << maxOpcode
<< std::endl;
std::exit(1);
} else {
maxOpcode = opcode;
}
}
EnumCaps caps = getCaps(inst);
std::string version = inst["version"].asString();
std::string lastVersion = inst["lastVersion"].asString();
Expand Down Expand Up @@ -384,12 +400,27 @@ void jsonToSpirv(const std::string& jsonPath, bool buildingHeaders)
return result;
};

unsigned maxValue = 0;
bool firstValue = true;
for (const auto& enumerant : source["enumerants"]) {
unsigned value;
bool skip_zero_in_bitfield;
std::tie(value, skip_zero_in_bitfield) = getValue(enumerant);
if (skip_zero_in_bitfield)
continue;
if (firstValue) {
maxValue = value;
firstValue = false;
} else {
if (maxValue > value) {
std::cerr << "Error: " << source["kind"] << " enumerant " << enumerant["enumerant"]
<< " is out of order. It has value " << value
<< " but follows the enumerant with value " << maxValue << std::endl;
std::exit(1);
} else {
maxValue = value;
}
}
EnumCaps caps(getCaps(enumerant));
std::string version = enumerant["version"].asString();
std::string lastVersion = enumerant["lastVersion"].asString();
Expand Down

0 comments on commit e50154d

Please sign in to comment.