How to get value of an operand in mlir

Input IR will be:

module {
func @maximum(%arg0: tensor<4xf32>) → tensor<4xf32> {
  %0 = mhlo.constant dense<0.000000e+00> : tensor
  %1 = chlo.broadcast_maximum %arg0, %0 {broadcast_dimensions = dense<> : tensor<0xi64>} : (tensor<4xf32>, tensor) → tensor<4xf32>
  return %1 : tensor<4xf32>

In this i will get the 2nd operand of chlo.broadcast_maximum by using op.getoperand (). Actually i want verify that the second operand of maximum is 0 or not. So may I know how to get the value of operand in mlir.


You IR seems incomplete, the type tensor alone does not exist, there is necessarily a shape and type in between < and >.

When you get an Operation *, you can get the operand through getOperand(0), getOperand(1), …

If you’re interested in the special case where the operand is a constant value, here is an example of a pattern that does that: llvm-project/TensorOps.cpp at main · llvm/llvm-project · GitHub