d-smirnov commented on a change in pull request #10189:
URL: https://github.com/apache/tvm/pull/10189#discussion_r832292587



##########
File path: src/target/source/source_module.cc
##########
@@ -237,14 +241,65 @@ class CSourceCrtMetadataModuleNode : public 
runtime::ModuleNode {
   }
 
   void GenerateInternalWorkspaceBuffers() {
+    Integer constants_byte_alignment_ =
+        target_->GetAttr<Integer>("workspace-byte-alignment").value_or(16);
+    size_t offset = 0;
     if (metadata_->pool_inputs.defined()) {
       for (const auto& kv : metadata_->pool_inputs.value()) {
         tir::usmp::AllocatedPoolInfo allocated_pool_info = kv.second;
         if (allocated_pool_info->pool_info->is_internal) {
-          code_ << "__attribute__((section(\".data.tvm\"), ";
-          code_ << "aligned(" << 16 << ")))\n";
-          code_ << "static uint8_t " << 
allocated_pool_info->pool_info->pool_name << "["
-                << allocated_pool_info->allocated_size->value << "];\n";
+          do {
+            for (const auto& kv : 
allocated_pool_info->pool_info->target_access) {
+              if (kv.first->str() == target_->str()) {
+                if (kTargetPoolReadOnlyAccess == kv.second) {
+                  // Pool is RO, form an initialized struct
+                  code_ << "__attribute__((section(\".rodata.tvm\"), ";
+                  code_ << "))\n";
+                  code_ << "static struct " << 
allocated_pool_info->pool_info->pool_name << " {\n";
+                  // emit struct field names
+                  auto const_info_arr = allocated_pool_info->constant_info_arr;
+                  std::vector<tir::usmp::ConstantInfo> 
const_info_vec(const_info_arr.begin(),
+                                                                      
const_info_arr.end());
+                  std::sort(const_info_vec.begin(), const_info_vec.end(),
+                            [](const tir::usmp::ConstantInfo& a, const 
tir::usmp::ConstantInfo& b) {
+                              return a->byte_offset->value < 
b->byte_offset->value;
+                            });
+                  for (const auto& const_info : const_info_vec) {
+                    const auto& data = const_info->data;
+                    const auto& offs = const_info->byte_offset;
+                    int64_t num_elements = 
std::accumulate(data.Shape().begin(), data.Shape().end(),
+                                                           1, 
std::multiplies<int64_t>());
+                    code_ << "  ";
+                    codegen_c_base_.PrintType(data.DataType(), code_);
+                    code_ << " " << const_info->name_hint << "[" << 
num_elements
+                          << "] __attribute__((packed, aligned(" << 
const_info->byte_alignment
+                          << ")));";
+                    code_ << " // " << num_elements * data.DataType().bytes()
+                          << " bytes, aligned offset: " << offs << "\n";
+                  }
+                  code_ << "} " << allocated_pool_info->pool_info->pool_name 
<< " = {\n";
+
+                  // emit struct field initialization data
+                  for (const auto& const_info : const_info_vec) {
+                    code_ << "  ." << const_info->name_hint << " = {\n";
+                    codegen::NDArrayDataToC(const_info->data, 4, code_);
+                    code_ << "  },\n";
+                  }
+                  code_ << "};";
+                  code_ << "// of total size " << 
allocated_pool_info->allocated_size->value
+                        << " bytes, aligned: " << offset << " bytes\n";
+
+                  goto endOfLoop;
+                }
+              }
+            }
+            code_ << "__attribute__((section(\".data.tvm\"), ";
+            code_ << "aligned(" << 16 << ")))\n";
+            code_ << "static uint8_t " << 
allocated_pool_info->pool_info->pool_name << "[";
+            code_ << allocated_pool_info->allocated_size->value << "];\n";
+
+          endOfLoop : {}
+          } while (false);

Review comment:
       removed

##########
File path: src/target/source/source_module.cc
##########
@@ -237,14 +241,65 @@ class CSourceCrtMetadataModuleNode : public 
runtime::ModuleNode {
   }
 
   void GenerateInternalWorkspaceBuffers() {
+    Integer constants_byte_alignment_ =
+        target_->GetAttr<Integer>("workspace-byte-alignment").value_or(16);
+    size_t offset = 0;
     if (metadata_->pool_inputs.defined()) {
       for (const auto& kv : metadata_->pool_inputs.value()) {
         tir::usmp::AllocatedPoolInfo allocated_pool_info = kv.second;
         if (allocated_pool_info->pool_info->is_internal) {
-          code_ << "__attribute__((section(\".data.tvm\"), ";
-          code_ << "aligned(" << 16 << ")))\n";
-          code_ << "static uint8_t " << 
allocated_pool_info->pool_info->pool_name << "["
-                << allocated_pool_info->allocated_size->value << "];\n";
+          do {
+            for (const auto& kv : 
allocated_pool_info->pool_info->target_access) {
+              if (kv.first->str() == target_->str()) {
+                if (kTargetPoolReadOnlyAccess == kv.second) {
+                  // Pool is RO, form an initialized struct
+                  code_ << "__attribute__((section(\".rodata.tvm\"), ";
+                  code_ << "))\n";
+                  code_ << "static struct " << 
allocated_pool_info->pool_info->pool_name << " {\n";
+                  // emit struct field names
+                  auto const_info_arr = allocated_pool_info->constant_info_arr;
+                  std::vector<tir::usmp::ConstantInfo> 
const_info_vec(const_info_arr.begin(),
+                                                                      
const_info_arr.end());
+                  std::sort(const_info_vec.begin(), const_info_vec.end(),
+                            [](const tir::usmp::ConstantInfo& a, const 
tir::usmp::ConstantInfo& b) {
+                              return a->byte_offset->value < 
b->byte_offset->value;
+                            });
+                  for (const auto& const_info : const_info_vec) {
+                    const auto& data = const_info->data;
+                    const auto& offs = const_info->byte_offset;
+                    int64_t num_elements = 
std::accumulate(data.Shape().begin(), data.Shape().end(),
+                                                           1, 
std::multiplies<int64_t>());
+                    code_ << "  ";
+                    codegen_c_base_.PrintType(data.DataType(), code_);
+                    code_ << " " << const_info->name_hint << "[" << 
num_elements
+                          << "] __attribute__((packed, aligned(" << 
const_info->byte_alignment
+                          << ")));";
+                    code_ << " // " << num_elements * data.DataType().bytes()
+                          << " bytes, aligned offset: " << offs << "\n";
+                  }
+                  code_ << "} " << allocated_pool_info->pool_info->pool_name 
<< " = {\n";
+
+                  // emit struct field initialization data
+                  for (const auto& const_info : const_info_vec) {
+                    code_ << "  ." << const_info->name_hint << " = {\n";
+                    codegen::NDArrayDataToC(const_info->data, 4, code_);
+                    code_ << "  },\n";
+                  }
+                  code_ << "};";
+                  code_ << "// of total size " << 
allocated_pool_info->allocated_size->value
+                        << " bytes, aligned: " << offset << " bytes\n";
+
+                  goto endOfLoop;

Review comment:
       removed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to