<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/154462>154462</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`parseSourceString` runs over input `StringRef`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SchrodingerZhu
</td>
</tr>
</table>
<pre>
Consider the following parsing code:
```c++
// MLIR
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <mlir/Dialect/Arith/IR/Arith.h>
#include <mlir/Dialect/ControlFlow/IR/ControlFlow.h>
#include <mlir/Dialect/DLTI/DLTI.h>
#include <mlir/Dialect/Func/IR/FuncOps.h>
#include <mlir/Dialect/LLVMIR/LLVMDialect.h>
#include <mlir/Dialect/MemRef/IR/MemRef.h>
#include <mlir/Dialect/SCF/IR/SCF.h>
#include <mlir/Dialect/UB/IR/UBOps.h>
#include <mlir/IR/AsmState.h>
#include <mlir/IR/BuiltinAttributes.h>
#include <mlir/IR/BuiltinOps.h>
#include <mlir/IR/BuiltinTypes.h>
#include <mlir/IR/Diagnostics.h>
#include <mlir/IR/DialectRegistry.h>
#include <mlir/InitAllDialects.h>
#include <mlir/Interfaces/DataLayoutInterfaces.h>
#include <mlir/Parser/Parser.h>
#include <mlir/Support/LLVM.h>
#include <mlir/Support/LogicalResult.h>
#include <mlir/Target/LLVMIR/Import.h>
using namespace mlir;
const char *data = "module {} sadfkhjasdf";
int main() {
std::string_view sliced(data, 9);
// 1) Build a registry and MLIR context with required dialects.
DialectRegistry registry;
mlir::registerAllDialects(registry);
MLIRContext context(registry);
context.loadAllAvailableDialects();
// 2) Parse the incoming MLIR module from string.
#ifdef DO_COPY
llvm::SourceMgr sourceMgr;
auto buffer = llvm::MemoryBuffer::getMemBufferCopy(sliced);
sourceMgr.AddNewSourceBuffer(std::move(buffer), llvm::SMLoc());
OwningOpRef<ModuleOp> module = parseSourceFile<ModuleOp>(sourceMgr, &context);
#else
OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(sliced, &context);
#endif
}
```
When compiled with `LLVM 20` with
```
clang++ main.cpp -lLLVM -lMLIR
```
The MLIR Asm Parser runs over the slice:
```
loc("-":1:11): error: custom op 'sadfkhjasdf' is unknown (tried 'builtin.sadfkhjasdf' as well)
```
with `DO_COPY`, the problem solves.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVt1u4ygUfhpygxpRHOfnwhdOMpEqNdtR09nV7s0Iw4nNDAYv4GT79iuwnSYzO61XSmQM5zu_nw-HOSdLDZChdI3S7YS1vjI2O_DKGiF1Cfavqp0URrxmG6OdFGCxrwAfjVLmLHWJG2ZdeHIjACU5Ijmak-7HEV2HH8kR3SG6w_vHh-f4lkjNVSsAo2Sj1KlGdHdom8ZYj-huD7Wxr-v2eAQ7rVDy6WPIwbSWw778hXytpEV0t5VMAQ_yuZW-QnT38Dy8jARujPbWqJ0y5wF-tTVSyfbx5aF_jETsWs0He2H91LiRyMfH3_cRFhb99kjoHupnOA5mu7eR0MNmN-AOm91I0Jf1gPmy_ijArnCuPnjm4WPJdSuVlzr33sqi9TBCeQ8Z5Ugv-_LajNG8lazUxnnJxwmH9DxDKZ23r-8DtPS5Uj3kA-Xagz0yDi4YYZ49slfT-rftd9GfmXXwtnhX9u0rDRQcK2pKyZl6Bteq9_n6wmwJN0x_qIOSC4jkbWxQmtXgGsYBR2QS-hI32nnMK2YxorlgnmGUbDGitDaiVYDRYo0WW-yYOH6vvjEnjojSDiu1xzWTGtEloqsoGa1h7LwInTDJnbdSl19PEs7YKclBILoMVhDd4BWiq05TBPUt8j7oCnwSmGHbVx0zLWLzxNxoD_94fJa-whb-bqUFgcVQ8aDpB8ZclHS2cB99cK87AXvFGUSXF_nBvWh50xvuHfhPueFwqgwTuVL5iUnFCgVX2q-D7kOmIeRIpHi1SM1NHQoWA-7rcLSmxl06pz0Tjg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