2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-10-26 14:35:22 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "SkSLCompiler.h"
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
#include "ast/SkSLASTPrecision.h"
|
2016-10-13 20:25:34 +00:00
|
|
|
#include "SkSLCFGGenerator.h"
|
2016-12-12 20:33:30 +00:00
|
|
|
#include "SkSLGLSLCodeGenerator.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "SkSLIRGenerator.h"
|
|
|
|
#include "SkSLParser.h"
|
|
|
|
#include "SkSLSPIRVCodeGenerator.h"
|
|
|
|
#include "ir/SkSLExpression.h"
|
2017-04-20 23:31:52 +00:00
|
|
|
#include "ir/SkSLExpressionStatement.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "ir/SkSLIntLiteral.h"
|
2016-10-12 13:39:56 +00:00
|
|
|
#include "ir/SkSLModifiersDeclaration.h"
|
2017-04-20 23:31:52 +00:00
|
|
|
#include "ir/SkSLNop.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "ir/SkSLSymbolTable.h"
|
2017-04-20 23:31:52 +00:00
|
|
|
#include "ir/SkSLTernaryExpression.h"
|
2016-10-20 16:54:00 +00:00
|
|
|
#include "ir/SkSLUnresolvedFunction.h"
|
2016-10-13 20:25:34 +00:00
|
|
|
#include "ir/SkSLVarDeclarations.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2017-03-16 13:56:54 +00:00
|
|
|
#ifdef SK_ENABLE_SPIRV_VALIDATION
|
|
|
|
#include "spirv-tools/libspirv.hpp"
|
|
|
|
#endif
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
|
|
|
|
// include the built-in shader symbols as static strings
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl.include"
|
|
|
|
;
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_VERT_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl_vert.include"
|
|
|
|
;
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
static const char* SKSL_FRAG_INCLUDE =
|
2016-07-01 15:22:01 +00:00
|
|
|
#include "sksl_frag.include"
|
|
|
|
;
|
|
|
|
|
2017-02-16 21:37:32 +00:00
|
|
|
static const char* SKSL_GEOM_INCLUDE =
|
|
|
|
#include "sksl_geom.include"
|
|
|
|
;
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
2016-10-26 14:35:22 +00:00
|
|
|
Compiler::Compiler()
|
2016-07-01 15:22:01 +00:00
|
|
|
: fErrorCount(0) {
|
2017-03-30 18:11:58 +00:00
|
|
|
auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this));
|
|
|
|
auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this));
|
2016-07-25 17:08:54 +00:00
|
|
|
fIRGenerator = new IRGenerator(&fContext, symbols, *this);
|
2016-07-01 15:22:01 +00:00
|
|
|
fTypes = types;
|
2016-07-25 17:08:54 +00:00
|
|
|
#define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \
|
|
|
|
fContext.f ## t ## _Type.get())
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Void);
|
|
|
|
ADD_TYPE(Float);
|
|
|
|
ADD_TYPE(Vec2);
|
|
|
|
ADD_TYPE(Vec3);
|
|
|
|
ADD_TYPE(Vec4);
|
|
|
|
ADD_TYPE(Double);
|
|
|
|
ADD_TYPE(DVec2);
|
|
|
|
ADD_TYPE(DVec3);
|
|
|
|
ADD_TYPE(DVec4);
|
|
|
|
ADD_TYPE(Int);
|
|
|
|
ADD_TYPE(IVec2);
|
|
|
|
ADD_TYPE(IVec3);
|
|
|
|
ADD_TYPE(IVec4);
|
|
|
|
ADD_TYPE(UInt);
|
|
|
|
ADD_TYPE(UVec2);
|
|
|
|
ADD_TYPE(UVec3);
|
|
|
|
ADD_TYPE(UVec4);
|
|
|
|
ADD_TYPE(Bool);
|
|
|
|
ADD_TYPE(BVec2);
|
|
|
|
ADD_TYPE(BVec3);
|
|
|
|
ADD_TYPE(BVec4);
|
|
|
|
ADD_TYPE(Mat2x2);
|
2017-03-31 17:56:23 +00:00
|
|
|
types->addWithoutOwnership(String("mat2x2"), fContext.fMat2x2_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Mat2x3);
|
|
|
|
ADD_TYPE(Mat2x4);
|
|
|
|
ADD_TYPE(Mat3x2);
|
|
|
|
ADD_TYPE(Mat3x3);
|
2017-03-31 17:56:23 +00:00
|
|
|
types->addWithoutOwnership(String("mat3x3"), fContext.fMat3x3_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(Mat3x4);
|
|
|
|
ADD_TYPE(Mat4x2);
|
|
|
|
ADD_TYPE(Mat4x3);
|
|
|
|
ADD_TYPE(Mat4x4);
|
2017-03-31 17:56:23 +00:00
|
|
|
types->addWithoutOwnership(String("mat4x4"), fContext.fMat4x4_Type.get());
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(GenType);
|
|
|
|
ADD_TYPE(GenDType);
|
|
|
|
ADD_TYPE(GenIType);
|
|
|
|
ADD_TYPE(GenUType);
|
|
|
|
ADD_TYPE(GenBType);
|
|
|
|
ADD_TYPE(Mat);
|
|
|
|
ADD_TYPE(Vec);
|
|
|
|
ADD_TYPE(GVec);
|
|
|
|
ADD_TYPE(GVec2);
|
|
|
|
ADD_TYPE(GVec3);
|
|
|
|
ADD_TYPE(GVec4);
|
|
|
|
ADD_TYPE(DVec);
|
|
|
|
ADD_TYPE(IVec);
|
|
|
|
ADD_TYPE(UVec);
|
|
|
|
ADD_TYPE(BVec);
|
|
|
|
|
|
|
|
ADD_TYPE(Sampler1D);
|
|
|
|
ADD_TYPE(Sampler2D);
|
|
|
|
ADD_TYPE(Sampler3D);
|
2016-10-12 13:39:56 +00:00
|
|
|
ADD_TYPE(SamplerExternalOES);
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(SamplerCube);
|
|
|
|
ADD_TYPE(Sampler2DRect);
|
|
|
|
ADD_TYPE(Sampler1DArray);
|
|
|
|
ADD_TYPE(Sampler2DArray);
|
|
|
|
ADD_TYPE(SamplerCubeArray);
|
|
|
|
ADD_TYPE(SamplerBuffer);
|
|
|
|
ADD_TYPE(Sampler2DMS);
|
|
|
|
ADD_TYPE(Sampler2DMSArray);
|
|
|
|
|
2016-11-11 21:08:03 +00:00
|
|
|
ADD_TYPE(ISampler2D);
|
|
|
|
|
2016-11-16 17:06:01 +00:00
|
|
|
ADD_TYPE(Image2D);
|
|
|
|
ADD_TYPE(IImage2D);
|
|
|
|
|
2016-11-22 14:44:03 +00:00
|
|
|
ADD_TYPE(SubpassInput);
|
|
|
|
ADD_TYPE(SubpassInputMS);
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
ADD_TYPE(GSampler1D);
|
|
|
|
ADD_TYPE(GSampler2D);
|
|
|
|
ADD_TYPE(GSampler3D);
|
|
|
|
ADD_TYPE(GSamplerCube);
|
|
|
|
ADD_TYPE(GSampler2DRect);
|
|
|
|
ADD_TYPE(GSampler1DArray);
|
|
|
|
ADD_TYPE(GSampler2DArray);
|
|
|
|
ADD_TYPE(GSamplerCubeArray);
|
|
|
|
ADD_TYPE(GSamplerBuffer);
|
|
|
|
ADD_TYPE(GSampler2DMS);
|
|
|
|
ADD_TYPE(GSampler2DMSArray);
|
|
|
|
|
|
|
|
ADD_TYPE(Sampler1DShadow);
|
|
|
|
ADD_TYPE(Sampler2DShadow);
|
|
|
|
ADD_TYPE(SamplerCubeShadow);
|
|
|
|
ADD_TYPE(Sampler2DRectShadow);
|
|
|
|
ADD_TYPE(Sampler1DArrayShadow);
|
|
|
|
ADD_TYPE(Sampler2DArrayShadow);
|
|
|
|
ADD_TYPE(SamplerCubeArrayShadow);
|
|
|
|
ADD_TYPE(GSampler2DArrayShadow);
|
|
|
|
ADD_TYPE(GSamplerCubeArrayShadow);
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
String skCapsName("sk_Caps");
|
|
|
|
Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName,
|
2016-11-21 20:59:48 +00:00
|
|
|
*fContext.fSkCaps_Type, Variable::kGlobal_Storage);
|
|
|
|
fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
|
|
|
|
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag ignored1;
|
|
|
|
std::vector<std::unique_ptr<ProgramElement>> ignored2;
|
2017-03-31 17:56:23 +00:00
|
|
|
this->internalConvertProgram(String(SKSL_INCLUDE), &ignored1, &ignored2);
|
2016-10-20 16:54:00 +00:00
|
|
|
fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
|
2016-07-01 15:22:01 +00:00
|
|
|
ASSERT(!fErrorCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Compiler::~Compiler() {
|
|
|
|
delete fIRGenerator;
|
|
|
|
}
|
|
|
|
|
2016-10-13 20:25:34 +00:00
|
|
|
// add the definition created by assigning to the lvalue to the definition set
|
2017-01-19 18:32:00 +00:00
|
|
|
void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
|
|
|
|
DefinitionMap* definitions) {
|
2016-10-13 20:25:34 +00:00
|
|
|
switch (lvalue->fKind) {
|
|
|
|
case Expression::kVariableReference_Kind: {
|
|
|
|
const Variable& var = ((VariableReference*) lvalue)->fVariable;
|
|
|
|
if (var.fStorage == Variable::kLocal_Storage) {
|
|
|
|
(*definitions)[&var] = expr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kSwizzle_Kind:
|
|
|
|
// We consider the variable written to as long as at least some of its components have
|
|
|
|
// been written to. This will lead to some false negatives (we won't catch it if you
|
|
|
|
// write to foo.x and then read foo.y), but being stricter could lead to false positives
|
2016-10-26 14:35:22 +00:00
|
|
|
// (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
|
|
|
|
// but since we pass foo as a whole it is flagged as an error) unless we perform a much
|
2016-10-13 20:25:34 +00:00
|
|
|
// more complicated whole-program analysis. This is probably good enough.
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
case Expression::kIndex_Kind:
|
|
|
|
// see comments in Swizzle
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
case Expression::kFieldAccess_Kind:
|
|
|
|
// see comments in Swizzle
|
2016-10-26 14:35:22 +00:00
|
|
|
this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
2016-10-13 20:25:34 +00:00
|
|
|
definitions);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// not an lvalue, can't happen
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add local variables defined by this node to the set
|
2016-10-26 14:35:22 +00:00
|
|
|
void Compiler::addDefinitions(const BasicBlock::Node& node,
|
2017-01-19 18:32:00 +00:00
|
|
|
DefinitionMap* definitions) {
|
2016-10-13 20:25:34 +00:00
|
|
|
switch (node.fKind) {
|
|
|
|
case BasicBlock::Node::kExpression_Kind: {
|
2017-04-20 23:31:52 +00:00
|
|
|
ASSERT(node.expression());
|
|
|
|
const Expression* expr = (Expression*) node.expression()->get();
|
2017-01-19 18:32:00 +00:00
|
|
|
switch (expr->fKind) {
|
|
|
|
case Expression::kBinary_Kind: {
|
|
|
|
BinaryExpression* b = (BinaryExpression*) expr;
|
|
|
|
if (b->fOperator == Token::EQ) {
|
|
|
|
this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
|
|
|
|
} else if (Token::IsAssignment(b->fOperator)) {
|
|
|
|
this->addDefinition(
|
|
|
|
b->fLeft.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kPrefix_Kind: {
|
|
|
|
const PrefixExpression* p = (PrefixExpression*) expr;
|
|
|
|
if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
|
|
|
|
this->addDefinition(
|
|
|
|
p->fOperand.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
}
|
|
|
|
break;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
2017-01-19 18:32:00 +00:00
|
|
|
case Expression::kPostfix_Kind: {
|
|
|
|
const PostfixExpression* p = (PostfixExpression*) expr;
|
|
|
|
if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) {
|
|
|
|
this->addDefinition(
|
|
|
|
p->fOperand.get(),
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
case Expression::kVariableReference_Kind: {
|
|
|
|
const VariableReference* v = (VariableReference*) expr;
|
|
|
|
if (v->fRefKind != VariableReference::kRead_RefKind) {
|
|
|
|
this->addDefinition(
|
|
|
|
v,
|
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression,
|
|
|
|
definitions);
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 18:32:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BasicBlock::Node::kStatement_Kind: {
|
2017-04-20 23:31:52 +00:00
|
|
|
const Statement* stmt = (Statement*) node.statement()->get();
|
2016-10-13 20:25:34 +00:00
|
|
|
if (stmt->fKind == Statement::kVarDeclarations_Kind) {
|
2017-01-19 18:32:00 +00:00
|
|
|
VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
|
2017-04-20 23:31:52 +00:00
|
|
|
for (const auto& decl : vd->fDeclaration->fVars) {
|
|
|
|
if (decl->fValue) {
|
|
|
|
(*definitions)[decl->fVar] = &decl->fValue;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
|
|
|
|
BasicBlock& block = cfg->fBlocks[blockId];
|
|
|
|
|
|
|
|
// compute definitions after this block
|
2017-01-19 18:32:00 +00:00
|
|
|
DefinitionMap after = block.fBefore;
|
2016-10-13 20:25:34 +00:00
|
|
|
for (const BasicBlock::Node& n : block.fNodes) {
|
|
|
|
this->addDefinitions(n, &after);
|
|
|
|
}
|
|
|
|
|
|
|
|
// propagate definitions to exits
|
|
|
|
for (BlockId exitId : block.fExits) {
|
|
|
|
BasicBlock& exit = cfg->fBlocks[exitId];
|
|
|
|
for (const auto& pair : after) {
|
2017-01-19 18:32:00 +00:00
|
|
|
std::unique_ptr<Expression>* e1 = pair.second;
|
|
|
|
auto found = exit.fBefore.find(pair.first);
|
|
|
|
if (found == exit.fBefore.end()) {
|
|
|
|
// exit has no definition for it, just copy it
|
|
|
|
workList->insert(exitId);
|
2016-10-13 20:25:34 +00:00
|
|
|
exit.fBefore[pair.first] = e1;
|
|
|
|
} else {
|
2017-01-19 18:32:00 +00:00
|
|
|
// exit has a (possibly different) value already defined
|
|
|
|
std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
|
2016-10-13 20:25:34 +00:00
|
|
|
if (e1 != e2) {
|
|
|
|
// definition has changed, merge and add exit block to worklist
|
|
|
|
workList->insert(exitId);
|
2017-02-27 18:26:45 +00:00
|
|
|
if (e1 && e2) {
|
|
|
|
exit.fBefore[pair.first] =
|
2017-01-19 18:32:00 +00:00
|
|
|
(std::unique_ptr<Expression>*) &fContext.fDefined_Expression;
|
2017-02-27 18:26:45 +00:00
|
|
|
} else {
|
|
|
|
exit.fBefore[pair.first] = nullptr;
|
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a map which maps all local variables in the function to null, indicating that their value
|
|
|
|
// is initially unknown
|
2017-01-19 18:32:00 +00:00
|
|
|
static DefinitionMap compute_start_state(const CFG& cfg) {
|
|
|
|
DefinitionMap result;
|
2016-10-26 14:35:22 +00:00
|
|
|
for (const auto& block : cfg.fBlocks) {
|
|
|
|
for (const auto& node : block.fNodes) {
|
2016-10-13 20:25:34 +00:00
|
|
|
if (node.fKind == BasicBlock::Node::kStatement_Kind) {
|
2017-04-20 23:31:52 +00:00
|
|
|
ASSERT(node.statement());
|
|
|
|
const Statement* s = node.statement()->get();
|
2016-10-13 20:25:34 +00:00
|
|
|
if (s->fKind == Statement::kVarDeclarations_Kind) {
|
|
|
|
const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
|
2017-04-20 23:31:52 +00:00
|
|
|
for (const auto& decl : vd->fDeclaration->fVars) {
|
|
|
|
result[decl->fVar] = nullptr;
|
2016-10-26 14:35:22 +00:00
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-20 23:31:52 +00:00
|
|
|
/**
|
|
|
|
* Returns true if assigning to this lvalue has no effect.
|
|
|
|
*/
|
|
|
|
static bool is_dead(const Expression& lvalue) {
|
|
|
|
switch (lvalue.fKind) {
|
|
|
|
case Expression::kVariableReference_Kind:
|
|
|
|
return ((VariableReference&) lvalue).fVariable.dead();
|
|
|
|
case Expression::kSwizzle_Kind:
|
|
|
|
return is_dead(*((Swizzle&) lvalue).fBase);
|
|
|
|
case Expression::kFieldAccess_Kind:
|
|
|
|
return is_dead(*((FieldAccess&) lvalue).fBase);
|
|
|
|
case Expression::kIndex_Kind: {
|
|
|
|
const IndexExpression& idx = (IndexExpression&) lvalue;
|
|
|
|
return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this is an assignment which can be collapsed down to just the right hand side due
|
|
|
|
* to a dead target and lack of side effects on the left hand side.
|
|
|
|
*/
|
|
|
|
static bool dead_assignment(const BinaryExpression& b) {
|
|
|
|
if (!Token::IsAssignment(b.fOperator)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return is_dead(*b.fLeft);
|
|
|
|
}
|
2016-10-13 20:25:34 +00:00
|
|
|
|
2017-04-20 23:31:52 +00:00
|
|
|
void Compiler::computeDataFlow(CFG* cfg) {
|
|
|
|
cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
|
2016-10-13 20:25:34 +00:00
|
|
|
std::set<BlockId> workList;
|
2017-04-20 23:31:52 +00:00
|
|
|
for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
|
2016-10-13 20:25:34 +00:00
|
|
|
workList.insert(i);
|
|
|
|
}
|
|
|
|
while (workList.size()) {
|
|
|
|
BlockId next = *workList.begin();
|
|
|
|
workList.erase(workList.begin());
|
2017-04-20 23:31:52 +00:00
|
|
|
this->scanCFG(cfg, next, &workList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
|
|
|
|
* IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
|
|
|
|
* the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
|
|
|
|
* need to be regenerated).
|
|
|
|
*/
|
|
|
|
bool try_replace_expression(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
std::unique_ptr<Expression>* newExpression) {
|
|
|
|
std::unique_ptr<Expression>* target = (*iter)->expression();
|
|
|
|
if (!b->tryRemoveExpression(iter)) {
|
|
|
|
*target = std::move(*newExpression);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*target = std::move(*newExpression);
|
|
|
|
return b->tryInsertExpression(iter, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-27 20:24:51 +00:00
|
|
|
* Returns true if the expression is a constant numeric literal with the specified value, or a
|
|
|
|
* constant vector with all elements equal to the specified value.
|
2017-04-20 23:31:52 +00:00
|
|
|
*/
|
2017-04-27 20:24:51 +00:00
|
|
|
bool is_constant(const Expression& expr, double value) {
|
2017-04-20 23:31:52 +00:00
|
|
|
switch (expr.fKind) {
|
|
|
|
case Expression::kIntLiteral_Kind:
|
|
|
|
return ((IntLiteral&) expr).fValue == value;
|
|
|
|
case Expression::kFloatLiteral_Kind:
|
|
|
|
return ((FloatLiteral&) expr).fValue == value;
|
2017-04-27 20:24:51 +00:00
|
|
|
case Expression::kConstructor_Kind: {
|
|
|
|
Constructor& c = (Constructor&) expr;
|
|
|
|
if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) {
|
|
|
|
for (int i = 0; i < c.fType.columns(); ++i) {
|
|
|
|
if (!is_constant(c.getVecComponent(i), value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapses the binary expression pointed to by iter down to just the right side (in both the IR
|
|
|
|
* and CFG structures).
|
|
|
|
*/
|
|
|
|
void delete_left(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
*outUpdated = true;
|
2017-05-05 14:04:06 +00:00
|
|
|
std::unique_ptr<Expression>* target = (*iter)->expression();
|
|
|
|
ASSERT((*target)->fKind == Expression::kBinary_Kind);
|
|
|
|
BinaryExpression& bin = (BinaryExpression&) **target;
|
|
|
|
bool result;
|
|
|
|
if (bin.fOperator == Token::EQ) {
|
|
|
|
result = !b->tryRemoveLValueBefore(iter, bin.fLeft.get());
|
|
|
|
} else {
|
|
|
|
result = !b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
|
|
|
|
}
|
|
|
|
if (!result) {
|
|
|
|
*target = std::move(bin.fRight);
|
2017-04-20 23:31:52 +00:00
|
|
|
*outNeedsRescan = true;
|
2017-05-05 14:04:06 +00:00
|
|
|
return;
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
2017-05-05 14:04:06 +00:00
|
|
|
--(*iter);
|
|
|
|
ASSERT((*iter)->expression() == &bin.fRight);
|
|
|
|
*iter = b->fNodes.erase(*iter);
|
|
|
|
ASSERT((*iter)->expression() == target);
|
|
|
|
*target = std::move(bin.fRight);
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
|
|
|
|
* CFG structures).
|
|
|
|
*/
|
|
|
|
void delete_right(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
*outUpdated = true;
|
2017-05-05 14:04:06 +00:00
|
|
|
std::unique_ptr<Expression>* target = (*iter)->expression();
|
|
|
|
ASSERT((*target)->fKind == Expression::kBinary_Kind);
|
|
|
|
BinaryExpression& bin = (BinaryExpression&) **target;
|
|
|
|
if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
|
|
|
|
*target = std::move(bin.fLeft);
|
2017-04-20 23:31:52 +00:00
|
|
|
*outNeedsRescan = true;
|
2017-05-05 14:04:06 +00:00
|
|
|
return;
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
2017-05-05 14:04:06 +00:00
|
|
|
--(*iter);
|
|
|
|
ASSERT((*iter)->expression() == &bin.fLeft);
|
|
|
|
*iter = b->fNodes.erase(*iter);
|
|
|
|
ASSERT((*iter)->expression() == target);
|
|
|
|
*target = std::move(bin.fLeft);
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:24:51 +00:00
|
|
|
/**
|
|
|
|
* Constructs the specified type using a single argument.
|
|
|
|
*/
|
|
|
|
static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
|
|
|
|
std::vector<std::unique_ptr<Expression>> args;
|
|
|
|
args.push_back(std::move(v));
|
|
|
|
auto result = std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
|
|
|
|
* expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
|
|
|
|
*/
|
|
|
|
static void vectorize(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
const Type& type,
|
|
|
|
std::unique_ptr<Expression>* otherExpression,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
ASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
|
|
|
|
ASSERT(type.kind() == Type::kVector_Kind);
|
|
|
|
ASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
|
|
|
|
*outUpdated = true;
|
|
|
|
std::unique_ptr<Expression>* target = (*iter)->expression();
|
|
|
|
if (!b->tryRemoveExpression(iter)) {
|
|
|
|
*target = construct(type, std::move(*otherExpression));
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
} else {
|
|
|
|
*target = construct(type, std::move(*otherExpression));
|
|
|
|
if (!b->tryInsertExpression(iter, target)) {
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
|
|
|
|
* left to yield vec<n>(x).
|
|
|
|
*/
|
|
|
|
static void vectorize_left(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
|
|
|
|
vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
|
|
|
|
* right to yield vec<n>(y).
|
|
|
|
*/
|
|
|
|
static void vectorize_right(BasicBlock* b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
|
|
|
|
vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that an expression which we were writing to is no longer being written to
|
|
|
|
void clear_write(const Expression& expr) {
|
|
|
|
switch (expr.fKind) {
|
|
|
|
case Expression::kVariableReference_Kind: {
|
|
|
|
((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kFieldAccess_Kind:
|
|
|
|
clear_write(*((FieldAccess&) expr).fBase);
|
|
|
|
break;
|
|
|
|
case Expression::kSwizzle_Kind:
|
|
|
|
clear_write(*((Swizzle&) expr).fBase);
|
|
|
|
break;
|
|
|
|
case Expression::kIndex_Kind:
|
|
|
|
clear_write(*((IndexExpression&) expr).fBase);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ABORT("shouldn't be writing to this kind of expression\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 23:31:52 +00:00
|
|
|
void Compiler::simplifyExpression(DefinitionMap& definitions,
|
|
|
|
BasicBlock& b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
std::unordered_set<const Variable*>* undefinedVariables,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
Expression* expr = (*iter)->expression()->get();
|
|
|
|
ASSERT(expr);
|
|
|
|
if ((*iter)->fConstantPropagation) {
|
|
|
|
std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
|
|
|
|
if (optimized) {
|
|
|
|
if (!try_replace_expression(&b, iter, &optimized)) {
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
ASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
|
|
|
|
expr = (*iter)->expression()->get();
|
|
|
|
*outUpdated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (expr->fKind) {
|
|
|
|
case Expression::kVariableReference_Kind: {
|
|
|
|
const Variable& var = ((VariableReference*) expr)->fVariable;
|
|
|
|
if (var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
|
|
|
|
(*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
|
|
|
|
(*undefinedVariables).insert(&var);
|
|
|
|
this->error(expr->fPosition,
|
|
|
|
"'" + var.fName + "' has not been assigned");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kTernary_Kind: {
|
|
|
|
TernaryExpression* t = (TernaryExpression*) expr;
|
|
|
|
if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
|
|
|
|
// ternary has a constant test, replace it with either the true or
|
|
|
|
// false branch
|
|
|
|
if (((BoolLiteral&) *t->fTest).fValue) {
|
|
|
|
(*iter)->setExpression(std::move(t->fIfTrue));
|
|
|
|
} else {
|
|
|
|
(*iter)->setExpression(std::move(t->fIfFalse));
|
|
|
|
}
|
|
|
|
*outUpdated = true;
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expression::kBinary_Kind: {
|
|
|
|
BinaryExpression* bin = (BinaryExpression*) expr;
|
2017-05-05 14:04:06 +00:00
|
|
|
if (dead_assignment(*bin)) {
|
|
|
|
delete_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// collapse useless expressions like x * 1 or x + 0
|
2017-04-27 20:24:51 +00:00
|
|
|
if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
|
|
|
|
(bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
|
|
|
|
((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
|
|
|
|
(bin->fRight->fType.kind() != Type::kVector_Kind))) {
|
|
|
|
break;
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
switch (bin->fOperator) {
|
|
|
|
case Token::STAR:
|
|
|
|
if (is_constant(*bin->fLeft, 1)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kScalar_Kind) {
|
|
|
|
// vec4(1) * x -> vec4(x)
|
|
|
|
vectorize_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// 1 * x -> x
|
|
|
|
// 1 * vec4(x) -> vec4(x)
|
|
|
|
// vec4(1) * vec4(x) -> vec4(x)
|
|
|
|
delete_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (is_constant(*bin->fLeft, 0)) {
|
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// 0 * vec4(x) -> vec4(0)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// 0 * x -> 0
|
|
|
|
// vec4(0) * x -> vec4(0)
|
|
|
|
// vec4(0) * vec4(x) -> vec4(0)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
|
|
|
else if (is_constant(*bin->fRight, 1)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// x * vec4(1) -> vec4(x)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// x * 1 -> x
|
|
|
|
// vec4(x) * 1 -> vec4(x)
|
|
|
|
// vec4(x) * vec4(1) -> vec4(x)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (is_constant(*bin->fRight, 0)) {
|
|
|
|
if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kScalar_Kind) {
|
|
|
|
// vec4(x) * 0 -> vec4(0)
|
|
|
|
vectorize_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// x * 0 -> 0
|
|
|
|
// x * vec4(0) -> vec4(0)
|
|
|
|
// vec4(x) * vec4(0) -> vec4(0)
|
|
|
|
delete_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-04-21 14:23:37 +00:00
|
|
|
case Token::PLUS:
|
2017-04-20 23:31:52 +00:00
|
|
|
if (is_constant(*bin->fLeft, 0)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kScalar_Kind) {
|
|
|
|
// vec4(0) + x -> vec4(x)
|
|
|
|
vectorize_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// 0 + x -> x
|
|
|
|
// 0 + vec4(x) -> vec4(x)
|
|
|
|
// vec4(0) + vec4(x) -> vec4(x)
|
|
|
|
delete_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
} else if (is_constant(*bin->fRight, 0)) {
|
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// x + vec4(0) -> vec4(x)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// x + 0 -> x
|
|
|
|
// vec4(x) + 0 -> vec4(x)
|
|
|
|
// vec4(x) + vec4(0) -> vec4(x)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
2017-04-27 20:24:51 +00:00
|
|
|
break;
|
|
|
|
case Token::MINUS:
|
2017-04-21 14:23:37 +00:00
|
|
|
if (is_constant(*bin->fRight, 0)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// x - vec4(0) -> vec4(x)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// x - 0 -> x
|
|
|
|
// vec4(x) - 0 -> vec4(x)
|
|
|
|
// vec4(x) - vec4(0) -> vec4(x)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Token::SLASH:
|
|
|
|
if (is_constant(*bin->fRight, 1)) {
|
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// x / vec4(1) -> vec4(x)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// x / 1 -> x
|
|
|
|
// vec4(x) / 1 -> vec4(x)
|
|
|
|
// vec4(x) / vec4(1) -> vec4(x)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
} else if (is_constant(*bin->fLeft, 0)) {
|
|
|
|
if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
|
|
|
|
bin->fRight->fType.kind() == Type::kVector_Kind) {
|
|
|
|
// 0 / vec4(x) -> vec4(0)
|
|
|
|
vectorize_left(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
} else {
|
|
|
|
// 0 / x -> 0
|
|
|
|
// vec4(0) / x -> vec4(0)
|
|
|
|
// vec4(0) / vec4(x) -> vec4(0)
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Token::PLUSEQ:
|
|
|
|
if (is_constant(*bin->fRight, 0)) {
|
|
|
|
clear_write(*bin->fLeft);
|
2017-04-21 14:23:37 +00:00
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
break;
|
2017-04-27 20:24:51 +00:00
|
|
|
case Token::MINUSEQ:
|
2017-04-21 14:23:37 +00:00
|
|
|
if (is_constant(*bin->fRight, 0)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
clear_write(*bin->fLeft);
|
2017-04-20 23:31:52 +00:00
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
break;
|
2017-04-27 20:24:51 +00:00
|
|
|
case Token::STAREQ:
|
|
|
|
if (is_constant(*bin->fRight, 1)) {
|
|
|
|
clear_write(*bin->fLeft);
|
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Token::SLASHEQ:
|
2017-04-20 23:31:52 +00:00
|
|
|
if (is_constant(*bin->fRight, 1)) {
|
2017-04-27 20:24:51 +00:00
|
|
|
clear_write(*bin->fLeft);
|
2017-04-20 23:31:52 +00:00
|
|
|
delete_right(&b, iter, outUpdated, outNeedsRescan);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::simplifyStatement(DefinitionMap& definitions,
|
|
|
|
BasicBlock& b,
|
|
|
|
std::vector<BasicBlock::Node>::iterator* iter,
|
|
|
|
std::unordered_set<const Variable*>* undefinedVariables,
|
|
|
|
bool* outUpdated,
|
|
|
|
bool* outNeedsRescan) {
|
|
|
|
Statement* stmt = (*iter)->statement()->get();
|
|
|
|
switch (stmt->fKind) {
|
|
|
|
case Statement::kVarDeclarations_Kind: {
|
|
|
|
VarDeclarations& vd = *((VarDeclarationsStatement&) *stmt).fDeclaration;
|
|
|
|
for (auto varIter = vd.fVars.begin(); varIter != vd.fVars.end(); ) {
|
|
|
|
const auto& varDecl = **varIter;
|
|
|
|
if (varDecl.fVar->dead() &&
|
|
|
|
(!varDecl.fValue ||
|
|
|
|
!varDecl.fValue->hasSideEffects())) {
|
|
|
|
if (varDecl.fValue) {
|
|
|
|
ASSERT((*iter)->statement()->get() == stmt);
|
|
|
|
if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
varIter = vd.fVars.erase(varIter);
|
|
|
|
*outUpdated = true;
|
|
|
|
} else {
|
|
|
|
++varIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vd.fVars.size() == 0) {
|
|
|
|
(*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kIf_Kind: {
|
|
|
|
IfStatement& i = (IfStatement&) *stmt;
|
|
|
|
if (i.fIfFalse && i.fIfFalse->isEmpty()) {
|
|
|
|
// else block doesn't do anything, remove it
|
|
|
|
i.fIfFalse.reset();
|
|
|
|
*outUpdated = true;
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
|
|
|
|
// if block doesn't do anything, no else block
|
|
|
|
if (i.fTest->hasSideEffects()) {
|
|
|
|
// test has side effects, keep it
|
|
|
|
(*iter)->setStatement(std::unique_ptr<Statement>(
|
|
|
|
new ExpressionStatement(std::move(i.fTest))));
|
|
|
|
} else {
|
|
|
|
// no if, no else, no test side effects, kill the whole if
|
|
|
|
// statement
|
|
|
|
(*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
|
|
|
|
}
|
|
|
|
*outUpdated = true;
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Statement::kExpression_Kind: {
|
|
|
|
ExpressionStatement& e = (ExpressionStatement&) *stmt;
|
|
|
|
ASSERT((*iter)->statement()->get() == &e);
|
|
|
|
if (!e.fExpression->hasSideEffects()) {
|
|
|
|
// Expression statement with no side effects, kill it
|
|
|
|
if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
|
|
|
|
*outNeedsRescan = true;
|
|
|
|
}
|
|
|
|
ASSERT((*iter)->statement()->get() == stmt);
|
|
|
|
(*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
|
|
|
|
*outUpdated = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::scanCFG(FunctionDefinition& f) {
|
|
|
|
CFG cfg = CFGGenerator().getCFG(f);
|
|
|
|
this->computeDataFlow(&cfg);
|
2016-10-13 20:25:34 +00:00
|
|
|
|
|
|
|
// check for unreachable code
|
|
|
|
for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
|
2016-10-26 14:35:22 +00:00
|
|
|
if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
|
2016-10-13 20:25:34 +00:00
|
|
|
cfg.fBlocks[i].fNodes.size()) {
|
2017-01-19 18:32:00 +00:00
|
|
|
Position p;
|
|
|
|
switch (cfg.fBlocks[i].fNodes[0].fKind) {
|
|
|
|
case BasicBlock::Node::kStatement_Kind:
|
2017-04-20 23:31:52 +00:00
|
|
|
p = (*cfg.fBlocks[i].fNodes[0].statement())->fPosition;
|
2017-01-19 18:32:00 +00:00
|
|
|
break;
|
|
|
|
case BasicBlock::Node::kExpression_Kind:
|
2017-04-20 23:31:52 +00:00
|
|
|
p = (*cfg.fBlocks[i].fNodes[0].expression())->fPosition;
|
2017-01-19 18:32:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-31 17:56:23 +00:00
|
|
|
this->error(p, String("unreachable"));
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fErrorCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-20 23:31:52 +00:00
|
|
|
// check for dead code & undefined variables, perform constant propagation
|
|
|
|
std::unordered_set<const Variable*> undefinedVariables;
|
|
|
|
bool updated;
|
|
|
|
bool needsRescan = false;
|
|
|
|
do {
|
|
|
|
if (needsRescan) {
|
|
|
|
cfg = CFGGenerator().getCFG(f);
|
|
|
|
this->computeDataFlow(&cfg);
|
|
|
|
needsRescan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
updated = false;
|
|
|
|
for (BasicBlock& b : cfg.fBlocks) {
|
|
|
|
DefinitionMap definitions = b.fBefore;
|
|
|
|
|
|
|
|
for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
|
|
|
|
if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
|
|
|
|
this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
|
|
|
|
&needsRescan);
|
|
|
|
} else {
|
|
|
|
this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
|
|
|
|
&needsRescan);
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
this->addDefinitions(*iter, &definitions);
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-20 23:31:52 +00:00
|
|
|
} while (updated);
|
|
|
|
ASSERT(!needsRescan);
|
2016-10-13 20:25:34 +00:00
|
|
|
|
|
|
|
// check for missing return
|
|
|
|
if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
|
|
|
|
if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
|
2017-03-31 17:56:23 +00:00
|
|
|
this->error(f.fPosition, String("function can exit without returning a value"));
|
2016-10-13 20:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
void Compiler::internalConvertProgram(String text,
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag* defaultPrecision,
|
2016-07-01 15:22:01 +00:00
|
|
|
std::vector<std::unique_ptr<ProgramElement>>* result) {
|
|
|
|
Parser parser(text, *fTypes, *this);
|
|
|
|
std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
|
|
|
|
if (fErrorCount) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
*defaultPrecision = Modifiers::kHighp_Flag;
|
2016-07-01 15:22:01 +00:00
|
|
|
for (size_t i = 0; i < parsed.size(); i++) {
|
|
|
|
ASTDeclaration& decl = *parsed[i];
|
|
|
|
switch (decl.fKind) {
|
|
|
|
case ASTDeclaration::kVar_Kind: {
|
2016-09-07 20:37:16 +00:00
|
|
|
std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations(
|
2016-10-26 14:35:22 +00:00
|
|
|
(ASTVarDeclarations&) decl,
|
2016-07-01 15:22:01 +00:00
|
|
|
Variable::kGlobal_Storage);
|
|
|
|
if (s) {
|
|
|
|
result->push_back(std::move(s));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ASTDeclaration::kFunction_Kind: {
|
|
|
|
std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction(
|
|
|
|
(ASTFunction&) decl);
|
2016-10-13 20:25:34 +00:00
|
|
|
if (!fErrorCount && f) {
|
|
|
|
this->scanCFG(*f);
|
2016-07-01 15:22:01 +00:00
|
|
|
result->push_back(std::move(f));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
case ASTDeclaration::kModifiers_Kind: {
|
|
|
|
std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration(
|
|
|
|
(ASTModifiersDeclaration&) decl);
|
|
|
|
if (f) {
|
|
|
|
result->push_back(std::move(f));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
case ASTDeclaration::kInterfaceBlock_Kind: {
|
|
|
|
std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock(
|
|
|
|
(ASTInterfaceBlock&) decl);
|
|
|
|
if (i) {
|
|
|
|
result->push_back(std::move(i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ASTDeclaration::kExtension_Kind: {
|
|
|
|
std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl);
|
|
|
|
if (e) {
|
|
|
|
result->push_back(std::move(e));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
case ASTDeclaration::kPrecision_Kind: {
|
|
|
|
*defaultPrecision = ((ASTPrecision&) decl).fPrecision;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
default:
|
|
|
|
ABORT("unsupported declaration: %s\n", decl.description().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
|
2016-12-12 20:33:30 +00:00
|
|
|
const Program::Settings& settings) {
|
2016-07-01 15:22:01 +00:00
|
|
|
fErrorText = "";
|
|
|
|
fErrorCount = 0;
|
2016-12-12 20:33:30 +00:00
|
|
|
fIRGenerator->start(&settings);
|
2016-07-25 17:08:54 +00:00
|
|
|
std::vector<std::unique_ptr<ProgramElement>> elements;
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag ignored;
|
2016-07-01 15:22:01 +00:00
|
|
|
switch (kind) {
|
|
|
|
case Program::kVertex_Kind:
|
2017-03-31 17:56:23 +00:00
|
|
|
this->internalConvertProgram(String(SKSL_VERT_INCLUDE), &ignored, &elements);
|
2016-07-01 15:22:01 +00:00
|
|
|
break;
|
|
|
|
case Program::kFragment_Kind:
|
2017-03-31 17:56:23 +00:00
|
|
|
this->internalConvertProgram(String(SKSL_FRAG_INCLUDE), &ignored, &elements);
|
2016-07-01 15:22:01 +00:00
|
|
|
break;
|
2017-02-16 21:37:32 +00:00
|
|
|
case Program::kGeometry_Kind:
|
2017-03-31 17:56:23 +00:00
|
|
|
this->internalConvertProgram(String(SKSL_GEOM_INCLUDE), &ignored, &elements);
|
2017-02-16 21:37:32 +00:00
|
|
|
break;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-10-20 16:54:00 +00:00
|
|
|
fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
|
2016-10-12 13:39:56 +00:00
|
|
|
Modifiers::Flag defaultPrecision;
|
|
|
|
this->internalConvertProgram(text, &defaultPrecision, &elements);
|
2016-12-12 20:33:30 +00:00
|
|
|
auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext,
|
|
|
|
std::move(elements),
|
|
|
|
fIRGenerator->fSymbolTable,
|
|
|
|
fIRGenerator->fInputs));
|
2016-11-21 20:59:48 +00:00
|
|
|
fIRGenerator->finish();
|
2016-07-01 15:22:01 +00:00
|
|
|
this->writeErrorCount();
|
2016-12-12 20:33:30 +00:00
|
|
|
if (fErrorCount) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-07-25 17:08:54 +00:00
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
bool Compiler::toSPIRV(const Program& program, OutputStream& out) {
|
2017-03-16 13:56:54 +00:00
|
|
|
#ifdef SK_ENABLE_SPIRV_VALIDATION
|
2017-03-31 17:56:23 +00:00
|
|
|
StringStream buffer;
|
2017-03-16 13:56:54 +00:00
|
|
|
SPIRVCodeGenerator cg(&fContext, &program, this, &buffer);
|
|
|
|
bool result = cg.generateCode();
|
|
|
|
if (result) {
|
|
|
|
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
|
2017-03-31 17:56:23 +00:00
|
|
|
ASSERT(0 == buffer.size() % 4);
|
2017-03-16 13:56:54 +00:00
|
|
|
auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
|
|
|
|
SkDebugf("SPIR-V validation error: %s\n", m);
|
|
|
|
};
|
|
|
|
tools.SetMessageConsumer(dumpmsg);
|
|
|
|
// Verify that the SPIR-V we produced is valid. If this assert fails, check the logs prior
|
|
|
|
// to the failure to see the validation errors.
|
2017-03-31 17:56:23 +00:00
|
|
|
ASSERT_RESULT(tools.Validate((const uint32_t*) buffer.data(), buffer.size() / 4));
|
|
|
|
out.write(buffer.data(), buffer.size());
|
2017-03-16 13:56:54 +00:00
|
|
|
}
|
|
|
|
#else
|
2016-12-12 20:33:30 +00:00
|
|
|
SPIRVCodeGenerator cg(&fContext, &program, this, &out);
|
|
|
|
bool result = cg.generateCode();
|
2017-03-16 13:56:54 +00:00
|
|
|
#endif
|
2016-12-12 20:33:30 +00:00
|
|
|
this->writeErrorCount();
|
2016-07-01 15:22:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
bool Compiler::toSPIRV(const Program& program, String* out) {
|
|
|
|
StringStream buffer;
|
2016-12-12 20:33:30 +00:00
|
|
|
bool result = this->toSPIRV(program, buffer);
|
|
|
|
if (result) {
|
2017-03-31 17:56:23 +00:00
|
|
|
*out = String(buffer.data(), buffer.size());
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-12-12 20:33:30 +00:00
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
bool Compiler::toGLSL(const Program& program, OutputStream& out) {
|
2016-12-12 20:33:30 +00:00
|
|
|
GLSLCodeGenerator cg(&fContext, &program, this, &out);
|
|
|
|
bool result = cg.generateCode();
|
|
|
|
this->writeErrorCount();
|
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
bool Compiler::toGLSL(const Program& program, String* out) {
|
|
|
|
StringStream buffer;
|
2016-12-12 20:33:30 +00:00
|
|
|
bool result = this->toGLSL(program, buffer);
|
2016-07-01 15:22:01 +00:00
|
|
|
if (result) {
|
2017-03-31 17:56:23 +00:00
|
|
|
*out = String(buffer.data(), buffer.size());
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2016-08-03 19:43:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
void Compiler::error(Position position, String msg) {
|
2016-12-12 20:33:30 +00:00
|
|
|
fErrorCount++;
|
|
|
|
fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
|
2016-11-21 20:59:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
String Compiler::errorText() {
|
|
|
|
String result = fErrorText;
|
2016-12-12 20:33:30 +00:00
|
|
|
return result;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
void Compiler::writeErrorCount() {
|
|
|
|
if (fErrorCount) {
|
|
|
|
fErrorText += to_string(fErrorCount) + " error";
|
|
|
|
if (fErrorCount > 1) {
|
|
|
|
fErrorText += "s";
|
|
|
|
}
|
|
|
|
fErrorText += "\n";
|
2016-08-03 19:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
} // namespace
|