[clang] [HIP] search fatbin symbols for libs passed by -l (PR #104638)

Artem Belevich via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 14:09:41 PDT 2024


================
@@ -76,8 +79,75 @@ class HIPUndefinedFatBinSymbols {
     return GPUBinHandleSymbols;
   }
 
+  // Collect symbols from static libraries specified by -l options.
+  void processStaticLibraries() {
+    llvm::SmallVector<llvm::StringRef, 16> LibNames;
+    llvm::SmallVector<llvm::StringRef, 16> LibPaths;
+    llvm::SmallVector<llvm::StringRef, 16> ExactLibNames;
+    llvm::Triple Triple(C.getDriver().getTargetTriple());
+    bool IsMSVC = Triple.isWindowsMSVCEnvironment();
+    llvm::StringRef Ext = IsMSVC ? ".lib" : ".a";
+
+    for (const auto *Arg : Args.filtered(options::OPT_l)) {
+      llvm::StringRef Value = Arg->getValue();
+      if (Value.starts_with(":"))
+        ExactLibNames.push_back(Value.drop_front());
+      else
+        LibNames.push_back(Value);
+    }
+    for (const auto *Arg : Args.filtered(options::OPT_L)) {
+      auto Path = Arg->getValue();
+      LibPaths.push_back(Path);
+      if (Verbose)
+        llvm::errs() << "HIP fatbin symbol search uses library path:  " << Path
+                     << "\n";
+    }
+
+    auto ProcessLib = [&](llvm::StringRef LibName, bool IsExact) {
+      llvm::SmallString<256> FullLibName;
+      if (IsExact)
+        FullLibName = LibName;
+      else {
+        if (IsMSVC)
+          (llvm::Twine(LibName) + Ext).toVector(FullLibName);
+        else
+          (llvm::Twine("lib") + LibName + Ext).toVector(FullLibName);
+      }
----------------
Artem-B wrote:

I think we could do it a bit more concisely.

```
    Twine LibNameTwine = IsExact  ? LibName
                         : IsMSVC ? LibName + Ext
                                  : "lib" + LibName + Ext;
    llvm::SmallString<256> FullLibName(LibNameTwine.str());

```

We could condense it even further, but that would be less readable, IMO:
```
   llvm::SmallString<256> FullLibName(
        {!(IsExact || IsMSVC) ? "" : "lib", LibName, IsExact ? "" : Ext});

```

https://github.com/llvm/llvm-project/pull/104638


More information about the cfe-commits mailing list