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" |
| 17 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 18 | #include "ir/SkSLModifiersDeclaration.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 19 | #include "ir/SkSLSymbolTable.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 20 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 21 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | #include "SkMutex.h" |
| 23 | |
| 24 | #define STRINGIFY(x) #x |
| 25 | |
| 26 | // include the built-in shader symbols as static strings |
| 27 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 28 | static const char* SKSL_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | #include "sksl.include" |
| 30 | ; |
| 31 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 32 | static const char* SKSL_VERT_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | #include "sksl_vert.include" |
| 34 | ; |
| 35 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 36 | static const char* SKSL_FRAG_INCLUDE = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | #include "sksl_frag.include" |
| 38 | ; |
| 39 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame^] | 40 | static const char* SKSL_GEOM_INCLUDE = |
| 41 | #include "sksl_geom.include" |
| 42 | ; |
| 43 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | namespace SkSL { |
| 45 | |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 46 | Compiler::Compiler() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | : fErrorCount(0) { |
| 48 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 49 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 50 | fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 51 | fTypes = types; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 52 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fName, \ |
| 53 | fContext.f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | ADD_TYPE(Void); |
| 55 | ADD_TYPE(Float); |
| 56 | ADD_TYPE(Vec2); |
| 57 | ADD_TYPE(Vec3); |
| 58 | ADD_TYPE(Vec4); |
| 59 | ADD_TYPE(Double); |
| 60 | ADD_TYPE(DVec2); |
| 61 | ADD_TYPE(DVec3); |
| 62 | ADD_TYPE(DVec4); |
| 63 | ADD_TYPE(Int); |
| 64 | ADD_TYPE(IVec2); |
| 65 | ADD_TYPE(IVec3); |
| 66 | ADD_TYPE(IVec4); |
| 67 | ADD_TYPE(UInt); |
| 68 | ADD_TYPE(UVec2); |
| 69 | ADD_TYPE(UVec3); |
| 70 | ADD_TYPE(UVec4); |
| 71 | ADD_TYPE(Bool); |
| 72 | ADD_TYPE(BVec2); |
| 73 | ADD_TYPE(BVec3); |
| 74 | ADD_TYPE(BVec4); |
| 75 | ADD_TYPE(Mat2x2); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 76 | types->addWithoutOwnership(SkString("mat2x2"), fContext.fMat2x2_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 77 | ADD_TYPE(Mat2x3); |
| 78 | ADD_TYPE(Mat2x4); |
| 79 | ADD_TYPE(Mat3x2); |
| 80 | ADD_TYPE(Mat3x3); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 81 | types->addWithoutOwnership(SkString("mat3x3"), fContext.fMat3x3_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 82 | ADD_TYPE(Mat3x4); |
| 83 | ADD_TYPE(Mat4x2); |
| 84 | ADD_TYPE(Mat4x3); |
| 85 | ADD_TYPE(Mat4x4); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 86 | types->addWithoutOwnership(SkString("mat4x4"), fContext.fMat4x4_Type.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | ADD_TYPE(GenType); |
| 88 | ADD_TYPE(GenDType); |
| 89 | ADD_TYPE(GenIType); |
| 90 | ADD_TYPE(GenUType); |
| 91 | ADD_TYPE(GenBType); |
| 92 | ADD_TYPE(Mat); |
| 93 | ADD_TYPE(Vec); |
| 94 | ADD_TYPE(GVec); |
| 95 | ADD_TYPE(GVec2); |
| 96 | ADD_TYPE(GVec3); |
| 97 | ADD_TYPE(GVec4); |
| 98 | ADD_TYPE(DVec); |
| 99 | ADD_TYPE(IVec); |
| 100 | ADD_TYPE(UVec); |
| 101 | ADD_TYPE(BVec); |
| 102 | |
| 103 | ADD_TYPE(Sampler1D); |
| 104 | ADD_TYPE(Sampler2D); |
| 105 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 106 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 107 | ADD_TYPE(SamplerCube); |
| 108 | ADD_TYPE(Sampler2DRect); |
| 109 | ADD_TYPE(Sampler1DArray); |
| 110 | ADD_TYPE(Sampler2DArray); |
| 111 | ADD_TYPE(SamplerCubeArray); |
| 112 | ADD_TYPE(SamplerBuffer); |
| 113 | ADD_TYPE(Sampler2DMS); |
| 114 | ADD_TYPE(Sampler2DMSArray); |
| 115 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 116 | ADD_TYPE(ISampler2D); |
| 117 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 118 | ADD_TYPE(Image2D); |
| 119 | ADD_TYPE(IImage2D); |
| 120 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 121 | ADD_TYPE(SubpassInput); |
| 122 | ADD_TYPE(SubpassInputMS); |
| 123 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 124 | ADD_TYPE(GSampler1D); |
| 125 | ADD_TYPE(GSampler2D); |
| 126 | ADD_TYPE(GSampler3D); |
| 127 | ADD_TYPE(GSamplerCube); |
| 128 | ADD_TYPE(GSampler2DRect); |
| 129 | ADD_TYPE(GSampler1DArray); |
| 130 | ADD_TYPE(GSampler2DArray); |
| 131 | ADD_TYPE(GSamplerCubeArray); |
| 132 | ADD_TYPE(GSamplerBuffer); |
| 133 | ADD_TYPE(GSampler2DMS); |
| 134 | ADD_TYPE(GSampler2DMSArray); |
| 135 | |
| 136 | ADD_TYPE(Sampler1DShadow); |
| 137 | ADD_TYPE(Sampler2DShadow); |
| 138 | ADD_TYPE(SamplerCubeShadow); |
| 139 | ADD_TYPE(Sampler2DRectShadow); |
| 140 | ADD_TYPE(Sampler1DArrayShadow); |
| 141 | ADD_TYPE(Sampler2DArrayShadow); |
| 142 | ADD_TYPE(SamplerCubeArrayShadow); |
| 143 | ADD_TYPE(GSampler2DArrayShadow); |
| 144 | ADD_TYPE(GSamplerCubeArrayShadow); |
| 145 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 146 | SkString skCapsName("sk_Caps"); |
| 147 | Variable* skCaps = new Variable(Position(), Modifiers(), skCapsName, |
| 148 | *fContext.fSkCaps_Type, Variable::kGlobal_Storage); |
| 149 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 150 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 151 | Modifiers::Flag ignored1; |
| 152 | std::vector<std::unique_ptr<ProgramElement>> ignored2; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 153 | this->internalConvertProgram(SkString(SKSL_INCLUDE), &ignored1, &ignored2); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 154 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 155 | ASSERT(!fErrorCount); |
| 156 | } |
| 157 | |
| 158 | Compiler::~Compiler() { |
| 159 | delete fIRGenerator; |
| 160 | } |
| 161 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 162 | // 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] | 163 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 164 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 165 | switch (lvalue->fKind) { |
| 166 | case Expression::kVariableReference_Kind: { |
| 167 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 168 | if (var.fStorage == Variable::kLocal_Storage) { |
| 169 | (*definitions)[&var] = expr; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | case Expression::kSwizzle_Kind: |
| 174 | // We consider the variable written to as long as at least some of its components have |
| 175 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 176 | // 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] | 177 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 178 | // 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] | 179 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 180 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 181 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 182 | definitions); |
| 183 | break; |
| 184 | case Expression::kIndex_Kind: |
| 185 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 186 | this->addDefinition(((IndexExpression*) 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::kFieldAccess_Kind: |
| 191 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 192 | this->addDefinition(((FieldAccess*) 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 | default: |
| 197 | // not an lvalue, can't happen |
| 198 | ASSERT(false); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 203 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 204 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 205 | switch (node.fKind) { |
| 206 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 207 | ASSERT(node.fExpression); |
| 208 | const Expression* expr = (Expression*) node.fExpression->get(); |
| 209 | switch (expr->fKind) { |
| 210 | case Expression::kBinary_Kind: { |
| 211 | BinaryExpression* b = (BinaryExpression*) expr; |
| 212 | if (b->fOperator == Token::EQ) { |
| 213 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
| 214 | } else if (Token::IsAssignment(b->fOperator)) { |
| 215 | this->addDefinition( |
| 216 | b->fLeft.get(), |
| 217 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 218 | definitions); |
| 219 | |
| 220 | } |
| 221 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 222 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 223 | case Expression::kPrefix_Kind: { |
| 224 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 225 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 226 | this->addDefinition( |
| 227 | p->fOperand.get(), |
| 228 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 229 | definitions); |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | case Expression::kPostfix_Kind: { |
| 234 | const PostfixExpression* p = (PostfixExpression*) expr; |
| 235 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 236 | this->addDefinition( |
| 237 | p->fOperand.get(), |
| 238 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression, |
| 239 | definitions); |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 240 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 241 | } |
| 242 | break; |
| 243 | } |
| 244 | default: |
| 245 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | } |
| 249 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 250 | const Statement* stmt = (Statement*) node.fStatement; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 251 | if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 252 | VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt; |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 253 | for (VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 254 | if (decl.fValue) { |
| 255 | (*definitions)[decl.fVar] = &decl.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 265 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 266 | |
| 267 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 268 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 269 | for (const BasicBlock::Node& n : block.fNodes) { |
| 270 | this->addDefinitions(n, &after); |
| 271 | } |
| 272 | |
| 273 | // propagate definitions to exits |
| 274 | for (BlockId exitId : block.fExits) { |
| 275 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 276 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 277 | std::unique_ptr<Expression>* e1 = pair.second; |
| 278 | auto found = exit.fBefore.find(pair.first); |
| 279 | if (found == exit.fBefore.end()) { |
| 280 | // exit has no definition for it, just copy it |
| 281 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 282 | exit.fBefore[pair.first] = e1; |
| 283 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 284 | // exit has a (possibly different) value already defined |
| 285 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 286 | if (e1 != e2) { |
| 287 | // definition has changed, merge and add exit block to worklist |
| 288 | workList->insert(exitId); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 289 | exit.fBefore[pair.first] = |
| 290 | (std::unique_ptr<Expression>*) &fContext.fDefined_Expression; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 298 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 299 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 300 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 301 | for (const auto& block : cfg.fBlocks) { |
| 302 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 303 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 304 | ASSERT(node.fStatement); |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 305 | const Statement* s = node.fStatement; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 306 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 307 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 308 | for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 309 | result[decl.fVar] = nullptr; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 310 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return result; |
| 316 | } |
| 317 | |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 318 | void Compiler::scanCFG(const FunctionDefinition& f) { |
| 319 | CFG cfg = CFGGenerator().getCFG(f); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 320 | |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 321 | // compute the data flow |
| 322 | cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 323 | std::set<BlockId> workList; |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 324 | for (BlockId i = 0; i < cfg.fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 325 | workList.insert(i); |
| 326 | } |
| 327 | while (workList.size()) { |
| 328 | BlockId next = *workList.begin(); |
| 329 | workList.erase(workList.begin()); |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 330 | this->scanCFG(&cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // check for unreachable code |
| 334 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 335 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 336 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 337 | Position p; |
| 338 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 339 | case BasicBlock::Node::kStatement_Kind: |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 340 | p = cfg.fBlocks[i].fNodes[0].fStatement->fPosition; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 341 | break; |
| 342 | case BasicBlock::Node::kExpression_Kind: |
| 343 | p = (*cfg.fBlocks[i].fNodes[0].fExpression)->fPosition; |
| 344 | break; |
| 345 | } |
| 346 | this->error(p, SkString("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | if (fErrorCount) { |
| 350 | return; |
| 351 | } |
| 352 | |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 353 | // check for undefined variables, perform constant propagation |
| 354 | for (BasicBlock& b : cfg.fBlocks) { |
| 355 | DefinitionMap definitions = b.fBefore; |
| 356 | for (BasicBlock::Node& n : b.fNodes) { |
| 357 | if (n.fKind == BasicBlock::Node::kExpression_Kind) { |
| 358 | ASSERT(n.fExpression); |
| 359 | Expression* expr = n.fExpression->get(); |
| 360 | if (n.fConstantPropagation) { |
| 361 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, |
| 362 | definitions); |
| 363 | if (optimized) { |
| 364 | n.fExpression->reset(optimized.release()); |
| 365 | expr = n.fExpression->get(); |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 366 | } |
| 367 | } |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 368 | if (expr->fKind == Expression::kVariableReference_Kind) { |
| 369 | const Variable& var = ((VariableReference*) expr)->fVariable; |
| 370 | if (var.fStorage == Variable::kLocal_Storage && |
| 371 | !definitions[&var]) { |
| 372 | this->error(expr->fPosition, |
| 373 | "'" + var.fName + "' has not been assigned"); |
| 374 | } |
| 375 | } |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 376 | } |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 377 | this->addDefinitions(n, &definitions); |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 378 | } |
Ethan Nicholas | e1d9cb8 | 2017-02-06 18:53:07 +0000 | [diff] [blame] | 379 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 380 | |
| 381 | // check for missing return |
| 382 | if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 383 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 384 | this->error(f.fPosition, SkString("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 389 | void Compiler::internalConvertProgram(SkString text, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 390 | Modifiers::Flag* defaultPrecision, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 391 | std::vector<std::unique_ptr<ProgramElement>>* result) { |
| 392 | Parser parser(text, *fTypes, *this); |
| 393 | std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 394 | if (fErrorCount) { |
| 395 | return; |
| 396 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 397 | *defaultPrecision = Modifiers::kHighp_Flag; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 398 | for (size_t i = 0; i < parsed.size(); i++) { |
| 399 | ASTDeclaration& decl = *parsed[i]; |
| 400 | switch (decl.fKind) { |
| 401 | case ASTDeclaration::kVar_Kind: { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 402 | std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDeclarations( |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 403 | (ASTVarDeclarations&) decl, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 404 | Variable::kGlobal_Storage); |
| 405 | if (s) { |
| 406 | result->push_back(std::move(s)); |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | case ASTDeclaration::kFunction_Kind: { |
| 411 | std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFunction( |
| 412 | (ASTFunction&) decl); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 413 | if (!fErrorCount && f) { |
| 414 | this->scanCFG(*f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 415 | result->push_back(std::move(f)); |
| 416 | } |
| 417 | break; |
| 418 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 419 | case ASTDeclaration::kModifiers_Kind: { |
| 420 | std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertModifiersDeclaration( |
| 421 | (ASTModifiersDeclaration&) decl); |
| 422 | if (f) { |
| 423 | result->push_back(std::move(f)); |
| 424 | } |
| 425 | break; |
| 426 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 427 | case ASTDeclaration::kInterfaceBlock_Kind: { |
| 428 | std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfaceBlock( |
| 429 | (ASTInterfaceBlock&) decl); |
| 430 | if (i) { |
| 431 | result->push_back(std::move(i)); |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | case ASTDeclaration::kExtension_Kind: { |
| 436 | std::unique_ptr<Extension> e = fIRGenerator->convertExtension((ASTExtension&) decl); |
| 437 | if (e) { |
| 438 | result->push_back(std::move(e)); |
| 439 | } |
| 440 | break; |
| 441 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 442 | case ASTDeclaration::kPrecision_Kind: { |
| 443 | *defaultPrecision = ((ASTPrecision&) decl).fPrecision; |
| 444 | break; |
| 445 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 446 | default: |
| 447 | ABORT("unsupported declaration: %s\n", decl.description().c_str()); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 452 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 453 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 454 | fErrorText = ""; |
| 455 | fErrorCount = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 456 | fIRGenerator->start(&settings); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 457 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 458 | Modifiers::Flag ignored; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 459 | switch (kind) { |
| 460 | case Program::kVertex_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 461 | this->internalConvertProgram(SkString(SKSL_VERT_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 462 | break; |
| 463 | case Program::kFragment_Kind: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 464 | this->internalConvertProgram(SkString(SKSL_FRAG_INCLUDE), &ignored, &elements); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 465 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame^] | 466 | case Program::kGeometry_Kind: |
| 467 | this->internalConvertProgram(SkString(SKSL_GEOM_INCLUDE), &ignored, &elements); |
| 468 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 469 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 470 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 471 | Modifiers::Flag defaultPrecision; |
| 472 | this->internalConvertProgram(text, &defaultPrecision, &elements); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 473 | auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext, |
| 474 | std::move(elements), |
| 475 | fIRGenerator->fSymbolTable, |
| 476 | fIRGenerator->fInputs)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 477 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 478 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 479 | if (fErrorCount) { |
| 480 | return nullptr; |
| 481 | } |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | bool Compiler::toSPIRV(const Program& program, SkWStream& out) { |
| 487 | SPIRVCodeGenerator cg(&fContext, &program, this, &out); |
| 488 | bool result = cg.generateCode(); |
| 489 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 490 | return result; |
| 491 | } |
| 492 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 493 | bool Compiler::toSPIRV(const Program& program, SkString* out) { |
| 494 | SkDynamicMemoryWStream buffer; |
| 495 | bool result = this->toSPIRV(program, buffer); |
| 496 | if (result) { |
| 497 | sk_sp<SkData> data(buffer.detachAsData()); |
| 498 | *out = SkString((const char*) data->data(), data->size()); |
| 499 | } |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | bool Compiler::toGLSL(const Program& program, SkWStream& out) { |
| 504 | GLSLCodeGenerator cg(&fContext, &program, this, &out); |
| 505 | bool result = cg.generateCode(); |
| 506 | this->writeErrorCount(); |
| 507 | return result; |
| 508 | } |
| 509 | |
| 510 | bool Compiler::toGLSL(const Program& program, SkString* out) { |
| 511 | SkDynamicMemoryWStream buffer; |
| 512 | bool result = this->toGLSL(program, buffer); |
| 513 | if (result) { |
| 514 | sk_sp<SkData> data(buffer.detachAsData()); |
| 515 | *out = SkString((const char*) data->data(), data->size()); |
| 516 | } |
| 517 | return result; |
| 518 | } |
| 519 | |
| 520 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 521 | void Compiler::error(Position position, SkString msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | fErrorCount++; |
| 523 | fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; |
| 524 | } |
| 525 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 526 | SkString Compiler::errorText() { |
| 527 | SkString result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 528 | return result; |
| 529 | } |
| 530 | |
| 531 | void Compiler::writeErrorCount() { |
| 532 | if (fErrorCount) { |
| 533 | fErrorText += to_string(fErrorCount) + " error"; |
| 534 | if (fErrorCount > 1) { |
| 535 | fErrorText += "s"; |
| 536 | } |
| 537 | fErrorText += "\n"; |
| 538 | } |
| 539 | } |
| 540 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 541 | } // namespace |