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