summaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGExpr.cpp
AgeCommit message (Collapse)Author
2025-06-19[CodeGen] Use range-based for loops (NFC) (#144939)Kazu Hirata
2025-06-06[ubsan] Add more -fsanitize-annotate-debug-info checks (#141997)Thurston Dang
This extends https://github.com/llvm/llvm-project/pull/138577 to more UBSan checks, by changing SanitizerDebugLocation (formerly SanitizerScope) to add annotations if enabled for the specified ordinals. Annotations will use the ordinal name if there is exactly one ordinal specified in the SanitizerDebugLocation; otherwise, it will use the handler name. Updates the tests from https://github.com/llvm/llvm-project/pull/141814. --------- Co-authored-by: Vitaly Buka <vitalybuka@google.com>
2025-06-05[clang] Simplify device kernel attributes (#137882)Nick Sarnie
We have multiple different attributes in clang representing device kernels for specific targets/languages. Refactor them into one attribute with different spellings to make it more easily scalable for new languages/targets. --------- Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
2025-06-05[CodeGen] Add TBAA struct path info for array members (#137719)Bruno De Fraine
This enables the LLVM optimizer to view accesses to distinct struct members as independent, also for array members. For example, the following two stores no longer alias: struct S { int a[10]; int b; }; void test(S *p, int i) { p->a[i] = ...; p->b = ...; } Array members were already added to TBAA struct type nodes in commit 57493e29. Here, we extend a path tag for an array subscript expression.
2025-06-04[clang] Function type attribute to prevent CFI instrumentation (#135836)PiJoules
This introduces the attribute discussed in https://discourse.llvm.org/t/rfc-function-type-attribute-to-prevent-cfi-instrumentation/85458. The proposed name has been changed from `no_cfi` to `cfi_unchecked_callee` to help differentiate from `no_sanitize("cfi")` more easily. The proposed attribute has the following semantics: 1. Indirect calls to a function type with this attribute will not be instrumented with CFI. That is, the indirect call will not be checked. Note that this only changes the behavior for indirect calls on pointers to function types having this attribute. It does not prevent all indirect function calls for a given type from being checked. 2. All direct references to a function whose type has this attribute will always reference the true function definition rather than an entry in the CFI jump table. 3. When a pointer to a function with this attribute is implicitly cast to a pointer to a function without this attribute, the compiler will give a warning saying this attribute is discarded. This warning can be silenced with an explicit C-style cast or C++ static_cast.
2025-06-02[CodeGen] Move CodeGenPGO behind unique_ptr (NFC) (#142155)Nikita Popov
The InstrProf headers are very expensive. Avoid including them in all of CodeGen/ by moving the CodeGenPGO member behind a unqiue_ptr. This reduces clang build time by 0.8%.
2025-05-29[NFCI][ubsan] Add/update deprecation TODOsThurston Dang
Mention -fsanitize-skip-hot-cutoffs and -fsanitize-annotate-debug-info are available as replacements
2025-05-27[KeyInstr][Clang] Assign matrix element atom (#134650)Orlando Cazalet-Hyams
This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668 The feature is only functional in LLVM if LLVM is built with CMake flag LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
2025-05-27[KeyInstr][Clang] Assign vector element atom (#134649)Orlando Cazalet-Hyams
This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668 The feature is only functional in LLVM if LLVM is built with CMake flag LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
2025-05-27[KeyInstr][Clang] Bitfield atom (#134648)Orlando Cazalet-Hyams
This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668 The feature is only functional in LLVM if LLVM is built with CMake flag LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
2025-05-26[Clang][CodeGen] Add metadata for load from reference (#98746)Yingwei Zheng
This patch adds `!nonnull/!align` for load from reference to improve codegen.
2025-05-23[KeyInstr][Clang] Assignment atom group (#134637)Orlando Cazalet-Hyams
This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668 The feature is only functional in LLVM if LLVM is built with CMake flag LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
2025-05-22[KeyInstr][Clang] Scalar init atom (#134633)Orlando Cazalet-Hyams
This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668 The feature is only functional in LLVM if LLVM is built with CMake flag LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.
2025-05-20[HLSL] Move where ZExt happens in 'EmitStoreThroughExtVectorComponentLValue' ↵Sarah Spall
to handle bug with hlsl boolean vector swizzles (#140627) In 'EmitStoreThroughExtVectorComponentLValue', move the code which ZExts in the case the Destination Scalar Type is larger than the Source Scalar Type, to the top of the function, to ensure each condition is handled. The previous code missed this case: ``` bool4 b = true.xxxx; b.xyz = false.xxx; ``` Leading to a bad shuffle vector. Closes #140564
2025-05-19[cfi] Enable -fsanitize-annotate-debug-info functionality for CFI checks ↵Thurston Dang
(#139809) This connects the -fsanitize-annotate-debug-info plumbing (https://github.com/llvm/llvm-project/pull/138577) to CFI check codegen, using SanitizerAnnotateDebugInfo() (https://github.com/llvm/llvm-project/pull/139965) and SanitizerInfoFromCFIKind (https://github.com/llvm/llvm-project/pull/140117). Note: SanitizerAnnotateDebugInfo() is updated to a public function because it is needed in ItaniumCXXABI. Updates the tests from https://github.com/llvm/llvm-project/pull/139149.
2025-05-15[sanitizer][NFCI] Add 'SanitizerAnnotateDebugInfo' (#139965)Thurston Dang
This generalizes the debug info annotation code from https://github.com/llvm/llvm-project/pull/139149 and moves it into a helper function, SanitizerAnnotateDebugInfo(). Future work can use 'ApplyDebugLocation ApplyTrapDI(*this, SanitizerAnnotateDebugInfo(Ordinal));' to add annotations to additional checks.
2025-05-13[Clang][counted_by] Add support for 'counted_by' on struct pointers (#137250)Bill Wendling
The 'counted_by' attribute is now available for pointers in structs. It generates code for sanity checks as well as __builtin_dynamic_object_size() calculations. For example: struct annotated_ptr { int count; char *buf __attribute__((counted_by(count))); }; If the pointer's type is 'void *', use the 'sized_by' attribute, which works similarly to 'counted_by', but can handle the 'void' base type: struct annotated_ptr { int count; void *buf __attribute__((sized_by(count))); }; If the 'count' field member occurs after the pointer, use the '-fexperimental-late-parse-attributes' flag during compilation. Note that 'counted_by' cannot be applied to a pointer to an incomplete type, because the size isn't known. struct foo; struct annotated_ptr { int count; struct foo *buf __attribute__((counted_by(count))); /* invalid */ }; Signed-off-by: Bill Wendling <morbo@google.com>
2025-05-11[clang] Use StringRef::consume_front (NFC) (#139472)Kazu Hirata
2025-05-09clang: Remove dest LangAS argument from performAddrSpaceCast (#138866)Matt Arsenault
It isn't used and is redundant with the result pointer type argument. A more reasonable API would only have LangAS parameters, or IR parameters, not both. Not all values have a meaningful value for this. I'm also not sure why we have this at all, it's not overridden by any targets and further simplification is possible.
2025-05-08clang: Fix broken implicit cast to generic address space (#138863)Matt Arsenault
This fixes emitting undefined behavior where a 64-bit generic pointer is written to a 32-bit slot allocated for a private pointer. This can be seen in test/CodeGenOpenCL/amdgcn-automatic-variable.cl's wrong_pointer_alloca.
2025-05-07[sanitizer] Add plumbing for -fsanitize-annotate-debug-info and partly ↵Thurston Dang
replace '-mllvm -array-bounds-pseudofn' (#138577) @fmayer introduced '-mllvm -array-bounds-pseudofn' (https://github.com/llvm/llvm-project/pull/128977/) to make it easier to see why crashes occurred, and to estimate with a profiler the cycles spent on these array-bounds checks. This functionality could be usefully generalized to other checks in future work. This patch adds the plumbing for -fsanitize-annotate-debug-info, and connects it to the existing array-bounds-pseudo-fn functionality i.e., -fsanitize-annotate-debug-info=array-bounds can be used as a replacement for '-mllvm -array-bounds-pseudofn', though we do not yet delete the latter. Note: we replaced '-mllvm -array-bounds-pseudofn' in clang/test/CodeGen/bounds-checking-debuginfo.c, because adding test cases would modify the line numbers in the test assertions, and therefore obscure that the test output is the same between '-mllvm -array-bounds-pseudofn' and -fsanitize-annotate-debug-info=array-bounds.
2025-05-04[clang] Remove unused local variables (NFC) (#138453)Kazu Hirata
2025-04-23[cfi] Fix one -fno-sanitize-merge case, and add two TODOs (#135438)Thurston Dang
-fno-sanitize-merge (introduced in https://github.com/llvm/llvm-project/pull/120464) nearly works for CFI: code that calls EmitCheck will already check the merge options. This patch fixes one EmitTrapCheck call, which did not check the merge options, and for two other EmitTrapChecks, adds two TODOs that explain why it is difficult to fix them.
2025-04-22[clang] Rework `hasBooleanRepresentation`. (#136038)Michele Scandale
This is a follow-up of 13aac46332f607a38067b5ddd466071683b8c255. This commit adjusts the implementation of `hasBooleanRepresentation` to be somewhat aligned to `hasIntegerRepresentation`. In particular vector of booleans should be handled in `hasBooleanRepresentation`, while `_Atomic(bool)` should not.
2025-04-18Revert "[Reland][Clang][CodeGen][UBSan] Add more precise attributes to ↵Vitaly Buka
recoverable ubsan handlers" (#136402) Reverts llvm/llvm-project#135135 Breaks several bots, details in #135135.
2025-04-17[Reland][Clang][CodeGen][UBSan] Add more precise attributes to recoverable ↵Yingwei Zheng
ubsan handlers (#135135) This patch relands https://github.com/llvm/llvm-project/pull/130990. If the check value is passed by reference, add `memory(read)`. Original PR description: This patch adds `memory(argmem: read, inaccessiblemem: readwrite)` to **recoverable** ubsan handlers in order to unblock some memory/loop optimizations. It provides an average of 3% performance improvement on llvm-test-suite (except for 49 test failures due to ubsan diagnostics).
2025-04-15[PAC] Add support for __ptrauth type qualifier (#100830)Akira Hatanaka
The qualifier allows programmer to directly control how pointers are signed when they are stored in a particular variable. The qualifier takes three arguments: the signing key, a flag specifying whether address discrimination should be used, and a non-negative integer that is used for additional discrimination. ``` typedef void (*my_callback)(const void*); my_callback __ptrauth(ptrauth_key_process_dependent_code, 1, 0xe27a) callback; ``` Co-Authored-By: John McCall rjmccall@apple.com
2025-04-14[clang][CodeGen] Add range metadata for atomic load of boolean type. #131476 ↵Jan Górski
(#133546) Fixes #131476. For `x86_64` it folds ``` movzbl t1(%rip), %eax andb $1, %al ``` into ``` movzbl t1(%rip), %eax ``` when run: `clang -S atomic-ops-load.c -o atomic-ops-load.s -O1 --target=x86_64`. But for riscv replaces: ``` lb a0, %lo(t1)(a0) andi a0, a0, 1 ``` with ``` lb a0, %lo(t1)(a0) zext.b a0, a0 ``` when run: `clang -S atomic-ops-load.c -o atomic-ops-load.s -O1 --target=riscv64`.
2025-04-12[Clang][CodeGen][UBSan] Remove redundant `EmitCheckValue` calls. NFCI (#135141)Yingwei Zheng
`EmitCheckValue` is called inside `EmitCheck`: https://github.com/llvm/llvm-project/blob/b122956390a6877536927c2b073a0b99f8b9704f/clang/lib/CodeGen/CGExpr.cpp#L3739 The outside calls are redundant because `EmitCheckValue(EmitCheckValue(V))` always returns `EmitCheckValue(V)`. Required by https://github.com/llvm/llvm-project/pull/135135.
2025-04-11[Clang][CodeGen] Do not set inbounds flag in `EmitMemberDataPointerAddress` ↵Yingwei Zheng
when the base pointer is null (#130952) See also https://github.com/llvm/llvm-project/pull/130734 for the original motivation. This pattern (`container_of`) is also widely used by real-world programs. Examples: https://github.com/llvm/llvm-project/blob/1d89d7d5d76e391b035f50343e2a4890506c6f2b/llvm/include/llvm/IR/SymbolTableListTraits.h#L77-L87 https://github.com/nodejs/node/blob/a2a53cb728ec5f776ac16879ce0f480a8e838320/src/util-inl.h#L134-L137 https://github.com/search?q=*%29nullptr-%3E*&type=code
2025-04-11[Clang][CodeGen] Do not set inbounds flag for struct GEP with null base ↵Yingwei Zheng
pointers (#130734) In the LLVM middle-end we want to fold `gep inbounds null, idx -> null`: https://alive2.llvm.org/ce/z/5ZkPx- This pattern is common in real-world programs (https://github.com/dtcxzyw/llvm-opt-benchmark/pull/55#issuecomment-1870963906). Generally, it exists in some (actually) unreachable blocks, which is introduced by JumpThreading. However, some old-style offsetof macros are still widely used in real-world C/C++ code (e.g., hwloc/slurm/luajit). To avoid breaking existing code and inconvenience to downstream users, this patch removes the inbounds flag from the struct gep if the base pointer is null.
2025-04-10[C11] Implement WG14 N1285 (temporary lifetimes) (#133472)Aaron Ballman
This feature largely models the same behavior as in C++11. It is technically a breaking change between C99 and C11, so the paper is not being backported to older language modes. One difference between C++ and C is that things which are rvalues in C are often lvalues in C++ (such as the result of a ternary operator or a comma operator). Fixes #96486
2025-04-10Revert "[Clang][CodeGen][UBSan] Add more precise attributes to recoverable ↵Yingwei Zheng
ubsan handlers" (#135130) Reverts llvm/llvm-project#130990 Breaks buildbot https://lab.llvm.org/buildbot/#/builders/186/builds/8072
2025-04-10[Clang][CodeGen][UBSan] Add more precise attributes to recoverable ubsan ↵Yingwei Zheng
handlers (#130990) This patch adds `memory(argmem: read, inaccessiblemem: readwrite) mustprogress` to **recoverable** ubsan handlers in order to unblock some memory/loop optimizations. It provides an average of 3% performance improvement on llvm-test-suite (except for 49 test failures due to ubsan diagnostics). Closes https://github.com/llvm/llvm-project/issues/130093.
2025-04-10[Clang][CodeGen] Respect -fwrapv-pointer when emitting struct GEPs (#134269)Yingwei Zheng
This patch turns off inbounds/nuw flags for member accesses when `-fwrapv-pointer` is set. Closes https://github.com/llvm/llvm-project/issues/132449. It is required by https://github.com/llvm/llvm-project/pull/130734.
2025-04-08[Clang][OpenCL][AMDGPU] Allow a kernel to call another kernel (#115821)Aniket Lal
This feature is currently not supported in the compiler. To facilitate this we emit a stub version of each kernel function body with different name mangling scheme, and replaces the respective kernel call-sites appropriately. Fixes https://github.com/llvm/llvm-project/issues/60313 D120566 was an earlier attempt made to upstream a solution for this issue. --------- Co-authored-by: anikelal <anikelal@amd.com>
2025-04-04[clang] [sanitizer] predict trap checks succeed (#134310)Florian Mayer
Trap checks fail at most once (when the program crashes).
2025-04-03[clang][NFC] Refactor CodeGen's hasBooleanRepresentation (#134159)Andy Kaylor
The ClangIR upstreaming project needs the same logic for hasBooleanRepresentation() that is currently implemented in the standard clang codegen. In order to share this code, this change moves the implementation of this function into the AST Type class. No functional change is intended by this change. The ClangIR use of this function will be added separately in a later change.
2025-04-03[ASan] Add metadata to renamed instructions so ASan doesn't use the i… ↵gbMattN
(#119387) …ncorrect name Clang needs variables to be represented with unique names. This means that if a variable shadows another, its given a different name internally to ensure it has a unique name. If ASan tries to use this name when printing an error, it will print the modified unique name, rather than the variable's source code name Fixes #47326
2025-03-28[clang] [sanitizer] add pseudofunction to indicate array-bounds check (#128977)Florian Mayer
With this, we can: * use profilers to estimate how many cycles we spend on these checks (subject to caveats), * more easily see why we crashed.
2025-03-11[HLSL] Make memory representation of boolean vectors in HLSL, vectors of ↵Sarah Spall
i32. Add support for boolean swizzling. (#123977) Make the memory representation of boolean vectors in HLSL, vectors of i32. Allow boolean swizzling for boolean vectors in HLSL. Add tests for boolean vectors and boolean vector swizzling. Closes #91639
2025-02-15[HLSL] Implement HLSL intialization list support (#123141)Chris B
This PR implements HLSL's initialization list behvaior as specified in the draft language specifcation under [*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Decl.Init.Agg). This behavior is a bit unusual for C/C++ because intermediate braces in initializer lists are ignored and a whole array of additional conversions occur unintuitively to how initializaiton works in C. The implementaiton in this PR generates a valid C/C++ initialization list AST for the HLSL initializer so that there are no changes required to Clang's CodeGen to support this. This design will also allow us to use Clang's rewrite to convert HLSL initializers to valid C/C++ initializers that are equivalent. It does have the downside that it will generate often redundant accesses during codegen. The IR optimizer is extremely good at eliminating those so this will have no impact on the final executable performance. There is some opportunity for optimizing the initializer list generation that we could consider in subsequent commits. One notable opportunity would be to identify aggregate objects that occur in the same place in both initializers and do not require converison, those aggregates could be initialized as aggregates rather than fully scalarized. Closes #56067 --------- Co-authored-by: Finn Plummer <50529406+inbelic@users.noreply.github.com> Co-authored-by: Helena Kotas <hekotas@microsoft.com> Co-authored-by: Justin Bogner <mail@justinbogner.com>
2025-02-14[HLSL] Implement HLSL Aggregate splatting (#118992)Sarah Spall
Implement HLSL Aggregate Splat casting that handles splatting for arrays and structs, and vectors if splatting from a vec1. Closes #100609 and Closes #100619 Depends on #118842
2025-02-08[CodeGen] Replace of PointerType::get(Type) with opaque version (NFC) (#124771)Mats Jun Larsen
Follow-up to https://github.com/llvm/llvm-project/issues/123569
2025-02-07[HLSL] Implement HLSL Elementwise casting (excluding splat cases); Re-land ↵Sarah Spall
#118842 (#126258) Implement HLSLElementwiseCast excluding support for splat cases Do not support casting types that contain bitfields. Partly closes https://github.com/llvm/llvm-project/issues/100609 and partly closes https://github.com/llvm/llvm-project/issues/100619 Re-land #118842 after fixing warning as an error, found by a buildbot.
2025-02-06Revert "[HLSL] Implement HLSL Flat casting (excluding splat cases)" (#126149)Sarah Spall
Reverts llvm/llvm-project#118842
2025-02-06[HLSL] Implement HLSL Flat casting (excluding splat cases) (#118842)Sarah Spall
Implement HLSLElementwiseCast excluding support for splat cases Do not support casting types that contain bitfields. Partly closes #100609 and partly closes #100619
2025-01-30Reapply "[ubsan] Connect -fsanitize-skip-hot-cutoff to ↵Thurston Dang
LowerAllowCheckPass<cutoffs>" (#125032) (#125037) This reverts commit 928cad49beec0120686478f502899222e836b545 i.e., relands dccd27112722109d2e2f03e8da9ce8690f06e11b, with a fix to avoid use-after-scope by changing the lambda to capture by value.
2025-01-29Revert "[ubsan] Connect -fsanitize-skip-hot-cutoff to ↵Thurston Dang
LowerAllowCheckPass<cutoffs>" (#125032) Reverts llvm/llvm-project#124857 due to buildbot breakage (https://lab.llvm.org/buildbot/#/builders/46/builds/11310)
2025-01-29[ubsan] Connect -fsanitize-skip-hot-cutoff to LowerAllowCheckPass<cutoffs> ↵Thurston Dang
(#124857) This adds the plumbing between -fsanitize-skip-hot-cutoff (introduced in https://github.com/llvm/llvm-project/pull/121619) and LowerAllowCheckPass<cutoffs> (introduced in https://github.com/llvm/llvm-project/pull/124211). The net effect is that -fsanitize-skip-hot-cutoff now combines the functionality of -ubsan-guard-checks and -lower-allow-check-percentile-cutoff (though this patch does not remove those yet), and generalizes the latter to allow per-sanitizer cutoffs. Note: this patch replaces Intrinsic::allow_ubsan_check's SanitizerHandler parameter with SanitizerOrdinal; this is necessary because the hot cutoffs are specified in terms of SanitizerOrdinal (e.g., null, alignment), not SanitizerHandler (e.g., TypeMismatch). Likewise, CodeGenFunction::EmitCheck is changed to emit allow_ubsan_check() for each individual check. --------- Co-authored-by: Vitaly Buka <vitalybuka@gmail.com> Co-authored-by: Vitaly Buka <vitalybuka@google.com>