Commit Graph

446 Commits

Author SHA1 Message Date
Lei Zhang
8654caa565 Prepare the C++ interface for publication.
* Use PIMPL idiom in the C++ interface.
* Clean up interface for assembling and disassembling.
* Add validation into C++ interface.
* Add more tests for the C++ interface.
2016-09-16 16:37:27 -04:00
Lei Zhang
b54686d017 Move functions for building modules outside of the C++ interface. 2016-09-16 14:02:47 -04:00
Lei Zhang
869440ebd4 Let SPIRV_ASSERT() exit the program if the assertion fails. 2016-09-16 13:14:57 -04:00
Lei Zhang
114af21994 Add MessageConsumer to PassManager, Pass, and analysis interfaces.
Also convert some uses of assert() in optimization code to use
SPIRV_ASSERT(), SPIRV_UNIMPLEMENTED(), or SPIRV_UNREACHABLE()
accordingly.
2016-09-16 13:14:57 -04:00
Lei Zhang
bfd7cd6d6e Add utility macros for logging messages.
Add the following macros for logging purpose:
* SPIRV_ASSERT
* SPIRV_DEBUG
* SPIRV_UNIMPLEMENTED
* SPIRV_UNREACHABLE

The last two is always turned on, while the first two can only
be turned on in debug build.
2016-09-16 09:56:34 -04:00
Lei Zhang
755f97f534 Add a callback mechanism for communicating messages to callers.
Every time an event happens in the library that the user should be
aware of, the callback will be invoked.

The existing diagnostic mechanism is hijacked internally by a
callback that creates an diagnostic object each time an event
happens.
2016-09-15 12:35:48 -04:00
David Neto
e0dd033414 Make friendly names for built-in variables.
Handles only OpDecorate

Does not handle:
- decorations on struct members
- decoration via OpGroupDecorate
2016-09-14 15:25:24 -04:00
David Neto
1f3fb506e8 Fix validator: OpUndef can be member of a constant composite
This was enabled in SPIR-V 1.0 Rev 7

Fixes: https://github.com/KhronosGroup/SPIRV-Tools/issues/414
2016-09-14 15:21:09 -04:00
David Neto
4e2ed04d6e Clarify: Ok to pass null diagnostic to spvBinaryParse
It's a valid use case: I might mostly trust my binaries and I don't
care to to collect detailed diagnostic feedback.
2016-09-14 15:19:10 -04:00
David Neto
5c9080eea8 Fix validator SSA check: Phi can use its own value sometimes
Defer removal of a Phi's result id from the undefined-forward-reference
set until after you've scanned the arguments.  The reordering is only
significant for Phi.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/415
2016-09-14 15:15:28 -04:00
qining
66f5b4bfc5 Build "spec id->default val str" mapping from string
Add function `ParseDefaultValuesString()` to build the spec id->default
value string mapping required by `SetSpecConstantDefaultValuePass`.
2016-09-14 12:26:34 -04:00
qining
5ac63523d7 Guard against nullptr in number parsing utilities
ParseNumber(): Returns false if the given string is a nullptr.
ParseAndEncodeXXXX(): Returns kInvalidText and populate error message:
  "The given text is a nullptr", if the givne string is a nullptr.
2016-09-13 12:12:31 -04:00
qining
abf57933ea Set default values for spec const decorated by SpecId
The pass instance is constructed with a map from spec id (uint32_t) to
default values in string format. The default value strings will be
parsed to numbers according to the target spec constant type.

If the Spec Id decoration is found to be applied on multiple different
target ids, that decoration instruction (OpDecorate or OpGroupDecorate)
will be skipped. But other decoration instrucitons may still be
processed.
2016-09-12 23:21:42 -04:00
qining
049f80f3b9 Add IsSigned() to opt::Analysis::Integer. 2016-09-08 19:32:20 -04:00
qining
1773b95737 Pull out the number parsing logic
Pull out the number parsing logic from
AssemblyContext::binaryEncodeNumericLiteral() to utilities.

The new utility function: `ParseAndEncodeNumber()` now accepts:
  * number text to parse
  * number type
  * a emit function, which is a function which will be called with each
    parsed uint32 word.
  * a pointer to std::string to be overwritten with error messages.
    (pass nullptr if expect no error message)
and returns:
  * an enum result type to indicate the status

Type/Structs moved to utility:
  * template<typename T> class ClampToZeroIfUnsignedType

New type:
* enum EncodeNumberStatus: success or error code
* NumberType: hold the number type information for the number to be parsed.
 * several helper functions are also added for NumberType.

