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 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLByteCodeGenerator.h" |
| 15 | #include "src/sksl/SkSLCFGGenerator.h" |
| 16 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
| 17 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 18 | #include "src/sksl/SkSLHCodeGenerator.h" |
| 19 | #include "src/sksl/SkSLIRGenerator.h" |
| 20 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
| 21 | #include "src/sksl/SkSLPipelineStageCodeGenerator.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 22 | #include "src/sksl/SkSLRehydrator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 24 | #include "src/sksl/SkSLSPIRVtoHLSL.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLEnum.h" |
| 26 | #include "src/sksl/ir/SkSLExpression.h" |
| 27 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 28 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 29 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 30 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 31 | #include "src/sksl/ir/SkSLNop.h" |
| 32 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 33 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 34 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 35 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 37 | #include <fstream> |
| 38 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 39 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 40 | #include "include/gpu/GrContextOptions.h" |
| 41 | #include "src/gpu/GrShaderCaps.h" |
| 42 | #endif |
| 43 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 44 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 45 | #include "spirv-tools/libspirv.hpp" |
| 46 | #endif |
| 47 | |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 48 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 49 | |
| 50 | #include "src/sksl/generated/sksl_fp.dehydrated.sksl" |
| 51 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
| 52 | #include "src/sksl/generated/sksl_geom.dehydrated.sksl" |
| 53 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
| 54 | #include "src/sksl/generated/sksl_interp.dehydrated.sksl" |
| 55 | #include "src/sksl/generated/sksl_pipeline.dehydrated.sksl" |
| 56 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 57 | |
| 58 | #else |
| 59 | |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 60 | // GN generates or copies all of these files to the skslc executable directory |
| 61 | static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl"; |
| 62 | static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl"; |
| 63 | static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl"; |
| 64 | static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl"; |
| 65 | static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl"; |
| 66 | static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl"; |
| 67 | static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl"; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 68 | |
| 69 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 70 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | namespace SkSL { |
| 72 | |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 73 | static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src, |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 74 | IRIntrinsicMap* target) { |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 75 | for (auto iter = src->begin(); iter != src->end(); ) { |
| 76 | std::unique_ptr<ProgramElement>& element = *iter; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 77 | switch (element->kind()) { |
| 78 | case ProgramElement::Kind::kFunction: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 79 | FunctionDefinition& f = element->as<FunctionDefinition>(); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 80 | SkASSERT(f.fDeclaration.fBuiltin); |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 81 | target->insertOrDie(f.fDeclaration.description(), std::move(element)); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 82 | iter = src->erase(iter); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 83 | break; |
| 84 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 85 | case ProgramElement::Kind::kEnum: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 86 | Enum& e = element->as<Enum>(); |
Ethan Nicholas | d83ded8 | 2020-09-29 17:05:54 -0400 | [diff] [blame] | 87 | target->insertOrDie(e.typeName(), std::move(element)); |
Brian Osman | 08f986d | 2020-05-13 17:06:46 -0400 | [diff] [blame] | 88 | iter = src->erase(iter); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 89 | break; |
| 90 | } |
| 91 | default: |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 92 | // Unsupported element, leave it in the list. |
| 93 | ++iter; |
| 94 | break; |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 99 | static void reset_call_counts(std::vector<std::unique_ptr<ProgramElement>>* src) { |
| 100 | for (std::unique_ptr<ProgramElement>& element : *src) { |
| 101 | if (element->is<FunctionDefinition>()) { |
| 102 | const FunctionDeclaration& fnDecl = element->as<FunctionDefinition>().fDeclaration; |
| 103 | fnDecl.fCallCount = 0; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 108 | Compiler::Compiler(Flags flags) |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 109 | : fGPUIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr)) |
| 110 | , fInterpreterIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr)) |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 111 | , fFlags(flags) |
| 112 | , fContext(std::make_shared<Context>()) |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 113 | , fErrorCount(0) { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 114 | fRootSymbolTable = std::make_shared<SymbolTable>(this); |
John Stiles | 941fc71 | 2020-09-19 12:47:10 +0000 | [diff] [blame] | 115 | fIRGenerator = |
| 116 | std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 117 | #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->fName, \ |
| 118 | fContext->f ## t ## _Type.get()) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | ADD_TYPE(Void); |
| 120 | ADD_TYPE(Float); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 121 | ADD_TYPE(Float2); |
| 122 | ADD_TYPE(Float3); |
| 123 | ADD_TYPE(Float4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 124 | ADD_TYPE(Half); |
| 125 | ADD_TYPE(Half2); |
| 126 | ADD_TYPE(Half3); |
| 127 | ADD_TYPE(Half4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 128 | ADD_TYPE(Int); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 129 | ADD_TYPE(Int2); |
| 130 | ADD_TYPE(Int3); |
| 131 | ADD_TYPE(Int4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 132 | ADD_TYPE(UInt); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 133 | ADD_TYPE(UInt2); |
| 134 | ADD_TYPE(UInt3); |
| 135 | ADD_TYPE(UInt4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 136 | ADD_TYPE(Short); |
| 137 | ADD_TYPE(Short2); |
| 138 | ADD_TYPE(Short3); |
| 139 | ADD_TYPE(Short4); |
| 140 | ADD_TYPE(UShort); |
| 141 | ADD_TYPE(UShort2); |
| 142 | ADD_TYPE(UShort3); |
| 143 | ADD_TYPE(UShort4); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 144 | ADD_TYPE(Byte); |
| 145 | ADD_TYPE(Byte2); |
| 146 | ADD_TYPE(Byte3); |
| 147 | ADD_TYPE(Byte4); |
| 148 | ADD_TYPE(UByte); |
| 149 | ADD_TYPE(UByte2); |
| 150 | ADD_TYPE(UByte3); |
| 151 | ADD_TYPE(UByte4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | ADD_TYPE(Bool); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 153 | ADD_TYPE(Bool2); |
| 154 | ADD_TYPE(Bool3); |
| 155 | ADD_TYPE(Bool4); |
| 156 | ADD_TYPE(Float2x2); |
| 157 | ADD_TYPE(Float2x3); |
| 158 | ADD_TYPE(Float2x4); |
| 159 | ADD_TYPE(Float3x2); |
| 160 | ADD_TYPE(Float3x3); |
| 161 | ADD_TYPE(Float3x4); |
| 162 | ADD_TYPE(Float4x2); |
| 163 | ADD_TYPE(Float4x3); |
| 164 | ADD_TYPE(Float4x4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 165 | ADD_TYPE(Half2x2); |
| 166 | ADD_TYPE(Half2x3); |
| 167 | ADD_TYPE(Half2x4); |
| 168 | ADD_TYPE(Half3x2); |
| 169 | ADD_TYPE(Half3x3); |
| 170 | ADD_TYPE(Half3x4); |
| 171 | ADD_TYPE(Half4x2); |
| 172 | ADD_TYPE(Half4x3); |
| 173 | ADD_TYPE(Half4x4); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | ADD_TYPE(GenType); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 175 | ADD_TYPE(GenHType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | ADD_TYPE(GenIType); |
| 177 | ADD_TYPE(GenUType); |
| 178 | ADD_TYPE(GenBType); |
| 179 | ADD_TYPE(Mat); |
| 180 | ADD_TYPE(Vec); |
| 181 | ADD_TYPE(GVec); |
| 182 | ADD_TYPE(GVec2); |
| 183 | ADD_TYPE(GVec3); |
| 184 | ADD_TYPE(GVec4); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 185 | ADD_TYPE(HVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | ADD_TYPE(IVec); |
| 187 | ADD_TYPE(UVec); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 188 | ADD_TYPE(SVec); |
| 189 | ADD_TYPE(USVec); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 190 | ADD_TYPE(ByteVec); |
| 191 | ADD_TYPE(UByteVec); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 | ADD_TYPE(BVec); |
| 193 | |
| 194 | ADD_TYPE(Sampler1D); |
| 195 | ADD_TYPE(Sampler2D); |
| 196 | ADD_TYPE(Sampler3D); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 197 | ADD_TYPE(SamplerExternalOES); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 198 | ADD_TYPE(SamplerCube); |
| 199 | ADD_TYPE(Sampler2DRect); |
| 200 | ADD_TYPE(Sampler1DArray); |
| 201 | ADD_TYPE(Sampler2DArray); |
| 202 | ADD_TYPE(SamplerCubeArray); |
| 203 | ADD_TYPE(SamplerBuffer); |
| 204 | ADD_TYPE(Sampler2DMS); |
| 205 | ADD_TYPE(Sampler2DMSArray); |
| 206 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 207 | ADD_TYPE(ISampler2D); |
| 208 | |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 209 | ADD_TYPE(Image2D); |
| 210 | ADD_TYPE(IImage2D); |
| 211 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 212 | ADD_TYPE(SubpassInput); |
| 213 | ADD_TYPE(SubpassInputMS); |
| 214 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 215 | ADD_TYPE(GSampler1D); |
| 216 | ADD_TYPE(GSampler2D); |
| 217 | ADD_TYPE(GSampler3D); |
| 218 | ADD_TYPE(GSamplerCube); |
| 219 | ADD_TYPE(GSampler2DRect); |
| 220 | ADD_TYPE(GSampler1DArray); |
| 221 | ADD_TYPE(GSampler2DArray); |
| 222 | ADD_TYPE(GSamplerCubeArray); |
| 223 | ADD_TYPE(GSamplerBuffer); |
| 224 | ADD_TYPE(GSampler2DMS); |
| 225 | ADD_TYPE(GSampler2DMSArray); |
| 226 | |
| 227 | ADD_TYPE(Sampler1DShadow); |
| 228 | ADD_TYPE(Sampler2DShadow); |
| 229 | ADD_TYPE(SamplerCubeShadow); |
| 230 | ADD_TYPE(Sampler2DRectShadow); |
| 231 | ADD_TYPE(Sampler1DArrayShadow); |
| 232 | ADD_TYPE(Sampler2DArrayShadow); |
| 233 | ADD_TYPE(SamplerCubeArrayShadow); |
| 234 | ADD_TYPE(GSampler2DArrayShadow); |
| 235 | ADD_TYPE(GSamplerCubeArrayShadow); |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 236 | ADD_TYPE(FragmentProcessor); |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 237 | ADD_TYPE(Sampler); |
| 238 | ADD_TYPE(Texture2D); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 239 | |
Brian Osman | 28590d5 | 2020-03-23 16:59:08 -0400 | [diff] [blame] | 240 | StringFragment fpAliasName("shader"); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 241 | fRootSymbolTable->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get()); |
Brian Osman | 28590d5 | 2020-03-23 16:59:08 -0400 | [diff] [blame] | 242 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame^] | 243 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 244 | // treat it as builtin (ie, no need to clone it into the Program). |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 245 | StringFragment skCapsName("sk_Caps"); |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame^] | 246 | fRootSymbolTable->add(skCapsName, |
| 247 | std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName, |
| 248 | fContext->fSkCaps_Type.get(), |
| 249 | /*builtin=*/false, Variable::kGlobal_Storage)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 250 | |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 251 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 252 | std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics; |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 253 | #if SKSL_STANDALONE |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 254 | this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable, |
| 255 | &gpuIntrinsics, &fGpuSymbolTable); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 256 | this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable, |
| 257 | &fVertexInclude, &fVertexSymbolTable); |
| 258 | this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable, |
| 259 | &fFragmentInclude, &fFragmentSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 260 | #else |
| 261 | { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 262 | Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu, |
| 263 | SKSL_INCLUDE_sksl_gpu_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 264 | fGpuSymbolTable = rehydrator.symbolTable(); |
| 265 | gpuIntrinsics = rehydrator.elements(); |
| 266 | } |
| 267 | { |
| 268 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert, |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 269 | SKSL_INCLUDE_sksl_vert_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 270 | fVertexSymbolTable = rehydrator.symbolTable(); |
| 271 | fVertexInclude = rehydrator.elements(); |
| 272 | } |
| 273 | { |
| 274 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag, |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 275 | SKSL_INCLUDE_sksl_frag_LENGTH); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 276 | fFragmentSymbolTable = rehydrator.symbolTable(); |
| 277 | fFragmentInclude = rehydrator.elements(); |
| 278 | } |
| 279 | #endif |
John Stiles | bc0c29e | 2020-09-28 13:13:40 -0400 | [diff] [blame] | 280 | // Call counts are used to track dead-stripping and inlinability within the program being |
| 281 | // currently compiled, and always should start at zero for a new program. Zero out any call |
| 282 | // counts that were registered during the assembly of the intrinsics/include data. (If we |
| 283 | // actually use calls from inside the intrinsics, we will clone them into the program and they |
| 284 | // will get new call counts.) |
| 285 | reset_call_counts(&gpuIntrinsics); |
| 286 | reset_call_counts(&fVertexInclude); |
| 287 | reset_call_counts(&fFragmentInclude); |
| 288 | |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 289 | grab_intrinsics(&gpuIntrinsics, fGPUIntrinsics.get()); |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 290 | SkASSERT(gpuIntrinsics.empty()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 291 | } |
| 292 | |
John Stiles | 656427a | 2020-08-27 15:26:26 -0400 | [diff] [blame] | 293 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 294 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 295 | void Compiler::loadGeometryIntrinsics() { |
| 296 | if (fGeometrySymbolTable) { |
| 297 | return; |
| 298 | } |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 299 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 300 | { |
| 301 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom, |
| 302 | SKSL_INCLUDE_sksl_geom_LENGTH); |
| 303 | fGeometrySymbolTable = rehydrator.symbolTable(); |
| 304 | fGeometryInclude = rehydrator.elements(); |
| 305 | } |
| 306 | #else |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 307 | this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable, |
| 308 | &fGeometryInclude, &fGeometrySymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | |
| 312 | void Compiler::loadPipelineIntrinsics() { |
| 313 | if (fPipelineSymbolTable) { |
| 314 | return; |
| 315 | } |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 316 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 317 | { |
| 318 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, |
| 319 | SKSL_INCLUDE_sksl_pipeline, |
| 320 | SKSL_INCLUDE_sksl_pipeline_LENGTH); |
| 321 | fPipelineSymbolTable = rehydrator.symbolTable(); |
| 322 | fPipelineInclude = rehydrator.elements(); |
| 323 | } |
| 324 | #else |
| 325 | this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE, |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 326 | fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 327 | #endif |
| 328 | } |
| 329 | |
| 330 | void Compiler::loadInterpreterIntrinsics() { |
| 331 | if (fInterpreterSymbolTable) { |
| 332 | return; |
| 333 | } |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 334 | std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics; |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 335 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 336 | { |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 337 | Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 338 | SKSL_INCLUDE_sksl_interp, |
| 339 | SKSL_INCLUDE_sksl_interp_LENGTH); |
| 340 | fInterpreterSymbolTable = rehydrator.symbolTable(); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 341 | interpIntrinsics = rehydrator.elements(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 342 | } |
| 343 | #else |
| 344 | this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE, |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 345 | fIRGenerator->fSymbolTable, &interpIntrinsics, |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 346 | &fInterpreterSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 347 | #endif |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 348 | grab_intrinsics(&interpIntrinsics, fInterpreterIntrinsics.get()); |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 349 | SkASSERT(interpIntrinsics.empty()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 352 | void Compiler::processIncludeFile(Program::Kind kind, const char* path, |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 353 | std::shared_ptr<SymbolTable> base, |
| 354 | std::vector<std::unique_ptr<ProgramElement>>* outElements, |
| 355 | std::shared_ptr<SymbolTable>* outSymbolTable) { |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 356 | std::ifstream in(path); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 357 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 358 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 359 | if (in.rdstate()) { |
| 360 | printf("error reading %s\n", path); |
| 361 | abort(); |
| 362 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 363 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 364 | fSource = source; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 365 | Program::Settings settings; |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 366 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 367 | GrContextOptions opts; |
| 368 | GrShaderCaps caps(opts); |
| 369 | settings.fCaps = ∩︀ |
| 370 | #endif |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 371 | SkASSERT(fIRGenerator->fCanInline); |
| 372 | fIRGenerator->fCanInline = false; |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 373 | fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 374 | fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements); |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 375 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 376 | if (this->fErrorCount) { |
| 377 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
| 378 | } |
| 379 | SkASSERT(!fErrorCount); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 380 | *outSymbolTable = fIRGenerator->fSymbolTable; |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 381 | #ifdef SK_DEBUG |
| 382 | fSource = nullptr; |
| 383 | #endif |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 384 | fIRGenerator->finish(); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 385 | } |
| 386 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 387 | // 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] | 388 | void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 389 | DefinitionMap* definitions) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 390 | switch (lvalue->kind()) { |
| 391 | case Expression::Kind::kVariableReference: { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 392 | const Variable& var = *lvalue->as<VariableReference>().fVariable; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 393 | if (var.fStorage == Variable::kLocal_Storage) { |
| 394 | (*definitions)[&var] = expr; |
| 395 | } |
| 396 | break; |
| 397 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 398 | case Expression::Kind::kSwizzle: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 399 | // We consider the variable written to as long as at least some of its components have |
| 400 | // been written to. This will lead to some false negatives (we won't catch it if you |
| 401 | // 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] | 402 | // (we write to foo.x, and then pass foo to a function which happens to only read foo.x, |
| 403 | // 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] | 404 | // more complicated whole-program analysis. This is probably good enough. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 405 | this->addDefinition(lvalue->as<Swizzle>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 406 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 407 | definitions); |
| 408 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 409 | case Expression::Kind::kIndex: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 410 | // see comments in Swizzle |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 411 | this->addDefinition(lvalue->as<IndexExpression>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 412 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 413 | definitions); |
| 414 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 415 | case Expression::Kind::kFieldAccess: |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 416 | // see comments in Swizzle |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 417 | this->addDefinition(lvalue->as<FieldAccess>().fBase.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 418 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 419 | definitions); |
| 420 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 421 | case Expression::Kind::kTernary: |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 422 | // To simplify analysis, we just pretend that we write to both sides of the ternary. |
| 423 | // This allows for false positives (meaning we fail to detect that a variable might not |
| 424 | // have been assigned), but is preferable to false negatives. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 425 | this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 426 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 427 | definitions); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 428 | this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(), |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 429 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 430 | definitions); |
| 431 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 432 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 433 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 434 | default: |
| 435 | // not an lvalue, can't happen |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 436 | SkASSERT(false); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | // add local variables defined by this node to the set |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 441 | void Compiler::addDefinitions(const BasicBlock::Node& node, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 442 | DefinitionMap* definitions) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 443 | if (node.isExpression()) { |
| 444 | Expression* expr = node.expression()->get(); |
| 445 | switch (expr->kind()) { |
| 446 | case Expression::Kind::kBinary: { |
| 447 | BinaryExpression* b = &expr->as<BinaryExpression>(); |
| 448 | if (b->getOperator() == Token::Kind::TK_EQ) { |
| 449 | this->addDefinition(&b->left(), &b->rightPointer(), definitions); |
| 450 | } else if (Compiler::IsAssignment(b->getOperator())) { |
| 451 | this->addDefinition( |
| 452 | &b->left(), |
| 453 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 454 | definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 455 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 456 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 457 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 458 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 459 | case Expression::Kind::kFunctionCall: { |
| 460 | const FunctionCall& c = expr->as<FunctionCall>(); |
| 461 | for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) { |
| 462 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 463 | this->addDefinition( |
| 464 | c.fArguments[i].get(), |
| 465 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 466 | definitions); |
| 467 | } |
| 468 | } |
| 469 | break; |
| 470 | } |
| 471 | case Expression::Kind::kPrefix: { |
| 472 | const PrefixExpression* p = &expr->as<PrefixExpression>(); |
| 473 | if (p->fOperator == Token::Kind::TK_MINUSMINUS || |
| 474 | p->fOperator == Token::Kind::TK_PLUSPLUS) { |
| 475 | this->addDefinition( |
| 476 | p->fOperand.get(), |
| 477 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 478 | definitions); |
| 479 | } |
| 480 | break; |
| 481 | } |
| 482 | case Expression::Kind::kPostfix: { |
| 483 | const PostfixExpression* p = &expr->as<PostfixExpression>(); |
| 484 | if (p->fOperator == Token::Kind::TK_MINUSMINUS || |
| 485 | p->fOperator == Token::Kind::TK_PLUSPLUS) { |
| 486 | this->addDefinition( |
| 487 | p->fOperand.get(), |
| 488 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 489 | definitions); |
| 490 | } |
| 491 | break; |
| 492 | } |
| 493 | case Expression::Kind::kVariableReference: { |
| 494 | const VariableReference* v = &expr->as<VariableReference>(); |
| 495 | if (v->fRefKind != VariableReference::kRead_RefKind) { |
| 496 | this->addDefinition( |
| 497 | v, |
| 498 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression, |
| 499 | definitions); |
| 500 | } |
| 501 | break; |
| 502 | } |
| 503 | default: |
| 504 | break; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 505 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 506 | } else if (node.isStatement()) { |
| 507 | Statement* stmt = node.statement()->get(); |
| 508 | if (stmt->is<VarDeclaration>()) { |
| 509 | VarDeclaration& vd = stmt->as<VarDeclaration>(); |
| 510 | if (vd.fValue) { |
| 511 | (*definitions)[vd.fVar] = &vd.fValue; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 512 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) { |
| 518 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 519 | |
| 520 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 521 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 522 | for (const BasicBlock::Node& n : block.fNodes) { |
| 523 | this->addDefinitions(n, &after); |
| 524 | } |
| 525 | |
| 526 | // propagate definitions to exits |
| 527 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 528 | if (exitId == blockId) { |
| 529 | continue; |
| 530 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 531 | BasicBlock& exit = cfg->fBlocks[exitId]; |
| 532 | for (const auto& pair : after) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 533 | std::unique_ptr<Expression>* e1 = pair.second; |
| 534 | auto found = exit.fBefore.find(pair.first); |
| 535 | if (found == exit.fBefore.end()) { |
| 536 | // exit has no definition for it, just copy it |
| 537 | workList->insert(exitId); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 538 | exit.fBefore[pair.first] = e1; |
| 539 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 540 | // exit has a (possibly different) value already defined |
| 541 | std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first]; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 542 | if (e1 != e2) { |
| 543 | // definition has changed, merge and add exit block to worklist |
| 544 | workList->insert(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 545 | if (e1 && e2) { |
| 546 | exit.fBefore[pair.first] = |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 547 | (std::unique_ptr<Expression>*) &fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 548 | } else { |
| 549 | exit.fBefore[pair.first] = nullptr; |
| 550 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // returns a map which maps all local variables in the function to null, indicating that their value |
| 558 | // is initially unknown |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 559 | static DefinitionMap compute_start_state(const CFG& cfg) { |
| 560 | DefinitionMap result; |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 561 | for (const auto& block : cfg.fBlocks) { |
| 562 | for (const auto& node : block.fNodes) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 563 | if (node.isStatement()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 564 | const Statement* s = node.statement()->get(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 565 | if (s->is<VarDeclarationsStatement>()) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 566 | const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 567 | for (const auto& decl : vd->fDeclaration->fVars) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 568 | if (decl->kind() == Statement::Kind::kVarDeclaration) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 569 | result[decl->as<VarDeclaration>().fVar] = nullptr; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 570 | } |
Mike Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 571 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | return result; |
| 577 | } |
| 578 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 579 | /** |
| 580 | * Returns true if assigning to this lvalue has no effect. |
| 581 | */ |
| 582 | static bool is_dead(const Expression& lvalue) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 583 | switch (lvalue.kind()) { |
| 584 | case Expression::Kind::kVariableReference: |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 585 | return lvalue.as<VariableReference>().fVariable->dead(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 586 | case Expression::Kind::kSwizzle: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 587 | return is_dead(*lvalue.as<Swizzle>().fBase); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 588 | case Expression::Kind::kFieldAccess: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 589 | return is_dead(*lvalue.as<FieldAccess>().fBase); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 590 | case Expression::Kind::kIndex: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 591 | const IndexExpression& idx = lvalue.as<IndexExpression>(); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 592 | return is_dead(*idx.fBase) && |
| 593 | !idx.fIndex->hasProperty(Expression::Property::kSideEffects); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 594 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 595 | case Expression::Kind::kTernary: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 596 | const TernaryExpression& t = lvalue.as<TernaryExpression>(); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 597 | return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse); |
| 598 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 599 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 600 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 601 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 602 | #ifdef SK_DEBUG |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 603 | ABORT("invalid lvalue: %s\n", lvalue.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 604 | #endif |
| 605 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 606 | } |
| 607 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 608 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 609 | /** |
| 610 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 611 | * to a dead target and lack of side effects on the left hand side. |
| 612 | */ |
| 613 | static bool dead_assignment(const BinaryExpression& b) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 614 | if (!Compiler::IsAssignment(b.getOperator())) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 615 | return false; |
| 616 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 617 | return is_dead(b.left()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void Compiler::computeDataFlow(CFG* cfg) { |
| 621 | cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 622 | std::set<BlockId> workList; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 623 | for (BlockId i = 0; i < cfg->fBlocks.size(); i++) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 624 | workList.insert(i); |
| 625 | } |
| 626 | while (workList.size()) { |
| 627 | BlockId next = *workList.begin(); |
| 628 | workList.erase(workList.begin()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 629 | this->scanCFG(cfg, next, &workList); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 630 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 635 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 636 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 637 | * need to be regenerated). |
| 638 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 639 | static bool try_replace_expression(BasicBlock* b, |
| 640 | std::vector<BasicBlock::Node>::iterator* iter, |
| 641 | std::unique_ptr<Expression>* newExpression) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 642 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 643 | if (!b->tryRemoveExpression(iter)) { |
| 644 | *target = std::move(*newExpression); |
| 645 | return false; |
| 646 | } |
| 647 | *target = std::move(*newExpression); |
| 648 | return b->tryInsertExpression(iter, target); |
| 649 | } |
| 650 | |
| 651 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 652 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 653 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 654 | */ |
Ethan Nicholas | 135e237 | 2020-09-29 10:21:29 -0400 | [diff] [blame] | 655 | template <typename T = SKSL_FLOAT> |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 656 | static bool is_constant(const Expression& expr, T value) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 657 | switch (expr.kind()) { |
| 658 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 659 | return expr.as<IntLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 660 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 661 | case Expression::Kind::kFloatLiteral: |
Ethan Nicholas | 135e237 | 2020-09-29 10:21:29 -0400 | [diff] [blame] | 662 | return expr.as<FloatLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 663 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 664 | case Expression::Kind::kConstructor: { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 665 | const Constructor& constructor = expr.as<Constructor>(); |
| 666 | if (constructor.isCompileTimeConstant()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 667 | const Type& constructorType = constructor.type(); |
| 668 | bool isFloat = constructorType.columns() > 1 |
| 669 | ? constructorType.componentType().isFloat() |
| 670 | : constructorType.isFloat(); |
| 671 | switch (constructorType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 672 | case Type::TypeKind::kVector: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 673 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 674 | if (isFloat) { |
| 675 | if (constructor.getFVecComponent(i) != value) { |
| 676 | return false; |
| 677 | } |
| 678 | } else { |
| 679 | if (constructor.getIVecComponent(i) != value) { |
| 680 | return false; |
| 681 | } |
| 682 | } |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 683 | } |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 684 | return true; |
| 685 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 686 | case Type::TypeKind::kScalar: |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 687 | SkASSERT(constructor.arguments().size() == 1); |
| 688 | return is_constant<T>(*constructor.arguments()[0], value); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 689 | |
| 690 | default: |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 691 | return false; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 692 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 693 | } |
| 694 | return false; |
| 695 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 696 | default: |
| 697 | return false; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 703 | * and CFG structures). |
| 704 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 705 | static void delete_left(BasicBlock* b, |
| 706 | std::vector<BasicBlock::Node>::iterator* iter, |
| 707 | bool* outUpdated, |
| 708 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 709 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 710 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 711 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 712 | Expression& left = bin.left(); |
| 713 | std::unique_ptr<Expression>& rightPointer = bin.rightPointer(); |
| 714 | SkASSERT(!left.hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 715 | bool result; |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 716 | if (bin.getOperator() == Token::Kind::TK_EQ) { |
| 717 | result = b->tryRemoveLValueBefore(iter, &left); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 718 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 719 | result = b->tryRemoveExpressionBefore(iter, &left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 720 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 721 | *target = std::move(rightPointer); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 722 | if (!result) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 723 | *outNeedsRescan = true; |
| 724 | return; |
| 725 | } |
| 726 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 727 | *outNeedsRescan = true; |
| 728 | return; |
| 729 | } |
| 730 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 731 | if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 732 | *outNeedsRescan = true; |
| 733 | return; |
| 734 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 735 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 736 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 741 | * CFG structures). |
| 742 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 743 | static void delete_right(BasicBlock* b, |
| 744 | std::vector<BasicBlock::Node>::iterator* iter, |
| 745 | bool* outUpdated, |
| 746 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 747 | *outUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 748 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 749 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 750 | std::unique_ptr<Expression>& leftPointer = bin.leftPointer(); |
| 751 | Expression& right = bin.right(); |
| 752 | SkASSERT(!right.hasSideEffects()); |
| 753 | if (!b->tryRemoveExpressionBefore(iter, &right)) { |
| 754 | *target = std::move(leftPointer); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 755 | *outNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 756 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 757 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 758 | *target = std::move(leftPointer); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 759 | if (*iter == b->fNodes.begin()) { |
| 760 | *outNeedsRescan = true; |
| 761 | return; |
| 762 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 763 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 764 | if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 765 | *outNeedsRescan = true; |
| 766 | return; |
| 767 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 768 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 769 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 772 | /** |
| 773 | * Constructs the specified type using a single argument. |
| 774 | */ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 775 | static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 776 | std::vector<std::unique_ptr<Expression>> args; |
| 777 | args.push_back(std::move(v)); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 778 | std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 779 | return result; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 784 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 785 | */ |
| 786 | static void vectorize(BasicBlock* b, |
| 787 | std::vector<BasicBlock::Node>::iterator* iter, |
| 788 | const Type& type, |
| 789 | std::unique_ptr<Expression>* otherExpression, |
| 790 | bool* outUpdated, |
| 791 | bool* outNeedsRescan) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 792 | SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary); |
| 793 | SkASSERT(type.typeKind() == Type::TypeKind::kVector); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 794 | SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 795 | *outUpdated = true; |
| 796 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 797 | if (!b->tryRemoveExpression(iter)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 798 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 799 | *outNeedsRescan = true; |
| 800 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 801 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 802 | if (!b->tryInsertExpression(iter, target)) { |
| 803 | *outNeedsRescan = true; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 810 | * left to yield vec<n>(x). |
| 811 | */ |
| 812 | static void vectorize_left(BasicBlock* b, |
| 813 | std::vector<BasicBlock::Node>::iterator* iter, |
| 814 | bool* outUpdated, |
| 815 | bool* outNeedsRescan) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 816 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 817 | vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 822 | * right to yield vec<n>(y). |
| 823 | */ |
| 824 | static void vectorize_right(BasicBlock* b, |
| 825 | std::vector<BasicBlock::Node>::iterator* iter, |
| 826 | bool* outUpdated, |
| 827 | bool* outNeedsRescan) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 828 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 829 | vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // Mark that an expression which we were writing to is no longer being written to |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 833 | static void clear_write(Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 834 | switch (expr.kind()) { |
| 835 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 836 | expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 837 | break; |
| 838 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 839 | case Expression::Kind::kFieldAccess: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 840 | clear_write(*expr.as<FieldAccess>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 841 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 842 | case Expression::Kind::kSwizzle: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 843 | clear_write(*expr.as<Swizzle>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 844 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 845 | case Expression::Kind::kIndex: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 846 | clear_write(*expr.as<IndexExpression>().fBase); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 847 | break; |
| 848 | default: |
| 849 | ABORT("shouldn't be writing to this kind of expression\n"); |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 854 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 855 | BasicBlock& b, |
| 856 | std::vector<BasicBlock::Node>::iterator* iter, |
| 857 | std::unordered_set<const Variable*>* undefinedVariables, |
| 858 | bool* outUpdated, |
| 859 | bool* outNeedsRescan) { |
| 860 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 861 | SkASSERT(expr); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 862 | if ((*iter)->fConstantPropagation) { |
| 863 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions); |
| 864 | if (optimized) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 865 | *outUpdated = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 866 | optimized = fIRGenerator->coerce(std::move(optimized), expr->type()); |
Ethan Nicholas | 1d88152 | 2020-09-11 09:32:54 -0400 | [diff] [blame] | 867 | SkASSERT(optimized); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 868 | if (!try_replace_expression(&b, iter, &optimized)) { |
| 869 | *outNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 870 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 871 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 872 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 873 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 874 | } |
| 875 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 876 | switch (expr->kind()) { |
| 877 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 878 | const VariableReference& ref = expr->as<VariableReference>(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 879 | const Variable* var = ref.fVariable; |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 880 | if (ref.refKind() != VariableReference::kWrite_RefKind && |
| 881 | ref.refKind() != VariableReference::kPointer_RefKind && |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 882 | var->fStorage == Variable::kLocal_Storage && !definitions[var] && |
| 883 | (*undefinedVariables).find(var) == (*undefinedVariables).end()) { |
| 884 | (*undefinedVariables).insert(var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 885 | this->error(expr->fOffset, |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 886 | "'" + var->fName + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 887 | } |
| 888 | break; |
| 889 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 890 | case Expression::Kind::kTernary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 891 | TernaryExpression* t = &expr->as<TernaryExpression>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 892 | if (t->fTest->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 893 | // ternary has a constant test, replace it with either the true or |
| 894 | // false branch |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 895 | if (t->fTest->as<BoolLiteral>().value()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 896 | (*iter)->setExpression(std::move(t->fIfTrue)); |
| 897 | } else { |
| 898 | (*iter)->setExpression(std::move(t->fIfFalse)); |
| 899 | } |
| 900 | *outUpdated = true; |
| 901 | *outNeedsRescan = true; |
| 902 | } |
| 903 | break; |
| 904 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 905 | case Expression::Kind::kBinary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 906 | BinaryExpression* bin = &expr->as<BinaryExpression>(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 907 | if (dead_assignment(*bin)) { |
| 908 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 909 | break; |
| 910 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 911 | Expression& left = bin->left(); |
| 912 | Expression& right = bin->right(); |
| 913 | const Type& leftType = left.type(); |
| 914 | const Type& rightType = right.type(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 915 | // collapse useless expressions like x * 1 or x + 0 |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 916 | if (((leftType.typeKind() != Type::TypeKind::kScalar) && |
| 917 | (leftType.typeKind() != Type::TypeKind::kVector)) || |
| 918 | ((rightType.typeKind() != Type::TypeKind::kScalar) && |
| 919 | (rightType.typeKind() != Type::TypeKind::kVector))) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 920 | break; |
| 921 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 922 | switch (bin->getOperator()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 923 | case Token::Kind::TK_STAR: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 924 | if (is_constant(left, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 925 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 926 | rightType.typeKind() == Type::TypeKind::kScalar) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 927 | // float4(1) * x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 928 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 929 | } else { |
| 930 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 931 | // 1 * float4(x) -> float4(x) |
| 932 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 933 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 934 | } |
| 935 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 936 | else if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 937 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 938 | rightType.typeKind() == Type::TypeKind::kVector && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 939 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 940 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 941 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 942 | } else { |
| 943 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 944 | // float4(0) * x -> float4(0) |
| 945 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 946 | if (!right.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 947 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 948 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 949 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 950 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 951 | else if (is_constant(right, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 952 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 953 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 954 | // x * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 955 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 956 | } else { |
| 957 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 958 | // float4(x) * 1 -> float4(x) |
| 959 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 960 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 961 | } |
| 962 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 963 | else if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 964 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 965 | rightType.typeKind() == Type::TypeKind::kScalar && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 966 | !left.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 967 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 968 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 969 | } else { |
| 970 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 971 | // x * float4(0) -> float4(0) |
| 972 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 973 | if (!left.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 974 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 975 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 976 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 977 | } |
| 978 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 979 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 980 | if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 981 | if (leftType.typeKind() == Type::TypeKind::kVector && |
| 982 | rightType.typeKind() == Type::TypeKind::kScalar) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 983 | // float4(0) + x -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 984 | vectorize_right(&b, iter, outUpdated, outNeedsRescan); |
| 985 | } else { |
| 986 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 987 | // 0 + float4(x) -> float4(x) |
| 988 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 989 | delete_left(&b, iter, outUpdated, outNeedsRescan); |
| 990 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 991 | } else if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 992 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 993 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 994 | // x + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 995 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 996 | } else { |
| 997 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 998 | // float4(x) + 0 -> float4(x) |
| 999 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1000 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1001 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 1002 | } |
| 1003 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1004 | case Token::Kind::TK_MINUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1005 | if (is_constant(right, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1006 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1007 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1008 | // x - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1009 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1010 | } else { |
| 1011 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1012 | // float4(x) - 0 -> float4(x) |
| 1013 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1014 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1015 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1016 | } |
| 1017 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1018 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1019 | if (is_constant(right, 1)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1020 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1021 | rightType.typeKind() == Type::TypeKind::kVector) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1022 | // x / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1023 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1024 | } else { |
| 1025 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1026 | // float4(x) / 1 -> float4(x) |
| 1027 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1028 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1029 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1030 | } else if (is_constant(left, 0)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1031 | if (leftType.typeKind() == Type::TypeKind::kScalar && |
| 1032 | rightType.typeKind() == Type::TypeKind::kVector && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1033 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1034 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1035 | vectorize_left(&b, iter, outUpdated, outNeedsRescan); |
| 1036 | } else { |
| 1037 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1038 | // float4(0) / x -> float4(0) |
| 1039 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1040 | if (!right.hasSideEffects()) { |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 1041 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1042 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1046 | case Token::Kind::TK_PLUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1047 | if (is_constant(right, 0)) { |
| 1048 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1049 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1050 | } |
| 1051 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1052 | case Token::Kind::TK_MINUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1053 | if (is_constant(right, 0)) { |
| 1054 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1055 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1056 | } |
| 1057 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1058 | case Token::Kind::TK_STAREQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1059 | if (is_constant(right, 1)) { |
| 1060 | clear_write(left); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1061 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1062 | } |
| 1063 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1064 | case Token::Kind::TK_SLASHEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 1065 | if (is_constant(right, 1)) { |
| 1066 | clear_write(left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1067 | delete_right(&b, iter, outUpdated, outNeedsRescan); |
| 1068 | } |
| 1069 | break; |
| 1070 | default: |
| 1071 | break; |
| 1072 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1073 | break; |
| 1074 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1075 | case Expression::Kind::kSwizzle: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 1076 | Swizzle& s = expr->as<Swizzle>(); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1077 | // detect identity swizzles like foo.rgba |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1078 | if ((int) s.fComponents.size() == s.fBase->type().columns()) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1079 | bool identity = true; |
| 1080 | for (int i = 0; i < (int) s.fComponents.size(); ++i) { |
| 1081 | if (s.fComponents[i] != i) { |
| 1082 | identity = false; |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | if (identity) { |
| 1087 | *outUpdated = true; |
| 1088 | if (!try_replace_expression(&b, iter, &s.fBase)) { |
| 1089 | *outNeedsRescan = true; |
| 1090 | return; |
| 1091 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1092 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000 |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1097 | if (s.fBase->kind() == Expression::Kind::kSwizzle) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1098 | Swizzle& base = s.fBase->as<Swizzle>(); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1099 | std::vector<int> final; |
| 1100 | for (int c : s.fComponents) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 1101 | final.push_back(base.fComponents[c]); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1102 | } |
| 1103 | *outUpdated = true; |
| 1104 | std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(), |
| 1105 | std::move(final))); |
| 1106 | if (!try_replace_expression(&b, iter, &replacement)) { |
| 1107 | *outNeedsRescan = true; |
| 1108 | return; |
| 1109 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1110 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1111 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1112 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1113 | } |
| 1114 | default: |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1119 | // Returns true if this statement could potentially execute a break at the current level. We ignore |
| 1120 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch. |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1121 | static bool contains_conditional_break(Statement& stmt) { |
| 1122 | class ContainsConditionalBreak : public ProgramVisitor { |
| 1123 | public: |
| 1124 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1125 | switch (stmt.kind()) { |
| 1126 | case Statement::Kind::kBlock: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1127 | return this->INHERITED::visitStatement(stmt); |
| 1128 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1129 | case Statement::Kind::kBreak: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1130 | return fInConditional > 0; |
| 1131 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1132 | case Statement::Kind::kIf: { |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1133 | ++fInConditional; |
| 1134 | bool result = this->INHERITED::visitStatement(stmt); |
| 1135 | --fInConditional; |
| 1136 | return result; |
| 1137 | } |
| 1138 | |
| 1139 | default: |
| 1140 | return false; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | int fInConditional = 0; |
| 1145 | using INHERITED = ProgramVisitor; |
| 1146 | }; |
| 1147 | |
| 1148 | return ContainsConditionalBreak{}.visitStatement(stmt); |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
Ethan Nicholas | 5005a22 | 2018-08-24 13:06:27 -0400 | [diff] [blame] | 1151 | // returns true if this statement definitely executes a break at the current level (we ignore |
| 1152 | // nested loops and switches, since any breaks inside of them will merely break the loop / switch) |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1153 | static bool contains_unconditional_break(Statement& stmt) { |
| 1154 | class ContainsUnconditionalBreak : public ProgramVisitor { |
| 1155 | public: |
| 1156 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1157 | switch (stmt.kind()) { |
| 1158 | case Statement::Kind::kBlock: |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1159 | return this->INHERITED::visitStatement(stmt); |
| 1160 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1161 | case Statement::Kind::kBreak: |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1162 | return true; |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1163 | |
| 1164 | default: |
| 1165 | return false; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1166 | } |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1167 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1168 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1169 | using INHERITED = ProgramVisitor; |
| 1170 | }; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1171 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1172 | return ContainsUnconditionalBreak{}.visitStatement(stmt); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1173 | } |
| 1174 | |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1175 | static void move_all_but_break(std::unique_ptr<Statement>& stmt, |
| 1176 | std::vector<std::unique_ptr<Statement>>* target) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1177 | switch (stmt->kind()) { |
| 1178 | case Statement::Kind::kBlock: { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1179 | // Recurse into the block. |
| 1180 | Block& block = static_cast<Block&>(*stmt); |
| 1181 | |
| 1182 | std::vector<std::unique_ptr<Statement>> blockStmts; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1183 | blockStmts.reserve(block.children().size()); |
| 1184 | for (std::unique_ptr<Statement>& stmt : block.children()) { |
| 1185 | move_all_but_break(stmt, &blockStmts); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1186 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1187 | |
| 1188 | target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts), |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1189 | block.symbolTable(), block.isScope())); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1190 | break; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1193 | case Statement::Kind::kBreak: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1194 | // Do not append a break to the target. |
| 1195 | break; |
| 1196 | |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1197 | default: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1198 | // Append normal statements to the target. |
| 1199 | target->push_back(std::move(stmt)); |
| 1200 | break; |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1204 | // Returns a block containing all of the statements that will be run if the given case matches |
| 1205 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 1206 | // broken by this call and must then be discarded). |
| 1207 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 1208 | // when break statements appear inside conditionals. |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1209 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement, |
| 1210 | SwitchCase* caseToCapture) { |
| 1211 | // We have to be careful to not move any of the pointers until after we're sure we're going to |
| 1212 | // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan |
| 1213 | // of action. First, find the switch-case we are interested in. |
| 1214 | auto iter = switchStatement->fCases.begin(); |
| 1215 | for (; iter != switchStatement->fCases.end(); ++iter) { |
| 1216 | if (iter->get() == caseToCapture) { |
| 1217 | break; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1218 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // Next, walk forward through the rest of the switch. If we find a conditional break, we're |
| 1222 | // stuck and can't simplify at all. If we find an unconditional break, we have a range of |
| 1223 | // statements that we can use for simplification. |
| 1224 | auto startIter = iter; |
| 1225 | Statement* unconditionalBreakStmt = nullptr; |
| 1226 | for (; iter != switchStatement->fCases.end(); ++iter) { |
| 1227 | for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) { |
| 1228 | if (contains_conditional_break(*stmt)) { |
| 1229 | // We can't reduce switch-cases to a block when they have conditional breaks. |
| 1230 | return nullptr; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1231 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1232 | |
| 1233 | if (contains_unconditional_break(*stmt)) { |
| 1234 | // We found an unconditional break. We can use this block, but we need to strip |
| 1235 | // out the break statement. |
| 1236 | unconditionalBreakStmt = stmt.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1237 | break; |
| 1238 | } |
| 1239 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1240 | |
| 1241 | if (unconditionalBreakStmt != nullptr) { |
| 1242 | break; |
| 1243 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1244 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1245 | |
| 1246 | // We fell off the bottom of the switch or encountered a break. We know the range of statements |
| 1247 | // that we need to move over, and we know it's safe to do so. |
| 1248 | std::vector<std::unique_ptr<Statement>> caseStmts; |
| 1249 | |
| 1250 | // We can move over most of the statements as-is. |
| 1251 | while (startIter != iter) { |
| 1252 | for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) { |
| 1253 | caseStmts.push_back(std::move(stmt)); |
| 1254 | } |
| 1255 | ++startIter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1256 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1257 | |
| 1258 | // If we found an unconditional break at the end, we need to move what we can while avoiding |
| 1259 | // that break. |
| 1260 | if (unconditionalBreakStmt != nullptr) { |
| 1261 | for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) { |
| 1262 | if (stmt.get() == unconditionalBreakStmt) { |
| 1263 | move_all_but_break(stmt, &caseStmts); |
| 1264 | unconditionalBreakStmt = nullptr; |
| 1265 | break; |
| 1266 | } |
| 1267 | |
| 1268 | caseStmts.push_back(std::move(stmt)); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break. |
| 1273 | |
| 1274 | // Return our newly-synthesized block. |
| 1275 | return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1278 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1279 | BasicBlock& b, |
| 1280 | std::vector<BasicBlock::Node>::iterator* iter, |
| 1281 | std::unordered_set<const Variable*>* undefinedVariables, |
| 1282 | bool* outUpdated, |
| 1283 | bool* outNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1284 | Statement* stmt = (*iter)->statement()->get(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1285 | switch (stmt->kind()) { |
| 1286 | case Statement::Kind::kVarDeclaration: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1287 | const auto& varDecl = stmt->as<VarDeclaration>(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1288 | if (varDecl.fVar->dead() && |
| 1289 | (!varDecl.fValue || |
| 1290 | !varDecl.fValue->hasSideEffects())) { |
| 1291 | if (varDecl.fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1292 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1293 | if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) { |
| 1294 | *outNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1295 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1296 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1297 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1298 | *outUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1299 | } |
| 1300 | break; |
| 1301 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1302 | case Statement::Kind::kIf: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1303 | IfStatement& i = stmt->as<IfStatement>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1304 | if (i.fTest->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1305 | // constant if, collapse down to a single branch |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 1306 | if (i.fTest->as<BoolLiteral>().value()) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1307 | SkASSERT(i.fIfTrue); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1308 | (*iter)->setStatement(std::move(i.fIfTrue)); |
| 1309 | } else { |
| 1310 | if (i.fIfFalse) { |
| 1311 | (*iter)->setStatement(std::move(i.fIfFalse)); |
| 1312 | } else { |
| 1313 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1314 | } |
| 1315 | } |
| 1316 | *outUpdated = true; |
| 1317 | *outNeedsRescan = true; |
| 1318 | break; |
| 1319 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1320 | if (i.fIfFalse && i.fIfFalse->isEmpty()) { |
| 1321 | // else block doesn't do anything, remove it |
| 1322 | i.fIfFalse.reset(); |
| 1323 | *outUpdated = true; |
| 1324 | *outNeedsRescan = true; |
| 1325 | } |
| 1326 | if (!i.fIfFalse && i.fIfTrue->isEmpty()) { |
| 1327 | // if block doesn't do anything, no else block |
| 1328 | if (i.fTest->hasSideEffects()) { |
| 1329 | // test has side effects, keep it |
| 1330 | (*iter)->setStatement(std::unique_ptr<Statement>( |
| 1331 | new ExpressionStatement(std::move(i.fTest)))); |
| 1332 | } else { |
| 1333 | // no if, no else, no test side effects, kill the whole if |
| 1334 | // statement |
| 1335 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1336 | } |
| 1337 | *outUpdated = true; |
| 1338 | *outNeedsRescan = true; |
| 1339 | } |
| 1340 | break; |
| 1341 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1342 | case Statement::Kind::kSwitch: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1343 | SwitchStatement& s = stmt->as<SwitchStatement>(); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1344 | int64_t switchValue; |
| 1345 | if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1346 | // switch is constant, replace it with the case that matches |
| 1347 | bool found = false; |
| 1348 | SwitchCase* defaultCase = nullptr; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1349 | for (const std::unique_ptr<SwitchCase>& c : s.fCases) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1350 | if (!c->fValue) { |
| 1351 | defaultCase = c.get(); |
| 1352 | continue; |
| 1353 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1354 | int64_t caseValue; |
| 1355 | SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue)); |
| 1356 | if (caseValue == switchValue) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1357 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
| 1358 | if (newBlock) { |
| 1359 | (*iter)->setStatement(std::move(newBlock)); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1360 | found = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1361 | break; |
| 1362 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1363 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1364 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1365 | "static switch contains non-static conditional break"); |
| 1366 | s.fIsStatic = false; |
| 1367 | } |
| 1368 | return; // can't simplify |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | if (!found) { |
| 1373 | // no matching case. use default if it exists, or kill the whole thing |
| 1374 | if (defaultCase) { |
| 1375 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 1376 | if (newBlock) { |
| 1377 | (*iter)->setStatement(std::move(newBlock)); |
| 1378 | } else { |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1379 | if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1380 | this->error(s.fOffset, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1381 | "static switch contains non-static conditional break"); |
| 1382 | s.fIsStatic = false; |
| 1383 | } |
| 1384 | return; // can't simplify |
| 1385 | } |
| 1386 | } else { |
| 1387 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1388 | } |
| 1389 | } |
| 1390 | *outUpdated = true; |
| 1391 | *outNeedsRescan = true; |
| 1392 | } |
| 1393 | break; |
| 1394 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1395 | case Statement::Kind::kExpression: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1396 | ExpressionStatement& e = stmt->as<ExpressionStatement>(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1397 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1398 | if (!e.expression()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1399 | // Expression statement with no side effects, kill it |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1400 | if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1401 | *outNeedsRescan = true; |
| 1402 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1403 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1404 | (*iter)->setStatement(std::unique_ptr<Statement>(new Nop())); |
| 1405 | *outUpdated = true; |
| 1406 | } |
| 1407 | break; |
| 1408 | } |
| 1409 | default: |
| 1410 | break; |
| 1411 | } |
| 1412 | } |
| 1413 | |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1414 | bool Compiler::scanCFG(FunctionDefinition& f) { |
| 1415 | bool madeChanges = false; |
| 1416 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1417 | CFG cfg = CFGGenerator().getCFG(f); |
| 1418 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1419 | |
| 1420 | // check for unreachable code |
| 1421 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1422 | const BasicBlock& block = cfg.fBlocks[i]; |
| 1423 | if (i != cfg.fStart && !block.fEntrances.size() && block.fNodes.size()) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1424 | int offset; |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1425 | const BasicBlock::Node& node = block.fNodes[0]; |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1426 | if (node.isStatement()) { |
| 1427 | offset = (*node.statement())->fOffset; |
| 1428 | } else { |
| 1429 | offset = (*node.expression())->fOffset; |
| 1430 | if ((*node.expression())->is<BoolLiteral>()) { |
| 1431 | // Function inlining can generate do { ... } while(false) loops which always |
| 1432 | // break, so the boolean condition is considered unreachable. Since not being |
| 1433 | // able to reach a literal is a non-issue in the first place, we don't report an |
| 1434 | // error in this case. |
| 1435 | continue; |
| 1436 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1437 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1438 | this->error(offset, String("unreachable")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | if (fErrorCount) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1442 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1445 | // check for dead code & undefined variables, perform constant propagation |
| 1446 | std::unordered_set<const Variable*> undefinedVariables; |
| 1447 | bool updated; |
| 1448 | bool needsRescan = false; |
| 1449 | do { |
| 1450 | if (needsRescan) { |
| 1451 | cfg = CFGGenerator().getCFG(f); |
| 1452 | this->computeDataFlow(&cfg); |
| 1453 | needsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1454 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1455 | |
| 1456 | updated = false; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1457 | bool first = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1458 | for (BasicBlock& b : cfg.fBlocks) { |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1459 | if (!first && b.fEntrances.empty()) { |
| 1460 | // Block was reachable before optimization, but has since become unreachable. In |
| 1461 | // addition to being dead code, it's broken - since control flow can't reach it, no |
| 1462 | // prior variable definitions can reach it, and therefore variables might look to |
| 1463 | // have not been properly assigned. Kill it. |
Brian Osman | db16c48 | 2020-09-09 15:15:06 -0400 | [diff] [blame] | 1464 | |
| 1465 | // We need to do this in two steps. For any variable declarations, the node list |
| 1466 | // will contain statement nodes for each VarDeclaration, and then a statement for |
| 1467 | // the VarDeclarationsStatement. When we replace the VDS with a Nop, we delete the |
| 1468 | // storage of the unique_ptr that the VD nodes are pointing to. So we remove those |
| 1469 | // from the node list entirely, first. |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1470 | b.fNodes.erase(std::remove_if(b.fNodes.begin(), b.fNodes.end(), |
| 1471 | [](const BasicBlock::Node& node) { |
| 1472 | return node.isStatement() && |
| 1473 | (*node.statement())->is<VarDeclaration>(); |
| 1474 | }), |
| 1475 | b.fNodes.end()); |
Brian Osman | db16c48 | 2020-09-09 15:15:06 -0400 | [diff] [blame] | 1476 | |
| 1477 | // Now replace any remaining statements in the block with Nops. |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1478 | for (BasicBlock::Node& node : b.fNodes) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1479 | if (node.isStatement() && !(*node.statement())->is<Nop>()) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1480 | node.setStatement(std::make_unique<Nop>()); |
| 1481 | madeChanges = true; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | continue; |
| 1485 | } |
| 1486 | first = false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1487 | DefinitionMap definitions = b.fBefore; |
| 1488 | |
| 1489 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1490 | if (iter->isExpression()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1491 | this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated, |
| 1492 | &needsRescan); |
| 1493 | } else { |
| 1494 | this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated, |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1495 | &needsRescan); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1496 | } |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1497 | if (needsRescan) { |
| 1498 | break; |
| 1499 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1500 | this->addDefinitions(*iter, &definitions); |
| 1501 | } |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1502 | |
| 1503 | if (needsRescan) { |
| 1504 | break; |
| 1505 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1506 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1507 | madeChanges |= updated; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1508 | } while (updated); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1509 | SkASSERT(!needsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1510 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1511 | // verify static ifs & switches, clean up dead variable decls |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1512 | for (BasicBlock& b : cfg.fBlocks) { |
| 1513 | DefinitionMap definitions = b.fBefore; |
| 1514 | |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1515 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1516 | if (iter->isStatement()) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1517 | const Statement& s = **iter->statement(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1518 | switch (s.kind()) { |
| 1519 | case Statement::Kind::kIf: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1520 | if (s.as<IfStatement>().fIsStatic && |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 1521 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1522 | this->error(s.fOffset, "static if has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1523 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1524 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1525 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1526 | case Statement::Kind::kSwitch: |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1527 | if (s.as<SwitchStatement>().fIsStatic && |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1528 | !(fFlags & kPermitInvalidStaticTests_Flag)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1529 | this->error(s.fOffset, "static switch has non-static test"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1530 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1531 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1532 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1533 | case Statement::Kind::kVarDeclarations: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1534 | VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration; |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1535 | decls.fVars.erase( |
| 1536 | std::remove_if(decls.fVars.begin(), decls.fVars.end(), |
| 1537 | [&](const std::unique_ptr<Statement>& var) { |
| 1538 | bool nop = var->is<Nop>(); |
| 1539 | madeChanges |= nop; |
| 1540 | return nop; |
| 1541 | }), |
| 1542 | decls.fVars.end()); |
| 1543 | if (decls.fVars.empty()) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 1544 | iter = b.fNodes.erase(iter); |
| 1545 | } else { |
| 1546 | ++iter; |
| 1547 | } |
| 1548 | break; |
| 1549 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1550 | default: |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1551 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1552 | break; |
| 1553 | } |
Ethan Nicholas | 91a1053 | 2017-06-22 11:24:38 -0400 | [diff] [blame] | 1554 | } else { |
| 1555 | ++iter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1560 | // check for missing return |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1561 | if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1562 | if (cfg.fBlocks[cfg.fExit].fEntrances.size()) { |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1563 | this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) + |
| 1564 | "' can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1565 | } |
| 1566 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1567 | |
| 1568 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1571 | std::unique_ptr<Program> Compiler::convertProgram( |
| 1572 | Program::Kind kind, |
| 1573 | String text, |
| 1574 | const Program::Settings& settings, |
| 1575 | const std::vector<std::unique_ptr<ExternalValue>>* externalValues) { |
| 1576 | SkASSERT(!externalValues || (kind == Program::kGeneric_Kind)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1577 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1578 | fErrorText = ""; |
| 1579 | fErrorCount = 0; |
John Stiles | 7b46300 | 2020-08-31 17:29:21 -0400 | [diff] [blame] | 1580 | fInliner.reset(context(), settings); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1581 | std::vector<std::unique_ptr<ProgramElement>>* inherited; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1582 | std::vector<std::unique_ptr<ProgramElement>> elements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1583 | switch (kind) { |
| 1584 | case Program::kVertex_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1585 | inherited = &fVertexInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1586 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1587 | fIRGenerator->start(&settings, fVertexSymbolTable, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1588 | break; |
| 1589 | case Program::kFragment_Kind: |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1590 | inherited = &fFragmentInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1591 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1592 | fIRGenerator->start(&settings, fFragmentSymbolTable, inherited); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1593 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1594 | case Program::kGeometry_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1595 | this->loadGeometryIntrinsics(); |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1596 | inherited = &fGeometryInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1597 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1598 | fIRGenerator->start(&settings, fGeometrySymbolTable, inherited); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1599 | break; |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 1600 | case Program::kFragmentProcessor_Kind: { |
Brian Osman | dd49617 | 2020-08-08 08:17:18 -0400 | [diff] [blame] | 1601 | #if !SKSL_STANDALONE |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1602 | { |
| 1603 | Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, |
| 1604 | SKSL_INCLUDE_sksl_fp, |
| 1605 | SKSL_INCLUDE_sksl_fp_LENGTH); |
| 1606 | fFPSymbolTable = rehydrator.symbolTable(); |
| 1607 | fFPInclude = rehydrator.elements(); |
| 1608 | } |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 1609 | fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get()); |
| 1610 | grab_intrinsics(&fFPInclude, fFPIntrinsics.get()); |
| 1611 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1612 | inherited = &fFPInclude; |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 1613 | fIRGenerator->fIntrinsics = fFPIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1614 | fIRGenerator->start(&settings, fFPSymbolTable, inherited); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1615 | break; |
| 1616 | #else |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 1617 | inherited = nullptr; |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1618 | fIRGenerator->start(&settings, fGpuSymbolTable, /*inherited=*/nullptr, |
| 1619 | /*builtin=*/true); |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1620 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 1621 | std::ifstream in(SKSL_FP_INCLUDE); |
| 1622 | std::string stdText{std::istreambuf_iterator<char>(in), |
| 1623 | std::istreambuf_iterator<char>()}; |
| 1624 | if (in.rdstate()) { |
| 1625 | printf("error reading %s\n", SKSL_FP_INCLUDE); |
| 1626 | abort(); |
| 1627 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1628 | const String* source = fRootSymbolTable->takeOwnershipOfString( |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 1629 | std::make_unique<String>(stdText.c_str())); |
| 1630 | fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1631 | fIRGenerator->fIsBuiltinCode = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1632 | break; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1633 | #endif |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 1634 | } |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1635 | case Program::kPipelineStage_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1636 | this->loadPipelineIntrinsics(); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1637 | inherited = &fPipelineInclude; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1638 | fIRGenerator->fIntrinsics = fGPUIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1639 | fIRGenerator->start(&settings, fPipelineSymbolTable, inherited); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 1640 | break; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 1641 | case Program::kGeneric_Kind: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1642 | this->loadInterpreterIntrinsics(); |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 1643 | inherited = nullptr; |
John Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 1644 | fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get(); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1645 | fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 1646 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1647 | } |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1648 | if (externalValues) { |
| 1649 | // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so |
| 1650 | // we're only making these visible to the current Program. |
| 1651 | for (const auto& ev : *externalValues) { |
| 1652 | fIRGenerator->fSymbolTable->addWithoutOwnership(ev->fName, ev.get()); |
| 1653 | } |
| 1654 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1655 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1656 | fSource = textPtr.get(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1657 | fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 1658 | auto result = std::make_unique<Program>(kind, |
| 1659 | std::move(textPtr), |
| 1660 | settings, |
| 1661 | fContext, |
| 1662 | inherited, |
| 1663 | std::move(elements), |
| 1664 | fIRGenerator->fSymbolTable, |
| 1665 | fIRGenerator->fInputs); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 1666 | fIRGenerator->finish(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1667 | if (fErrorCount) { |
| 1668 | return nullptr; |
| 1669 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1670 | if (settings.fOptimize && !this->optimize(*result)) { |
| 1671 | return nullptr; |
| 1672 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1673 | return result; |
| 1674 | } |
| 1675 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1676 | bool Compiler::optimize(Program& program) { |
| 1677 | SkASSERT(!fErrorCount); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1678 | fIRGenerator->fKind = program.fKind; |
| 1679 | fIRGenerator->fSettings = &program.fSettings; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1680 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1681 | while (fErrorCount == 0) { |
| 1682 | bool madeChanges = false; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1683 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1684 | // Scan and optimize based on the control-flow graph for each function. |
| 1685 | for (ProgramElement& element : program) { |
| 1686 | if (element.is<FunctionDefinition>()) { |
| 1687 | madeChanges |= this->scanCFG(element.as<FunctionDefinition>()); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 1692 | madeChanges |= fInliner.analyze(program); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1693 | |
| 1694 | // Remove dead functions. We wait until after analysis so that we still report errors, |
| 1695 | // even in unused code. |
| 1696 | if (program.fSettings.fRemoveDeadFunctions) { |
| 1697 | program.fElements.erase( |
| 1698 | std::remove_if(program.fElements.begin(), |
| 1699 | program.fElements.end(), |
| 1700 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 1701 | if (!element->is<FunctionDefinition>()) { |
| 1702 | return false; |
| 1703 | } |
| 1704 | const auto& fn = element->as<FunctionDefinition>(); |
| 1705 | bool dead = fn.fDeclaration.fCallCount == 0 && |
| 1706 | fn.fDeclaration.fName != "main"; |
| 1707 | madeChanges |= dead; |
| 1708 | return dead; |
| 1709 | }), |
| 1710 | program.fElements.end()); |
| 1711 | } |
| 1712 | |
| 1713 | if (program.fKind != Program::kFragmentProcessor_Kind) { |
| 1714 | // Remove dead variables. |
John Stiles | eadfc3b | 2020-09-02 14:12:41 -0400 | [diff] [blame] | 1715 | for (ProgramElement& element : program) { |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1716 | if (!element.is<VarDeclarations>()) { |
| 1717 | continue; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1718 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1719 | VarDeclarations& vars = element.as<VarDeclarations>(); |
| 1720 | vars.fVars.erase( |
| 1721 | std::remove_if(vars.fVars.begin(), vars.fVars.end(), |
| 1722 | [&](const std::unique_ptr<Statement>& stmt) { |
| 1723 | bool dead = stmt->as<VarDeclaration>().fVar->dead(); |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1724 | madeChanges |= dead; |
| 1725 | return dead; |
| 1726 | }), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1727 | vars.fVars.end()); |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1728 | } |
| 1729 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1730 | // Remove empty variable declarations with no variables left inside of them. |
| 1731 | program.fElements.erase( |
| 1732 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 1733 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 1734 | if (!element->is<VarDeclarations>()) { |
| 1735 | return false; |
| 1736 | } |
| 1737 | bool dead = element->as<VarDeclarations>().fVars.empty(); |
| 1738 | madeChanges |= dead; |
| 1739 | return dead; |
| 1740 | }), |
| 1741 | program.fElements.end()); |
| 1742 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1743 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1744 | if (!madeChanges) { |
| 1745 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1746 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1747 | } |
| 1748 | return fErrorCount == 0; |
| 1749 | } |
| 1750 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1751 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1752 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1753 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1754 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1755 | StringStream buffer; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1756 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1757 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1758 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1759 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1760 | if (result) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1761 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1762 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1763 | SkASSERT(0 == data.size() % 4); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1764 | auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) { |
| 1765 | SkDebugf("SPIR-V validation error: %s\n", m); |
| 1766 | }; |
| 1767 | tools.SetMessageConsumer(dumpmsg); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1768 | // 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] | 1769 | // to the failure to see the validation errors. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1770 | SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4)); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1771 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1772 | } |
| 1773 | #else |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1774 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1775 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1776 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1777 | fSource = nullptr; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1778 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1779 | return result; |
| 1780 | } |
| 1781 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1782 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1783 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1784 | bool result = this->toSPIRV(program, buffer); |
| 1785 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1786 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1787 | } |
| 1788 | return result; |
| 1789 | } |
| 1790 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1791 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1792 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1793 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1794 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1795 | fSource = nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1796 | return result; |
| 1797 | } |
| 1798 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1799 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1800 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1801 | bool result = this->toGLSL(program, buffer); |
| 1802 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1803 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1804 | } |
| 1805 | return result; |
| 1806 | } |
| 1807 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 1808 | bool Compiler::toHLSL(Program& program, String* out) { |
| 1809 | String spirv; |
| 1810 | if (!this->toSPIRV(program, &spirv)) { |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | return SPIRVtoHLSL(spirv, out); |
| 1815 | } |
| 1816 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1817 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1818 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1819 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1820 | return result; |
| 1821 | } |
| 1822 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1823 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1824 | StringStream buffer; |
| 1825 | bool result = this->toMetal(program, buffer); |
| 1826 | if (result) { |
| 1827 | *out = buffer.str(); |
| 1828 | } |
| 1829 | return result; |
| 1830 | } |
| 1831 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1832 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1833 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1834 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1835 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1836 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1837 | fSource = nullptr; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1838 | return result; |
| 1839 | } |
| 1840 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1841 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1842 | fSource = program.fSource.get(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1843 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1844 | bool result = cg.generateCode(); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1845 | fSource = nullptr; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1846 | return result; |
| 1847 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1848 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1849 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1850 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 1851 | |
| 1852 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 1853 | bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1854 | fSource = program.fSource.get(); |
| 1855 | StringStream buffer; |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 1856 | PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1857 | bool result = cg.generateCode(); |
| 1858 | fSource = nullptr; |
| 1859 | if (result) { |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 1860 | outArgs->fCode = buffer.str(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1861 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1862 | return result; |
| 1863 | } |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1864 | #endif |
| 1865 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1866 | std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) { |
Mike Klein | 853d4ed | 2020-08-20 00:41:00 +0000 | [diff] [blame] | 1867 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
Brian Osman | 808f021 | 2020-01-21 15:36:47 -0500 | [diff] [blame] | 1868 | fSource = program.fSource.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1869 | std::unique_ptr<ByteCode> result(new ByteCode()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1870 | ByteCodeGenerator cg(fContext.get(), &program, this, result.get()); |
| 1871 | bool success = cg.generateCode(); |
| 1872 | fSource = nullptr; |
| 1873 | if (success) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1874 | return result; |
| 1875 | } |
Mike Klein | 853d4ed | 2020-08-20 00:41:00 +0000 | [diff] [blame] | 1876 | #else |
| 1877 | ABORT("ByteCode interpreter not enabled"); |
| 1878 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1879 | return nullptr; |
| 1880 | } |
| 1881 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1882 | const char* Compiler::OperatorName(Token::Kind op) { |
| 1883 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1884 | case Token::Kind::TK_PLUS: return "+"; |
| 1885 | case Token::Kind::TK_MINUS: return "-"; |
| 1886 | case Token::Kind::TK_STAR: return "*"; |
| 1887 | case Token::Kind::TK_SLASH: return "/"; |
| 1888 | case Token::Kind::TK_PERCENT: return "%"; |
| 1889 | case Token::Kind::TK_SHL: return "<<"; |
| 1890 | case Token::Kind::TK_SHR: return ">>"; |
| 1891 | case Token::Kind::TK_LOGICALNOT: return "!"; |
| 1892 | case Token::Kind::TK_LOGICALAND: return "&&"; |
| 1893 | case Token::Kind::TK_LOGICALOR: return "||"; |
| 1894 | case Token::Kind::TK_LOGICALXOR: return "^^"; |
| 1895 | case Token::Kind::TK_BITWISENOT: return "~"; |
| 1896 | case Token::Kind::TK_BITWISEAND: return "&"; |
| 1897 | case Token::Kind::TK_BITWISEOR: return "|"; |
| 1898 | case Token::Kind::TK_BITWISEXOR: return "^"; |
| 1899 | case Token::Kind::TK_EQ: return "="; |
| 1900 | case Token::Kind::TK_EQEQ: return "=="; |
| 1901 | case Token::Kind::TK_NEQ: return "!="; |
| 1902 | case Token::Kind::TK_LT: return "<"; |
| 1903 | case Token::Kind::TK_GT: return ">"; |
| 1904 | case Token::Kind::TK_LTEQ: return "<="; |
| 1905 | case Token::Kind::TK_GTEQ: return ">="; |
| 1906 | case Token::Kind::TK_PLUSEQ: return "+="; |
| 1907 | case Token::Kind::TK_MINUSEQ: return "-="; |
| 1908 | case Token::Kind::TK_STAREQ: return "*="; |
| 1909 | case Token::Kind::TK_SLASHEQ: return "/="; |
| 1910 | case Token::Kind::TK_PERCENTEQ: return "%="; |
| 1911 | case Token::Kind::TK_SHLEQ: return "<<="; |
| 1912 | case Token::Kind::TK_SHREQ: return ">>="; |
| 1913 | case Token::Kind::TK_LOGICALANDEQ: return "&&="; |
| 1914 | case Token::Kind::TK_LOGICALOREQ: return "||="; |
| 1915 | case Token::Kind::TK_LOGICALXOREQ: return "^^="; |
| 1916 | case Token::Kind::TK_BITWISEANDEQ: return "&="; |
| 1917 | case Token::Kind::TK_BITWISEOREQ: return "|="; |
| 1918 | case Token::Kind::TK_BITWISEXOREQ: return "^="; |
| 1919 | case Token::Kind::TK_PLUSPLUS: return "++"; |
| 1920 | case Token::Kind::TK_MINUSMINUS: return "--"; |
| 1921 | case Token::Kind::TK_COMMA: return ","; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1922 | default: |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1923 | ABORT("unsupported operator: %d\n", (int) op); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | bool Compiler::IsAssignment(Token::Kind op) { |
| 1929 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1930 | case Token::Kind::TK_EQ: // fall through |
| 1931 | case Token::Kind::TK_PLUSEQ: // fall through |
| 1932 | case Token::Kind::TK_MINUSEQ: // fall through |
| 1933 | case Token::Kind::TK_STAREQ: // fall through |
| 1934 | case Token::Kind::TK_SLASHEQ: // fall through |
| 1935 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 1936 | case Token::Kind::TK_SHLEQ: // fall through |
| 1937 | case Token::Kind::TK_SHREQ: // fall through |
| 1938 | case Token::Kind::TK_BITWISEOREQ: // fall through |
| 1939 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
| 1940 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 1941 | case Token::Kind::TK_LOGICALOREQ: // fall through |
| 1942 | case Token::Kind::TK_LOGICALXOREQ: // fall through |
| 1943 | case Token::Kind::TK_LOGICALANDEQ: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1944 | return true; |
| 1945 | default: |
| 1946 | return false; |
| 1947 | } |
| 1948 | } |
| 1949 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 1950 | Token::Kind Compiler::RemoveAssignment(Token::Kind op) { |
| 1951 | switch (op) { |
| 1952 | case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS; |
| 1953 | case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS; |
| 1954 | case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR; |
| 1955 | case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH; |
| 1956 | case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT; |
| 1957 | case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL; |
| 1958 | case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR; |
| 1959 | case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR; |
| 1960 | case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR; |
| 1961 | case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND; |
| 1962 | case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR; |
| 1963 | case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR; |
| 1964 | case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND; |
| 1965 | default: return op; |
| 1966 | } |
| 1967 | } |
| 1968 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1969 | Position Compiler::position(int offset) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1970 | SkASSERT(fSource); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1971 | int line = 1; |
| 1972 | int column = 1; |
| 1973 | for (int i = 0; i < offset; i++) { |
| 1974 | if ((*fSource)[i] == '\n') { |
| 1975 | ++line; |
| 1976 | column = 1; |
| 1977 | } |
| 1978 | else { |
| 1979 | ++column; |
| 1980 | } |
| 1981 | } |
| 1982 | return Position(line, column); |
| 1983 | } |
| 1984 | |
| 1985 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1986 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1987 | Position pos = this->position(offset); |
| 1988 | fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1991 | String Compiler::errorText() { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1992 | this->writeErrorCount(); |
| 1993 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1994 | String result = fErrorText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1995 | return result; |
| 1996 | } |
| 1997 | |
| 1998 | void Compiler::writeErrorCount() { |
| 1999 | if (fErrorCount) { |
| 2000 | fErrorText += to_string(fErrorCount) + " error"; |
| 2001 | if (fErrorCount > 1) { |
| 2002 | fErrorText += "s"; |
| 2003 | } |
| 2004 | fErrorText += "\n"; |
| 2005 | } |
| 2006 | } |
| 2007 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 2008 | } // namespace SkSL |