Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

external-swiftshader: Commit

external/swiftshader


Commit MetaInfo

Revision2f4b603e72ca22585bb090f9ff67517608a466f6 (tree)
Time2020-04-14 18:43:47
AuthorNicolas Capens <capn@goog...>
CommiterNicolas Capens

Log Message

Only store component count in Operand

The Operand class is a low-level abstraction of rvalues and constants.
It should not carry the SPIR-V type ID. We only need the size in
components.

Bug: b/129000021
Change-Id: I6cb3ed6341b1ccf5ef759075d7410ba447617c8b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43693
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>

Change Summary

Incremental Difference

--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -795,7 +795,7 @@ SpirvShader::Object &SpirvShader::CreateConstant(InsnIterator insn)
795795 auto &objectTy = getType(typeId);
796796 object.kind = Object::Kind::Constant;
797797 object.definition = insn;
798- object.constantValue = std::unique_ptr<uint32_t[]>(new uint32_t[objectTy.componentCount]);
798+ object.constantValue.resize(objectTy.componentCount);
799799
800800 return object;
801801 }
@@ -2138,7 +2138,7 @@ SpirvShader::EmitResult SpirvShader::EmitSelect(InsnIterator insn, EmitState *st
21382138 auto &type = getType(insn.resultTypeId());
21392139 auto &dst = state->createIntermediate(insn.resultId(), type.componentCount);
21402140 auto cond = Operand(this, state, insn.word(3));
2141- auto condIsScalar = (getType(cond).componentCount == 1);
2141+ auto condIsScalar = (cond.componentCount == 1);
21422142 auto lhs = Operand(this, state, insn.word(4));
21432143 auto rhs = Operand(this, state, insn.word(5));
21442144
@@ -2419,11 +2419,18 @@ VkShaderStageFlagBits SpirvShader::executionModelToStage(spv::ExecutionModel mod
24192419 }
24202420 }
24212421
2422-SpirvShader::Operand::Operand(SpirvShader const *shader, EmitState const *state, SpirvShader::Object::ID objId)
2423- : obj(shader->getObject(objId))
2424- , intermediate(obj.kind == SpirvShader::Object::Kind::Intermediate ? &state->getIntermediate(objId) : nullptr)
2422+SpirvShader::Operand::Operand(const SpirvShader *shader, const EmitState *state, SpirvShader::Object::ID objectId)
2423+ : Operand(state, shader->getObject(objectId))
24252424 {}
24262425
2426+SpirvShader::Operand::Operand(const EmitState *state, const Object &object)
2427+ : constant(object.constantValue.data())
2428+ , intermediate(object.kind == SpirvShader::Object::Kind::Intermediate ? &state->getIntermediate(object.id()) : nullptr)
2429+ , componentCount(intermediate ? intermediate->componentCount : object.constantValue.size())
2430+{
2431+ ASSERT(intermediate || (object.kind == SpirvShader::Object::Kind::Constant));
2432+}
2433+
24272434 SpirvRoutine::SpirvRoutine(vk::PipelineLayout const *pipelineLayout)
24282435 : pipelineLayout(pipelineLayout)
24292436 {
--- a/src/Pipeline/SpirvShader.hpp
+++ b/src/Pipeline/SpirvShader.hpp
@@ -72,10 +72,10 @@ class Intermediate
7272 {
7373 public:
7474 Intermediate(uint32_t componentCount)
75- : scalar(new rr::Value *[componentCount])
76- , componentCount(componentCount)
75+ : componentCount(componentCount)
76+ , scalar(new rr::Value *[componentCount])
7777 {
78- memset(scalar, 0, sizeof(rr::Value *) * componentCount);
78+ for(auto i = 0u; i < componentCount; i++) { scalar[i] = nullptr; }
7979 }
8080
8181 ~Intermediate()
@@ -119,6 +119,8 @@ public:
119119 Intermediate &operator=(Intermediate const &) = delete;
120120 Intermediate &operator=(Intermediate &&) = delete;
121121
122+ const uint32_t componentCount;
123+
122124 private:
123125 void emplace(uint32_t i, rr::Value *value)
124126 {
@@ -128,7 +130,6 @@ private:
128130 }
129131
130132 rr::Value **const scalar;
131- uint32_t componentCount;
132133 };
133134
134135 class SpirvShader
@@ -276,7 +277,7 @@ public:
276277 Object::ID id() const { return definition.resultId(); }
277278
278279 InsnIterator definition;
279- std::unique_ptr<uint32_t[]> constantValue = nullptr;
280+ std::vector<uint32_t> constantValue;
280281
281282 enum class Kind
282283 {
@@ -989,11 +990,8 @@ private:
989990 // significantly different based on whether the value is uniform across lanes.
990991 class Operand
991992 {
992- SpirvShader::Object const &obj;
993- Intermediate const *intermediate;
994-
995993 public:
996- Operand(SpirvShader const *shader, EmitState const *state, SpirvShader::Object::ID objId);
994+ Operand(const SpirvShader *shader, const EmitState *state, SpirvShader::Object::ID objectId);
997995
998996 RValue<SIMD::Float> Float(uint32_t i) const
999997 {
@@ -1005,9 +1003,7 @@ private:
10051003 // Constructing a constant SIMD::Float is not guaranteed to preserve the data's exact
10061004 // bit pattern, but SPIR-V provides 32-bit words representing "the bit pattern for the constant".
10071005 // Thus we must first construct an integer constant, and bitcast to float.
1008- ASSERT(obj.kind == SpirvShader::Object::Kind::Constant);
1009- auto constantValue = reinterpret_cast<uint32_t *>(obj.constantValue.get());
1010- return As<SIMD::Float>(SIMD::UInt(constantValue[i]));
1006+ return As<SIMD::Float>(SIMD::UInt(constant[i]));
10111007 }
10121008
10131009 RValue<SIMD::Int> Int(uint32_t i) const
@@ -1016,9 +1012,8 @@ private:
10161012 {
10171013 return intermediate->Int(i);
10181014 }
1019- ASSERT(obj.kind == SpirvShader::Object::Kind::Constant);
1020- auto constantValue = reinterpret_cast<int *>(obj.constantValue.get());
1021- return SIMD::Int(constantValue[i]);
1015+
1016+ return SIMD::Int(constant[i]);
10221017 }
10231018
10241019 RValue<SIMD::UInt> UInt(uint32_t i) const
@@ -1027,15 +1022,19 @@ private:
10271022 {
10281023 return intermediate->UInt(i);
10291024 }
1030- ASSERT(obj.kind == SpirvShader::Object::Kind::Constant);
1031- auto constantValue = reinterpret_cast<uint32_t *>(obj.constantValue.get());
1032- return SIMD::UInt(constantValue[i]);
1033- }
10341025
1035- Type::ID typeId() const
1036- {
1037- return obj.typeId();
1026+ return SIMD::UInt(constant[i]);
10381027 }
1028+
1029+ private:
1030+ // Delegate constructor
1031+ Operand(const EmitState *state, const Object &object);
1032+
1033+ const uint32_t *constant;
1034+ const Intermediate *intermediate;
1035+
1036+ public:
1037+ const uint32_t componentCount;
10391038 };
10401039
10411040 Type const &getType(Type::ID id) const
@@ -1050,11 +1049,6 @@ private:
10501049 return getType(object.typeId());
10511050 }
10521051
1053- Type const &getType(const Operand &operand) const
1054- {
1055- return getType(operand.typeId());
1056- }
1057-
10581052 Object const &getObject(Object::ID id) const
10591053 {
10601054 auto it = defs.find(id);
--- a/src/Pipeline/SpirvShaderArithmetic.cpp
+++ b/src/Pipeline/SpirvShaderArithmetic.cpp
@@ -41,12 +41,11 @@ SpirvShader::EmitResult SpirvShader::EmitMatrixTimesVector(InsnIterator insn, Em
4141 auto &dst = state->createIntermediate(insn.resultId(), type.componentCount);
4242 auto lhs = Operand(this, state, insn.word(3));
4343 auto rhs = Operand(this, state, insn.word(4));
44- auto rhsType = getType(rhs);
4544
4645 for(auto i = 0u; i < type.componentCount; i++)
4746 {
4847 SIMD::Float v = lhs.Float(i) * rhs.Float(0);
49- for(auto j = 1u; j < rhsType.componentCount; j++)
48+ for(auto j = 1u; j < rhs.componentCount; j++)
5049 {
5150 v += lhs.Float(i + type.componentCount * j) * rhs.Float(j);
5251 }
@@ -62,14 +61,13 @@ SpirvShader::EmitResult SpirvShader::EmitVectorTimesMatrix(InsnIterator insn, Em
6261 auto &dst = state->createIntermediate(insn.resultId(), type.componentCount);
6362 auto lhs = Operand(this, state, insn.word(3));
6463 auto rhs = Operand(this, state, insn.word(4));
65- auto lhsType = getType(lhs);
6664
6765 for(auto i = 0u; i < type.componentCount; i++)
6866 {
69- SIMD::Float v = lhs.Float(0) * rhs.Float(i * lhsType.componentCount);
70- for(auto j = 1u; j < lhsType.componentCount; j++)
67+ SIMD::Float v = lhs.Float(0) * rhs.Float(i * lhs.componentCount);
68+ for(auto j = 1u; j < lhs.componentCount; j++)
7169 {
72- v += lhs.Float(j) * rhs.Float(i * lhsType.componentCount + j);
70+ v += lhs.Float(j) * rhs.Float(i * lhs.componentCount + j);
7371 }
7472 dst.move(i, v);
7573 }
@@ -110,17 +108,9 @@ SpirvShader::EmitResult SpirvShader::EmitOuterProduct(InsnIterator insn, EmitSta
110108 auto &dst = state->createIntermediate(insn.resultId(), type.componentCount);
111109 auto lhs = Operand(this, state, insn.word(3));
112110 auto rhs = Operand(this, state, insn.word(4));
113- auto &lhsType = getType(lhs);
114- auto &rhsType = getType(rhs);
115111
116- ASSERT(type.definition.opcode() == spv::OpTypeMatrix);
117- ASSERT(lhsType.definition.opcode() == spv::OpTypeVector);
118- ASSERT(rhsType.definition.opcode() == spv::OpTypeVector);
119- ASSERT(getType(lhsType.element).opcode() == spv::OpTypeFloat);
120- ASSERT(getType(rhsType.element).opcode() == spv::OpTypeFloat);
121-
122- auto numRows = lhsType.definition.word(3);
123- auto numCols = rhsType.definition.word(3);
112+ auto numRows = lhs.componentCount;
113+ auto numCols = rhs.componentCount;
124114
125115 for(auto col = 0u; col < numCols; col++)
126116 {
--- a/src/Pipeline/SpirvShaderControlFlow.cpp
+++ b/src/Pipeline/SpirvShaderControlFlow.cpp
@@ -496,7 +496,7 @@ SpirvShader::EmitResult SpirvShader::EmitBranchConditional(InsnIterator insn, Em
496496 auto falseBlockId = Block::ID(block.branchInstruction.word(3));
497497
498498 auto cond = Operand(this, state, condId);
499- ASSERT_MSG(getType(cond).componentCount == 1, "Condition must be a Boolean type scalar");
499+ ASSERT_MSG(getType(getObject(condId)).componentCount == 1, "Condition must be a Boolean type scalar");
500500
501501 // TODO: Optimize for case where all lanes take same path.
502502
@@ -515,7 +515,7 @@ SpirvShader::EmitResult SpirvShader::EmitSwitch(InsnIterator insn, EmitState *st
515515 auto selId = Object::ID(block.branchInstruction.word(1));
516516
517517 auto sel = Operand(this, state, selId);
518- ASSERT_MSG(getType(sel).componentCount == 1, "Selector must be a scalar");
518+ ASSERT_MSG(sel.componentCount == 1, "Selector must be a scalar");
519519
520520 auto numCases = (block.branchInstruction.wordCount() - 3) / 2;
521521
--- a/src/Pipeline/SpirvShaderDebugger.cpp
+++ b/src/Pipeline/SpirvShaderDebugger.cpp
@@ -1153,11 +1153,12 @@ void SpirvShader::Impl::Debugger::exposeVariable(
11531153 EmitState *state,
11541154 int wordOffset /* = 0 */) const
11551155 {
1156+ auto &obj = shader->getObject(id);
1157+
11561158 if(type != nullptr)
11571159 {
11581160 if(auto ty = debug::cast<debug::BasicType>(type))
11591161 {
1160- auto &obj = shader->getObject(id);
11611162 SIMD::Int val;
11621163 switch(obj.kind)
11631164 {
@@ -1288,7 +1289,7 @@ void SpirvShader::Impl::Debugger::exposeVariable(
12881289
12891290 // No debug type information. Derive from SPIR-V.
12901291 Operand val(shader, state, id);
1291- switch(shader->getType(val).opcode())
1292+ switch(shader->getType(obj).opcode())
12921293 {
12931294 case spv::OpTypeInt:
12941295 {
@@ -1302,7 +1303,7 @@ void SpirvShader::Impl::Debugger::exposeVariable(
13021303 break;
13031304 case spv::OpTypeVector:
13041305 {
1305- auto count = shader->getType(val).definition.word(3);
1306+ auto count = shader->getType(obj).definition.word(3);
13061307 switch(count)
13071308 {
13081309 case 1:
--- a/src/Pipeline/SpirvShaderGLSLstd450.cpp
+++ b/src/Pipeline/SpirvShaderGLSLstd450.cpp
@@ -338,12 +338,11 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
338338 {
339339 auto p0 = Operand(this, state, insn.word(5));
340340 auto p1 = Operand(this, state, insn.word(6));
341- auto p0Type = getType(p0);
342341
343342 // sqrt(dot(p0-p1, p0-p1))
344343 SIMD::Float d = (p0.Float(0) - p1.Float(0)) * (p0.Float(0) - p1.Float(0));
345344
346- for(auto i = 1u; i < p0Type.componentCount; i++)
345+ for(auto i = 1u; i < p0.componentCount; i++)
347346 {
348347 d += (p0.Float(i) - p1.Float(i)) * (p0.Float(i) - p1.Float(i));
349348 }
@@ -378,14 +377,13 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
378377 case GLSLstd450ModfStruct:
379378 {
380379 auto val = Operand(this, state, insn.word(5));
381- auto valTy = getType(val);
382380
383- for(auto i = 0u; i < valTy.componentCount; i++)
381+ for(auto i = 0u; i < val.componentCount; i++)
384382 {
385383 SIMD::Float whole, frac;
386384 std::tie(whole, frac) = Modf(val.Float(i));
387385 dst.move(i, frac);
388- dst.move(i + valTy.componentCount, whole);
386+ dst.move(i + val.componentCount, whole);
389387 }
390388 break;
391389 }
@@ -527,12 +525,12 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
527525 case GLSLstd450FrexpStruct:
528526 {
529527 auto val = Operand(this, state, insn.word(5));
530- auto numComponents = getType(val).componentCount;
531- for(auto i = 0u; i < numComponents; i++)
528+
529+ for(auto i = 0u; i < val.componentCount; i++)
532530 {
533531 auto significandAndExponent = Frexp(val.Float(i));
534532 dst.move(i, significandAndExponent.first);
535- dst.move(i + numComponents, significandAndExponent.second);
533+ dst.move(val.componentCount + i, significandAndExponent.second);
536534 }
537535 break;
538536 }
@@ -785,8 +783,8 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
785783 case GLSLstd450Determinant:
786784 {
787785 auto mat = Operand(this, state, insn.word(5));
788- auto numComponents = getType(mat).componentCount;
789- switch(numComponents)
786+
787+ switch(mat.componentCount)
790788 {
791789 case 4: // 2x2
792790 dst.move(0, Determinant(
@@ -807,15 +805,15 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
807805 mat.Float(12), mat.Float(13), mat.Float(14), mat.Float(15)));
808806 break;
809807 default:
810- UNREACHABLE("GLSLstd450Determinant can only operate with square matrices. Got %d elements", int(numComponents));
808+ UNREACHABLE("GLSLstd450Determinant can only operate with square matrices. Got %d elements", int(mat.componentCount));
811809 }
812810 break;
813811 }
814812 case GLSLstd450MatrixInverse:
815813 {
816814 auto mat = Operand(this, state, insn.word(5));
817- auto numComponents = getType(mat).componentCount;
818- switch(numComponents)
815+
816+ switch(mat.componentCount)
819817 {
820818 case 4: // 2x2
821819 {
@@ -854,7 +852,7 @@ SpirvShader::EmitResult SpirvShader::EmitExtGLSLstd450(InsnIterator insn, EmitSt
854852 break;
855853 }
856854 default:
857- UNREACHABLE("GLSLstd450MatrixInverse can only operate with square matrices. Got %d elements", int(numComponents));
855+ UNREACHABLE("GLSLstd450MatrixInverse can only operate with square matrices. Got %d elements", int(mat.componentCount));
858856 }
859857 break;
860858 }
--- a/src/Pipeline/SpirvShaderImage.cpp
+++ b/src/Pipeline/SpirvShaderImage.cpp
@@ -119,7 +119,6 @@ SpirvShader::EmitResult SpirvShader::EmitImageSample(ImageInstruction instructio
119119 auto samplerDescriptor = (sampledImage.opcode() == spv::OpSampledImage) ? state->getPointer(sampledImage.definition.word(4)).base : imageDescriptor;
120120
121121 auto coordinate = Operand(this, state, coordinateId);
122- auto &coordinateType = getType(coordinate);
123122
124123 Pointer<Byte> sampler = samplerDescriptor + OFFSET(vk::SampledImageDescriptor, sampler); // vk::Sampler*
125124 Pointer<Byte> texture = imageDescriptor + OFFSET(vk::SampledImageDescriptor, texture); // sw::Texture*
@@ -204,7 +203,7 @@ SpirvShader::EmitResult SpirvShader::EmitImageSample(ImageInstruction instructio
204203
205204 Array<SIMD::Float> in(16); // Maximum 16 input parameter components.
206205
207- uint32_t coordinates = coordinateType.componentCount - instruction.isProj();
206+ uint32_t coordinates = coordinate.componentCount - instruction.isProj();
208207 instruction.coordinates = coordinates;
209208
210209 uint32_t i = 0;
@@ -246,17 +245,16 @@ SpirvShader::EmitResult SpirvShader::EmitImageSample(ImageInstruction instructio
246245 {
247246 auto dxValue = Operand(this, state, gradDxId);
248247 auto dyValue = Operand(this, state, gradDyId);
249- auto &dxyType = getType(dxValue);
250- ASSERT(dxyType.componentCount == getType(dyValue).componentCount);
248+ ASSERT(dxValue.componentCount == dxValue.componentCount);
251249
252- instruction.grad = dxyType.componentCount;
250+ instruction.grad = dxValue.componentCount;
253251
254- for(uint32_t j = 0; j < dxyType.componentCount; j++, i++)
252+ for(uint32_t j = 0; j < dxValue.componentCount; j++, i++)
255253 {
256254 in[i] = dxValue.Float(j);
257255 }
258256
259- for(uint32_t j = 0; j < dxyType.componentCount; j++, i++)
257+ for(uint32_t j = 0; j < dxValue.componentCount; j++, i++)
260258 {
261259 in[i] = dyValue.Float(j);
262260 }
@@ -273,11 +271,9 @@ SpirvShader::EmitResult SpirvShader::EmitImageSample(ImageInstruction instructio
273271 if(constOffset)
274272 {
275273 auto offsetValue = Operand(this, state, offsetId);
276- auto &offsetType = getType(offsetValue);
274+ instruction.offset = offsetValue.componentCount;
277275
278- instruction.offset = offsetType.componentCount;
279-
280- for(uint32_t j = 0; j < offsetType.componentCount; j++, i++)
276+ for(uint32_t j = 0; j < offsetValue.componentCount; j++, i++)
281277 {
282278 in[i] = As<SIMD::Float>(offsetValue.Int(j)); // Integer values, but transfered as float.
283279 }
@@ -383,7 +379,7 @@ void SpirvShader::GetImageDimensions(EmitState const *state, Type const &resultT
383379 if(lodId != 0)
384380 {
385381 auto lodVal = Operand(this, state, lodId);
386- ASSERT(getType(lodVal).componentCount == 1);
382+ ASSERT(lodVal.componentCount == 1);
387383 auto lod = lodVal.Int(0);
388384 auto one = SIMD::Int(1);
389385 for(uint32_t i = 0; i < dimensions; i++)
@@ -476,12 +472,12 @@ SIMD::Pointer SpirvShader::GetTexelAddress(EmitState const *state, SIMD::Pointer
476472 auto routine = state->routine;
477473 bool isArrayed = imageType.definition.word(5) != 0;
478474 auto dim = static_cast<spv::Dim>(imageType.definition.word(3));
479- int dims = getType(coordinate).componentCount - (isArrayed ? 1 : 0);
475+ int dims = coordinate.componentCount - (isArrayed ? 1 : 0);
480476
481477 SIMD::Int u = coordinate.Int(0);
482478 SIMD::Int v = SIMD::Int(0);
483479
484- if(getType(coordinate).componentCount > 1)
480+ if(coordinate.componentCount > 1)
485481 {
486482 v = coordinate.Int(1);
487483 }
--- a/src/Pipeline/SpirvShaderMemory.cpp
+++ b/src/Pipeline/SpirvShaderMemory.cpp
@@ -99,7 +99,7 @@ SpirvShader::EmitResult SpirvShader::EmitStore(InsnIterator insn, EmitState *sta
9999 if(object.kind == Object::Kind::Constant)
100100 {
101101 // Constant source data.
102- const uint32_t *src = object.constantValue.get();
102+ const uint32_t *src = object.constantValue.data();
103103 VisitMemoryObject(pointerId, [&](const MemoryElement &el) {
104104 auto p = ptr + el.offset;
105105 if(interleavedByLane) { p = InterleaveByLane(p); }
Show on old repository browser