[Sparse] Improve error message for captured pointer
diff --git a/enzyme/Enzyme/FunctionUtils.cpp b/enzyme/Enzyme/FunctionUtils.cpp
index b74dc80..73ac945 100644
--- a/enzyme/Enzyme/FunctionUtils.cpp
+++ b/enzyme/Enzyme/FunctionUtils.cpp
@@ -8038,6 +8038,11 @@
       continue;
     }
     if (auto SI = dyn_cast<StoreInst>(U)) {
+      if (SI->getValueOperand() == val) {
+          EmitFailure("IllegalSparse", SI->getDebugLoc(), SI,
+                      " result of __enzyme_todense was captured (e.g. stored into memory and not directly loaded from or stored into) ", *SI);
+	  continue;
+      }
       assert(SI->getValueOperand() != val);
       auto diff = toInt(B, replacements[SI->getPointerOperand()]);
       SmallVector<Value *, 2> args;
diff --git a/enzyme/test/Integration/Sparse/ringspring.cpp b/enzyme/test/Integration/Sparse/ringspring.cpp
index dd4242a..001d6bc 100644
--- a/enzyme/test/Integration/Sparse/ringspring.cpp
+++ b/enzyme/test/Integration/Sparse/ringspring.cpp
@@ -1,5 +1,6 @@
 // This should work on LLVM 7, 8, 9, however in CI the version of clang installed on Ubuntu 18.04 cannot load
 // a clang plugin properly without segfaulting on exit. This is fine on Ubuntu 20.04 or later LLVM versions...
+// RUN: if [ %llvmver -ge 12 ]; then %clang++ -fno-exceptions  -ffast-math -mllvm -enable-load-pre=0 -std=c++11 -O0 %s -S -emit-llvm -o - %loadClangEnzyme -mllvm -enzyme-auto-sparsity=1 | %lli - ; fi
 // RUN: if [ %llvmver -ge 12 ]; then %clang++ -fno-exceptions  -ffast-math -mllvm -enable-load-pre=0 -std=c++11 -O1 %s -S -emit-llvm -o - %loadClangEnzyme -mllvm -enzyme-auto-sparsity=1 | %lli - ; fi
 // RUN: if [ %llvmver -ge 12 ]; then %clang++ -fno-exceptions  -ffast-math -mllvm -enable-load-pre=0 -std=c++11 -O2 %s -S -emit-llvm -o - %loadClangEnzyme -mllvm -enzyme-auto-sparsity=1  | %lli - ; fi
 // RUN: if [ %llvmver -ge 12 ]; then %clang++ -fno-exceptions  -ffast-math -mllvm -enable-load-pre=0 -std=c++11 -O3 %s -S -emit-llvm -o - %loadClangEnzyme  -mllvm -enzyme-auto-sparsity=1 | %lli - ; fi