Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: src/sksl/SkSLCompiler.cpp

Issue 2405383003: added basic dataflow analysis to skslc (Closed)
Patch Set: various fixes Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 }
198 case BasicBlock::Node::kStatement_Kind: {
199 const Statement* stmt = (Statement*) node.fNode;
200 if (stmt->fKind == Statement::kVarDeclarations_Kind) {
201 const VarDeclarationsStatement* vd = (VarDeclarationsStatement*) stmt;
202 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
203 if (decl.fValue) {
204 (*definitions)[decl.fVar] = decl.fValue.get();
205 }
206 }
207 }
208 }
209 }
210 }
211
212 void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
213 BasicBlock& block = cfg->fBlocks[blockId];
214
215 // compute definitions after this block
216 std::unordered_map<const Variable*, const Expression*> after = block.fBefore ;
217 for (const BasicBlock::Node& n : block.fNodes) {
218 this->addDefinitions(n, &after);
219 }
220
221 // propagate definitions to exits
222 for (auto iter = block.fExits.begin(); iter != block.fExits.end(); iter++) {
dogben 2016/10/13 03:55:43 nit: range loop would be easier to understand. Sam
ethannicholas 2016/10/13 17:41:27 Fixed.
223 BasicBlock& exit = cfg->fBlocks[*iter];
224 for (auto afterIter = after.begin(); afterIter != after.end(); afterIter ++) {
225 const Expression* e1 = afterIter->second;
226 if (exit.fBefore.find(afterIter->first) == exit.fBefore.end()) {
227 exit.fBefore[afterIter->first] = e1;
228 } else {
229 const Expression* e2 = exit.fBefore[afterIter->first];
230 if (e1 != e2) {
231 // definition has changed, merge and add exit block to workl ist
232 workList->insert(*iter);
233 if (!e1 || !e2) {
234 exit.fBefore[afterIter->first] = nullptr;
235 } else {
236 exit.fBefore[afterIter->first] = fContext.fDefined_Expre ssion.get();
237 }
238 }
239 }
240 }
241 }
242 }
243
244 // returns a map which maps all local variables in the function to null, indicat ing that their value
245 // is initially unknown
246 static std::unordered_map<const Variable*, const Expression*> compute_start_stat e(const CFG& cfg) {
247 std::unordered_map<const Variable*, const Expression*> result;
248 for (const auto block : cfg.fBlocks) {
249 for (const auto node : block.fNodes) {
250 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
251 const Statement* s = (Statement*) node.fNode;
252 if (s->fKind == Statement::kVarDeclarations_Kind) {
253 const VarDeclarationsStatement* vd = (const VarDeclarationsS tatement*) s;
254 for (const VarDeclaration& decl : vd->fDeclaration->fVars) {
255 result[decl.fVar] = nullptr;
256 }
257 }
258 }
259 }
260 }
261 return result;
262 }
263
264 void Compiler::scanCFG(const FunctionDefinition& f) {
265 CFG cfg = CFGGenerator().getCFG(f);
266
267 // compute the data flow
268 cfg.fBlocks[cfg.fStart].fBefore = compute_start_state(cfg);
269 std::set<BlockId> workList;
270 for (BlockId i = 0; i < cfg.fBlocks.size(); i++) {
271 workList.insert(i);
272 }
273 while (workList.size()) {
274 BlockId next = *workList.begin();
275 workList.erase(workList.begin());
276 this->scanCFG(&cfg, next, &workList);
277 }
278
279 // check for unreachable code
280 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
281 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
282 cfg.fBlocks[i].fNodes.size()) {
283 this->error(cfg.fBlocks[i].fNodes[0].fNode->fPosition, "unreachable" );
284 }
285 }
286 if (fErrorCount) {
287 return;
288 }
289
290 // check for undefined variables
291 for (const BasicBlock& b : cfg.fBlocks) {
292 std::unordered_map<const Variable*, const Expression*> definitions = b.f Before;
293 for (const BasicBlock::Node& n : b.fNodes) {
294 if (n.fKind == BasicBlock::Node::kExpression_Kind) {
295 const Expression* expr = (const Expression*) n.fNode;
296 if (expr->fKind == Expression::kVariableReference_Kind) {
297 const Variable& var = ((VariableReference*) expr)->fVariable ;
298 if (var.fStorage == Variable::kLocal_Storage &&
299 !definitions[&var]) {
300 this->error(expr->fPosition,
301 "'" + var.fName + "' has not been assigned") ;
302 }
303 }
304 }
305 this->addDefinitions(n, &definitions);
306 }
307 }
308
309 // check for missing return
310 if (f.fDeclaration.fReturnType != *fContext.fVoid_Type) {
311 if (cfg.fStart != cfg.fExit && cfg.fBlocks[cfg.fExit].fEntrances.size()) {
dogben 2016/10/13 03:55:43 nit: It's always true that fStart != fExit, becaus
ethannicholas 2016/10/13 17:41:27 Well, it wasn't true when I wrote that! Fixed :-)
312 this->error(f.fPosition, "function can exit without returning a valu e");
313 }
314 }
315 }
316
144 void Compiler::internalConvertProgram(std::string text, 317 void Compiler::internalConvertProgram(std::string text,
145 Modifiers::Flag* defaultPrecision, 318 Modifiers::Flag* defaultPrecision,
146 std::vector<std::unique_ptr<ProgramElement >>* result) { 319 std::vector<std::unique_ptr<ProgramElement >>* result) {
147 Parser parser(text, *fTypes, *this); 320 Parser parser(text, *fTypes, *this);
148 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); 321 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
149 if (fErrorCount) { 322 if (fErrorCount) {
150 return; 323 return;
151 } 324 }
152 *defaultPrecision = Modifiers::kHighp_Flag; 325 *defaultPrecision = Modifiers::kHighp_Flag;
153 for (size_t i = 0; i < parsed.size(); i++) { 326 for (size_t i = 0; i < parsed.size(); i++) {
154 ASTDeclaration& decl = *parsed[i]; 327 ASTDeclaration& decl = *parsed[i];
155 switch (decl.fKind) { 328 switch (decl.fKind) {
156 case ASTDeclaration::kVar_Kind: { 329 case ASTDeclaration::kVar_Kind: {
157 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec larations( 330 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec larations(
158 (ASTVar Declarations&) decl, 331 (ASTVar Declarations&) decl,
159 Variabl e::kGlobal_Storage); 332 Variabl e::kGlobal_Storage);
160 if (s) { 333 if (s) {
161 result->push_back(std::move(s)); 334 result->push_back(std::move(s));
162 } 335 }
163 break; 336 break;
164 } 337 }
165 case ASTDeclaration::kFunction_Kind: { 338 case ASTDeclaration::kFunction_Kind: {
166 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun ction( 339 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun ction(
167 ( ASTFunction&) decl); 340 ( ASTFunction&) decl);
168 if (f) { 341 if (!fErrorCount && f) {
342 this->scanCFG(*f);
169 result->push_back(std::move(f)); 343 result->push_back(std::move(f));
170 } 344 }
171 break; 345 break;
172 } 346 }
173 case ASTDeclaration::kModifiers_Kind: { 347 case ASTDeclaration::kModifiers_Kind: {
174 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertM odifiersDeclaration( 348 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertM odifiersDeclaration(
175 (ASTModifiers Declaration&) decl); 349 (ASTModifiers Declaration&) decl);
176 if (f) { 350 if (f) {
177 result->push_back(std::move(f)); 351 result->push_back(std::move(f));
178 } 352 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 std::string* out) { 454 std::string* out) {
281 std::stringstream buffer; 455 std::stringstream buffer;
282 bool result = this->toGLSL(kind, text, caps, buffer); 456 bool result = this->toGLSL(kind, text, caps, buffer);
283 if (result) { 457 if (result) {
284 *out = buffer.str(); 458 *out = buffer.str();
285 } 459 }
286 return result; 460 return result;
287 } 461 }
288 462
289 } // namespace 463 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698