[clang] [clang][Interp] Only evaluate the source array initialization of an `ArrayInitLoopExpr` once (PR #68039)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 12 23:39:31 PDT 2023
================
@@ -478,6 +480,43 @@ template <class Emitter> class SourceLocScope final {
bool Enabled = false;
};
+template <class Emitter> class StoredOpaqueValueScope final {
+public:
+ StoredOpaqueValueScope(ByteCodeExprGen<Emitter> *Ctx) : Ctx(Ctx) {}
+
+ bool VisitAndStoreOpaqueValue(const OpaqueValueExpr *Ove) {
+ assert(Ove && "OpaqueValueExpr is a nullptr!");
+ assert(!Ctx->OpaqueExprs.contains(Ove) &&
+ "OpaqueValueExpr already stored!");
+
+ std::optional<PrimType> CommonTy = Ctx->classify(Ove);
+ std::optional<unsigned> LocalIndex = Ctx->allocateLocalPrimitive(
+ Ove, *CommonTy, Ove->getType().isConstQualified());
+ if (!LocalIndex)
+ return false;
+
+ if (!Ctx->visit(Ove))
+ return false;
+
+ if (!Ctx->emitSetLocal(*CommonTy, *LocalIndex, Ove))
+ return false;
+
+ Ctx->OpaqueExprs.insert({Ove, *LocalIndex});
+ StoredValues.emplace_back(Ove);
+
+ return true;
+ }
+
+ ~StoredOpaqueValueScope() {
+ for (const auto *SV : StoredValues)
+ Ctx->OpaqueExprs.erase(SV);
+ }
+
+private:
+ ByteCodeExprGen<Emitter> *Ctx;
+ std::vector<const OpaqueValueExpr *> StoredValues;
----------------
tbaederr wrote:
Well you don't ever reuse the same `StoredOpaqueValueScope`, do you? The scope should add the value in its constructor and remove it in its destructor. So as long as we don't evaluate the same `OpaqueExpr` twice, we should be fine and no vector is needed.
https://github.com/llvm/llvm-project/pull/68039
More information about the cfe-commits
mailing list