How to register a dialect?

I am working on a ‘mytoy’ dialect for learning purposes and want to register it with mlir. When I try to use mlir-opt I get following error:

error: Dialect `mytoy' not found for custom op 'mytoy.constant' ........ 
note: Registered dialects: acc, affine, amdgpu, amx, arith, arm_neon, arm_sve, async, bufferization, builtin, cf, complex, dlti, emitc, func, gpu, index, linalg, llvm, math, memref, ml_program, nvgpu, nvvm, omp, pdl, pdl_interp, quant, rocdl, scf, shape, sparse_tensor, spirv, tensor, test, test_dyn, tosa, transform, vector, x86vector ; for more info on dialect registration see

This document states that :
“Finally, dialects can be registered with the context. The sole purpose of the registration is to make these dialects available for the textual parser used by tools like mlir-opt or mlir-translate

How can I register my dialect with the context as stated in this document, so that the parser can recognize it?

Registering dialects is done in the C++ code. In this case, the toy tutorial is implementing its opt tool in llvm-project/toyc.cpp at main · llvm/llvm-project · GitHub

This is then used as shown here: Chapter 2: Emitting Basic MLIR - MLIR

I am not strictly following the Toy tutorial :grimacing:. I have defined a dialact and just want the mlir-opt and mlir -translate tools to recognize and parse the dialact, as it does for all other dialects (eg: math, tensor etc). To these tools I am providing handwritten .mlir files which have code written in the dialect I have defined. :face_with_head_bandage:

When constructing an MLIRContext, you can pass a previously constructed DialectRegistry. That registry has methods to register dialects to be available for the parser in the given context.