You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by ma...@apache.org on 2021/11/29 10:47:49 UTC

[tvm] branch main updated: Section names for TVM generated constants (#9524)

This is an automated email from the ASF dual-hosted git repository.

manupa pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new f32042f  Section names for TVM generated constants (#9524)
f32042f is described below

commit f32042f98b1948d7be468969d705acc44b9b1cbe
Author: Grant Watson <gr...@arm.com>
AuthorDate: Mon Nov 29 10:47:28 2021 +0000

    Section names for TVM generated constants (#9524)
    
    This PR places the TVM generated static const arrays in a memory section named .rodata.tvm.
    The arrays default to 16-byte aligned but a constants-byte-alignment parameter can be added to the target to set alignment to a specific value.
    This allows the linker script to optionally place TVM generated constants in particular memory regions.
---
 src/target/source/codegen_c_host.cc       | 15 ++++++++++-----
 src/target/source/codegen_c_host.h        |  2 +-
 src/target/target_kind.cc                 |  1 +
 tests/python/relay/aot/test_crt_aot.py    | 23 +++++++++++++++++++++++
 tests/python/unittest/test_link_params.py |  2 +-
 5 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/target/source/codegen_c_host.cc b/src/target/source/codegen_c_host.cc
index 0715f07..515cdcc 100644
--- a/src/target/source/codegen_c_host.cc
+++ b/src/target/source/codegen_c_host.cc
@@ -73,7 +73,8 @@ void CodeGenCHost::AddFunction(const PrimFunc& f) {
   }
 }
 
-void CodeGenCHost::DeclareParameters(Map<String, LinkedParam> params) {
+void CodeGenCHost::DeclareParameters(Map<String, LinkedParam> params,
+                                     const Integer& constants_byte_alignment) {
   for (auto kv : params) {
     decl_stream << "\n"
                 << "#ifdef __cplusplus\n"
@@ -85,8 +86,10 @@ void CodeGenCHost::DeclareParameters(Map<String, LinkedParam> params) {
       num_elements *= dim;
     }
     PrintType(kv.second->param.DataType(), decl_stream);
-    decl_stream << " " << ::tvm::runtime::symbol::tvm_param_prefix << kv.first << "["
-                << num_elements << "] = {\n";
+    decl_stream << " __attribute__((section(\".rodata.tvm\"), "
+                << "aligned(" << constants_byte_alignment->value << "))) "
+                << ::tvm::runtime::symbol::tvm_param_prefix << kv.first << "[" << num_elements
+                << "] = {\n";
     NDArrayDataToC(kv.second->param, 4, decl_stream);
     decl_stream << "};\n"
                 << "#ifdef __cplusplus\n"
@@ -421,14 +424,16 @@ runtime::Module BuildCHost(IRModule mod, Target target) {
     cg.AddFunction(f);
   }
 
+  auto constants_byte_alignment = target->GetAttr<Integer>("constants-byte-alignment").value_or(16);
+
   if (could_have_linked_params && !aot_executor_fn.defined()) {
     ICHECK(found_linked_params) << "-link-params given but none found";
-    cg.DeclareParameters(linked_params);
+    cg.DeclareParameters(linked_params, constants_byte_alignment);
     cg.LinkParameters(linked_params);
   }
 
   if (could_have_linked_params && aot_executor_fn.defined()) {
-    cg.DeclareParameters(linked_params);
+    cg.DeclareParameters(linked_params, constants_byte_alignment);
     cg.AddFunction(aot_executor_fn);
   }
 
diff --git a/src/target/source/codegen_c_host.h b/src/target/source/codegen_c_host.h
index d11fa5d..c94612c 100644
--- a/src/target/source/codegen_c_host.h
+++ b/src/target/source/codegen_c_host.h
@@ -45,7 +45,7 @@ class CodeGenCHost : public CodeGenC {
   void DefineModuleName();
 
   /*! \brief Add linked parameters, if they are present. */
-  void DeclareParameters(Map<String, LinkedParam> params);
+  void DeclareParameters(Map<String, LinkedParam> params, const Integer& constants_byte_alignment);
   void LinkParameters(Map<String, LinkedParam> params);
 
   void PrintType(DataType t, std::ostream& os) final;  // NOLINT(*)
diff --git a/src/target/target_kind.cc b/src/target/target_kind.cc
index b44ea25..8246d61 100644
--- a/src/target/target_kind.cc
+++ b/src/target/target_kind.cc
@@ -276,6 +276,7 @@ TVM_REGISTER_TARGET_KIND("c", kDLCPU)
     .add_attr_option<String>("march")
     .add_attr_option<String>("executor")
     .add_attr_option<Integer>("workspace-byte-alignment")
+    .add_attr_option<Integer>("constants-byte-alignment")
     .add_attr_option<Bool>("unpacked-api")
     .add_attr_option<String>("interface-api")
     .set_default_keys({"cpu"});
diff --git a/tests/python/relay/aot/test_crt_aot.py b/tests/python/relay/aot/test_crt_aot.py
index 3c7e836..8a2b1f1 100644
--- a/tests/python/relay/aot/test_crt_aot.py
+++ b/tests/python/relay/aot/test_crt_aot.py
@@ -733,5 +733,28 @@ def test_aot_codegen_backend_alloc_workspace_calls():
     assert source.count("TVMBackendAllocWorkspace") == 3
 
 
+@pytest.mark.parametrize("constants_byte_alignment", [8, 16, 32])
+def test_constants_alignment(constants_byte_alignment):
+    """Test that constants_byte_alignment correctly sets constants byte alignment"""
+
+    use_unpacked_api = True
+    interface_api = "c"
+
+    mod, params = testing.mobilenet.get_workload(batch_size=1)
+    data_shape = [int(x) for x in mod["main"].checked_type.arg_types[0].shape]
+    data = np.random.uniform(size=data_shape).astype("float32")
+    inputs = {"data": data}
+    output_list = generate_ref_data(mod, inputs, params)
+    target_opts = {"-constants-byte-alignment": constants_byte_alignment}
+    compiled_test_mods = compile_models(
+        AOTTestModel(module=mod, inputs=inputs, outputs=output_list, params=params),
+        interface_api,
+        use_unpacked_api,
+        target_opts=target_opts,
+    )
+    source = compiled_test_mods[0].executor_factory.lib.imported_modules[0].get_source()
+    assert f'__attribute__((section(".rodata.tvm"), aligned({constants_byte_alignment})))' in source
+
+
 if __name__ == "__main__":
     sys.exit(pytest.main([__file__] + sys.argv[1:]))
diff --git a/tests/python/unittest/test_link_params.py b/tests/python/unittest/test_link_params.py
index 57ec70d..0e56f6c 100644
--- a/tests/python/unittest/test_link_params.py
+++ b/tests/python/unittest/test_link_params.py
@@ -283,7 +283,7 @@ def test_c_link_params():
             c_dtype = _get_c_datatype(dtype)
             src_lines = src.split("\n")
             param = lib.params["p0"].numpy().reshape(np.prod(KERNEL_SHAPE))
-            param_def = f"static const {c_dtype} __tvm_param__p0[{np.prod(param.shape)}] = {{"
+            param_def = f'static const {c_dtype} __attribute__((section(".rodata.tvm"), aligned(16))) __tvm_param__p0[{np.prod(param.shape)}] = {{'
             for i, line in enumerate(src_lines):
                 if line == param_def:
                     i += 1