Lowering Custom Ops in a Custom dialect to LLVM

Hi all,
I have created a small dialect called Poseidon. Inside that dialect I have declare ConstantOp & AddOp. I able to generate a IR from Toy Dialect in the Toy examples.
I have similar opinion about this concern lowering-tensorstoreop-to-llvm-ir.

module {
  func.func @main() {
    %alloc = memref.alloc() : memref<2x4xf64>
    %alloc_0 = memref.alloc() : memref<2x4xf64>
    %alloc_1 = memref.alloc() : memref<2x4xf64>
    %0 = poseidon.constant dense<[[1.200000e+00, 2.100000e+00, 4.700000e+00, 1.900000e+00], [1.100000e+00, 2.900000e+00, 2.700000e+00, 7.500000e+00]]> : tensor<2x4xf64>
    memref.tensor_store %0, %alloc_1 : memref<2x4xf64>
    %1 = poseidon.constant dense<[[4.000000e+00, 5.000000e+00, 5.800000e+00, 1.400000e+00], [1.700000e+00, 2.300000e+00, 3.700000e+00, 1.900000e+00]]> : tensor<2x4xf64>
    memref.tensor_store %1, %alloc_0 : memref<2x4xf64>
    affine.for %arg0 = 0 to 2 {
      affine.for %arg1 = 0 to 1 step 4 {
        %2 = affine.vector_load %alloc_1[%arg0, %arg1] : memref<2x4xf64>, vector<4xf64>
        %3 = affine.vector_load %alloc_0[%arg0, %arg1] : memref<2x4xf64>, vector<4xf64>
        %4 = poseidon.add %2, %3 : vector<4xf64>
        affine.vector_store %4, %alloc[%arg0, %arg1] : memref<2x4xf64>, vector<4xf64>
      }
    }
    toy.print %alloc : memref<2x4xf64>
    memref.dealloc %alloc_1 : memref<2x4xf64>
    memref.dealloc %alloc_0 : memref<2x4xf64>
    memref.dealloc %alloc : memref<2x4xf64>
    return
  }
}

But currently I am facing problem to lower it down to LLVM. So when I write ConstantOpLowering in toy example(mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp).

struct ConstantOp_Lowering : public ConvertOpToLLVMPattern<poseidon::Constantop> {
  using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;

  LogicalResult
  matchAndRewrite(poseidon::Constantop op, OpAdaptor adaptor,
                  ConversionPatternRewriter &rewriter) const override {
        return LLVM::detail::oneToOneRewrite(op, LLVM::ConstantOp::getOperationName(),
                                       adaptor.getOperands(), op->getAttrs(),
                                       *getTypeConverter(), rewriter);            
    }
};

I am getting the error

