From a9c41029b09e521cc46f7f0885e4975e9eefbdd6 Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Tue, 22 Nov 2022 11:57:11 +0200 Subject: [PATCH 1/2] ApplyCircOp: Return value instead of reference-to-local filteredTypes is local to filterRegSize and gets destroyed at the end of the function. Returning a TypeRange on it is UB, so return it by value instead. --- include/QuantumSSAOps.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/QuantumSSAOps.td b/include/QuantumSSAOps.td index ed412f0..dfeea20 100644 --- a/include/QuantumSSAOps.td +++ b/include/QuantumSSAOps.td @@ -997,7 +997,7 @@ def ApplyCircOp : QuantumSSA_Op<"apply"> { }]; let extraClassDeclaration = [{ - static TypeRange filterRegSize(TypeRange types, MLIRContext *ctx) { + static llvm::SmallVector filterRegSize(TypeRange types, MLIRContext *ctx) { Type rstate = RstateType::get(ctx, llvm::None); llvm::SmallVector filteredTypes; filteredTypes.reserve(types.size()); From 88a677dd91d9bbba2fe6888a7b26b5e2ba4510ec Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Tue, 22 Nov 2022 11:58:42 +0200 Subject: [PATCH 2/2] ExtractOp: Preserve container during iteration It looks like temporaries in the range-expression get destroyed after it's evaluated. In this case the range-expression is a call to `Optional::operator*` that returns a reference to the Optional's inner value, but the Optional itself is a temporary. Fix this by assigning the value inside the optional to a variable. --- include/QuantumSSAOps.td | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/QuantumSSAOps.td b/include/QuantumSSAOps.td index dfeea20..39db062 100644 --- a/include/QuantumSSAOps.td +++ b/include/QuantumSSAOps.td @@ -242,7 +242,8 @@ def ExtractOp : QuantumSSA_Op<"extract", [NoSideEffect]> { // bounds checking (if possible) if(this->const_idx()) { - for (auto idxAttr : *this->const_idx()) { + auto idxs = *this->const_idx(); + for (auto idxAttr : idxs) { int64_t idx = idxAttr.dyn_cast().getInt(); if (idx >= regsize) return this->emitOpError() << "detected index value out of bounds, idx: "