Sfoglia il codice sorgente

[spirv] Add get constant composite API to spirv context.

Ehsan 6 anni fa
parent
commit
adb52d2474

+ 5 - 2
tools/clang/include/clang/SPIRV/SPIRVContext.h

@@ -219,7 +219,10 @@ public:
   SpirvConstant *getConstantFloat32(float value, bool specConst = false);
   SpirvConstant *getConstantFloat64(double value, bool specConst = false);
   SpirvConstant *getConstantBool(bool value, bool specConst = false);
-  // TODO: Add getConstant* methods for other types.
+  SpirvConstant *
+  getConstantComposite(QualType compositeType,
+                       llvm::ArrayRef<SpirvConstant *> constituents,
+                       bool specConst = false);
 
 private:
   template <class T>
@@ -317,13 +320,13 @@ private:
   // Unique constants
   // We currently do a linear search to find an existing constant (if any). This
   // can be done in a more efficient way if needed.
+  llvm::SmallVector<SpirvConstantComposite *, 8> compositeConstants;
   llvm::SmallVector<SpirvConstantInteger *, 8> integerConstants;
   llvm::SmallVector<SpirvConstantFloat *, 8> floatConstants;
   SpirvConstantBoolean *boolTrueConstant;
   SpirvConstantBoolean *boolFalseConstant;
   SpirvConstantBoolean *boolTrueSpecConstant;
   SpirvConstantBoolean *boolFalseSpecConstant;
-  // TODO: Add vectors of other constant types here.
 };
 
 } // end namespace spirv

+ 21 - 0
tools/clang/lib/SPIRV/SPIRVContext.cpp

@@ -394,5 +394,26 @@ SpirvConstant *SpirvContext::getConstantBool(bool value, bool specConst) {
   return boolConst;
 }
 
+SpirvConstant *
+SpirvContext::getConstantComposite(QualType compositeType,
+                                   llvm::ArrayRef<SpirvConstant *> constituents,
+                                   bool specConst) {
+  SpirvConstantComposite tempConstant(compositeType, constituents, specConst);
+  auto found =
+      std::find_if(compositeConstants.begin(), compositeConstants.end(),
+                   [&tempConstant](SpirvConstantComposite *cachedConstant) {
+                     return tempConstant == *cachedConstant;
+                   });
+
+  if (found != compositeConstants.end())
+    return *found;
+
+  // Couldn't find the constant. Create one.
+  auto *compositeConst =
+      new (this) SpirvConstantComposite(compositeType, constituents, specConst);
+  compositeConstants.push_back(compositeConst);
+  return compositeConst;
+}
+
 } // end namespace spirv
 } // end namespace clang