# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
import tvm
from tvm import te

def test_double_buffer():
    dtype = 'int64'
    n = 100
    m = 4
    tx = te.thread_axis("threadIdx.x")
    ib = tvm.tir.ir_builder.create()
    A = ib.pointer("float32", name="A")
    C = ib.pointer("float32", name="C")
    ib.scope_attr(tx, "thread_extent", 1)
    with ib.for_range(0, n) as i:
        B = ib.allocate("float32", m, name="B", scope="shared")
        with ib.new_scope():
            ib.scope_attr(B.asobject(), "double_buffer_scope", 1)
            with ib.for_range(0, m) as j:
                B[j] = A[i * 4 + j]
        with ib.for_range(0, m) as j:
            C[j] = B[j] + 1

    stmt = ib.get()
    stmt = tvm.tir.ir_pass.InjectDoubleBuffer(stmt, 2)
    stmt = tvm.tir.ir_pass.Simplify(stmt)
    assert isinstance(stmt.body.body, tvm.tir.Allocate)
    assert stmt.body.body.extents[0].value == 2
    mod = tvm.IRModule({
        "db" : tvm.tir.PrimFunc([A.asobject(), C.asobject()], stmt)
    })
    f = tvm.tir.transform.ThreadSync("shared")(mod)["db"]
    count = [0]
    def count_sync(op):
        if isinstance(op, tvm.tir.Call) and op.name == "tvm_storage_sync":
            count[0] += 1
    tvm.tir.ir_pass.PostOrderVisit(f.body, count_sync)
    assert count[0] == 4


if __name__ == "__main__":
    test_double_buffer()

[((24, 9, 24, 38), 'tvm.te.thread_axis', 'te.thread_axis', ({(24, 24, 24, 37): '"""threadIdx.x"""'}, {}), "('threadIdx.x')", False, 'from tvm import te\n'), ((25, 9, 25, 36), 'tvm.tir.ir_builder.create', 'tvm.tir.ir_builder.create', ({}, {}), '()', False, 'import tvm\n'), ((39, 11, 39, 54), 'tvm.tir.ir_pass.InjectDoubleBuffer', 'tvm.tir.ir_pass.InjectDoubleBuffer', ({(39, 46, 39, 50): 'stmt', (39, 52, 39, 53): '2'}, {}), '(stmt, 2)', False, 'import tvm\n'), ((40, 11, 40, 41), 'tvm.tir.ir_pass.Simplify', 'tvm.tir.ir_pass.Simplify', ({(40, 36, 40, 40): 'stmt'}, {}), '(stmt)', False, 'import tvm\n'), ((51, 4, 51, 54), 'tvm.tir.ir_pass.PostOrderVisit', 'tvm.tir.ir_pass.PostOrderVisit', ({(51, 35, 51, 41): 'f.body', (51, 43, 51, 53): 'count_sync'}, {}), '(f.body, count_sync)', False, 'import tvm\n'), ((46, 8, 46, 46), 'tvm.tir.transform.ThreadSync', 'tvm.tir.transform.ThreadSync', ({(46, 37, 46, 45): '"""shared"""'}, {}), "('shared')", False, 'import tvm\n')]