Lots of cleanup in SpvEmitter.

This commit is contained in:
Ben Vanik 2015-11-23 21:20:59 -08:00
parent e4d012911a
commit cf68d02142
4 changed files with 1428 additions and 1418 deletions

View File

@ -41,16 +41,16 @@ OpFunctionEnd
spv_disasm.Disassemble(asm_result->words(), asm_result->word_count());
SpvEmitter e;
auto glsl_std_450 = e.import("GLSL.std.450");
auto fn = e.makeMain();
auto float_1_0 = e.makeFloatConstant(1.0f);
auto acos = e.createBuiltinCall(
spv::Decoration::Invariant, e.makeFloatType(32), glsl_std_450,
auto glsl_std_450 = e.ImportExtendedInstructions("GLSL.std.450");
auto fn = e.MakeMainEntry();
auto float_1_0 = e.MakeFloatConstant(1.0f);
auto acos = e.CreateExtendedInstructionCall(
spv::Decoration::Invariant, e.MakeFloatType(32), glsl_std_450,
static_cast<int>(spv::GLSLstd450::Acos), {{float_1_0}});
e.makeReturn(false);
e.MakeReturn(true);
std::vector<uint32_t> words;
e.dump(words);
e.Serialize(words);
auto disasm_result2 = spv_disasm.Disassemble(words.data(), words.size());

File diff suppressed because it is too large Load Diff

View File

@ -64,87 +64,86 @@ class SpvEmitter {
SpvEmitter();
~SpvEmitter();
void setSource(spv::SourceLanguage language, int version) {
// Document what source language and text this module was translated from.
void SetSourceLanguage(spv::SourceLanguage language, int version) {
source_language_ = language;
source_version_ = version;
}
void addSourceExtension(const char* ext) { extensions_.push_back(ext); }
// Document an extension to the source language. Informational only.
void AddSourceExtension(const char* ext) {
source_extensions_.push_back(ext);
}
Id import(const char* name);
void setMemoryModel(spv::AddressingModel addressing_model,
// Set addressing model and memory model for the entire module.
void SetMemoryModel(spv::AddressingModel addressing_model,
spv::MemoryModel memory_model) {
addressing_model_ = addressing_model;
memory_model_ = memory_model;
}
void addCapability(spv::Capability cap) { capabilities_.push_back(cap); }
// Declare a capability used by this module.
void DeclareCapability(spv::Capability cap) { capabilities_.push_back(cap); }
// To get a new <id> for anything needing a new one.
Id getUniqueId() { return ++unique_id_; }
// To get a set of new <id>s, e.g., for a set of function parameters
Id getUniqueIds(int numIds) {
Id id = unique_id_ + 1;
unique_id_ += numIds;
return id;
}
// Import an extended set of instructions that can be later referenced by the
// returned id.
Id ImportExtendedInstructions(const char* name);
// For creating new types (will return old type if the requested one was
// already made).
Id makeVoidType();
Id makeBoolType();
Id makePointer(spv::StorageClass, Id type);
Id makeIntegerType(int width, bool hasSign); // generic
Id makeIntType(int width) { return makeIntegerType(width, true); }
Id makeUintType(int width) { return makeIntegerType(width, false); }
Id makeFloatType(int width);
Id makeStructType(std::vector<Id>& members, const char*);
Id makeStructResultType(Id type0, Id type1);
Id makeVectorType(Id component, int size);
Id makeMatrixType(Id component, int cols, int rows);
Id makeArrayType(Id element, unsigned size);
Id makeRuntimeArray(Id element);
Id makeFunctionType(Id return_type, std::vector<Id>& param_types);
Id makeImageType(Id sampledType, spv::Dim, bool depth, bool arrayed, bool ms,
unsigned sampled, spv::ImageFormat format);
Id makeSamplerType();
Id makeSampledImageType(Id imageType);
Id MakeVoidType();
Id MakeBoolType();
Id MakePointer(spv::StorageClass storage_class, Id pointee);
Id MakeIntegerType(int bit_width, bool is_signed);
Id MakeIntType(int bit_width) { return MakeIntegerType(bit_width, true); }
Id MakeUintType(int bit_width) { return MakeIntegerType(bit_width, false); }
Id MakeFloatType(int bit_width);
Id MakeStructType(std::initializer_list<Id> members, const char* name);
Id MakePairStructType(Id type0, Id type1);
Id MakeVectorType(Id component_type, int component_count);
Id MakeMatrix2DType(Id component_type, int cols, int rows);
Id MakeArrayType(Id element_type, int length);
Id MakeRuntimeArray(Id element_type);
Id MakeFunctionType(Id return_type, std::initializer_list<Id> param_types);
Id MakeImageType(Id sampled_type, spv::Dim dim, bool has_depth,
bool is_arrayed, bool is_multisampled, int sampled,
spv::ImageFormat format);
Id MakeSamplerType();
Id MakeSampledImageType(Id image_type);
// For querying about types.
Id getTypeId(Id result_id) const { return module_.getTypeId(result_id); }
Id getDerefTypeId(Id result_id) const;
Op getOpCode(Id id) const { return module_.getInstruction(id)->opcode(); }
Op getTypeClass(Id type_id) const { return getOpCode(type_id); }
Op getMostBasicTypeClass(Id type_id) const;
int getNumComponents(Id result_id) const {
return getNumTypeComponents(getTypeId(result_id));
Id GetTypeId(Id result_id) const { return module_.type_id(result_id); }
Id GetDerefTypeId(Id result_id) const;
Op GetOpcode(Id id) const { return module_.instruction(id)->opcode(); }
Op GetTypeClass(Id type_id) const { return GetOpcode(type_id); }
Op GetMostBasicTypeClass(Id type_id) const;
int GetComponentCount(Id result_id) const {
return GetTypeComponentCount(GetTypeId(result_id));
}
int getNumTypeComponents(Id type_id) const;
Id getScalarTypeId(Id type_id) const;
Id getContainedTypeId(Id type_id) const;
Id getContainedTypeId(Id type_id, int) const;
spv::StorageClass getTypeStorageClass(Id type_id) const {
return module_.getStorageClass(type_id);
int GetTypeComponentCount(Id type_id) const;
Id GetScalarTypeId(Id type_id) const;
Id GetContainedTypeId(Id type_id) const;
Id GetContainedTypeId(Id type_id, int member) const;
spv::StorageClass GetTypeStorageClass(Id type_id) const {
return module_.storage_class(type_id);
}
bool isPointer(Id result_id) const {
return isPointerType(getTypeId(result_id));
bool IsPointer(Id result_id) const {
return IsPointerType(GetTypeId(result_id));
}
bool isScalar(Id result_id) const {
return isScalarType(getTypeId(result_id));
bool IsScalar(Id result_id) const {
return IsScalarType(GetTypeId(result_id));
}
bool isVector(Id result_id) const {
return isVectorType(getTypeId(result_id));
bool IsVector(Id result_id) const {
return IsVectorType(GetTypeId(result_id));
}
bool isMatrix(Id result_id) const {
return isMatrixType(getTypeId(result_id));
bool IsMatrix(Id result_id) const {
return IsMatrixType(GetTypeId(result_id));
}
bool isAggregate(Id result_id) const {
return isAggregateType(getTypeId(result_id));
bool IsAggregate(Id result_id) const {
return IsAggregateType(GetTypeId(result_id));
}
bool isBoolType(Id type_id) const {
bool IsBoolType(Id type_id) const {
return grouped_types_[static_cast<int>(spv::Op::OpTypeBool)].size() > 0 &&
type_id ==
grouped_types_[static_cast<int>(spv::Op::OpTypeBool)]
@ -152,222 +151,228 @@ class SpvEmitter {
->result_id();
}
bool isPointerType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypePointer;
bool IsPointerType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypePointer;
}
bool isScalarType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeFloat ||
getTypeClass(type_id) == spv::Op::OpTypeInt ||
getTypeClass(type_id) == spv::Op::OpTypeBool;
bool IsScalarType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeFloat ||
GetTypeClass(type_id) == spv::Op::OpTypeInt ||
GetTypeClass(type_id) == spv::Op::OpTypeBool;
}
bool isVectorType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeVector;
bool IsVectorType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeVector;
}
bool isMatrixType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeMatrix;
bool IsMatrixType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeMatrix;
}
bool isStructType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeStruct;
bool IsStructType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeStruct;
}
bool isArrayType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeArray;
bool IsArrayType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeArray;
}
bool isAggregateType(Id type_id) const {
return isArrayType(type_id) || isStructType(type_id);
bool IsAggregateType(Id type_id) const {
return IsArrayType(type_id) || IsStructType(type_id);
}
bool isImageType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeImage;
bool IsImageType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeImage;
}
bool isSamplerType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeSampler;
bool IsSamplerType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeSampler;
}
bool isSampledImageType(Id type_id) const {
return getTypeClass(type_id) == spv::Op::OpTypeSampledImage;
bool IsSampledImageType(Id type_id) const {
return GetTypeClass(type_id) == spv::Op::OpTypeSampledImage;
}
bool isConstantOpCode(Op opcode) const;
bool isConstant(Id result_id) const {
return isConstantOpCode(getOpCode(result_id));
bool IsConstantOpCode(Op opcode) const;
bool IsConstant(Id result_id) const {
return IsConstantOpCode(GetOpcode(result_id));
}
bool isConstantScalar(Id result_id) const {
return getOpCode(result_id) == spv::Op::OpConstant;
bool IsConstantScalar(Id result_id) const {
return GetOpcode(result_id) == spv::Op::OpConstant;
}
unsigned int getConstantScalar(Id result_id) const {
return module_.getInstruction(result_id)->immediate_operand(0);
uint32_t GetConstantScalar(Id result_id) const {
return module_.instruction(result_id)->immediate_operand(0);
}
spv::StorageClass getStorageClass(Id result_id) const {
return getTypeStorageClass(getTypeId(result_id));
spv::StorageClass GetStorageClass(Id result_id) const {
return GetTypeStorageClass(GetTypeId(result_id));
}
int getTypeNumColumns(Id type_id) const {
assert(isMatrixType(type_id));
return getNumTypeComponents(type_id);
int GetTypeColumnCount(Id type_id) const {
assert(IsMatrixType(type_id));
return GetTypeComponentCount(type_id);
}
int getNumColumns(Id result_id) const {
return getTypeNumColumns(getTypeId(result_id));
int GetColumnCount(Id result_id) const {
return GetTypeColumnCount(GetTypeId(result_id));
}
int getTypeNumRows(Id type_id) const {
assert(isMatrixType(type_id));
return getNumTypeComponents(getContainedTypeId(type_id));
int GetTypeRowCount(Id type_id) const {
assert(IsMatrixType(type_id));
return GetTypeComponentCount(GetContainedTypeId(type_id));
}
int getNumRows(Id result_id) const {
return getTypeNumRows(getTypeId(result_id));
int GetRowCount(Id result_id) const {
return GetTypeRowCount(GetTypeId(result_id));
}
spv::Dim getTypeDimensionality(Id type_id) const {
assert(isImageType(type_id));
spv::Dim GetTypeDimensionality(Id type_id) const {
assert(IsImageType(type_id));
return static_cast<spv::Dim>(
module_.getInstruction(type_id)->immediate_operand(1));
module_.instruction(type_id)->immediate_operand(1));
}
Id getImageType(Id result_id) const {
Id type_id = getTypeId(result_id);
assert(isImageType(type_id) || isSampledImageType(type_id));
return isSampledImageType(type_id)
? module_.getInstruction(type_id)->id_operand(0)
Id GetImageType(Id result_id) const {
Id type_id = GetTypeId(result_id);
assert(IsImageType(type_id) || IsSampledImageType(type_id));
return IsSampledImageType(type_id)
? module_.instruction(type_id)->id_operand(0)
: type_id;
}
bool isArrayedImageType(Id type_id) const {
assert(isImageType(type_id));
return module_.getInstruction(type_id)->immediate_operand(3) != 0;
bool IsArrayedImageType(Id type_id) const {
assert(IsImageType(type_id));
return module_.instruction(type_id)->immediate_operand(3) != 0;
}
// For making new constants (will return old constant if the requested one was
// already made).
Id makeBoolConstant(bool b, bool is_spec_constant = false);
Id makeIntConstant(int i, bool is_spec_constant = false) {
return makeIntConstant(makeIntType(32), (unsigned)i, is_spec_constant);
Id MakeBoolConstant(bool value, bool is_spec_constant = false);
Id MakeIntConstant(int value, bool is_spec_constant = false) {
return MakeIntegerConstant(MakeIntType(32), static_cast<uint32_t>(value),
is_spec_constant);
}
Id makeUintConstant(uint32_t u, bool is_spec_constant = false) {
return makeIntConstant(makeUintType(32), u, is_spec_constant);
Id MakeUintConstant(uint32_t value, bool is_spec_constant = false) {
return MakeIntegerConstant(MakeUintType(32), value, is_spec_constant);
}
template <typename T>
Id makeUintConstant(T u, bool is_spec_constant = false) {
Id MakeUintConstant(T value, bool is_spec_constant = false) {
static_assert(sizeof(T) == sizeof(uint32_t), "Invalid type");
return makeIntConstant(makeUintType(32), static_cast<uint32_t>(u),
is_spec_constant);
return MakeIntegerConstant(MakeUintType(32), static_cast<uint32_t>(value),
is_spec_constant);
}
Id makeFloatConstant(float f, bool is_spec_constant = false);
Id makeDoubleConstant(double d, bool is_spec_constant = false);
Id MakeFloatConstant(float value, bool is_spec_constant = false);
Id MakeDoubleConstant(double value, bool is_spec_constant = false);
// Turn the array of constants into a proper spv constant of the requested
// type.
Id makeCompositeConstant(Id type, std::vector<Id>& comps);
// Turns the array of constants into a proper constant of the requested type.
Id MakeCompositeConstant(Id type, std::initializer_list<Id> components);
// Methods for adding information outside the CFG.
Instruction* addEntryPoint(spv::ExecutionModel, Function*, const char* name);
void addExecutionMode(Function*, spv::ExecutionMode mode, int value1 = -1,
// Declares an entry point and its execution model.
Instruction* AddEntryPoint(spv::ExecutionModel execution_model,
Function* entry_point, const char* name);
void AddExecutionMode(Function* entry_point,
spv::ExecutionMode execution_mode, int value1 = -1,
int value2 = -1, int value3 = -1);
void addName(Id, const char* name);
void addMemberName(Id, int member, const char* name);
void addLine(Id target, Id file_name, int line, int column);
void addDecoration(Id, spv::Decoration, int num = -1);
void addMemberDecoration(Id, unsigned int member, spv::Decoration,
void AddName(Id target_id, const char* name);
void AddMemberName(Id target_id, int member, const char* name);
void AddLine(Id target_id, Id file_name, int line_number, int column_number);
void AddDecoration(Id target_id, spv::Decoration decoration, int num = -1);
void AddMemberDecoration(Id target_id, int member, spv::Decoration,
int num = -1);
// At the end of what block do the next create*() instructions go?
Block* build_point() const { return build_point_; }
void set_build_point(Block* build_point) { build_point_ = build_point; }
// Make the main function.
Function* makeMain();
// Makes the main function.
Function* MakeMainEntry();
// Make a shader-style function, and create its entry block if entry is
// Makes a shader-style function, and create its entry block if entry is
// non-zero.
// Return the function, pass back the entry.
Function* makeFunctionEntry(Id return_type, const char* name,
std::vector<Id>& param_types, Block** entry = 0);
Function* MakeFunctionEntry(Id return_type, const char* name,
std::initializer_list<Id> param_types,
Block** entry = 0);
// Create a return. An 'implicit' return is one not appearing in the source
// code. In the case of an implicit return, no post-return block is inserted.
void makeReturn(bool implicit, Id retVal = 0);
// Creates a return statement.
// An 'implicit' return is one not appearing in the source code. In the case
// of an implicit return, no post-return block is inserted.
void MakeReturn(bool implicit, Id return_value = 0);
// Generate all the code needed to finish up a function.
void leaveFunction();
// Generates all the code needed to finish up a function.
void LeaveFunction();
// Create a discard.
void makeDiscard();
// Creates a fragment-shader discard (kill).
void MakeDiscard();
// Create a global or function local or IO variable.
Id createVariable(spv::StorageClass storage_class, Id type,
// Creates a global or function local or IO variable.
Id CreateVariable(spv::StorageClass storage_class, Id type,
const char* name = 0);
// Create an imtermediate with an undefined value.
Id createUndefined(Id type);
// Creates an intermediate object whose value is undefined.
Id CreateUndefined(Id type);
// Store into an Id and return the l-value
void createStore(Id rvalue, Id lvalue);
// Stores the given value into the specified pointer.
void CreateStore(Id pointer_id, Id value_id);
// Load from an Id and return it
Id createLoad(Id lvalue);
// Loads the value from the given pointer.
Id CreateLoad(Id pointer_id);
// Create an OpAccessChain instruction
Id createAccessChain(spv::StorageClass storage_class, Id base,
std::vector<Id>& offsets);
// Creates a pointer into a composite object that can be used with OpLoad and
// OpStore.
Id CreateAccessChain(spv::StorageClass storage_class, Id base_id,
std::vector<Id> index_ids);
// Create an OpArrayLength instruction
Id createArrayLength(Id base, unsigned int member);
// Queries the length of a run-time array.
Id CreateArrayLength(Id struct_id, int array_member);
// Create an OpCompositeExtract instruction
Id createCompositeExtract(Id composite, Id type_id, unsigned index);
Id createCompositeExtract(Id composite, Id type_id,
std::vector<unsigned>& indexes);
Id createCompositeInsert(Id object, Id composite, Id type_id, unsigned index);
Id createCompositeInsert(Id object, Id composite, Id type_id,
std::vector<unsigned>& indexes);
Id CreateCompositeExtract(Id composite, Id type_id, uint32_t index);
Id CreateCompositeExtract(Id composite, Id type_id,
std::vector<uint32_t> indexes);
Id CreateCompositeInsert(Id object, Id composite, Id type_id, uint32_t index);
Id CreateCompositeInsert(Id object, Id composite, Id type_id,
std::vector<uint32_t> indexes);
Id createVectorExtractDynamic(Id vector, Id type_id, Id component_index);
Id createVectorInsertDynamic(Id vector, Id type_id, Id component,
Id CreateVectorExtractDynamic(Id vector, Id type_id, Id component_index);
Id CreateVectorInsertDynamic(Id vector, Id type_id, Id component,
Id component_index);
void createNoResultOp(Op);
void createNoResultOp(Op, Id operand);
void createNoResultOp(Op, const std::vector<Id>& operands);
void createControlBarrier(spv::Scope execution, spv::Scope memory,
spv::MemorySemanticsMask);
void createMemoryBarrier(unsigned execution_scope, unsigned memory_semantics);
Id createUnaryOp(Op, Id type_id, Id operand);
Id createBinOp(Op, Id type_id, Id operand1, Id operand2);
Id createTriOp(Op, Id type_id, Id operand1, Id operand2, Id operand3);
Id createOp(Op, Id type_id, const std::vector<Id>& operands);
Id createFunctionCall(Function*, std::vector<spv::Id>&);
// Does nothing.
void CreateNop();
// Take an rvalue (source) and a set of channels to extract from it to
// make a new rvalue, which is returned.
Id createRvalueSwizzle(Id type_id, Id source,
std::vector<unsigned>& channels);
// Waits for other invocations of this module to reach the current point of
// execution.
void CreateControlBarrier(spv::Scope execution_scope, spv::Scope memory_scope,
spv::MemorySemanticsMask memory_semantics);
// Controls the order that memory accesses are observed.
void CreateMemoryBarrier(spv::Scope execution_scope,
spv::MemorySemanticsMask memory_semantics);
// Take a copy of an lvalue (target) and a source of components, and set the
Id CreateUnaryOp(Op opcode, Id type_id, Id operand);
Id CreateBinOp(Op opcode, Id type_id, Id operand1, Id operand2);
Id CreateTriOp(Op opcode, Id type_id, Id operand1, Id operand2, Id operand3);
Id CreateOp(Op opcode, Id type_id, const std::vector<Id>& operands);
Id CreateFunctionCall(Function* function, std::vector<spv::Id> args);
// Takes an rvalue (source) and a set of channels to extract from it to
// make a new rvalue.
Id CreateSwizzle(Id type_id, Id source, std::vector<uint32_t> channels);
// Takes a copy of an lvalue (target) and a source of components, and sets the
// source components into the lvalue where the 'channels' say to put them.
// An updated version of the target is returned.
// (No true lvalue or stores are used.)
Id createLvalueSwizzle(Id type_id, Id target, Id source,
std::vector<unsigned>& channels);
Id CreateLvalueSwizzle(Id type_id, Id target, Id source,
std::vector<uint32_t> channels);
// If the value passed in is an instruction and the precision is not EMpNone,
// it gets tagged with the requested precision.
void setPrecision(Id value, spv::Decoration precision) {
void SetPrecision(Id value, spv::Decoration precision) {
CheckNotImplemented("setPrecision");
}
// Can smear a scalar to a vector for the following forms:
// - promoteScalar(scalar, vector) // smear scalar to width of vector
// - promoteScalar(vector, scalar) // smear scalar to width of vector
// - promoteScalar(pointer, scalar) // smear scalar to width of what pointer
// points to
// - promoteScalar(scalar, scalar) // do nothing
// Smears a scalar to a vector for the following forms:
// - PromoteScalar(scalar, vector) // smear scalar to width of vector
// - PromoteScalar(vector, scalar) // smear scalar to width of vector
// - PromoteScalar(pointer, scalar) // smear scalar to width of what pointer
// points to
// - PromoteScalar(scalar, scalar) // do nothing
// Other forms are not allowed.
//
// Note: One of the arguments will change, with the result coming back that
// way rather than
// through the return value.
void promoteScalar(spv::Decoration precision, Id& left, Id& right);
// way rather than through the return value.
void PromoteScalar(spv::Decoration precision, Id& left, Id& right);
// make a value by smearing the scalar to fill the type
Id smearScalar(spv::Decoration precision, Id scalarVal, Id);
// Makes a value by smearing the scalar to fill the type.
Id SmearScalar(spv::Decoration precision, Id scalar_value, Id vector_type_id);
// Create a call to a built-in function.
Id createBuiltinCall(spv::Decoration precision, Id result_type, Id builtins,
int entry_point, std::initializer_list<Id> args);
// Executes an instruction in an imported set of extended instructions.
Id CreateExtendedInstructionCall(spv::Decoration precision, Id result_type,
Id instruction_set, int instruction_ordinal,
std::initializer_list<Id> args);
// List of parameters used to create a texture operation
struct TextureParameters {
@ -375,45 +380,45 @@ class SpvEmitter {
Id coords;
Id bias;
Id lod;
Id Dref;
Id depth_ref;
Id offset;
Id offsets;
Id gradX;
Id gradY;
Id grad_x;
Id grad_y;
Id sample;
Id comp;
};
// Select the correct texture operation based on all inputs, and emit the
// correct instruction
Id createTextureCall(spv::Decoration precision, Id result_type, bool fetch,
bool proj, bool gather, const TextureParameters&);
// Selects the correct texture operation based on all inputs, and emit the
// correct instruction.
Id CreateTextureCall(spv::Decoration precision, Id result_type, bool fetch,
bool proj, bool gather,
const TextureParameters& parameters);
// Emit the OpTextureQuery* instruction that was passed in.
// Figure out the right return value and type, and return it.
Id createTextureQueryCall(Op, const TextureParameters&);
// Emits the OpTextureQuery* instruction that was passed in and figures out
// the right return value and type.
Id CreateTextureQueryCall(Op opcode, const TextureParameters& parameters);
Id createSamplePositionCall(spv::Decoration precision, Id, Id);
Id createBitFieldExtractCall(spv::Decoration precision, Id, Id, Id,
Id CreateSamplePositionCall(spv::Decoration precision, Id, Id);
Id CreateBitFieldExtractCall(spv::Decoration precision, Id, Id, Id,
bool isSigned);
Id createBitFieldInsertCall(spv::Decoration precision, Id, Id, Id, Id);
Id CreateBitFieldInsertCall(spv::Decoration precision, Id, Id, Id, Id);
// Reduction comparision for composites: For equal and not-equal resulting in
// a scalar.
Id createCompare(spv::Decoration precision, Id, Id,
bool /* true if for equal, fales if for not-equal */);
Id CreateCompare(spv::Decoration precision, Id value1, Id value2,
bool is_equal);
// OpCompositeConstruct
Id createCompositeConstruct(Id type_id, std::vector<Id>& constituents);
Id CreateCompositeConstruct(Id type_id, std::vector<Id> constituent_ids);
// vector or scalar constructor
Id createConstructor(spv::Decoration precision,
const std::vector<Id>& sources, Id result_type_id);
Id CreateConstructor(spv::Decoration precision, std::vector<Id> source_ids,
Id result_type_id);
// matrix constructor
Id createMatrixConstructor(spv::Decoration precision,
const std::vector<Id>& sources, Id constructee);
Id CreateMatrixConstructor(spv::Decoration precision, std::vector<Id> sources,
Id constructee);
// Helper to use for building nested control flow with if-then-else.
class If {
@ -421,8 +426,8 @@ class SpvEmitter {
If(SpvEmitter& emitter, Id condition);
~If() = default;
void makeBeginElse();
void makeEndIf();
void MakeBeginElse();
void MakeEndIf();
private:
If(const If&) = delete;
@ -437,7 +442,7 @@ class SpvEmitter {
Block* merge_block_ = nullptr;
};
// Make a switch statement.
// Makes a switch statement.
// A switch has 'numSegments' of pieces of code, not containing any
// case/default labels, all separated by one or more case/default labels.
// Each possible case value v is a jump to the caseValues[v] segment. The
@ -452,46 +457,46 @@ class SpvEmitter {
//
// Returns the right set of basic blocks to start each code segment with, so
// that the caller's recursion stack can hold the memory for it.
void makeSwitch(Id condition, int numSegments, std::vector<int>& caseValues,
std::vector<int>& valueToSegment, int defaultSegment,
std::vector<Block*>& segmentBB); // return argument
void MakeSwitch(Id condition, int segment_count, std::vector<int> case_values,
std::vector<int> value_index_to_segment, int default_segment,
std::vector<Block*>& segment_blocks);
// Add a branch to the innermost switch's merge block.
void addSwitchBreak();
// Adds a branch to the innermost switch's merge block.
void AddSwitchBreak();
// Move to the next code segment, passing in the return argument in
// makeSwitch()
void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
// Move sto the next code segment, passing in the return argument in
// MakeSwitch().
void NextSwitchSegment(std::vector<Block*>& segment_block, int next_segment);
// Finish off the innermost switch.
void endSwitch(std::vector<Block*>& segmentBB);
// Finishes off the innermost switch.
void EndSwitch(std::vector<Block*>& segment_block);
// Start the beginning of a new loop, and prepare the builder to
// Starts the beginning of a new loop, and prepare the builder to
// generate code for the loop test.
// The loopTestFirst parameter is true when the loop test executes before
// the body. (It is false for do-while loops.)
void makeNewLoop(bool loopTestFirst);
// The test_first parameter is true when the loop test executes before
// the body (it is false for do-while loops).
void MakeNewLoop(bool test_first);
// Add the branch for the loop test, based on the given condition.
// Adds the branch for the loop test, based on the given condition.
// The true branch goes to the first block in the loop body, and
// the false branch goes to the loop's merge block. The builder insertion
// point will be placed at the start of the body.
void createLoopTestBranch(Id condition);
void CreateLoopTestBranch(Id condition);
// Generate an unconditional branch to the loop body. The builder insertion
// point will be placed at the start of the body. Use this when there is
// no loop test.
void createBranchToBody();
// Generates an unconditional branch to the loop body.
// The builder insertion point will be placed at the start of the body.
// Use this when there is no loop test.
void CreateBranchToBody();
// Add a branch to the test of the current (innermost) loop.
// Adds a branch to the test of the current (innermost) loop.
// The way we generate code, that's also the loop header.
void createLoopContinue();
void CreateLoopContinue();
// Add an exit (e.g. "break") for the innermost loop that you're in
void createLoopExit();
// Adds an exit (e.g. "break") for the innermost loop that you're in.
void CreateLoopExit();
// Close the innermost loop that you're in
void closeLoop();
// Close the innermost loop that you're in.
void CloseLoop();
// Access chain design for an R-Value vs. L-Value:
//
@ -528,7 +533,7 @@ class SpvEmitter {
// base object
std::vector<Id> index_chain;
Id instr; // cache the instruction that generates this access chain
std::vector<unsigned> swizzle; // each std::vector element selects the next
std::vector<uint32_t> swizzle; // each std::vector element selects the next
// GLSL component number
Id component; // a dynamic component index, can coexist with a swizzle,
// done after the swizzle, NoResult if not present
@ -549,83 +554,98 @@ class SpvEmitter {
AccessChain access_chain() { return access_chain_; }
void set_access_chain(AccessChain new_chain) { access_chain_ = new_chain; }
// clear accessChain
void clearAccessChain();
void ClearAccessChain();
// set new base as an l-value base
void setAccessChainLValue(Id lvalue) {
assert(isPointer(lvalue));
void set_access_chain_lvalue(Id lvalue) {
assert(IsPointer(lvalue));
access_chain_.base = lvalue;
}
// set new base value as an r-value
void setAccessChainRValue(Id rvalue) {
void set_access_chain_rvalue(Id rvalue) {
access_chain_.is_rvalue = true;
access_chain_.base = rvalue;
}
// push offset onto the end of the chain
void accessChainPush(Id offset) {
void PushAccessChainOffset(Id offset) {
access_chain_.index_chain.push_back(offset);
}
// push new swizzle onto the end of any existing swizzle, merging into a
// single swizzle
void accessChainPushSwizzle(std::vector<unsigned>& swizzle,
void PushAccessChainSwizzle(std::vector<uint32_t> swizzle,
Id pre_swizzle_base_type);
// push a variable component selection onto the access chain; supporting only
// one, so unsided
void accessChainPushComponent(Id component, Id pre_swizzle_base_type) {
void PushAccessChainComponent(Id component, Id pre_swizzle_base_type) {
access_chain_.component = component;
if (access_chain_.pre_swizzle_base_type == NoType)
if (access_chain_.pre_swizzle_base_type == NoType) {
access_chain_.pre_swizzle_base_type = pre_swizzle_base_type;
}
}
// use accessChain and swizzle to store value
void accessChainStore(Id rvalue);
void CreateAccessChainStore(Id rvalue);
// use accessChain and swizzle to load an r-value
Id accessChainLoad(Id result_type);
Id CreateAccessChainLoad(Id result_type_id);
// get the direct pointer for an l-value
Id accessChainGetLValue();
Id CreateAccessChainLValue();
void dump(std::vector<unsigned int>&) const;
void Serialize(std::vector<uint32_t>& out) const;
private:
// Maximum dimension for column/row in a matrix.
static const int kMaxMatrixSize = 4;
// Asserts on unimplemnted functionality.
void CheckNotImplemented(const char* message);
// Allocates a new <id>.
Id AllocateUniqueId() { return ++unique_id_; }
Id makeIntConstant(Id type_id, unsigned value, bool is_spec_constant);
Id findScalarConstant(Op type_class, Op opcode, Id type_id,
unsigned value) const;
Id findScalarConstant(Op type_class, Op opcode, Id type_id, unsigned v1,
unsigned v2) const;
Id findCompositeConstant(Op type_class, std::vector<Id>& comps) const;
Id collapseAccessChain();
void transferAccessChainSwizzle(bool dynamic);
void simplifyAccessChainSwizzle();
void createAndSetNoPredecessorBlock(const char*);
void createBranch(Block* block);
void createSelectionMerge(Block* merge_block,
// Allocates a contiguous sequence of <id>s.
Id AllocateUniqueIds(int count) {
Id id = unique_id_ + 1;
unique_id_ += count;
return id;
}
Id MakeIntegerConstant(Id type_id, uint32_t value, bool is_spec_constant);
Id FindScalarConstant(Op type_class, Op opcode, Id type_id,
uint32_t value) const;
Id FindScalarConstant(Op type_class, Op opcode, Id type_id, uint32_t v1,
uint32_t v2) const;
Id FindCompositeConstant(Op type_class,
std::initializer_list<Id> components) const;
Id CollapseAccessChain();
void SimplifyAccessChainSwizzle();
void TransferAccessChainSwizzle(bool dynamic);
void SerializeInstructions(
std::vector<uint32_t>& out,
const std::vector<Instruction*>& instructions) const;
void CreateAndSetNoPredecessorBlock(const char* name);
void CreateBranch(Block* block);
void CreateSelectionMerge(Block* merge_block,
spv::SelectionControlMask control);
void createLoopMerge(Block* merge_block, Block* continueBlock,
void CreateLoopMerge(Block* merge_block, Block* continueBlock,
spv::LoopControlMask control);
void createConditionalBranch(Id condition, Block* then_block,
void CreateConditionalBranch(Id condition, Block* then_block,
Block* else_block);
void dumpInstructions(std::vector<unsigned int>&,
const std::vector<Instruction*>&) const;
struct Loop; // Defined below.
void createBranchToLoopHeaderFromInside(const Loop& loop);
void CreateBranchToLoopHeaderFromInside(const Loop& loop);
// Asserts on unimplemented functionality.
void CheckNotImplemented(const char* message);
spv::SourceLanguage source_language_ = spv::SourceLanguage::Unknown;
int source_version_ = 0;
std::vector<const char*> extensions_;
std::vector<const char*> source_extensions_;
spv::AddressingModel addressing_model_ = spv::AddressingModel::Logical;
spv::MemoryModel memory_model_ = spv::MemoryModel::GLSL450;
std::vector<spv::Capability> capabilities_;
@ -647,12 +667,13 @@ class SpvEmitter {
std::vector<Instruction*> externals_;
// not output, internally used for quick & dirty canonical (unique) creation
std::vector<Instruction*> grouped_constants_[static_cast<int>(
spv::Op::OpConstant)]; // all types appear before OpConstant
// All types appear before OpConstant.
std::vector<Instruction*>
grouped_constants_[static_cast<int>(spv::Op::OpConstant)];
std::vector<Instruction*>
grouped_types_[static_cast<int>(spv::Op::OpConstant)];
// stack of switches
// Stack of switches.
std::stack<Block*> switch_merges_;
// Data that needs to be kept in order to properly handle loops.

View File

@ -86,55 +86,66 @@ class Instruction {
explicit Instruction(Op opcode) : opcode_(opcode) {}
~Instruction() = default;
void addIdOperand(Id id) { operands_.push_back(id); }
void AddIdOperand(Id id) { operands_.push_back(id); }
void addIdOperands(const std::vector<Id>& ids) {
void AddIdOperands(const std::vector<Id>& ids) {
for (auto id : ids) {
operands_.push_back(id);
}
}
void AddIdOperands(std::initializer_list<Id> ids) {
for (auto id : ids) {
operands_.push_back(id);
}
}
void addImmediateOperand(uint32_t immediate) {
void AddImmediateOperand(uint32_t immediate) {
operands_.push_back(immediate);
}
template <typename T>
void addImmediateOperand(T immediate) {
void AddImmediateOperand(T immediate) {
static_assert(sizeof(T) == sizeof(uint32_t), "Invalid operand size");
operands_.push_back(static_cast<uint32_t>(immediate));
}
void addImmediateOperands(const std::vector<uint32_t>& immediates) {
void AddImmediateOperands(const std::vector<uint32_t>& immediates) {
for (auto immediate : immediates) {
operands_.push_back(immediate);
}
}
void addStringOperand(const char* str) {
void AddImmediateOperands(std::initializer_list<uint32_t> immediates) {
for (auto immediate : immediates) {
operands_.push_back(immediate);
}
}
void AddStringOperand(const char* str) {
original_string_ = str;
uint32_t word;
char* wordString = (char*)&word;
char* wordPtr = wordString;
int charCount = 0;
char* word_string = reinterpret_cast<char*>(&word);
char* word_ptr = word_string;
int char_count = 0;
char c;
do {
c = *(str++);
*(wordPtr++) = c;
++charCount;
if (charCount == 4) {
addImmediateOperand(word);
wordPtr = wordString;
charCount = 0;
*(word_ptr++) = c;
++char_count;
if (char_count == 4) {
AddImmediateOperand(word);
word_ptr = word_string;
char_count = 0;
}
} while (c != 0);
// deal with partial last word
if (charCount > 0) {
if (char_count > 0) {
// pad with 0s
for (; charCount < 4; ++charCount) {
*(wordPtr++) = 0;
for (; char_count < 4; ++char_count) {
*(word_ptr++) = 0;
}
addImmediateOperand(word);
AddImmediateOperand(word);
}
}
@ -147,17 +158,17 @@ class Instruction {
const char* string_operand() const { return original_string_.c_str(); }
// Write out the binary form.
void dump(std::vector<uint32_t>& out) const {
uint32_t wordCount = 1;
void Serialize(std::vector<uint32_t>& out) const {
uint32_t word_count = 1;
if (type_id_) {
++wordCount;
++word_count;
}
if (result_id_) {
++wordCount;
++word_count;
}
wordCount += static_cast<uint32_t>(operands_.size());
word_count += static_cast<uint32_t>(operands_.size());
out.push_back((wordCount << spv::WordCountShift) |
out.push_back((word_count << spv::WordCountShift) |
static_cast<uint32_t>(opcode_));
if (type_id_) {
out.push_back(type_id_);
@ -185,19 +196,24 @@ class Block {
public:
Block(Id id, Function& parent);
~Block() {
// TODO: free instructions
for (size_t i = 0; i < instructions_.size(); ++i) {
delete instructions_[i];
}
for (size_t i = 0; i < local_variables_.size(); ++i) {
delete local_variables_[i];
}
}
Id id() { return instructions_.front()->result_id(); }
Function& parent() const { return parent_; }
void push_instruction(Instruction* inst);
void push_local_variable(Instruction* inst) {
local_variables_.push_back(inst);
void AddInstruction(Instruction* instr);
void AddLocalVariable(Instruction* instr) {
local_variables_.push_back(instr);
}
void push_predecessor(Block* predecessor) {
void AddPredecessor(Block* predecessor) {
predecessors_.push_back(predecessor);
}
@ -222,7 +238,7 @@ class Block {
}
}
void dump(std::vector<uint32_t>& out) const {
void Serialize(std::vector<uint32_t>& out) const {
// skip the degenerate unreachable blocks
// TODO: code gen: skip all unreachable blocks (transitive closure)
// (but, until that's done safer to keep non-degenerate
@ -231,12 +247,12 @@ class Block {
return;
}
instructions_[0]->dump(out);
instructions_[0]->Serialize(out);
for (auto variable : local_variables_) {
variable->dump(out);
variable->Serialize(out);
}
for (int i = 1; i < instructions_.size(); ++i) {
instructions_[i]->dump(out);
instructions_[i]->Serialize(out);
}
}
@ -275,32 +291,32 @@ class Function {
Id param_id(int p) { return parameter_instructions_[p]->result_id(); }
void push_block(Block* block) { blocks_.push_back(block); }
void pop_block(Block*) { blocks_.pop_back(); }
void pop_block(Block* block) { blocks_.pop_back(); }
Module& parent() const { return parent_; }
Block* entry_block() const { return blocks_.front(); }
Block* last_block() const { return blocks_.back(); }
void push_local_variable(Instruction* inst);
void AddLocalVariable(Instruction* instr);
Id return_type() const { return function_instruction_.type_id(); }
void dump(std::vector<uint32_t>& out) const {
void Serialize(std::vector<uint32_t>& out) const {
// OpFunction
function_instruction_.dump(out);
function_instruction_.Serialize(out);
// OpFunctionParameter
for (auto instruction : parameter_instructions_) {
instruction->dump(out);
instruction->Serialize(out);
}
// Blocks
for (auto block : blocks_) {
block->dump(out);
block->Serialize(out);
}
Instruction end(0, 0, spv::Op::OpFunctionEnd);
end.dump(out);
end.Serialize(out);
}
private:
@ -317,32 +333,35 @@ class Module {
public:
Module() = default;
~Module() {
// TODO delete things
for (size_t i = 0; i < functions_.size(); ++i) {
delete functions_[i];
}
}
void push_function(Function* function) { functions_.push_back(function); }
void AddFunction(Function* function) { functions_.push_back(function); }
void mapInstruction(Instruction* instruction) {
spv::Id result_id = instruction->result_id();
// map the instruction's result id
if (result_id >= id_to_instruction_.size())
void MapInstruction(Instruction* instr) {
spv::Id result_id = instr->result_id();
// Map the instruction's result id.
if (result_id >= id_to_instruction_.size()) {
id_to_instruction_.resize(result_id + 16);
id_to_instruction_[result_id] = instruction;
}
id_to_instruction_[result_id] = instr;
}
Instruction* getInstruction(Id id) const { return id_to_instruction_[id]; }
Instruction* instruction(Id id) const { return id_to_instruction_[id]; }
spv::Id getTypeId(Id result_id) const {
spv::Id type_id(Id result_id) const {
return id_to_instruction_[result_id]->type_id();
}
spv::StorageClass getStorageClass(Id type_id) const {
spv::StorageClass storage_class(Id type_id) const {
return (spv::StorageClass)id_to_instruction_[type_id]->immediate_operand(0);
}
void dump(std::vector<uint32_t>& out) const {
void Serialize(std::vector<uint32_t>& out) const {
for (auto function : functions_) {
function->dump(out);
function->Serialize(out);
}
}
@ -351,37 +370,36 @@ class Module {
std::vector<Function*> functions_;
// map from result id to instruction having that result id
// Maps from result id to instruction having that result id.
std::vector<Instruction*> id_to_instruction_;
// map from a result id to its type id
};
inline Function::Function(Id id, Id resultType, Id functionType,
Id firstParamId, Module& parent)
inline Function::Function(Id id, Id result_type_id, Id function_type_id,
Id first_param_id, Module& parent)
: parent_(parent),
function_instruction_(id, resultType, spv::Op::OpFunction) {
function_instruction_(id, result_type_id, spv::Op::OpFunction) {
// OpFunction
function_instruction_.addImmediateOperand(
function_instruction_.AddImmediateOperand(
static_cast<uint32_t>(spv::FunctionControlMask::MaskNone));
function_instruction_.addIdOperand(functionType);
parent.mapInstruction(&function_instruction_);
parent.push_function(this);
function_instruction_.AddIdOperand(function_type_id);
parent.MapInstruction(&function_instruction_);
parent.AddFunction(this);
// OpFunctionParameter
Instruction* typeInst = parent.getInstruction(functionType);
int numParams = typeInst->operand_count() - 1;
for (int p = 0; p < numParams; ++p) {
auto param = new Instruction(firstParamId + p, typeInst->id_operand(p + 1),
spv::Op::OpFunctionParameter);
parent.mapInstruction(param);
Instruction* type_instr = parent.instruction(function_type_id);
int param_count = type_instr->operand_count() - 1;
for (int p = 0; p < param_count; ++p) {
auto param =
new Instruction(first_param_id + p, type_instr->id_operand(p + 1),
spv::Op::OpFunctionParameter);
parent.MapInstruction(param);
parameter_instructions_.push_back(param);
}
}
inline void Function::push_local_variable(Instruction* inst) {
blocks_[0]->push_local_variable(inst);
parent_.mapInstruction(inst);
inline void Function::AddLocalVariable(Instruction* instr) {
blocks_[0]->AddLocalVariable(instr);
parent_.MapInstruction(instr);
}
inline Block::Block(Id id, Function& parent)
@ -389,10 +407,10 @@ inline Block::Block(Id id, Function& parent)
instructions_.push_back(new Instruction(id, NoType, spv::Op::OpLabel));
}
inline void Block::push_instruction(Instruction* inst) {
inline void Block::AddInstruction(Instruction* inst) {
instructions_.push_back(inst);
if (inst->result_id()) {
parent_.parent().mapInstruction(inst);
parent_.parent().MapInstruction(inst);
}
}