2010-06-07 23:20:08 +00:00
|
|
|
//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-07 23:20:08 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-15 17:48:49 +00:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2023-11-08 10:53:49 -08:00
|
|
|
#include "BackendConsumer.h"
|
|
|
|
#include "LinkInModulesPass.h"
|
2018-12-11 03:18:39 +00:00
|
|
|
#include "clang/Basic/CodeGenOptions.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2011-07-05 22:02:36 +00:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2013-12-27 08:11:08 +00:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2017-01-05 16:02:32 +00:00
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2016-12-15 02:19:17 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2015-09-26 01:25:08 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-05-08 02:28:32 +00:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2020-12-09 15:06:50 +03:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2022-09-08 23:33:07 -07:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2015-01-15 02:16:55 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 11:18:46 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-11-11 05:35:12 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2016-12-23 20:44:01 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2018-01-09 22:03:47 +00:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2023-11-13 10:04:50 +00:00
|
|
|
#include "llvm/Frontend/Driver/CodeGenOptions.h"
|
2013-01-02 11:45:17 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2022-11-08 16:58:53 +00:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2015-02-13 09:57:03 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 11:45:17 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-12-23 20:44:01 +00:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2020-05-27 23:12:36 -07:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2014-01-13 09:26:48 +00:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2022-11-19 22:09:05 +00:00
|
|
|
#include "llvm/IRPrinter/IRPrintingPasses.h"
|
2016-08-12 18:12:08 +00:00
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
2017-01-05 16:02:32 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2021-10-08 10:48:15 -07:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2022-03-25 11:03:02 -04:00
|
|
|
#include "llvm/Object/OffloadBinary.h"
|
2016-12-23 20:44:01 +00:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
2019-02-02 23:19:32 +00:00
|
|
|
#include "llvm/Passes/PassPlugin.h"
|
2019-08-14 07:11:09 +00:00
|
|
|
#include "llvm/Passes/StandardInstrumentations.h"
|
[Profile] Add binary profile correlation for code coverage. (#69493)
## Motivation
Since we don't need the metadata sections at runtime, we can somehow
offload them from memory at runtime. Initially, I explored [debug info
correlation](https://discourse.llvm.org/t/instrprofiling-lightweight-instrumentation/59113),
which is used for PGO with value profiling disabled. However, it
currently only works with DWARF and it's be hard to add such artificial
debug info for every function in to CodeView which is used on Windows.
So, offloading profile metadata sections at runtime seems to be a
platform independent option.
## Design
The idea is to use new section names for profile name and data sections
and mark them as metadata sections. Under this mode, the new sections
are non-SHF_ALLOC in ELF. So, they are not loaded into memory at runtime
and can be stripped away as a post-linking step. After the process
exits, the generated raw profiles will contains only headers + counters.
llvm-profdata can be used correlate raw profiles with the unstripped
binary to generate indexed profile.
## Data
For chromium base_unittests with code coverage on linux, the binary size
overhead due to instrumentation reduced from 64M to 38.8M (39.4%) and
the raw profile files size reduce from 128M to 68M (46.9%)
```
$ bloaty out/cov/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +14.6Mi [NEW] +14.6Mi __llvm_prf_data
[NEW] +10.6Mi [NEW] +10.6Mi __llvm_prf_names
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
[ = ] 0 +65% +1.23Ki .relro_padding
+62% +1.20Ki [ = ] 0 [Unmapped]
+13% +448 +19% +448 .init_array
+8.8% +192 [ = ] 0 [ELF Section Headers]
+0.0% +136 +0.0% +80 [7 Others]
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.5% +80 +1.2% +64 .plt
[ = ] 0 -99.2% -3.68Ki [LOAD #5 [RW]]
+195% +64.0Mi +194% +64.0Mi TOTAL
$ bloaty out/cov-cor/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
+13% +448 +19% +448 .init_array
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.2% +64 +1.2% +64 .plt
+2.9% +64 [ = ] 0 [ELF Section Headers]
+0.0% +40 +0.0% +40 .data
+1.2% +32 +1.2% +32 .got.plt
+0.0% +24 +0.0% +8 [5 Others]
[ = ] 0 -22.9% -872 [LOAD #5 [RW]]
-74.5% -1.44Ki [ = ] 0 [Unmapped]
[ = ] 0 -76.5% -1.45Ki .relro_padding
+118% +38.8Mi +117% +38.8Mi TOTAL
```
A few things to note:
1. llvm-profdata doesn't support filter raw profiles by binary id yet,
so when a raw profile doesn't belongs to the binary being digested by
llvm-profdata, merging will fail. Once this is implemented,
llvm-profdata should be able to only merge raw profiles with the same
binary id as the binary and discard the rest (with mismatched/missing
binary id). The workflow I have in mind is to have scripts invoke
llvm-profdata to get all binary ids for all raw profiles, and
selectively choose the raw pnrofiles with matching binary id and the
binary to llvm-profdata for merging.
2. Note: In COFF, currently they are still loaded into memory but not
used. I didn't do it in this patch because I noticed that `.lcovmap` and
`.lcovfunc` are loaded into memory. A separate patch will address it.
3. This should works with PGO when value profiling is disabled as debug
info correlation currently doing, though I haven't tested this yet.
2023-12-14 14:16:38 -05:00
|
|
|
#include "llvm/ProfileData/InstrProfCorrelator.h"
|
2018-11-17 18:04:13 +00:00
|
|
|
#include "llvm/Support/BuryPointer.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-08-12 18:12:08 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2019-03-30 08:42:48 +00:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "llvm/Support/Timer.h"
|
2020-05-09 15:10:51 +01:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2023-02-01 09:24:44 -08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2010-06-07 23:20:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2023-06-26 10:26:56 +02:00
|
|
|
#include "llvm/TargetParser/SubtargetFeature.h"
|
2023-02-07 12:21:51 +00:00
|
|
|
#include "llvm/TargetParser/Triple.h"
|
2023-11-09 23:10:49 -08:00
|
|
|
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
|
2023-03-24 00:09:29 +00:00
|
|
|
#include "llvm/Transforms/IPO/EmbedBitcodePass.h"
|
2020-01-24 12:24:18 -08:00
|
|
|
#include "llvm/Transforms/IPO/LowerTypeTests.h"
|
2017-06-01 23:27:51 +00:00
|
|
|
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
|
2018-04-24 00:59:22 +00:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombine.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2019-02-13 22:22:48 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
|
2021-06-11 10:13:10 -07:00
|
|
|
#include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
|
2017-11-14 01:47:24 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
|
2020-11-12 13:19:04 -08:00
|
|
|
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
|
2018-03-23 22:16:59 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
|
2019-05-14 21:17:21 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
|
2019-04-25 17:52:43 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
|
2022-08-18 23:53:09 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/KCFI.h"
|
2020-09-14 09:12:13 -07:00
|
|
|
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
|
2019-01-16 09:28:01 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
|
2023-09-30 16:59:37 -07:00
|
|
|
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
|
2022-09-06 15:49:39 +02:00
|
|
|
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
|
2022-09-08 23:33:07 -07:00
|
|
|
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
|
2019-01-16 09:28:01 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
|
2013-01-28 01:36:00 +00:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2021-02-10 03:38:19 -08:00
|
|
|
#include "llvm/Transforms/Scalar/EarlyCSE.h"
|
2016-03-11 09:02:43 +00:00
|
|
|
#include "llvm/Transforms/Scalar/GVN.h"
|
2022-09-08 23:33:07 -07:00
|
|
|
#include "llvm/Transforms/Scalar/JumpThreading.h"
|
2021-03-11 06:55:13 -08:00
|
|
|
#include "llvm/Transforms/Utils/Debugify.h"
|
2019-06-20 19:35:25 +00:00
|
|
|
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
|
2022-01-31 16:33:56 -08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2014-03-09 11:36:40 +00:00
|
|
|
#include <memory>
|
2022-12-03 11:06:12 -06:00
|
|
|
#include <optional>
|
2010-06-07 23:20:08 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-06-08 17:37:47 +02:00
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
|
|
|
|
#include "llvm/Support/Extension.def"
|
|
|
|
|
2021-12-16 14:19:03 -08:00
|
|
|
namespace llvm {
|
2023-09-13 08:55:22 -07:00
|
|
|
extern cl::opt<bool> PrintPipelinePasses;
|
2022-09-08 17:26:53 -07:00
|
|
|
|
|
|
|
// Experiment to move sanitizers earlier.
|
|
|
|
static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
|
|
|
|
"sanitizer-early-opt-ep", cl::Optional,
|
|
|
|
cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
|
2023-11-08 10:53:49 -08:00
|
|
|
|
[Profile] Add binary profile correlation for code coverage. (#69493)
## Motivation
Since we don't need the metadata sections at runtime, we can somehow
offload them from memory at runtime. Initially, I explored [debug info
correlation](https://discourse.llvm.org/t/instrprofiling-lightweight-instrumentation/59113),
which is used for PGO with value profiling disabled. However, it
currently only works with DWARF and it's be hard to add such artificial
debug info for every function in to CodeView which is used on Windows.
So, offloading profile metadata sections at runtime seems to be a
platform independent option.
## Design
The idea is to use new section names for profile name and data sections
and mark them as metadata sections. Under this mode, the new sections
are non-SHF_ALLOC in ELF. So, they are not loaded into memory at runtime
and can be stripped away as a post-linking step. After the process
exits, the generated raw profiles will contains only headers + counters.
llvm-profdata can be used correlate raw profiles with the unstripped
binary to generate indexed profile.
## Data
For chromium base_unittests with code coverage on linux, the binary size
overhead due to instrumentation reduced from 64M to 38.8M (39.4%) and
the raw profile files size reduce from 128M to 68M (46.9%)
```
$ bloaty out/cov/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +14.6Mi [NEW] +14.6Mi __llvm_prf_data
[NEW] +10.6Mi [NEW] +10.6Mi __llvm_prf_names
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
[ = ] 0 +65% +1.23Ki .relro_padding
+62% +1.20Ki [ = ] 0 [Unmapped]
+13% +448 +19% +448 .init_array
+8.8% +192 [ = ] 0 [ELF Section Headers]
+0.0% +136 +0.0% +80 [7 Others]
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.5% +80 +1.2% +64 .plt
[ = ] 0 -99.2% -3.68Ki [LOAD #5 [RW]]
+195% +64.0Mi +194% +64.0Mi TOTAL
$ bloaty out/cov-cor/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
+13% +448 +19% +448 .init_array
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.2% +64 +1.2% +64 .plt
+2.9% +64 [ = ] 0 [ELF Section Headers]
+0.0% +40 +0.0% +40 .data
+1.2% +32 +1.2% +32 .got.plt
+0.0% +24 +0.0% +8 [5 Others]
[ = ] 0 -22.9% -872 [LOAD #5 [RW]]
-74.5% -1.44Ki [ = ] 0 [Unmapped]
[ = ] 0 -76.5% -1.45Ki .relro_padding
+118% +38.8Mi +117% +38.8Mi TOTAL
```
A few things to note:
1. llvm-profdata doesn't support filter raw profiles by binary id yet,
so when a raw profile doesn't belongs to the binary being digested by
llvm-profdata, merging will fail. Once this is implemented,
llvm-profdata should be able to only merge raw profiles with the same
binary id as the binary and discard the rest (with mismatched/missing
binary id). The workflow I have in mind is to have scripts invoke
llvm-profdata to get all binary ids for all raw profiles, and
selectively choose the raw pnrofiles with matching binary id and the
binary to llvm-profdata for merging.
2. Note: In COFF, currently they are still loaded into memory but not
used. I didn't do it in this patch because I noticed that `.lcovmap` and
`.lcovfunc` are loaded into memory. A separate patch will address it.
3. This should works with PGO when value profiling is disabled as debug
info correlation currently doing, though I haven't tested this yet.
2023-12-14 14:16:38 -05:00
|
|
|
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
|
|
|
|
|
2023-11-08 10:53:49 -08:00
|
|
|
// Re-link builtin bitcodes after optimization
|
2023-11-29 16:44:40 -08:00
|
|
|
cl::opt<bool> ClRelinkBuiltinBitcodePostop(
|
2023-11-08 10:53:49 -08:00
|
|
|
"relink-builtin-bitcode-postop", cl::Optional,
|
|
|
|
cl::desc("Re-link builtin bitcodes after optimization."), cl::init(false));
|
[Profile] Add binary profile correlation for code coverage. (#69493)
## Motivation
Since we don't need the metadata sections at runtime, we can somehow
offload them from memory at runtime. Initially, I explored [debug info
correlation](https://discourse.llvm.org/t/instrprofiling-lightweight-instrumentation/59113),
which is used for PGO with value profiling disabled. However, it
currently only works with DWARF and it's be hard to add such artificial
debug info for every function in to CodeView which is used on Windows.
So, offloading profile metadata sections at runtime seems to be a
platform independent option.
## Design
The idea is to use new section names for profile name and data sections
and mark them as metadata sections. Under this mode, the new sections
are non-SHF_ALLOC in ELF. So, they are not loaded into memory at runtime
and can be stripped away as a post-linking step. After the process
exits, the generated raw profiles will contains only headers + counters.
llvm-profdata can be used correlate raw profiles with the unstripped
binary to generate indexed profile.
## Data
For chromium base_unittests with code coverage on linux, the binary size
overhead due to instrumentation reduced from 64M to 38.8M (39.4%) and
the raw profile files size reduce from 128M to 68M (46.9%)
```
$ bloaty out/cov/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +14.6Mi [NEW] +14.6Mi __llvm_prf_data
[NEW] +10.6Mi [NEW] +10.6Mi __llvm_prf_names
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
[ = ] 0 +65% +1.23Ki .relro_padding
+62% +1.20Ki [ = ] 0 [Unmapped]
+13% +448 +19% +448 .init_array
+8.8% +192 [ = ] 0 [ELF Section Headers]
+0.0% +136 +0.0% +80 [7 Others]
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.5% +80 +1.2% +64 .plt
[ = ] 0 -99.2% -3.68Ki [LOAD #5 [RW]]
+195% +64.0Mi +194% +64.0Mi TOTAL
$ bloaty out/cov-cor/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
+13% +448 +19% +448 .init_array
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.2% +64 +1.2% +64 .plt
+2.9% +64 [ = ] 0 [ELF Section Headers]
+0.0% +40 +0.0% +40 .data
+1.2% +32 +1.2% +32 .got.plt
+0.0% +24 +0.0% +8 [5 Others]
[ = ] 0 -22.9% -872 [LOAD #5 [RW]]
-74.5% -1.44Ki [ = ] 0 [Unmapped]
[ = ] 0 -76.5% -1.45Ki .relro_padding
+118% +38.8Mi +117% +38.8Mi TOTAL
```
A few things to note:
1. llvm-profdata doesn't support filter raw profiles by binary id yet,
so when a raw profile doesn't belongs to the binary being digested by
llvm-profdata, merging will fail. Once this is implemented,
llvm-profdata should be able to only merge raw profiles with the same
binary id as the binary and discard the rest (with mismatched/missing
binary id). The workflow I have in mind is to have scripts invoke
llvm-profdata to get all binary ids for all raw profiles, and
selectively choose the raw pnrofiles with matching binary id and the
binary to llvm-profdata for merging.
2. Note: In COFF, currently they are still loaded into memory but not
used. I didn't do it in this patch because I noticed that `.lcovmap` and
`.lcovfunc` are loaded into memory. A separate patch will address it.
3. This should works with PGO when value profiling is disabled as debug
info correlation currently doing, though I haven't tested this yet.
2023-12-14 14:16:38 -05:00
|
|
|
} // namespace llvm
|
2021-12-16 14:19:03 -08:00
|
|
|
|
2010-06-07 23:20:08 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-02-13 16:07:05 +00:00
|
|
|
// Default filename used for profile generation.
|
2021-12-16 14:19:03 -08:00
|
|
|
std::string getDefaultProfileGenName() {
|
[Profile] Add binary profile correlation for code coverage. (#69493)
## Motivation
Since we don't need the metadata sections at runtime, we can somehow
offload them from memory at runtime. Initially, I explored [debug info
correlation](https://discourse.llvm.org/t/instrprofiling-lightweight-instrumentation/59113),
which is used for PGO with value profiling disabled. However, it
currently only works with DWARF and it's be hard to add such artificial
debug info for every function in to CodeView which is used on Windows.
So, offloading profile metadata sections at runtime seems to be a
platform independent option.
## Design
The idea is to use new section names for profile name and data sections
and mark them as metadata sections. Under this mode, the new sections
are non-SHF_ALLOC in ELF. So, they are not loaded into memory at runtime
and can be stripped away as a post-linking step. After the process
exits, the generated raw profiles will contains only headers + counters.
llvm-profdata can be used correlate raw profiles with the unstripped
binary to generate indexed profile.
## Data
For chromium base_unittests with code coverage on linux, the binary size
overhead due to instrumentation reduced from 64M to 38.8M (39.4%) and
the raw profile files size reduce from 128M to 68M (46.9%)
```
$ bloaty out/cov/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +14.6Mi [NEW] +14.6Mi __llvm_prf_data
[NEW] +10.6Mi [NEW] +10.6Mi __llvm_prf_names
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
[ = ] 0 +65% +1.23Ki .relro_padding
+62% +1.20Ki [ = ] 0 [Unmapped]
+13% +448 +19% +448 .init_array
+8.8% +192 [ = ] 0 [ELF Section Headers]
+0.0% +136 +0.0% +80 [7 Others]
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.5% +80 +1.2% +64 .plt
[ = ] 0 -99.2% -3.68Ki [LOAD #5 [RW]]
+195% +64.0Mi +194% +64.0Mi TOTAL
$ bloaty out/cov-cor/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
+13% +448 +19% +448 .init_array
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.2% +64 +1.2% +64 .plt
+2.9% +64 [ = ] 0 [ELF Section Headers]
+0.0% +40 +0.0% +40 .data
+1.2% +32 +1.2% +32 .got.plt
+0.0% +24 +0.0% +8 [5 Others]
[ = ] 0 -22.9% -872 [LOAD #5 [RW]]
-74.5% -1.44Ki [ = ] 0 [Unmapped]
[ = ] 0 -76.5% -1.45Ki .relro_padding
+118% +38.8Mi +117% +38.8Mi TOTAL
```
A few things to note:
1. llvm-profdata doesn't support filter raw profiles by binary id yet,
so when a raw profile doesn't belongs to the binary being digested by
llvm-profdata, merging will fail. Once this is implemented,
llvm-profdata should be able to only merge raw profiles with the same
binary id as the binary and discard the rest (with mismatched/missing
binary id). The workflow I have in mind is to have scripts invoke
llvm-profdata to get all binary ids for all raw profiles, and
selectively choose the raw pnrofiles with matching binary id and the
binary to llvm-profdata for merging.
2. Note: In COFF, currently they are still loaded into memory but not
used. I didn't do it in this patch because I noticed that `.lcovmap` and
`.lcovfunc` are loaded into memory. A separate patch will address it.
3. This should works with PGO when value profiling is disabled as debug
info correlation currently doing, though I haven't tested this yet.
2023-12-14 14:16:38 -05:00
|
|
|
return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE
|
|
|
|
? "default_%m.proflite"
|
|
|
|
: "default_%m.profraw";
|
2021-12-16 14:19:03 -08:00
|
|
|
}
|
2017-02-13 16:07:05 +00:00
|
|
|
|
2010-06-07 23:20:08 +00:00
|
|
|
class EmitAssemblyHelper {
|
2011-09-25 23:23:43 +00:00
|
|
|
DiagnosticsEngine &Diags;
|
2017-01-05 16:02:32 +00:00
|
|
|
const HeaderSearchOptions &HSOpts;
|
2010-06-07 23:20:08 +00:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
2011-12-02 22:17:00 +00:00
|
|
|
const clang::TargetOptions &TargetOpts;
|
2011-07-05 22:02:36 +00:00
|
|
|
const LangOptions &LangOpts;
|
2023-11-08 10:53:49 -08:00
|
|
|
llvm::Module *TheModule;
|
2023-02-01 09:24:44 -08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
|
2010-06-07 23:20:08 +00:00
|
|
|
|
|
|
|
Timer CodeGenerationTime;
|
|
|
|
|
2016-07-15 00:55:40 +00:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS;
|
2010-06-07 23:20:08 +00:00
|
|
|
|
2022-04-04 11:23:24 +03:00
|
|
|
Triple TargetTriple;
|
|
|
|
|
2015-02-01 12:26:23 +00:00
|
|
|
TargetIRAnalysis getTargetIRAnalysis() const {
|
2015-01-31 11:18:46 +00:00
|
|
|
if (TM)
|
2015-02-01 12:26:23 +00:00
|
|
|
return TM->getTargetIRAnalysis();
|
2015-01-31 11:18:46 +00:00
|
|
|
|
2015-02-01 12:26:23 +00:00
|
|
|
return TargetIRAnalysis();
|
2015-01-31 11:18:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 15:15:49 +00:00
|
|
|
/// Generates the TargetMachine.
|
2016-07-15 00:55:40 +00:00
|
|
|
/// Leaves TM unchanged if it is unable to create the target machine.
|
2012-10-24 03:52:31 +00:00
|
|
|
/// Some of our clang tests specify triples which are not built
|
|
|
|
/// into clang. This is okay because these tests check the generated
|
|
|
|
/// IR, and they require DataLayout which depends on the triple.
|
|
|
|
/// In this case, we allow this method to fail and not report an error.
|
|
|
|
/// When MustCreateTM is used, we print an error if we are unable to load
|
|
|
|
/// the requested target.
|
2016-07-15 00:55:40 +00:00
|
|
|
void CreateTargetMachine(bool MustCreateTM);
|
2010-06-07 23:20:08 +00:00
|
|
|
|
2015-04-14 15:15:49 +00:00
|
|
|
/// Add passes necessary to emit assembly or LLVM IR.
|
2010-06-07 23:20:08 +00:00
|
|
|
///
|
|
|
|
/// \return True on success.
|
2016-07-15 00:55:40 +00:00
|
|
|
bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
|
2018-05-22 18:52:37 +00:00
|
|
|
raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
|
|
|
|
std::error_code EC;
|
2019-08-14 23:04:18 +00:00
|
|
|
auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
|
2019-08-05 05:43:48 +00:00
|
|
|
llvm::sys::fs::OF_None);
|
2018-05-22 18:52:37 +00:00
|
|
|
if (EC) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
|
|
|
|
F.reset();
|
|
|
|
}
|
|
|
|
return F;
|
|
|
|
}
|
2010-06-07 23:20:08 +00:00
|
|
|
|
2023-11-08 10:53:49 -08:00
|
|
|
void RunOptimizationPipeline(
|
|
|
|
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC);
|
2021-10-11 13:29:58 -07:00
|
|
|
void RunCodegenPipeline(BackendAction Action,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> &OS,
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
|
|
|
|
|
2022-04-07 08:43:10 -07:00
|
|
|
/// Check whether we should emit a module summary for regular LTO.
|
|
|
|
/// The module summary should be emitted by default for regular LTO
|
|
|
|
/// except for ld64 targets.
|
|
|
|
///
|
|
|
|
/// \return True if the module summary should be emitted.
|
|
|
|
bool shouldEmitRegularLTOSummary() const {
|
|
|
|
return CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses &&
|
|
|
|
TargetTriple.getVendor() != llvm::Triple::Apple;
|
|
|
|
}
|
|
|
|
|
2010-06-07 23:20:08 +00:00
|
|
|
public:
|
2017-01-05 16:02:32 +00:00
|
|
|
EmitAssemblyHelper(DiagnosticsEngine &_Diags,
|
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
const CodeGenOptions &CGOpts,
|
2011-12-02 22:17:00 +00:00
|
|
|
const clang::TargetOptions &TOpts,
|
2023-11-08 10:53:49 -08:00
|
|
|
const LangOptions &LOpts, llvm::Module *M,
|
2023-02-01 09:24:44 -08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
|
2017-01-05 16:02:32 +00:00
|
|
|
: Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
|
2023-02-01 09:24:44 -08:00
|
|
|
TargetOpts(TOpts), LangOpts(LOpts), TheModule(M), VFS(std::move(VFS)),
|
2022-04-04 11:23:24 +03:00
|
|
|
CodeGenerationTime("codegen", "Code Generation Time"),
|
|
|
|
TargetTriple(TheModule->getTargetTriple()) {}
|
2010-06-07 23:20:08 +00:00
|
|
|
|
|
|
|
~EmitAssemblyHelper() {
|
2013-12-20 20:26:53 +00:00
|
|
|
if (CodeGenOpts.DisableFree)
|
Provide a BuryPointer for unique_ptrs.
In theory, it'd be nice if we could move to a case where all buried
pointers were buried via unique_ptr to demonstrate that the program had
finished with the value (that we could really have cleanly deallocated
it) but instead chose to bury it.
I think the main reason that's not possible right now is the various
IntrusiveRefCntPtrs in the Frontend, sharing ownership for a variety of
compiler bits (see the various similar
"CompilerInstance::releaseAndLeak*" functions). I have yet to figure out
their correct ownership semantics - but perhaps, even if the
intrusiveness can be removed, the shared ownership may yet remain and
that would lead to a non-unique burying as is there today. (though we
could model that a little better - by passing in a shared_ptr, etc -
rather than needing the two step that's currently used in those other
releaseAndLeak* functions)
This might be a bit more robust if BuryPointer took the boolean:
BuryPointer(bool, unique_ptr<T>)
and the choice to bury was made internally - that way, even when
DisableFree was not set, the unique_ptr would still be null in the
caller and there'd be no chance of accidentally having a different
codepath where the value is used after burial in !DisableFree, but it
becomes null only in DisableFree, etc...
llvm-svn: 216742
2014-08-29 16:53:14 +00:00
|
|
|
BuryPointer(std::move(TM));
|
2010-06-07 23:20:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:03:18 +00:00
|
|
|
std::unique_ptr<TargetMachine> TM;
|
2013-12-20 20:26:53 +00:00
|
|
|
|
2022-04-11 12:52:31 +02:00
|
|
|
// Emit output using the new pass manager for the optimization pipeline.
|
2023-11-08 10:53:49 -08:00
|
|
|
void EmitAssembly(BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS,
|
|
|
|
BackendConsumer *BC);
|
2010-06-07 23:20:08 +00:00
|
|
|
};
|
[Profile] Add binary profile correlation for code coverage. (#69493)
## Motivation
Since we don't need the metadata sections at runtime, we can somehow
offload them from memory at runtime. Initially, I explored [debug info
correlation](https://discourse.llvm.org/t/instrprofiling-lightweight-instrumentation/59113),
which is used for PGO with value profiling disabled. However, it
currently only works with DWARF and it's be hard to add such artificial
debug info for every function in to CodeView which is used on Windows.
So, offloading profile metadata sections at runtime seems to be a
platform independent option.
## Design
The idea is to use new section names for profile name and data sections
and mark them as metadata sections. Under this mode, the new sections
are non-SHF_ALLOC in ELF. So, they are not loaded into memory at runtime
and can be stripped away as a post-linking step. After the process
exits, the generated raw profiles will contains only headers + counters.
llvm-profdata can be used correlate raw profiles with the unstripped
binary to generate indexed profile.
## Data
For chromium base_unittests with code coverage on linux, the binary size
overhead due to instrumentation reduced from 64M to 38.8M (39.4%) and
the raw profile files size reduce from 128M to 68M (46.9%)
```
$ bloaty out/cov/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +14.6Mi [NEW] +14.6Mi __llvm_prf_data
[NEW] +10.6Mi [NEW] +10.6Mi __llvm_prf_names
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
[ = ] 0 +65% +1.23Ki .relro_padding
+62% +1.20Ki [ = ] 0 [Unmapped]
+13% +448 +19% +448 .init_array
+8.8% +192 [ = ] 0 [ELF Section Headers]
+0.0% +136 +0.0% +80 [7 Others]
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.5% +80 +1.2% +64 .plt
[ = ] 0 -99.2% -3.68Ki [LOAD #5 [RW]]
+195% +64.0Mi +194% +64.0Mi TOTAL
$ bloaty out/cov-cor/base_unittests.stripped -- out/no-cov/base_unittests.stripped
FILE SIZE VM SIZE
-------------- --------------
+121% +30.4Mi +121% +30.4Mi .text
[NEW] +5.86Mi [NEW] +5.86Mi __llvm_prf_cnts
+95% +1.75Mi +95% +1.75Mi .eh_frame
+108% +400Ki +108% +400Ki .eh_frame_hdr
+9.5% +211Ki +9.5% +211Ki .rela.dyn
+9.2% +95.0Ki +9.2% +95.0Ki .data.rel.ro
+5.0% +87.3Ki +5.0% +87.3Ki .rodata
[ = ] 0 +13% +47.0Ki .bss
+40% +1.78Ki +40% +1.78Ki .got
+12% +1.49Ki +12% +1.49Ki .gcc_except_table
+13% +448 +19% +448 .init_array
+0.1% +96 +0.1% +96 .dynsym
+1.2% +96 +1.2% +96 .rela.plt
+1.2% +64 +1.2% +64 .plt
+2.9% +64 [ = ] 0 [ELF Section Headers]
+0.0% +40 +0.0% +40 .data
+1.2% +32 +1.2% +32 .got.plt
+0.0% +24 +0.0% +8 [5 Others]
[ = ] 0 -22.9% -872 [LOAD #5 [RW]]
-74.5% -1.44Ki [ = ] 0 [Unmapped]
[ = ] 0 -76.5% -1.45Ki .relro_padding
+118% +38.8Mi +117% +38.8Mi TOTAL
```
A few things to note:
1. llvm-profdata doesn't support filter raw profiles by binary id yet,
so when a raw profile doesn't belongs to the binary being digested by
llvm-profdata, merging will fail. Once this is implemented,
llvm-profdata should be able to only merge raw profiles with the same
binary id as the binary and discard the rest (with mismatched/missing
binary id). The workflow I have in mind is to have scripts invoke
llvm-profdata to get all binary ids for all raw profiles, and
selectively choose the raw pnrofiles with matching binary id and the
binary to llvm-profdata for merging.
2. Note: In COFF, currently they are still loaded into memory but not
used. I didn't do it in this patch because I noticed that `.lcovmap` and
`.lcovfunc` are loaded into memory. A separate patch will address it.
3. This should works with PGO when value profiling is disabled as debug
info correlation currently doing, though I haven't tested this yet.
2023-12-14 14:16:38 -05:00
|
|
|
} // namespace
|
2012-05-22 17:19:45 +00:00
|
|
|
|
2019-07-25 20:53:15 +00:00
|
|
|
static SanitizerCoverageOptions
|
|
|
|
getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
|
2015-05-07 18:31:29 +00:00
|
|
|
SanitizerCoverageOptions Opts;
|
|
|
|
Opts.CoverageType =
|
|
|
|
static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
|
|
|
|
Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
|
|
|
|
Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
|
|
|
|
Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
|
2016-08-30 01:27:03 +00:00
|
|
|
Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
|
|
|
|
Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
|
2015-05-07 18:31:29 +00:00
|
|
|
Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
|
2016-02-17 21:34:43 +00:00
|
|
|
Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
|
2016-09-14 01:39:49 +00:00
|
|
|
Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
|
2017-05-05 23:28:18 +00:00
|
|
|
Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
|
2017-06-08 22:58:19 +00:00
|
|
|
Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
|
2020-04-09 02:33:36 -07:00
|
|
|
Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
|
2017-07-28 00:10:10 +00:00
|
|
|
Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
|
2017-08-18 18:43:30 +00:00
|
|
|
Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
|
2021-11-08 17:52:36 -08:00
|
|
|
Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads;
|
|
|
|
Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores;
|
2022-09-15 15:33:43 -07:00
|
|
|
Opts.CollectControlFlow = CGOpts.SanitizeCoverageControlFlow;
|
2019-07-25 20:53:15 +00:00
|
|
|
return Opts;
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:49:39 +02:00
|
|
|
static SanitizerBinaryMetadataOptions
|
|
|
|
getSanitizerBinaryMetadataOptions(const CodeGenOptions &CGOpts) {
|
|
|
|
SanitizerBinaryMetadataOptions Opts;
|
|
|
|
Opts.Covered = CGOpts.SanitizeBinaryMetadataCovered;
|
|
|
|
Opts.Atomics = CGOpts.SanitizeBinaryMetadataAtomics;
|
2022-10-17 15:13:56 +02:00
|
|
|
Opts.UAR = CGOpts.SanitizeBinaryMetadataUAR;
|
2022-09-06 15:49:39 +02:00
|
|
|
return Opts;
|
|
|
|
}
|
|
|
|
|
2017-04-24 19:34:12 +00:00
|
|
|
// Check if ASan should use GC-friendly instrumentation for globals.
|
|
|
|
// First of all, there is no point if -fdata-sections is off (expect for MachO,
|
|
|
|
// where this is not a factor). Also, on ELF this feature requires an assembler
|
|
|
|
// extension that only works with -integrated-as at the moment.
|
|
|
|
static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
|
2017-05-09 21:57:43 +00:00
|
|
|
if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
|
|
|
|
return false;
|
2017-04-24 19:34:12 +00:00
|
|
|
switch (T.getObjectFormat()) {
|
|
|
|
case Triple::MachO:
|
|
|
|
case Triple::COFF:
|
2017-04-26 00:51:06 +00:00
|
|
|
return true;
|
2017-04-24 19:34:12 +00:00
|
|
|
case Triple::ELF:
|
2022-02-23 16:08:25 -08:00
|
|
|
return !CGOpts.DisableIntegratedAS;
|
2020-06-19 13:43:35 +02:00
|
|
|
case Triple::GOFF:
|
|
|
|
llvm::report_fatal_error("ASan not implemented for GOFF");
|
[sanitizers] Use covering ObjectFormatType switches
Summary:
This patch removes the `default` case from some switches on
`llvm::Triple::ObjectFormatType`, and cases for the missing enumerators
(`UnknownObjectFormat`, `Wasm`, and `XCOFF`) are then added.
For `UnknownObjectFormat`, the effect of the action for the `default`
case is maintained; otherwise, where `llvm_unreachable` is called,
`report_fatal_error` is used instead.
Where the `default` case returns a default value, `report_fatal_error`
is used for XCOFF as a placeholder. For `Wasm`, the effect of the action
for the `default` case in maintained.
The code is structured to avoid strongly implying that the `Wasm` case
is present for any reason other than to make the switch cover all
`ObjectFormatType` enumerator values.
Reviewers: sfertile, jasonliu, daltenty
Reviewed By: sfertile
Subscribers: hiraditya, aheejin, sunfish, llvm-commits, cfe-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64222
llvm-svn: 366544
2019-07-19 08:46:18 +00:00
|
|
|
case Triple::XCOFF:
|
|
|
|
llvm::report_fatal_error("ASan not implemented for XCOFF.");
|
|
|
|
case Triple::Wasm:
|
2022-03-29 14:46:24 -05:00
|
|
|
case Triple::DXContainer:
|
2022-04-19 17:28:54 -07:00
|
|
|
case Triple::SPIRV:
|
[sanitizers] Use covering ObjectFormatType switches
Summary:
This patch removes the `default` case from some switches on
`llvm::Triple::ObjectFormatType`, and cases for the missing enumerators
(`UnknownObjectFormat`, `Wasm`, and `XCOFF`) are then added.
For `UnknownObjectFormat`, the effect of the action for the `default`
case is maintained; otherwise, where `llvm_unreachable` is called,
`report_fatal_error` is used instead.
Where the `default` case returns a default value, `report_fatal_error`
is used for XCOFF as a placeholder. For `Wasm`, the effect of the action
for the `default` case in maintained.
The code is structured to avoid strongly implying that the `Wasm` case
is present for any reason other than to make the switch cover all
`ObjectFormatType` enumerator values.
Reviewers: sfertile, jasonliu, daltenty
Reviewed By: sfertile
Subscribers: hiraditya, aheejin, sunfish, llvm-commits, cfe-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64222
llvm-svn: 366544
2019-07-19 08:46:18 +00:00
|
|
|
case Triple::UnknownObjectFormat:
|
|
|
|
break;
|
2017-04-24 19:34:12 +00:00
|
|
|
}
|
[sanitizers] Use covering ObjectFormatType switches
Summary:
This patch removes the `default` case from some switches on
`llvm::Triple::ObjectFormatType`, and cases for the missing enumerators
(`UnknownObjectFormat`, `Wasm`, and `XCOFF`) are then added.
For `UnknownObjectFormat`, the effect of the action for the `default`
case is maintained; otherwise, where `llvm_unreachable` is called,
`report_fatal_error` is used instead.
Where the `default` case returns a default value, `report_fatal_error`
is used for XCOFF as a placeholder. For `Wasm`, the effect of the action
for the `default` case in maintained.
The code is structured to avoid strongly implying that the `Wasm` case
is present for any reason other than to make the switch cover all
`ObjectFormatType` enumerator values.
Reviewers: sfertile, jasonliu, daltenty
Reviewed By: sfertile
Subscribers: hiraditya, aheejin, sunfish, llvm-commits, cfe-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64222
llvm-svn: 366544
2019-07-19 08:46:18 +00:00
|
|
|
return false;
|
2017-04-24 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 11:06:12 -06:00
|
|
|
static std::optional<llvm::CodeModel::Model>
|
2017-08-03 02:16:28 +00:00
|
|
|
getCodeModel(const CodeGenOptions &CodeGenOpts) {
|
|
|
|
unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
|
2018-08-22 11:34:28 +00:00
|
|
|
.Case("tiny", llvm::CodeModel::Tiny)
|
2017-08-03 02:16:28 +00:00
|
|
|
.Case("small", llvm::CodeModel::Small)
|
|
|
|
.Case("kernel", llvm::CodeModel::Kernel)
|
|
|
|
.Case("medium", llvm::CodeModel::Medium)
|
|
|
|
.Case("large", llvm::CodeModel::Large)
|
|
|
|
.Case("default", ~1u)
|
|
|
|
.Default(~0u);
|
2017-03-31 02:05:15 +00:00
|
|
|
assert(CodeModel != ~0u && "invalid code model!");
|
2017-08-03 02:16:28 +00:00
|
|
|
if (CodeModel == ~1u)
|
2022-12-03 11:06:12 -06:00
|
|
|
return std::nullopt;
|
2017-03-31 02:05:15 +00:00
|
|
|
return static_cast<llvm::CodeModel::Model>(CodeModel);
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:17:46 -08:00
|
|
|
static CodeGenFileType getCodeGenFileType(BackendAction Action) {
|
2017-03-31 02:05:15 +00:00
|
|
|
if (Action == Backend_EmitObj)
|
2023-09-14 14:10:14 -07:00
|
|
|
return CodeGenFileType::ObjectFile;
|
2017-03-31 02:05:15 +00:00
|
|
|
else if (Action == Backend_EmitMCNull)
|
2023-09-14 14:10:14 -07:00
|
|
|
return CodeGenFileType::Null;
|
2017-03-31 02:05:15 +00:00
|
|
|
else {
|
|
|
|
assert(Action == Backend_EmitAssembly && "Invalid action!");
|
2023-09-14 14:10:14 -07:00
|
|
|
return CodeGenFileType::AssemblyFile;
|
2017-03-31 02:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 00:06:28 +02:00
|
|
|
static bool actionRequiresCodeGen(BackendAction Action) {
|
|
|
|
return Action != Backend_EmitNothing && Action != Backend_EmitBC &&
|
|
|
|
Action != Backend_EmitLL;
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:26:37 -08:00
|
|
|
static bool initTargetOptions(DiagnosticsEngine &Diags,
|
2020-06-01 23:17:29 -07:00
|
|
|
llvm::TargetOptions &Options,
|
2017-03-31 02:05:15 +00:00
|
|
|
const CodeGenOptions &CodeGenOpts,
|
|
|
|
const clang::TargetOptions &TargetOpts,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
const HeaderSearchOptions &HSOpts) {
|
2020-11-18 15:26:21 -08:00
|
|
|
switch (LangOpts.getThreadModel()) {
|
|
|
|
case LangOptions::ThreadModelKind::POSIX:
|
|
|
|
Options.ThreadModel = llvm::ThreadModel::POSIX;
|
|
|
|
break;
|
|
|
|
case LangOptions::ThreadModelKind::Single:
|
|
|
|
Options.ThreadModel = llvm::ThreadModel::Single;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-31 02:05:15 +00:00
|
|
|
|
|
|
|
// Set float ABI type.
|
|
|
|
assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
|
|
|
|
CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
|
|
|
|
"Invalid Floating Point ABI!");
|
|
|
|
Options.FloatABIType =
|
|
|
|
llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
|
|
|
|
.Case("soft", llvm::FloatABI::Soft)
|
|
|
|
.Case("softfp", llvm::FloatABI::Soft)
|
|
|
|
.Case("hard", llvm::FloatABI::Hard)
|
|
|
|
.Default(llvm::FloatABI::Default);
|
|
|
|
|
|
|
|
// Set FP fusion mode.
|
|
|
|
switch (LangOpts.getDefaultFPContractMode()) {
|
2020-05-04 10:48:12 -07:00
|
|
|
case LangOptions::FPM_Off:
|
2017-04-20 17:09:35 +00:00
|
|
|
// Preserve any contraction performed by the front-end. (Strict performs
|
Misc typos fixes in ./lib folder
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned`
Reviewers: teemperor
Reviewed By: teemperor
Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D55475
llvm-svn: 348755
2018-12-10 12:37:46 +00:00
|
|
|
// splitting of the muladd intrinsic in the backend.)
|
2017-04-20 17:09:35 +00:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
|
2017-03-31 02:05:15 +00:00
|
|
|
break;
|
2020-05-04 10:48:12 -07:00
|
|
|
case LangOptions::FPM_On:
|
[HIP] Fix regressions due to fp contract change
Recently HIP toolchain made a change to use clang instead of opt/llc to do compilation
(https://reviews.llvm.org/D81861). The intention is to make HIP toolchain canonical like
other toolchains.
However, this change introduced an unintentional change regarding backend fp fuse
option, which caused regressions in some HIP applications.
Basically before the change, HIP toolchain used clang to generate bitcode, then use
opt/llc to optimize bitcode and generate ISA. As such, the amdgpu backend takes
the default fp fuse mode which is 'Standard'. This mode respect contract flag of
fmul/fadd instructions and do not fuse fmul/fadd instructions without contract flag.
However, after the change, HIP toolchain now use clang to generate IR, do optimization,
and generate ISA as one process. Now amdgpu backend fp fuse option is determined
by -ffp-contract option, which is 'fast' by default. And this -ffp-contract=fast language option
is translated to 'Fast' fp fuse option in backend. Suddenly backend starts to fuse fmul/fadd
instructions without contract flag.
This causes wrong result for some device library functions, e.g. tan(-1e20), which should
return 0.8446, now returns -0.933. What is worse is that since backend with 'Fast' fp fuse
option does not respect contract flag, there is no way to use #pragma clang fp contract
directive to enforce fp contract requirements.
This patch fixes the regression by introducing a new value 'fast-honor-pragmas' for -ffp-contract
and use it for HIP by default. 'fast-honor-pragmas' is equivalent to 'fast' in frontend but
let the backend to use 'Standard' fp fuse option. 'fast-honor-pragmas' is useful since 'Fast'
fp fuse option in backend does not honor contract flag, it is of little use to HIP
applications since all code with #pragma STDC FP_CONTRACT or any IR from a
source compiled with -ffp-contract=on is broken.
Differential Revision: https://reviews.llvm.org/D90174
2020-10-23 16:24:48 -04:00
|
|
|
case LangOptions::FPM_FastHonorPragmas:
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
|
|
|
|
break;
|
2020-05-04 10:48:12 -07:00
|
|
|
case LangOptions::FPM_Fast:
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Add -fbinutils-version= to gate ELF features on the specified binutils version
There are two use cases.
Assembler
We have accrued some code gated on MCAsmInfo::useIntegratedAssembler(). Some
features are supported by latest GNU as, but we have to use
MCAsmInfo::useIntegratedAs() because the newer versions have not been widely
adopted (e.g. SHF_LINK_ORDER 'o' and 'unique' linkage in 2.35, --compress-debug-sections= in 2.26).
Linker
We want to use features supported only by LLD or very new GNU ld, or don't want
to work around older GNU ld. We currently can't represent that "we don't care
about old GNU ld". You can find such workarounds in a few other places, e.g.
Mips/MipsAsmprinter.cpp PowerPC/PPCTOCRegDeps.cpp X86/X86MCInstrLower.cpp
AArch64 TLS workaround for R_AARCH64_TLSLD_MOVW_DTPREL_* (PR ld/18276),
R_AARCH64_TLSLE_LDST8_TPREL_LO12 (https://bugs.llvm.org/show_bug.cgi?id=36727 https://sourceware.org/bugzilla/show_bug.cgi?id=22969)
Mixed SHF_LINK_ORDER and non-SHF_LINK_ORDER components (supported by LLD in D84001;
GNU ld feature request https://sourceware.org/bugzilla/show_bug.cgi?id=16833 may take a while before available).
This feature allows to garbage collect some unused sections (e.g. fragmented .gcc_except_table).
This patch adds `-fbinutils-version=` to clang and `-binutils-version` to llc.
It changes one codegen place in SHF_MERGE to demonstrate its usage.
`-fbinutils-version=2.35` means the produced object file does not care about GNU
ld<2.35 compatibility. When `-fno-integrated-as` is specified, the produced
assembly can be consumed by GNU as>=2.35, but older versions may not work.
`-fbinutils-version=none` means that we can use all ELF features, regardless of
GNU as/ld support.
Both clang and llc need `parseBinutilsVersion`. Such command line parsing is
usually implemented in `llvm/lib/CodeGen/CommandFlags.cpp` (LLVMCodeGen),
however, ClangCodeGen does not depend on LLVMCodeGen. So I add
`parseBinutilsVersion` to `llvm/lib/Target/TargetMachine.cpp` (LLVMTarget).
Differential Revision: https://reviews.llvm.org/D85474
2021-01-26 12:28:23 -08:00
|
|
|
Options.BinutilsVersion =
|
|
|
|
llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion);
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.UseInitArray = CodeGenOpts.UseInitArray;
|
|
|
|
Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
|
2017-06-09 00:40:30 +00:00
|
|
|
Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
|
|
|
|
|
|
|
|
// Set EABI version.
|
2017-07-01 07:57:23 +00:00
|
|
|
Options.EABIVersion = TargetOpts.EABIVersion;
|
2017-03-31 02:05:15 +00:00
|
|
|
|
2020-12-14 13:08:48 +01:00
|
|
|
if (LangOpts.hasSjLjExceptions())
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
|
2020-12-14 13:08:48 +01:00
|
|
|
if (LangOpts.hasSEHExceptions())
|
2017-11-29 07:25:12 +00:00
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
|
2020-12-14 13:08:48 +01:00
|
|
|
if (LangOpts.hasDWARFExceptions())
|
2017-11-29 07:25:12 +00:00
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
|
2020-12-14 13:08:48 +01:00
|
|
|
if (LangOpts.hasWasmExceptions())
|
2019-09-12 04:01:37 +00:00
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
|
2017-03-31 02:05:15 +00:00
|
|
|
|
2020-06-01 21:02:02 -04:00
|
|
|
Options.NoInfsFPMath = LangOpts.NoHonorInfs;
|
|
|
|
Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
|
Fix `unsafe-fp-math` attribute emission.
The conditions for which Clang emits the `unsafe-fp-math` function
attribute has been modified as part of
`84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7`.
In the backend code generators `"unsafe-fp-math"="true"` enable floating
point contraction for the whole function.
The intent of the change in `84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7`
was to prevent backend code generators performing contractions when that
is not expected.
However the change is inaccurate and incomplete because it allows
`unsafe-fp-math` to be set also when only in-statement contraction is
allowed.
Consider the following example
```
float foo(float a, float b, float c) {
float tmp = a * b;
return tmp + c;
}
```
and compile it with the command line
```
clang -fno-math-errno -funsafe-math-optimizations -ffp-contract=on \
-O2 -mavx512f -S -o -
```
The resulting assembly has a `vfmadd213ss` instruction which corresponds
to a fused multiply-add. From the user perspective there shouldn't be
any contraction because the multiplication and the addition are not in
the same statement.
The optimized IR is:
```
define float @test(float noundef %a, float noundef %b, float noundef %c) #0 {
%mul = fmul reassoc nsz arcp afn float %b, %a
%add = fadd reassoc nsz arcp afn float %mul, %c
ret float %add
}
attributes #0 = {
[...]
"no-signed-zeros-fp-math"="true"
"no-trapping-math"="true"
[...]
"unsafe-fp-math"="true"
}
```
The `"unsafe-fp-math"="true"` function attribute allows the backend code
generator to perform `(fadd (fmul a, b), c) -> (fmadd a, b, c)`.
In the current IR representation there is no way to determine the
statement boundaries from the original source code.
Because of this for in-statement only contraction the generated IR
doesn't have instructions with the `contract` fast-math flag and
`llvm.fmuladd` is being used to represent contractions opportunities
that occur within a single statement.
Therefore `"unsafe-fp-math"="true"` can only be emitted when contraction
across statements is allowed.
Moreover the change in `84a9ec2ff1ee97fd7e8ed988f5e7b197aab84a7` doesn't
take into account that the floating point math function attributes can
be refined during IR code generation of a function to handle the cases
where the floating point math options are modified within a compound
statement via pragmas (see `CGFPOptionsRAII`).
For consistency `unsafe-fp-math` needs to be disabled if the contraction
mode for any scope/operation is not `fast`.
Similarly for consistency reason the initialization of `UnsafeFPMath` of
in `TargetOptions` for the backend code generation should take into
account the contraction mode as well.
Reviewed By: zahiraam
Differential Revision: https://reviews.llvm.org/D136786
2022-11-11 12:06:29 -08:00
|
|
|
Options.UnsafeFPMath = LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
|
|
|
|
LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
|
|
|
|
(LangOpts.getDefaultFPContractMode() ==
|
|
|
|
LangOptions::FPModeKind::FPM_Fast ||
|
|
|
|
LangOpts.getDefaultFPContractMode() ==
|
|
|
|
LangOptions::FPModeKind::FPM_FastHonorPragmas);
|
2021-10-08 14:26:14 -04:00
|
|
|
Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
|
2020-06-01 23:17:29 -07:00
|
|
|
|
|
|
|
Options.BBSections =
|
|
|
|
llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
|
|
|
|
.Case("all", llvm::BasicBlockSection::All)
|
|
|
|
.Case("labels", llvm::BasicBlockSection::Labels)
|
|
|
|
.StartsWith("list=", llvm::BasicBlockSection::List)
|
|
|
|
.Case("none", llvm::BasicBlockSection::None)
|
|
|
|
.Default(llvm::BasicBlockSection::None);
|
|
|
|
|
|
|
|
if (Options.BBSections == llvm::BasicBlockSection::List) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
|
2020-11-09 09:26:37 -08:00
|
|
|
if (!MBOrErr) {
|
2020-06-01 23:17:29 -07:00
|
|
|
Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
|
|
|
|
<< MBOrErr.getError().message();
|
2020-11-09 09:26:37 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
|
2020-06-01 23:17:29 -07:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:00:46 -07:00
|
|
|
Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.FunctionSections = CodeGenOpts.FunctionSections;
|
|
|
|
Options.DataSections = CodeGenOpts.DataSections;
|
2021-03-09 10:38:00 -05:00
|
|
|
Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
|
2020-06-01 23:17:29 -07:00
|
|
|
Options.UniqueBasicBlockSectionNames =
|
|
|
|
CodeGenOpts.UniqueBasicBlockSectionNames;
|
2020-01-13 09:28:02 +00:00
|
|
|
Options.TLSSize = CodeGenOpts.TLSSize;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
|
|
|
|
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
|
2018-01-08 13:42:26 +00:00
|
|
|
Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
|
2021-04-14 13:54:47 -07:00
|
|
|
Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
|
2018-07-18 00:27:07 +00:00
|
|
|
Options.EmitAddrsig = CodeGenOpts.Addrsig;
|
2019-10-31 08:55:57 +00:00
|
|
|
Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
|
2020-03-09 11:02:35 +01:00
|
|
|
Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
|
2023-05-01 10:30:29 -04:00
|
|
|
Options.EnableAIXExtendedAltivecABI = LangOpts.EnableAIXExtendedAltivecABI;
|
2023-06-11 15:27:22 -07:00
|
|
|
Options.XRayFunctionIndex = CodeGenOpts.XRayFunctionIndex;
|
2021-08-05 12:17:50 -07:00
|
|
|
Options.LoopAlignment = CodeGenOpts.LoopAlignment;
|
2022-02-10 15:10:48 -08:00
|
|
|
Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
|
|
|
|
Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
|
|
|
|
Options.Hotpatch = CodeGenOpts.HotPatch;
|
|
|
|
Options.JMCInstrument = CodeGenOpts.JMCInstrument;
|
2023-05-15 11:14:05 -04:00
|
|
|
Options.XCOFFReadOnlyPointers = CodeGenOpts.XCOFFReadOnlyPointers;
|
2017-03-31 02:05:15 +00:00
|
|
|
|
2021-09-08 07:26:08 -07:00
|
|
|
switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
|
|
|
|
case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
|
|
|
|
Options.SwiftAsyncFramePointer =
|
|
|
|
SwiftAsyncFramePointerMode::DeploymentBased;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
|
|
|
|
Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
|
|
|
|
Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-26 21:36:35 +00:00
|
|
|
Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
|
2022-06-12 17:15:56 -04:00
|
|
|
Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
|
2023-06-06 14:00:47 -04:00
|
|
|
Options.MCOptions.EmitCompactUnwindNonCanonical =
|
|
|
|
CodeGenOpts.EmitCompactUnwindNonCanonical;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
|
|
|
|
Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
|
2022-04-26 18:45:09 +03:00
|
|
|
Options.MCOptions.MCUseDwarfDirectory =
|
|
|
|
CodeGenOpts.NoDwarfDirectoryAsm
|
|
|
|
? llvm::MCTargetOptions::DisableDwarfDirectory
|
|
|
|
: llvm::MCTargetOptions::EnableDwarfDirectory;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
|
|
|
|
Options.MCOptions.MCIncrementalLinkerCompatible =
|
|
|
|
CodeGenOpts.IncrementalLinkerCompatible;
|
|
|
|
Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
|
2019-08-08 19:19:20 +00:00
|
|
|
Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
|
2021-01-08 12:58:03 -08:00
|
|
|
Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
|
2017-03-31 02:05:15 +00:00
|
|
|
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
|
|
|
|
Options.MCOptions.ABIName = TargetOpts.ABI;
|
|
|
|
for (const auto &Entry : HSOpts.UserEntries)
|
|
|
|
if (!Entry.IsFramework &&
|
|
|
|
(Entry.Group == frontend::IncludeDirGroup::Quoted ||
|
|
|
|
Entry.Group == frontend::IncludeDirGroup::Angled ||
|
|
|
|
Entry.Group == frontend::IncludeDirGroup::System))
|
|
|
|
Options.MCOptions.IASSearchPaths.push_back(
|
|
|
|
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
|
2020-06-18 08:57:50 -04:00
|
|
|
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
|
|
|
|
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
|
2022-10-27 16:52:15 -07:00
|
|
|
Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
|
2023-11-06 12:30:19 -05:00
|
|
|
Options.MCOptions.PPCUseFullRegisterNames =
|
|
|
|
CodeGenOpts.PPCUseFullRegisterNames;
|
2022-04-01 22:03:48 +00:00
|
|
|
Options.MisExpect = CodeGenOpts.MisExpect;
|
2020-11-09 09:26:37 -08:00
|
|
|
|
|
|
|
return true;
|
2017-03-31 02:05:15 +00:00
|
|
|
}
|
2020-08-28 16:32:35 -07:00
|
|
|
|
2023-01-14 12:31:01 -08:00
|
|
|
static std::optional<GCOVOptions>
|
|
|
|
getGCOVOptions(const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts) {
|
2023-05-17 16:09:12 -07:00
|
|
|
if (CodeGenOpts.CoverageNotesFile.empty() &&
|
|
|
|
CodeGenOpts.CoverageDataFile.empty())
|
2022-12-03 11:13:43 -08:00
|
|
|
return std::nullopt;
|
2018-01-09 22:03:47 +00:00
|
|
|
// Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
|
|
|
|
// LLVM's -default-gcov-version flag is set to something invalid.
|
|
|
|
GCOVOptions Options;
|
2023-05-17 16:09:12 -07:00
|
|
|
Options.EmitNotes = !CodeGenOpts.CoverageNotesFile.empty();
|
|
|
|
Options.EmitData = !CodeGenOpts.CoverageDataFile.empty();
|
2018-01-09 22:03:47 +00:00
|
|
|
llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
2018-11-17 19:41:39 +00:00
|
|
|
Options.Filter = CodeGenOpts.ProfileFilterFiles;
|
|
|
|
Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
|
2020-09-29 10:38:51 -07:00
|
|
|
Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
|
2018-01-09 22:03:47 +00:00
|
|
|
return Options;
|
|
|
|
}
|
2017-03-31 02:05:15 +00:00
|
|
|
|
2023-01-14 12:31:01 -08:00
|
|
|
static std::optional<InstrProfOptions>
|
2019-04-25 17:52:43 +00:00
|
|
|
getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
if (!CodeGenOpts.hasProfileClangInstr())
|
2022-12-03 11:13:43 -08:00
|
|
|
return std::nullopt;
|
2019-04-25 17:52:43 +00:00
|
|
|
InstrProfOptions Options;
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
|
|
|
Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
|
2020-09-29 10:38:51 -07:00
|
|
|
Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
|
2019-04-25 17:52:43 +00:00
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:08:21 +00:00
|
|
|
static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
|
2016-04-12 20:22:32 +00:00
|
|
|
SmallVector<const char *, 16> BackendArgs;
|
|
|
|
BackendArgs.push_back("clang"); // Fake program name.
|
|
|
|
if (!CodeGenOpts.DebugPass.empty()) {
|
|
|
|
BackendArgs.push_back("-debug-pass");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
|
|
|
|
}
|
|
|
|
if (!CodeGenOpts.LimitFloatPrecision.empty()) {
|
|
|
|
BackendArgs.push_back("-limit-float-precision");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
|
|
|
|
}
|
2021-04-01 18:41:44 +02:00
|
|
|
// Check for the default "clang" invocation that won't set any cl::opt values.
|
|
|
|
// Skip trying to parse the command line invocation to avoid the issues
|
|
|
|
// described below.
|
|
|
|
if (BackendArgs.size() == 1)
|
|
|
|
return;
|
2016-04-12 20:22:32 +00:00
|
|
|
BackendArgs.push_back(nullptr);
|
2021-04-01 18:41:44 +02:00
|
|
|
// FIXME: The command line parser below is not thread-safe and shares a global
|
|
|
|
// state, so this call might crash or overwrite the options of another Clang
|
|
|
|
// instance in the same process.
|
2016-04-12 20:22:32 +00:00
|
|
|
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
|
|
|
|
BackendArgs.data());
|
|
|
|
}
|
|
|
|
|
2016-07-15 00:55:40 +00:00
|
|
|
void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
|
2010-06-07 23:20:08 +00:00
|
|
|
// Create the TargetMachine for generating code.
|
|
|
|
std::string Error;
|
|
|
|
std::string Triple = TheModule->getTargetTriple();
|
|
|
|
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
|
|
|
|
if (!TheTarget) {
|
2012-10-24 03:52:31 +00:00
|
|
|
if (MustCreateTM)
|
2013-03-27 00:14:35 +00:00
|
|
|
Diags.Report(diag::err_fe_unable_to_create_target) << Error;
|
2016-07-15 00:55:40 +00:00
|
|
|
return;
|
2010-06-07 23:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 11:06:12 -06:00
|
|
|
std::optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
|
2015-09-26 01:25:08 +00:00
|
|
|
std::string FeaturesStr =
|
|
|
|
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
|
2018-01-18 00:20:03 +00:00
|
|
|
llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
|
2023-09-14 14:10:14 -07:00
|
|
|
std::optional<CodeGenOptLevel> OptLevelOrNone =
|
2023-01-16 23:55:22 +00:00
|
|
|
CodeGenOpt::getLevel(CodeGenOpts.OptimizationLevel);
|
|
|
|
assert(OptLevelOrNone && "Invalid optimization level!");
|
2023-09-14 14:10:14 -07:00
|
|
|
CodeGenOptLevel OptLevel = *OptLevelOrNone;
|
2011-11-16 08:38:55 +00:00
|
|
|
|
2011-12-02 22:17:00 +00:00
|
|
|
llvm::TargetOptions Options;
|
2020-11-09 09:26:37 -08:00
|
|
|
if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
|
|
|
|
HSOpts))
|
|
|
|
return;
|
2016-07-15 00:55:40 +00:00
|
|
|
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
|
|
|
|
Options, RM, CM, OptLevel));
|
2023-09-26 09:44:31 -07:00
|
|
|
TM->setLargeDataThreshold(CodeGenOpts.LargeDataThreshold);
|
2012-10-24 00:53:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 00:55:40 +00:00
|
|
|
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
|
|
|
|
BackendAction Action,
|
2018-05-22 18:52:37 +00:00
|
|
|
raw_pwrite_stream &OS,
|
|
|
|
raw_pwrite_stream *DwoOS) {
|
2012-02-29 20:14:59 +00:00
|
|
|
// Add LibraryInfo.
|
2020-04-11 01:00:58 +00:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
2023-11-13 10:04:50 +00:00
|
|
|
llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
|
2016-07-15 00:55:40 +00:00
|
|
|
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
|
2012-02-29 20:14:59 +00:00
|
|
|
|
2010-06-07 23:20:08 +00:00
|
|
|
// Normal mode, emit a .s or .o file by running the code generator. Note,
|
|
|
|
// this also adds codegenerator level optimization passes.
|
2019-11-13 15:17:46 -08:00
|
|
|
CodeGenFileType CGFT = getCodeGenFileType(Action);
|
2011-07-05 22:02:36 +00:00
|
|
|
|
|
|
|
// Add ObjC ARC final-cleanup optimizations. This is done as part of the
|
|
|
|
// "codegen" passes so that it isn't run multiple times when there is
|
|
|
|
// inlining happening.
|
2015-05-02 00:56:15 +00:00
|
|
|
if (CodeGenOpts.OptimizationLevel > 0)
|
2016-07-15 00:55:40 +00:00
|
|
|
CodeGenPasses.add(createObjCARCContractPass());
|
2011-07-05 22:02:36 +00:00
|
|
|
|
2018-05-22 18:52:37 +00:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
|
2010-06-07 23:20:08 +00:00
|
|
|
/*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_interface_with_target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-29 21:42:05 -07:00
|
|
|
static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
|
2016-12-23 20:44:01 +00:00
|
|
|
switch (Opts.OptimizationLevel) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid optimization level!");
|
|
|
|
|
2020-11-08 13:51:29 -08:00
|
|
|
case 0:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::O0;
|
2020-11-08 13:51:29 -08:00
|
|
|
|
2016-12-23 20:44:01 +00:00
|
|
|
case 1:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::O1;
|
2016-12-23 20:44:01 +00:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
switch (Opts.OptimizeSize) {
|
|
|
|
default:
|
2018-04-06 15:14:32 +00:00
|
|
|
llvm_unreachable("Invalid optimization level for size!");
|
2016-12-23 20:44:01 +00:00
|
|
|
|
|
|
|
case 0:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::O2;
|
2016-12-23 20:44:01 +00:00
|
|
|
|
|
|
|
case 1:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::Os;
|
2016-12-23 20:44:01 +00:00
|
|
|
|
|
|
|
case 2:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::Oz;
|
2016-12-23 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
2021-07-29 21:42:05 -07:00
|
|
|
return OptimizationLevel::O3;
|
2016-12-23 20:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:53:09 +00:00
|
|
|
static void addKCFIPass(const Triple &TargetTriple, const LangOptions &LangOpts,
|
|
|
|
PassBuilder &PB) {
|
|
|
|
// If the back-end supports KCFI operand bundle lowering, skip KCFIPass.
|
|
|
|
if (TargetTriple.getArch() == llvm::Triple::x86_64 ||
|
2023-06-23 16:31:42 +00:00
|
|
|
TargetTriple.isAArch64(64) || TargetTriple.isRISCV())
|
2022-08-18 23:53:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure we lower KCFI operand bundles with -O0.
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[&](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
if (Level == OptimizationLevel::O0 &&
|
|
|
|
LangOpts.Sanitize.has(SanitizerKind::KCFI))
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(KCFIPass()));
|
|
|
|
});
|
|
|
|
|
|
|
|
// When optimizations are requested, run KCIFPass after InstCombine to
|
|
|
|
// avoid unnecessary checks.
|
|
|
|
PB.registerPeepholeEPCallback(
|
|
|
|
[&](FunctionPassManager &FPM, OptimizationLevel Level) {
|
|
|
|
if (Level != OptimizationLevel::O0 &&
|
|
|
|
LangOpts.Sanitize.has(SanitizerKind::KCFI))
|
|
|
|
FPM.addPass(KCFIPass());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-10 14:28:48 -08:00
|
|
|
static void addSanitizers(const Triple &TargetTriple,
|
|
|
|
const CodeGenOptions &CodeGenOpts,
|
|
|
|
const LangOptions &LangOpts, PassBuilder &PB) {
|
2022-09-08 17:26:53 -07:00
|
|
|
auto SanitizersCallback = [&](ModulePassManager &MPM,
|
|
|
|
OptimizationLevel Level) {
|
2021-05-25 12:28:36 +02:00
|
|
|
if (CodeGenOpts.hasSanitizeCoverage()) {
|
2021-02-10 14:28:48 -08:00
|
|
|
auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
|
2022-09-06 20:55:39 -07:00
|
|
|
MPM.addPass(SanitizerCoveragePass(
|
2021-02-10 14:28:48 -08:00
|
|
|
SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
|
2021-05-04 09:50:43 -04:00
|
|
|
CodeGenOpts.SanitizeCoverageIgnorelistFiles));
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
|
|
|
|
2022-09-06 15:49:39 +02:00
|
|
|
if (CodeGenOpts.hasSanitizeBinaryMetadata()) {
|
|
|
|
MPM.addPass(SanitizerBinaryMetadataPass(
|
2023-02-10 09:34:08 +01:00
|
|
|
getSanitizerBinaryMetadataOptions(CodeGenOpts),
|
|
|
|
CodeGenOpts.SanitizeMetadataIgnorelistFiles));
|
2022-09-06 15:49:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 14:28:48 -08:00
|
|
|
auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
|
|
|
if (LangOpts.Sanitize.has(Mask)) {
|
|
|
|
int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
|
|
|
|
bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
|
|
|
|
|
2022-01-14 00:20:33 -08:00
|
|
|
MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
|
|
|
|
CodeGenOpts.SanitizeMemoryParamRetval);
|
2022-09-05 22:30:23 -07:00
|
|
|
MPM.addPass(MemorySanitizerPass(options));
|
2021-07-29 21:42:05 -07:00
|
|
|
if (Level != OptimizationLevel::O0) {
|
2022-09-08 23:33:07 -07:00
|
|
|
// MemorySanitizer inserts complex instrumentation that mostly follows
|
|
|
|
// the logic of the original code, but operates on "shadow" values. It
|
|
|
|
// can benefit from re-running some general purpose optimization
|
|
|
|
// passes.
|
2023-11-08 10:53:49 -08:00
|
|
|
MPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
|
2022-09-08 23:33:07 -07:00
|
|
|
FunctionPassManager FPM;
|
|
|
|
FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
|
|
|
|
FPM.addPass(InstCombinePass());
|
|
|
|
FPM.addPass(JumpThreadingPass());
|
2022-11-13 14:54:26 -08:00
|
|
|
FPM.addPass(GVNPass());
|
2022-09-08 23:33:07 -07:00
|
|
|
FPM.addPass(InstCombinePass());
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
|
2021-02-10 03:38:19 -08:00
|
|
|
}
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
MSanPass(SanitizerKind::Memory, false);
|
|
|
|
MSanPass(SanitizerKind::KernelMemory, true);
|
|
|
|
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
|
2021-09-15 22:26:22 +02:00
|
|
|
MPM.addPass(ModuleThreadSanitizerPass());
|
2021-02-10 14:28:48 -08:00
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
|
|
|
if (LangOpts.Sanitize.has(Mask)) {
|
2021-11-02 16:05:14 -07:00
|
|
|
bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
|
2021-02-10 14:28:48 -08:00
|
|
|
bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
|
2021-02-09 23:02:50 -08:00
|
|
|
llvm::AsanDtorKind DestructorKind =
|
2021-04-28 14:06:28 -07:00
|
|
|
CodeGenOpts.getSanitizeAddressDtor();
|
2021-11-02 17:06:28 -07:00
|
|
|
AddressSanitizerOptions Opts;
|
|
|
|
Opts.CompileKernel = CompileKernel;
|
|
|
|
Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
|
|
|
|
Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
|
|
|
|
Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
|
2022-09-05 22:37:45 -07:00
|
|
|
MPM.addPass(AddressSanitizerPass(Opts, UseGlobalGC, UseOdrIndicator,
|
|
|
|
DestructorKind));
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
ASanPass(SanitizerKind::Address, false);
|
|
|
|
ASanPass(SanitizerKind::KernelAddress, true);
|
|
|
|
|
|
|
|
auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
|
|
|
if (LangOpts.Sanitize.has(Mask)) {
|
|
|
|
bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
|
2021-07-22 14:19:21 -07:00
|
|
|
MPM.addPass(HWAddressSanitizerPass(
|
2021-06-28 11:16:40 +02:00
|
|
|
{CompileKernel, Recover,
|
|
|
|
/*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
HWASanPass(SanitizerKind::HWAddress, false);
|
|
|
|
HWASanPass(SanitizerKind::KernelHWAddress, true);
|
|
|
|
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
|
2021-02-22 14:47:29 -05:00
|
|
|
MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
2022-09-08 17:26:53 -07:00
|
|
|
};
|
2022-09-08 15:39:15 -07:00
|
|
|
if (ClSanitizeOnOptimizerEarlyEP) {
|
|
|
|
PB.registerOptimizerEarlyEPCallback(
|
|
|
|
[SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
ModulePassManager NewMPM;
|
|
|
|
SanitizersCallback(NewMPM, Level);
|
|
|
|
if (!NewMPM.isEmpty()) {
|
|
|
|
// Sanitizers can abandon<GlobalsAA>.
|
2023-11-08 10:53:49 -08:00
|
|
|
NewMPM.addPass(RequireAnalysisPass<GlobalsAA, llvm::Module>());
|
2022-09-08 15:39:15 -07:00
|
|
|
MPM.addPass(std::move(NewMPM));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// LastEP does not need GlobalsAA.
|
2022-09-08 17:26:53 -07:00
|
|
|
PB.registerOptimizerLastEPCallback(SanitizersCallback);
|
2022-09-08 15:39:15 -07:00
|
|
|
}
|
2021-02-10 14:28:48 -08:00
|
|
|
}
|
|
|
|
|
2021-10-11 13:29:58 -07:00
|
|
|
void EmitAssemblyHelper::RunOptimizationPipeline(
|
|
|
|
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
|
2023-11-08 10:53:49 -08:00
|
|
|
std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) {
|
2022-12-04 20:44:52 +00:00
|
|
|
std::optional<PGOOptions> PGOOpt;
|
2017-07-27 15:29:53 +00:00
|
|
|
|
|
|
|
if (CodeGenOpts.hasProfileIRInstr())
|
|
|
|
// -fprofile-generate.
|
2023-02-01 09:24:44 -08:00
|
|
|
PGOOpt = PGOOptions(
|
|
|
|
CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
|
|
|
|
: CodeGenOpts.InstrProfileOutput,
|
2023-07-11 12:08:08 -07:00
|
|
|
"", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
|
2023-08-15 09:39:03 -04:00
|
|
|
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
|
|
|
|
/*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
|
2019-03-04 20:21:31 +00:00
|
|
|
else if (CodeGenOpts.hasProfileIRUse()) {
|
2017-07-27 15:29:53 +00:00
|
|
|
// -fprofile-use.
|
2019-03-04 20:21:31 +00:00
|
|
|
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
|
|
|
|
: PGOOptions::NoCSAction;
|
2023-07-11 12:08:08 -07:00
|
|
|
PGOOpt = PGOOptions(
|
|
|
|
CodeGenOpts.ProfileInstrumentUsePath, "",
|
|
|
|
CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
|
|
|
|
PGOOptions::IRUse, CSAction, CodeGenOpts.DebugInfoForProfiling);
|
2019-03-04 20:21:31 +00:00
|
|
|
} else if (!CodeGenOpts.SampleProfileFile.empty())
|
2017-07-27 15:29:53 +00:00
|
|
|
// -fprofile-sample-use
|
2020-08-18 16:28:47 -07:00
|
|
|
PGOOpt = PGOOptions(
|
|
|
|
CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
|
2023-07-11 12:08:08 -07:00
|
|
|
CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
|
|
|
|
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
|
|
|
|
CodeGenOpts.PseudoProbeForProfiling);
|
|
|
|
else if (!CodeGenOpts.MemoryProfileUsePath.empty())
|
|
|
|
// -fmemory-profile-use (without any of the above options)
|
|
|
|
PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
|
|
|
|
PGOOptions::NoAction, PGOOptions::NoCSAction,
|
|
|
|
CodeGenOpts.DebugInfoForProfiling);
|
2020-08-18 16:28:47 -07:00
|
|
|
else if (CodeGenOpts.PseudoProbeForProfiling)
|
|
|
|
// -fpseudo-probe-for-profiling
|
2023-07-11 12:08:08 -07:00
|
|
|
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
|
|
|
|
PGOOptions::NoAction, PGOOptions::NoCSAction,
|
2023-02-01 09:24:44 -08:00
|
|
|
CodeGenOpts.DebugInfoForProfiling, true);
|
2017-07-27 15:29:53 +00:00
|
|
|
else if (CodeGenOpts.DebugInfoForProfiling)
|
|
|
|
// -fdebug-info-for-profiling
|
2023-07-11 12:08:08 -07:00
|
|
|
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
|
|
|
|
PGOOptions::NoAction, PGOOptions::NoCSAction, true);
|
2019-03-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Check to see if we want to generate a CS profile.
|
|
|
|
if (CodeGenOpts.hasProfileCSIRInstr()) {
|
|
|
|
assert(!CodeGenOpts.hasProfileCSIRUse() &&
|
|
|
|
"Cannot have both CSProfileUse pass and CSProfileGen pass at "
|
|
|
|
"the same time");
|
2022-06-20 10:51:34 -07:00
|
|
|
if (PGOOpt) {
|
2019-03-04 20:21:31 +00:00
|
|
|
assert(PGOOpt->Action != PGOOptions::IRInstr &&
|
|
|
|
PGOOpt->Action != PGOOptions::SampleUse &&
|
|
|
|
"Cannot run CSProfileGen pass with ProfileGen or SampleUse "
|
|
|
|
" pass");
|
|
|
|
PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
|
2021-12-16 14:19:03 -08:00
|
|
|
? getDefaultProfileGenName()
|
2019-03-04 20:21:31 +00:00
|
|
|
: CodeGenOpts.InstrProfileOutput;
|
|
|
|
PGOOpt->CSAction = PGOOptions::CSIRInstr;
|
|
|
|
} else
|
2023-02-01 09:24:44 -08:00
|
|
|
PGOOpt =
|
|
|
|
PGOOptions("",
|
|
|
|
CodeGenOpts.InstrProfileOutput.empty()
|
|
|
|
? getDefaultProfileGenName()
|
|
|
|
: CodeGenOpts.InstrProfileOutput,
|
2023-07-11 12:08:08 -07:00
|
|
|
"", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
|
|
|
|
PGOOptions::CSIRInstr, CodeGenOpts.DebugInfoForProfiling);
|
2019-03-04 20:21:31 +00:00
|
|
|
}
|
2021-08-18 16:59:02 -07:00
|
|
|
if (TM)
|
|
|
|
TM->setPGOOption(PGOOpt);
|
2017-07-27 15:29:53 +00:00
|
|
|
|
2019-05-23 18:51:02 +00:00
|
|
|
PipelineTuningOptions PTO;
|
2019-05-24 17:40:52 +00:00
|
|
|
PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
|
2019-05-23 18:51:02 +00:00
|
|
|
// For historical reasons, loop interleaving is set to mirror setting for loop
|
|
|
|
// unrolling.
|
|
|
|
PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
|
|
|
|
PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
|
|
|
|
PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
|
2020-12-09 22:41:47 -08:00
|
|
|
PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
|
[LPM] Port CGProfilePass from NPM to LPM
Reviewers: hans, chandlerc!, asbirlea, nikic
Reviewed By: hans, nikic
Subscribers: steven_wu, dexonsmith, nikic, echristo, void, zhizhouy, cfe-commits, aeubanks, MaskRay, jvesely, nhaehnle, hiraditya, kerbowa, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D83013
2020-07-08 12:30:28 -07:00
|
|
|
// Only enable CGProfilePass when using integrated assembler, since
|
|
|
|
// non-integrated assemblers don't recognize .cgprofile section.
|
|
|
|
PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
|
2023-07-11 15:00:14 -07:00
|
|
|
PTO.UnifiedLTO = CodeGenOpts.UnifiedLTO;
|
2019-05-23 18:51:02 +00:00
|
|
|
|
2021-05-07 14:32:40 -07:00
|
|
|
LoopAnalysisManager LAM;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
CGSCCAnalysisManager CGAM;
|
|
|
|
ModuleAnalysisManager MAM;
|
2021-04-06 11:55:18 +07:00
|
|
|
|
2021-05-07 14:32:20 -07:00
|
|
|
bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
|
2019-08-14 07:11:09 +00:00
|
|
|
PassInstrumentationCallbacks PIC;
|
2021-05-07 14:32:20 -07:00
|
|
|
PrintPassOptions PrintPassOpts;
|
|
|
|
PrintPassOpts.Indent = DebugPassStructure;
|
|
|
|
PrintPassOpts.SkipAnalyses = DebugPassStructure;
|
2022-11-01 13:42:07 +07:00
|
|
|
StandardInstrumentations SI(
|
|
|
|
TheModule->getContext(),
|
|
|
|
(CodeGenOpts.DebugPassManager || DebugPassStructure),
|
2023-06-21 14:13:05 -07:00
|
|
|
CodeGenOpts.VerifyEach, PrintPassOpts);
|
2023-03-15 11:46:44 -07:00
|
|
|
SI.registerCallbacks(PIC, &MAM);
|
2021-05-03 16:09:56 -07:00
|
|
|
PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
|
2016-12-23 20:44:01 +00:00
|
|
|
|
2023-03-29 11:33:11 +01:00
|
|
|
// Handle the assignment tracking feature options.
|
|
|
|
switch (CodeGenOpts.getAssignmentTrackingMode()) {
|
|
|
|
case CodeGenOptions::AssignmentTrackingOpts::Forced:
|
2022-11-08 16:58:53 +00:00
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[&](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
MPM.addPass(AssignmentTrackingPass());
|
|
|
|
});
|
2023-03-29 11:33:11 +01:00
|
|
|
break;
|
|
|
|
case CodeGenOptions::AssignmentTrackingOpts::Enabled:
|
|
|
|
// Disable assignment tracking in LTO builds for now as the performance
|
|
|
|
// cost is too high. Disable for LLDB tuning due to llvm.org/PR43126.
|
|
|
|
if (!CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.PrepareForLTO &&
|
|
|
|
CodeGenOpts.getDebuggerTuning() != llvm::DebuggerKind::LLDB) {
|
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[&](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
// Only use assignment tracking if optimisations are enabled.
|
|
|
|
if (Level != OptimizationLevel::O0)
|
|
|
|
MPM.addPass(AssignmentTrackingPass());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CodeGenOptions::AssignmentTrackingOpts::Disabled:
|
|
|
|
break;
|
2022-11-08 16:58:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:26:36 +02:00
|
|
|
// Enable verify-debuginfo-preserve-each for new PM.
|
|
|
|
DebugifyEachInstrumentation Debugify;
|
|
|
|
DebugInfoPerPass DebugInfoBeforePass;
|
|
|
|
if (CodeGenOpts.EnableDIPreservationVerify) {
|
|
|
|
Debugify.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
|
|
|
|
Debugify.setDebugInfoBeforePass(DebugInfoBeforePass);
|
|
|
|
|
|
|
|
if (!CodeGenOpts.DIBugsReportFilePath.empty())
|
|
|
|
Debugify.setOrigDIVerifyBugsReportFilePath(
|
|
|
|
CodeGenOpts.DIBugsReportFilePath);
|
2023-03-16 09:47:21 -07:00
|
|
|
Debugify.registerCallbacks(PIC, MAM);
|
2022-07-06 14:26:36 +02:00
|
|
|
}
|
2019-02-02 23:19:32 +00:00
|
|
|
// Attempt to load pass plugins and register their callbacks with PB.
|
|
|
|
for (auto &PluginFN : CodeGenOpts.PassPlugins) {
|
|
|
|
auto PassPlugin = PassPlugin::Load(PluginFN);
|
|
|
|
if (PassPlugin) {
|
|
|
|
PassPlugin->registerPassBuilderCallbacks(PB);
|
|
|
|
} else {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_load_plugin)
|
|
|
|
<< PluginFN << toString(PassPlugin.takeError());
|
|
|
|
}
|
|
|
|
}
|
2023-12-22 20:39:22 -06:00
|
|
|
for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
|
2023-11-06 22:26:38 -06:00
|
|
|
PassCallback(PB);
|
2019-06-08 17:37:47 +02:00
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
|
|
|
|
#include "llvm/Support/Extension.def"
|
2019-02-02 23:19:32 +00:00
|
|
|
|
2017-07-25 10:46:07 +00:00
|
|
|
// Register the target library analysis directly and give it a customized
|
|
|
|
// preset TLI.
|
2020-04-11 01:00:58 +00:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
2023-11-13 10:04:50 +00:00
|
|
|
llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
|
2017-07-25 10:46:07 +00:00
|
|
|
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
|
|
|
|
|
2016-12-23 20:44:01 +00:00
|
|
|
// Register all the basic analyses with the managers.
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
2021-05-03 16:09:56 -07:00
|
|
|
ModulePassManager MPM;
|
2023-10-03 18:05:54 +02:00
|
|
|
// Add a verifier pass, before any other passes, to catch CodeGen issues.
|
|
|
|
if (CodeGenOpts.VerifyModule)
|
|
|
|
MPM.addPass(VerifierPass());
|
2016-12-23 20:44:01 +00:00
|
|
|
|
2016-12-27 00:13:09 +00:00
|
|
|
if (!CodeGenOpts.DisableLLVMPasses) {
|
2020-11-08 13:51:29 -08:00
|
|
|
// Map our optimization levels into one of the distinct levels used to
|
|
|
|
// configure the pipeline.
|
2021-07-29 21:42:05 -07:00
|
|
|
OptimizationLevel Level = mapToLevel(CodeGenOpts);
|
2020-11-08 13:51:29 -08:00
|
|
|
|
2023-09-05 08:57:16 -07:00
|
|
|
const bool PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
|
|
|
|
const bool PrepareForLTO = CodeGenOpts.PrepareForLTO;
|
2017-06-29 23:08:38 +00:00
|
|
|
|
2020-12-22 21:41:25 -08:00
|
|
|
if (LangOpts.ObjCAutoRefCount) {
|
|
|
|
PB.registerPipelineStartEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
if (Level != OptimizationLevel::O0)
|
2020-12-22 21:41:25 -08:00
|
|
|
MPM.addPass(
|
|
|
|
createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
|
|
|
|
});
|
|
|
|
PB.registerPipelineEarlySimplificationEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
if (Level != OptimizationLevel::O0)
|
2020-12-22 21:41:25 -08:00
|
|
|
MPM.addPass(ObjCARCAPElimPass());
|
|
|
|
});
|
|
|
|
PB.registerScalarOptimizerLateEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](FunctionPassManager &FPM, OptimizationLevel Level) {
|
|
|
|
if (Level != OptimizationLevel::O0)
|
2020-12-22 21:41:25 -08:00
|
|
|
FPM.addPass(ObjCARCOptPass());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:25:35 -08:00
|
|
|
// If we reached here with a non-empty index file name, then the index
|
|
|
|
// file was empty and we are not performing ThinLTO backend compilation
|
2021-02-10 01:44:05 -08:00
|
|
|
// (used in testing in a distributed build environment).
|
|
|
|
bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
|
|
|
|
// If so drop any the type test assume sequences inserted for whole program
|
|
|
|
// vtables so that codegen doesn't complain.
|
|
|
|
if (IsThinLTOPostLink)
|
2020-11-05 10:25:35 -08:00
|
|
|
PB.registerPipelineStartEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
2020-11-05 10:25:35 -08:00
|
|
|
MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
|
|
|
|
/*ImportSummary=*/nullptr,
|
|
|
|
/*DropTypeTests=*/true));
|
|
|
|
});
|
2020-01-24 12:24:18 -08:00
|
|
|
|
2021-02-26 20:06:49 -08:00
|
|
|
if (CodeGenOpts.InstrumentFunctions ||
|
|
|
|
CodeGenOpts.InstrumentFunctionEntryBare ||
|
|
|
|
CodeGenOpts.InstrumentFunctionsAfterInlining ||
|
|
|
|
CodeGenOpts.InstrumentForProfiling) {
|
2020-11-04 10:14:37 -08:00
|
|
|
PB.registerPipelineStartEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
2020-11-04 10:14:37 -08:00
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(
|
|
|
|
EntryExitInstrumenterPass(/*PostInlining=*/false)));
|
|
|
|
});
|
2021-02-26 20:06:49 -08:00
|
|
|
PB.registerOptimizerLastEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
2021-02-26 20:06:49 -08:00
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(
|
|
|
|
EntryExitInstrumenterPass(/*PostInlining=*/true)));
|
|
|
|
});
|
2020-11-05 10:25:35 -08:00
|
|
|
}
|
2019-06-20 19:35:25 +00:00
|
|
|
|
2020-11-05 10:25:35 -08:00
|
|
|
// Register callbacks to schedule sanitizer passes at the appropriate part
|
|
|
|
// of the pipeline.
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
|
|
|
|
PB.registerScalarOptimizerLateEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[](FunctionPassManager &FPM, OptimizationLevel Level) {
|
2020-11-05 10:25:35 -08:00
|
|
|
FPM.addPass(BoundsCheckingPass());
|
|
|
|
});
|
2020-05-27 23:12:36 -07:00
|
|
|
|
2021-02-10 01:44:05 -08:00
|
|
|
// Don't add sanitizers if we are here from ThinLTO PostLink. That already
|
|
|
|
// done on PreLink stage.
|
2022-08-18 23:53:09 +00:00
|
|
|
if (!IsThinLTOPostLink) {
|
2021-02-10 01:44:05 -08:00
|
|
|
addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
|
2022-08-18 23:53:09 +00:00
|
|
|
addKCFIPass(TargetTriple, LangOpts, PB);
|
|
|
|
}
|
2020-11-12 13:19:04 -08:00
|
|
|
|
2023-01-14 12:31:01 -08:00
|
|
|
if (std::optional<GCOVOptions> Options =
|
|
|
|
getGCOVOptions(CodeGenOpts, LangOpts))
|
2020-11-05 10:25:35 -08:00
|
|
|
PB.registerPipelineStartEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
|
2020-11-05 10:25:35 -08:00
|
|
|
MPM.addPass(GCOVProfilerPass(*Options));
|
|
|
|
});
|
2023-01-14 12:31:01 -08:00
|
|
|
if (std::optional<InstrProfOptions> Options =
|
2020-11-05 10:25:35 -08:00
|
|
|
getInstrProfOptions(CodeGenOpts, LangOpts))
|
|
|
|
PB.registerPipelineStartEPCallback(
|
2021-07-29 21:42:05 -07:00
|
|
|
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
|
2023-12-10 18:03:08 -08:00
|
|
|
MPM.addPass(InstrProfilingLoweringPass(*Options, false));
|
2020-11-05 10:25:35 -08:00
|
|
|
});
|
2019-02-13 22:22:48 +00:00
|
|
|
|
2023-05-26 15:07:24 -07:00
|
|
|
// TODO: Consider passing the MemoryProfileOutput to the pass builder via
|
|
|
|
// the PGOOptions, and set this up there.
|
|
|
|
if (!CodeGenOpts.MemoryProfileOutput.empty()) {
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[](ModulePassManager &MPM, OptimizationLevel Level) {
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
|
|
|
|
MPM.addPass(ModuleMemProfilerPass());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:09:29 +00:00
|
|
|
if (CodeGenOpts.FatLTO) {
|
2024-01-23 14:04:52 -08:00
|
|
|
MPM.addPass(PB.buildFatLTODefaultPipeline(
|
|
|
|
Level, PrepareForThinLTO,
|
|
|
|
PrepareForThinLTO || shouldEmitRegularLTOSummary()));
|
2023-09-05 08:57:16 -07:00
|
|
|
} else if (PrepareForThinLTO) {
|
2023-10-03 18:05:54 +02:00
|
|
|
MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level));
|
2023-09-05 08:57:16 -07:00
|
|
|
} else if (PrepareForLTO) {
|
2023-10-03 18:05:54 +02:00
|
|
|
MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level));
|
2020-11-05 10:25:35 -08:00
|
|
|
} else {
|
2023-10-03 18:05:54 +02:00
|
|
|
MPM.addPass(PB.buildPerModuleDefaultPipeline(Level));
|
2020-11-05 10:25:35 -08:00
|
|
|
}
|
2016-12-23 20:44:01 +00:00
|
|
|
}
|
2021-11-07 00:06:28 +02:00
|
|
|
|
2023-11-08 10:53:49 -08:00
|
|
|
// Re-link against any bitcodes supplied via the -mlink-builtin-bitcode option
|
|
|
|
// Some optimizations may generate new function calls that would not have
|
|
|
|
// been linked pre-optimization (i.e. fused sincos calls generated by
|
|
|
|
// AMDGPULibCalls::fold_sincos.)
|
|
|
|
if (ClRelinkBuiltinBitcodePostop)
|
|
|
|
MPM.addPass(LinkInModulesPass(BC, false));
|
|
|
|
|
2021-11-07 00:06:28 +02:00
|
|
|
// Add a verifier pass if requested. We don't have to do this if the action
|
|
|
|
// requires code generation because there will already be a verifier pass in
|
|
|
|
// the code-generation pipeline.
|
2023-10-03 18:05:54 +02:00
|
|
|
// Since we already added a verifier pass above, this
|
|
|
|
// might even not run the analysis, if previous passes caused no changes.
|
2021-11-07 00:06:28 +02:00
|
|
|
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
|
|
|
|
MPM.addPass(VerifierPass());
|
|
|
|
|
2022-11-18 08:45:05 +00:00
|
|
|
if (Action == Backend_EmitBC || Action == Backend_EmitLL) {
|
2018-08-24 19:31:52 +00:00
|
|
|
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
|
2021-10-06 12:10:08 -07:00
|
|
|
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
|
2021-10-06 12:10:08 -07:00
|
|
|
CodeGenOpts.EnableSplitLTOUnit);
|
2022-11-18 08:45:05 +00:00
|
|
|
if (Action == Backend_EmitBC) {
|
|
|
|
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
|
|
|
|
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
|
|
|
|
if (!ThinLinkOS)
|
|
|
|
return;
|
|
|
|
}
|
2023-07-11 15:00:14 -07:00
|
|
|
if (CodeGenOpts.UnifiedLTO)
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
|
2023-07-11 15:00:14 -07:00
|
|
|
MPM.addPass(ThinLTOBitcodeWriterPass(
|
|
|
|
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
|
2022-11-18 08:45:05 +00:00
|
|
|
} else {
|
|
|
|
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
|
|
|
|
/*EmitLTOSummary=*/true));
|
|
|
|
}
|
2017-06-01 23:27:51 +00:00
|
|
|
} else {
|
2018-06-22 20:23:21 +00:00
|
|
|
// Emit a module summary by default for Regular LTO except for ld64
|
|
|
|
// targets
|
2022-04-07 08:43:10 -07:00
|
|
|
bool EmitLTOSummary = shouldEmitRegularLTOSummary();
|
2019-01-11 18:32:07 +00:00
|
|
|
if (EmitLTOSummary) {
|
2023-07-11 15:00:14 -07:00
|
|
|
if (!TheModule->getModuleFlag("ThinLTO") && !CodeGenOpts.UnifiedLTO)
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0));
|
2021-10-06 12:10:08 -07:00
|
|
|
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
|
2021-10-06 12:10:08 -07:00
|
|
|
uint32_t(1));
|
2023-07-11 15:00:14 -07:00
|
|
|
if (CodeGenOpts.UnifiedLTO)
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
|
2019-01-11 18:32:07 +00:00
|
|
|
}
|
2022-11-18 08:45:05 +00:00
|
|
|
if (Action == Backend_EmitBC)
|
|
|
|
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
|
|
|
|
EmitLTOSummary));
|
|
|
|
else
|
|
|
|
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists,
|
|
|
|
EmitLTOSummary));
|
2017-06-01 23:27:51 +00:00
|
|
|
}
|
2021-10-11 13:29:58 -07:00
|
|
|
}
|
2023-03-24 00:09:29 +00:00
|
|
|
if (CodeGenOpts.FatLTO) {
|
2023-12-18 13:03:13 -08:00
|
|
|
// Set the EnableSplitLTOUnit and UnifiedLTO module flags, since FatLTO
|
2023-03-24 00:09:29 +00:00
|
|
|
// uses a different action than Backend_EmitBC or Backend_EmitLL.
|
|
|
|
if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit",
|
2023-03-24 00:09:29 +00:00
|
|
|
uint32_t(CodeGenOpts.EnableSplitLTOUnit));
|
2024-01-23 14:04:52 -08:00
|
|
|
if (CodeGenOpts.UnifiedLTO && !TheModule->getModuleFlag("UnifiedLTO"))
|
2023-11-08 10:53:49 -08:00
|
|
|
TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
|
2023-03-24 00:09:29 +00:00
|
|
|
}
|
2021-10-11 13:29:58 -07:00
|
|
|
|
2023-09-13 08:55:22 -07:00
|
|
|
// Print a textual, '-passes=' compatible, representation of pipeline if
|
|
|
|
// requested.
|
|
|
|
if (PrintPipelinePasses) {
|
|
|
|
MPM.printPipeline(outs(), [&PIC](StringRef ClassName) {
|
|
|
|
auto PassName = PIC.getPassNameForClassName(ClassName);
|
|
|
|
return PassName.empty() ? ClassName : PassName;
|
|
|
|
});
|
|
|
|
outs() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-17 11:41:36 +01:00
|
|
|
if (LangOpts.HIPStdPar && !LangOpts.CUDAIsDevice &&
|
|
|
|
LangOpts.HIPStdParInterposeAlloc)
|
|
|
|
MPM.addPass(HipStdParAllocationInterpositionPass());
|
|
|
|
|
2021-10-11 13:29:58 -07:00
|
|
|
// Now that we have all of the passes ready, run them.
|
2022-01-18 14:08:48 -08:00
|
|
|
{
|
|
|
|
PrettyStackTraceString CrashInfo("Optimizer");
|
|
|
|
llvm::TimeTraceScope TimeScope("Optimizer");
|
|
|
|
MPM.run(*TheModule, MAM);
|
|
|
|
}
|
2021-10-11 13:29:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmitAssemblyHelper::RunCodegenPipeline(
|
|
|
|
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
|
|
|
|
// We still use the legacy PM to run the codegen pipeline since the new PM
|
|
|
|
// does not work with the codegen pipeline.
|
|
|
|
// FIXME: make the new PM work with the codegen pipeline.
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
|
|
|
|
// Append any output we need to the pass manager.
|
|
|
|
switch (Action) {
|
2016-12-23 20:44:01 +00:00
|
|
|
case Backend_EmitAssembly:
|
|
|
|
case Backend_EmitMCNull:
|
|
|
|
case Backend_EmitObj:
|
|
|
|
CodeGenPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2019-06-26 21:36:35 +00:00
|
|
|
if (!CodeGenOpts.SplitDwarfOutput.empty()) {
|
2019-06-15 14:07:43 +00:00
|
|
|
DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
|
2018-05-22 18:52:37 +00:00
|
|
|
if (!DwoOS)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!AddEmitPasses(CodeGenPasses, Action, *OS,
|
|
|
|
DwoOS ? &DwoOS->os() : nullptr))
|
2016-12-23 20:44:01 +00:00
|
|
|
// FIXME: Should we handle this error differently?
|
|
|
|
return;
|
|
|
|
break;
|
2021-10-11 13:29:58 -07:00
|
|
|
default:
|
|
|
|
return;
|
2016-12-23 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 08:55:22 -07:00
|
|
|
// If -print-pipeline-passes is requested, don't run the legacy pass manager.
|
|
|
|
// FIXME: when codegen is switched to use the new pass manager, it should also
|
|
|
|
// emit pass names here.
|
|
|
|
if (PrintPipelinePasses) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-18 14:08:48 -08:00
|
|
|
{
|
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
|
|
|
llvm::TimeTraceScope TimeScope("CodeGenPasses");
|
|
|
|
CodeGenPasses.run(*TheModule);
|
|
|
|
}
|
2021-10-11 13:29:58 -07:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:18:31 -07:00
|
|
|
void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
|
2023-11-08 10:53:49 -08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS,
|
|
|
|
BackendConsumer *BC) {
|
2021-10-11 13:29:58 -07:00
|
|
|
TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
|
|
|
|
setCommandLineOpts(CodeGenOpts);
|
|
|
|
|
2021-11-07 00:06:28 +02:00
|
|
|
bool RequiresCodeGen = actionRequiresCodeGen(Action);
|
2021-10-11 13:29:58 -07:00
|
|
|
CreateTargetMachine(RequiresCodeGen);
|
|
|
|
|
|
|
|
if (RequiresCodeGen && !TM)
|
|
|
|
return;
|
|
|
|
if (TM)
|
|
|
|
TheModule->setDataLayout(TM->createDataLayout());
|
|
|
|
|
2016-12-23 20:44:01 +00:00
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
2021-10-11 13:29:58 -07:00
|
|
|
std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
|
2023-11-08 10:53:49 -08:00
|
|
|
RunOptimizationPipeline(Action, OS, ThinLinkOS, BC);
|
2021-10-11 13:29:58 -07:00
|
|
|
RunCodegenPipeline(Action, OS, DwoOS);
|
2018-05-22 18:52:37 +00:00
|
|
|
|
|
|
|
if (ThinLinkOS)
|
|
|
|
ThinLinkOS->keep();
|
|
|
|
if (DwoOS)
|
|
|
|
DwoOS->keep();
|
2016-12-23 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 23:17:29 -07:00
|
|
|
static void runThinLTOBackend(
|
2023-11-08 10:53:49 -08:00
|
|
|
DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex,
|
|
|
|
llvm::Module *M, const HeaderSearchOptions &HeaderOpts,
|
|
|
|
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
|
|
|
|
const LangOptions &LOpts, std::unique_ptr<raw_pwrite_stream> OS,
|
|
|
|
std::string SampleProfile, std::string ProfileRemapping,
|
|
|
|
BackendAction Action) {
|
2023-07-28 16:07:14 -07:00
|
|
|
DenseMap<StringRef, DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
|
2016-08-12 18:12:08 +00:00
|
|
|
ModuleToDefinedGVSummaries;
|
|
|
|
CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
|
|
|
|
2017-04-19 20:08:21 +00:00
|
|
|
setCommandLineOpts(CGOpts);
|
|
|
|
|
2016-12-28 18:00:08 +00:00
|
|
|
// We can simply import the values mentioned in the combined index, since
|
|
|
|
// we should only invoke this using the individual indexes written out
|
|
|
|
// via a WriteIndexesThinBackend.
|
2016-08-12 18:12:08 +00:00
|
|
|
FunctionImporter::ImportMapTy ImportList;
|
2021-03-29 15:04:28 -07:00
|
|
|
if (!lto::initImportList(*M, *CombinedIndex, ImportList))
|
2020-09-16 12:08:15 -07:00
|
|
|
return;
|
2016-08-12 18:12:08 +00:00
|
|
|
|
2022-11-22 13:46:42 -08:00
|
|
|
auto AddStream = [&](size_t Task, const Twine &ModuleName) {
|
2021-12-21 18:59:47 -05:00
|
|
|
return std::make_unique<CachedFileStream>(std::move(OS),
|
|
|
|
CGOpts.ObjectFilenameForDebug);
|
2016-08-17 06:23:08 +00:00
|
|
|
};
|
2016-08-12 18:12:08 +00:00
|
|
|
lto::Config Conf;
|
2018-04-17 16:39:25 +00:00
|
|
|
if (CGOpts.SaveTempsFilePrefix != "") {
|
|
|
|
if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
|
|
|
|
/* UseInputModulePath */ false)) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
|
|
|
|
<< '\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 02:05:15 +00:00
|
|
|
Conf.CPU = TOpts.CPU;
|
|
|
|
Conf.CodeModel = getCodeModel(CGOpts);
|
|
|
|
Conf.MAttrs = TOpts.Features;
|
2018-01-18 00:20:03 +00:00
|
|
|
Conf.RelocModel = CGOpts.RelocationModel;
|
2023-09-14 14:10:14 -07:00
|
|
|
std::optional<CodeGenOptLevel> OptLevelOrNone =
|
2023-01-16 23:55:22 +00:00
|
|
|
CodeGenOpt::getLevel(CGOpts.OptimizationLevel);
|
|
|
|
assert(OptLevelOrNone && "Invalid optimization level!");
|
|
|
|
Conf.CGOptLevel = *OptLevelOrNone;
|
[ThinLTO] Pass down opt level to LTO backend and handle -O0 LTO in new PM
Summary:
The opt level was not being passed down to the ThinLTO backend when
invoked via clang (for distributed ThinLTO).
This exposed an issue where the new PM was asserting if the Thin or
regular LTO backend pipelines were invoked with -O0 (not a new issue,
could be provoked by invoking in-process *LTO backends via linker using
new PM and -O0). Fix this similar to the old PM where -O0 only does the
necessary lowering of type metadata (WPD and LowerTypeTest passes) and
then quits, rather than asserting.
Reviewers: xur
Subscribers: mehdi_amini, inglorion, eraman, hiraditya, steven_wu, dexonsmith, cfe-commits, llvm-commits, pcc
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D61022
llvm-svn: 359025
2019-04-23 18:56:19 +00:00
|
|
|
Conf.OptLevel = CGOpts.OptimizationLevel;
|
2020-06-01 23:17:29 -07:00
|
|
|
initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
|
2017-03-21 21:35:04 +00:00
|
|
|
Conf.SampleProfile = std::move(SampleProfile);
|
2020-01-09 20:58:31 -08:00
|
|
|
Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
|
|
|
|
// For historical reasons, loop interleaving is set to mirror setting for loop
|
|
|
|
// unrolling.
|
|
|
|
Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
|
|
|
|
Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
|
|
|
|
Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
|
[LPM] Port CGProfilePass from NPM to LPM
Reviewers: hans, chandlerc!, asbirlea, nikic
Reviewed By: hans, nikic
Subscribers: steven_wu, dexonsmith, nikic, echristo, void, zhizhouy, cfe-commits, aeubanks, MaskRay, jvesely, nhaehnle, hiraditya, kerbowa, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D83013
2020-07-08 12:30:28 -07:00
|
|
|
// Only enable CGProfilePass when using integrated assembler, since
|
|
|
|
// non-integrated assemblers don't recognize .cgprofile section.
|
|
|
|
Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
|
2019-03-04 20:21:31 +00:00
|
|
|
|
|
|
|
// Context sensitive profile.
|
|
|
|
if (CGOpts.hasProfileCSIRInstr()) {
|
|
|
|
Conf.RunCSIRInstr = true;
|
|
|
|
Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
|
|
|
|
} else if (CGOpts.hasProfileCSIRUse()) {
|
|
|
|
Conf.RunCSIRInstr = false;
|
|
|
|
Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
|
|
|
|
}
|
|
|
|
|
2018-10-10 23:13:35 +00:00
|
|
|
Conf.ProfileRemapping = std::move(ProfileRemapping);
|
2017-11-13 15:38:33 +00:00
|
|
|
Conf.DebugPassManager = CGOpts.DebugPassManager;
|
2023-06-21 14:13:05 -07:00
|
|
|
Conf.VerifyEach = CGOpts.VerifyEach;
|
2018-05-05 14:37:29 +00:00
|
|
|
Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
|
|
|
|
Conf.RemarksFilename = CGOpts.OptRecordFile;
|
2019-03-12 21:22:27 +00:00
|
|
|
Conf.RemarksPasses = CGOpts.OptRecordPasses;
|
2019-06-17 16:06:00 +00:00
|
|
|
Conf.RemarksFormat = CGOpts.OptRecordFormat;
|
2019-06-15 15:38:51 +00:00
|
|
|
Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
|
|
|
|
Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
|
2017-03-31 22:35:47 +00:00
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
2023-11-08 10:53:49 -08:00
|
|
|
Conf.PreCodeGenModuleHook = [](size_t Task, const llvm::Module &Mod) {
|
2017-03-31 22:35:47 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case Backend_EmitLL:
|
2023-11-08 10:53:49 -08:00
|
|
|
Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) {
|
2017-03-31 22:35:47 +00:00
|
|
|
M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case Backend_EmitBC:
|
2023-11-08 10:53:49 -08:00
|
|
|
Conf.PreCodeGenModuleHook = [&](size_t Task, const llvm::Module &Mod) {
|
2018-02-14 19:11:37 +00:00
|
|
|
WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
|
2017-03-31 22:35:47 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Conf.CGFileType = getCodeGenFileType(Action);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-14 10:45:00 -07:00
|
|
|
if (Error E =
|
|
|
|
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
|
|
|
|
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
|
2021-03-29 15:04:28 -07:00
|
|
|
/* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
|
2016-08-12 18:12:08 +00:00
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:53:49 -08:00
|
|
|
void clang::EmitBackendOutput(
|
|
|
|
DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderOpts,
|
|
|
|
const CodeGenOptions &CGOpts, const clang::TargetOptions &TOpts,
|
|
|
|
const LangOptions &LOpts, StringRef TDesc, llvm::Module *M,
|
|
|
|
BackendAction Action, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS, BackendConsumer *BC) {
|
2019-03-30 08:42:48 +00:00
|
|
|
|
2019-12-11 11:49:42 +00:00
|
|
|
llvm::TimeTraceScope TimeScope("Backend");
|
2019-03-30 08:42:48 +00:00
|
|
|
|
2018-02-16 23:38:22 +00:00
|
|
|
std::unique_ptr<llvm::Module> EmptyModule;
|
2016-08-12 18:12:08 +00:00
|
|
|
if (!CGOpts.ThinLTOIndexFile.empty()) {
|
2017-01-06 23:37:33 +00:00
|
|
|
// If we are performing a ThinLTO importing compile, load the function index
|
|
|
|
// into memory and pass it into runThinLTOBackend, which will run the
|
|
|
|
// function importer and invoke LTO passes.
|
2021-10-21 15:57:15 -07:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
|
|
|
|
if (Error E = llvm::getModuleSummaryIndexForFile(
|
|
|
|
CGOpts.ThinLTOIndexFile,
|
|
|
|
/*IgnoreEmptyThinLTOIndexFile*/ true)
|
|
|
|
.moveInto(CombinedIndex)) {
|
|
|
|
logAllUnhandledErrors(std::move(E), errs(),
|
2017-01-06 23:37:33 +00:00
|
|
|
"Error loading index file '" +
|
|
|
|
CGOpts.ThinLTOIndexFile + "': ");
|
|
|
|
return;
|
|
|
|
}
|
2021-10-21 15:57:15 -07:00
|
|
|
|
2017-01-06 23:37:33 +00:00
|
|
|
// A null CombinedIndex means we should skip ThinLTO compilation
|
|
|
|
// (LLVM will optionally ignore empty index files, returning null instead
|
|
|
|
// of an error).
|
2018-02-16 23:38:22 +00:00
|
|
|
if (CombinedIndex) {
|
|
|
|
if (!CombinedIndex->skipModuleByDistributedBackend()) {
|
2020-06-01 23:17:29 -07:00
|
|
|
runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
|
|
|
|
TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
|
2018-10-10 23:13:35 +00:00
|
|
|
CGOpts.ProfileRemappingFile, Action);
|
2018-02-16 23:38:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Distributed indexing detected that nothing from the module is needed
|
|
|
|
// for the final linking. So we can skip the compilation. We sill need to
|
|
|
|
// output an empty object file to make sure that a linker does not fail
|
|
|
|
// trying to read it. Also for some features, like CFI, we must skip
|
|
|
|
// the compilation as CombinedIndex does not contain all required
|
|
|
|
// information.
|
2019-08-14 23:04:18 +00:00
|
|
|
EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
|
2018-02-16 23:38:22 +00:00
|
|
|
EmptyModule->setTargetTriple(M->getTargetTriple());
|
|
|
|
M = EmptyModule.get();
|
2017-01-06 23:37:33 +00:00
|
|
|
}
|
2016-08-12 18:12:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 09:24:44 -08:00
|
|
|
EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M, VFS);
|
2023-11-08 10:53:49 -08:00
|
|
|
AsmHelper.EmitAssembly(Action, std::move(OS), BC);
|
2014-01-02 15:08:04 +00:00
|
|
|
|
2016-03-04 19:00:41 +00:00
|
|
|
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
|
|
|
|
// DataLayout.
|
|
|
|
if (AsmHelper.TM) {
|
2015-07-24 16:04:29 +00:00
|
|
|
std::string DLDesc = M->getDataLayout().getStringRepresentation();
|
2021-04-19 13:39:20 -04:00
|
|
|
if (DLDesc != TDesc) {
|
2014-01-02 15:08:04 +00:00
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error, "backend data layout '%0' does not match "
|
|
|
|
"expected target description '%1'");
|
2021-04-19 13:39:20 -04:00
|
|
|
Diags.Report(DiagID) << DLDesc << TDesc;
|
2014-01-02 15:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-07 23:20:08 +00:00
|
|
|
}
|
2016-05-11 16:26:03 +00:00
|
|
|
|
|
|
|
// With -fembed-bitcode, save a copy of the llvm IR as data in the
|
|
|
|
// __LLVM,__bitcode section.
|
|
|
|
void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
|
|
|
|
llvm::MemoryBufferRef Buf) {
|
|
|
|
if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
|
|
|
|
return;
|
2022-01-31 16:46:11 -08:00
|
|
|
llvm::embedBitcodeInModule(
|
[LTO] Support for embedding bitcode section during LTO
Summary:
This adds support for embedding bitcode in a binary during LTO. The libLTO gains supports the `-lto-embed-bitcode` flag. The option allows users of the LTO library to embed a bitcode section. For example, LLD can pass the option via `ld.lld -mllvm=-lto-embed-bitcode`.
This feature allows doing something comparable to `clang -c -fembed-bitcode`, but on the (LTO) linker level. Having bitcode alongside native code has many use-cases. To give an example, the MacOS linker can create a `-bitcode_bundle` section containing bitcode. Also, having this feature built into LLVM is an alternative to 3rd party tools such as [[ https://github.com/travitch/whole-program-llvm | wllvm ]] or [[ https://github.com/SRI-CSL/gllvm | gllvm ]]. As with these tools, this feature simplifies creating "whole-program" llvm bitcode files, but in contrast to wllvm/gllvm it does not rely on a specific llvm frontend/driver.
Patch by Josef Eisl <josef.eisl@oracle.com>
Reviewers: #llvm, #clang, rsmith, pcc, alexshap, tejohnson
Reviewed By: tejohnson
Subscribers: tejohnson, mehdi_amini, inglorion, hiraditya, aheejin, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits, #llvm, #clang
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D68213
2019-12-12 11:59:36 -08:00
|
|
|
*M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
|
|
|
|
CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
|
2020-10-28 12:08:26 -07:00
|
|
|
CGOpts.CmdArgs);
|
2016-05-11 16:26:03 +00:00
|
|
|
}
|
2021-12-03 15:48:36 -05:00
|
|
|
|
|
|
|
void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts,
|
|
|
|
DiagnosticsEngine &Diags) {
|
|
|
|
if (CGOpts.OffloadObjects.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (StringRef OffloadObject : CGOpts.OffloadObjects) {
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr =
|
2022-05-06 13:56:42 -04:00
|
|
|
llvm::MemoryBuffer::getFileOrSTDIN(OffloadObject);
|
2023-08-30 10:05:06 +09:00
|
|
|
if (ObjectOrErr.getError()) {
|
2021-12-03 15:48:36 -05:00
|
|
|
auto DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
|
|
|
|
"could not open '%0' for embedding");
|
2022-05-06 13:56:42 -04:00
|
|
|
Diags.Report(DiagID) << OffloadObject;
|
2021-12-03 15:48:36 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:56:42 -04:00
|
|
|
llvm::embedBufferInModule(*M, **ObjectOrErr, ".llvm.offloading",
|
2022-06-01 15:28:34 -04:00
|
|
|
Align(object::OffloadBinary::getAlignment()));
|
2021-12-03 15:48:36 -05:00
|
|
|
}
|
|
|
|
}
|