[1/2] Building CXX object tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o
FAILED: tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o 
/usr/bin/clang++ -DBUILD_EXAMPLES -DGTEST_HAS_RTTI=0 -DMLIR_CUDA_CONVERSIONS_ENABLED=0 -DMLIR_INCLUDE_TESTS -DMLIR_ROCM_CONVERSIONS_ENABLED=0 -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/Projects/llvm-project/build/tools/mlir/examples/toy/Ch7 -I/home/Projects/llvm-project/mlir/examples/toy/Ch7 -I/home/Projects/llvm-project/build/include -I/home/Projects/llvm-project/llvm/include -I/home/Projects/llvm-project/mlir/include -I/home/Projects/llvm-project/build/tools/mlir/include -I/home/Projects/llvm-project/mlir/examples/toy/Ch7/include -I/home/Projects/llvm-project/build/tools/mlir/examples/toy/Ch7/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -Werror=mismatched-tags -g  -fno-exceptions -fno-rtti -gsplit-dwarf -std=c++17 -MD -MT tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o -MF tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o.d -o tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o -c /home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp
In file included from /home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:26:
In file included from /home/Projects/llvm-project/mlir/examples/toy/Ch7/include/toy/Dialect.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/BuiltinTypes.h:12:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/BuiltinAttributeInterfaces.h:12:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/AffineMap.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/AffineExpr.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/Support/LLVM.h:24:
In file included from /home/Projects/llvm-project/llvm/include/llvm/Support/Casting.h:20:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/memory:76:
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:962:34: error: no matching constructor for initialization of '(anonymous namespace)::ConstantOp_Lowering'
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                                 ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:277:14: note: in instantiation of function template specialization 'std::make_unique<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
        std::make_unique<T>(std::forward<Args>(args)...);
             ^
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:1746:25: note: in instantiation of function template specialization 'mlir::RewritePattern::create<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
        RewritePattern::create<T>(std::forward<Args>(args)...);
                        ^
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:1606:6: note: in instantiation of function template specialization 'mlir::RewritePatternSet::addImpl<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
    (addImpl<Ts>(/*debugLabels=*/std::nullopt,
     ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:276:12: note: in instantiation of function template specialization 'mlir::RewritePatternSet::add<(anonymous namespace)::PrintOpLowering, (anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *, void>' requested here
  patterns.add<PrintOpLowering, ConstantOp_Lowering>(&getContext());
           ^
/home/Projects/llvm-project/mlir/include/mlir/Conversion/LLVMCommon/Pattern.h:139:12: note: candidate inherited constructor not viable: no known conversion from 'mlir::MLIRContext *' to 'mlir::LLVMTypeConverter &' for 1st argument
  explicit ConvertOpToLLVMPattern(LLVMTypeConverter &typeConverter,
           ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:207:33: note: constructor from base class 'ConvertOpToLLVMPattern<mlir::poseidon::Constantop>' inherited here
  using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
                                ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'mlir::MLIRContext *' to 'const (anonymous namespace)::ConstantOp_Lowering' for 1st argument
struct ConstantOp_Lowering : public ConvertOpToLLVMPattern<poseidon::Constantop> {
       ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'mlir::MLIRContext *' to '(anonymous namespace)::ConstantOp_Lowering' for 1st argument
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
1 error generated.
ninja: build stopped: subcommand failed.
[1/3] Building CXX object tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o
FAILED: tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o 
/usr/bin/clang++ -DBUILD_EXAMPLES -DGTEST_HAS_RTTI=0 -DMLIR_CUDA_CONVERSIONS_ENABLED=0 -DMLIR_INCLUDE_TESTS -DMLIR_ROCM_CONVERSIONS_ENABLED=0 -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/Projects/llvm-project/build/tools/mlir/examples/toy/Ch7 -I/home/Projects/llvm-project/mlir/examples/toy/Ch7 -I/home/Projects/llvm-project/build/include -I/home/Projects/llvm-project/llvm/include -I/home/Projects/llvm-project/mlir/include -I/home/Projects/llvm-project/build/tools/mlir/include -I/home/Projects/llvm-project/mlir/examples/toy/Ch7/include -I/home/Projects/llvm-project/build/tools/mlir/examples/toy/Ch7/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -Werror=mismatched-tags -g  -fno-exceptions -fno-rtti -gsplit-dwarf -std=c++17 -MD -MT tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o -MF tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o.d -o tools/mlir/examples/toy/Ch7/CMakeFiles/toyc-ch7.dir/mlir/LowerToPos_LLVM.cpp.o -c /home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp
In file included from /home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:26:
In file included from /home/Projects/llvm-project/mlir/examples/toy/Ch7/include/toy/Dialect.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/BuiltinTypes.h:12:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/BuiltinAttributeInterfaces.h:12:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/AffineMap.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/IR/AffineExpr.h:17:
In file included from /home/Projects/llvm-project/mlir/include/mlir/Support/LLVM.h:24:
In file included from /home/Projects/llvm-project/llvm/include/llvm/Support/Casting.h:20:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/memory:76:
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/unique_ptr.h:962:34: error: no matching constructor for initialization of '(anonymous namespace)::ConstantOp_Lowering'
    { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                                 ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:277:14: note: in instantiation of function template specialization 'std::make_unique<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
        std::make_unique<T>(std::forward<Args>(args)...);
             ^
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:1746:25: note: in instantiation of function template specialization 'mlir::RewritePattern::create<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
        RewritePattern::create<T>(std::forward<Args>(args)...);
                        ^
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:1606:6: note: in instantiation of function template specialization 'mlir::RewritePatternSet::addImpl<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested here
    (addImpl<Ts>(/*debugLabels=*/std::nullopt,
     ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:276:12: note: in instantiation of function template specialization 'mlir::RewritePatternSet::add<(anonymous namespace)::PrintOpLowering, (anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *, void>' requested here
  patterns.add<PrintOpLowering, ConstantOp_Lowering>(&getContext());
           ^
/home/Projects/llvm-project/mlir/include/mlir/Conversion/LLVMCommon/Pattern.h:139:12: note: candidate inherited constructor not viable: no known conversion from 'mlir::MLIRContext *' to 'mlir::LLVMTypeConverter &' for 1st argument
  explicit ConvertOpToLLVMPattern(LLVMTypeConverter &typeConverter,
           ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:207:33: note: constructor from base class 'ConvertOpToLLVMPattern<mlir::poseidon::Constantop>' inherited here
  using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
                                ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'mlir::MLIRContext *' to 'const (anonymous namespace)::ConstantOp_Lowering' for 1st argument
struct ConstantOp_Lowering : public ConvertOpToLLVMPattern<poseidon::Constantop> {
       ^
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'mlir::MLIRContext *' to '(anonymous namespace)::ConstantOp_Lowering' for 1st argument
/home/Projects/llvm-project/mlir/examples/toy/Ch7/mlir/LowerToPos_LLVM.cpp:206:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
1 error generated.
/home/Projects/llvm-project/mlir/include/mlir/IR/PatternMatch.h:277:14: note: in instantiation of function template specialization 'std::make_unique<(anonymous namespace)::ConstantOp_Lowering, mlir::MLIRContext *>' requested
ninja: build stopped: subcommand failed.

Is that the correct approach for doing it? Or do you recommend that I should try a different approach?

You have a build error, the rest of the context of what you’re trying to do isn’t very relevant here:

This line (which you’re not showing in your snippets as far as I can tell).
The compiler tells you that there are 3 possible constructors: “(the implicit copy constructor)”, “(the implicit move constructor)”, and “(the implicit default constructor)”.
The latter takes no argument, but you’re passing a pointer an MLIRContext.

Thank you for quick reply, Now it is building correctly.
I have changed

patterns.add<PrintOpLowering, ConstantOp_Lowering>(&getContext());

with

populatePoseidonToLLVMConversionPatterns(typeConverter, patterns);

But it is showing the error.

loc("mlir/examples/toy/Ch7/demo.toy":2:11): error: failed to legalize operation 'poseidon.constant'