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 | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 10 | #include "SkSLCFGGenerator.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 11 | #include "SkSLCPPCodeGenerator.h" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 12 | #include "SkSLGLSLCodeGenerator.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 13 | #include "SkSLHCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | #include "SkSLIRGenerator.h" |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 15 | #include "SkSLMetalCodeGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 16 | #include "SkSLSPIRVCodeGenerator.h" |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 17 | #include "ir/SkSLEnum.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 | #include "ir/SkSLExpression.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 19 | #include "ir/SkSLExpressionStatement.h" |
Ethan Nicholas | c6a19f1 | 2018-03-29 16:46:56 -0400 | [diff] [blame] | 20 | #include "ir/SkSLFunctionCall.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | #include "ir/SkSLIntLiteral.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 22 | #include "ir/SkSLModifiersDeclaration.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 23 | #include "ir/SkSLNop.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | #include "ir/SkSLSymbolTable.h" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 25 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 26 | #include "ir/SkSLUnresolvedFunction.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 27 | #include "ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 29 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 30 | #include "spirv-tools/libspirv.hpp" |
| 31 | #endif |
| 32 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | // include the built-in shader symbols as static strings |
| 34 | |
Ethan Nicholas | 7970765 | 2017-11-16 11:20:11 -0500 | [diff] [blame] | 35 | #define STRINGIFY(x) #x |
| 36 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 37 | static const char* SKSL_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 38 | #include "sksl.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | ; |
| 40 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 41 | static const char* SKSL_VERT_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 42 | #include "sksl_vert.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | ; |
| 44 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 45 | static const char* SKSL_FRAG_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 46 | #include "sksl_frag.inc" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | ; |
| 48 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 49 | static const char* SKSL_GEOM_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 50 | #include "sksl_geom.inc" |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 51 | ; |
| 52 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 53 | static const char* SKSL_FP_INCLUDE = |
Ben Wagner | a56c4d2 | 2018-01-24 17:32:17 -0500 | [diff] [blame] | 54 | #include "sksl_enums.inc" |
| 55 | #include "sksl_fp.inc" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 56 | ; |
| 57 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 58 | static const char* SKSL_CPU_INCLUDE = |
| 59 | #include "sksl_cpu.inc" |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 60 | ; |
| 61 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 62 | namespace SkSL { |
| 63 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 64 | Compiler::Compiler(Flags flags) |
| 65 | : fFlags(flags) |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 66 | , fContext(new Context()) |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 67 | , fErrorCount(0) { |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 68 | auto types = std::shared_ptr<SymbolTable>(new SymbolTable(this)); |
| 69 | auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, this)); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 70 | fIRGenerator = new IRGenerator(fContext.get(), symbols, *this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | fTypes = types; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 72 | #define ADD_TYPE(t) types->addWithoutOwnership(fContext->f ## t ## _Type->fName, \ |
| 73 | fContext->f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | ADD_TYPE(Void); |
| 75 | ADD_TYPE(Float); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 76 | ADD_TYPE(Float2); |
| 77 | ADD_TYPE(Float3); |
| 78 | ADD_TYPE(Float4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 79 | ADD_TYPE(Half); |
| 80 | ADD_TYPE(Half2); |
| 81 | ADD_TYPE(Half3); |
| 82 | ADD_TYPE(Half4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 83 | ADD_TYPE(Double); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 84 | ADD_TYPE(Double2); |
| 85 | ADD_TYPE(Double3); |
| 86 | ADD_TYPE(Double4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | ADD_TYPE(Int); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 88 | ADD_TYPE(Int2); |
| 89 | ADD_TYPE(Int3); |
| 90 | ADD_TYPE(Int4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 91 | ADD_TYPE(UInt); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 92 | ADD_TYPE(UInt2); |
| 93 | ADD_TYPE(UInt3); |
| 94 | ADD_TYPE(UInt4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 95 | ADD_TYPE(Short); |
| 96 | ADD_TYPE(Short2); |
| 97 | ADD_TYPE(Short3); |
| 98 | ADD_TYPE(Short4); |
| 99 | ADD_TYPE(UShort); |
| 100 | ADD_TYPE(UShort2); |
| 101 | ADD_TYPE(UShort3); |
| 102 | ADD_TYPE(UShort4); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 103 | ADD_TYPE(Byte); |
| 104 | ADD_TYPE(Byte2); |
| 105 | ADD_TYPE(Byte3); |
| 106 | ADD_TYPE(Byte4); |
| 107 | ADD_TYPE(UByte); |
| 108 | ADD_TYPE(UByte2); |
| 109 | ADD_TYPE(UByte3); |
| 110 | ADD_TYPE(UByte4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 111 | ADD_TYPE(Bool); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 112 | ADD_TYPE(Bool2); |
| 113 | ADD_TYPE(Bool3); |
| 114 | ADD_TYPE(Bool4); |
| 115 | ADD_TYPE(Float2x2); |
| 116 | ADD_TYPE(Float2x3); |
| 117 | ADD_TYPE(Float2x4); |
| 118 | ADD_TYPE(Float3x2); |
| 119 | ADD_TYPE(Float3x3); |
| 120 | ADD_TYPE(Float3x4); |
| 121 | ADD_TYPE(Float4x2); |
| 122 | ADD_TYPE(Float4x3); |
| 123 | ADD_TYPE(Float4x4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 124 | ADD_TYPE(Half2x2); |
| 125 | ADD_TYPE(Half2x3); |
| 126 | ADD_TYPE(Half2x4); |
| 127 | ADD_TYPE(Half3x2); |
| 128 | ADD_TYPE(Half3x3); |
| 129 | ADD_TYPE(Half3x4); |
| 130 | ADD_TYPE(Half4x2); |
| 131 | ADD_TYPE(Half4x3); |
| 132 | ADD_TYPE(Half4x4); |
| 133 | ADD_TYPE(Double2x2); |
| 134 | ADD_TYPE(Double2x3); |
| 135 | ADD_TYPE(Double2x4); |
| 136 | ADD_TYPE(Double3x2); |
| 137 | ADD_TYPE(Double3x3); |
| 138 | ADD_TYPE(Double3x4); |
| 139 | ADD_TYPE(Double4x2); |
| 140 | ADD_TYPE(Double4x3); |
| 141 | ADD_TYPE(Double4x4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 142 | ADD_TYPE(GenType); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 143 | ADD_TYPE(GenHType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 144 | ADD_TYPE(GenDType); |
| 145 | ADD_TYPE(GenIType); |
| 146 | ADD_TYPE(GenUType); |
| 147 | ADD_TYPE(GenBType); |
| 148 | ADD_TYPE(Mat); |
| 149 | ADD_TYPE(Vec); |
| 150 | ADD_TYPE(GVec); |
| 151 | ADD_TYPE(GVec2); |
| 152 | ADD_TYPE(GVec3); |
| 153 | ADD_TYPE(GVec4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 154 | ADD_TYPE(HVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 155 | ADD_TYPE(DVec); |
| 156 | ADD_TYPE(IVec); |
| 157 | ADD_TYPE(UVec); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 158 | ADD_TYPE(SVec); |
| 159 | ADD_TYPE(USVec); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 160 | ADD_TYPE(ByteVec); |
| 161 | ADD_TYPE(UByteVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | ADD_TYPE(BVec); |
| 163 | |
| 164 | ADD_TYPE(Sampler1D); |
| 165 | ADD_TYPE(Sampler2D); |
| 166 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 167 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | ADD_TYPE(SamplerCube); |
| 169 | ADD_TYPE(Sampler2DRect); |
| 170 | ADD_TYPE(Sampler1DArray); |
| 171 | ADD_TYPE(Sampler2DArray); |
| 172 | ADD_TYPE(SamplerCubeArray); |
| 173 | ADD_TYPE(SamplerBuffer); |
| 174 | ADD_TYPE(Sampler2DMS); |
| 175 | ADD_TYPE(Sampler2DMSArray); |
| 176 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 177 | ADD_TYPE(ISampler2D); |
| 178 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 179 | ADD_TYPE(Image2D); |
| 180 | ADD_TYPE(IImage2D); |
| 181 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 182 | ADD_TYPE(SubpassInput); |
| 183 | ADD_TYPE(SubpassInputMS); |
| 184 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 185 | ADD_TYPE(GSampler1D); |
| 186 | ADD_TYPE(GSampler2D); |
| 187 | ADD_TYPE(GSampler3D); |
| 188 | ADD_TYPE(GSamplerCube); |
| 189 | ADD_TYPE(GSampler2DRect); |
| 190 | ADD_TYPE(GSampler1DArray); |
| 191 | ADD_TYPE(GSampler2DArray); |
| 192 | ADD_TYPE(GSamplerCubeArray); |
| 193 | ADD_TYPE(GSamplerBuffer); |
| 194 | ADD_TYPE(GSampler2DMS); |
| 195 | ADD_TYPE(GSampler2DMSArray); |
| 196 | |
| 197 | ADD_TYPE(Sampler1DShadow); |
| 198 | ADD_TYPE(Sampler2DShadow); |
| 199 | ADD_TYPE(SamplerCubeShadow); |
| 200 | ADD_TYPE(Sampler2DRectShadow); |
| 201 | ADD_TYPE(Sampler1DArrayShadow); |
| 202 | ADD_TYPE(Sampler2DArrayShadow); |
| 203 | ADD_TYPE(SamplerCubeArrayShadow); |
| 204 | ADD_TYPE(GSampler2DArrayShadow); |
| 205 | ADD_TYPE(GSamplerCubeArrayShadow); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 206 | ADD_TYPE(FragmentProcessor); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 207 | ADD_TYPE(SkRasterPipeline); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 209 | StringFragment skCapsName("sk_Caps"); |
| 210 | Variable* skCaps = new Variable(-1, Modifiers(), skCapsName, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 211 | *fContext->fSkCaps_Type, Variable::kGlobal_Storage); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 212 | fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps)); |
| 213 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 214 | StringFragment skArgsName("sk_Args"); |
| 215 | Variable* skArgs = new Variable(-1, Modifiers(), skArgsName, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 216 | *fContext->fSkArgs_Type, Variable::kGlobal_Storage); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 217 | fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs)); |
| 218 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 219 | std::vector<std::unique_ptr<ProgramElement>> ignored; |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 220 | fIRGenerator->convertProgram(Program::kFragment_Kind, SKSL_INCLUDE, strlen(SKSL_INCLUDE), |
| 221 | *fTypes, &ignored); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 222 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 223 | if (fErrorCount) { |
| 224 | printf("Unexpected errors: %s\n", fErrorText.c_str()); |
| 225 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 226 | SkASSERT(!fErrorCount); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 227 | |
| 228 | Program::Settings settings; |
| 229 | fIRGenerator->start(&settings, nullptr); |
| 230 | fIRGenerator->convertProgram(Program::kFragment_Kind, SKSL_VERT_INCLUDE, |
| 231 | strlen(SKSL_VERT_INCLUDE), *fTypes, &fVertexInclude); |
| 232 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
| 233 | fVertexSymbolTable = fIRGenerator->fSymbolTable; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 234 | fIRGenerator->finish(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 235 | |
| 236 | fIRGenerator->start(&settings, nullptr); |
| 237 | fIRGenerator->convertProgram(Program::kVertex_Kind, SKSL_FRAG_INCLUDE, |
| 238 | strlen(SKSL_FRAG_INCLUDE), *fTypes, &fFragmentInclude); |
| 239 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
| 240 | fFragmentSymbolTable = fIRGenerator->fSymbolTable; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 241 | fIRGenerator->finish(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 242 | |
| 243 | fIRGenerator->start(&settings, nullptr); |
| 244 | fIRGenerator->convertProgram(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, |
| 245 | strlen(SKSL_GEOM_INCLUDE), *fTypes, &fGeometryInclude); |
| 246 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
| 247 | fGeometrySymbolTable = fIRGenerator->fSymbolTable; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 248 | fIRGenerator->finish(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | Compiler::~Compiler() { |
| 252 | delete fIRGenerator; |
| 253 | } |
| 254 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 255 | // 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] | 256 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 257 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 258 | switch (lvalue->fKind) { |
| 259 | case Expression::kVariableReference_Kind: { |
| 260 | const Variable& var = ((VariableReference*) lvalue)->fVariable; |
| 261 | if (var.fStorage == Variable::kLocal_Storage) { |
| 262 | (*definitions)[&var] = expr; |
| 263 | } |
| 264 | break; |
| 265 | } |
| 266 | case Expression::kSwizzle_Kind: |
| 267 | // We consider the variable written to as long as at least some of its components have |
| 268 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 269 | // 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] | 270 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 271 | // 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] | 272 | // more complicated whole-program analysis. This is probably good enough. |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 273 | this->addDefinition(((Swizzle*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 274 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 275 | definitions); |
| 276 | break; |
| 277 | case Expression::kIndex_Kind: |
| 278 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 279 | this->addDefinition(((IndexExpression*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 280 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 281 | definitions); |
| 282 | break; |
| 283 | case Expression::kFieldAccess_Kind: |
| 284 | // see comments in Swizzle |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 285 | this->addDefinition(((FieldAccess*) lvalue)->fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 286 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 287 | definitions); |
| 288 | break; |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 289 | case Expression::kTernary_Kind: |
| 290 | // To simplify analysis, we just pretend that we write to both sides of the ternary. |
| 291 | // This allows for false positives (meaning we fail to detect that a variable might not |
| 292 | // have been assigned), but is preferable to false negatives. |
| 293 | this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 294 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 295 | definitions); |
| 296 | this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 297 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 298 | definitions); |
| 299 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 300 | default: |
| 301 | // not an lvalue, can't happen |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 302 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 307 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 308 | DefinitionMap* definitions) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 309 | switch (node.fKind) { |
| 310 | case BasicBlock::Node::kExpression_Kind: { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 311 | SkASSERT(node.expression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 312 | const Expression* expr = (Expression*) node.expression()->get(); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 313 | switch (expr->fKind) { |
| 314 | case Expression::kBinary_Kind: { |
| 315 | BinaryExpression* b = (BinaryExpression*) expr; |
| 316 | if (b->fOperator == Token::EQ) { |
| 317 | this->addDefinition(b->fLeft.get(), &b->fRight, definitions); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 318 | } else if (Compiler::IsAssignment(b->fOperator)) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 319 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 320 | b->fLeft.get(), |
| 321 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 322 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 323 | |
| 324 | } |
| 325 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 326 | } |
Ethan Nicholas | c6a19f1 | 2018-03-29 16:46:56 -0400 | [diff] [blame] | 327 | case Expression::kFunctionCall_Kind: { |
| 328 | const FunctionCall& c = (const FunctionCall&) *expr; |
| 329 | for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) { |
| 330 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 331 | this->addDefinition( |
| 332 | c.fArguments[i].get(), |
| 333 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 334 | definitions); |
| 335 | } |
| 336 | } |
| 337 | break; |
| 338 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 339 | case Expression::kPrefix_Kind: { |
| 340 | const PrefixExpression* p = (PrefixExpression*) expr; |
| 341 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 342 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 343 | p->fOperand.get(), |
| 344 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 345 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 346 | } |
| 347 | break; |
| 348 | } |
| 349 | case Expression::kPostfix_Kind: { |
| 350 | const PostfixExpression* p = (PostfixExpression*) expr; |
| 351 | if (p->fOperator == Token::MINUSMINUS || p->fOperator == Token::PLUSPLUS) { |
| 352 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 353 | p->fOperand.get(), |
| 354 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 355 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 356 | } |
| 357 | break; |
| 358 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 359 | case Expression::kVariableReference_Kind: { |
| 360 | const VariableReference* v = (VariableReference*) expr; |
| 361 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 362 | this->addDefinition( |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 363 | v, |
| 364 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 365 | definitions); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 368 | default: |
| 369 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 370 | } |
| 371 | break; |
| 372 | } |
| 373 | case BasicBlock::Node::kStatement_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 374 | const Statement* stmt = (Statement*) node.statement()->get(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 375 | if (stmt->fKind == Statement::kVarDeclaration_Kind) { |
| 376 | VarDeclaration& vd = (VarDeclaration&) *stmt; |
| 377 | if (vd.fValue) { |
| 378 | (*definitions)[vd.fVar] = &vd.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 387 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 388 | |
| 389 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 390 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 391 | for (const BasicBlock::Node& n : block.fNodes) { |
| 392 | this->addDefinitions(n, &after); |
| 393 | } |
| 394 | |
| 395 | // propagate definitions to exits |
| 396 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 397 | if (exitId == blockId) { |
| 398 | continue; |
| 399 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 400 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 401 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 402 | std::unique_ptr<Expression>* e1 = pair.second; |
| 403 | auto found = exit.fBefore.find(pair.first); |
| 404 | if (found == exit.fBefore.end()) { |
| 405 | // exit has no definition for it, just copy it |
| 406 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 407 | exit.fBefore[pair.first] = e1; |
| 408 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 409 | // exit has a (possibly different) value already defined |
| 410 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 411 | if (e1 != e2) { |
| 412 | // definition has changed, merge and add exit block to worklist |
| 413 | workList->insert(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 414 | if (e1 && e2) { |
| 415 | exit.fBefore[pair.first] = |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 416 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 417 | } else { |
| 418 | exit.fBefore[pair.first] = nullptr; |
| 419 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 427 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 428 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 429 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 430 | for (const auto& block : cfg.fBlocks) { |
| 431 | for (const auto& node : block.fNodes) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 432 | if (node.fKind == BasicBlock::Node::kStatement_Kind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 433 | SkASSERT(node.statement()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 434 | const Statement* s = node.statement()->get(); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 435 | if (s->fKind == Statement::kVarDeclarations_Kind) { |
| 436 | const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 437 | for (const auto& decl : vd->fDeclaration->fVars) { |
| 438 | if (decl->fKind == Statement::kVarDeclaration_Kind) { |
| 439 | result[((VarDeclaration&) *decl).fVar] = nullptr; |
| 440 | } |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 441 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | return result; |
| 447 | } |
| 448 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 449 | /** |
| 450 | * Returns true if assigning to this lvalue has no effect. |
| 451 | */ |
| 452 | static bool is_dead(const Expression& lvalue) { |
| 453 | switch (lvalue.fKind) { |
| 454 | case Expression::kVariableReference_Kind: |
| 455 | return ((VariableReference&) lvalue).fVariable.dead(); |
| 456 | case Expression::kSwizzle_Kind: |
| 457 | return is_dead(*((Swizzle&) lvalue).fBase); |
| 458 | case Expression::kFieldAccess_Kind: |
| 459 | return is_dead(*((FieldAccess&) lvalue).fBase); |
| 460 | case Expression::kIndex_Kind: { |
| 461 | const IndexExpression& idx = (IndexExpression&) lvalue; |
| 462 | return is_dead(*idx.fBase) && !idx.fIndex->hasSideEffects(); |
| 463 | } |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 464 | case Expression::kTernary_Kind: { |
| 465 | const TernaryExpression& t = (TernaryExpression&) lvalue; |
| 466 | return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse); |
| 467 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 468 | default: |
| 469 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
| 470 | } |
| 471 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 472 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 473 | /** |
| 474 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 475 | * to a dead target and lack of side effects on the left hand side. |
| 476 | */ |
| 477 | static bool dead_assignment(const BinaryExpression& b) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 478 | if (!Compiler::IsAssignment(b.fOperator)) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | return is_dead(*b.fLeft); |
| 482 | } |
| 483 | |
| 484 | void Compiler::computeDataFlow(CFG* cfg) { |
| 485 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 486 | std::set<BlockId> workList; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 487 | for (BlockId i = 0; i < cfg->fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 488 | workList.insert(i); |
| 489 | } |
| 490 | while (workList.size()) { |
| 491 | BlockId next = *workList.begin(); |
| 492 | workList.erase(workList.begin()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 493 | this->scanCFG(cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 494 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 499 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 500 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 501 | * need to be regenerated). |
| 502 | */ |
| 503 | bool try_replace_expression(BasicBlock* b, |
| 504 | std::vector<BasicBlock::Node>::iterator* iter, |
| 505 | std::unique_ptr<Expression>* newExpression) { |
| 506 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 507 | if (!b->tryRemoveExpression(iter)) { |
| 508 | *target = std::move(*newExpression); |
| 509 | return false; |
| 510 | } |
| 511 | *target = std::move(*newExpression); |
| 512 | return b->tryInsertExpression(iter, target); |
| 513 | } |
| 514 | |
| 515 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 516 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 517 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 518 | */ |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 519 | bool is_constant(const Expression& expr, double value) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 520 | switch (expr.fKind) { |
| 521 | case Expression::kIntLiteral_Kind: |
| 522 | return ((IntLiteral&) expr).fValue == value; |
| 523 | case Expression::kFloatLiteral_Kind: |
| 524 | return ((FloatLiteral&) expr).fValue == value; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 525 | case Expression::kConstructor_Kind: { |
| 526 | Constructor& c = (Constructor&) expr; |
| 527 | if (c.fType.kind() == Type::kVector_Kind && c.isConstant()) { |
| 528 | for (int i = 0; i < c.fType.columns(); ++i) { |
| 529 | if (!is_constant(c.getVecComponent(i), value)) { |
| 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | return true; |
| 534 | } |
| 535 | return false; |
| 536 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 537 | default: |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 544 | * and CFG structures). |
| 545 | */ |
| 546 | void delete_left(BasicBlock* b, |
| 547 | std::vector<BasicBlock::Node>::iterator* iter, |
| 548 | bool* outUpdated, |
| 549 | bool* outNeedsRescan) { |
| 550 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 551 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 552 | SkASSERT((*target)->fKind == Expression::kBinary_Kind); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 553 | BinaryExpression& bin = (BinaryExpression&) **target; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 554 | SkASSERT(!bin.fLeft->hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 555 | bool result; |
| 556 | if (bin.fOperator == Token::EQ) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 557 | result = b->tryRemoveLValueBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 558 | } else { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 559 | result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 560 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 561 | *target = std::move(bin.fRight); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 562 | if (!result) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 563 | *outNeedsRescan = true; |
| 564 | return; |
| 565 | } |
| 566 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 567 | *outNeedsRescan = true; |
| 568 | return; |
| 569 | } |
| 570 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 571 | if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 572 | (*iter)->expression() != &bin.fRight) { |
| 573 | *outNeedsRescan = true; |
| 574 | return; |
| 575 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 576 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 577 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 582 | * CFG structures). |
| 583 | */ |
| 584 | void delete_right(BasicBlock* b, |
| 585 | std::vector<BasicBlock::Node>::iterator* iter, |
| 586 | bool* outUpdated, |
| 587 | bool* outNeedsRescan) { |
| 588 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 589 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 590 | SkASSERT((*target)->fKind == Expression::kBinary_Kind); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 591 | BinaryExpression& bin = (BinaryExpression&) **target; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 592 | SkASSERT(!bin.fRight->hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 593 | if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) { |
| 594 | *target = std::move(bin.fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 595 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 596 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 597 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 598 | *target = std::move(bin.fLeft); |
| 599 | if (*iter == b->fNodes.begin()) { |
| 600 | *outNeedsRescan = true; |
| 601 | return; |
| 602 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 603 | --(*iter); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 604 | if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind || |
| 605 | (*iter)->expression() != &bin.fLeft)) { |
| 606 | *outNeedsRescan = true; |
| 607 | return; |
| 608 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 609 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 610 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 611 | } |
| 612 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 613 | /** |
| 614 | * Constructs the specified type using a single argument. |
| 615 | */ |
| 616 | static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) { |
| 617 | std::vector<std::unique_ptr<Expression>> args; |
| 618 | args.push_back(std::move(v)); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 619 | auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args))); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 620 | return result; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 625 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 626 | */ |
| 627 | static void vectorize(BasicBlock* b, |
| 628 | std::vector<BasicBlock::Node>::iterator* iter, |
| 629 | const Type& type, |
| 630 | std::unique_ptr<Expression>* otherExpression, |
| 631 | bool* outUpdated, |
| 632 | bool* outNeedsRescan) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 633 | SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind); |
| 634 | SkASSERT(type.kind() == Type::kVector_Kind); |
| 635 | SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 636 | *outUpdated = true; |
| 637 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 638 | if (!b->tryRemoveExpression(iter)) { |
| 639 | *target = construct(type, std::move(*otherExpression)); |
| 640 | *outNeedsRescan = true; |
| 641 | } else { |
| 642 | *target = construct(type, std::move(*otherExpression)); |
| 643 | if (!b->tryInsertExpression(iter, target)) { |
| 644 | *outNeedsRescan = true; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 651 | * left to yield vec<n>(x). |
| 652 | */ |
| 653 | static void vectorize_left(BasicBlock* b, |
| 654 | std::vector<BasicBlock::Node>::iterator* iter, |
| 655 | bool* outUpdated, |
| 656 | bool* outNeedsRescan) { |
| 657 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 658 | vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 663 | * right to yield vec<n>(y). |
| 664 | */ |
| 665 | static void vectorize_right(BasicBlock* b, |
| 666 | std::vector<BasicBlock::Node>::iterator* iter, |
| 667 | bool* outUpdated, |
| 668 | bool* outNeedsRescan) { |
| 669 | BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression(); |
| 670 | vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan); |
| 671 | } |
| 672 | |
| 673 | // Mark that an expression which we were writing to is no longer being written to |
| 674 | void clear_write(const Expression& expr) { |
| 675 | switch (expr.fKind) { |
| 676 | case Expression::kVariableReference_Kind: { |
| 677 | ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind); |
| 678 | break; |
| 679 | } |
| 680 | case Expression::kFieldAccess_Kind: |
| 681 | clear_write(*((FieldAccess&) expr).fBase); |
| 682 | break; |
| 683 | case Expression::kSwizzle_Kind: |
| 684 | clear_write(*((Swizzle&) expr).fBase); |
| 685 | break; |
| 686 | case Expression::kIndex_Kind: |
| 687 | clear_write(*((IndexExpression&) expr).fBase); |
| 688 | break; |
| 689 | default: |
| 690 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 695 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 696 | BasicBlock& b, |
| 697 | std::vector<BasicBlock::Node>::iterator* iter, |
| 698 | std::unordered_set<const Variable*>* undefinedVariables, |
| 699 | bool* outUpdated, |
| 700 | bool* outNeedsRescan) { |
| 701 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 702 | SkASSERT(expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 703 | if ((*iter)->fConstantPropagation) { |
| 704 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 705 | if (optimized) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 706 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 707 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 708 | *outNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 709 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 710 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 711 | SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 712 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | switch (expr->fKind) { |
| 716 | case Expression::kVariableReference_Kind: { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 717 | const VariableReference& ref = (VariableReference&) *expr; |
| 718 | const Variable& var = ref.fVariable; |
| 719 | if (ref.refKind() != VariableReference::kWrite_RefKind && |
| 720 | ref.refKind() != VariableReference::kPointer_RefKind && |
| 721 | var.fStorage == Variable::kLocal_Storage && !definitions[&var] && |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 722 | (*undefinedVariables).find(&var) == (*undefinedVariables).end()) { |
| 723 | (*undefinedVariables).insert(&var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 724 | this->error(expr->fOffset, |
| 725 | "'" + var.fName + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 726 | } |
| 727 | break; |
| 728 | } |
| 729 | case Expression::kTernary_Kind: { |
| 730 | TernaryExpression* t = (TernaryExpression*) expr; |
| 731 | if (t->fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 732 | // ternary has a constant test, replace it with either the true or |
| 733 | // false branch |
| 734 | if (((BoolLiteral&) *t->fTest).fValue) { |
| 735 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 736 | } else { |
| 737 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 738 | } |
| 739 | *outUpdated = true; |
| 740 | *outNeedsRescan = true; |
| 741 | } |
| 742 | break; |
| 743 | } |
| 744 | case Expression::kBinary_Kind: { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 745 | BinaryExpression* bin = (BinaryExpression*) expr; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 746 | if (dead_assignment(*bin)) { |
| 747 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 748 | break; |
| 749 | } |
| 750 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 751 | if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) && |
| 752 | (bin->fLeft->fType.kind() != Type::kVector_Kind)) || |
| 753 | ((bin->fRight->fType.kind() != Type::kScalar_Kind) && |
| 754 | (bin->fRight->fType.kind() != Type::kVector_Kind))) { |
| 755 | break; |
| 756 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 757 | switch (bin->fOperator) { |
| 758 | case Token::STAR: |
| 759 | if (is_constant(*bin->fLeft, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 760 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 761 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 762 | // float4(1) * x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 763 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 764 | } else { |
| 765 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 766 | // 1 * float4(x) -> float4(x) |
| 767 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 768 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 769 | } |
| 770 | } |
| 771 | else if (is_constant(*bin->fLeft, 0)) { |
| 772 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 773 | bin->fRight->fType.kind() == Type::kVector_Kind && |
| 774 | !bin->fRight->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 775 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 776 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 777 | } else { |
| 778 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 779 | // float4(0) * x -> float4(0) |
| 780 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 781 | if (!bin->fRight->hasSideEffects()) { |
| 782 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 783 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 784 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 785 | } |
| 786 | else if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 787 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 788 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 789 | // x * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 790 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 791 | } else { |
| 792 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 793 | // float4(x) * 1 -> float4(x) |
| 794 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 795 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 796 | } |
| 797 | } |
| 798 | else if (is_constant(*bin->fRight, 0)) { |
| 799 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 800 | bin->fRight->fType.kind() == Type::kScalar_Kind && |
| 801 | !bin->fLeft->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 802 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 803 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 804 | } else { |
| 805 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 806 | // x * float4(0) -> float4(0) |
| 807 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 808 | if (!bin->fLeft->hasSideEffects()) { |
| 809 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 810 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 811 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 812 | } |
| 813 | break; |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 814 | case Token::PLUS: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 815 | if (is_constant(*bin->fLeft, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 816 | if (bin->fLeft->fType.kind() == Type::kVector_Kind && |
| 817 | bin->fRight->fType.kind() == Type::kScalar_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 818 | // float4(0) + x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 819 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 820 | } else { |
| 821 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 822 | // 0 + float4(x) -> float4(x) |
| 823 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 824 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 825 | } |
| 826 | } else if (is_constant(*bin->fRight, 0)) { |
| 827 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 828 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 829 | // x + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 830 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 831 | } else { |
| 832 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 833 | // float4(x) + 0 -> float4(x) |
| 834 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 835 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 836 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 837 | } |
| 838 | break; |
| 839 | case Token::MINUS: |
| 840 | if (is_constant(*bin->fRight, 0)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 841 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 842 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 843 | // x - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 844 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 845 | } else { |
| 846 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 847 | // float4(x) - 0 -> float4(x) |
| 848 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 849 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 850 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 851 | } |
| 852 | break; |
| 853 | case Token::SLASH: |
| 854 | if (is_constant(*bin->fRight, 1)) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 855 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
| 856 | bin->fRight->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 857 | // x / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 858 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 859 | } else { |
| 860 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 861 | // float4(x) / 1 -> float4(x) |
| 862 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 863 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 864 | } |
| 865 | } else if (is_constant(*bin->fLeft, 0)) { |
| 866 | if (bin->fLeft->fType.kind() == Type::kScalar_Kind && |
Ethan Nicholas | 08dae92 | 2018-01-23 10:31:56 -0500 | [diff] [blame] | 867 | bin->fRight->fType.kind() == Type::kVector_Kind && |
| 868 | !bin->fRight->hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 869 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 870 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 871 | } else { |
| 872 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 873 | // float4(0) / x -> float4(0) |
| 874 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 875 | if (!bin->fRight->hasSideEffects()) { |
| 876 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 877 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | break; |
| 881 | case Token::PLUSEQ: |
| 882 | if (is_constant(*bin->fRight, 0)) { |
| 883 | clear_write(*bin->fLeft); |
| 884 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 885 | } |
| 886 | break; |
| 887 | case Token::MINUSEQ: |
| 888 | if (is_constant(*bin->fRight, 0)) { |
| 889 | clear_write(*bin->fLeft); |
| 890 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 891 | } |
| 892 | break; |
| 893 | case Token::STAREQ: |
| 894 | if (is_constant(*bin->fRight, 1)) { |
| 895 | clear_write(*bin->fLeft); |
| 896 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 897 | } |
| 898 | break; |
| 899 | case Token::SLASHEQ: |
| 900 | if (is_constant(*bin->fRight, 1)) { |
| 901 | clear_write(*bin->fLeft); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 902 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 903 | } |
| 904 | break; |
| 905 | default: |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | default: |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 914 | // returns true if this statement could potentially execute a break at the current level (we ignore |
| 915 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
| 916 | static bool contains_break(Statement& s) { |
| 917 | switch (s.fKind) { |
| 918 | case Statement::kBlock_Kind: |
| 919 | for (const auto& sub : ((Block&) s).fStatements) { |
| 920 | if (contains_break(*sub)) { |
| 921 | return true; |
| 922 | } |
| 923 | } |
| 924 | return false; |
| 925 | case Statement::kBreak_Kind: |
| 926 | return true; |
| 927 | case Statement::kIf_Kind: { |
| 928 | const IfStatement& i = (IfStatement&) s; |
| 929 | return contains_break(*i.fIfTrue) || (i.fIfFalse && contains_break(*i.fIfFalse)); |
| 930 | } |
| 931 | default: |
| 932 | return false; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | // Returns a block containing all of the statements that will be run if the given case matches |
| 937 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 938 | // broken by this call and must then be discarded). |
| 939 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 940 | // when break statements appear inside conditionals. |
| 941 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* s, SwitchCase* c) { |
| 942 | bool capturing = false; |
| 943 | std::vector<std::unique_ptr<Statement>*> statementPtrs; |
| 944 | for (const auto& current : s->fCases) { |
| 945 | if (current.get() == c) { |
| 946 | capturing = true; |
| 947 | } |
| 948 | if (capturing) { |
| 949 | for (auto& stmt : current->fStatements) { |
| 950 | if (stmt->fKind == Statement::kBreak_Kind) { |
| 951 | capturing = false; |
| 952 | break; |
| 953 | } |
| 954 | if (contains_break(*stmt)) { |
| 955 | return nullptr; |
| 956 | } |
| 957 | statementPtrs.push_back(&stmt); |
| 958 | } |
| 959 | if (!capturing) { |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | std::vector<std::unique_ptr<Statement>> statements; |
| 965 | for (const auto& s : statementPtrs) { |
| 966 | statements.push_back(std::move(*s)); |
| 967 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 968 | return std::unique_ptr<Statement>(new Block(-1, std::move(statements), s->fSymbols)); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 969 | } |
| 970 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 971 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 972 | BasicBlock& b, |
| 973 | std::vector<BasicBlock::Node>::iterator* iter, |
| 974 | std::unordered_set<const Variable*>* undefinedVariables, |
| 975 | bool* outUpdated, |
| 976 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 977 | Statement* stmt = (*iter)->statement()->get(); |
| 978 | switch (stmt->fKind) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 979 | case Statement::kVarDeclaration_Kind: { |
| 980 | const auto& varDecl = (VarDeclaration&) *stmt; |
| 981 | if (varDecl.fVar->dead() && |
| 982 | (!varDecl.fValue || |
| 983 | !varDecl.fValue->hasSideEffects())) { |
| 984 | if (varDecl.fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 985 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 986 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 987 | *outNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 988 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 989 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 990 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 991 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 992 | } |
| 993 | break; |
| 994 | } |
| 995 | case Statement::kIf_Kind: { |
| 996 | IfStatement& i = (IfStatement&) *stmt; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 997 | if (i.fTest->fKind == Expression::kBoolLiteral_Kind) { |
| 998 | // constant if, collapse down to a single branch |
| 999 | if (((BoolLiteral&) *i.fTest).fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1000 | SkASSERT(i.fIfTrue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1001 | (*iter)->setStatement(std::move(i.fIfTrue)); |
| 1002 | } else { |
| 1003 | if (i.fIfFalse) { |
| 1004 | (*iter)->setStatement(std::move(i.fIfFalse)); |
| 1005 | } else { |
| 1006 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1007 | } |
| 1008 | } |
| 1009 | *outUpdated = true; |
| 1010 | *outNeedsRescan = true; |
| 1011 | break; |
| 1012 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1013 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 1014 | // else block doesn't do anything, remove it |
| 1015 | i.fIfFalse.reset(); |
| 1016 | *outUpdated = true; |
| 1017 | *outNeedsRescan = true; |
| 1018 | } |
| 1019 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 1020 | // if block doesn't do anything, no else block |
| 1021 | if (i.fTest->hasSideEffects()) { |
| 1022 | // test has side effects, keep it |
| 1023 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 1024 | new ExpressionStatement(std::move(i.fTest)))); |
| 1025 | } else { |
| 1026 | // no if, no else, no test side effects, kill the whole if |
| 1027 | // statement |
| 1028 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1029 | } |
| 1030 | *outUpdated = true; |
| 1031 | *outNeedsRescan = true; |
| 1032 | } |
| 1033 | break; |
| 1034 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1035 | case Statement::kSwitch_Kind: { |
| 1036 | SwitchStatement& s = (SwitchStatement&) *stmt; |
| 1037 | if (s.fValue->isConstant()) { |
| 1038 | // switch is constant, replace it with the case that matches |
| 1039 | bool found = false; |
| 1040 | SwitchCase* defaultCase = nullptr; |
| 1041 | for (const auto& c : s.fCases) { |
| 1042 | if (!c->fValue) { |
| 1043 | defaultCase = c.get(); |
| 1044 | continue; |
| 1045 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1046 | SkASSERT(c->fValue->fKind == s.fValue->fKind); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1047 | found = c->fValue->compareConstant(*fContext, *s.fValue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1048 | if (found) { |
| 1049 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
| 1050 | if (newBlock) { |
| 1051 | (*iter)->setStatement(std::move(newBlock)); |
| 1052 | break; |
| 1053 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1054 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1055 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1056 | "static switch contains non-static conditional break"); |
| 1057 | s.fIsStatic = false; |
| 1058 | } |
| 1059 | return; // can't simplify |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | if (!found) { |
| 1064 | // no matching case. use default if it exists, or kill the whole thing |
| 1065 | if (defaultCase) { |
| 1066 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 1067 | if (newBlock) { |
| 1068 | (*iter)->setStatement(std::move(newBlock)); |
| 1069 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1070 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1071 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1072 | "static switch contains non-static conditional break"); |
| 1073 | s.fIsStatic = false; |
| 1074 | } |
| 1075 | return; // can't simplify |
| 1076 | } |
| 1077 | } else { |
| 1078 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1079 | } |
| 1080 | } |
| 1081 | *outUpdated = true; |
| 1082 | *outNeedsRescan = true; |
| 1083 | } |
| 1084 | break; |
| 1085 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1086 | case Statement::kExpression_Kind: { |
| 1087 | ExpressionStatement& e = (ExpressionStatement&) *stmt; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1088 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1089 | if (!e.fExpression->hasSideEffects()) { |
| 1090 | // Expression statement with no side effects, kill it |
| 1091 | if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) { |
| 1092 | *outNeedsRescan = true; |
| 1093 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1094 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1095 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1096 | *outUpdated = true; |
| 1097 | } |
| 1098 | break; |
| 1099 | } |
| 1100 | default: |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void Compiler::scanCFG(FunctionDefinition& f) { |
| 1106 | CFG cfg = CFGGenerator().getCFG(f); |
| 1107 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1108 | |
| 1109 | // check for unreachable code |
| 1110 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 1111 | if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() && |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1112 | cfg.fBlocks[i].fNodes.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1113 | int offset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1114 | switch (cfg.fBlocks[i].fNodes[0].fKind) { |
| 1115 | case BasicBlock::Node::kStatement_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1116 | offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1117 | break; |
| 1118 | case BasicBlock::Node::kExpression_Kind: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1119 | offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1120 | break; |
| 1121 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1122 | this->error(offset, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | if (fErrorCount) { |
| 1126 | return; |
| 1127 | } |
| 1128 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1129 | // check for dead code & undefined variables, perform constant propagation |
| 1130 | std::unordered_set<const Variable*> undefinedVariables; |
| 1131 | bool updated; |
| 1132 | bool needsRescan = false; |
| 1133 | do { |
| 1134 | if (needsRescan) { |
| 1135 | cfg = CFGGenerator().getCFG(f); |
| 1136 | this->computeDataFlow(&cfg); |
| 1137 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1138 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1139 | |
| 1140 | updated = false; |
| 1141 | for (BasicBlock& b : cfg.fBlocks) { |
| 1142 | DefinitionMap definitions = b.fBefore; |
| 1143 | |
| 1144 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
| 1145 | if (iter->fKind == BasicBlock::Node::kExpression_Kind) { |
| 1146 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 1147 | &needsRescan); |
| 1148 | } else { |
| 1149 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
| 1150 | &needsRescan); |
| 1151 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1152 | if (needsRescan) { |
| 1153 | break; |
| 1154 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1155 | this->addDefinitions(*iter, &definitions); |
| 1156 | } |
| 1157 | } |
| 1158 | } while (updated); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1159 | SkASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1160 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1161 | // verify static ifs & switches, clean up dead variable decls |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1162 | for (BasicBlock& b : cfg.fBlocks) { |
| 1163 | DefinitionMap definitions = b.fBefore; |
| 1164 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1165 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1166 | if (iter->fKind == BasicBlock::Node::kStatement_Kind) { |
| 1167 | const Statement& s = **iter->statement(); |
| 1168 | switch (s.fKind) { |
| 1169 | case Statement::kIf_Kind: |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1170 | if (((const IfStatement&) s).fIsStatic && |
| 1171 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1172 | this->error(s.fOffset, "static if has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1173 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1174 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1175 | break; |
| 1176 | case Statement::kSwitch_Kind: |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1177 | if (((const SwitchStatement&) s).fIsStatic && |
| 1178 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1179 | this->error(s.fOffset, "static switch has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1180 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1181 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1182 | break; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1183 | case Statement::kVarDeclarations_Kind: { |
| 1184 | VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration; |
| 1185 | for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) { |
| 1186 | if ((*varIter)->fKind == Statement::kNop_Kind) { |
| 1187 | varIter = decls.fVars.erase(varIter); |
| 1188 | } else { |
| 1189 | ++varIter; |
| 1190 | } |
| 1191 | } |
| 1192 | if (!decls.fVars.size()) { |
| 1193 | iter = b.fNodes.erase(iter); |
| 1194 | } else { |
| 1195 | ++iter; |
| 1196 | } |
| 1197 | break; |
| 1198 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1199 | default: |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1200 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1201 | break; |
| 1202 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1203 | } else { |
| 1204 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1209 | // check for missing return |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1210 | if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1211 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1212 | this->error(f.fOffset, String("function can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1217 | std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1218 | const Program::Settings& settings) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1219 | fErrorText = ""; |
| 1220 | fErrorCount = 0; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1221 | std::vector<std::unique_ptr<ProgramElement>>* inherited; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1222 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1223 | switch (kind) { |
| 1224 | case Program::kVertex_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1225 | inherited = &fVertexInclude; |
| 1226 | fIRGenerator->fSymbolTable = fVertexSymbolTable; |
| 1227 | fIRGenerator->start(&settings, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1228 | break; |
| 1229 | case Program::kFragment_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1230 | inherited = &fFragmentInclude; |
| 1231 | fIRGenerator->fSymbolTable = fFragmentSymbolTable; |
| 1232 | fIRGenerator->start(&settings, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1233 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1234 | case Program::kGeometry_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1235 | inherited = &fGeometryInclude; |
| 1236 | fIRGenerator->fSymbolTable = fGeometrySymbolTable; |
| 1237 | fIRGenerator->start(&settings, inherited); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1238 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1239 | case Program::kFragmentProcessor_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1240 | inherited = nullptr; |
| 1241 | fIRGenerator->start(&settings, nullptr); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1242 | fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), *fTypes, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1243 | &elements); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1244 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1245 | break; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1246 | case Program::kCPU_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1247 | inherited = nullptr; |
| 1248 | fIRGenerator->start(&settings, nullptr); |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1249 | fIRGenerator->convertProgram(kind, SKSL_CPU_INCLUDE, strlen(SKSL_CPU_INCLUDE), |
| 1250 | *fTypes, &elements); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1251 | fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1252 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1253 | } |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1254 | for (auto& element : elements) { |
| 1255 | if (element->fKind == ProgramElement::kEnum_Kind) { |
| 1256 | ((Enum&) *element).fBuiltin = true; |
| 1257 | } |
| 1258 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1259 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1260 | fSource = textPtr.get(); |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 1261 | fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), *fTypes, &elements); |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1262 | if (!fErrorCount) { |
| 1263 | for (auto& element : elements) { |
| 1264 | if (element->fKind == ProgramElement::kFunction_Kind) { |
| 1265 | this->scanCFG((FunctionDefinition&) *element); |
| 1266 | } |
| 1267 | } |
| 1268 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1269 | auto result = std::unique_ptr<Program>(new Program(kind, |
| 1270 | std::move(textPtr), |
| 1271 | settings, |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1272 | fContext, |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1273 | inherited, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1274 | std::move(elements), |
| 1275 | fIRGenerator->fSymbolTable, |
| 1276 | fIRGenerator->fInputs)); |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1277 | fIRGenerator->finish(); |
| 1278 | fSource = nullptr; |
| 1279 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1280 | if (fErrorCount) { |
| 1281 | return nullptr; |
| 1282 | } |
| 1283 | return result; |
| 1284 | } |
| 1285 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1286 | bool Compiler::toSPIRV(const Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1287 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1288 | StringStream buffer; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1289 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1290 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1291 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1292 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1293 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1294 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1295 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1296 | SkASSERT(0 == data.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1297 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1298 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1299 | }; |
| 1300 | tools.SetMessageConsumer(dumpmsg); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1301 | // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1302 | // to the failure to see the validation errors. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1303 | SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1304 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1305 | } |
| 1306 | #else |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1307 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1308 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1309 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1310 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1311 | #endif |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1312 | this->writeErrorCount(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1313 | return result; |
| 1314 | } |
| 1315 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1316 | bool Compiler::toSPIRV(const Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1317 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1318 | bool result = this->toSPIRV(program, buffer); |
| 1319 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1320 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1321 | } |
| 1322 | return result; |
| 1323 | } |
| 1324 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1325 | bool Compiler::toGLSL(const Program& program, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1326 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1327 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1328 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1329 | fSource = nullptr; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1330 | this->writeErrorCount(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1331 | return result; |
| 1332 | } |
| 1333 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1334 | bool Compiler::toGLSL(const Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1335 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1336 | bool result = this->toGLSL(program, buffer); |
| 1337 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1338 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1339 | } |
| 1340 | return result; |
| 1341 | } |
| 1342 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1343 | bool Compiler::toMetal(const Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1344 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1345 | bool result = cg.generateCode(); |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1346 | this->writeErrorCount(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1347 | return result; |
| 1348 | } |
| 1349 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1350 | bool Compiler::toCPP(const Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1351 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1352 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1353 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1354 | fSource = nullptr; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1355 | this->writeErrorCount(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1356 | return result; |
| 1357 | } |
| 1358 | |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1359 | bool Compiler::toH(const Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1360 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1361 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1362 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1363 | fSource = nullptr; |
Kevin Lubick | f203078 | 2018-06-19 12:04:18 +0000 | [diff] [blame] | 1364 | this->writeErrorCount(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1365 | return result; |
| 1366 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1367 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1368 | const char* Compiler::OperatorName(Token::Kind kind) { |
| 1369 | switch (kind) { |
| 1370 | case Token::PLUS: return "+"; |
| 1371 | case Token::MINUS: return "-"; |
| 1372 | case Token::STAR: return "*"; |
| 1373 | case Token::SLASH: return "/"; |
| 1374 | case Token::PERCENT: return "%"; |
| 1375 | case Token::SHL: return "<<"; |
| 1376 | case Token::SHR: return ">>"; |
| 1377 | case Token::LOGICALNOT: return "!"; |
| 1378 | case Token::LOGICALAND: return "&&"; |
| 1379 | case Token::LOGICALOR: return "||"; |
| 1380 | case Token::LOGICALXOR: return "^^"; |
| 1381 | case Token::BITWISENOT: return "~"; |
| 1382 | case Token::BITWISEAND: return "&"; |
| 1383 | case Token::BITWISEOR: return "|"; |
| 1384 | case Token::BITWISEXOR: return "^"; |
| 1385 | case Token::EQ: return "="; |
| 1386 | case Token::EQEQ: return "=="; |
| 1387 | case Token::NEQ: return "!="; |
| 1388 | case Token::LT: return "<"; |
| 1389 | case Token::GT: return ">"; |
| 1390 | case Token::LTEQ: return "<="; |
| 1391 | case Token::GTEQ: return ">="; |
| 1392 | case Token::PLUSEQ: return "+="; |
| 1393 | case Token::MINUSEQ: return "-="; |
| 1394 | case Token::STAREQ: return "*="; |
| 1395 | case Token::SLASHEQ: return "/="; |
| 1396 | case Token::PERCENTEQ: return "%="; |
| 1397 | case Token::SHLEQ: return "<<="; |
| 1398 | case Token::SHREQ: return ">>="; |
| 1399 | case Token::LOGICALANDEQ: return "&&="; |
| 1400 | case Token::LOGICALOREQ: return "||="; |
| 1401 | case Token::LOGICALXOREQ: return "^^="; |
| 1402 | case Token::BITWISEANDEQ: return "&="; |
| 1403 | case Token::BITWISEOREQ: return "|="; |
| 1404 | case Token::BITWISEXOREQ: return "^="; |
| 1405 | case Token::PLUSPLUS: return "++"; |
| 1406 | case Token::MINUSMINUS: return "--"; |
| 1407 | case Token::COMMA: return ","; |
| 1408 | default: |
| 1409 | ABORT("unsupported operator: %d\n", kind); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | bool Compiler::IsAssignment(Token::Kind op) { |
| 1415 | switch (op) { |
| 1416 | case Token::EQ: // fall through |
| 1417 | case Token::PLUSEQ: // fall through |
| 1418 | case Token::MINUSEQ: // fall through |
| 1419 | case Token::STAREQ: // fall through |
| 1420 | case Token::SLASHEQ: // fall through |
| 1421 | case Token::PERCENTEQ: // fall through |
| 1422 | case Token::SHLEQ: // fall through |
| 1423 | case Token::SHREQ: // fall through |
| 1424 | case Token::BITWISEOREQ: // fall through |
| 1425 | case Token::BITWISEXOREQ: // fall through |
| 1426 | case Token::BITWISEANDEQ: // fall through |
| 1427 | case Token::LOGICALOREQ: // fall through |
| 1428 | case Token::LOGICALXOREQ: // fall through |
| 1429 | case Token::LOGICALANDEQ: |
| 1430 | return true; |
| 1431 | default: |
| 1432 | return false; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | Position Compiler::position(int offset) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1437 | SkASSERT(fSource); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1438 | int line = 1; |
| 1439 | int column = 1; |
| 1440 | for (int i = 0; i < offset; i++) { |
| 1441 | if ((*fSource)[i] == '\n') { |
| 1442 | ++line; |
| 1443 | column = 1; |
| 1444 | } |
| 1445 | else { |
| 1446 | ++column; |
| 1447 | } |
| 1448 | } |
| 1449 | return Position(line, column); |
| 1450 | } |
| 1451 | |
| 1452 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1453 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1454 | Position pos = this->position(offset); |
| 1455 | fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1458 | String Compiler::errorText() { |
| 1459 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1460 | return result; |
| 1461 | } |
| 1462 | |
| 1463 | void Compiler::writeErrorCount() { |
| 1464 | if (fErrorCount) { |
| 1465 | fErrorText += to_string(fErrorCount) + " error"; |
| 1466 | if (fErrorCount > 1) { |
| 1467 | fErrorText += "s"; |
| 1468 | } |
| 1469 | fErrorText += "\n"; |
| 1470 | } |
| 1471 | } |
| 1472 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1473 | } // namespace |