ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #include "SkSLCompiler.h" |
| 9 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 10 | #include "ast/SkSLASTPrecision.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include "SkSLCFGGenerator.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 12 | #include "SkSLGLSLCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include "SkSLIRGenerator.h" |
| 14 | #include "SkSLParser.h" |
| 15 | #include "SkSLSPIRVCodeGenerator.h" |
| 16 | #include "ir/SkSLExpression.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 17 | #include "ir/SkSLExpressionStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 19 | #include "ir/SkSLModifiersDeclaration.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 20 | #include "ir/SkSLNop.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | #include "ir/SkSLSymbolTable.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 22 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 23 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 24 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 26 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 27 | #include "spirv-tools/libspirv.hpp" |
| 28 | #endif |
| 29 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 30 | #define STRINGIFY(x) #x |
| 31 | |
| 32 | // include the built-in shader symbols as static strings |
| 33 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 34 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | #include "sksl.include" |
| 36 | ; |
| 37 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #include "sksl_vert.include" |
| 40 | ; |
| 41 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 42 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | #include "sksl_frag.include" |
| 44 | ; |
| 45 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 46 | static const char* SKSL_GEOM_INCLUDE = |
| 47 | #include "sksl_geom.include" |
| 48 | ; |
| 49 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | namespace SkSL { |
| 51 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 52 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | : fErrorCount(0) { |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 54 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this)); |
| 55 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 56 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 58 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 59 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 60 | ADD_TYPE(Void); |
| 61 | ADD_TYPE(Float); |
| 62 | ADD_TYPE(Vec2); |
| 63 | ADD_TYPE(Vec3); |
| 64 | ADD_TYPE(Vec4); |
| 65 | ADD_TYPE(Double); |
| 66 | ADD_TYPE(DVec2); |
| 67 | ADD_TYPE(DVec3); |
| 68 | ADD_TYPE(DVec4); |
| 69 | ADD_TYPE(Int); |
| 70 | ADD_TYPE(IVec2); |
| 71 | ADD_TYPE(IVec3); |
| 72 | ADD_TYPE(IVec4); |
| 73 | ADD_TYPE(UInt); |
| 74 | ADD_TYPE(UVec2); |
| 75 | ADD_TYPE(UVec3); |
| 76 | ADD_TYPE(UVec4); |
| 77 | ADD_TYPE(Bool); |
| 78 | ADD_TYPE(BVec2); |
| 79 | ADD_TYPE(BVec3); |
| 80 | ADD_TYPE(BVec4); |
| 81 | ADD_TYPE(Mat2x2); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 82 | types->addWithoutOwnership(String("mat2x2"), fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | ADD_TYPE(Mat2x3); |
| 84 | ADD_TYPE(Mat2x4); |
| 85 | ADD_TYPE(Mat3x2); |
| 86 | ADD_TYPE(Mat3x3); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 87 | types->addWithoutOwnership(String("mat3x3"), fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | ADD_TYPE(Mat3x4); |
| 89 | ADD_TYPE(Mat4x2); |
| 90 | ADD_TYPE(Mat4x3); |
| 91 | ADD_TYPE(Mat4x4); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 92 | types->addWithoutOwnership(String("mat4x4"), fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | ADD_TYPE(GenType); |
| 94 | ADD_TYPE(GenDType); |
| 95 | ADD_TYPE(GenIType); |
| 96 | ADD_TYPE(GenUType); |
| 97 | ADD_TYPE(GenBType); |
| 98 | ADD_TYPE(Mat); |
| 99 | ADD_TYPE(Vec); |
| 100 | ADD_TYPE(GVec); |
| 101 | ADD_TYPE(GVec2); |
| 102 | ADD_TYPE(GVec3); |
| 103 | ADD_TYPE(GVec4); |
| 104 | ADD_TYPE(DVec); |
| 105 | ADD_TYPE(IVec); |
| 106 | ADD_TYPE(UVec); |
| 107 | ADD_TYPE(BVec); |
| 108 | |
| 109 | ADD_TYPE(Sampler1D); |
| 110 | ADD_TYPE(Sampler2D); |
| 111 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 112 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | ADD_TYPE(SamplerCube); |
| 114 | ADD_TYPE(Sampler2DRect); |
| 115 | ADD_TYPE(Sampler1DArray); |
| 116 | ADD_TYPE(Sampler2DArray); |
| 117 | ADD_TYPE(SamplerCubeArray); |
| 118 | ADD_TYPE(SamplerBuffer); |
| 119 | ADD_TYPE(Sampler2DMS); |
| 120 | ADD_TYPE(Sampler2DMSArray); |
| 121 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 122 | ADD_TYPE(ISampler2D); |
| 123 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 124 | ADD_TYPE(Image2D); |
| 125 | ADD_TYPE(IImage2D); |
| 126 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 127 | ADD_TYPE(SubpassInput); |
| 128 | ADD_TYPE(SubpassInputMS); |
| 129 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | ADD_TYPE(GSampler1D); |
| 131 | ADD_TYPE(GSampler2D); |
| 132 | ADD_TYPE(GSampler3D); |
| 133 | ADD_TYPE(GSamplerCube); |
| 134 | ADD_TYPE(GSampler2DRect); |
| 135 | ADD_TYPE(GSampler1DArray); |
| 136 | ADD_TYPE(GSampler2DArray); |
| 137 | ADD_TYPE(GSamplerCubeArray); |
| 138 | ADD_TYPE(GSamplerBuffer); |
| 139 | ADD_TYPE(GSampler2DMS); |
| 140 | ADD_TYPE(GSampler2DMSArray); |
| 141 | |
| 142 | ADD_TYPE(Sampler1DShadow); |
| 143 | ADD_TYPE(Sampler2DShadow); |
| 144 | ADD_TYPE(SamplerCubeShadow); |
| 145 | ADD_TYPE(Sampler2DRectShadow); |
| 146 | ADD_TYPE(Sampler1DArrayShadow); |
| 147 | ADD_TYPE(Sampler2DArrayShadow); |
| 148 | ADD_TYPE(SamplerCubeArrayShadow); |
| 149 | ADD_TYPE(GSampler2DArrayShadow); |
| 150 | ADD_TYPE(GSamplerCubeArrayShadow); |
| 151 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 152 | String skCapsName("sk_Caps"); |
| 153 | Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName, |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 154 | *fContext.fSkCaps_Type, Variable::kGlobal_Storage); |
| 155 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 156 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 157 | Modifiers::Flag ignored1; |
| 158 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 159 | this->internalConvertProgram(String(SKSL_INCLUDE), &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 160 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | ASSERT(!fErrorCount); |
| 162 | } |
| 163 | |
| 164 | Compiler::~Compiler() { |
| 165 | delete fIRGenerator; |
| 166 | } |
| 167 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 168 | // add the definition created by assigning to the lvalue to the definition set |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 169 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 170 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 171 | switch (lvalue->fKind) { |
| 172 | case Expression::kVariableReference_Kind: { |
| 173 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 174 | if (var.fStorage == Variable::kLocal_Storage) { |
| 175 | (*definitions)[&var] = expr; |
| 176 | } |
| 177 | break; |
| 178 | } |
| 179 | case Expression::kSwizzle_Kind: |
| 180 | // We consider the variable written to as long as at least some of its components have |
| 181 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 182 | // write to foo.x and then read foo.y), but being stricter could lead to false positives |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 183 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 184 | // but since we pass foo as a whole it is flagged as an error) unless we perform a much |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 185 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 186 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 187 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 188 | definitions); |
| 189 | break; |
| 190 | case Expression::kIndex_Kind: |
| 191 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 192 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 193 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 194 | definitions); |
| 195 | break; |
| 196 | case Expression::kFieldAccess_Kind: |
| 197 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 198 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 199 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 200 | definitions); |
| 201 | break; |
| 202 | default: |
| 203 | // not an lvalue, can't happen |
| 204 | ASSERT(false); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 209 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 210 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 211 | switch (node.fKind) { |
| 212 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 213 | ASSERT(node.expression()); |
| 214 | const Expression* expr = (Expression*) node.expression()->get(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 215 | switch (expr->fKind) { |
| 216 | case Expression::kBinary_Kind: { |
| 217 | BinaryExpression* b = (BinaryExpression*) expr; |
| 218 | if (b->fOperator == Token::EQ) { |
| 219 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
| 220 | } else if (Token::IsAssignment(b->fOperator)) { |
| 221 | this->addDefinition( |
| 222 | b->fLeft.get(), |
| 223 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 224 | definitions); |
| 225 | |
| 226 | } |
| 227 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 228 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 229 | case Expression::kPrefix_Kind: { |
| 230 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 231 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 232 | this->addDefinition( |
| 233 | p->fOperand.get(), |
| 234 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 235 | definitions); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | case Expression::kPostfix_Kind: { |
| 240 | const PostfixExpression* p = (PostfixExpression*) expr; |
| 241 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 242 | this->addDefinition( |
| 243 | p->fOperand.get(), |
| 244 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 245 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 249 | case Expression::kVariableReference_Kind: { |
| 250 | const VariableReference* v = (VariableReference*) expr; |
| 251 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 252 | this->addDefinition( |
| 253 | v, |
| 254 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 255 | definitions); |
| 256 | } |
| 257 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 258 | default: |
| 259 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 260 | } |
| 261 | break; |
| 262 | } |
| 263 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 264 | const Statement* stmt = (Statement*) node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 265 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 266 | VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 267 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 268 | if (decl->fValue) { |
| 269 | (*definitions)[decl->fVar] = &decl->fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 279 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 280 | |
| 281 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 282 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 283 | for (const BasicBlock::Node& n : block.fNodes) { |
| 284 | this->addDefinitions(n, &after); |
| 285 | } |
| 286 | |
| 287 | // propagate definitions to exits |
| 288 | for (BlockId exitId : block.fExits) { |
| 289 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 290 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 291 | std::unique_ptr<Expression>* e1 = pair.second; |
| 292 | auto found = exit.fBefore.find(pair.first); |
| 293 | if (found == exit.fBefore.end()) { |
| 294 | // exit has no definition for it, just copy it |
| 295 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 296 | exit.fBefore[pair.first] = e1; |
| 297 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 298 | // exit has a (possibly different) value already defined |
| 299 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 300 | if (e1 != e2) { |
| 301 | // definition has changed, merge and add exit block to worklist |
| 302 | workList->insert(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 303 | if (e1 && e2) { |
| 304 | exit.fBefore[pair.first] = |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 305 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 306 | } else { |
| 307 | exit.fBefore[pair.first] = nullptr; |
| 308 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 316 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 317 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 318 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 319 | for (const auto& block : cfg.fBlocks) { |
| 320 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 321 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 322 | ASSERT(node.statement()); |
| 323 | const Statement* s = node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 324 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 325 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 326 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 327 | result[decl->fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 328 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return result; |
| 334 | } |
| 335 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 336 | /** |
| 337 | * Returns true if assigning to this lvalue has no effect. |
| 338 | */ |
| 339 | static bool is_dead(const Expression& lvalue) { |
| 340 | switch (lvalue.fKind) { |
| 341 | case Expression::kVariableReference_Kind: |
| 342 | return ((VariableReference&) lvalue).fVariable.dead(); |
| 343 | case Expression::kSwizzle_Kind: |
| 344 | return is_dead(*((Swizzle&) lvalue).fBase); |
| 345 | case Expression::kFieldAccess_Kind: |
| 346 | return is_dead(*((FieldAccess&) lvalue).fBase); |
| 347 | case Expression::kIndex_Kind: { |
| 348 | const IndexExpression& idx = (IndexExpression&) lvalue; |
| 349 | return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects(); |
| 350 | } |
| 351 | default: |
| 352 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
| 353 | } |
| 354 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 355 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 356 | /** |
| 357 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 358 | * to a dead target and lack of side effects on the left hand side. |
| 359 | */ |
| 360 | static bool dead_assignment(const BinaryExpression& b) { |
| 361 | if (!Token::IsAssignment(b.fOperator)) { |
| 362 | return false; |
| 363 | } |
| 364 | return is_dead(*b.fLeft); |
| 365 | } |
| 366 | |
| 367 | void Compiler::computeDataFlow(CFG* cfg) { |
| 368 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 369 | std::set<BlockId> workList; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 370 | for (BlockId i = 0; i < cfg->fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 371 | workList.insert(i); |
| 372 | } |
| 373 | while (workList.size()) { |
| 374 | BlockId next = *workList.begin(); |
| 375 | workList.erase(workList.begin()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 376 | this->scanCFG(cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 377 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 382 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 383 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 384 | * need to be regenerated). |
| 385 | */ |
| 386 | bool try_replace_expression(BasicBlock* b, |
| 387 | std::vector<BasicBlock::Node>::iterator* iter, |
| 388 | std::unique_ptr<Expression>* newExpression) { |
| 389 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 390 | if (!b->tryRemoveExpression(iter)) { |
| 391 | *target = std::move(*newExpression); |
| 392 | return false; |
| 393 | } |
| 394 | *target = std::move(*newExpression); |
| 395 | return b->tryInsertExpression(iter, target); |
| 396 | } |
| 397 | |
| 398 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 399 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 400 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 401 | */ |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 402 | bool is_constant(const Expression& expr, double value) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 403 | switch (expr.fKind) { |
| 404 | case Expression::kIntLiteral_Kind: |
| 405 | return ((IntLiteral&) expr).fValue == value; |
| 406 | case Expression::kFloatLiteral_Kind: |
| 407 | return ((FloatLiteral&) expr).fValue == value; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 408 | case Expression::kConstructor_Kind: { |
| 409 | Constructor& c = (Constructor&) expr; |
| 410 | if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) { |
| 411 | for (int i = 0; i < c.fType.columns(); ++i) { |
| 412 | if (!is_constant(c.getVecComponent(i), value)) { |
| 413 | return false; |
| 414 | } |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 420 | default: |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 427 | * and CFG structures). |
| 428 | */ |
| 429 | void delete_left(BasicBlock* b, |
| 430 | std::vector<BasicBlock::Node>::iterator* iter, |
| 431 | bool* outUpdated, |
| 432 | bool* outNeedsRescan) { |
| 433 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 434 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 435 | ASSERT((*target)->fKind == Expression::kBinary_Kind); |
| 436 | BinaryExpression& bin = (BinaryExpression&) **target; |
| 437 | bool result; |
| 438 | if (bin.fOperator == Token::EQ) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 439 | result = b->tryRemoveLValueBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 440 | } else { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 441 | result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 442 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 443 | *target = std::move(bin.fRight); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 444 | if (!result) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 445 | *outNeedsRescan = true; |
| 446 | return; |
| 447 | } |
| 448 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 449 | *outNeedsRescan = true; |
| 450 | return; |
| 451 | } |
| 452 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 453 | if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 454 | (*iter)->expression() != &bin.fRight) { |
| 455 | *outNeedsRescan = true; |
| 456 | return; |
| 457 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 458 | *iter = b->fNodes.erase(*iter); |
| 459 | ASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 464 | * CFG structures). |
| 465 | */ |
| 466 | void delete_right(BasicBlock* b, |
| 467 | std::vector<BasicBlock::Node>::iterator* iter, |
| 468 | bool* outUpdated, |
| 469 | bool* outNeedsRescan) { |
| 470 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 471 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 472 | ASSERT((*target)->fKind == Expression::kBinary_Kind); |
| 473 | BinaryExpression& bin = (BinaryExpression&) **target; |
| 474 | if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) { |
| 475 | *target = std::move(bin.fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 476 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 477 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 478 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 479 | *target = std::move(bin.fLeft); |
| 480 | if (*iter == b->fNodes.begin()) { |
| 481 | *outNeedsRescan = true; |
| 482 | return; |
| 483 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 484 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 485 | if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 486 | (*iter)->expression() != &bin.fLeft)) { |
| 487 | *outNeedsRescan = true; |
| 488 | return; |
| 489 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 490 | *iter = b->fNodes.erase(*iter); |
| 491 | ASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 494 | /** |
| 495 | * Constructs the specified type using a single argument. |
| 496 | */ |
| 497 | static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) { |
| 498 | std::vector<std::unique_ptr<Expression>> args; |
| 499 | args.push_back(std::move(v)); |
| 500 | auto result = std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 506 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 507 | */ |
| 508 | static void vectorize(BasicBlock* b, |
| 509 | std::vector<BasicBlock::Node>::iterator* iter, |
| 510 | const Type& type, |
| 511 | std::unique_ptr<Expression>* otherExpression, |
| 512 | bool* outUpdated, |
| 513 | bool* outNeedsRescan) { |
| 514 | ASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind); |
| 515 | ASSERT(type.kind() == Type::kVector_Kind); |
| 516 | ASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind); |
| 517 | *outUpdated = true; |
| 518 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 519 | if (!b->tryRemoveExpression(iter)) { |
| 520 | *target = construct(type, std::move(*otherExpression)); |
| 521 | *outNeedsRescan = true; |
| 522 | } else { |
| 523 | *target = construct(type, std::move(*otherExpression)); |
| 524 | if (!b->tryInsertExpression(iter, target)) { |
| 525 | *outNeedsRescan = true; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 532 | * left to yield vec<n>(x). |
| 533 | */ |
| 534 | static void vectorize_left(BasicBlock* b, |
| 535 | std::vector<BasicBlock::Node>::iterator* iter, |
| 536 | bool* outUpdated, |
| 537 | bool* outNeedsRescan) { |
| 538 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 539 | vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 544 | * right to yield vec<n>(y). |
| 545 | */ |
| 546 | static void vectorize_right(BasicBlock* b, |
| 547 | std::vector<BasicBlock::Node>::iterator* iter, |
| 548 | bool* outUpdated, |
| 549 | bool* outNeedsRescan) { |
| 550 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 551 | vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan); |
| 552 | } |
| 553 | |
| 554 | // Mark that an expression which we were writing to is no longer being written to |
| 555 | void clear_write(const Expression& expr) { |
| 556 | switch (expr.fKind) { |
| 557 | case Expression::kVariableReference_Kind: { |
| 558 | ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind); |
| 559 | break; |
| 560 | } |
| 561 | case Expression::kFieldAccess_Kind: |
| 562 | clear_write(*((FieldAccess&) expr).fBase); |
| 563 | break; |
| 564 | case Expression::kSwizzle_Kind: |
| 565 | clear_write(*((Swizzle&) expr).fBase); |
| 566 | break; |
| 567 | case Expression::kIndex_Kind: |
| 568 | clear_write(*((IndexExpression&) expr).fBase); |
| 569 | break; |
| 570 | default: |
| 571 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 576 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 577 | BasicBlock& b, |
| 578 | std::vector<BasicBlock::Node>::iterator* iter, |
| 579 | std::unordered_set<const Variable*>* undefinedVariables, |
| 580 | bool* outUpdated, |
| 581 | bool* outNeedsRescan) { |
| 582 | Expression* expr = (*iter)->expression()->get(); |
| 583 | ASSERT(expr); |
| 584 | if ((*iter)->fConstantPropagation) { |
| 585 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 586 | if (optimized) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 587 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 588 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 589 | *outNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 590 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 591 | } |
| 592 | ASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind); |
| 593 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | switch (expr->fKind) { |
| 597 | case Expression::kVariableReference_Kind: { |
| 598 | const Variable& var = ((VariableReference*) expr)->fVariable; |
| 599 | if (var.fStorage == Variable::kLocal_Storage && !definitions[&var] && |
| 600 | (*undefinedVariables).find(&var) == (*undefinedVariables).end()) { |
| 601 | (*undefinedVariables).insert(&var); |
| 602 | this->error(expr->fPosition, |
| 603 | "'" + var.fName + "' has not been assigned"); |
| 604 | } |
| 605 | break; |
| 606 | } |
| 607 | case Expression::kTernary_Kind: { |
| 608 | TernaryExpression* t = (TernaryExpression*) expr; |
| 609 | if (t->fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 610 | // ternary has a constant test, replace it with either the true or |
| 611 | // false branch |
| 612 | if (((BoolLiteral&) *t->fTest).fValue) { |
| 613 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 614 | } else { |
| 615 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 616 | } |
| 617 | *outUpdated = true; |
| 618 | *outNeedsRescan = true; |
| 619 | } |
| 620 | break; |
| 621 | } |
| 622 | case Expression::kBinary_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 623 | BinaryExpression* bin = (BinaryExpression*) expr; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 624 | if (dead_assignment(*bin)) { |
| 625 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 626 | break; |
| 627 | } |
| 628 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 629 | if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) && |
| 630 | (bin->fLeft->fType.kind() != Type::kVector_Kind)) || |
| 631 | ((bin->fRight->fType.kind() != Type::kScalar_Kind) && |
| 632 | (bin->fRight->fType.kind() != Type::kVector_Kind))) { |
| 633 | break; |
| 634 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 635 | switch (bin->fOperator) { |
| 636 | case Token::STAR: |
| 637 | if (is_constant(*bin->fLeft, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 638 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 639 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 640 | // vec4(1) * x -> vec4(x) |
| 641 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 642 | } else { |
| 643 | // 1 * x -> x |
| 644 | // 1 * vec4(x) -> vec4(x) |
| 645 | // vec4(1) * vec4(x) -> vec4(x) |
| 646 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 647 | } |
| 648 | } |
| 649 | else if (is_constant(*bin->fLeft, 0)) { |
| 650 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 651 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 652 | // 0 * vec4(x) -> vec4(0) |
| 653 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 654 | } else { |
| 655 | // 0 * x -> 0 |
| 656 | // vec4(0) * x -> vec4(0) |
| 657 | // vec4(0) * vec4(x) -> vec4(0) |
| 658 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 659 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 660 | } |
| 661 | else if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 662 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 663 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 664 | // x * vec4(1) -> vec4(x) |
| 665 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 666 | } else { |
| 667 | // x * 1 -> x |
| 668 | // vec4(x) * 1 -> vec4(x) |
| 669 | // vec4(x) * vec4(1) -> vec4(x) |
| 670 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 671 | } |
| 672 | } |
| 673 | else if (is_constant(*bin->fRight, 0)) { |
| 674 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 675 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 676 | // vec4(x) * 0 -> vec4(0) |
| 677 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 678 | } else { |
| 679 | // x * 0 -> 0 |
| 680 | // x * vec4(0) -> vec4(0) |
| 681 | // vec4(x) * vec4(0) -> vec4(0) |
| 682 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 683 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 684 | } |
| 685 | break; |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 686 | case Token::PLUS: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 687 | if (is_constant(*bin->fLeft, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 688 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 689 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
| 690 | // vec4(0) + x -> vec4(x) |
| 691 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 692 | } else { |
| 693 | // 0 + x -> x |
| 694 | // 0 + vec4(x) -> vec4(x) |
| 695 | // vec4(0) + vec4(x) -> vec4(x) |
| 696 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 697 | } |
| 698 | } else if (is_constant(*bin->fRight, 0)) { |
| 699 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 700 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 701 | // x + vec4(0) -> vec4(x) |
| 702 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 703 | } else { |
| 704 | // x + 0 -> x |
| 705 | // vec4(x) + 0 -> vec4(x) |
| 706 | // vec4(x) + vec4(0) -> vec4(x) |
| 707 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 708 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 709 | } |
| 710 | break; |
| 711 | case Token::MINUS: |
| 712 | if (is_constant(*bin->fRight, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 713 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 714 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 715 | // x - vec4(0) -> vec4(x) |
| 716 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 717 | } else { |
| 718 | // x - 0 -> x |
| 719 | // vec4(x) - 0 -> vec4(x) |
| 720 | // vec4(x) - vec4(0) -> vec4(x) |
| 721 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 722 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 723 | } |
| 724 | break; |
| 725 | case Token::SLASH: |
| 726 | if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 727 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 728 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 729 | // x / vec4(1) -> vec4(x) |
| 730 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 731 | } else { |
| 732 | // x / 1 -> x |
| 733 | // vec4(x) / 1 -> vec4(x) |
| 734 | // vec4(x) / vec4(1) -> vec4(x) |
| 735 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 736 | } |
| 737 | } else if (is_constant(*bin->fLeft, 0)) { |
| 738 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 739 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
| 740 | // 0 / vec4(x) -> vec4(0) |
| 741 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 742 | } else { |
| 743 | // 0 / x -> 0 |
| 744 | // vec4(0) / x -> vec4(0) |
| 745 | // vec4(0) / vec4(x) -> vec4(0) |
| 746 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 747 | } |
| 748 | } |
| 749 | break; |
| 750 | case Token::PLUSEQ: |
| 751 | if (is_constant(*bin->fRight, 0)) { |
| 752 | clear_write(*bin->fLeft); |
| 753 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 754 | } |
| 755 | break; |
| 756 | case Token::MINUSEQ: |
| 757 | if (is_constant(*bin->fRight, 0)) { |
| 758 | clear_write(*bin->fLeft); |
| 759 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 760 | } |
| 761 | break; |
| 762 | case Token::STAREQ: |
| 763 | if (is_constant(*bin->fRight, 1)) { |
| 764 | clear_write(*bin->fLeft); |
| 765 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 766 | } |
| 767 | break; |
| 768 | case Token::SLASHEQ: |
| 769 | if (is_constant(*bin->fRight, 1)) { |
| 770 | clear_write(*bin->fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 771 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 772 | } |
| 773 | break; |
| 774 | default: |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | default: |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 783 | |
| 784 | // returns true if this statement could potentially execute a break at the current level (we ignore |
| 785 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
| 786 | static bool contains_break(Statement& s) { |
| 787 | switch (s.fKind) { |
| 788 | case Statement::kBlock_Kind: |
| 789 | for (const auto& sub : ((Block&) s).fStatements) { |
| 790 | if (contains_break(*sub)) { |
| 791 | return true; |
| 792 | } |
| 793 | } |
| 794 | return false; |
| 795 | case Statement::kBreak_Kind: |
| 796 | return true; |
| 797 | case Statement::kIf_Kind: { |
| 798 | const IfStatement& i = (IfStatement&) s; |
| 799 | return contains_break(*i.fIfTrue) || (i.fIfFalse && contains_break(*i.fIfFalse)); |
| 800 | } |
| 801 | default: |
| 802 | return false; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | // Returns a block containing all of the statements that will be run if the given case matches |
| 807 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 808 | // broken by this call and must then be discarded). |
| 809 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 810 | // when break statements appear inside conditionals. |
| 811 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) { |
| 812 | bool capturing = false; |
| 813 | std::vector<std::unique_ptr<Statement>*> statementPtrs; |
| 814 | for (const auto& current : s->fCases) { |
| 815 | if (current.get() == c) { |
| 816 | capturing = true; |
| 817 | } |
| 818 | if (capturing) { |
| 819 | for (auto& stmt : current->fStatements) { |
| 820 | if (stmt->fKind == Statement::kBreak_Kind) { |
| 821 | capturing = false; |
| 822 | break; |
| 823 | } |
| 824 | if (contains_break(*stmt)) { |
| 825 | return nullptr; |
| 826 | } |
| 827 | statementPtrs.push_back(&stmt); |
| 828 | } |
| 829 | if (!capturing) { |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | std::vector<std::unique_ptr<Statement>> statements; |
| 835 | for (const auto& s : statementPtrs) { |
| 836 | statements.push_back(std::move(*s)); |
| 837 | } |
| 838 | return std::unique_ptr<Statement>(new Block(Position(), std::move(statements))); |
| 839 | } |
| 840 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 841 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 842 | BasicBlock& b, |
| 843 | std::vector<BasicBlock::Node>::iterator* iter, |
| 844 | std::unordered_set<const Variable*>* undefinedVariables, |
| 845 | bool* outUpdated, |
| 846 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 847 | Statement* stmt = (*iter)->statement()->get(); |
| 848 | switch (stmt->fKind) { |
| 849 | case Statement::kVarDeclarations_Kind: { |
| 850 | VarDeclarations& vd = *((VarDeclarationsStatement&) *stmt).fDeclaration; |
| 851 | for (auto varIter = vd.fVars.begin(); varIter != vd.fVars.end(); ) { |
| 852 | const auto& varDecl = **varIter; |
| 853 | if (varDecl.fVar->dead() && |
| 854 | (!varDecl.fValue || |
| 855 | !varDecl.fValue->hasSideEffects())) { |
| 856 | if (varDecl.fValue) { |
| 857 | ASSERT((*iter)->statement()->get() == stmt); |
| 858 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 859 | *outNeedsRescan = true; |
| 860 | } |
| 861 | } |
| 862 | varIter = vd.fVars.erase(varIter); |
| 863 | *outUpdated = true; |
| 864 | } else { |
| 865 | ++varIter; |
| 866 | } |
| 867 | } |
| 868 | if (vd.fVars.size() == 0) { |
| 869 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 870 | } |
| 871 | break; |
| 872 | } |
| 873 | case Statement::kIf_Kind: { |
| 874 | IfStatement& i = (IfStatement&) *stmt; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 875 | if (i.fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 876 | // constant if, collapse down to a single branch |
| 877 | if (((BoolLiteral&) *i.fTest).fValue) { |
| 878 | ASSERT(i.fIfTrue); |
| 879 | (*iter)->setStatement(std::move(i.fIfTrue)); |
| 880 | } else { |
| 881 | if (i.fIfFalse) { |
| 882 | (*iter)->setStatement(std::move(i.fIfFalse)); |
| 883 | } else { |
| 884 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 885 | } |
| 886 | } |
| 887 | *outUpdated = true; |
| 888 | *outNeedsRescan = true; |
| 889 | break; |
| 890 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 891 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 892 | // else block doesn't do anything, remove it |
| 893 | i.fIfFalse.reset(); |
| 894 | *outUpdated = true; |
| 895 | *outNeedsRescan = true; |
| 896 | } |
| 897 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 898 | // if block doesn't do anything, no else block |
| 899 | if (i.fTest->hasSideEffects()) { |
| 900 | // test has side effects, keep it |
| 901 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 902 | new ExpressionStatement(std::move(i.fTest)))); |
| 903 | } else { |
| 904 | // no if, no else, no test side effects, kill the whole if |
| 905 | // statement |
| 906 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 907 | } |
| 908 | *outUpdated = true; |
| 909 | *outNeedsRescan = true; |
| 910 | } |
| 911 | break; |
| 912 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 913 | case Statement::kSwitch_Kind: { |
| 914 | SwitchStatement& s = (SwitchStatement&) *stmt; |
| 915 | if (s.fValue->isConstant()) { |
| 916 | // switch is constant, replace it with the case that matches |
| 917 | bool found = false; |
| 918 | SwitchCase* defaultCase = nullptr; |
| 919 | for (const auto& c : s.fCases) { |
| 920 | if (!c->fValue) { |
| 921 | defaultCase = c.get(); |
| 922 | continue; |
| 923 | } |
| 924 | ASSERT(c->fValue->fKind == s.fValue->fKind); |
| 925 | found = c->fValue->compareConstant(fContext, *s.fValue); |
| 926 | if (found) { |
| 927 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
| 928 | if (newBlock) { |
| 929 | (*iter)->setStatement(std::move(newBlock)); |
| 930 | break; |
| 931 | } else { |
| 932 | if (s.fIsStatic) { |
| 933 | this->error(s.fPosition, |
| 934 | "static switch contains non-static conditional break"); |
| 935 | s.fIsStatic = false; |
| 936 | } |
| 937 | return; // can't simplify |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | if (!found) { |
| 942 | // no matching case. use default if it exists, or kill the whole thing |
| 943 | if (defaultCase) { |
| 944 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 945 | if (newBlock) { |
| 946 | (*iter)->setStatement(std::move(newBlock)); |
| 947 | } else { |
| 948 | if (s.fIsStatic) { |
| 949 | this->error(s.fPosition, |
| 950 | "static switch contains non-static conditional break"); |
| 951 | s.fIsStatic = false; |
| 952 | } |
| 953 | return; // can't simplify |
| 954 | } |
| 955 | } else { |
| 956 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 957 | } |
| 958 | } |
| 959 | *outUpdated = true; |
| 960 | *outNeedsRescan = true; |
| 961 | } |
| 962 | break; |
| 963 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 964 | case Statement::kExpression_Kind: { |
| 965 | ExpressionStatement& e = (ExpressionStatement&) *stmt; |
| 966 | ASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 967 | if (!e.fExpression->hasSideEffects()) { |
| 968 | // Expression statement with no side effects, kill it |
| 969 | if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) { |
| 970 | *outNeedsRescan = true; |
| 971 | } |
| 972 | ASSERT((*iter)->statement()->get() == stmt); |
| 973 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 974 | *outUpdated = true; |
| 975 | } |
| 976 | break; |
| 977 | } |
| 978 | default: |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | void Compiler::scanCFG(FunctionDefinition& f) { |
| 984 | CFG cfg = CFGGenerator().getCFG(f); |
| 985 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 986 | |
| 987 | // check for unreachable code |
| 988 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 989 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 990 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 991 | Position p; |
| 992 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 993 | case BasicBlock::Node::kStatement_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 994 | p = (*cfg.fBlocks[i].fNodes[0].statement())->fPosition; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 995 | break; |
| 996 | case BasicBlock::Node::kExpression_Kind: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 997 | p = (*cfg.fBlocks[i].fNodes[0].expression())->fPosition; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 998 | break; |
| 999 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1000 | this->error(p, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | if (fErrorCount) { |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1007 | // check for dead code & undefined variables, perform constant propagation |
| 1008 | std::unordered_set<const Variable*> undefinedVariables; |
| 1009 | bool updated; |
| 1010 | bool needsRescan = false; |
| 1011 | do { |
| 1012 | if (needsRescan) { |
| 1013 | cfg = CFGGenerator().getCFG(f); |
| 1014 | this->computeDataFlow(&cfg); |
| 1015 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1016 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1017 | |
| 1018 | updated = false; |
| 1019 | for (BasicBlock& b : cfg.fBlocks) { |
| 1020 | DefinitionMap definitions = b.fBefore; |
| 1021 | |
| 1022 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
| 1023 | if (iter->fKind == BasicBlock::Node::kExpression_Kind) { |
| 1024 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 1025 | &needsRescan); |
| 1026 | } else { |
| 1027 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
| 1028 | &needsRescan); |
| 1029 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame^] | 1030 | if (needsRescan) { |
| 1031 | break; |
| 1032 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1033 | this->addDefinitions(*iter, &definitions); |
| 1034 | } |
| 1035 | } |
| 1036 | } while (updated); |
| 1037 | ASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1038 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1039 | // verify static ifs & switches |
| 1040 | for (BasicBlock& b : cfg.fBlocks) { |
| 1041 | DefinitionMap definitions = b.fBefore; |
| 1042 | |
| 1043 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
| 1044 | if (iter->fKind == BasicBlock::Node::kStatement_Kind) { |
| 1045 | const Statement& s = **iter->statement(); |
| 1046 | switch (s.fKind) { |
| 1047 | case Statement::kIf_Kind: |
| 1048 | if (((const IfStatement&) s).fIsStatic) { |
| 1049 | this->error(s.fPosition, "static if has non-static test"); |
| 1050 | } |
| 1051 | break; |
| 1052 | case Statement::kSwitch_Kind: |
| 1053 | if (((const SwitchStatement&) s).fIsStatic) { |
| 1054 | this->error(s.fPosition, "static switch has non-static test"); |
| 1055 | } |
| 1056 | break; |
| 1057 | default: |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1064 | // check for missing return |
| 1065 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 1066 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1067 | this->error(f.fPosition, String("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1072 | void Compiler::internalConvertProgram(String text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1073 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1074 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 1075 | Parser parser(text, *fTypes, *this); |
| 1076 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 1077 | if (fErrorCount) { |
| 1078 | return; |
| 1079 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1080 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1081 | for (size_t i = 0; i < parsed.size(); i++) { |
| 1082 | ASTDeclaration& decl = *parsed[i]; |
| 1083 | switch (decl.fKind) { |
| 1084 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 1085 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 1086 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1087 | Variable::kGlobal_Storage); |
| 1088 | if (s) { |
| 1089 | result->push_back(std::move(s)); |
| 1090 | } |
| 1091 | break; |
| 1092 | } |
| 1093 | case ASTDeclaration::kFunction_Kind: { |
| 1094 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 1095 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1096 | if (!fErrorCount && f) { |
| 1097 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1098 | result->push_back(std::move(f)); |
| 1099 | } |
| 1100 | break; |
| 1101 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1102 | case ASTDeclaration::kModifiers_Kind: { |
| 1103 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 1104 | (ASTModifiersDeclaration&) decl); |
| 1105 | if (f) { |
| 1106 | result->push_back(std::move(f)); |
| 1107 | } |
| 1108 | break; |
| 1109 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1110 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 1111 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 1112 | (ASTInterfaceBlock&) decl); |
| 1113 | if (i) { |
| 1114 | result->push_back(std::move(i)); |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | case ASTDeclaration::kExtension_Kind: { |
| 1119 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 1120 | if (e) { |
| 1121 | result->push_back(std::move(e)); |
| 1122 | } |
| 1123 | break; |
| 1124 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1125 | case ASTDeclaration::kPrecision_Kind: { |
| 1126 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 1127 | break; |
| 1128 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1129 | default: |
| 1130 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1135 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1136 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1137 | fErrorText = ""; |
| 1138 | fErrorCount = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1139 | fIRGenerator->start(&settings); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1140 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1141 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1142 | switch (kind) { |
| 1143 | case Program::kVertex_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1144 | this->internalConvertProgram(String(SKSL_VERT_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1145 | break; |
| 1146 | case Program::kFragment_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1147 | this->internalConvertProgram(String(SKSL_FRAG_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1148 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1149 | case Program::kGeometry_Kind: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1150 | this->internalConvertProgram(String(SKSL_GEOM_INCLUDE), &ignored, &elements); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1151 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1152 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 1153 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1154 | Modifiers::Flag defaultPrecision; |
| 1155 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1156 | auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext, |
| 1157 | std::move(elements), |
| 1158 | fIRGenerator->fSymbolTable, |
| 1159 | fIRGenerator->fInputs)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1160 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1161 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1162 | if (fErrorCount) { |
| 1163 | return nullptr; |
| 1164 | } |
| 1165 | return result; |
| 1166 | } |
| 1167 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1168 | bool Compiler::toSPIRV(const Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1169 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1170 | StringStream buffer; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1171 | SPIRVCodeGenerator cg(&fContext, &program, this, &buffer); |
| 1172 | bool result = cg.generateCode(); |
| 1173 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1174 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1175 | ASSERT(0 == buffer.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1176 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1177 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1178 | }; |
| 1179 | tools.SetMessageConsumer(dumpmsg); |
| 1180 | // Verify that the SPIR-V we produced is valid. If this assert fails, check the logs prior |
| 1181 | // to the failure to see the validation errors. |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1182 | ASSERT_RESULT(tools.Validate((const uint32_t*) buffer.data(), buffer.size() / 4)); |
| 1183 | out.write(buffer.data(), buffer.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1184 | } |
| 1185 | #else |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1186 | SPIRVCodeGenerator cg(&fContext, &program, this, &out); |
| 1187 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1188 | #endif |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1189 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1190 | return result; |
| 1191 | } |
| 1192 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1193 | bool Compiler::toSPIRV(const Program& program, String* out) { |
| 1194 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1195 | bool result = this->toSPIRV(program, buffer); |
| 1196 | if (result) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1197 | *out = String(buffer.data(), buffer.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1198 | } |
| 1199 | return result; |
| 1200 | } |
| 1201 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1202 | bool Compiler::toGLSL(const Program& program, OutputStream& out) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1203 | GLSLCodeGenerator cg(&fContext, &program, this, &out); |
| 1204 | bool result = cg.generateCode(); |
| 1205 | this->writeErrorCount(); |
| 1206 | return result; |
| 1207 | } |
| 1208 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1209 | bool Compiler::toGLSL(const Program& program, String* out) { |
| 1210 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1211 | bool result = this->toGLSL(program, buffer); |
| 1212 | if (result) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1213 | *out = String(buffer.data(), buffer.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1214 | } |
| 1215 | return result; |
| 1216 | } |
| 1217 | |
| 1218 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1219 | void Compiler::error(Position position, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1220 | fErrorCount++; |
| 1221 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 1222 | } |
| 1223 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1224 | String Compiler::errorText() { |
| 1225 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1226 | return result; |
| 1227 | } |
| 1228 | |
| 1229 | void Compiler::writeErrorCount() { |
| 1230 | if (fErrorCount) { |
| 1231 | fErrorText += to_string(fErrorCount) + " error"; |
| 1232 | if (fErrorCount > 1) { |
| 1233 | fErrorText += "s"; |
| 1234 | } |
| 1235 | fErrorText += "\n"; |
| 1236 | } |
| 1237 | } |
| 1238 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1239 | } // namespace |