Functions moved to utility:
  * Helpers:
    * template<typename T> checkRangeAndIfHexThenSignExtend() -> CheckRangeAndIfHex....()
  * Interfaces:
    * template<typename T> parseNumber() -> ParseNumber()
    * binaryEncodeIntegerLiteral() -> ParseAndEncodeIntegerNumber()
    * binaryEncodeFloatingPointLiteral() -> ParseAndEncodeFloatingPointNumber()
    * binaryEncodeNumericLiteral() -> ParseAndEncodeNumber()

Tests added/moved to test/ParseNumber.cpp, including tests for:
* ParseNumber(): This is moved from TextToBinary.cpp to ParseNumber.cpp
* ParseAndEncodeIntegerNumber(): New added
* ParseAndEncodeFloatingPointNumber(): New added
* ParseAndEncodeNumber(): New added

Note that the error messages are kept almost the same as before, but
they may be inappropriate for an utility function. Those will be fixed
in another CL.
2016-09-08 18:42:54 -04:00
Lei Zhang
a35919c008 Merge diagnostic tests and single them out in a separate binary. 2016-09-08 17:26:53 -04:00
Lei Zhang
219f0cc13c Create separate binaries for validation tests. 2016-09-07 17:43:15 -04:00
Lei Zhang
ef600df16e Prefix test binaries with test_, rename UnitSPIRV, update README. 2016-09-07 17:26:18 -04:00
David Neto
9fc8658ef3 Relicense SPIRV-Tools under Apache 2.0
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/383

Finalize v2016.4
2016-09-02 10:00:29 -04:00
David Neto
0e001b70e4 Fix Google copyright lines 2016-09-02 10:00:29 -04:00
Lei Zhang
2ad3b74fa3 Tests for preserving line debug insts even killing its affecting targets. 2016-09-01 11:51:09 -04:00
qining
eb60e2945a Unify constants pass
De-duplicate constants and unifies the uses of constants for a SPIR-V
module. If two constants are defined exactly the same, only one of them
will be kept and all the uses of the removed constant will be redirected
to the kept one.

This pass handles normal constants (defined with
OpConstant{|True|False|Composite}), some spec constants (those defined
with OpSpecConstant{Op|Composite}) and null constants (defined with
OpConstantNull).

There are several cases not handled by this pass:

  1) If there are decorations for the result id of a constant defining
  instruction, that instruction will not be processed. This means the
  instruction won't be used to replace other instructions and other
  instructions won't be used to replace it either.

  2) This pass does not unify null constants (defined with
  OpConstantNull instruction) with their equivalent zero-valued normal
  constants (defined with OpConstant{|False|Composite} with zero as the
  operand values or component values).
2016-09-01 11:46:05 -04:00
David Neto
909d7f9bf5 Refactor CapabilitySet into templated class EnumSet 2016-09-01 09:32:22 -04:00
qining
2ce67252c8 Add forwarding so that passes' ctor can have args.
Also removed the default argument value of `skip_nop` for function
`SinglePassRunAndCheck()` and `SinglePassRunAndDisassemble()`. This is
required to support variadic arguments.
2016-08-31 15:27:55 -04:00
David Neto
273920c554 Handle capabilities larger than value 63
Use libspirv::CapabilitySet instead of a 64-bit mask.

Remove dead function spvOpcodeRequiresCapability and its tests.

The JSON grammar parser is simplified since it just writes the
list of capabilities as a braced list, and takes advantage of
the CapabilitySet intializer-list constructor.
2016-08-30 15:41:39 -04:00
David Neto
47f2a1414b Add libspirv::CapabilitySet
It's optimized for the common case, where capabilities have value
at most 63.
2016-08-30 15:41:39 -04:00
qining
76982706f9 Get annotation instructions of a given id. 2016-08-30 15:08:24 -04:00
qining
ecb5692389 Add AppendNames() interface for AssemblyBuilder 2016-08-29 17:18:31 -04:00
qining
380f36eae1 Fold spec constants to normal constants (values fixed)
For the spec constants defined by OpSpecConstantOp and
OpSpecContantComposite, if all of their operands are constants with
determined values (normal constants whose values are fixed), calculate
the correct values of the spec constants and re-define them as normal
constants.

In short, this pass replaces all the spec constants defined by
OpSpecContantOp and OpSpecConstantComposite with normal constants when
possible. So far not all valid operations of OpSpecConstantOp are
supported, we have several constriction here:

1) Only 32-bit integer and boolean (both scalar and vector) are
supported for any arithmetic operations. Integers in other width (like
64-bit) are not supported.
2) OpSConvert, OpFConvert, OpQuantizeToF16, and all the
operations under Kernel capability, are not supported.
3) OpCompositeInsert is not supported.

