@@ -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 (auto Name : FatBinSymbols)
64
67
llvm::errs () << " Found undefined HIP fatbin symbol: " << Name << " \n " ;
@@ -76,8 +79,70 @@ 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
+ IsExact ? Twine (LibName).str ()
109
+ : IsMSVC ? (Twine (LibName) + Ext).str ()
110
+ : (Twine (" lib" ) + LibName + Ext).str ());
111
+
112
+ bool Found = false ;
113
+ for (const auto Path : LibPaths) {
114
+ llvm::SmallString<256 > FullPath = Path;
115
+ llvm::sys::path::append (FullPath, FullLibName);
116
+
117
+ if (llvm::sys::fs::exists (FullPath)) {
118
+ if (Verbose)
119
+ llvm::errs () << " HIP fatbin symbol search found library: "
120
+ << FullPath << " \n " ;
121
+ auto BufferOrErr = llvm::MemoryBuffer::getFile (FullPath);
122
+ if (!BufferOrErr) {
123
+ errorHandler (llvm::errorCodeToError (BufferOrErr.getError ()));
124
+ continue ;
125
+ }
126
+ processInput (BufferOrErr.get ()->getMemBufferRef ());
127
+ Found = true ;
128
+ break ;
129
+ }
130
+ }
131
+ if (!Found && Verbose)
132
+ llvm::errs () << " HIP fatbin symbol search could not find library: "
133
+ << FullLibName << " \n " ;
134
+ };
135
+
136
+ for (const auto LibName : ExactLibNames)
137
+ ProcessLib (LibName, true );
138
+
139
+ for (const auto LibName : LibNames)
140
+ ProcessLib (LibName, false );
141
+ }
142
+
79
143
private:
80
144
const Compilation &C;
145
+ const llvm::opt::ArgList &Args;
81
146
unsigned DiagID;
82
147
bool Quiet;
83
148
bool Verbose;
@@ -301,7 +366,7 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
301
366
auto HostTriple =
302
367
C.getSingleOffloadToolChain <Action::OFK_Host>()->getTriple ();
303
368
304
- HIPUndefinedFatBinSymbols Symbols (C);
369
+ HIPUndefinedFatBinSymbols Symbols (C, Args );
305
370
306
371
std::string PrimaryHipFatbinSymbol;
307
372
std::string PrimaryGpuBinHandleSymbol;
0 commit comments