VariadicList Not Working as Expected

Hey everyone!

I’m running into an issue with VariadicList initialization in Mojo #3987

Here’s a minimal example of what I’m trying:


fn main():
    alias shape_type = __mlir_type[`!kgen.variadic<`, Int, `>`]

    # Case 1: Direct initialization (works)
    var shape1 = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0))

    # Case 2: Expected to prepopulate with 5 elements (doesn’t work)
    alias rank = 5
    var shape2 = __mlir_op.`pop.variadic.create`[_type = shape_type, size = rank](Int(0))

    # Case 3: Trying to append elements (no such op?)
    var shape3 = __mlir_op.`pop.variadic.create`[_type = shape_type]()
    for _ in range(rank):
        shape3 = __mlir_op.`pop.variadic.append`(shape3, Int(0))  # Doesn't seem to exist

    print(VariadicList(shape1).__len__())  # Output: 2
    print(VariadicList(shape2).__len__())  # Output: 0 (Expected: 5)
    print(VariadicList(shape3).__len__())  # Output: 0 (Expected: 5)

Is there a way to repeat elements when using pop.variadic.create for a VariadicList?

For example, instead of manually passing multiple values like this:

var shape = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0), Int(0), Int(0), Int(0))

Is there a way to dynamically repeat an element n times inside create itself, without manually listing them?

actually i got the answer,

I assumed that pop.variadic.create could take a size argument, but I found the correct way to do it using pop.variadic.splat

fn main():
    alias shape_type = __mlir_type[`!kgen.variadic<`, Int, `>`]

    # Case 1: Known arguments (works correctly)
    var shape1 = __mlir_op.`pop.variadic.create`[_type = shape_type](Int(0), Int(0))

    # Case 2: Prepopulate with a fixed number of elements (works)
    alias rank = 5
    var shape2 = __mlir_op.`pop.variadic.splat`[_type = shape_type, numElements = index(rank)](Int(0))

    print(VariadicList(shape1).__len__())  # Expected: 2, Output: 2
    print(VariadicList(shape2).__len__())  # Expected: 5, Output: 5

Findings

  1. pop.variadic.create only works with explicitly passed arguments.
  • It does not support a size parameter for automatic repetition.
  1. pop.variadic.splat correctly initializes a VariadicList with repeated values.
  • The numElements argument controls the repetition.
  1. There is no pop.variadic.append operation for VariadicList.
  • Based on my research on the Mojo compiler symbols, VariadicList is immutable after creation.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.