@@ -52,13 +52,16 @@ static std::string normalizeForBundler(const llvm::Triple &T,
52
52
// input object or archive files.
53
53
class HIPUndefinedFatBinSymbols {
54
54
public:
55
- HIPUndefinedFatBinSymbols (const Compilation &C)
56
- : C(C), DiagID(C.getDriver().getDiags().getCustomDiagID(
57
- DiagnosticsEngine::Error,
58
- " Error collecting HIP undefined fatbin symbols: %0" )),
55
+ HIPUndefinedFatBinSymbols (const Compilation &C,
56
+ const llvm::opt::ArgList &Args_)
57
+ : C(C), Args(Args_),
58
+ DiagID (C.getDriver().getDiags().getCustomDiagID(
59
+ DiagnosticsEngine::Error,
60
+ " Error collecting HIP undefined fatbin symbols: %0" )),
59
61
Quiet(C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)),
60
62
Verbose(C.getArgs().hasArg(options::OPT_v)) {
61
63
populateSymbols ();
64
+ processStaticLibraries ();
62
65
if (Verbose) {
63
66
for (const auto &Name : FatBinSymbols)
64
67
llvm::errs () << " Found undefined HIP fatbin symbol: " << Name << " \n " ;
@@ -76,8 +79,75 @@ class HIPUndefinedFatBinSymbols {
76
79
return GPUBinHandleSymbols;
77
80
}
78
81
82
+ // Collect symbols from static libraries specified by -l options.
83
+ void processStaticLibraries () {
84
+ llvm::SmallVector<llvm::StringRef, 16 > LibNames;
85
+ llvm::SmallVector<llvm::StringRef, 16 > LibPaths;
86
+ llvm::SmallVector<llvm::StringRef, 16 > ExactLibNames;
87
+ llvm::Triple Triple (C.getDriver ().getTargetTriple ());
88
+ bool IsMSVC = Triple.isWindowsMSVCEnvironment ();
89
+ llvm::StringRef Ext = IsMSVC ? " .lib" : " .a" ;
90
+
91
+ for (const auto *Arg : Args.filtered (options::OPT_l)) {
92
+ llvm::StringRef Value = Arg->getValue ();
93
+ if (Value.starts_with (" :" ))
94
+ ExactLibNames.push_back (Value.drop_front ());
95
+ else
96
+ LibNames.push_back (Value);
97
+ }
98
+ for (const auto *Arg : Args.filtered (options::OPT_L)) {
99
+ auto Path = Arg->getValue ();
100
+ LibPaths.push_back (Path);
101
+ if (Verbose)
102
+ llvm::errs () << " HIP fatbin symbol search uses library path: " << Path
103
+ << " \n " ;
104
+ }
105
+
106
+ auto ProcessLib = [&](llvm::StringRef LibName, bool IsExact) {
107
+ llvm::SmallString<256 > FullLibName;
108
+ if (IsExact)
109
+ FullLibName = LibName;
110
+ else {
111
+ if (IsMSVC)
112
+ (llvm::Twine (LibName) + Ext).toVector (FullLibName);
113
+ else
114
+ (llvm::Twine (" lib" ) + LibName + Ext).toVector (FullLibName);
115
+ }
116
+
117
+ bool Found = false ;
118
+ for (const auto &Path : LibPaths) {
119
+ llvm::SmallString<256 > FullPath = Path;
120
+ llvm::sys::path::append (FullPath, FullLibName);
121
+
122
+ if (llvm::sys::fs::exists (FullPath)) {
123
+ if (Verbose)
124
+ llvm::errs () << " HIP fatbin symbol search found library: "
125
+ << FullPath << " \n " ;
126
+ auto BufferOrErr = llvm::MemoryBuffer::getFile (FullPath);
127
+ if (!BufferOrErr) {
128
+ errorHandler (llvm::errorCodeToError (BufferOrErr.getError ()));
129
+ continue ;
130
+ }
131
+ processInput (BufferOrErr.get ()->getMemBufferRef ());
132
+ Found = true ;
133
+ break ;
134
+ }
135
+ }
136
+ if (!Found && Verbose)
137
+ llvm::errs () << " HIP fatbin symbol search could not find library: "
138
+ << FullLibName << " \n " ;
139
+ };
140
+
141
+ for (const auto &LibName : ExactLibNames)
142
+ ProcessLib (LibName, true );
143
+
144
+ for (const auto &LibName : LibNames)
145
+ ProcessLib (LibName, false );
146
+ }
147
+
79
148
private:
80
149
const Compilation &C;
150
+ const llvm::opt::ArgList &Args;
81
151
unsigned DiagID;
82
152
bool Quiet;
83
153
bool Verbose;
@@ -301,7 +371,7 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
301
371
auto HostTriple =
302
372
C.getSingleOffloadToolChain <Action::OFK_Host>()->getTriple ();
303
373
304
- HIPUndefinedFatBinSymbols Symbols (C);
374
+ HIPUndefinedFatBinSymbols Symbols (C, Args );
305
375
306
376
std::string PrimaryHipFatbinSymbol;
307
377
std::string PrimaryGpuBinHandleSymbol;
0 commit comments