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 2022/06/21 21:21:10 UTC

[tvm] branch main updated: [LLVM] Retrieve entire target string from LLVMModule (#11802)

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

masahi 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 7b0f791e12 [LLVM] Retrieve entire target string from LLVMModule (#11802)
7b0f791e12 is described below

commit 7b0f791e120eedcebfb403d8ac44e289a7065651
Author: Krzysztof Parzyszek <kp...@quicinc.com>
AuthorDate: Tue Jun 21 16:21:05 2022 -0500

    [LLVM] Retrieve entire target string from LLVMModule (#11802)
    
    The blob-embedding code creates a new LLVM module for which is needs more
    information than just the target triple. The `_get_target_triple` function
    in LLVMModule returned the triple with additional options appended to the
    string. Instead of piggy-backing those extra options on top of the triple,
    replace `_get_target_triple` with `_get_target_string`, which will return
    the entire target string.
---
 python/tvm/runtime/module.py    | 16 +++++++++-------
 src/target/codegen.cc           |  4 ++--
 src/target/llvm/codegen_blob.cc |  4 ++--
 src/target/llvm/codegen_blob.h  |  2 +-
 src/target/llvm/llvm_module.cc  | 28 +++++++---------------------
 5 files changed, 21 insertions(+), 33 deletions(-)

diff --git a/python/tvm/runtime/module.py b/python/tvm/runtime/module.py
index c614e5d757..d2c7617ed8 100644
--- a/python/tvm/runtime/module.py
+++ b/python/tvm/runtime/module.py
@@ -436,7 +436,7 @@ class Module(object):
         files = addons if addons else []
         is_system_lib = False
         has_c_module = False
-        llvm_target_triple = None
+        llvm_target_string = None
         for index, module in enumerate(modules):
             if fcompile is not None and hasattr(fcompile, "object_format"):
                 if module.type_key == "c":
@@ -475,8 +475,8 @@ class Module(object):
             is_system_lib = (
                 module.type_key == "llvm" and module.get_function("__tvm_is_system_module")()
             )
-            llvm_target_triple = (
-                module.type_key == "llvm" and module.get_function("_get_target_triple")()
+            llvm_target_string = (
+                module.type_key == "llvm" and module.get_function("_get_target_string")()
             )
         if not fcompile:
             if file_name.endswith(".tar"):
@@ -484,16 +484,18 @@ class Module(object):
             else:
                 fcompile = _cc.create_shared
 
-        if llvm_target_triple is None and hasattr(fcompile, "get_target_triple"):
-            llvm_target_triple = fcompile.get_target_triple()
+        if llvm_target_string is None and hasattr(fcompile, "get_target_triple"):
+            triple = fcompile.get_target_triple()
+            assert triple, "Target triple should not be empty"
+            llvm_target_string = "llvm -mtriple " + triple
 
         if getattr(fcompile, "need_system_lib", False) and not is_system_lib:
             raise ValueError("%s need --system-lib option" % str(fcompile))
 
         if self.imported_modules:
-            if enabled("llvm") and llvm_target_triple:
+            if enabled("llvm") and llvm_target_string:
                 path_obj = os.path.join(workspace_dir, f"devc.{object_format}")
-                m = _ffi_api.ModulePackImportsToLLVM(self, is_system_lib, llvm_target_triple)
+                m = _ffi_api.ModulePackImportsToLLVM(self, is_system_lib, llvm_target_string)
                 m.save(path_obj)
                 files.append(path_obj)
             else:
diff --git a/src/target/codegen.cc b/src/target/codegen.cc
index 3c4866be1b..f6b694cb7c 100644
--- a/src/target/codegen.cc
+++ b/src/target/codegen.cc
@@ -291,7 +291,7 @@ std::string PackImportsToC(const runtime::Module& mod, bool system_lib) {
 }
 
 runtime::Module PackImportsToLLVM(const runtime::Module& mod, bool system_lib,
-                                  const std::string& target_triple) {
+                                  const std::string& llvm_target_string) {
   std::string bin = SerializeModule(mod);
 
   uint64_t nbytes = bin.length();
@@ -309,7 +309,7 @@ runtime::Module PackImportsToLLVM(const runtime::Module& mod, bool system_lib,
   // the codegen function.
   const PackedFunc* codegen_f = runtime::Registry::Get(codegen_f_name);
   ICHECK(codegen_f != nullptr) << "codegen.codegen_blob is not presented.";
-  return (*codegen_f)(blob_byte_array, system_lib, target_triple);
+  return (*codegen_f)(blob_byte_array, system_lib, llvm_target_string);
 }
 
 TVM_REGISTER_GLOBAL("target.Build").set_body_typed(Build);
diff --git a/src/target/llvm/codegen_blob.cc b/src/target/llvm/codegen_blob.cc
index f7c466068a..dc9760f21f 100644
--- a/src/target/llvm/codegen_blob.cc
+++ b/src/target/llvm/codegen_blob.cc
@@ -32,9 +32,9 @@ namespace tvm {
 namespace codegen {
 
 std::pair<std::unique_ptr<llvm::Module>, std::shared_ptr<llvm::LLVMContext>> CodeGenBlob(
-    const std::string& data, bool system_lib, const std::string& target_triple) {
+    const std::string& data, bool system_lib, const std::string& llvm_target_string) {
   InitializeLLVM();
-  Target target = Target("llvm -mtriple " + target_triple);
+  Target target(llvm_target_string);
   auto tm = GetLLVMTargetMachine(target);
   auto triple = tm->getTargetTriple();
   auto ctx = std::make_shared<llvm::LLVMContext>();
diff --git a/src/target/llvm/codegen_blob.h b/src/target/llvm/codegen_blob.h
index 2821f44ebd..bc238543e6 100644
--- a/src/target/llvm/codegen_blob.h
+++ b/src/target/llvm/codegen_blob.h
@@ -42,7 +42,7 @@ namespace codegen {
  * \return LLVM module and LLVM context
  */
 std::pair<std::unique_ptr<llvm::Module>, std::shared_ptr<llvm::LLVMContext>> CodeGenBlob(
-    const std::string& data, bool system_lib, const std::string& target_triple);
+    const std::string& data, bool system_lib, const std::string& llvm_target_string);
 
 }  // namespace codegen
 }  // namespace tvm
diff --git a/src/target/llvm/llvm_module.cc b/src/target/llvm/llvm_module.cc
index c7aea3dc19..30a1b39872 100644
--- a/src/target/llvm/llvm_module.cc
+++ b/src/target/llvm/llvm_module.cc
@@ -69,24 +69,9 @@ class LLVMModuleNode final : public runtime::ModuleNode {
       return PackedFunc(nullptr);
     } else if (name == "get_const_vars") {
       return PackedFunc(nullptr);
-    } else if (name == "_get_target_triple") {
-      std::ostringstream target_triple_ss;
-      target_triple_ss << tm_->getTargetTriple().str();
-      // getTargetTriple() doesn't include other flags besides the triple. Add back flags which are
-      // important for ModulePackImportsToLLVM.
-      if (tm_->Options.FloatABIType == llvm::FloatABI::ABIType::Soft) {
-        target_triple_ss << " -mfloat-abi=soft";
-      }
-      std::string mabi = tm_->Options.MCOptions.ABIName;
-      if (!mabi.empty()) {
-        target_triple_ss << " -mabi=" << mabi;
-      }
-      llvm::StringRef mcpu = tm_->getTargetCPU();
-      if (!mcpu.empty() && mcpu != "generic") {
-        target_triple_ss << " -mcpu=" << mcpu.str();
-      }
-      std::string target_triple = target_triple_ss.str();
-      return PackedFunc([target_triple](TVMArgs args, TVMRetValue* rv) { *rv = target_triple; });
+    } else if (name == "_get_target_string") {
+      std::string target_string = LLVMTargetToString(target_);
+      return PackedFunc([target_string](TVMArgs args, TVMRetValue* rv) { *rv = target_string; });
     }
     if (ee_ == nullptr) LazyInitJIT();
 
@@ -342,7 +327,8 @@ class LLVMModuleNode final : public runtime::ModuleNode {
       target_metadata = os.str();
     }
     mptr_ = module_.get();
-    tm_ = GetLLVMTargetMachine(Target(target_metadata));
+    target_ = Target(target_metadata);
+    tm_ = GetLLVMTargetMachine(target_);
   }
 
   void LoadIR(const std::string& file_name) {
@@ -509,9 +495,9 @@ TVM_REGISTER_GLOBAL("codegen.llvm_target_enabled")
 
 TVM_REGISTER_GLOBAL("codegen.codegen_blob")
     .set_body_typed([](std::string data, bool system_lib,
-                       std::string target_triple) -> runtime::Module {
+                       std::string llvm_target_string) -> runtime::Module {
       auto n = make_object<LLVMModuleNode>();
-      auto p = CodeGenBlob(data, system_lib, target_triple);
+      auto p = CodeGenBlob(data, system_lib, llvm_target_string);
       n->Init(std::move(p.first), p.second);
       return runtime::Module(n);
     });