How to create a memref.alloca using Opbuilder?

I want to build a op like:

%1 = memref.alloca() : memref<512xi32>

I have used

builder.create<memref::AllocaOp>(loc,builder.getI32Type().dyn_cast<MemRefType>(),
                                                                  builder.getI32IntegerAttr(512));

but it show:(I have register memref in standalone-opt)

LLVM ERROR: Building op `memref.alloca` but it isn't registered in this MLIRContext: the dialect may not be loaded or this operation isn't registered by the dialect.

How can I converse IntegerType to MemRefType safely?

I have did this:

    memref::AllocaOp stack=builder.create<memref::AllocaOp>(loc,builder.getI32Type().dyn_cast<MemRefType>(),
                                                                  builder.getI32IntegerAttr(512));

but it show

standalone-opt: /home/saltyfish/src/llvm-project/mlir/lib/IR/TypeRange.cpp:20: mlir::TypeRange::TypeRange(ArrayRef<mlir::Type>): Assertion `llvm::all_of(types, [](Type t) { return t; }) && "attempting to construct a TypeRange with null types"' failed.

The result of dyn_cast should alway be checked before being used: it can return null when the type mismatch.