Commit Graph

63 Commits

Author SHA1 Message Date
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
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
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
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
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
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
qining
76982706f9 Get annotation instructions of a given id. 2016-08-30 15:08:24 -04:00
Lei Zhang
d9e63de117 Create a separate header and source file for each pass. 2016-08-29 15:46:09 -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
Lei Zhang
b1b2cac2cf Create const overloads for begin() and end() in Instruction. 2016-08-26 21:58:49 -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
qining
ec07a07724 Add a method in the types to check whether there is no decoration on a
type.
2016-08-26 12:09:21 -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
Lei Zhang
f51d82338e Add a home brewed make_unique to ease creating unique_ptrs. 2016-08-24 09:41:55 -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
qining
e8b7c7b210 Replace the SetOperand() with SetInOperand() and SetResultType() 2016-08-16 15:05:07 -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
qining
c814911904 Add another construtor for opt::ir::Instruction 2016-08-13 11:17:11 -04:00
qining
d065593404 Add iterator getter interface for types_values_ 2016-08-12 15:10:51 -04:00
Lei Zhang
94912ad1ba Turn off modifying methods for const iterators. 2016-08-12 14:05:41 -04:00
qining
81532584cb Clear old def-use record when re-analyze an ID 2016-08-12 13:04:07 -04:00
Lei Zhang
95dc16d1ec Add make_range() & make_const_range() for creating iterator ranges. 2016-08-12 10:34:47 -04:00
Lei Zhang
4b3247feba Avoid non-oneliner definition in class and add missing iterators. 2016-08-12 10:34:47 -04:00
qining
7a94e66433 Move the def-use analysis for single inst to public 2016-08-12 10:25:37 -04:00
qining
e5bb46d274 Add getter for pointee, element type 2016-08-11 20:53:57 -04:00
Lei Zhang
645ee1d9e0 Create an iterator class for in-memory representation. 2016-08-11 18:35:12 -04:00
qining
f949e321f2 Make the lookup up of type id accepts const arg 2016-08-11 18:21:57 -04:00
David Neto
82405cf499 Reuse the iterator on Id and type lookup 2016-08-11 18:18:12 -04:00
qining
f4e3a905d4 Add the const version of AsXXtype() interface. 2016-08-11 17:13:43 -04:00
qining
cd6d6c0ccf Add GetId(Type* type) API to the type manager 2016-08-11 15:10:09 -04:00
Lei Zhang
dda9de965d Make type manager RAII-like w.r.t. the module to be analyzed. 2016-08-10 12:53:17 -04:00
Lei Zhang
c562e231e3 Optimization: Add type manager.
Type manager will construct a map of types gradually from
instructions.
2016-08-10 12:53:17 -04:00
Lei Zhang
6d4d15b9d0 Optimization: Add type class hierarchy. 2016-08-10 12:53:17 -04:00
Lei Zhang
80c94a4fa8 Change the interfaces of in-memory representation to use pointers.
Previously we use vectors of objects and move semantics to handle
ownership. That approach has the flaw that inserting an object into
the middle of a vector, which may trigger a vector reallocation,
can invalidate some addresses taken from instructions.

Now the in-memory representation internally uses vector of unique
pointers to handle ownership. Since objects are explicitly heap-
allocated now, pointers to them won't be invalidated by vector
resizing anymore.
2016-08-10 12:11:33 -04:00