summaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
AgeCommit message (Collapse)Author
2021-12-03CodeGen: Strip exception specifications from function types in CFI type names.Peter Collingbourne
With C++17 the exception specification has been made part of the function type, and therefore part of mangled type names. However, it's valid to convert function pointers with an exception specification to function pointers with the same argument and return types but without an exception specification, which means that e.g. a function of type "void () noexcept" can be called through a pointer of type "void ()". We must therefore consider the two types to be compatible for CFI purposes. We can do this by stripping the exception specification before mangling the type name, which is what this patch does. Differential Revision: https://reviews.llvm.org/D115015
2021-12-01[clang][ARM] PACBTI-M frontend supportTies Stuij
Handle branch protection option on the commandline as well as a function attribute. One patch for both mechanisms, as they use the same underlying parsing mechanism. These are recorded in a set of LLVM IR module-level attributes like we do for AArch64 PAC/BTI (see https://reviews.llvm.org/D85649): - command-line options are "translated" to module-level LLVM IR attributes (metadata). - functions have PAC/BTI specific attributes iff the __attribute__((target("branch-protection=...))) was used in the function declaration. - command-line option -mbranch-protection to armclang targeting Arm, following this grammar: branch-protection ::= "-mbranch-protection=" <protection> protection ::= "none" | "standard" | "bti" [ "+" <pac-ret-clause> ] | <pac-ret-clause> [ "+" "bti"] pac-ret-clause ::= "pac-ret" [ "+" <pac-ret-option> ] pac-ret-option ::= "leaf" ["+" "b-key"] | "b-key" ["+" "leaf"] b-key is simply a placeholder to make it consistent with AArch64's version. In Arm, however, it triggers a warning informing that b-key is unsupported and a-key will be selected instead. - Handle _attribute_((target(("branch-protection=..."))) for AArch32 with the same grammer as the commandline options. This patch is part of a series that adds support for the PACBTI-M extension of the Armv8.1-M architecture, as detailed here: https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/armv8-1-m-pointer-authentication-and-branch-target-identification-extension The PACBTI-M specification can be found in the Armv8-M Architecture Reference Manual: https://developer.arm.com/documentation/ddi0553/latest The following people contributed to this patch: - Momchil Velikov - Victor Campos - Ties Stuij Reviewed By: vhscampos Differential Revision: https://reviews.llvm.org/D112421
2021-11-29Reapply 'Implement target_clones multiversioning'Erich Keane
See discussion in D51650, this change was a little aggressive in an error while doing a 'while we were here', so this removes that error condition, as it is apparently useful. This reverts commit bb4934601d731465e01e2e22c80ce2dbe687d73f.
2021-11-18[X86] Add -mskip-rax-setup support to align with GCCPhoebe Wang
AMD64 ABI mandates caller to specify the number of used SSE registers when passing variable arguments. GCC also provides option -mskip-rax-setup to skip the setup of rax when SSE is disabled. This helps to reduce the code size, see pr23258. Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D112413
2021-11-14[clang] Use isa instead of dyn_cast (NFC)Kazu Hirata
2021-11-12Revert "Implement target_clones multiversioning"Adrian Kuegel
This reverts commit 9deab60ae710f8c4cc810cd680edfb64c803f42d. There is a possibly unintended semantic change.
2021-11-11Implement target_clones multiversioningErich Keane
As discussed here: https://lwn.net/Articles/691932/ GCC6.0 adds target_clones multiversioning. This functionality is an odd cross between the cpu_dispatch and 'target' MV, but is compatible with neither. This attribute allows you to list all options, then emits a separately optimized version of each function per-option (similar to the cpu_specific attribute). It automatically generates a resolver, just like the other two. The mangling however, is... ODD to say the least. The mangling format is: <normal_mangling>.<option string>.<option ordinal>. Differential Revision:https://reviews.llvm.org/D51650
2021-11-10Emit hidden hostcall argument for sanitized kernelsYaxun (Sam) Liu
this patch - https://reviews.llvm.org/D110337 changes the way how hostcall hidden argument is emitted for printf, but the sanitized kernels also use hostcall buffer to report a error for invalid memory access, which is not handled by the above patch and it leads to vdi runtime error: Device::callbackQueue aborting with error : HSA_STATUS_ERROR_MEMORY_FAULT: Agent attempted to access an inaccessible address. code: 0x2b Patch by: Praveen Velliengiri Reviewed by: Yaxun Liu, Matt Arsenault Differential Revision: https://reviews.llvm.org/D112820
2021-11-10[CUDA][HIP] Allow comdat for kernelsYaxun (Sam) Liu
Two identical instantiations of a template function can be emitted by two TU's with linkonce_odr linkage without causing duplicate symbols in linker. MSVC also requires these symbols be in comdat sections. Linux does not require the symbols in comdat sections to be merged by linker but by default clang puts them in comdat sections. If a template kernel is instantiated identically in two TU's. MSVC requires that them to be in comdat sections, otherwise MSVC linker will diagnose them as duplicate symbols. However, currently clang does not put instantiated template kernels in comdat sections, which causes link error for MSVC. This patch allows putting instantiated template kernels into comdat sections. Reviewed by: Artem Belevich, Reid Kleckner Differential Revision: https://reviews.llvm.org/D112492
2021-11-09[clang] Run LLVM Verifier in modes without CodeGen tooItay Bookstein
Previously, the Backend_Emit{Nothing,BC,LL} modes did not run the LLVM verifier since it is usually added via the TargetMachine::addPassesToEmitFile method according to the DisableVerify parameter. This is called from EmitAssemblyHelper::AddEmitPasses, which is only relevant for BackendAction-s that require CodeGen. Note: * In these particular situations the verifier is added to the optimization pipeline rather than the codegen pipeline so that it runs prior to the BC/LL emission pass. * This change applies to both the old and the new PMs. * Because the clang tests use -emit-llvm ubiquitously, this change will enable the verifier for them. * A small bug is fixed in emitIFuncDefinition so that the clang/test/CodeGen/ifunc.c test would pass: the emitIFuncDefinition incorrectly passed the GlobalDecl of the IFunc itself to the call to GetOrCreateLLVMFunction for creating the resolver. Signed-off-by: Itay Bookstein <ibookstein@gmail.com> Reviewed By: rjmccall Differential Revision: https://reviews.llvm.org/D113352
2021-11-09[CodeGen] Diagnose and reject non-function ifunc resolversItay Bookstein
Signed-off-by: Itay Bookstein <ibookstein@gmail.com> Reviewed By: MaskRay, erichkeane Differential Revision: https://reviews.llvm.org/D112868
2021-11-09[clang][openmp][NFC] Remove arch-specific CGOpenMPRuntimeGPU filesAtmn Patel
The existing CGOpenMPRuntimeAMDGCN and CGOpenMPRuntimeNVPTX classes are just code bloat. By removing them, the codebase gets a bit cleaner. Reviewed By: jdoerfert, JonChesterfield, tianshilei1992 Differential Revision: https://reviews.llvm.org/D113421
2021-11-09Revert "[clang][openmp][NFC] Remove arch-specific CGOpenMPRuntimeGPU files"Atmn Patel
This reverts commit 81a7cad2ffc18f15b732f69d991c8398c979c5ca.
2021-11-09[clang][openmp][NFC] Remove arch-specific CGOpenMPRuntimeGPU filesAtmn Patel
The existing CGOpenMPRuntimeAMDGCN and CGOpenMPRuntimeNVPTX classes are just code bloat. By removing them, the codebase gets a bit cleaner. Reviewed By: jdoerfert, JonChesterfield, tianshilei1992 Differential Revision: https://reviews.llvm.org/D113421
2021-11-07[clang] Use llvm::reverse. NFCI.Benjamin Kramer
2021-10-31[Verifier] Add verification logic for GlobalIFuncsItay Bookstein
Verify that the resolver exists, that it is a defined Function, and that its return type matches the ifunc's type. Add corresponding check to BitcodeReader, change clang to emit the correct type, and fix tests to comply. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D112349
2021-10-21Revert "AddGlobalAnnotations for function with or without function body."Aaron Ballman
This reverts commit 121b2252de0eed68f2ddf5f09e924a6c35423d47. The following code causes a crash in some circumstances: struct k { ~k() __attribute__((annotate(""))) {} }; void m() { k(); }
2021-10-20[IR] Refactor GlobalIFunc to inherit from GlobalObject, Remove ↵Itay Bookstein
GlobalIndirectSymbol As discussed in: * https://reviews.llvm.org/D94166 * https://lists.llvm.org/pipermail/llvm-dev/2020-September/145031.html The GlobalIndirectSymbol class lost most of its meaning in https://reviews.llvm.org/D109792, which disambiguated getBaseObject (now getAliaseeObject) between GlobalIFunc and everything else. In addition, as long as GlobalIFunc is not a GlobalObject and getAliaseeObject returns GlobalObjects, a GlobalAlias whose aliasee is a GlobalIFunc cannot currently be modeled properly. Creating aliases for GlobalIFuncs does happen in the wild (e.g. glibc). In addition, calling getAliaseeObject on a GlobalIFunc will currently return nullptr, which is undesirable because it should return the object itself for non-aliases. This patch refactors the GlobalIFunc class to inherit directly from GlobalObject, and removes GlobalIndirectSymbol (while inlining the relevant parts into GlobalAlias and GlobalIFunc). This allows for calling getAliaseeObject() on a GlobalIFunc to return the GlobalIFunc itself, making getAliaseeObject() more consistent and enabling alias-to-ifunc to be properly modeled in the IR. I exercised some judgement in the API clients of GlobalIndirectSymbol: some were 'monomorphized' for GlobalAlias and GlobalIFunc, and some remained shared (with the type adapted to become GlobalValue). Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D108872
2021-10-17[clang] Use llvm::erase_if (NFC)Kazu Hirata
2021-10-11AddGlobalAnnotations for function with or without function body.Chris Bieneman
When AnnotateAttr is on a function, AddGlobalAnnotations is only called in CodeGenModule::EmitGlobalFunctionDefinition which means AnnotateAttr on function declaration without function body will be ignored. The patch will move AddGlobalAnnotations to CodeGenModule::SetFunctionAttributes, so with or without function body, the AnnotateAttr will get code gen for a function. It'll help case when AnnotateAttr is on external function, and the AnnotateAttr will be consumed in IR level. For example, a pass to collect num of uses for functions with __attribute((annotate("count_use"))) after optimizations, As long as there's __attribute((annotate("count_use"))), function with or without function body should be counted. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D111109 Patch by: python3kgae (Xiang Li)
2021-10-06Reland [IR] Increase max alignment to 4GBArthur Eubanks
Currently the max alignment representable is 1GB, see D108661. Setting the align of an object to 4GB is desirable in some cases to make sure the lower 32 bits are clear which can be used for some optimizations, e.g. https://crbug.com/1016945. This uses an extra bit in instructions that carry an alignment. We can store 15 bits of "free" information, and with this change some instructions (e.g. AtomicCmpXchgInst) use 14 bits. We can increase the max alignment representable above 4GB (up to 2^62) since we're only using 33 of the 64 values, but I've just limited it to 4GB for now. The one place we have to update the bitcode format is for the alloca instruction. It stores its alignment into 5 bits of a 32 bit bitfield. I've added another field which is 8 bits and should be future proof for a while. For backward compatibility, we check if the old field has a value and use that, otherwise use the new field. Updating clang's max allowed alignment will come in a future patch. Reviewed By: hans Differential Revision: https://reviews.llvm.org/D110451
2021-10-06Revert "Reland [IR] Increase max alignment to 4GB"Arthur Eubanks
This reverts commit 8d64314ffea55f2ad94c1b489586daa8ce30f451.
2021-10-06Reland [IR] Increase max alignment to 4GBArthur Eubanks
Currently the max alignment representable is 1GB, see D108661. Setting the align of an object to 4GB is desirable in some cases to make sure the lower 32 bits are clear which can be used for some optimizations, e.g. https://crbug.com/1016945. This uses an extra bit in instructions that carry an alignment. We can store 15 bits of "free" information, and with this change some instructions (e.g. AtomicCmpXchgInst) use 14 bits. We can increase the max alignment representable above 4GB (up to 2^62) since we're only using 33 of the 64 values, but I've just limited it to 4GB for now. The one place we have to update the bitcode format is for the alloca instruction. It stores its alignment into 5 bits of a 32 bit bitfield. I've added another field which is 8 bits and should be future proof for a while. For backward compatibility, we check if the old field has a value and use that, otherwise use the new field. Updating clang's max allowed alignment will come in a future patch. Reviewed By: hans Differential Revision: https://reviews.llvm.org/D110451
2021-10-06Revert "[IR] Increase max alignment to 4GB"Arthur Eubanks
This reverts commit df84c1fe78130a86445d57563dea742e1b85156a. Breaks some bots
2021-10-06[IR] Increase max alignment to 4GBArthur Eubanks
Currently the max alignment representable is 1GB, see D108661. Setting the align of an object to 4GB is desirable in some cases to make sure the lower 32 bits are clear which can be used for some optimizations, e.g. https://crbug.com/1016945. This uses an extra bit in instructions that carry an alignment. We can store 15 bits of "free" information, and with this change some instructions (e.g. AtomicCmpXchgInst) use 14 bits. We can increase the max alignment representable above 4GB (up to 2^62) since we're only using 33 of the 64 values, but I've just limited it to 4GB for now. The one place we have to update the bitcode format is for the alloca instruction. It stores its alignment into 5 bits of a 32 bit bitfield. I've added another field which is 8 bits and should be future proof for a while. For backward compatibility, we check if the old field has a value and use that, otherwise use the new field. Updating clang's max allowed alignment will come in a future patch. Reviewed By: hans Differential Revision: https://reviews.llvm.org/D110451
2021-09-28Reland [clang] Rework dontcall attributesArthur Eubanks
To avoid using the AST when emitting diagnostics, split the "dontcall" attribute into "dontcall-warn" and "dontcall-error", and also add the frontend attribute value as the LLVM attribute value. This gives us all the information to report diagnostics we need from within the IR (aside from access to the original source). One downside is we directly use LLVM's demangler rather than using the existing Clang diagnostic pretty printing of symbols. Previous revisions didn't properly declare the new dependencies. Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D110364
2021-09-28Revert "[clang] Rework dontcall attributes"Arthur Eubanks
This reverts commit 2943071e2ee0c7f31f34062a44d12aeb0e3a66fd. Breaks bots
2021-09-28[clang] Rework dontcall attributesArthur Eubanks
To avoid using the AST when emitting diagnostics, split the "dontcall" attribute into "dontcall-warn" and "dontcall-error", and also add the frontend attribute value as the LLVM attribute value. This gives us all the information to report diagnostics we need from within the IR (aside from access to the original source). One downside is we directly use LLVM's demangler rather than using the existing Clang diagnostic pretty printing of symbols. Reviewed By: nickdesaulniers Differential Revision: https://reviews.llvm.org/D110364
2021-09-28Simplify handling of builtin with inline redefinitionserge-sans-paille
(This is a recommit of 3d6f49a56995b845 that should no longer break validation since bd379915de38a9af3d65e1). It is a common practice in glibc header to provide an inline redefinition of an existing function. It is especially the case for fortified function. Clang currently has an imperfect approach to the problem, using a combination of trivially recursive function detection and noinline attribute. Simplify the logic by suffixing these functions by `.inline` during codegen, so that they are not recognized as builtin by llvm. After that patch, clang passes all tests from https://github.com/serge-sans-paille/fortify-test-suite Differential Revision: https://reviews.llvm.org/D109967
2021-09-28Revert "Simplify handling of builtin with inline redefinition"Kevin Athey
This reverts commit 3d6f49a56995b845c40be5827ded5d1e3f692cec. Broke bot: https://lab.llvm.org/buildbot/#/builders/5/builds/12360
2021-09-28Simplify handling of builtin with inline redefinitionserge-sans-paille
It is a common practice in glibc header to provide an inline redefinition of an existing function. It is especially the case for fortified function. Clang currently has an imperfect approach to the problem, using a combination of trivially recursive function detection and noinline attribute. Simplify the logic by suffixing these functions by `.inline` during codegen, so that they are not recognized as builtin by llvm. After that patch, clang passes all tests from https://github.com/serge-sans-paille/fortify-test-suite Differential Revision: https://reviews.llvm.org/D109967
2021-09-17Make multiversioning work with internal linkageErich Keane
We previously made all multiversioning resolvers/ifuncs have weak ODR linkage in IR, since we NEED to emit the whole resolver every time we see a call, but it is not necessarily the place where all the definitions live. HOWEVER, when doing so, we neglected the case where the versions have internal linkage. This patch ensures we do this, so you don't get weird behavior with static functions.
2021-08-31[OpenCL] Defines helper function for kernel language compatible OpenCL versionJustas Janickas
This change defines a helper function getOpenCLCompatibleVersion() inside LangOptions class. The function contains mapping between C++ for OpenCL versions and their corresponding compatible OpenCL versions. This mapping function should be updated each time a new C++ for OpenCL language version is introduced. The helper function is expected to simplify conditions on OpenCL C and C++ for OpenCL versions inside compiler code. Code refactoring performed. Differential Revision: https://reviews.llvm.org/D108693
2021-08-30[NFC][clang] Move IR-independent parts of target MV support to ↵Andrei Elovikov
X86TargetParser.cpp ...that is located under llvm/lib/Support/. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D108423
2021-08-26Fix __attribute__((annotate("")) with non-zero globals ASAlex Richardson
The existing code attempting to bitcast from a value in the default globals AS to i8 addrspace(0)* was triggering an assertion failure in our downstream fork. I found this while compiling poppler for CHERI-RISC-V (we use AS200 for all globals). The test case uses AMDGPU since that is one of the in-tree targets with a non-zero default globals address space. The new test previously triggered a "Invalid constantexpr bitcast!" assertion and now correctly generates code with addrspace(1) pointers. Reviewed By: rjmccall Differential Revision: https://reviews.llvm.org/D105972
2021-08-25[Clang] add support for error+warning fn attrsNick Desaulniers
Add support for the GNU C style __attribute__((error(""))) and __attribute__((warning(""))). These attributes are meant to be put on declarations of functions whom should not be called. They are frequently used to provide compile time diagnostics similar to _Static_assert, but which may rely on non-ICE conditions (ie. relying on compiler optimizations). This is also similar to diagnose_if function attribute, but can diagnose after optimizations have been run. While users may instead simply call undefined functions in such cases to get a linkage failure from the linker, these provide a much more ergonomic and actionable diagnostic to users and do so at compile time rather than at link time. Users instead may be able use inline asm .err directives. These are used throughout the Linux kernel in its implementation of BUILD_BUG and BUILD_BUG_ON macros. These macros generally cannot be converted to use _Static_assert because many of the parameters are not ICEs. The Linux kernel still needs to be modified to make use of these when building with Clang; I have a patch that does so I will send once this feature is landed. To do so, we create a new IR level Function attribute, "dontcall" (both error and warning boil down to one IR Fn Attr). Then, similar to calls to inline asm, we attach a !srcloc Metadata node to call sites of such attributed callees. The backend diagnoses these during instruction selection, while we still know that a call is a call (vs say a JMP that's a tail call) in an arch agnostic manner. The frontend then reconstructs the SourceLocation from that Metadata, and determines whether to emit an error or warning based on the callee's attribute. Link: https://bugs.llvm.org/show_bug.cgi?id=16428 Link: https://github.com/ClangBuiltLinux/linux/issues/1173 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D106030
2021-08-25[CUDA] Fix static device variables with -fgpu-rdcJonas Hahnfeld
NVPTX does not allow dots in the identifier, so ptxas errors out with fatal : Parsing error near '.static': syntax error because it parses .static as a directive. Avoid this problem by using two underscores, similar to what OpenMP does for outlined functions. Differential Revision: https://reviews.llvm.org/D108456
2021-08-23[clang][NFC] Tighten up code for GetGlobalVarAddressSpaceAndy Wingo
The LangAS local is only used in the OpenCL case; move its decl inwards. Differential Revision: https://reviews.llvm.org/D108449
2021-08-23[clang][NFC] GetOrCreateLLVMGlobal takes LangASAndy Wingo
Pass a LangAS instead of a target address space to GetOrCreateLLVMGlobal, to remove a place where the frontend assumes that target address space 0 is special. Differential Revision: https://reviews.llvm.org/D108445
2021-08-17[NFC] Cleanup more AttributeList::addAttribute()Arthur Eubanks
2021-08-17[NFC] Migrate some callers away from Function/AttributeLists methods that ↵Arthur Eubanks
take an index These methods can be confusing.
2021-08-17[NFC] Replace Function handling of attributes with less confusing callsArthur Eubanks
To avoid magic constants and confusing indexes.
2021-08-13[NFC] Cleanup callers of AttributeList::hasAttributes()Arthur Eubanks
AttributeList::hasAttributes() is confusing, use clearer methods like hasFnAttrs().
2021-08-13[NFC] Rename AttributeList::getParam/Ret/FnAttributes() -> get*Attributes()Arthur Eubanks
This is more consistent with similar methods.
2021-08-09[cuda] Mark builtin texture/surface reference variable as ↵Michael Liao
'externally_initialized'. - They need to be preserved even if there's no reference within the device code as the host code may need to initialize them based on the application logic. Reviewed By: tra Differential Revision: https://reviews.llvm.org/D107718
2021-08-04Apply -fmacro-prefix-map to __builtin_FILE()Pavel Asyutchenko
This matches the behavior of GCC. Patch does not change remapping logic itself, so adding one simple smoke test should be enough. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D107393
2021-07-23[HIP] Preserve ASAN bitcode library functionsYaxun (Sam) Liu
Address sanitizer passes may generate call of ASAN bitcode library functions after bitcode linking in lld, therefore lld cannot add those symbols since it does not know they will be used later. To solve this issue, clang emits a reference to a bicode library function which calls all ASAN functions which need to be preserved. This basically force all ASAN functions to be linked in. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D106315
2021-06-25[OpenMP] Add Module metadata for OpenMP compilationJoseph Huber
This patch adds a module level metadata flag indicating that the module was compiled with the `-fopenmp` flag. This will make it easier for passes like OpenMPOpt to determine if it should be run. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D102361
2021-06-21[IR] convert warn-stack-size from module flag to fn attrNick Desaulniers
Otherwise, this causes issues when building with LTO for object files that use different values. Link: https://github.com/ClangBuiltLinux/linux/issues/1395 Reviewed By: dblaikie, MaskRay Differential Revision: https://reviews.llvm.org/D104342
2021-06-11[ADT] Remove APInt/APSInt toString() std::string variantsSimon Pilgrim
<string> is currently the highest impact header in a clang+llvm build: https://commondatastorage.googleapis.com/chromium-browser-clang/llvm-include-analysis.html One of the most common places this is being included is the APInt.h header, which needs it for an old toString() implementation that returns std::string - an inefficient method compared to the SmallString versions that it actually wraps. This patch replaces these APInt/APSInt methods with a pair of llvm::toString() helpers inside StringExtras.h, adjusts users accordingly and removes the <string> from APInt.h - I was hoping that more of these users could be converted to use the SmallString methods, but it appears that most end up creating a std::string anyhow. I avoided trying to use the raw_ostream << operators as well as I didn't want to lose having the integer radix explicit in the code. Differential Revision: https://reviews.llvm.org/D103888