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