@@ -1373,6 +1373,132 @@ static bool isNoCommonDefault(const llvm::Triple &Triple) {
1373
1373
}
1374
1374
}
1375
1375
1376
+ static bool shouldEmitRemarks (const ArgList &Args) {
1377
+ // -fsave-optimization-record enables it.
1378
+ if (Args.hasFlag (options::OPT_fsave_optimization_record,
1379
+ options::OPT_fno_save_optimization_record, false ))
1380
+ return true ;
1381
+
1382
+ // -fsave-optimization-record=<format> enables it as well.
1383
+ if (Args.hasFlag (options::OPT_fsave_optimization_record_EQ,
1384
+ options::OPT_fno_save_optimization_record, false ))
1385
+ return true ;
1386
+
1387
+ // -foptimization-record-file alone enables it too.
1388
+ if (Args.hasFlag (options::OPT_foptimization_record_file_EQ,
1389
+ options::OPT_fno_save_optimization_record, false ))
1390
+ return true ;
1391
+
1392
+ // -foptimization-record-passes alone enables it too.
1393
+ if (Args.hasFlag (options::OPT_foptimization_record_passes_EQ,
1394
+ options::OPT_fno_save_optimization_record, false ))
1395
+ return true ;
1396
+ return false ;
1397
+ }
1398
+
1399
+ static bool hasMultipleInvocations (const llvm::Triple &Triple,
1400
+ const ArgList &Args) {
1401
+ // Supported only on Darwin where we invoke the compiler multiple times
1402
+ // followed by an invocation to lipo.
1403
+ if (!Triple.isOSDarwin ())
1404
+ return false ;
1405
+ // If more than one "-arch <arch>" is specified, we're targeting multiple
1406
+ // architectures resulting in a fat binary.
1407
+ return Args.getAllArgValues (options::OPT_arch).size () > 1 ;
1408
+ }
1409
+
1410
+ static bool checkRemarksOptions (const Driver &D, const ArgList &Args,
1411
+ const llvm::Triple &Triple) {
1412
+ // When enabling remarks, we need to error if:
1413
+ // * The remark file is specified but we're targeting multiple architectures,
1414
+ // which means more than one remark file is being generated.
1415
+ bool hasMultipleInvocations = ::hasMultipleInvocations (Triple, Args);
1416
+ bool hasExplicitOutputFile =
1417
+ Args.getLastArg (options::OPT_foptimization_record_file_EQ);
1418
+ if (hasMultipleInvocations && hasExplicitOutputFile) {
1419
+ D.Diag (diag::err_drv_invalid_output_with_multiple_archs)
1420
+ << " -foptimization-record-file" ;
1421
+ return false ;
1422
+ }
1423
+ return true ;
1424
+ }
1425
+
1426
+ static void renderRemarksOptions (const ArgList &Args, ArgStringList &CmdArgs,
1427
+ const llvm::Triple &Triple,
1428
+ const InputInfo &Input, const JobAction &JA) {
1429
+ CmdArgs.push_back (" -opt-record-file" );
1430
+
1431
+ const Arg *A = Args.getLastArg (options::OPT_foptimization_record_file_EQ);
1432
+ if (A) {
1433
+ CmdArgs.push_back (A->getValue ());
1434
+ } else {
1435
+ bool hasMultipleArchs =
1436
+ Triple.isOSDarwin () && // Only supported on Darwin platforms.
1437
+ Args.getAllArgValues (options::OPT_arch).size () > 1 ;
1438
+ SmallString<128 > F;
1439
+
1440
+ if (Args.hasArg (options::OPT_c) || Args.hasArg (options::OPT_S)) {
1441
+ if (Arg *FinalOutput = Args.getLastArg (options::OPT_o))
1442
+ F = FinalOutput->getValue ();
1443
+ }
1444
+
1445
+ if (F.empty ()) {
1446
+ // Use the input filename.
1447
+ F = llvm::sys::path::stem (Input.getBaseInput ());
1448
+
1449
+ // If we're compiling for an offload architecture (i.e. a CUDA device),
1450
+ // we need to make the file name for the device compilation different
1451
+ // from the host compilation.
1452
+ if (!JA.isDeviceOffloading (Action::OFK_None) &&
1453
+ !JA.isDeviceOffloading (Action::OFK_Host)) {
1454
+ llvm::sys::path::replace_extension (F, " " );
1455
+ F += Action::GetOffloadingFileNamePrefix (JA.getOffloadingDeviceKind (),
1456
+ Triple.normalize ());
1457
+ F += " -" ;
1458
+ F += JA.getOffloadingArch ();
1459
+ }
1460
+ }
1461
+
1462
+ // If we're having more than one "-arch", we should name the files
1463
+ // differently so that every cc1 invocation writes to a different file.
1464
+ // We're doing that by appending "-<arch>" with "<arch>" being the arch
1465
+ // name from the triple.
1466
+ if (hasMultipleArchs) {
1467
+ // First, remember the extension.
1468
+ SmallString<64 > OldExtension = llvm::sys::path::extension (F);
1469
+ // then, remove it.
1470
+ llvm::sys::path::replace_extension (F, " " );
1471
+ // attach -<arch> to it.
1472
+ F += " -" ;
1473
+ F += Triple.getArchName ();
1474
+ // put back the extension.
1475
+ llvm::sys::path::replace_extension (F, OldExtension);
1476
+ }
1477
+
1478
+ std::string Extension = " opt." ;
1479
+ if (const Arg *A =
1480
+ Args.getLastArg (options::OPT_fsave_optimization_record_EQ))
1481
+ Extension += A->getValue ();
1482
+ else
1483
+ Extension += " yaml" ;
1484
+
1485
+ llvm::sys::path::replace_extension (F, Extension);
1486
+ CmdArgs.push_back (Args.MakeArgString (F));
1487
+ }
1488
+
1489
+ if (const Arg *A =
1490
+ Args.getLastArg (options::OPT_foptimization_record_passes_EQ)) {
1491
+ CmdArgs.push_back (" -opt-record-passes" );
1492
+ CmdArgs.push_back (A->getValue ());
1493
+ }
1494
+
1495
+ if (const Arg *A =
1496
+ Args.getLastArg (options::OPT_fsave_optimization_record_EQ)) {
1497
+ CmdArgs.push_back (" -opt-record-format" );
1498
+ CmdArgs.push_back (A->getValue ());
1499
+ }
1500
+ }
1501
+
1376
1502
namespace {
1377
1503
void RenderARMABI (const llvm::Triple &Triple, const ArgList &Args,
1378
1504
ArgStringList &CmdArgs) {
@@ -5174,85 +5300,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
5174
5300
CmdArgs.push_back (" -fapple-pragma-pack" );
5175
5301
5176
5302
// Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
5177
- if (Args.hasFlag (options::OPT_fsave_optimization_record,
5178
- options::OPT_foptimization_record_file_EQ,
5179
- options::OPT_fno_save_optimization_record, false ) ||
5180
- Args.hasFlag (options::OPT_fsave_optimization_record_EQ,
5181
- options::OPT_fno_save_optimization_record, false ) ||
5182
- Args.hasFlag (options::OPT_foptimization_record_passes_EQ,
5183
- options::OPT_fno_save_optimization_record, false )) {
5184
- CmdArgs.push_back (" -opt-record-file" );
5185
-
5186
- const Arg *A = Args.getLastArg (options::OPT_foptimization_record_file_EQ);
5187
- if (A) {
5188
- CmdArgs.push_back (A->getValue ());
5189
- } else {
5190
- bool hasMultipleArchs =
5191
- Triple.isOSDarwin () && // Only supported on Darwin platforms.
5192
- Args.getAllArgValues (options::OPT_arch).size () > 1 ;
5193
- SmallString<128 > F;
5194
-
5195
- if (Args.hasArg (options::OPT_c) || Args.hasArg (options::OPT_S)) {
5196
- if (Arg *FinalOutput = Args.getLastArg (options::OPT_o))
5197
- F = FinalOutput->getValue ();
5198
- }
5199
-
5200
- if (F.empty ()) {
5201
- // Use the input filename.
5202
- F = llvm::sys::path::stem (Input.getBaseInput ());
5203
-
5204
- // If we're compiling for an offload architecture (i.e. a CUDA device),
5205
- // we need to make the file name for the device compilation different
5206
- // from the host compilation.
5207
- if (!JA.isDeviceOffloading (Action::OFK_None) &&
5208
- !JA.isDeviceOffloading (Action::OFK_Host)) {
5209
- llvm::sys::path::replace_extension (F, " " );
5210
- F += Action::GetOffloadingFileNamePrefix (JA.getOffloadingDeviceKind (),
5211
- Triple.normalize ());
5212
- F += " -" ;
5213
- F += JA.getOffloadingArch ();
5214
- }
5215
- }
5216
-
5217
- // If we're having more than one "-arch", we should name the files
5218
- // differently so that every cc1 invocation writes to a different file.
5219
- // We're doing that by appending "-<arch>" with "<arch>" being the arch
5220
- // name from the triple.
5221
- if (hasMultipleArchs) {
5222
- // First, remember the extension.
5223
- SmallString<64 > OldExtension = llvm::sys::path::extension (F);
5224
- // then, remove it.
5225
- llvm::sys::path::replace_extension (F, " " );
5226
- // attach -<arch> to it.
5227
- F += " -" ;
5228
- F += Triple.getArchName ();
5229
- // put back the extension.
5230
- llvm::sys::path::replace_extension (F, OldExtension);
5231
- }
5232
-
5233
- std::string Extension = " opt." ;
5234
- if (const Arg *A =
5235
- Args.getLastArg (options::OPT_fsave_optimization_record_EQ))
5236
- Extension += A->getValue ();
5237
- else
5238
- Extension += " yaml" ;
5239
-
5240
- llvm::sys::path::replace_extension (F, Extension);
5241
- CmdArgs.push_back (Args.MakeArgString (F));
5242
- }
5243
-
5244
- if (const Arg *A =
5245
- Args.getLastArg (options::OPT_foptimization_record_passes_EQ)) {
5246
- CmdArgs.push_back (" -opt-record-passes" );
5247
- CmdArgs.push_back (A->getValue ());
5248
- }
5249
-
5250
- if (const Arg *A =
5251
- Args.getLastArg (options::OPT_fsave_optimization_record_EQ)) {
5252
- CmdArgs.push_back (" -opt-record-format" );
5253
- CmdArgs.push_back (A->getValue ());
5254
- }
5255
- }
5303
+ if (shouldEmitRemarks (Args) && checkRemarksOptions (D, Args, Triple))
5304
+ renderRemarksOptions (Args, CmdArgs, Triple, Input, JA);
5256
5305
5257
5306
bool RewriteImports = Args.hasFlag (options::OPT_frewrite_imports,
5258
5307
options::OPT_fno_rewrite_imports, false );
0 commit comments