Mlir slice

i am going to slice a mlir into several IRs in operation level via a pass
my question is,
to build a new IR, a new context is needed ?
or is there similar work?

It is pretty rare to need a new Context for such cases.
The Context as the “memory storage pool” for Types and Attributes objects that is shared by some pieces of IR. Other than managing the lifetime of these objects it is fine to just keep reusing it.

there would be error if keep using context

i use llvm-project/TestSlicing.cpp at main · llvm/llvm-project · GitHub to test since
it also slice IR
[ if (!isalinalg::LinalgOp(op))
return WalkResult::advance();
] is removed for my test.

this pass will crash at llvm-project/TestSlicing.cpp at d42c76aba0f4465cd4ae4ed0c0cc64d141cdf476 · llvm/llvm-project · GitHub

i checked the bt:
146 /// Return the context this operation is associated with.
147 MLIRContext *getContext() { return location->getContext();
it crashed at L147 in mlir/include/mlir/IR/Operation.h file

is this a bug of MLIR ?

This has nothing to do with the MLIRContext itself – one of the utilities was likely called on a null op or an erased op. Separately, it’s also a bug that the code at this line isn’t checking for parentFuncOp being null.

This has nothing to do with the MLIRContext itself
→ Indeed it is.

ModuleOp module = getOperation();
auto funcOps = module.getOpsfunc::FuncOp();
unsigned opNum = 0;
for (auto funcOp : funcOps) {
// TODO: For now this is just looking for Linalg ops. It can be generalized
// to look for other ops using flags.
*funcOp.walk([&](Operation op) {
if (!isalinalg::LinalgOp(op))
return WalkResult::advance();
std::string append =
std::string(“backward_slice”) + std::to_string(opNum);
(void)createBackwardSliceFunction(op, append);
return WalkResult::advance();

funcOp.walk([&](Operation *op) will walk itself
then func::FuncOp parentFuncOp = op->getParentOfTypefunc::FuncOp();
func’s parent is null

is this expected behavior that funcOp.walk will walk itself?