瀏覽代碼

[spirv] Emit fatal error for failing to find counter variable (#939)

Lei Zhang 7 年之前
父節點
當前提交
f9b53ba663

+ 4 - 3
tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

@@ -521,11 +521,12 @@ uint32_t DeclResultIdMapper::getOrRegisterFnResultId(const FunctionDecl *fn) {
   return id;
 }
 
-const CounterIdAliasPair &
+const CounterIdAliasPair *
 DeclResultIdMapper::getCounterIdAliasPair(const ValueDecl *decl) {
   const auto counter = counterVars.find(decl);
-  assert(counter != counterVars.end());
-  return counter->second;
+  if (counter != counterVars.end())
+    return &counter->second;
+  return nullptr;
 }
 
 void DeclResultIdMapper::createCounterVar(const ValueDecl *decl, bool isAlias) {

+ 3 - 1
tools/clang/lib/SPIRV/DeclResultIdMapper.h

@@ -306,7 +306,9 @@ public:
 
   /// \brief Returns the associated counter's (<result-id>, is-alias-or-not)
   /// pair for the given {RW|Append|Consume}StructuredBuffer variable.
-  const CounterIdAliasPair &getCounterIdAliasPair(const ValueDecl *decl);
+  /// Returns nullptr if the given decl has no associated counter variable
+  /// created.
+  const CounterIdAliasPair *getCounterIdAliasPair(const ValueDecl *decl);
 
   /// \brief Returns the <type-id> for the given cbuffer, tbuffer,
   /// ConstantBuffer, TextureBuffer, or push constant block.

+ 21 - 5
tools/clang/lib/SPIRV/SPIRVEmitter.cpp

@@ -2609,7 +2609,7 @@ uint32_t SPIRVEmitter::incDecRWACSBufferCounter(const CXXMemberCallExpr *expr,
   const uint32_t counterPtrType = theBuilder.getPointerType(
       theBuilder.getInt32Type(), spv::StorageClass::Uniform);
   const uint32_t counterPtr = theBuilder.createAccessChain(
-      counterPtrType, counterPair.get(theBuilder, typeTranslator), {zero});
+      counterPtrType, counterPair->get(theBuilder, typeTranslator), {zero});
 
   uint32_t index = 0;
   if (isInc) {
@@ -2626,7 +2626,7 @@ uint32_t SPIRVEmitter::incDecRWACSBufferCounter(const CXXMemberCallExpr *expr,
   return index;
 }
 
-void SPIRVEmitter::tryToAssignCounterVar(const ValueDecl *dstDecl,
+bool SPIRVEmitter::tryToAssignCounterVar(const ValueDecl *dstDecl,
                                          const Expr *srcExpr) {
   // For parameters of forward-declared functions. We must make sure the
   // associated counter variable is created. But for forward-declared functions,
@@ -2637,12 +2637,28 @@ void SPIRVEmitter::tryToAssignCounterVar(const ValueDecl *dstDecl,
   if (TypeTranslator::isRWAppendConsumeSBuffer(getTypeOrFnRetType(dstDecl))) {
     // Internal RW/Append/Consume StructuredBuffer. We also need to
     // initialize the associated counter.
-    const auto &srcPair =
+    const auto *srcPair =
         declIdMapper.getCounterIdAliasPair(getReferencedDef(srcExpr));
-    const auto &dstPair = declIdMapper.getCounterIdAliasPair(dstDecl);
+    const auto *dstPair = declIdMapper.getCounterIdAliasPair(dstDecl);
 
-    dstPair.assign(srcPair, theBuilder, typeTranslator);
+    if (!srcPair) {
+      emitFatalError(
+          "cannot handle counter variable associated with the given expr",
+          srcExpr->getLocStart())
+          << srcExpr->getSourceRange();
+      return false;
+    }
+    if (!dstDecl) {
+      emitFatalError(
+          "cannot handle counter variable associated with the given decl",
+          dstDecl->getLocation());
+      return false;
+    }
+
+    dstPair->assign(*srcPair, theBuilder, typeTranslator);
   }
+
+  return true;
 }
 
 SpirvEvalInfo

+ 3 - 2
tools/clang/lib/SPIRV/SPIRVEmitter.h

@@ -671,10 +671,11 @@ private:
 
   /// Assigns the counter variable associated with srcExpr to the one associated
   /// with dstDecl if the dstDecl is an internal RW/Append/Consume structured
-  /// buffer.
+  /// buffer. Returns false if there is no associated counter variable for
+  /// srcExpr or dstDecl.
   ///
   /// Note: legalization specific code
-  void tryToAssignCounterVar(const ValueDecl *dstDecl, const Expr *srcExpr);
+  bool tryToAssignCounterVar(const ValueDecl *dstDecl, const Expr *srcExpr);
 
   /// \brief Loads numWords 32-bit unsigned integers or stores numWords 32-bit
   /// unsigned integers (based on the doStore parameter) to the given