aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/ModuleUtils.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-06-05Add -funique-source-file-identifier option.Peter Collingbourne1-4/+6
This option complements -funique-source-file-names and allows the user to use a different unique identifier than the source file path. Reviewers: teresajohnson Reviewed By: teresajohnson Pull Request: https://github.com/llvm/llvm-project/pull/142901
2025-04-15Introduce -funique-source-file-names flag.Peter Collingbourne1-21/+19
The purpose of this flag is to allow the compiler to assume that each object file passed to the linker has been compiled using a unique source file name. This is useful for reducing link times when doing ThinLTO in combination with whole-program devirtualization or CFI, as it allows modules without exported symbols to be built with ThinLTO. Reviewers: vitalybuka, teresajohnson Reviewed By: teresajohnson Pull Request: https://github.com/llvm/llvm-project/pull/135728
2025-01-21[IR] Remove unused variables from #123617Mats Larsen1-1/+0
Failed to notice them when landing that patch - apologies!
2025-01-21[IR] Replace of PointerType::get(Type) with opaque version (NFC) (#123617)Mats Jun Larsen1-4/+4
In accordance with https://github.com/llvm/llvm-project/issues/123569 In order to keep the patch at reasonable size, this PR only covers for the llvm subproject, unittests excluded.
2024-08-20Fix KCFI types for generated functions with integer normalization (#104826)Sami Tolvanen1-5/+7
With -fsanitize-cfi-icall-experimental-normalize-integers, Clang appends ".normalized" to KCFI types in CodeGenModule::CreateKCFITypeId, which changes type hashes also for functions that don't have integer types in their signatures. However, llvm::setKCFIType does not take integer normalization into account, which means LLVM generated functions with KCFI types, e.g. sanitizer constructors, will fail KCFI checks when integer normalization is enabled in Clang. Add a cfi-normalize-integers module flag to indicate integer normalization is used, and append ".normalized" to KCFI types also in llvm::setKCFIType to fix the type mismatch.
2024-08-02[ModuleUtils] Add transformGlobal{C,D}tors (#101757)Vitaly Buka1-0/+44
For #101772
2024-05-15[IR] Move GlobalValue::getGUID() out of line (NFC)Nikita Popov1-0/+1
Avoid including MD5.h in a core IR header.
2024-01-17[VFABI] Move the Vector ABI demangling utility to LLVMCore. (#77513)Alexandros Lamprineas1-29/+0
This fixes #71892 allowing us to check magled names in the IR verifier.
2023-12-06[NFC] Replace CallInst with FunctionType in VFABI, VFShape API (#74569)Paschalis Mpeis1-1/+2
Minor simplification applied to VFShape::getScalarShape, VFShape::get, and VFABI::tryDemangleForVFABI methods. Also, remove unnecessary `static_cast` in `SLPVectorizer.cpp`
2023-11-23[SVE] Don't require lookup when demangling vector function mappings (#72260)Graham Hunter1-1/+1
We can determine the VF from a combination of the mangled name (which indicates the arguments that take vectors) and the element sizes of the arguments for the scalar function the mapping has been established for. The assert when demangling fails has been removed in favour of just not adding the mapping, which prevents the crash seen in https://github.com/llvm/llvm-project/issues/71892 This patch also stops using _LLVM_ as an ISA for scalable vector tests, since there aren't defined rules for the way vector arguments should be handled (e.g. packed vs. unpacked representation).
2023-11-22[IR] Replace uses of IRBuilder::getInt8PtrTy with getPtrTy. NFC (#73154)Craig Topper1-5/+5
2023-11-07[NFC] Remove Type::getInt8PtrTy (#71029)Paulo Matos1-1/+1
Replace this with PointerType::getUnqual(). Followup to the opaque pointer transition. Fixes an in-code TODO item.
2023-11-06[Transforms] Use StringRef::starts_with/ends_with instead of ↵Simon Pilgrim1-1/+1
startswith/endswith. NFC. startswith/endswith wrap starts_with/ends_with and will eventually go away (to more closely match string_view)
2023-07-14[llvm] Remove calls to supportsTypedPointers() (NFC)Nikita Popov1-6/+2
Always returns false now.
2023-07-08Add missing StringExtras.h includesElliot Goodrich1-0/+2
In preparation for removing the `#include "llvm/ADT/StringExtras.h"` from the header to source file of `llvm/Support/Error.h`, first add in all the missing includes that were previously included transitively through this header. This is fixing all files missed in b0abd4893fa1. Differential Revision: https://reviews.llvm.org/D154543
2023-03-21[ModuleUtils] Handle globals_ctors/dtors with non-literal type (PR56809)Nikita Popov1-4/+6
If the global already exists, use its existing type, so we don't try to mix literal and non-literal structs among the elements. Fixes https://github.com/llvm/llvm-project/issues/56809.
2023-01-24[SanitizerBinaryMetadata] Declare callbacks extern weakMarco Elver1-13/+40
Declare callbacks extern weak (if no existing declaration exists), and only call if the function address is non-null. This allows to attach semantic metadata to binaries where no user of that metadata exists, avoiding to have to link empty stub callbacks. Once the binary is linked (statically or dynamically) against a tool runtime that implements the callbacks, the respective callbacks will be called. This vastly simplifies gradual deployment of tools using the metadata, esp. avoiding having to recompile large codebases with different compiler flags (which negatively impacts compiler caches). Reviewed By: dvyukov, vitalybuka Differential Revision: https://reviews.llvm.org/D142408
2023-01-17Utils: Add utility pass to lower ifuncsMatt Arsenault1-0/+99
Create a global constructor which will initialize a global table of function pointers. For now, this is only used as a reduction technique for llvm-reduce. In the future this may be useful to support ifunc on systems where the program loader doesn't natively support it.
2023-01-11[ModuleUtils][KCFI] Set patchable-function-prefix for synthesized functionsSami Tolvanen1-0/+7
When -fpatchable-function-entry is used to emit prefix nops before functions, KCFI assumes all indirectly called functions have the same number of prefix nops, because the nops are emitted between the KCFI type hash and the function entry. However, as patchable-function-prefix is a function attribute set by Clang, functions later synthesized by LLVM don't inherit this attribute and end up not having prefix nops. One of these functions is asan.module_ctor, which the Linux kernel ends up calling indirectly when KASAN is enabled. In order to avoid tripping KCFI, save the expected prefix offset to a module flag, and use it when we're setting KCFI type for the relevant synthesized functions. Link: https://github.com/ClangBuiltLinux/linux/issues/1742 Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D141172
2023-01-11[GCOV] Set !kcfi_type metadata for indirectly called functionsSami Tolvanen1-1/+1
With CONFIG_GCOV_KERNEL, the Linux kernel indirectly calls the __llvm_gcov_* functions generated by LLVM. With -fsanitize=kcfi, these calls are made from instrumented code and fail indirect call checks as they don't have !kcfi_type metadata. Similarly to D138945, set type metadata for these functions to allow GCOV and KCFI to be both enabled. Link: https://github.com/ClangBuiltLinux/linux/issues/1778 Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D141444
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille1-2/+2
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-12-16[Transforms,InstCombine] std::optional::value => operator*/operator->Fangrui Song1-1/+1
value() has undesired exception checking semantics and calls __throw_bad_optional_access in libc++. Moreover, the API is unavailable without _LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).
2022-12-14llvm-reduce: Fix invalid reductions with llvm.usedMatt Arsenault1-20/+56
Fixes issue 59413.
2022-12-14[Analysis] llvm::Optional => std::optionalFangrui Song1-1/+1
2022-12-09[ModuleUtils][KCFI] Set !kcfi_type metadata for sanitizer constructorsSami Tolvanen1-0/+16
Set KCFI type metadata for the sanitizer constructors to prevent runtime failures when these functions are indirectly called in instrumented code. This fixes a compatibility issue with KASAN and -fsanitize=kcfi in the Linux kernel. Link: https://github.com/ClangBuiltLinux/linux/issues/1742 Reviewed By: nickdesaulniers, MaskRay Differential Revision: https://reviews.llvm.org/D138945
2022-11-29Utils: Fix appending to global_ctors with program address spacesMatt Arsenault1-2/+5
Also fix constructing sanitizer constructors in address space 0 so it's testable (this was also failing the verifier on the type of global_ctors).
2022-11-28Utils: Use StringRef and rename variable for clarityMatt Arsenault1-3/+3
2022-07-13[llvm] Use value instead of getValue (NFC)Kazu Hirata1-1/+1
2022-07-07[Metadata] Add 'exclude' metadata to add the exclude flags on globalsJoseph Huber1-0/+1
This patchs adds a new metadata kind `exclude` which implies that the global variable should be given the necessary flags during code generation to not be included in the final executable. This is done using the ``SHF_EXCLUDE`` flag on ELF for example. This should make it easier to specify this flag on a variable without needing to explicitly check the section name in the target backend. Depends on D129053 D129052 Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D129151
2022-07-07[Clang] Use metadata to make identifying embedded objects easierJoseph Huber1-0/+7
Currently we use the `embedBufferInModule` function to store binary strings containing device offloading data inside the host object to create a fatbinary. In the case of LTO, we need to extract this object from the LLVM-IR. This patch adds a metadata node for the embedded objects containing the embedded pointers and the sections they were stored at. This should create a cleaner interface for identifying these values. In the future it may be worthwhile to also encode an `ID` in the metadata corresponding to the object's special section type if relevant. This would allow us to extract the data from an object file and LLVM-IR using the same ID. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D129033
2022-06-25[llvm] Don't use Optional::hasValue (NFC)Kazu Hirata1-1/+1
This patch replaces Optional::hasValue with the implicit cast to bool in conditionals only.
2022-06-25Revert "Don't use Optional::hasValue (NFC)"Kazu Hirata1-2/+2
This reverts commit aa8feeefd3ac6c78ee8f67bf033976fc7d68bc6d.
2022-06-25Don't use Optional::hasValue (NFC)Kazu Hirata1-2/+2
2022-04-15[OpenMP] Use new offloading binary when embedding offloading imagesJoseph Huber1-5/+5
The previous patch introduced the offloading binary format so we can store some metada along with the binary image. This patch introduces using this inside the linker wrapper and Clang instead of the previous method that embedded the metadata in the section name. Differential Revision: https://reviews.llvm.org/D122683
2022-03-01Cleanup includes: TransformsUtilsserge-sans-paille1-1/+0
Estimation on the impact on preprocessor output: before: 1065307662 after: 1064800684 Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D120741
2022-02-24[OpenMP] Make section variable external to prevent collisionsJoseph Huber1-2/+3
Summary: We use a section to embed offloading code into the host for later linking. This is normally unique to the translation unit as it is thrown away during linking. However, if the user performs a relocatable link the sections will be merged and we won't be able to access the files stored inside. This patch changes the section variables to have external linkage and a name defined by the section name, so if two sections are combined during linking we get an error.
2022-02-03ModuleUtils - VFABI::setVectorVariantNames - use ArrayRef<> instead of const ↵Simon Pilgrim1-2/+2
SmallVector to pass argument
2022-01-31[ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtilsFangrui Song1-0/+13
D116542 adds EmbedBufferInModule which introduces a layer violation (https://llvm.org/docs/CodingStandards.html#library-layering). See 2d5f857a1eaf5f7a806d12953c79b96ed8952da8 for detail. EmbedBufferInModule does not use BitcodeWriter functionality and should be moved LLVMTransformsUtils. While here, change the function case to the prevailing convention. It seems that EmbedBufferInModule just follows the steps of EmbedBitcodeInModule. EmbedBitcodeInModule calls WriteBitcodeToFile but has IR update operations which ideally should be refactored to another library. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D118666
2022-01-07[ModuleUtils] Remove dead arg from filterDeadComdatFunctions() (NFC)Nikita Popov1-1/+1
The module argument is no longer used.
2022-01-06[IR] Track users of comdatsNikita Popov1-54/+18
Track all GlobalObjects that reference a given comdat, which allows determining whether a function in a comdat is dead without scanning the whole module. In particular, this makes filterDeadComdatFunctions() have complexity O(#DeadFunctions) rather than O(#SymbolsInModule), which addresses half of the compile-time issue exposed by D115545. Differential Revision: https://reviews.llvm.org/D115864
2021-09-09[IR, Transforms] Use arg_empty (NFC)Kazu Hirata1-1/+1
2021-08-17[NFC] More get/removeAttribute() cleanupArthur Eubanks1-2/+1
2021-08-17[NFC] Replace Function handling of attributes with less confusing callsArthur Eubanks1-1/+1
To avoid magic constants and confusing indexes.
2021-07-22[Transforms] Remove getOrCreateInitFunction (NFC)Kazu Hirata1-22/+0
The last use was removed on Jan 16, 2019 in commit 81101de5853b4ed64640220a086a67b16f36f153.
2021-07-21[sanitizer] Place module_ctor/module_dtor in llvm.usedFangrui Song1-0/+2
This removes an abuse of ELF linker behaviors while keeping Mach-O/COFF linker behaviors unchanged. ELF: when module_ctor is in a comdat, this patch removes reliance on a linker abuse (an SHT_INIT_ARRAY in a section group retains the whole group) by using SHF_GNU_RETAIN. No linker behavior difference when module_ctor is not in a comdat. Mach-O: module_ctor gets `N_NO_DEAD_STRIP`. No linker behavior difference because module_ctor is already referenced by a `S_MOD_INIT_FUNC_POINTERS` section (GC root). PE/COFF: no-op. SanitizerCoverage already appends module_ctor to `llvm.used`. Other sanitizers: llvm.used for local linkage is not implemented in `TargetLoweringObjectFileCOFF::emitLinkerDirectives` (once implemented or switched to a non-local linkage, COFF can use module_ctor in comdat (i.e. generalize ELF-specific rL301586)). There is no object file size difference. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D106246
2021-04-29[ASAN] NFC: Use addrspace cast for pointers in non-zero addrspaceReshabh Sharma1-1/+1
Pointers in non-zero address spaces need to be address space casted before appending to the used list. Reviewed by: vitalybuka Differential Revision: https://reviews.llvm.org/D101363
2021-04-21[IR][sanitizer] Set nounwind on module ctor/dtor, additionally set uwtable ↵Fangrui Song1-2/+3
if -fasynchronous-unwind-tables On ELF targets, if a function has uwtable or personality, or does not have nounwind (`needsUnwindTableEntry`), it marks that `.eh_frame` is needed in the module. Then, a function gets `.eh_frame` if `needsUnwindTableEntry` or `-g[123]` is specified. (i.e. If -g[123], every function gets `.eh_frame`. This behavior is strange but that is the status quo on GCC and Clang.) Let's take asan as an example. Other sanitizers are similar. `asan.module_[cd]tor` has no attribute. `needsUnwindTableEntry` returns true, so every function gets `.eh_frame` if `-g[123]` is specified. This is the root cause that `-fno-exceptions -fno-asynchronous-unwind-tables -g` produces .debug_frame while `-fno-exceptions -fno-asynchronous-unwind-tables -g -fsanitize=address` produces .eh_frame. This patch * sets the nounwind attribute on sanitizer module ctor/dtor. * let Clang emit a module flag metadata "uwtable" for -fasynchronous-unwind-tables. If "uwtable" is set, sanitizer module ctor/dtor additionally get the uwtable attribute. The "uwtable" mechanism is generic: synthesized functions not cloned/specialized from existing ones should consider `Function::createWithDefaultAttr` instead of `Function::create` if they want to get some default attributes which have more of module semantics. Other candidates: "frame-pointer" (https://github.com/ClangBuiltLinux/linux/issues/955 https://github.com/ClangBuiltLinux/linux/issues/1238), dso_local, etc. Differential Revision: https://reviews.llvm.org/D100251
2021-03-29Don't use $ as suffix for symbol names in ThinLTOBitcodeWriter and other placesHans Wennborg1-1/+1
Using $ breaks demangling of the symbols. For example, $ c++filt _Z3foov\$123 _Z3foov$123 This causes problems for developers who would like to see nice stack traces etc., but also for automatic crash tracking systems which try to organize crashes based on the stack traces. Instead, use the period as suffix separator, since Itanium demanglers normally ignore such suffixes: $ c++filt _Z3foov.123 foo() [clone .123] This is already done in some places; try to do it everywhere. Differential revision: https://reviews.llvm.org/D97484
2021-02-20[LTO] Fix cloning of llvm*.used when splitting moduleTeresa Johnson1-5/+7
Refines the fix in 3c4c205060c9398da705eb71b63ddd8a04999de9 to only put globals whose defs were cloned into the split regular LTO module on the cloned llvm*.used globals. This avoids an issue where one of the attached values was a local that was promoted in the original module after the module was cloned. We only need to have the values defined in the new module on those globals. Fixes PR49251. Differential Revision: https://reviews.llvm.org/D97013
2020-06-10[KernelAddressSanitizer] Make globals constructors compatible with kernel [v2]Marco Elver1-5/+11
[ v1 was reverted by c6ec352a6bde1995794c523adc2ebab802ccdf0a due to modpost failing; v2 fixes this. More info: https://github.com/ClangBuiltLinux/linux/issues/1045#issuecomment-640381783 ] This makes -fsanitize=kernel-address emit the correct globals constructors for the kernel. We had to do the following: * Disable generation of constructors that rely on linker features such as dead-global elimination. * Only instrument globals *not* in explicit sections. The kernel uses sections for special globals, which we should not touch. * Do not instrument globals that are prefixed with "__" nor that are aliased by a symbol that is prefixed with "__". For example, modpost relies on specially named aliases to find globals and checks their contents. Unfortunately modpost relies on size stored as ELF debug info and any padding of globals currently causes the debug info to cause size reported to be *with* redzone which throws modpost off. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=203493 Tested: * With 'clang/test/CodeGen/asan-globals.cpp'. * With test_kasan.ko, we can see: BUG: KASAN: global-out-of-bounds in kasan_global_oob+0xb3/0xba [test_kasan] * allyesconfig, allmodconfig (x86_64) Reviewed By: glider Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D81390