[Mlir-commits] [mlir] [mlir][AsmParser] Avoid use of moved value (PR #108789)

Youngsuk Kim llvmlistbot at llvm.org
Sun Sep 15 21:13:24 PDT 2024


https://github.com/JOE1994 created https://github.com/llvm/llvm-project/pull/108789

'std::string detailData' is moved in the innermost loop of a 2-layer loop, but is written to throughout the whole duration of the 2-layer loop.

After move, std::string is in an unspecified state (implementation-dependent).

Avoid using a moved value, as it incurs undefined behavior.

>From 0183078f70a1252b2e2f92855ef0a978dd5ff13a Mon Sep 17 00:00:00 2001
From: JOE1994 <joseph942010 at gmail.com>
Date: Sun, 15 Sep 2024 23:10:44 -0400
Subject: [PATCH] [mlir][AsmParser] Avoid use of moved value

'std::string detailData' is moved in the innermost loop of a 2-layer loop,
but is written to throughout the whole duration of the 2-layer loop.

After move, std::string is in an unspecified state (implementation-dependent).

Avoid using a moved value, as it incurs undefined behavior.
---
 mlir/lib/AsmParser/Parser.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp
index 2e4c4a36d46b9b..83eec3244009d8 100644
--- a/mlir/lib/AsmParser/Parser.cpp
+++ b/mlir/lib/AsmParser/Parser.cpp
@@ -2412,14 +2412,15 @@ ParseResult OperationParser::parseOptionalBlockArgList(Block *owner) {
 //===----------------------------------------------------------------------===//
 
 ParseResult OperationParser::codeCompleteSSAUse() {
-  std::string detailData;
-  llvm::raw_string_ostream detailOS(detailData);
   for (IsolatedSSANameScope &scope : isolatedNameScopes) {
     for (auto &it : scope.values) {
       if (it.second.empty())
         continue;
       Value frontValue = it.second.front().value;
 
+      std::string detailData;
+      llvm::raw_string_ostream detailOS(detailData);
+
       // If the value isn't a forward reference, we also add the name of the op
       // to the detail.
       if (auto result = dyn_cast<OpResult>(frontValue)) {
@@ -2440,7 +2441,7 @@ ParseResult OperationParser::codeCompleteSSAUse() {
         detailOS << ", ...";
 
       state.codeCompleteContext->appendSSAValueCompletion(
-          it.getKey(), std::move(detailOS.str()));
+          it.getKey(), std::move(detailData));
     }
   }
 



More information about the Mlir-commits mailing list