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