Note that this pass does not unify normal constants. This means it is
possible to have new generatd constants defining the same values.
2016-08-29 08:58:53 -04:00
David Neto
1d59aa0777 Pass manager recomputes Id bound automatically.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/371
in the sense that the id bound is correct after all the passes
have been run.  But it might be inconsistent between passes.
2016-08-27 13:19:18 -04:00
David Neto
d7b6362ed7 Add Module::ComputeIdBound
When IDs change, something should use this utility method
and Module::SetIdBound before writing out the binary.
2016-08-26 16:27:12 -04:00
Lei Zhang
0a175eb0d6 Add tests for decoration_empty(). 2016-08-26 14:05:04 -04:00
David Neto
58e1a1e3be Test ir::Instruction::GetOperand and GetInOperand 2016-08-26 10:59:25 -04:00
David Neto
cc60caba1d IrLoader gracefully handles incomplete blocks and functions
This lets us write smaller test cases with the IrLoader, avoiding
boilerplate for function begin/end, and basic block begin/end.

Also ForEachInst is more forgiving of cases where a basic block
doesn't have a label, and when a function doesn't have a defining
or end instruction.
2016-08-26 10:15:15 -04:00
David Neto
53013cc816 Add Instruction begin and end mutable iterators
Refactored the instuction test.
2016-08-25 17:48:58 -04:00
David Neto
2607bbf30b Add Instruction cbegin and cend to access operands 2016-08-25 15:24:32 -04:00
David Neto
97fc6aa3b8 ForEachInst optionally runs on attached debug line insts
Also:
- Add const forms of ForEachInst
- Rewrite Module::ToBinary in terms of ForEachInst
- Add Instruction::ToBinaryWithoutAttachedDebugInsts
- Delete the ToBinary method on Function, BasicBlock, and Instruction
  since it can now be implemented with ForEachInst in a less confusing
  way, e.g. without recursion.
- Preserve debug line instructions on OpFunctionEnd (and store that
  instruction as a unique-pointer, for regularity).
2016-08-25 11:43:22 -04:00
David Neto
97d06a36b0 Mere mention of PointSize BuiltIn does not require Shader capability
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/365 which
was reopened for this.
2016-08-24 10:04:17 -04:00
Lei Zhang
f51d82338e Add a home brewed make_unique to ease creating unique_ptrs. 2016-08-24 09:41:55 -04:00
David Neto
358cb2940a Relax ClipDistance, CullDistance capability check in all environments
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/365
2016-08-23 18:41:44 -04:00
David Neto
ccabcc4673 Add opt::ir::Module::SetIdBound 2016-08-23 15:20:34 -04:00
qining
7e775aa99e Get the number of elements for Vector and Matrix type 2016-08-23 11:34:24 -04:00
qining
b891845b53 Refine the DefUseManager
* Fix the behavior when analyzing an individual instruction:
  * exisiting instruction:
      Clear the original records and re-analyze it as a new instruction.
  * new instruction with exisiting result id:
      Clear the original records of the exisiting result id. This means
      the records of the analyzed result-id-defining instruction will be
      overwritten by the record of the new instruction with the same
      result id.
  * new instruction with new result id or without result id:
      Just update the internal records to incorperate the new
      instruction.
* Add tests for analyzing individual instruction w/o an exisiting module.
* Refactor ClearInst() implementation
* Remove ClearDef() function.
* Fixed a bug in DefUseManager::ReplaceAllUsesWith() that OpName
  instruction may trigger the assertion incorrectly.
* update the blurbs for EraseUseRecordsOfOperandIds()
2016-08-17 23:13:01 -04:00
qining
6c5bdc9a39 Get the width from integer and float type 2016-08-17 20:33:56 -04:00
Lei Zhang
b65124f097 Make clear that we are using ptrdiff_t as the iterator difference type.
And ptrdiff_t is a implementation defined signed type. Comparing it
with unsigned number literal causes compiler warnings.
2016-08-16 11:21:08 -04:00
Lei Zhang
9747d33d08 Derive iterator from std::iterator to reap standarad algorithms.
By deriving from std::iterator, iterator_traits will be properly
set up for our custom iterator type, thus we can use algorithms
from STL with our custom iterators.
2016-08-15 16:29:56 -04:00
Lei Zhang
1eec0ed4b9 Make analyses RAII-like and turn disable copy/move constructors. 2016-08-15 13:41:47 -04:00
Lei Zhang
097ff25ea7 Allow OpUndef appearing in the type and global variable section.
Also clean up API functions for adding instructions into the
type and global values section.
2016-08-15 10:44:14 -04:00
David Neto
4987ae6549 Test INotEqual for OpSpecConstantOp in asm, disasm 2016-08-12 16:00:46 -04:00
Umar Arshad
b01755a5e2 OpUndef can appear in type declaration section 2016-08-12 14:28:17 -04:00