Issue 154462
Summary `parseSourceString` runs over input `StringRef`
Labels new issue
Assignees
Reporter SchrodingerZhu
    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 = "" {} 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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to