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