OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLCompiler.h" | 8 #include "SkSLCompiler.h" |
9 | 9 |
10 #include <fstream> | 10 #include <fstream> |
11 #include <streambuf> | 11 #include <streambuf> |
12 | 12 |
13 #include "ast/SkSLASTPrecision.h" | 13 #include "ast/SkSLASTPrecision.h" |
| 14 #include "SkSLCFGGenerator.h" |
14 #include "SkSLIRGenerator.h" | 15 #include "SkSLIRGenerator.h" |
15 #include "SkSLParser.h" | 16 #include "SkSLParser.h" |
16 #include "SkSLSPIRVCodeGenerator.h" | 17 #include "SkSLSPIRVCodeGenerator.h" |
17 #include "ir/SkSLExpression.h" | 18 #include "ir/SkSLExpression.h" |
18 #include "ir/SkSLIntLiteral.h" | 19 #include "ir/SkSLIntLiteral.h" |
19 #include "ir/SkSLModifiersDeclaration.h" | 20 #include "ir/SkSLModifiersDeclaration.h" |
20 #include "ir/SkSLSymbolTable.h" | 21 #include "ir/SkSLSymbolTable.h" |
21 #include "ir/SkSLVarDeclaration.h" | 22 #include "ir/SkSLVarDeclarations.h" |
22 #include "SkMutex.h" | 23 #include "SkMutex.h" |
23 | 24 |
24 #define STRINGIFY(x) #x | 25 #define STRINGIFY(x) #x |
25 | 26 |
26 // include the built-in shader symbols as static strings | 27 // include the built-in shader symbols as static strings |
27 | 28 |
28 static const char* SKSL_INCLUDE = | 29 static const char* SKSL_INCLUDE = |
29 #include "sksl.include" | 30 #include "sksl.include" |
30 ; | 31 ; |
31 | 32 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 Modifiers::Flag ignored1; | 135 Modifiers::Flag ignored1; |
135 std::vector<std::unique_ptr<ProgramElement>> ignored2; | 136 std::vector<std::unique_ptr<ProgramElement>> ignored2; |
136 this->internalConvertProgram(SKSL_INCLUDE, &ignored1, &ignored2); | 137 this->internalConvertProgram(SKSL_INCLUDE, &ignored1, &ignored2); |
137 ASSERT(!fErrorCount); | 138 ASSERT(!fErrorCount); |
138 } | 139 } |
139 | 140 |
140 Compiler::~Compiler() { | 141 Compiler::~Compiler() { |
141 delete fIRGenerator; | 142 delete fIRGenerator; |
142 } | 143 } |
143 | 144 |
| 145 // add the definition created by assigning to the lvalue to the definition set |
| 146 void Compiler::addDefinition(const Expression* lvalue, const Expression* expr, |
| 147 std::unordered_map<const Variable*, const Expression*
>* definitions) { |
| 148 switch (lvalue->fKind) { |
| 149 case Expression::kVariableReference_Kind: { |
| 150 const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 151 if (var.fStorage == Variable::kLocal_Storage) { |
| 152 (*definitions)[&var] = expr; |
| 153 } |
| 154 break; |
| 155 } |
| 156 case Expression::kSwizzle_Kind: |
| 157 // We consider the variable written to as long as at least some of i
ts components have |
| 158 // been written to. This will lead to some false negatives (we won't
catch it if you |
| 159 // write to foo.x and then read foo.y), but being stricter could lea
d to false positives |
| 160 // (we write to foo.x, and then pass foo to a function which happens
to only read foo.x, |
| 161 // but since we pass foo as a whole it is flagged as an error) unles
s we perform a much |
| 162 // more complicated whole-program analysis. This is probably good en
ough. |
| 163 this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
| 164 fContext.fDefined_Expression.get(), |
| 165 definitions); |
| 166 break; |
| 167 case Expression::kIndex_Kind: |
| 168 // see comments in Swizzle |
| 169 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
| 170 fContext.fDefined_Expression.get(), |
| 171 definitions); |
| 172 break; |
| 173 case Expression::kFieldAccess_Kind: |
| 174 // see comments in Swizzle |
| 175 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
| 176 fContext.fDefined_Expression.get(), |
| 177 definitions); |
| 178 break; |
| 179 default: |
| 180 // not an lvalue, can't happen |
| 181 ASSERT(false); |
| 182 } |
| 183 } |
| 184 |
| 185 // add local variables defined by this node to the set |
| 186 void Compiler::addDefinitions(const BasicBlock::Node& node, |
| 187 std::unordered_map<const Variable*, const Expressi
on*>* definitions) { |
| 188 switch (node.fKind) { |
| 189 case BasicBlock::Node::kExpression_Kind: { |
| 190 const Expression* expr = (Expression*) node.fNode; |
| 191 if (expr->fKind == Expression::kBinary_Kind) { |
| 192 const BinaryExpression* b = (BinaryExpression*) expr; |
| 193 if (b->fOperator == Token::EQ) { |
| 194 this->addDefinition(b->fLeft.get(), b->fRight.get(), definit
ions); |
| 195 } |
| 196 } |
| 197 break; |
| 198 } |
| 199 case BasicBlock::Node::kStatement_Kind: { |
| 200 const Statement* stmt = (Statement*) node.fNode; |
| 201 if (stmt->fKind == Statement::kVarDeclarations_Kind) { |
| 202 const VarDeclarationsStatement* vd = (VarDeclarationsStatement*)
stmt; |
| 203 for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 204 if (decl.fValue) { |
| 205 (*definitions)[decl.fVar] = decl.fValue.get(); |
| 206 } |
| 207 } |
| 208 } |
| 209 break; |
| 210 } |
| 211 } |
| 212 } |
| 213 |
| 214 void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 215 BasicBlock& block = cfg->fBlocks[blockId]; |
| 216 |
| 217 // compute definitions after this block |
| 218 std::unordered_map<const Variable*, const Expression*> after = block.fBefore
; |
| 219 for (const BasicBlock::Node& n : block.fNodes) { |
| 220 this->addDefinitions(n, &after); |
| 221 } |
| 222 |
| 223 // propagate definitions to exits |
| 224 for (BlockId exitId : block.fExits) { |
| 225 BasicBlock& exit = cfg->fBlocks[exitId]; |
| 226 for (const auto& pair : after) { |
| 227 const Expression* e1 = pair.second; |
| 228 if (exit.fBefore.find(pair.first) == exit.fBefore.end()) { |
| 229 exit.fBefore[pair.first] = e1; |
| 230 } else { |
| 231 const Expression* e2 = exit.fBefore[pair.first]; |
| 232 if (e1 != e2) { |
| 233 // definition has changed, merge and add exit block to workl
ist |
| 234 workList->insert(exitId); |
| 235 if (!e1 || !e2) { |
| 236 exit.fBefore[pair.first] = nullptr; |
| 237 } else { |
| 238 exit.fBefore[pair.first] = fContext.fDefined_Expression.
get(); |
| 239 } |
| 240 } |
| 241 } |
| 242 } |
| 243 } |
| 244 } |
| 245 |
| 246 // returns a map which maps all local variables in the function to null, indicat
ing that their value |
| 247 // is initially unknown |
| 248 static std::unordered_map<const Variable*, const Expression*> compute_start_stat
e(const CFG& cfg) { |
| 249 std::unordered_map<const Variable*, const Expression*> result; |
| 250 for (const auto block : cfg.fBlocks) { |
| 251 for (const auto node : block.fNodes) { |
| 252 if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
| 253 const Statement* s = (Statement*) node.fNode; |
| 254 if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 255 const VarDeclarationsStatement* vd = (const VarDeclarationsS
tatement*) s; |
| 256 for (const VarDeclaration& decl : vd->fDeclaration->fVars) { |
| 257 result[decl.fVar] = nullptr; |
| 258 } |
| 259 } |
| 260 } |
| 261 } |
| 262 } |
| 263 return result; |
| 264 } |
| 265 |
| 266 void Compiler::scanCFG(const FunctionDefinition& f) { |
| 267 CFG cfg = CFGGenerator().getCFG(f); |
| 268 |
| 269 // compute the data flow |
| 270 cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg); |
| 271 std::set<BlockId> workList; |
| 272 for (BlockId i = 0; i < cfg.fBlocks.size(); i++) { |
| 273 workList.insert(i); |
| 274 } |
| 275 while (workList.size()) { |
| 276 BlockId next = *workList.begin(); |
| 277 workList.erase(workList.begin()); |
| 278 this->scanCFG(&cfg, next, &workList); |
| 279 } |
| 280 |
| 281 // check for unreachable code |
| 282 for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
| 283 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
| 284 cfg.fBlocks[i].fNodes.size()) { |
| 285 this->error(cfg.fBlocks[i].fNodes[0].fNode->fPosition, "unreachable"
); |
| 286 } |
| 287 } |
| 288 if (fErrorCount) { |
| 289 return; |
| 290 } |
| 291 |
| 292 // check for undefined variables |
| 293 for (const BasicBlock& b : cfg.fBlocks) { |
| 294 std::unordered_map<const Variable*, const Expression*> definitions = b.f
Before; |
| 295 for (const BasicBlock::Node& n : b.fNodes) { |
| 296 if (n.fKind == BasicBlock::Node::kExpression_Kind) { |
| 297 const Expression* expr = (const Expression*) n.fNode; |
| 298 if (expr->fKind == Expression::kVariableReference_Kind) { |
| 299 const Variable& var = ((VariableReference*) expr)->fVariable
; |
| 300 if (var.fStorage == Variable::kLocal_Storage && |
| 301 !definitions[&var]) { |
| 302 this->error(expr->fPosition, |
| 303 "'" + var.fName + "' has not been assigned")
; |
| 304 } |
| 305 } |
| 306 } |
| 307 this->addDefinitions(n, &definitions); |
| 308 } |
| 309 } |
| 310 |
| 311 // check for missing return |
| 312 if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) { |
| 313 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
| 314 this->error(f.fPosition, "function can exit without returning a valu
e"); |
| 315 } |
| 316 } |
| 317 } |
| 318 |
144 void Compiler::internalConvertProgram(std::string text, | 319 void Compiler::internalConvertProgram(std::string text, |
145 Modifiers::Flag* defaultPrecision, | 320 Modifiers::Flag* defaultPrecision, |
146 std::vector<std::unique_ptr<ProgramElement
>>* result) { | 321 std::vector<std::unique_ptr<ProgramElement
>>* result) { |
147 Parser parser(text, *fTypes, *this); | 322 Parser parser(text, *fTypes, *this); |
148 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); | 323 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
149 if (fErrorCount) { | 324 if (fErrorCount) { |
150 return; | 325 return; |
151 } | 326 } |
152 *defaultPrecision = Modifiers::kHighp_Flag; | 327 *defaultPrecision = Modifiers::kHighp_Flag; |
153 for (size_t i = 0; i < parsed.size(); i++) { | 328 for (size_t i = 0; i < parsed.size(); i++) { |
154 ASTDeclaration& decl = *parsed[i]; | 329 ASTDeclaration& decl = *parsed[i]; |
155 switch (decl.fKind) { | 330 switch (decl.fKind) { |
156 case ASTDeclaration::kVar_Kind: { | 331 case ASTDeclaration::kVar_Kind: { |
157 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( | 332 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( |
158 (ASTVar
Declarations&) decl, | 333 (ASTVar
Declarations&) decl, |
159 Variabl
e::kGlobal_Storage); | 334 Variabl
e::kGlobal_Storage); |
160 if (s) { | 335 if (s) { |
161 result->push_back(std::move(s)); | 336 result->push_back(std::move(s)); |
162 } | 337 } |
163 break; | 338 break; |
164 } | 339 } |
165 case ASTDeclaration::kFunction_Kind: { | 340 case ASTDeclaration::kFunction_Kind: { |
166 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( | 341 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( |
167 (
ASTFunction&) decl); | 342 (
ASTFunction&) decl); |
168 if (f) { | 343 if (!fErrorCount && f) { |
| 344 this->scanCFG(*f); |
169 result->push_back(std::move(f)); | 345 result->push_back(std::move(f)); |
170 } | 346 } |
171 break; | 347 break; |
172 } | 348 } |
173 case ASTDeclaration::kModifiers_Kind: { | 349 case ASTDeclaration::kModifiers_Kind: { |
174 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertM
odifiersDeclaration( | 350 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertM
odifiersDeclaration( |
175 (ASTModifiers
Declaration&) decl); | 351 (ASTModifiers
Declaration&) decl); |
176 if (f) { | 352 if (f) { |
177 result->push_back(std::move(f)); | 353 result->push_back(std::move(f)); |
178 } | 354 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 std::string* out) { | 456 std::string* out) { |
281 std::stringstream buffer; | 457 std::stringstream buffer; |
282 bool result = this->toGLSL(kind, text, caps, buffer); | 458 bool result = this->toGLSL(kind, text, caps, buffer); |
283 if (result) { | 459 if (result) { |
284 *out = buffer.str(); | 460 *out = buffer.str(); |
285 } | 461 } |
286 return result; | 462 return result; |
287 } | 463 } |
288 | 464 |
289 } // namespace | 465 } // namespace |
OLD | NEW |