diff --git a/mlir/include/mlir/Dialect/GPU/GPUOps.td b/mlir/include/mlir/Dialect/GPU/GPUOps.td --- a/mlir/include/mlir/Dialect/GPU/GPUOps.td +++ b/mlir/include/mlir/Dialect/GPU/GPUOps.td @@ -454,6 +454,10 @@ /// the operand will be dropped. The block argument must not have any uses. void eraseKernelArgument(unsigned index); + /// Add the given value as a kernel argument. Returns the corresponding newly + /// added BlockArgument. + BlockArgument addKernelArgument(Value argument); + static StringRef getBlocksKeyword() { return "blocks"; } static StringRef getThreadsKeyword() { return "threads"; } static StringRef getArgsKeyword() { return "args"; } diff --git a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp --- a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp +++ b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp @@ -201,6 +201,8 @@ result.addOperands( {gridSizeX, gridSizeY, gridSizeZ, blockSizeX, blockSizeY, blockSizeZ}); result.addOperands(operands); + // We want to be able to add operands later, for instance due to code motion. + result.setOperandListToResizable(); // Create a kernel body region with kNumConfigRegionAttributes + N arguments, // where the first kNumConfigRegionAttributes arguments have `index` type and @@ -449,6 +451,15 @@ getOperation()->eraseOperand(kNumConfigOperands + index); } +BlockArgument LaunchOp::addKernelArgument(Value value) { + Block &entryBlock = body().front(); + Operation *op = getOperation(); + llvm::SmallVector operands(op->getOperands()); + operands.push_back(value); + op->setOperands(operands); + return entryBlock.addArgument(value.getType()); +} + namespace { // Clone any known constants passed as operands to the kernel into its body. class PropagateConstantBounds : public OpRewritePattern {