test_tir_transform_combine_context_call.py 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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.
17
import tvm
18
from tvm import te
19 20

def test_for():
21
    dev_type = te.var("dev_type")
22
    def device_context(dev_id):
23
        ctx = tvm.tir.call_extern("handle", "device_context", dev_type, dev_id)
24 25
        return tvm.tir.Call(
            "handle", "tvm_thread_context", [ctx], tvm.tir.Call.Intrinsic, None, 0)
26

27 28
    ib = tvm.tir.ir_builder.create()
    n = te.var("n")
29 30
    A = ib.allocate("float32", n, name="A", scope="global")
    with ib.for_range(0, n, name="i") as i:
31
        ib.emit(tvm.tir.call_extern
32 33
                ("int32", "fadd", device_context(0), A))
        with ib.for_range(0, 10, name="j") as j:
34
            ib.emit(tvm.tir.call_extern
35
                    ("int32", "fadd", device_context(1), A))
36
            ib.emit(tvm.tir.call_extern
37 38
                    ("int32", "fadd", device_context(0), A))
    body = ib.get()
39
    f = tvm.tir.ir_pass.MakeAPI(body, "func", [dev_type, n], 2, True)
40 41

    # temp adapter to convert loweredFunc to IRModule
42
    # to test passes in the new style.x
43 44 45 46 47 48
    mod = tvm.testing.LoweredFuncsToIRModule([f])

    mod = tvm.tir.transform.CombineContextCall()(mod)

    assert mod["func"].body.value.dtype == "handle"
    assert mod["func"].body.body.value.dtype == "handle"
49 50 51 52


if __name__ == "__main__":
    test_for()