Skip to content

Commit

Permalink
Use AssemblyGrammar for capability validation.
Browse files Browse the repository at this point in the history
Also:
- ForEach() for spv_capability_mask_t.
- Add capability min/max constants.
- Move max definition from validate_types.cpp to spirv_definition.h.
  • Loading branch information
Dejan Mircevski authored and dneto0 committed Feb 2, 2016
1 parent 5f99fc3 commit 0c8bdfe
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 847 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ if (NOT ${SPIRV_SKIP_EXECUTABLES})
${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.Storage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.SSA.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/ValidateID.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/ValidationState.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp)

add_executable(UnitSPIRV ${TEST_SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion include/libspirv/libspirv.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern "C" {

// Helpers

#define spvIsInBitfield(value, bitfield) (value == (value & bitfield))
#define spvIsInBitfield(value, bitfield) ((value) == ((value) & bitfield))

#define SPV_BIT(shift) (1 << (shift))

Expand Down
20 changes: 20 additions & 0 deletions source/spirv_definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#ifndef LIBSPIRV_SPIRV_DEFINITION_H_
#define LIBSPIRV_SPIRV_DEFINITION_H_

#include <cstdint>

#include "libspirv/libspirv.h"

// A bit mask representing a set of capabilities.
// Currently there are 57 distinct capabilities, so 64 bits
// should be enough.
Expand All @@ -39,6 +43,22 @@ typedef uint64_t spv_capability_mask_t;
#define SPV_CAPABILITY_AS_MASK(capability) \
(spv_capability_mask_t(1) << (capability))

// Min/max capability IDs.
enum {
kCapabilitiesMinValue = SpvCapabilityMatrix,
kCapabilitiesMaxValue = SpvCapabilityStorageImageWriteWithoutFormat
};

// Applies f to every capability present in a mask.
namespace libspirv {
template <typename Functor>
inline void ForEach(spv_capability_mask_t capabilities, Functor f) {
for (int cap = kCapabilitiesMinValue; cap <= kCapabilitiesMaxValue; ++cap)
if (spvIsInBitfield(SPV_CAPABILITY_AS_MASK(cap), capabilities))
f(static_cast<SpvCapability>(cap));
}
} // end namespace libspirv

typedef struct spv_header_t {
uint32_t magic;
uint32_t version;
Expand Down
2 changes: 1 addition & 1 deletion source/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ spv_result_t spvValidate(const spv_const_context context,

// NOTE: Parse the module and perform inline validation checks. These
// checks do not require the the knowledge of the whole module.
ValidationState_t vstate(pDiagnostic, options);
ValidationState_t vstate(pDiagnostic, options, context);
spvCheckReturn(spvBinaryParse(context, &vstate, binary->code,
binary->wordCount, setHeader,
ProcessInstruction, pDiagnostic));
Expand Down
18 changes: 14 additions & 4 deletions source/validate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@

#include "libspirv/libspirv.h"

#include "assembly_grammar.h"
#include "binary.h"
#include "diagnostic.h"
#include "instruction.h"
#include "spirv_definition.h"
#include "table.h"

// Structures
Expand Down Expand Up @@ -171,7 +173,8 @@ class Functions {

class ValidationState_t {
public:
ValidationState_t(spv_diagnostic* diagnostic, uint32_t options);
ValidationState_t(spv_diagnostic* diagnostic, uint32_t options,
const spv_const_context context);

// Forward declares the id in the module
spv_result_t forwardDeclareId(uint32_t id);
Expand Down Expand Up @@ -266,8 +269,14 @@ class ValidationState_t {
// Registers the capability and its dependent capabilities
void registerCapability(SpvCapability cap);

// Returns true if the capabillity is enabled in the module
bool hasCapability(SpvCapability cap);
// Returns true if the capability is enabled in the module.
bool hasCapability(SpvCapability cap) const;

// Returns true if any of the capabilities are enabled. Always true for
// capabilities==0.
bool HasAnyOf(spv_capability_mask_t capabilities) const;

AssemblyGrammar& grammar() { return grammar_; }

private:
spv_diagnostic* diagnostic_;
Expand Down Expand Up @@ -298,6 +307,7 @@ class ValidationState_t {
// IDs that are entry points, ie, arguments to OpEntryPoint.
std::vector<uint32_t> entry_points_;

AssemblyGrammar grammar_;
};

} // namespace libspirv
Expand Down Expand Up @@ -342,7 +352,7 @@ spv_result_t spvValidateIDs(const spv_instruction_t* pInstructions,
const spv_ext_inst_table extInstTable,
spv_position position, spv_diagnostic* pDiagnostic);

#define spvCheckReturn(expression) \
#define spvCheckReturn(expression) \
if (spv_result_t error = (expression)) return error;

#endif // LIBSPIRV_VALIDATE_H_
Loading

0 comments on commit 0c8bdfe

Please sign in to comment.