comaniac commented on a change in pull request #6347:
URL: https://github.com/apache/incubator-tvm/pull/6347#discussion_r479451668



##########
File path: src/target/llvm/llvm_common.cc
##########
@@ -140,16 +132,43 @@ std::unique_ptr<llvm::TargetMachine> 
GetLLVMTargetMachine(const std::string& tar
   }
 
   std::string err;
-  const llvm::Target* target = 
llvm::TargetRegistry::lookupTarget(target_triple, err);
-  if (target == nullptr) {
+  const llvm::Target* llvm_target = 
llvm::TargetRegistry::lookupTarget(target_triple, err);
+  if (llvm_target == nullptr) {
     CHECK(allow_null) << err << " target_triple=" << target_triple;
     return nullptr;
   }
   llvm::TargetMachine* tm =
-      target->createTargetMachine(target_triple, mcpu, mattr, opt, 
llvm::Reloc::PIC_);
+      llvm_target->createTargetMachine(target_triple, mcpu, mattr, opt, 
llvm::Reloc::PIC_);
   return std::unique_ptr<llvm::TargetMachine>(tm);
 }
 
+std::string LLVMTargetToString(const Target& target) {
+  std::ostringstream os;
+  os << "llvm";
+  if (Optional<String> mtriple = target->GetAttr<String>("mtriple")) {
+    os << " -mtriple=" << mtriple.value();
+  }
+  if (Optional<String> mcpu = target->GetAttr<String>("mcpu")) {
+    os << " -mcpu=" << mcpu.value();
+  }
+  if (Optional<Array<String>> mattr = target->GetAttr<Array<String>>("mattr")) 
{
+    bool is_first;
+    os << " -mattr=";
+    for (const String& attr : mattr.value()) {
+      if (is_first) {
+        is_first = false;
+      } else {
+        os << ",";
+      }

Review comment:
       ```suggestion
         if (!is_first) {
           os << ',';
         }
         is_first = false;
   ```

##########
File path: src/target/llvm/codegen_amdgpu.cc
##########
@@ -228,23 +233,50 @@ inline int DetectROCMApiVersion() {
   return 305;
 }
 
-runtime::Module BuildAMDGPU(IRModule mod, std::string target) {
+static void UpdateTargetConfig(const String& key, const String& value,

Review comment:
       This is more like "canonicalize" instead of "update". What do you think?

##########
File path: src/target/llvm/codegen_nvptx.cc
##########
@@ -254,14 +254,35 @@ inline int DetectCUDAComputeVersion() {
   }
 }
 
-runtime::Module BuildNVPTX(IRModule mod, std::string target) {
+static void UpdateTargetConfig(const String& key, const String& value,
+                               Map<String, ObjectRef>* target_config, bool 
error_if_inconsistent) {
+  if (target_config->count(key)) {
+    const ObjectRef& obj = (*target_config)[key];
+    CHECK(obj->IsInstance<StringObj>())
+        << "TypeError: In code generation for AMDGPU, expect key \"" << key

Review comment:
       AMDGPU?

##########
File path: src/target/llvm/llvm_module.cc
##########
@@ -252,18 +251,21 @@ class LLVMModuleNode final : public runtime::ModuleNode {
       LOG(FATAL) << "Fail to load module: " << msg;
     }
     std::string target_;

Review comment:
       Better to use other names...

##########
File path: src/target/llvm/codegen_nvptx.cc
##########
@@ -254,14 +254,35 @@ inline int DetectCUDAComputeVersion() {
   }
 }
 
-runtime::Module BuildNVPTX(IRModule mod, std::string target) {
+static void UpdateTargetConfig(const String& key, const String& value,

Review comment:
       If this function can be used, we should move it to be a target utility 
function.

##########
File path: src/target/llvm/llvm_common.cc
##########
@@ -58,53 +59,45 @@ void InitializeLLVM() {
   }
 }
 
-void ParseLLVMTargetOptions(const std::string& target_str, std::string* 
triple, std::string* mcpu,
+void ParseLLVMTargetOptions(const Target& target, std::string* triple, 
std::string* mcpu,
                             std::string* mattr, llvm::TargetOptions* options) {
-  // setup target triple
-  size_t start = 0;
-  if (target_str.length() >= 4 && target_str.substr(0, 4) == "llvm") {
-    start = 4;
-  }
   // simple parser
   triple->resize(0);
   mcpu->resize(0);
   mattr->resize(0);
-
   bool soft_float_abi = false;
-  std::string key, value;
-  std::istringstream is(target_str.substr(start, target_str.length() - start));
-  while (is >> key) {
-    if (key == "-system-lib" || key == "-system-lib=0" || key == 
"-system-lib=1") {
-      continue;
-    }
-    size_t pos = key.find('=');
-    if (pos != std::string::npos) {
-      CHECK_GE(key.length(), pos + 1) << "invalid argument " << key;
-      value = key.substr(pos + 1, key.length() - 1);
-      key = key.substr(0, pos);
-    } else {
-      CHECK(is >> value) << "Unspecified value for option " << key;
+  if (const Optional<String>& v = target->GetAttr<String>("mtriple")) {
+    *triple = v.value();
+  }
+  if (const Optional<String>& v = target->GetAttr<String>("mcpu")) {
+    *mcpu = v.value();
+  }
+  if (const Optional<Array<String>>& v = 
target->GetAttr<Array<String>>("mattr")) {
+    std::ostringstream os;
+    bool is_first = true;
+    for (const String& s : v.value()) {
+      if (is_first) {
+        is_first = false;
+      } else {
+        os << ',';
+      }

Review comment:
       ```suggestion
         if (!is_first) {
           os << ',';
         }
         is_first = false;
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to