43
43
#include " comgr-diagnostic-handler.h"
44
44
#include " comgr-env.h"
45
45
#include " comgr-spirv-command.h"
46
+ #include " comgr-unbundle-command.h"
46
47
#include " lld/Common/CommonLinkerContext.h"
47
48
#include " lld/Common/Driver.h"
48
49
#include " clang/CodeGen/CodeGenAction.h"
@@ -1255,9 +1256,10 @@ amd_comgr_status_t AMDGPUCompiler::unbundle() {
1255
1256
}
1256
1257
1257
1258
// Collect bitcode memory buffers from bitcodes, bundles, and archives
1259
+ auto Cache = CommandCache::get (LogS);
1258
1260
for (auto *Input : InSet->DataObjects ) {
1259
1261
1260
- std::string FileExtension;
1262
+ const char * FileExtension;
1261
1263
amd_comgr_data_kind_t UnbundledDataKind;
1262
1264
switch (Input->DataKind ) {
1263
1265
case AMD_COMGR_DATA_KIND_BC_BUNDLE:
@@ -1288,22 +1290,22 @@ amd_comgr_status_t AMDGPUCompiler::unbundle() {
1288
1290
const size_t BufSize = sizeof (char ) * 30 ;
1289
1291
char *Buf = (char *)malloc (BufSize);
1290
1292
snprintf (Buf, BufSize, " comgr-bundle-%d.%s" , std::rand () % 10000 ,
1291
- FileExtension. c_str () );
1293
+ FileExtension);
1292
1294
Input->Name = Buf;
1293
1295
}
1294
1296
1295
1297
// Write input file system so that OffloadBundler API can process
1296
1298
// TODO: Switch write to VFS
1297
- std::string InputFilePath = getFilePath (Input, InputDir). str (). str ( );
1299
+ SmallString< 128 > InputFilePath = getFilePath (Input, InputDir);
1298
1300
if (auto Status = outputToFile (Input, InputFilePath)) {
1299
1301
return Status;
1300
1302
}
1301
1303
1302
1304
// Bundler input name
1303
- BundlerConfig.InputFileNames .push_back (InputFilePath);
1305
+ BundlerConfig.InputFileNames .emplace_back (InputFilePath);
1304
1306
1305
1307
// Generate prefix for output files
1306
- std::string OutputPrefix = std::string ( Input->Name ) ;
1308
+ StringRef OutputPrefix = Input->Name ;
1307
1309
size_t Index = OutputPrefix.find_last_of (" ." );
1308
1310
OutputPrefix = OutputPrefix.substr (0 , Index);
1309
1311
@@ -1314,53 +1316,35 @@ amd_comgr_status_t AMDGPUCompiler::unbundle() {
1314
1316
<< " Unbundled Files Extension: ." << FileExtension << " \n " ;
1315
1317
}
1316
1318
1317
- for (size_t I = 0 ; I < ActionInfo->BundleEntryIDs .size (); I++) {
1318
- auto Entry = ActionInfo->BundleEntryIDs [I];
1319
- BundlerConfig.TargetNames .push_back (Entry);
1320
-
1319
+ for (StringRef Entry : ActionInfo->BundleEntryIDs ) {
1321
1320
// Add an output file for each target
1322
- std::string OutputFileName =
1323
- OutputPrefix + ' -' + Entry + " ." + FileExtension;
1321
+ SmallString<128 > OutputFilePath = OutputDir;
1322
+ sys::path::append (OutputFilePath,
1323
+ OutputPrefix + " -" + Entry + " ." + FileExtension);
1324
1324
1325
- // TODO: Switch this to LLVM path APIs
1326
- std::string OutputFilePath = OutputDir.str ().str () + " /" + OutputFileName;
1327
- BundlerConfig.OutputFileNames .push_back (OutputFilePath);
1325
+ BundlerConfig.TargetNames .emplace_back (Entry);
1326
+ BundlerConfig.OutputFileNames .emplace_back (OutputFilePath);
1328
1327
1329
1328
if (env::shouldEmitVerboseLogs ()) {
1330
- LogS << " \t Bundle Entry ID: " << BundlerConfig.TargetNames [I] << " \n "
1331
- << " \t Output Filename: " << BundlerConfig.OutputFileNames [I]
1332
- << " \n " ;
1329
+ LogS << " \t Bundle Entry ID: " << Entry << " \n "
1330
+ << " \t Output Filename: " << OutputFilePath << " \n " ;
1333
1331
LogS.flush ();
1334
1332
}
1335
1333
}
1336
1334
1337
- OffloadBundler Bundler (BundlerConfig);
1338
-
1339
- switch (Input->DataKind ) {
1340
- case AMD_COMGR_DATA_KIND_BC_BUNDLE: {
1341
- llvm::Error Err = Bundler.UnbundleFiles ();
1342
- llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (),
1343
- " Unbundle Bitcodes Error: " );
1344
- break ;
1345
- }
1346
- case AMD_COMGR_DATA_KIND_AR_BUNDLE: {
1347
- llvm::Error Err = Bundler.UnbundleArchive ();
1348
- llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (),
1349
- " Unbundle Archives Error: " );
1350
- break ;
1351
- }
1352
- case AMD_COMGR_DATA_KIND_OBJ_BUNDLE: {
1353
- llvm::Error Err = Bundler.UnbundleFiles ();
1354
- llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (),
1355
- " Unbundle Objects Error: " );
1356
- break ;
1357
- }
1358
- default :
1359
- llvm_unreachable (" invalid bundle type" );
1335
+ UnbundleCommand Unbundle (Input->DataKind , BundlerConfig);
1336
+ if (Cache) {
1337
+ if (auto Status = Cache->execute (Unbundle, LogS)) {
1338
+ return Status;
1339
+ }
1340
+ } else {
1341
+ if (auto Status = Unbundle.execute (LogS)) {
1342
+ return Status;
1343
+ }
1360
1344
}
1361
1345
1362
1346
// Add new bitcodes to OutSetT
1363
- for (auto OutputFilePath : BundlerConfig.OutputFileNames ) {
1347
+ for (StringRef OutputFilePath : BundlerConfig.OutputFileNames ) {
1364
1348
1365
1349
amd_comgr_data_t ResultT;
1366
1350
@@ -1371,22 +1355,15 @@ amd_comgr_status_t AMDGPUCompiler::unbundle() {
1371
1355
ScopedDataObjectReleaser SDOR (ResultT);
1372
1356
1373
1357
DataObject *Result = DataObject::convert (ResultT);
1374
- if (auto Status = inputFromFile (Result, StringRef ( OutputFilePath) ))
1358
+ if (auto Status = inputFromFile (Result, OutputFilePath))
1375
1359
return Status;
1376
1360
1377
- StringRef OutputFileName =
1378
- llvm::sys::path::filename (StringRef (OutputFilePath));
1361
+ StringRef OutputFileName = sys::path::filename (OutputFilePath);
1379
1362
Result->setName (OutputFileName);
1380
1363
1381
1364
if (auto Status = amd_comgr_data_set_add (OutSetT, ResultT)) {
1382
1365
return Status;
1383
1366
}
1384
-
1385
- // Remove input and output file after reading back into Comgr data
1386
- if (!env::shouldEmitVerboseLogs ()) {
1387
- sys::fs::remove (InputFilePath);
1388
- sys::fs::remove (OutputFilePath);
1389
- }
1390
1367
}
1391
1368
}
1392
1369
0 commit comments