[clang] [llvm] [LV] Support generating masks for switch terminators. (PR #99808)

2024-07-21 Thread Florian Hahn via cfe-commits

https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/99808

Update createEdgeMask to created masks where the terminator in Src is a switch. 
We need to handle 2 separate cases:

1. Dst is not the default desintation. Dst is reached if any of the cases with 
destination == Dst are taken. Join the conditions for each case where 
destination == Dst using a logical OR.
2. Dst is the default destination. Dst is reached if none of the cases with 
destination != Dst are taken. Join the conditions for each case where the 
destination is != Dst using a logical OR and negate it.

Fixes https://github.com/llvm/llvm-project/issues/48188.

>From 19d0aec67eb5f115c0b5349bbf2742154f66c0f1 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sat, 20 Jul 2024 21:12:00 +0100
Subject: [PATCH] [LV] Support generating masks for switch terminators.

Update createEdgeMask to created masks where the terminator in Src is a
switch. We need to handle 2 separate cases:

1. Dst is not the default desintation. Dst is reached if any of the cases
   with destination == Dst are taken. Join the conditions for each case
  where destination == Dst using a logical OR.
2. Dst is the default destination. Dst is reached if none of the cases
   with destination != Dst are taken. Join the conditions for each case
   where the destination is != Dst using a logical OR and negate it.

Fixes https://github.com/llvm/llvm-project/issues/48188.
---
 .../Frontend/optimization-remark-analysis.c   |   2 +-
 .../Vectorize/LoopVectorizationLegality.cpp   |  10 +-
 .../Transforms/Vectorize/LoopVectorize.cpp|  35 +
 .../LoopVectorize/X86/predicate-switch.ll | 647 +-
 .../Transforms/LoopVectorize/no_switch.ll |  12 +-
 .../LoopVectorize/predicate-switch.ll | 200 +-
 .../X86/pr48844-br-to-switch-vectorization.ll |  60 +-
 7 files changed, 920 insertions(+), 46 deletions(-)

diff --git a/clang/test/Frontend/optimization-remark-analysis.c 
b/clang/test/Frontend/optimization-remark-analysis.c
index e43984942a6ef..9d8917265a320 100644
--- a/clang/test/Frontend/optimization-remark-analysis.c
+++ b/clang/test/Frontend/optimization-remark-analysis.c
@@ -1,7 +1,7 @@
 // RUN: %clang -O1 -fvectorize -target x86_64-unknown-unknown -emit-llvm 
-Rpass-analysis -S %s -o - 2>&1 | FileCheck %s --check-prefix=RPASS
 // RUN: %clang -O1 -fvectorize -target x86_64-unknown-unknown -emit-llvm -S %s 
-o - 2>&1 | FileCheck %s
 
-// RPASS: {{.*}}:12:5: remark: loop not vectorized: loop contains a switch 
statement
+// RPASS-NOT: {{.*}}:12:5: remark: loop not vectorized
 // CHECK-NOT: remark: loop not vectorized: loop contains a switch statement
 
 double foo(int N, int *Array) {
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp 
b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index f54eebb2874ab..7f84455150093 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1348,11 +1348,11 @@ bool 
LoopVectorizationLegality::canVectorizeWithIfConvert() {
   // Collect the blocks that need predication.
   for (BasicBlock *BB : TheLoop->blocks()) {
 // We don't support switch statements inside loops.
-if (!isa(BB->getTerminator())) {
-  reportVectorizationFailure("Loop contains a switch statement",
- "loop contains a switch statement",
- "LoopContainsSwitch", ORE, TheLoop,
- BB->getTerminator());
+if (!isa(BB->getTerminator())) {
+  reportVectorizationFailure("Loop contains an unsupported termaintor",
+ "loop contains an unsupported terminator",
+ "LoopContainsUnsupportedTerminator", ORE,
+ TheLoop, BB->getTerminator());
   return false;
 }
 
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp 
b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 6d28b8fabe42e..2530762e3e424 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7763,6 +7763,41 @@ VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock 
*Src, BasicBlock *Dst) {
 
   VPValue *SrcMask = getBlockInMask(Src);
 
+  if (auto *SI = dyn_cast(Src->getTerminator())) {
+// Create mask where the terminator in Src is a switch. We need to handle 2
+// separate cases:
+// 1. Dst is not the default desintation. Dst is reached if any of the 
cases
+// with destination == Dst are taken. Join the conditions for each case
+// where destination == Dst using a logical OR.
+// 2. Dst is the default destination. Dst is reached if none of the cases
+// with destination != Dst are taken. Join the conditions for each case
+// where the destination is != Dst using a logical OR and negate it.
+VPValue *Mask = nullptr;
+VPValue *Cond = 

[clang] f9f6f5a - [TBAA] Remove remaining entry BB in check lines for tbaa-pointers.c (2)

2024-07-19 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-07-19T15:48:50+01:00
New Revision: f9f6f5a9c7748fe485adee206748adf28a849b79

URL: 
https://github.com/llvm/llvm-project/commit/f9f6f5a9c7748fe485adee206748adf28a849b79
DIFF: 
https://github.com/llvm/llvm-project/commit/f9f6f5a9c7748fe485adee206748adf28a849b79.diff

LOG: [TBAA] Remove remaining entry BB in check lines for tbaa-pointers.c (2)

Missed out of 47c08fb8d79ec.

Added: 


Modified: 
clang/test/CodeGen/tbaa-pointers.c

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-pointers.c 
b/clang/test/CodeGen/tbaa-pointers.c
index c036110f45c70..75d8c3d501750 100644
--- a/clang/test/CodeGen/tbaa-pointers.c
+++ b/clang/test/CodeGen/tbaa-pointers.c
@@ -91,8 +91,7 @@ void p4char_const1(const char ptr) {
 void p4char_const2(const char **const **ptr) {
   // COMMON-LABEL: define void @p4char_const2(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:[[PTR_ADDR:%.+]] = alloca ptr, align 8
+  // COMMON: [[PTR_ADDR:%.+]] = alloca ptr, align 8
   // ENABLED-NEXT:   store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[P4CHAR_0]]
   // ENABLED-NEXT:   [[BASE_0:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, 
!tbaa [[P4CHAR_0]]
   // ENABLED-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[P3CHAR_0]]



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 47c08fb - [TBAA] Remove references to entry BB in check lines for tbaa-pointers.c

2024-07-19 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-07-19T14:51:27+01:00
New Revision: 47c08fb8d79ec1bf85cc542be6ca2591ebf2d361

URL: 
https://github.com/llvm/llvm-project/commit/47c08fb8d79ec1bf85cc542be6ca2591ebf2d361
DIFF: 
https://github.com/llvm/llvm-project/commit/47c08fb8d79ec1bf85cc542be6ca2591ebf2d361.diff

LOG: [TBAA] Remove references to entry BB in check lines for tbaa-pointers.c

Follow-up to 123c036bd361d to remove references to entry BB name from
checks, to fix tests for builds that do not generate block names.

Added: 


Modified: 
clang/test/CodeGen/tbaa-pointers.c

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-pointers.c 
b/clang/test/CodeGen/tbaa-pointers.c
index f85fca5e34fc0..c036110f45c70 100644
--- a/clang/test/CodeGen/tbaa-pointers.c
+++ b/clang/test/CodeGen/tbaa-pointers.c
@@ -6,15 +6,14 @@
 void p2unsigned(unsigned **ptr) {
   // COMMON-LABEL: define void @p2unsigned(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:   [[PTR_ADDR:%.+]] = alloca ptr, align 8
+  // COMMON:[[PTR_ADDR:%.+]] = alloca ptr, align 8
   // ENABLED-NEXT:  store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[P2INT_0:!.+]]
   // ENABLED-NEXT:  [[BASE:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa 
[[P2INT_0]]
   // ENABLED-NEXT:  store ptr null, ptr [[BASE]], align 8, !tbaa 
[[P1INT_0:!.+]]
   // DEFAULT-NEXT:  store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[ANYPTR:!.+]]
   // DEFAULT-NEXT:  [[BASE:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:  store ptr null, ptr [[BASE]], align 8, !tbaa [[ANYPTR]]
-  // COMMON-NEXT:  ret void
+  // COMMON-NEXT:   ret void
   //
   *ptr = 0;
 }
@@ -22,15 +21,14 @@ void p2unsigned(unsigned **ptr) {
 void p2unsigned_volatile(unsigned *volatile *ptr) {
   // COMMON-LABEL: define void @p2unsigned_volatile(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:[[PTR_ADDR:%.+]] = alloca ptr, align 8
+  // COMMON: [[PTR_ADDR:%.+]] = alloca ptr, align 8
   // ENABLED-NEXT:   store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[P2INT_0]]
   // ENABLED-NEXT:   [[BASE:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa 
[[P2INT_0]]
   // ENABLED-NEXT:   store volatile ptr null, ptr [[BASE]], align 8, !tbaa 
[[P1INT_0]]
   // DEFAULT-NEXT:   store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:   [[BASE:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:   store volatile ptr null, ptr [[BASE]], align 8, !tbaa 
[[ANYPTR]]
-  // COMMON-NEXT:   ret void
+  // COMMON-NEXT:ret void
   //
   *ptr = 0;
 }
@@ -38,8 +36,7 @@ void p2unsigned_volatile(unsigned *volatile *ptr) {
 void p3int(int ***ptr) {
   // COMMON-LABEL: define void @p3int(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:[[PTR_ADDR:%.+]] = alloca ptr, align 8
+  // COMMON: [[PTR_ADDR:%.+]] = alloca ptr, align 8
   // ENABLED-NEXT:   store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[P3INT_0:!.+]]
   // ENABLED-NEXT:   [[BASE_0:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, 
!tbaa [[P3INT_0]]
   // ENABLED-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[P2INT_0]]
@@ -48,8 +45,7 @@ void p3int(int ***ptr) {
   // DEFAULT-NEXT:   [[BASE_0:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, 
!tbaa [[ANYPTR]]
   // DEFAULT-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:   store ptr null, ptr [[BASE_1]], align 8, !tbaa [[ANYPTR]]
-
-  // COMMON-NEXT:   ret void
+  // COMMON-NEXT:ret void
   //
   **ptr = 0;
 }
@@ -57,8 +53,7 @@ void p3int(int ***ptr) {
 void p4char(char ptr) {
   // COMMON-LABEL: define void @p4char(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:[[PTR_ADDR:%.+]] = alloca ptr, align 8
+  // COMMON: [[PTR_ADDR:%.+]] = alloca ptr, align 8
   // ENABLED-NEXT:   store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa 
[[P4CHAR_0:!.+]]
   // ENABLED-NEXT:   [[BASE_0:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, 
!tbaa [[P4CHAR_0]]
   // ENABLED-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[P3CHAR_0:!.+]]
@@ -69,7 +64,7 @@ void p4char(char ptr) {
   // DEFAULT-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:   [[BASE_2:%.+]] = load ptr, ptr [[BASE_1]], align 8, !tbaa 
[[ANYPTR]]
   // DEFAULT-NEXT:   store ptr null, ptr [[BASE_2]], align 8, !tbaa [[ANYPTR]]
-  // COMMON-NEXT:   ret void
+  // COMMON-NEXT:ret void
   //
   ***ptr = 0;
 }
@@ -77,8 +72,7 @@ void p4char(char ptr) {
 void p4char_const1(const char ptr) {
   // COMMON-LABEL: define void @p4char_const1(
   // COMMON-SAME:ptr noundef [[PTR:%.+]])
-  // COMMON-NEXT:  entry:
-  // COMMON-NEXT:

[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-07-12 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-07-12 Thread Florian Hahn via cfe-commits


@@ -233,6 +233,7 @@ ENUM_CODEGENOPT(StructReturnConvention, 
StructReturnConventionKind, 2, SRCK_Defa
 
 CODEGENOPT(RelaxAll  , 1, 0) ///< Relax all machine code instructions.
 CODEGENOPT(RelaxedAliasing   , 1, 0) ///< Set when -fno-strict-aliasing is 
enabled.
+CODEGENOPT(PointerTBAA, 1, 0) ///< Whether or not to use distinct TBAA tags 
for pointers.

fhahn wrote:

Adjusted, thanks!

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-07-10 Thread Florian Hahn via cfe-commits


@@ -392,6 +392,10 @@ Non-comprehensive list of changes in this release
 - ``#pragma GCC diagnostic warning "-Wfoo"`` can now downgrade ``-Werror=foo``
   errors and certain default-to-error ``-W`` diagnostics to warnings.
 
+- Clang now emits distinct type-based alias analysis tags for incompatible
+  pointers, enabling more powerful alias analysis when accessing pointer types.
+  The new behavior can be disabledusing ``-fno-pointer-tbaa``.

fhahn wrote:

Updated ,thanks!

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-07-10 Thread Florian Hahn via cfe-commits

https://github.com/fhahn commented:

> First of all I want to say that this is a really cool project, especially the 
> type sanitizer part. Thanks for working on this!
> 
> > Just updated the tysan branches again. Unfortunately we aren't yet at a 
> > point where LLVM is `tysan` clean, there is a large number of reported 
> > violations when running the tblgen built with `tysan` even without this 
> > patch. I'd expect that a substantial amount of work is still needed to get 
> > to a state where we can build LLVM successfully with tysan.
> 
> Given that not even LLVM itself is clean (which has probably some of the 
> densest population of compiler engineers), and you say that this will require 
> a significant amount of work, would it maybe make sense to have this 
> off-by-default for now and only enable it in a year or so by default? 
> Especially if there are problems in any system libraries, like libc++, it 
> would be rather unfortunate, since users can't do much about it.

Fair point especially as we are close to cutting a release branch and it should 
definitely be off by default there. I changed it to be disabled by default.

Not sure if we should wait a whole year, but we can figure out the details 
separately after the release branch has been cut

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-07-10 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Clang][ARM][AArch64] Alway emit protection attributes for functions. (PR #82819)

2024-07-10 Thread Florian Hahn via cfe-commits

fhahn wrote:

It looks like there's still a failure related to this patch on current main 
AFAICT (MSAN finds a use of uninitialized value): 
https://lab.llvm.org/buildbot/#/builders/169/builds/852/steps/12/logs/stdio

```
FAIL: Clang :: CodeGen/arm-branch-protection-attr-1.c (3 of 83868)
 TEST 'Clang :: CodeGen/arm-branch-protection-attr-1.c' 
FAILED 
Exit Code: 2
Command Output (stderr):
--
RUN: at line 2: /b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/bin/clang 
-cc1 -internal-isystem 
/b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/lib/clang/19/include 
-nostdsysteminc -triple thumbv7m-unknown-unknown-eabi -emit-llvm 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/CodeGen/arm-branch-protection-attr-1.c
 -o -| 
/b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/bin/FileCheck 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/CodeGen/arm-branch-protection-attr-1.c
 --check-prefix=CHECK
+ /b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/bin/clang -cc1 
-internal-isystem 
/b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/lib/clang/19/include 
-nostdsysteminc -triple thumbv7m-unknown-unknown-eabi -emit-llvm 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/CodeGen/arm-branch-protection-attr-1.c
 -o -
+ /b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/bin/FileCheck 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/test/CodeGen/arm-branch-protection-attr-1.c
 --check-prefix=CHECK
==227510==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x5f411b60 in 
clang::TargetInfo::BranchProtectionInfo::setFnAttributes(llvm::AttrBuilder&) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/include/clang/Basic/TargetInfo.h:1468:11
#1 0x5f411931 in 
clang::TargetInfo::BranchProtectionInfo::setFnAttributes(llvm::Function&) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/include/clang/Basic/TargetInfo.h:1455:7
#2 0x5f418b20 in (anonymous 
namespace)::ARMTargetCodeGenInfo::setTargetAttributes(clang::Decl const*, 
llvm::GlobalValue*, clang::CodeGen::CodeGenModule&) const 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/Targets/ARM.cpp:155:15
#3 0x5e980b31 in 
clang::CodeGen::CodeGenModule::setNonAliasAttributes(clang::GlobalDecl, 
llvm::GlobalObject*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:2727:26
#4 0x5e98fe97 in 
clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, 
llvm::GlobalValue*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:5980:3
#5 0x5e9853f4 in 
clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, 
llvm::GlobalValue*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:4141:12
#6 0x5e98bfb3 in 
clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:3852:5
#7 0x5e982db1 in 
clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:6863:5
#8 0x5f1ef187 in (anonymous 
namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp:190:18
#9 0x5f1e1510 in 
clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:199:8
#10 0x616c3460 in clang::ParseAST(clang::Sema&, bool, bool) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/Parse/ParseAST.cpp:167:31
#11 0x5f6dd91b in clang::FrontendAction::Execute() 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1078:8
#12 0x5f628f29 in 
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1061:33
#13 0x5f7e7b3d in 
clang::ExecuteCompilerInvocation(clang::CompilerInstance*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:280:25
#14 0x5b0a2b10 in cc1_main(llvm::ArrayRef, char const*, 
void*) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/tools/driver/cc1_main.cpp:276:15
#15 0x5b09d0bf in ExecuteCC1Tool(llvm::SmallVectorImpl&, 
llvm::ToolContext const&) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/tools/driver/driver.cpp:215:12
#16 0x5b09ad69 in clang_main(int, char**, llvm::ToolContext const&) 
/b/sanitizer-x86_64-linux-fast/build/llvm-project/clang/tools/driver/driver.cpp:256:12
#17 0x5b0b8018 in main 
/b/sanitizer-x86_64-linux-fast/build/llvm_build_msan/tools/clang/tools/driver/clang-driver.cpp:17:10

[clang] [clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (PR #98138)

2024-07-09 Thread Florian Hahn via cfe-commits

fhahn wrote:

Would it be possible to add a test case? Could you add a bit more detail how 
different mange contexts cause crashes in TBAA?

https://github.com/llvm/llvm-project/pull/98138
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SCCP] Add support for vectors (PR #98026)

2024-07-09 Thread Florian Hahn via cfe-commits

https://github.com/fhahn approved this pull request.

LGTM, thanks!

https://github.com/llvm/llvm-project/pull/98026
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-27 Thread Florian Hahn via cfe-commits

https://github.com/fhahn commented:

> Changes generally LGTM though I think this should come with a release note so 
> users know about the new command line options and functionality?

Thanks, I tried to add release note entries for the new behavior + the new 
flags. Hope I added them in the right place.

> I don't see any issues with the code. I'd prefer if we has a 
> TYSan-instrumented bootstrap build of LLVM before we merge this, so we're 
> confident this doesn't cause any unexpected issues.

Just updated the tysan branches again. Unfortunately we aren't yet at a point 
where LLVM is `tysan` clean, there is a large number of reported violations 
when running the tblgen built with `tysan` even without this patch. I'd expect 
that a substantial amount of work is still needed to get to a state where we 
can build LLVM successfully with tysan.

What is feasible at the moment is building SingleSource and for that the patch 
doesn't add new regressions (there are ~20 tests failing with tysan out of 
~1800). Still building MultiSource, but some files take a huge amount of time 
to build with tysan :(

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-27 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-27 Thread Florian Hahn via cfe-commits


@@ -185,10 +185,56 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type 
*Ty) {
 return getChar();
 
   // Handle pointers and references.
-  // TODO: Implement C++'s type "similarity" and consider dis-"similar"
-  // pointers distinct.
-  if (Ty->isPointerType() || Ty->isReferenceType())
-return createScalarTypeNode("any pointer", getChar(), Size);
+  //
+  // C has a very strict rule for pointer aliasing. C23 6.7.6.1p2:
+  // For two pointer types to be compatible, both shall be identically
+  // qualified and both shall be pointers to compatible types.
+  //
+  // This rule is impractically strict; we want to at least ignore CVR

fhahn wrote:

Sounds good!

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Pipelines] Move IPSCCP after inliner pipeline (PR #96620)

2024-06-27 Thread Florian Hahn via cfe-commits

https://github.com/fhahn commented:

Running IPSCCP twice seems like quite a heavy hammer, I'd expect a noticeable 
compile-time impact.

I'd recommend to try to extract a reproducer from your motivating use case and 
check why IPSCCP cannot perform the desired optimization before inlining. Note 
that we run SCCP after inlining I think, which is the non-IP version of IPSCCP

https://github.com/llvm/llvm-project/pull/96620
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-26 Thread Florian Hahn via cfe-commits


@@ -185,10 +185,33 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type 
*Ty) {
 return getChar();
 
   // Handle pointers and references.
-  // TODO: Implement C++'s type "similarity" and consider dis-"similar"
-  // pointers distinct.
-  if (Ty->isPointerType() || Ty->isReferenceType())
-return createScalarTypeNode("any pointer", getChar(), Size);
+  if (Ty->isPointerType() || Ty->isReferenceType()) {
+llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), 
Size);
+if (CodeGenOpts.RelaxedPointerAliasing)
+  return AnyPtr;
+// Compute the depth of the pointer and generate a tag of the form 
"p
+// ".
+unsigned PtrDepth = 0;
+do {
+  PtrDepth++;
+  Ty = Ty->getPointeeType().getTypePtr();
+} while (Ty->isPointerType() || Ty->isReferenceType());

fhahn wrote:

Adjusted, thanks!

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-26 Thread Florian Hahn via cfe-commits


@@ -185,10 +185,33 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type 
*Ty) {
 return getChar();
 
   // Handle pointers and references.

fhahn wrote:

Added, thank you very much!

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-25 Thread Florian Hahn via cfe-commits

fhahn wrote:

Rebased, updated and added option to disable this (`-fno-pointer-tbaa`). I 
think this would be ready for a review now :) 

https://github.com/llvm/llvm-project/pull/76612
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add value_type attr, use it to add noalias when pass-by-value. (PR #95004)

2024-06-19 Thread Florian Hahn via cfe-commits

fhahn wrote:

Thanks, I'll hope to get back to this and make the suggested adjustments soonish

https://github.com/llvm/llvm-project/pull/95004
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit distinct TBAA tags for pointers with different depths,types. (PR #76612)

2024-06-11 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/76612

>From 61c94b87858e51748a9664acab51ca4c964265a4 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sat, 30 Dec 2023 10:39:58 +
Subject: [PATCH] [TBAA] Emit distinct TBAA tags for pointers with different
 depths,types.

This patch extends Clang's TBAA generation code to emit distinct tags
for incompatible pointer types.

Pointers with different element types are incompatible if the pointee
types are also incompatible (modulo sugar/modifiers).

Express this in TBAA by generating different tags for pointers based
on the pointer depth and pointee type. To get the TBAA tag for the
pointee type it uses getTypeInfoHelper on the pointee type.

(Moved from https://reviews.llvm.org/D122573)
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 29 ++--
 clang/test/CodeGen/tbaa-pointers.c | 72 +-
 2 files changed, 67 insertions(+), 34 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 284421f494711..d7a45173cdf8f 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -185,10 +185,31 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type 
*Ty) {
 return getChar();
 
   // Handle pointers and references.
-  // TODO: Implement C++'s type "similarity" and consider dis-"similar"
-  // pointers distinct.
-  if (Ty->isPointerType() || Ty->isReferenceType())
-return createScalarTypeNode("any pointer", getChar(), Size);
+  if (Ty->isPointerType() || Ty->isReferenceType()) {
+llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), 
Size);
+// Compute the depth of the pointer and generate a tag of the form 
"p
+// ".
+unsigned PtrDepth = 0;
+do {
+  PtrDepth++;
+  Ty = Ty->getPointeeType().getTypePtr();
+} while (Ty->isPointerType() || Ty->isReferenceType());
+// TODO: Implement C++'s type "similarity" and consider dis-"similar"
+// pointers distinct for non-builtin types.
+if (isa(Ty)) {
+  llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty);
+  StringRef Name =
+  cast(
+  ScalarMD->getOperand(CodeGenOpts.NewStructPathTBAA ? 2 : 0))
+  ->getString();
+  SmallString<256> OutName("p");
+  OutName += std::to_string(PtrDepth);
+  OutName += " ";
+  OutName += Name;
+  return createScalarTypeNode(OutName, AnyPtr, Size);
+}
+return AnyPtr;
+  }
 
   // Accesses to arrays are accesses to objects of their element types.
   if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
diff --git a/clang/test/CodeGen/tbaa-pointers.c 
b/clang/test/CodeGen/tbaa-pointers.c
index b9ebe87982001..a3a7aa0d66473 100644
--- a/clang/test/CodeGen/tbaa-pointers.c
+++ b/clang/test/CodeGen/tbaa-pointers.c
@@ -4,9 +4,9 @@ void p2unsigned(unsigned **ptr) {
   // CHECK-LABEL: define void @p2unsigned(ptr noundef %ptr)
   // CHECK-NEXT: entry:
   // CHECK-NEXT:  %ptr.addr = alloca ptr, align 8
-  // CHECK-NEXT:  store ptr %ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0:!.+]]
-  // CHECK-NEXT:  [[BASE:%.+]] = load ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:  store ptr null, ptr [[BASE]], align 8, !tbaa 
[[ANY_POINTER_0]]
+  // CHECK-NEXT:  store ptr %ptr, ptr %ptr.addr, align 8, !tbaa [[P2INT_0:!.+]]
+  // CHECK-NEXT:  [[BASE:%.+]] = load ptr, ptr %ptr.addr, align 8, !tbaa 
[[P2INT_0]]
+  // CHECK-NEXT:  store ptr null, ptr [[BASE]], align 8, !tbaa [[P1INT_0:!.+]]
   // CHECK-NEXT:  ret void
   //
   *ptr = 0;
@@ -16,9 +16,9 @@ void p2unsigned_volatile(unsigned *volatile *ptr) {
   // CHECK-LABEL: define void @p2unsigned_volatile(ptr noundef %ptr)
   // CHECK-NEXT: entry:
   // CHECK-NEXT:   %ptr.addr = alloca ptr, align 8
-  // CHECK-NEXT:   store ptr %ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:   [[BASE:%.+]] = load ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:   store volatile ptr null, ptr [[BASE]], align 8, !tbaa 
[[ANY_POINTER_0]]
+  // CHECK-NEXT:   store ptr %ptr, ptr %ptr.addr, align 8, !tbaa [[P2INT_0]]
+  // CHECK-NEXT:   [[BASE:%.+]] = load ptr, ptr %ptr.addr, align 8, !tbaa 
[[P2INT_0]]
+  // CHECK-NEXT:   store volatile ptr null, ptr [[BASE]], align 8, !tbaa 
[[P1INT_0]]
   // CHECK-NEXT:   ret void
   //
   *ptr = 0;
@@ -28,10 +28,10 @@ void p3int(int ***ptr) {
   // CHECK-LABEL: define void @p3int(ptr noundef %ptr)
   // CHECK-NEXT: entry:
   // CHECK-NEXT:   %ptr.addr = alloca ptr, align 8
-  // CHECK-NEXT:   store ptr %ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:   [[BASE_0:%.+]] = load ptr, ptr %ptr.addr, align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:   [[BASE_1:%.+]] = load ptr, ptr [[BASE_0]], align 8, !tbaa 
[[ANY_POINTER_0]]
-  // CHECK-NEXT:   store ptr null, ptr [[BASE_1]], align 8, !tbaa 
[[ANY_POINTER_0]]
+  // CHECK-NEXT:   store ptr %ptr, ptr %ptr.addr, align 8, !tbaa 
[[P3INT_0:!.+]]
+  // 

[clang] [clang] Add value_type attr, use it to add noalias when pass-by-value. (PR #95004)

2024-06-10 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/95004
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add value_type attr, use it to add noalias when pass-by-value. (PR #95004)

2024-06-10 Thread Florian Hahn via cfe-commits

https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/95004

This adds an attribute version of -fpass-by-value-is-noalias (added in 
a874d63344)

Still needs proper docs.

>From 34e880a20b0b36218a1307e52ccb71092bfd64f7 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Mon, 10 Jun 2024 16:54:02 +0100
Subject: [PATCH] [clang] Add value_type attr, use it to add noalias when
 pass-by-value.

This adds an attribute version of -fpass-by-value-is-noalias (added in
a874d63344)

Still needs proper docs.
---
 clang/include/clang/Basic/Attr.td | 10 +
 clang/lib/CodeGen/CGCall.cpp  |  7 ++--
 clang/test/CodeGenCXX/value-type-attr.cpp | 40 +++
 ...a-attribute-supported-attributes-list.test |  1 +
 4 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/value-type-attr.cpp

diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index b70b0c8b836a5..811e1620f38d7 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1811,6 +1811,16 @@ def TrivialABI : InheritableAttr {
   let SimpleHandler = 1;
 }
 
+def ValueType : InheritableAttr {
+  // This attribute does not have a C [[]] spelling because it requires the
+  // CPlusPlus language option.
+  let Spellings = [Clang<"value_type", 0>];
+  let Subjects = SubjectList<[CXXRecord]>;
+  let Documentation = [TrivialABIDocs];
+  let LangOpts = [CPlusPlus];
+  let SimpleHandler = 1;
+}
+
 def MaxFieldAlignment : InheritableAttr {
   // This attribute has no spellings as it is only ever created implicitly.
   let Spellings = [];
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 65d82285b907b..55733734d1de2 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2744,9 +2744,10 @@ void CodeGenModule::ConstructAttributeList(StringRef 
Name,
 Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));
 
   auto *Decl = ParamType->getAsRecordDecl();
-  if (CodeGenOpts.PassByValueIsNoAlias && Decl &&
-  Decl->getArgPassingRestrictions() ==
-  RecordArgPassingKind::CanPassInRegs)
+  if (Decl && ((CodeGenOpts.PassByValueIsNoAlias &&
+Decl->getArgPassingRestrictions() ==
+RecordArgPassingKind::CanPassInRegs) ||
+   Decl->hasAttr()))
 // When calling the function, the pointer passed in will be the only
 // reference to the underlying object. Mark it accordingly.
 Attrs.addAttribute(llvm::Attribute::NoAlias);
diff --git a/clang/test/CodeGenCXX/value-type-attr.cpp 
b/clang/test/CodeGenCXX/value-type-attr.cpp
new file mode 100644
index 0..46938dd7d271a
--- /dev/null
+++ b/clang/test/CodeGenCXX/value-type-attr.cpp
@@ -0,0 +1,40 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py 
UTC_ARGS: --version 5
+// RUN: %clang_cc1 -triple arm64-apple-ios11 -std=c++11 -fcxx-exceptions 
-fexceptions -emit-llvm -o - %s | FileCheck %s
+
+struct Small {
+  int *p;
+  Small();
+  ~Small();
+  Small(const Small &) noexcept;
+  Small =(const Small &);
+};
+
+
+struct __attribute__((value_type)) SmallVT {
+  int *p;
+  SmallVT();
+  ~SmallVT();
+  SmallVT(const SmallVT &) noexcept;
+  SmallVT =(const SmallVT &);
+};
+
+struct Large {
+  int *p;
+  int a[128];
+  Large();
+  ~Large();
+  Large(const Large &) noexcept;
+  Large =(const Large &);
+};
+
+struct __attribute__((value_type)) LargeVT {
+  int *p;
+  int a[128];
+  LargeVT();
+  ~LargeVT();
+  LargeVT(const LargeVT &) noexcept;
+  LargeVT =(const LargeVT &);
+};
+
+void foo(Small a, SmallVT b, Large c, LargeVT d) {}
+// CHECK: define void @_Z3foo5Small7SmallVT5Large7LargeVT(ptr noundef %a, ptr 
noalias noundef %b, ptr noundef %c, ptr noalias noundef %d)
diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test 
b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
index 99732694f72a5..4474a2c415bb2 100644
--- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -201,6 +201,7 @@
 // CHECK-NEXT: Uninitialized (SubjectMatchRule_variable_is_local)
 // CHECK-NEXT: UnsafeBufferUsage (SubjectMatchRule_function)
 // CHECK-NEXT: UseHandle (SubjectMatchRule_variable_is_parameter)
+// CHECK-NEXT: ValueType (SubjectMatchRule_record)
 // CHECK-NEXT: VecReturn (SubjectMatchRule_record)
 // CHECK-NEXT: VecTypeHint (SubjectMatchRule_function)
 // CHECK-NEXT: WarnUnused (SubjectMatchRule_record)

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] clang-doc switched from using relative to absolute paths (PR #93281)

2024-05-24 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/93281

>From f5872e7c82d097ae3c141765d3f1d7e3d0b25b82 Mon Sep 17 00:00:00 2001
From: PeterChou1 
Date: Fri, 24 May 2024 04:28:08 -0400
Subject: [PATCH 1/2] clang-doc switched from using relative to absolute paths

---
 clang-tools-extra/clang-doc/assets/index.js | 72 ++---
 1 file changed, 35 insertions(+), 37 deletions(-)

diff --git a/clang-tools-extra/clang-doc/assets/index.js 
b/clang-tools-extra/clang-doc/assets/index.js
index a5ac90f2e06e7..fe35e706cc98d 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -1,48 +1,46 @@
-// Append using posix-style a file name or directory to Base
-function append(Base, New) {
-  if (!New)
-return Base;
-  if (Base)
-Base += "/";
-  Base += New;
-  return Base;
-}
-
-// Get relative path to access FilePath from CurrentDirectory
-function computeRelativePath(FilePath, CurrentDirectory) {
-  var Path = FilePath;
-  while (Path) {
-if (CurrentDirectory == Path)
-  return FilePath.substring(Path.length + 1);
-Path = Path.substring(0, Path.lastIndexOf("/"));
-  }
-
-  var Dir = CurrentDirectory;
-  var Result = "";
-  while (Dir) {
-if (Dir == FilePath)
-  break;
-Dir = Dir.substring(0, Dir.lastIndexOf("/"));
-Result = append(Result, "..")
+function genLink(Ref) {
+  var Path = 
`${window.location.protocol}//${window.location.host}/${Ref.Path}`;
+  if (Ref.RefType !== "namespace") {
+if (Ref.Path === "") {
+  Path = `${Path}${Ref.Name}.html`;
+}
+else {
+  Path = `${Path}/${Ref.Name}.html`;
+}
   }
-  Result = append(Result, FilePath.substring(Dir.length))
-  return Result;
-}
-
-function genLink(Ref, CurrentDirectory) {
-  var Path = computeRelativePath(Ref.Path, CurrentDirectory);
-  if (Ref.RefType == "namespace")
-Path = append(Path, "index.html");
-  else
-Path = append(Path, Ref.Name + ".html")
 
-ANode = document.createElement("a");
+  ANode = document.createElement("a");
   ANode.setAttribute("href", Path);
   var TextNode = document.createTextNode(Ref.Name);
   ANode.appendChild(TextNode);
   return ANode;
 }
 
+function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
+  // Out will store the HTML elements that Index requires to be generated
+  var Out = [];
+  if (Index.Name) {
+var SpanNode = document.createElement("span");
+var TextNode = document.createTextNode(Index.Name);
+SpanNode.appendChild(genLink(Index));
+Out.push(SpanNode);
+  }
+  if (Index.Children.length == 0)
+return Out;
+  // Only the outermost list should use ol, the others should use ul
+  var ListNodeName = IsOutermostList ? "ol" : "ul";
+  var ListNode = document.createElement(ListNodeName);
+  for (Child of Index.Children) {
+var LiNode = document.createElement("li");
+ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
+for (Node of ChildNodes)
+  LiNode.appendChild(Node);
+ListNode.appendChild(LiNode);
+  }
+  Out.push(ListNode);
+  return Out;
+}
+
 function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
   // Out will store the HTML elements that Index requires to be generated
   var Out = [];

>From a22609f75064194604e476a12bc8154676dacfb9 Mon Sep 17 00:00:00 2001
From: PeterChou1 
Date: Fri, 24 May 2024 05:10:03 -0400
Subject: [PATCH 2/2] remove duplicate function

---
 clang-tools-extra/clang-doc/assets/index.js | 24 -
 1 file changed, 24 deletions(-)

diff --git a/clang-tools-extra/clang-doc/assets/index.js 
b/clang-tools-extra/clang-doc/assets/index.js
index fe35e706cc98d..b013418c82093 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -16,30 +16,6 @@ function genLink(Ref) {
   return ANode;
 }
 
-function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
-  // Out will store the HTML elements that Index requires to be generated
-  var Out = [];
-  if (Index.Name) {
-var SpanNode = document.createElement("span");
-var TextNode = document.createTextNode(Index.Name);
-SpanNode.appendChild(genLink(Index));
-Out.push(SpanNode);
-  }
-  if (Index.Children.length == 0)
-return Out;
-  // Only the outermost list should use ol, the others should use ul
-  var ListNodeName = IsOutermostList ? "ol" : "ul";
-  var ListNode = document.createElement(ListNodeName);
-  for (Child of Index.Children) {
-var LiNode = document.createElement("li");
-ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
-for (Node of ChildNodes)
-  LiNode.appendChild(Node);
-ListNode.appendChild(LiNode);
-  }
-  Out.push(ListNode);
-  return Out;
-}
 
 function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
   // Out will store the HTML elements that Index requires to be generated

___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[clang] Run ObjCContractPass in Distributed Thin-LTO Pipeline (PR #92331)

2024-05-16 Thread Florian Hahn via cfe-commits

fhahn wrote:

Shouldn't this be added to the LTO code generator? In `libLTO` (used by Apple's 
linker) it is added here `llvm/lib/LTO/ThinLTOCodeGenerator.cpp`

https://github.com/llvm/llvm-project/pull/92331
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 8a4cbea - [Clang] Unbreak build take 2 using uint64_t() explicitly.

2024-05-15 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-05-15T15:49:03+01:00
New Revision: 8a4cbeada930bf11fe740a2038bd5a3230712284

URL: 
https://github.com/llvm/llvm-project/commit/8a4cbeada930bf11fe740a2038bd5a3230712284
DIFF: 
https://github.com/llvm/llvm-project/commit/8a4cbeada930bf11fe740a2038bd5a3230712284.diff

LOG: [Clang] Unbreak build take 2 using uint64_t() explicitly.

Added: 


Modified: 
clang/lib/CodeGen/Targets/Sparc.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/Targets/Sparc.cpp 
b/clang/lib/CodeGen/Targets/Sparc.cpp
index 13e9550781d17..561f0b514d909 100644
--- a/clang/lib/CodeGen/Targets/Sparc.cpp
+++ b/clang/lib/CodeGen/Targets/Sparc.cpp
@@ -266,7 +266,8 @@ SparcV9ABIInfo::classifyType(QualType Ty, unsigned 
SizeLimit) const {
   // All structs, even empty ones, should take up a register argument slot,
   // so pin the minimum struct size to one bit.
   CB.pad(llvm::alignTo(
-  std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), 1ULL), 64));
+  std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), uint64_t(1)),
+  64));
 
   // Try to use the original type for coercion.
   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][SPARC] Treat empty structs as if it's a one-bit type in the CC (PR #90338)

2024-05-15 Thread Florian Hahn via cfe-commits

fhahn wrote:

This breaks building on some platforms. Should be fixed with 
da116bd82c0a78d2022c34b56e45cf6e4f91eaed

https://github.com/llvm/llvm-project/pull/90338
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] da116bd - [Clang] Use ULL for std::max constant argument to fix build failure.

2024-05-15 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-05-15T15:37:52+01:00
New Revision: da116bd82c0a78d2022c34b56e45cf6e4f91eaed

URL: 
https://github.com/llvm/llvm-project/commit/da116bd82c0a78d2022c34b56e45cf6e4f91eaed
DIFF: 
https://github.com/llvm/llvm-project/commit/da116bd82c0a78d2022c34b56e45cf6e4f91eaed.diff

LOG: [Clang] Use ULL for std::max constant argument to fix build failure.

getKnownMinValue returns uint64_t, use ULL to make sure the second arg
is also 64 bit.

Added: 


Modified: 
clang/lib/CodeGen/Targets/Sparc.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/Targets/Sparc.cpp 
b/clang/lib/CodeGen/Targets/Sparc.cpp
index b82e9a69e1967..13e9550781d17 100644
--- a/clang/lib/CodeGen/Targets/Sparc.cpp
+++ b/clang/lib/CodeGen/Targets/Sparc.cpp
@@ -266,7 +266,7 @@ SparcV9ABIInfo::classifyType(QualType Ty, unsigned 
SizeLimit) const {
   // All structs, even empty ones, should take up a register argument slot,
   // so pin the minimum struct size to one bit.
   CB.pad(llvm::alignTo(
-  std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), 1UL), 64));
+  std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), 1ULL), 64));
 
   // Try to use the original type for coercion.
   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -0,0 +1,116 @@
+; RUN: opt < %s -passes='print' -debug-only=loop-accesses 
-disable-output 2>&1 | FileCheck %s

fhahn wrote:

Please also check the `access-info` report.

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -0,0 +1,116 @@
+; RUN: opt < %s -passes='print' -debug-only=loop-accesses 
-disable-output 2>&1 | FileCheck %s
+
+; REQUIRES: asserts
+
+
+define void @frexp_f64(ptr %in, ptr %out1, ptr %out2, i32 %N) {
+; CHECK: LAA: Allow to vectorize math function with write-only attribute: 
%call = tail call double @frexp
+entry:
+  %cmp4 = icmp sgt i32 %N, 0
+  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup
+
+for.body.preheader:
+  %wide.trip.count = zext nneg i32 %N to i64
+  br label %for.body
+
+for.cond.cleanup:
+  ret void
+
+for.body:
+  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, 
%for.body ]

fhahn wrote:

please strip `%indvars.` prefix to keep value names more concise 

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -0,0 +1,116 @@
+; RUN: opt < %s -passes='print' -debug-only=loop-accesses 
-disable-output 2>&1 | FileCheck %s
+
+; REQUIRES: asserts
+
+
+define void @frexp_f64(ptr %in, ptr %out1, ptr %out2, i32 %N) {
+; CHECK: LAA: Allow to vectorize math function with write-only attribute: 
%call = tail call double @frexp
+entry:
+  %cmp4 = icmp sgt i32 %N, 0

fhahn wrote:

this shouldn't be needed, please try to simplify the test cases

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -2477,6 +2493,15 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo 
*LI,
 
   // Save 'store' instructions. Abort if other instructions write to 
memory.
   if (I.mayWriteToMemory()) {
+// We can safety handle math functions that have vectorized
+// counterparts and have the memory write-only attribute set.

fhahn wrote:

I am not sure the reasoning here is correct; the writes could still alias with 
other accesses,


e.g. something like below would be considered as safe, but `%out2` could 
overlap with `%in` in a way that results in a backwards dependence at runtime.

```
define void @frexp_f64(ptr %in, ptr %out1, ptr %out2, i32 %N) {
entry:
  %cmp4 = icmp sgt i32 %N, 0
  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader:
  %wide.trip.count = zext nneg i32 %N to i64
  br label %for.body

for.cond.cleanup:
  ret void

for.body:
  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, 
%for.body ]
  %arrayidx = getelementptr inbounds double, ptr %in, i64 %indvars.iv
  %0 = load double, ptr %arrayidx, align 8
  %add.ptr = getelementptr inbounds i32, ptr %out2, i64 %indvars.iv
  %call = tail call double @frexp(double noundef %0, ptr noundef %add.ptr)
  store double %call, ptr %arrayidx, align 8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
}

declare double @frexp(double, ptr) #1
attributes #1 = { memory(argmem: write) }
```

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -0,0 +1,116 @@
+; RUN: opt < %s -passes='print' -debug-only=loop-accesses 
-disable-output 2>&1 | FileCheck %s
+
+; REQUIRES: asserts
+
+
+define void @frexp_f64(ptr %in, ptr %out1, ptr %out2, i32 %N) {
+; CHECK: LAA: Allow to vectorize math function with write-only attribute: 
%call = tail call double @frexp
+entry:
+  %cmp4 = icmp sgt i32 %N, 0
+  br i1 %cmp4, label %for.body.preheader, label %for.cond.cleanup
+
+for.body.preheader:
+  %wide.trip.count = zext nneg i32 %N to i64

fhahn wrote:

this shouldn't be needed, please try to simplify the test cases

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -2477,6 +2493,15 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo 
*LI,
 
   // Save 'store' instructions. Abort if other instructions write to 
memory.
   if (I.mayWriteToMemory()) {
+// We can safety handle math functions that have vectorized

fhahn wrote:

typo: safely

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits


@@ -2477,6 +2493,15 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo 
*LI,
 
   // Save 'store' instructions. Abort if other instructions write to 
memory.

fhahn wrote:

comment out of date 

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-05-14 Thread Florian Hahn via cfe-commits

https://github.com/fhahn requested changes to this pull request.


https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang backend] In AArch64's DataLayout, specify a minimum function alignment of 4. (PR #90702)

2024-05-02 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/90702

>From 4c312af7af5d0e419269c5b2304b0f898363bb85 Mon Sep 17 00:00:00 2001
From: Doug Wyatt 
Date: Tue, 30 Apr 2024 21:43:16 -0700
Subject: [PATCH 1/5] In AArch64's DataLayout, specify a minimum function
 alignment of 4. This addresses an issue where the explicit alignment of 2
 (for C++ ABI reasons) was being propagated to the back end and causing
 under-aligned functions (in special sections). (#90358)

---
 clang/lib/Basic/Targets/AArch64.cpp  | 12 ++--
 llvm/lib/IR/AutoUpgrade.cpp  |  8 
 llvm/lib/Target/AArch64/AArch64TargetMachine.cpp |  8 
 3 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Basic/Targets/AArch64.cpp 
b/clang/lib/Basic/Targets/AArch64.cpp
index c8d243a8fb7aea..a5dd803f636b90 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -1480,11 +1480,11 @@ AArch64leTargetInfo::AArch64leTargetInfo(const 
llvm::Triple ,
 void AArch64leTargetInfo::setDataLayout() {
   if (getTriple().isOSBinFormatMachO()) {
 if(getTriple().isArch32Bit())
-  resetDataLayout("e-m:o-p:32:32-i64:64-i128:128-n32:64-S128", "_");
+  resetDataLayout("e-m:o-p:32:32-Fn32-i64:64-i128:128-n32:64-S128", "_");
 else
-  resetDataLayout("e-m:o-i64:64-i128:128-n32:64-S128", "_");
+  resetDataLayout("e-m:o-Fn32-i64:64-i128:128-n32:64-S128", "_");
   } else
-resetDataLayout("e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
+
resetDataLayout("e-m:e-Fn32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
 }
 
 void AArch64leTargetInfo::getTargetDefines(const LangOptions ,
@@ -1507,7 +1507,7 @@ void AArch64beTargetInfo::getTargetDefines(const 
LangOptions ,
 
 void AArch64beTargetInfo::setDataLayout() {
   assert(!getTriple().isOSBinFormatMachO());
-  resetDataLayout("E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
+  resetDataLayout("E-m:e-Fn32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
 }
 
 WindowsARM64TargetInfo::WindowsARM64TargetInfo(const llvm::Triple ,
@@ -1530,8 +1530,8 @@ WindowsARM64TargetInfo::WindowsARM64TargetInfo(const 
llvm::Triple ,
 
 void WindowsARM64TargetInfo::setDataLayout() {
   resetDataLayout(Triple.isOSBinFormatMachO()
-  ? "e-m:o-i64:64-i128:128-n32:64-S128"
-  : "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128",
+  ? "e-m:o-Fn32-i64:64-i128:128-n32:64-S128"
+  : 
"e-m:w-p:64:64-Fn32-i32:32-i64:64-i128:128-n32:64-S128",
   Triple.isOSBinFormatMachO() ? "_" : "");
 }
 
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index 634b2dd5119e8d..eed946dc36580e 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -5387,6 +5387,14 @@ std::string llvm::UpgradeDataLayoutString(StringRef DL, 
StringRef TT) {
 return Res;
   }
 
+  // AArch64 data layout upgrades.
+  if (T.isAArch64()) {
+// Add "-Fn32"
+if (!DL.contains("-Fn32"))
+  Res.append("-Fn32");
+return Res;
+  }
+
   if (!T.isX86())
 return Res;
 
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp 
b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index 7ef78cbba352a5..4ff5fb94162a93 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -275,15 +275,15 @@ static std::string computeDataLayout(const Triple ,
  bool LittleEndian) {
   if (TT.isOSBinFormatMachO()) {
 if (TT.getArch() == Triple::aarch64_32)
-  return "e-m:o-p:32:32-i64:64-i128:128-n32:64-S128";
-return "e-m:o-i64:64-i128:128-n32:64-S128";
+  return "e-m:o-p:32:32-Fn32-i64:64-i128:128-n32:64-S128";
+return "e-m:o-Fn32-i64:64-i128:128-n32:64-S128";
   }
   if (TT.isOSBinFormatCOFF())
-return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
+return "e-m:w-p:64:64-Fn32-i32:32-i64:64-i128:128-n32:64-S128";
   std::string Endian = LittleEndian ? "e" : "E";
   std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : 
"";
   return Endian + "-m:e" + Ptr32 +
- "-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
+ "-Fn32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
 }
 
 static StringRef computeDefaultCPU(const Triple , StringRef CPU) {

>From 5a2b9e1b0444c52cd7f7cf9a3ddbb573403101a5 Mon Sep 17 00:00:00 2001
From: Doug Wyatt 
Date: Wed, 1 May 2024 07:27:11 -0700
Subject: [PATCH 2/5] Move "-Fn32" to the end of the data layout strings, to
 make the auto-upgrade simplest.

---
 clang/lib/Basic/Targets/AArch64.cpp  | 12 ++--
 llvm/lib/Target/AArch64/AArch64TargetMachine.cpp |  8 
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Basic/Targets/AArch64.cpp 
b/clang/lib/Basic/Targets/AArch64.cpp
index a5dd803f636b90..1a02520d7bd1f8 100644
--- 

[clang] [llvm] [clang backend] In AArch64's DataLayout, specify a minimum function alignment of 4. (PR #90702)

2024-05-02 Thread Florian Hahn via cfe-commits

https://github.com/fhahn approved this pull request.

LGTM, thanks!

https://github.com/llvm/llvm-project/pull/90702
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang backend] In AArch64's DataLayout, specify a minimum function alignment of 4. (PR #90702)

2024-05-02 Thread Florian Hahn via cfe-commits


@@ -1480,11 +1480,11 @@ AArch64leTargetInfo::AArch64leTargetInfo(const 
llvm::Triple ,
 void AArch64leTargetInfo::setDataLayout() {
   if (getTriple().isOSBinFormatMachO()) {
 if(getTriple().isArch32Bit())
-  resetDataLayout("e-m:o-p:32:32-i64:64-i128:128-n32:64-S128", "_");
+  resetDataLayout("e-m:o-p:32:32-i64:64-i128:128-n32:64-S128-Fn32", "_");

fhahn wrote:

Just to double check @TNorthover, is an alignment of 32 OK for arm64_32?

https://github.com/llvm/llvm-project/pull/90702
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [LV][LAA] Vectorize math lib calls with mem write-only attribute (PR #78432)

2024-04-30 Thread Florian Hahn via cfe-commits


@@ -0,0 +1,117 @@
+; RUN: opt < %s -mattr=+sve -vector-library=ArmPL 
-passes=inject-tli-mappings,loop-vectorize -debug-only=loop-accesses 
-disable-output 2>&1 | FileCheck %s
+
+; REQUIRES: asserts
+
+target triple = "aarch64-unknown-linux-gnu"
+
+; TODO: add mappings for frexp/frexpf

fhahn wrote:

Are you planning to add support to LoopVectorizationLegality as well? Would 
probably good to do this first 

https://github.com/llvm/llvm-project/pull/78432
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Reformat suspicious condition (PR #89923)

2024-04-30 Thread Florian Hahn via cfe-commits


@@ -1444,7 +1444,7 @@ struct PragmaWarningHandler : public PragmaHandler {
  .Case("once", PPCallbacks::PWS_Once)
  .Case("suppress", PPCallbacks::PWS_Suppress)
  .Default(-1);
-  if ((SpecifierValid = SpecifierInt != -1))
+  if (SpecifierValid = (SpecifierInt != -1))

fhahn wrote:

Unfortunately this introduces a new warning

```
/Users/florianhahn/projects/llvm-project/clang/lib/Lex/Pragma.cpp:1447:30: 
warning: using the result of an assignment as a condition without parentheses 
[-Wparentheses]
 1447 |   if (SpecifierValid = (SpecifierInt != -1))
  |   ~~~^~
/Users/florianhahn/projects/llvm-project/clang/lib/Lex/Pragma.cpp:1447:30: 
note: place parentheses around the assignment to silence this warning
 1447 |   if (SpecifierValid = (SpecifierInt != -1))
  |  ^
  |   ()
/Users/florianhahn/projects/llvm-project/clang/lib/Lex/Pragma.cpp:1447:30: 
note: use '==' to turn this assignment into an equality comparison
 1447 |   if (SpecifierValid = (SpecifierInt != -1))
  |  ^
  |  ==
1 warning generated.
```

We likely need to outer parenthesis as well

```
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index ced407355e00..2972ee2197e2 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -1444,7 +1444,7 @@ struct PragmaWarningHandler : public PragmaHandler {
  .Case("once", PPCallbacks::PWS_Once)
  .Case("suppress", PPCallbacks::PWS_Suppress)
  .Default(-1);
-  if (SpecifierValid = (SpecifierInt != -1))
+  if ((SpecifierValid = (SpecifierInt != -1)))
 Specifier =
 static_cast(SpecifierInt);
```

https://github.com/llvm/llvm-project/pull/89923
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Introduce `SemaCUDA` (PR #88559)

2024-04-13 Thread Florian Hahn via cfe-commits

fhahn wrote:

Is it possible that this broke this bot failing with the error below?

```
usr/local/clang-17.0.2/bin/clang++ -DGTEST_HAS_RTTI=0 -D_CINDEX_LIB_ -D_DEBUG 
-D_GLIBCXX_ASSERTIONS -D_LARGE_FILE_API -D__STDC_CONSTANT_MACROS 
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS 
-I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/build/tools/clang/tools/libclang
 
-I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/tools/libclang
 
-I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/include
 
-I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/build/tools/clang/include
 -I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/build/include 
-I/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/llvm/include
 -mcmodel=large -fPIC -Werror -Werror=date-time 
-Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter 
-Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic 
-Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough 
-Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor 
-Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion 
-Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color 
-ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual 
-Wno-nested-anon-types -O3 -DNDEBUG -std=c++17 -fPIC  -fno-exceptions 
-funwind-tables -fno-rtti -UNDEBUG -MD -MT 
tools/clang/tools/libclang/CMakeFiles/libclang.dir/CIndexCodeCompletion.cpp.o 
-MF 
tools/clang/tools/libclang/CMakeFiles/libclang.dir/CIndexCodeCompletion.cpp.o.d 
-o 
tools/clang/tools/libclang/CMakeFiles/libclang.dir/CIndexCodeCompletion.cpp.o 
-c 
/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/tools/libclang/CIndexCodeCompletion.cpp
In file included from 
/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/tools/libclang/CIndexCodeCompletion.cpp:30:
In file included from 
/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/include/clang/Sema/Sema.h:41:
/home/powerllvm/powerllvm_env/aix-ppc64/clang-ppc64-aix/llvm-project/clang/include/clang/Basic/Cuda.h:59:3:
 error: expected identifier
   59 |   SM_32,
  |   ^
/usr/include/sys/mac.h:79:15: note: expanded from macro 'SM_32'
   79 | #define SM_32 8 /* number of 32 bit words for markings */
  |   ^
```
 https://lab.llvm.org/buildbot/#/builders/214/builds/11887

https://github.com/llvm/llvm-project/pull/88559
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 1803d67 - [Driver] Add missing include of std::set.

2024-04-06 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-04-06T14:56:35+01:00
New Revision: 1803d675004bb512051d2df7e1ae3ea95692fc67

URL: 
https://github.com/llvm/llvm-project/commit/1803d675004bb512051d2df7e1ae3ea95692fc67
DIFF: 
https://github.com/llvm/llvm-project/commit/1803d675004bb512051d2df7e1ae3ea95692fc67.diff

LOG: [Driver] Add missing include of std::set.

4ddd4ed7fe15a added a use of std::set without including it. With some
recent libc++, std::set isn't included transitively causing build
failures.

Add explicit include.

Added: 


Modified: 
clang/lib/Driver/ToolChains/AIX.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/AIX.cpp 
b/clang/lib/Driver/ToolChains/AIX.cpp
index 7a62b0f9aec419..3f10888596a29a 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -17,6 +17,8 @@
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Support/Path.h"
 
+#include 
+
 using AIX = clang::driver::toolchains::AIX;
 using namespace clang::driver;
 using namespace clang::driver::tools;



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix tbaa.struct metadata for bitfields using big endian. (PR #87753)

2024-04-05 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/87753
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix tbaa.struct metadata for bitfields using big endian. (PR #87753)

2024-04-05 Thread Florian Hahn via cfe-commits

https://github.com/fhahn approved this pull request.

LGTM, thanks!

https://github.com/llvm/llvm-project/pull/87753
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [WebAssembly] Change the default linker for `wasm32-wasip2` (PR #84569)

2024-03-18 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/84569

>From 99ca952b60344d2ff683d05d8baa423aa11da83a Mon Sep 17 00:00:00 2001
From: Alex Crichton 
Date: Fri, 8 Mar 2024 13:36:18 -0800
Subject: [PATCH 1/2] [WebAssembly] Change the default linker for
 `wasm32-wasip2`

This commit changes the default linker in the WebAssembly toolchain for
the `wasm32-wasip2` target. This target is being added to the
WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the
Component Model by default, in contrast with the preexisting
`wasm32-wasi` target (in the process of being renamed to
`wasm32-wasip1`) which outputs a core WebAssembly module by default.

The `wasm-component-ld` project currently lives in my GitHub account at
https://github.com/alexcrichton/wasm-component-ld and isn't necessarily
"official" yet, but it's expected to continue to evolve as the
`wasm32-wasip2` target continues to shape up and evolve.
---
 clang/lib/Driver/ToolChains/WebAssembly.cpp | 6 ++
 clang/lib/Driver/ToolChains/WebAssembly.h   | 2 +-
 clang/test/Driver/wasm-toolchain.c  | 8 
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp 
b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index b8c2573d6265fb..a6c43c627f7206 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -221,6 +221,12 @@ WebAssembly::WebAssembly(const Driver , const 
llvm::Triple ,
   }
 }
 
+const char *WebAssembly::getDefaultLinker() const {
+  if (getOS() == "wasip2")
+return "wasm-component-ld";
+  return "wasm-ld";
+}
+
 bool WebAssembly::IsMathErrnoDefault() const { return false; }
 
 bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.h 
b/clang/lib/Driver/ToolChains/WebAssembly.h
index ae60f464c10818..76e0ca39bd748d 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.h
+++ b/clang/lib/Driver/ToolChains/WebAssembly.h
@@ -67,7 +67,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly final : public 
ToolChain {
llvm::opt::ArgStringList ) const override;
   SanitizerMask getSupportedSanitizers() const override;
 
-  const char *getDefaultLinker() const override { return "wasm-ld"; }
+  const char *getDefaultLinker() const override;
 
   CXXStdlibType GetDefaultCXXStdlibType() const override {
 return ToolChain::CST_Libcxx;
diff --git a/clang/test/Driver/wasm-toolchain.c 
b/clang/test/Driver/wasm-toolchain.c
index f950283ec42aa0..59cfdfb3cb113b 100644
--- a/clang/test/Driver/wasm-toolchain.c
+++ b/clang/test/Driver/wasm-toolchain.c
@@ -197,3 +197,11 @@
 // RUN: not %clang -### %s --target=wasm32-unknown-unknown 
--sysroot=%s/no-sysroot-there -fPIC -mno-mutable-globals %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=PIC_NO_MUTABLE_GLOBALS %s
 // PIC_NO_MUTABLE_GLOBALS: error: invalid argument '-fPIC' not allowed with 
'-mno-mutable-globals'
+
+// Test that `wasm32-wasip2` invokes the `wasm-component-ld` linker by default
+// instead of `wasm-ld`.
+
+// RUN: %clang -### -O2 --target=wasm32-wasip2 %s --sysroot /foo 2>&1 \
+// RUN:   | FileCheck -check-prefix=LINK_WASIP2 %s
+// LINK_WASIP2: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_WASIP2: wasm-component-ld{{.*}}" "-L/foo/lib/wasm32-wasip2" "crt1.o" 
"[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"

>From d1a1b22da3e1361a34f2d4fa8a2f9ef361f109ee Mon Sep 17 00:00:00 2001
From: Alex Crichton 
Date: Mon, 18 Mar 2024 12:23:23 -0700
Subject: [PATCH 2/2] Pass a path to `wasm-ld` to `wasm-component-ld`

This commit adds an explicit argument that's passed to
`wasm-component-ld` containing the location of `wasm-ld` itself. This
enables `wasm-component-ld` to avoid hunting around looking for it and
instead use the install that's paired with Clang itself.

Additionally this reinterprets the `-fuse-ld=lld` argument to explicitly
requesting the `wasm-ld` linker flavor, even on `wasm32-wasip2` targets.
---
 clang/lib/Driver/ToolChains/WebAssembly.cpp | 20 ++--
 clang/test/Driver/wasm-toolchain.c  | 16 
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp 
b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index a6c43c627f7206..61bf9d8aae72d2 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -44,8 +44,15 @@ std::string wasm::Linker::getLinkerPath(const ArgList ) 
const {
   llvm::sys::fs::can_execute(UseLinker))
 return std::string(UseLinker);
 
-  // Accept 'lld', and 'ld' as aliases for the default linker
-  if (UseLinker != "lld" && UseLinker != "ld")
+  // Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
+  // linker. Note that for `wasm32-wasip2` this overrides the default 
linker
+  // of `wasm-component-ld`.
+  if (UseLinker == "lld") 

[clang] [TBAA] Generate tbaa.struct single field with char tag for unions. (PR #84370)

2024-03-11 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/84370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Generate tbaa.struct single field with char tag for unions. (PR #84370)

2024-03-11 Thread Florian Hahn via cfe-commits

fhahn wrote:

> Do you have a testcase that shows how this goes wrong without this change ?

I am not 100% if the type-based aliasing rules allow aliasing a union access 
containing a float and a plain pointer to float, but I am thinking about 
something like the below, where we remove the first store to `f`, even though 
`f` may alias the union if the float is selected. Again, not sure if that's 
100% within the C/C++ type-based aliasing rules, but it is what I could 
construct easily. 

Irrespective of this it should show how the TBAA tags for the load of `u` 
incorrectly claims it is loading an int.


```
union U2 {
  int d;
  float f;
};

struct UnionMember2 {
  int p;
  U2 u;
};

void use(int, U2);
void copy12( UnionMember2 *a2, float *f) {
  *f = 1.0;
  UnionMember2 a1 = *a2;
  int p = a1.p;
  U2 u = a1.u;
  *f = 2.0;
  use(p, u);
}
```

https://clang.godbolt.org/z/Y6PPWEo69

https://github.com/llvm/llvm-project/pull/84370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Generate tbaa.struct single field with char tag for unions. (PR #84370)

2024-03-11 Thread Florian Hahn via cfe-commits

fhahn wrote:

ping :) (apologies for early ping, but I'd like to bump this as this is a 
potential mis-compile I think(

https://github.com/llvm/llvm-project/pull/84370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Add bail-out to skip tbaa generation to getTBAAStructInfo. (PR #84386)

2024-03-08 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/84386
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Add bail-out to skip tbaa generation to getTBAAStructInfo. (PR #84386)

2024-03-07 Thread Florian Hahn via cfe-commits

https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/84386

Without this bail out, we may generate fields with null nodes as tags are 
generated by using getTypeInfo which has the same bail out.

>From 36144a7b2a84b10711904c1a568bf02554f83296 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Thu, 7 Mar 2024 21:45:49 +
Subject: [PATCH] [TBAA] Add bail-out to skip tbaa generation to
 getTBAAStructInfo.

Without this bail out, we may generate fields with null nodes as tags
are generated by using getTypeInfo which has the same bail out.
---
 clang/lib/CodeGen/CodeGenTBAA.cpp | 3 +++
 clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp | 2 --
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 8a081612193978..1f07205a5af225 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -343,6 +343,9 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 
 llvm::MDNode *
 CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
+  if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
+return nullptr;
+
   const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
 
   if (llvm::MDNode *N = StructMetadataCache[Ty])
diff --git a/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp 
b/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp
index 931ff2476cd1bb..ce613b9d6b23f8 100644
--- a/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp
+++ b/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp
@@ -5,9 +5,7 @@
 // RUN: FileCheck %s
 //
 // Check that we do not create tbaa for instructions generated for copies.
-// FIXME: !tbaa.struct is generated with null node as tag.
 
-// CHECK: !tbaa.struct
 // CHECK-NOT: !tbaa
 
 struct A {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 167b90d - [TBAA] Add test showing tbaa.struct being generated with relaxed-alias.

2024-03-07 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-03-07T21:40:23Z
New Revision: 167b90d0401d0fe488195c7e3d6fc1edc8fc5d94

URL: 
https://github.com/llvm/llvm-project/commit/167b90d0401d0fe488195c7e3d6fc1edc8fc5d94
DIFF: 
https://github.com/llvm/llvm-project/commit/167b90d0401d0fe488195c7e3d6fc1edc8fc5d94.diff

LOG: [TBAA] Add test showing tbaa.struct being generated with relaxed-alias.

Add test showing that tbaa.struct is generated when using TSan with
relaxed-aliasing.

Added: 
clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp

Modified: 


Removed: 




diff  --git a/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp 
b/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp
new file mode 100644
index 00..931ff2476cd1bb
--- /dev/null
+++ b/clang/test/CodeGen/tbaa-struct-relaxed-aliasing-with-tsan.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - -O1 
-relaxed-aliasing -fsanitize=thread -disable-llvm-optzns %s | \
+// RUN: FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -new-struct-path-tbaa \
+// RUN: -emit-llvm -o - -O1 -relaxed-aliasing -fsanitize=thread 
-disable-llvm-optzns %s | \
+// RUN: FileCheck %s
+//
+// Check that we do not create tbaa for instructions generated for copies.
+// FIXME: !tbaa.struct is generated with null node as tag.
+
+// CHECK: !tbaa.struct
+// CHECK-NOT: !tbaa
+
+struct A {
+  short s;
+  int i;
+  char c;
+  int j;
+};
+
+void copyStruct(A *a1, A *a2) {
+  *a1 = *a2;
+}
+
+void copyInt(int *a, int *b) {
+  *a = *b;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Generate tbaa.struct single field with char tag for unions. (PR #84370)

2024-03-07 Thread Florian Hahn via cfe-commits

https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/84370

At the moment,distinct fields for each union member are generated. When copying 
a union, we don't know which union member is active, so there's no benefit from 
recording the different fields. It can result in converting tbaa.struct fields 
to incorrect tbaa nodes when extracting fields.

>From 379844ae4a78d13ca80a12ee34338a53723ffab4 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Thu, 7 Mar 2024 16:28:00 +
Subject: [PATCH] [TBAA] Generate tbaa.struct single field with char tag for
 unions.

At the moment,distinct fields for each union member are generated. When
copying a union, we don't know which union member is active, so there's
no benefit from recording the different fields. It can also result in
converting tbaa.struct fields to incorrect tbaa nodes when extracting
fields.
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 8 
 clang/test/CodeGen/tbaa-struct.cpp | 8 +++-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 8a081612193978..69fb1e11edc7dc 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -286,6 +286,14 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
   /* Things not handled yet include: C++ base classes, bitfields, */
 
   if (const RecordType *TTy = QTy->getAs()) {
+if (TTy->isUnionType()) {
+  uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
+  llvm::MDNode *TBAAType = getChar();
+  llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
+  Fields.push_back(
+  llvm::MDBuilder::TBAAStructField(BaseOffset, Size, TBAATag));
+  return true;
+}
 const RecordDecl *RD = TTy->getDecl()->getDefinition();
 if (RD->hasFlexibleArrayMember())
   return false;
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index 63e4097946448e..9b4b7415142d93 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -191,7 +191,7 @@ void copy12(UnionMember2 *a1, UnionMember2 *a2) {
 // (offset, size) = (0,1) char; (4,2) short; (8,4) int; (12,1) char; (16,4) 
int; (20,4) int
 // CHECK-OLD: [[TS2]] = !{i64 0, i64 1, !{{.*}}, i64 4, i64 2, !{{.*}}, i64 8, 
i64 4, !{{.*}}, i64 12, i64 1, !{{.*}}, i64 16, i64 4, {{.*}}, i64 20, i64 4, 
{{.*}}}
 // (offset, size) = (0,8) char; (0,2) char; (4,8) char
-// CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
+// CHECK-OLD: [[TS3]] = !{i64 0, i64 12, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS6]] = !{i64 0, i64 2, [[TAG_CHAR]], i64 2, i64 1, 
[[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
@@ -199,10 +199,8 @@ void copy12(UnionMember2 *a1, UnionMember2 *a2) {
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 // CHECK-OLD: [[TS7]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 3, i64 1, [[TAG_CHAR]], i64 4, 
i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]], i64 16, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS8]] = !{i64 0, i64 4, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE]]}
-// CHECK-OLD: [[TS9]] = !{i64 0, i64 8, [[TAG_DOUBLE]], i64 0, i64 4, 
[[TAG_FLOAT:!.+]], i64 8, i64 4, [[TAG_INT]]}
-// CHECK-OLD: [[TAG_FLOAT]]  = !{[[FLOAT:!.+]], [[FLOAT]], i64 0}
-// CHECK-OLD: [[FLOAT]] = !{!"float", [[CHAR]], i64 0}
-// CHECK-OLD: [[TS10]] = !{i64 0, i64 4, [[TAG_INT]], i64 8, i64 8, 
[[TAG_DOUBLE]], i64 8, i64 4, [[TAG_FLOAT:!.+]]}
+// CHECK-OLD: [[TS9]] = !{i64 0, i64 8, [[TAG_CHAR]], i64 8, i64 4, 
[[TAG_INT]]}
+// CHECK-OLD: [[TS10]] = !{i64 0, i64 4, [[TAG_INT]], i64 8, i64 8, 
[[TAG_CHAR]]}
 
 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 6f29941 - [TBAA] Add extra tests to copy structs with union members.

2024-03-07 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-03-07T18:57:09Z
New Revision: 6f299417769ade1635c91f974a8745e237cc9adf

URL: 
https://github.com/llvm/llvm-project/commit/6f299417769ade1635c91f974a8745e237cc9adf
DIFF: 
https://github.com/llvm/llvm-project/commit/6f299417769ade1635c91f974a8745e237cc9adf.diff

LOG: [TBAA] Add extra tests to copy structs with union members.

Adds extra test coverage for TBAA generation for copies of structs with
union members.

Added: 


Modified: 
clang/test/CodeGen/tbaa-struct.cpp

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index 883c982be26c8f..63e4097946448e 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -151,6 +151,38 @@ void copy10(NamedBitfields3 *a1, NamedBitfields3 *a2) {
   *a1 = *a2;
 }
 
+union U2 {
+  double d;
+  float f;
+};
+
+struct UnionMember1 {
+  U2 u;
+  int p;
+};
+
+void copy11(UnionMember1 *a1, UnionMember1 *a2) {
+// CHECK-LABEL: _Z6copy11P12UnionMember1S0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, 
i64 16, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TS9:!.*]]
+// CHECK-NEW-SAME: !tbaa [[TAG_UnionMember1:!.+]], !tbaa.struct
+  *a1 = *a2;
+}
+
+struct UnionMember2 {
+  int p;
+  U2 u;
+};
+
+void copy12(UnionMember2 *a1, UnionMember2 *a2) {
+// CHECK-LABEL: _Z6copy12P12UnionMember2S0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, 
i64 16, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TS10:!.*]]
+// CHECK-NEW-SAME: !tbaa [[TAG_UnionMember2:!.+]], !tbaa.struct
+
+  *a1 = *a2;
+}
+
 // CHECK-OLD: [[TS]] = !{i64 0, i64 2, !{{.*}}, i64 4, i64 4, !{{.*}}, i64 8, 
i64 1, !{{.*}}, i64 12, i64 4, !{{.*}}}
 // CHECK-OLD: [[CHAR:!.*]] = !{!"omnipotent char", !{{.*}}}
 // CHECK-OLD: [[TAG_INT:!.*]] = !{[[INT:!.*]], [[INT]], i64 0}
@@ -167,6 +199,10 @@ void copy10(NamedBitfields3 *a1, NamedBitfields3 *a2) {
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 // CHECK-OLD: [[TS7]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 3, i64 1, [[TAG_CHAR]], i64 4, 
i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]], i64 16, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS8]] = !{i64 0, i64 4, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE]]}
+// CHECK-OLD: [[TS9]] = !{i64 0, i64 8, [[TAG_DOUBLE]], i64 0, i64 4, 
[[TAG_FLOAT:!.+]], i64 8, i64 4, [[TAG_INT]]}
+// CHECK-OLD: [[TAG_FLOAT]]  = !{[[FLOAT:!.+]], [[FLOAT]], i64 0}
+// CHECK-OLD: [[FLOAT]] = !{!"float", [[CHAR]], i64 0}
+// CHECK-OLD: [[TS10]] = !{i64 0, i64 4, [[TAG_INT]], i64 8, i64 8, 
[[TAG_DOUBLE]], i64 8, i64 4, [[TAG_FLOAT:!.+]]}
 
 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}
@@ -188,3 +224,7 @@ void copy10(NamedBitfields3 *a1, NamedBitfields3 *a2) {
 // CHECK-NEW-DAG: [[TYPE_NamedBitfields2]] = !{[[TYPE_char]], i64 24, 
!"_ZTS15NamedBitfields2", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, 
i64 1, [[TYPE_char]], i64 2, i64 1, [[TYPE_int]], i64 3, i64 4, [[TYPE_int]], 
i64 3, i64 4, [[TYPE_char]], i64 4, i64 1, [[TYPE_double]], i64 8, i64 8, 
[[TYPE_int]], i64 16, i64 4}
 // CHECK-NEW-DAG: [[TAG_NamedBitfields3]] = !{[[TYPE_NamedBitfields3:!.+]], 
[[TYPE_NamedBitfields3]], i64 0, i64 16}
 // CHECK-NEW-DAG: [[TYPE_NamedBitfields3]] = !{[[TYPE_char]], i64 16, 
!"_ZTS15NamedBitfields3", [[TYPE_int]], i64 1, i64 4, [[TYPE_int]], i64 2, i64 
4, [[TYPE_double]], i64 8, i64 8}
+// CHECK-NEW-DAG: [[TAG_UnionMember1]] = !{[[TYPE_UnionMember1:!.+]], 
[[TYPE_UnionMember1]], i64 0, i64 16}
+// CHECK-NEW-DAG: [[TYPE_UnionMember1]] = !{[[TYPE_char]], i64 16, 
!"_ZTS12UnionMember1", [[TYPE_char]], i64 0, i64 8, [[TYPE_int]], i64 8, i64 4}
+// CHECK-NEW-DAG: [[TAG_UnionMember2]] = !{[[TYPE_UnionMember2:!.+]], 
[[TYPE_UnionMember2]], i64 0, i64 16}
+// CHECK-NEW-DAG: [[TYPE_UnionMember2]] = !{[[TYPE_char]], i64 16, 
!"_ZTS12UnionMember2", [[TYPE_int]], i64 0, i64 4, [[TYPE_char]], i64 8, i64 8}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-27 Thread Florian Hahn via cfe-commits

fhahn wrote:

Thanks!

https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-27 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-27 Thread Florian Hahn via cfe-commits

fhahn wrote:

> Whitespace is weird in a few places; otherwise looks fine.

Argh yes, should be fixed in the latest version. Still need to figure out how 
to make sure all changes are formatted once the branch contains merges (before 
the format checker complains and shows the commits)

https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-27 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/82922

>From 556fcefed9645aa0a0a965ff8f22d8accdf9eefc Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 25 Feb 2024 13:23:17 +
Subject: [PATCH 1/7] [TBAA] Skip all bitfields when generating !tbaa.struct
 metadata.

At the moment, clang generates what I believe are incorrect !tbaa.struct
fields for named bitfields. At the moment, the base type size is used
for named bifields (e.g. sizeof(int)) instead of the bifield width per
field. This results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields
from !tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones
(note that CollectFields has a TODO to support bitfields). As bitfields
specify their widths in bits, while !tbaa metadata uses bytes for sizes
and offsets, I don't think we would be able to generate correct metadata
for them in general.

If this understanding is correct, I can also extend the verifier to
check that !tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 2 +-
 clang/test/CodeGen/tbaa-struct.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index dc288bc3f6157a..43a1aee3d73823 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -298,7 +298,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 unsigned idx = 0;
 for (RecordDecl::field_iterator i = RD->field_begin(),
  e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+  if ((*i)->isZeroSize(Context) || (*i)->isBitField())
 continue;
   uint64_t Offset = BaseOffset +
 Layout.getFieldOffset(idx) / Context.getCharWidth();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..17c9d6bf6a7260 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -130,7 +130,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
-// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TS6]] = !{i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 

>From 32be3b7d944fc5da50add4b6fea551ba6c9d428c Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Mon, 26 Feb 2024 10:19:57 +
Subject: [PATCH 2/7] WIP use CGBitFieldInfo.

---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/CodeGen/CodeGenTBAA.cpp   | 33 +++--
 clang/lib/CodeGen/CodeGenTBAA.h |  4 +++-
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 95e457bef28ed3..ed8db055723024 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -397,7 +397,7 @@ CodeGenModule::CodeGenModule(ASTContext ,
   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
   (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
-TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
+TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts, 
getLangOpts(),
getCXXABI().getMangleContext()));
 
   // If debug info or coverage generation is enabled, create the CGDebugInfo
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 43a1aee3d73823..6a453bb1144582 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -14,6 +14,8 @@
 //
 
//===--===//
 
+#include "CGRecordLayout.h"
+#include "CodeGenTypes.h"
 #include "CodeGenTBAA.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
@@ -29,10 +31,10 @@
 using namespace clang;
 using namespace CodeGen;
 
-CodeGenTBAA::CodeGenTBAA(ASTContext , llvm::Module ,
+CodeGenTBAA::CodeGenTBAA(ASTContext , CodeGenTypes , llvm::Module 
,
  const CodeGenOptions ,
  const LangOptions , MangleContext )
-  : Context(Ctx), Module(M), CodeGenOpts(CGO),
+  : 

[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits


@@ -294,18 +297,49 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 return false;
 
 const ASTRecordLayout  = Context.getASTRecordLayout(RD);
+const CGRecordLayout  = CGTypes.getCGRecordLayout(RD);
 
 unsigned idx = 0;
-for (RecordDecl::field_iterator i = RD->field_begin(),
- e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
+ i != e;) {
+  if ((*i)->isZeroSize(Context)) {
+++i;
+++idx;
 continue;
-  uint64_t Offset = BaseOffset +
-Layout.getFieldOffset(idx) / Context.getCharWidth();
+  }
+
+  uint64_t Offset =
+  BaseOffset + Layout.getFieldOffset(idx) / Context.getCharWidth();
   QualType FieldQTy = i->getType();
+  // Create a single field for consecutive named bitfields using char as
+  // base type.
+  if ((*i)->isBitField() && !(*i)->isUnnamedBitfield()) {
+unsigned CurrentBitFieldSize = 0;
+unsigned CurrentBitFieldOffset = CGRL.getBitFieldInfo(*i).Offset;
+while (i != e && (*i)->isBitField() && !(*i)->isUnnamedBitfield()) {
+  const CGBitFieldInfo  = CGRL.getBitFieldInfo(*i);
+  if (CurrentBitFieldSize + CurrentBitFieldOffset != Info.Offset)
+break;
+  CurrentBitFieldSize += Info.Size;

fhahn wrote:

Thanks, updated the patch to add a field for the first bitfield in the run 
using its StorageSize, and skip bitfields where the offset in the run isn't 0 
(`Offset`). 

https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits


@@ -294,18 +297,49 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 return false;
 
 const ASTRecordLayout  = Context.getASTRecordLayout(RD);
+const CGRecordLayout  = CGTypes.getCGRecordLayout(RD);
 
 unsigned idx = 0;
-for (RecordDecl::field_iterator i = RD->field_begin(),
- e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
+ i != e;) {
+  if ((*i)->isZeroSize(Context)) {
+++i;
+++idx;
 continue;
-  uint64_t Offset = BaseOffset +
-Layout.getFieldOffset(idx) / Context.getCharWidth();
+  }
+
+  uint64_t Offset =
+  BaseOffset + Layout.getFieldOffset(idx) / Context.getCharWidth();
   QualType FieldQTy = i->getType();
+  // Create a single field for consecutive named bitfields using char as
+  // base type.
+  if ((*i)->isBitField() && !(*i)->isUnnamedBitfield()) {
+unsigned CurrentBitFieldSize = 0;
+unsigned CurrentBitFieldOffset = CGRL.getBitFieldInfo(*i).Offset;
+while (i != e && (*i)->isBitField() && !(*i)->isUnnamedBitfield()) {

fhahn wrote:

Yes, test showing that added in 54cff50791dec977feb0badb74919d97dff5b859, 
thanks!

https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/82922

>From 556fcefed9645aa0a0a965ff8f22d8accdf9eefc Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 25 Feb 2024 13:23:17 +
Subject: [PATCH 1/6] [TBAA] Skip all bitfields when generating !tbaa.struct
 metadata.

At the moment, clang generates what I believe are incorrect !tbaa.struct
fields for named bitfields. At the moment, the base type size is used
for named bifields (e.g. sizeof(int)) instead of the bifield width per
field. This results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields
from !tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones
(note that CollectFields has a TODO to support bitfields). As bitfields
specify their widths in bits, while !tbaa metadata uses bytes for sizes
and offsets, I don't think we would be able to generate correct metadata
for them in general.

If this understanding is correct, I can also extend the verifier to
check that !tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 2 +-
 clang/test/CodeGen/tbaa-struct.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index dc288bc3f6157a..43a1aee3d73823 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -298,7 +298,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 unsigned idx = 0;
 for (RecordDecl::field_iterator i = RD->field_begin(),
  e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+  if ((*i)->isZeroSize(Context) || (*i)->isBitField())
 continue;
   uint64_t Offset = BaseOffset +
 Layout.getFieldOffset(idx) / Context.getCharWidth();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..17c9d6bf6a7260 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -130,7 +130,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
-// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TS6]] = !{i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 

>From 32be3b7d944fc5da50add4b6fea551ba6c9d428c Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Mon, 26 Feb 2024 10:19:57 +
Subject: [PATCH 2/6] WIP use CGBitFieldInfo.

---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/CodeGen/CodeGenTBAA.cpp   | 33 +++--
 clang/lib/CodeGen/CodeGenTBAA.h |  4 +++-
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 95e457bef28ed3..ed8db055723024 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -397,7 +397,7 @@ CodeGenModule::CodeGenModule(ASTContext ,
   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
   (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
-TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
+TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts, 
getLangOpts(),
getCXXABI().getMangleContext()));
 
   // If debug info or coverage generation is enabled, create the CGDebugInfo
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 43a1aee3d73823..6a453bb1144582 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -14,6 +14,8 @@
 //
 
//===--===//
 
+#include "CGRecordLayout.h"
+#include "CodeGenTypes.h"
 #include "CodeGenTBAA.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
@@ -29,10 +31,10 @@
 using namespace clang;
 using namespace CodeGen;
 
-CodeGenTBAA::CodeGenTBAA(ASTContext , llvm::Module ,
+CodeGenTBAA::CodeGenTBAA(ASTContext , CodeGenTypes , llvm::Module 
,
  const CodeGenOptions ,
  const LangOptions , MangleContext )
-  : Context(Ctx), Module(M), CodeGenOpts(CGO),
+  : 

[clang] 54cff50 - [TBAA] Add !tbaa.struct test with unnamed bitfields.

2024-02-26 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-02-26T22:50:50Z
New Revision: 54cff50791dec977feb0badb74919d97dff5b859

URL: 
https://github.com/llvm/llvm-project/commit/54cff50791dec977feb0badb74919d97dff5b859
DIFF: 
https://github.com/llvm/llvm-project/commit/54cff50791dec977feb0badb74919d97dff5b859.diff

LOG: [TBAA] Add !tbaa.struct test with unnamed bitfields.

Extra tests with unnamed bitfields for
https://github.com/llvm/llvm-project/pull/82922.

Added: 


Modified: 
clang/test/CodeGen/tbaa-struct.cpp

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index e25fbc1a778103..28c7d396121af4 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -134,6 +134,23 @@ void copy9(NamedBitfields2 *a1, NamedBitfields2 *a2) {
   *a1 = *a2;
 }
 
+// Test with unnamed bitfield at the start and in between named ones..
+struct NamedBitfields3 {
+  unsigned : 11;
+  signed f0 : 9;
+  char : 2;
+  int f1 : 2;
+  double f2;
+};
+
+void copy10(NamedBitfields3 *a1, NamedBitfields3 *a2) {
+// CHECK-LABEL: _Z6copy10P15NamedBitfields3S0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, 
i64 16, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TS8:!.*]]
+// CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields3:!.+]], !tbaa.struct
+  *a1 = *a2;
+}
+
 // CHECK-OLD: [[TS]] = !{i64 0, i64 2, !{{.*}}, i64 4, i64 4, !{{.*}}, i64 8, 
i64 1, !{{.*}}, i64 12, i64 4, !{{.*}}}
 // CHECK-OLD: [[CHAR:!.*]] = !{!"omnipotent char", !{{.*}}}
 // CHECK-OLD: [[TAG_INT:!.*]] = !{[[INT:!.*]], [[INT]], i64 0}
@@ -149,6 +166,7 @@ void copy9(NamedBitfields2 *a1, NamedBitfields2 *a2) {
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 // CHECK-OLD: [[TS7]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 3, i64 4, [[TAG_INT]], i64 3, i64 
4, [[TAG_INT]], i64 4, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]], i64 
16, i64 4, [[TAG_INT]]}
+// CHECK-OLD: [[TS8]] = !{i64 1, i64 4, [[TAG_INT]], i64 2, i64 4, 
[[TAG_INT]], i64 8, i64 8, [[TAG_DOUBLE]]}
 
 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}
@@ -168,3 +186,5 @@ void copy9(NamedBitfields2 *a1, NamedBitfields2 *a2) {
 // CHECK-NEW-DAG: [[TYPE_double]] = !{[[TYPE_char]], i64 8, !"double"}
 // CHECK-NEW-DAG: [[TAG_NamedBitfields2]] = !{[[TYPE_NamedBitfields2:!.+]], 
[[TYPE_NamedBitfields2]], i64 0, i64 24}
 // CHECK-NEW-DAG: [[TYPE_NamedBitfields2]] = !{[[TYPE_char]], i64 24, 
!"_ZTS15NamedBitfields2", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, 
i64 1, [[TYPE_char]], i64 2, i64 1, [[TYPE_int]], i64 3, i64 4, [[TYPE_int]], 
i64 3, i64 4, [[TYPE_char]], i64 4, i64 1, [[TYPE_double]], i64 8, i64 8, 
[[TYPE_int]], i64 16, i64 4}
+// CHECK-NEW-DAG: [[TAG_NamedBitfields3]] = !{[[TYPE_NamedBitfields3:!.+]], 
[[TYPE_NamedBitfields3]], i64 0, i64 16}
+// CHECK-NEW-DAG: [[TYPE_NamedBitfields3]] = !{[[TYPE_char]], i64 16, 
!"_ZTS15NamedBitfields3", [[TYPE_int]], i64 1, i64 4, [[TYPE_int]], i64 2, i64 
4, [[TYPE_double]], i64 8, i64 8}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

fhahn wrote:

> This seems like it messes up the metadata in a different way: we have no 
> representation of the bitfield at all, so it looks like it's padding.
> 
> I think we want to treat multiple adjacent bitfields as a single "field". So 
> NamedBitfields from the testcase would have three fields in the LLVM TBAA 
> data: one "field" containing both bitfields, followed by a field for the 
> char, followed by a field for the double. You should be able to use 
> CGBitFieldInfo for this, I think?

Thanks, updated the patch as suggested.

https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Handle bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/82922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Skip all bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/82922

>From 556fcefed9645aa0a0a965ff8f22d8accdf9eefc Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 25 Feb 2024 13:23:17 +
Subject: [PATCH 1/5] [TBAA] Skip all bitfields when generating !tbaa.struct
 metadata.

At the moment, clang generates what I believe are incorrect !tbaa.struct
fields for named bitfields. At the moment, the base type size is used
for named bifields (e.g. sizeof(int)) instead of the bifield width per
field. This results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields
from !tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones
(note that CollectFields has a TODO to support bitfields). As bitfields
specify their widths in bits, while !tbaa metadata uses bytes for sizes
and offsets, I don't think we would be able to generate correct metadata
for them in general.

If this understanding is correct, I can also extend the verifier to
check that !tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 2 +-
 clang/test/CodeGen/tbaa-struct.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index dc288bc3f6157a..43a1aee3d73823 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -298,7 +298,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 unsigned idx = 0;
 for (RecordDecl::field_iterator i = RD->field_begin(),
  e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+  if ((*i)->isZeroSize(Context) || (*i)->isBitField())
 continue;
   uint64_t Offset = BaseOffset +
 Layout.getFieldOffset(idx) / Context.getCharWidth();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..17c9d6bf6a7260 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -130,7 +130,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
-// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TS6]] = !{i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 

>From 32be3b7d944fc5da50add4b6fea551ba6c9d428c Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Mon, 26 Feb 2024 10:19:57 +
Subject: [PATCH 2/5] WIP use CGBitFieldInfo.

---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/CodeGen/CodeGenTBAA.cpp   | 33 +++--
 clang/lib/CodeGen/CodeGenTBAA.h |  4 +++-
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 95e457bef28ed3..ed8db055723024 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -397,7 +397,7 @@ CodeGenModule::CodeGenModule(ASTContext ,
   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
   (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
-TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
+TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts, 
getLangOpts(),
getCXXABI().getMangleContext()));
 
   // If debug info or coverage generation is enabled, create the CGDebugInfo
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 43a1aee3d73823..6a453bb1144582 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -14,6 +14,8 @@
 //
 
//===--===//
 
+#include "CGRecordLayout.h"
+#include "CodeGenTypes.h"
 #include "CodeGenTBAA.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
@@ -29,10 +31,10 @@
 using namespace clang;
 using namespace CodeGen;
 
-CodeGenTBAA::CodeGenTBAA(ASTContext , llvm::Module ,
+CodeGenTBAA::CodeGenTBAA(ASTContext , CodeGenTypes , llvm::Module 
,
  const CodeGenOptions ,
  const LangOptions , MangleContext )
-  : Context(Ctx), Module(M), CodeGenOpts(CGO),
+  : 

[clang] f290c00 - [TBAA] Add additional bitfield tests.

2024-02-26 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-02-26T12:36:00Z
New Revision: f290c000d87bfc72a31b151dffa2d190596ebe91

URL: 
https://github.com/llvm/llvm-project/commit/f290c000d87bfc72a31b151dffa2d190596ebe91
DIFF: 
https://github.com/llvm/llvm-project/commit/f290c000d87bfc72a31b151dffa2d190596ebe91.diff

LOG: [TBAA] Add additional bitfield tests.

Additional test for https://github.com/llvm/llvm-project/pull/82922/.

Added: 


Modified: 
clang/test/CodeGen/tbaa-struct.cpp

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..e25fbc1a778103 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -109,8 +109,6 @@ struct NamedBitfields {
double f3;
 };
 
-NamedBitfields g;
-
 void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-LABEL: _Z5copy8P14NamedBitfieldsS0_
 // CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, 
i64 16, i1 false),
@@ -119,6 +117,23 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
   *a1 = *a2;
 }
 
+struct NamedBitfields2 {
+  char a, b, c;
+   signed f0 : 3;
+   unsigned f1 : 4;
+   char f2 : 7;
+   double f3;
+   unsigned f4 : 4;
+};
+
+void copy9(NamedBitfields2 *a1, NamedBitfields2 *a2) {
+// CHECK-LABEL: _Z5copy9P15NamedBitfields2S0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(24) %a1, ptr noundef nonnull align 8 dereferenceable(24) %a2, 
i64 24, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TS7:!.*]]
+// CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields2:!.+]], !tbaa.struct
+  *a1 = *a2;
+}
+
 // CHECK-OLD: [[TS]] = !{i64 0, i64 2, !{{.*}}, i64 4, i64 4, !{{.*}}, i64 8, 
i64 1, !{{.*}}, i64 12, i64 4, !{{.*}}}
 // CHECK-OLD: [[CHAR:!.*]] = !{!"omnipotent char", !{{.*}}}
 // CHECK-OLD: [[TAG_INT:!.*]] = !{[[INT:!.*]], [[INT]], i64 0}
@@ -133,6 +148,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
+// CHECK-OLD: [[TS7]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]], i64 3, i64 4, [[TAG_INT]], i64 3, i64 
4, [[TAG_INT]], i64 4, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE]], i64 
16, i64 4, [[TAG_INT]]}
 
 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}
@@ -150,3 +166,5 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-NEW-DAG: [[TAG_NamedBitfields]] = !{[[TYPE_NamedBitfields:!.+]], 
[[TYPE_NamedBitfields]], i64 0, i64 16}
 // CHECK-NEW-DAG: [[TYPE_NamedBitfields]] = !{[[TYPE_char]], i64 16, 
!"_ZTS14NamedBitfields", [[TYPE_int]], i64 0, i64 4, [[TYPE_int]], i64 1, i64 
4, [[TYPE_char]], i64 2, i64 1, [[TYPE_double:!.+]], i64 8, i64 8}
 // CHECK-NEW-DAG: [[TYPE_double]] = !{[[TYPE_char]], i64 8, !"double"}
+// CHECK-NEW-DAG: [[TAG_NamedBitfields2]] = !{[[TYPE_NamedBitfields2:!.+]], 
[[TYPE_NamedBitfields2]], i64 0, i64 24}
+// CHECK-NEW-DAG: [[TYPE_NamedBitfields2]] = !{[[TYPE_char]], i64 24, 
!"_ZTS15NamedBitfields2", [[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 1, 
i64 1, [[TYPE_char]], i64 2, i64 1, [[TYPE_int]], i64 3, i64 4, [[TYPE_int]], 
i64 3, i64 4, [[TYPE_char]], i64 4, i64 1, [[TYPE_double]], i64 8, i64 8, 
[[TYPE_int]], i64 16, i64 4}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Skip all bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-26 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/82922

>From 556fcefed9645aa0a0a965ff8f22d8accdf9eefc Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 25 Feb 2024 13:23:17 +
Subject: [PATCH 1/2] [TBAA] Skip all bitfields when generating !tbaa.struct
 metadata.

At the moment, clang generates what I believe are incorrect !tbaa.struct
fields for named bitfields. At the moment, the base type size is used
for named bifields (e.g. sizeof(int)) instead of the bifield width per
field. This results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields
from !tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones
(note that CollectFields has a TODO to support bitfields). As bitfields
specify their widths in bits, while !tbaa metadata uses bytes for sizes
and offsets, I don't think we would be able to generate correct metadata
for them in general.

If this understanding is correct, I can also extend the verifier to
check that !tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 2 +-
 clang/test/CodeGen/tbaa-struct.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index dc288bc3f6157a..43a1aee3d73823 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -298,7 +298,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 unsigned idx = 0;
 for (RecordDecl::field_iterator i = RD->field_begin(),
  e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+  if ((*i)->isZeroSize(Context) || (*i)->isBitField())
 continue;
   uint64_t Offset = BaseOffset +
 Layout.getFieldOffset(idx) / Context.getCharWidth();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..17c9d6bf6a7260 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -130,7 +130,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
-// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TS6]] = !{i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 

>From 32be3b7d944fc5da50add4b6fea551ba6c9d428c Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Mon, 26 Feb 2024 10:19:57 +
Subject: [PATCH 2/2] WIP use CGBitFieldInfo.

---
 clang/lib/CodeGen/CodeGenModule.cpp |  2 +-
 clang/lib/CodeGen/CodeGenTBAA.cpp   | 33 +++--
 clang/lib/CodeGen/CodeGenTBAA.h |  4 +++-
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 95e457bef28ed3..ed8db055723024 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -397,7 +397,7 @@ CodeGenModule::CodeGenModule(ASTContext ,
   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
   (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
-TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
+TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts, 
getLangOpts(),
getCXXABI().getMangleContext()));
 
   // If debug info or coverage generation is enabled, create the CGDebugInfo
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 43a1aee3d73823..6a453bb1144582 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -14,6 +14,8 @@
 //
 
//===--===//
 
+#include "CGRecordLayout.h"
+#include "CodeGenTypes.h"
 #include "CodeGenTBAA.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Attr.h"
@@ -29,10 +31,10 @@
 using namespace clang;
 using namespace CodeGen;
 
-CodeGenTBAA::CodeGenTBAA(ASTContext , llvm::Module ,
+CodeGenTBAA::CodeGenTBAA(ASTContext , CodeGenTypes , llvm::Module 
,
  const CodeGenOptions ,
  const LangOptions , MangleContext )
-  : Context(Ctx), Module(M), CodeGenOpts(CGO),
+  : 

[clang] [TBAA] Skip all bitfields when generating !tbaa.struct metadata. (PR #82922)

2024-02-25 Thread Florian Hahn via cfe-commits

https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/82922

At the moment, clang generates what I believe are incorrect !tbaa.struct fields 
for named bitfields. At the moment, the base type size is used for named 
bifields (e.g. sizeof(int)) instead of the bifield width per field. This 
results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields from 
!tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones (note 
that CollectFields has a TODO to support bitfields). As bitfields specify their 
widths in bits, while !tbaa metadata uses bytes for sizes and offsets, I don't 
think we would be able to generate correct metadata for them in general.

If this understanding is correct, I can also extend the verifier to check that 
!tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586

>From 556fcefed9645aa0a0a965ff8f22d8accdf9eefc Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 25 Feb 2024 13:23:17 +
Subject: [PATCH] [TBAA] Skip all bitfields when generating !tbaa.struct
 metadata.

At the moment, clang generates what I believe are incorrect !tbaa.struct
fields for named bitfields. At the moment, the base type size is used
for named bifields (e.g. sizeof(int)) instead of the bifield width per
field. This results in overalpping fields in !tbaa.struct metadata.

This causes incorrect results when extracting individual copied fields
from !tbaa.struct as in added in dc85719d5.

This patch fixes that by skipping all bitfields, not only unnamed ones
(note that CollectFields has a TODO to support bitfields). As bitfields
specify their widths in bits, while !tbaa metadata uses bytes for sizes
and offsets, I don't think we would be able to generate correct metadata
for them in general.

If this understanding is correct, I can also extend the verifier to
check that !tbaa.struct fields aren't overlapping.

Fixes https://github.com/llvm/llvm-project/issues/82586
---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 2 +-
 clang/test/CodeGen/tbaa-struct.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index dc288bc3f6157a..43a1aee3d73823 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -298,7 +298,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
 unsigned idx = 0;
 for (RecordDecl::field_iterator i = RD->field_begin(),
  e = RD->field_end(); i != e; ++i, ++idx) {
-  if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
+  if ((*i)->isZeroSize(Context) || (*i)->isBitField())
 continue;
   uint64_t Offset = BaseOffset +
 Layout.getFieldOffset(idx) / Context.getCharWidth();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index ff5521fcf3f604..17c9d6bf6a7260 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -130,7 +130,7 @@ void copy8(NamedBitfields *a1, NamedBitfields *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
-// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TS6]] = !{i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, 
[[TAG_DOUBLE:!.+]]}
 // CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 7110147 - [TBAA] Test for tbaa.struct creation for struct with named bitfields.

2024-02-25 Thread Florian Hahn via cfe-commits

Author: Florian Hahn
Date: 2024-02-25T13:17:08Z
New Revision: 711014714716753f791291ed6a152e00899469a3

URL: 
https://github.com/llvm/llvm-project/commit/711014714716753f791291ed6a152e00899469a3
DIFF: 
https://github.com/llvm/llvm-project/commit/711014714716753f791291ed6a152e00899469a3.diff

LOG: [TBAA] Test for tbaa.struct creation for struct with named bitfields.

Add test for tbaa.struct metadata creation for copies of a struct with
named bitfields.

Test for https://github.com/llvm/llvm-project/issues/82586.

Added: 


Modified: 
clang/test/CodeGen/tbaa-struct.cpp

Removed: 




diff  --git a/clang/test/CodeGen/tbaa-struct.cpp 
b/clang/test/CodeGen/tbaa-struct.cpp
index 47ccec3fb4162a..ff5521fcf3f604 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -102,6 +102,23 @@ void copy7(A *a1, AA *a2) {
   *a1 = *a2;
 }
 
+struct NamedBitfields {
+   signed f0 : 9;
+   unsigned f1 : 2;
+   char f2;
+   double f3;
+};
+
+NamedBitfields g;
+
+void copy8(NamedBitfields *a1, NamedBitfields *a2) {
+// CHECK-LABEL: _Z5copy8P14NamedBitfieldsS0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 
dereferenceable(16) %a1, ptr noundef nonnull align 8 dereferenceable(16) %a2, 
i64 16, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TS6:!.*]]
+// CHECK-NEW-SAME: !tbaa [[TAG_NamedBitfields:!.+]], !tbaa.struct
+  *a1 = *a2;
+}
+
 // CHECK-OLD: [[TS]] = !{i64 0, i64 2, !{{.*}}, i64 4, i64 4, !{{.*}}, i64 8, 
i64 1, !{{.*}}, i64 12, i64 4, !{{.*}}}
 // CHECK-OLD: [[CHAR:!.*]] = !{!"omnipotent char", !{{.*}}}
 // CHECK-OLD: [[TAG_INT:!.*]] = !{[[INT:!.*]], [[INT]], i64 0}
@@ -113,6 +130,9 @@ void copy7(A *a1, AA *a2) {
 // CHECK-OLD: [[TS3]] = !{i64 0, i64 8, !{{.*}}, i64 0, i64 2, !{{.*}}, i64 4, 
i64 8, !{{.*}}}
 // CHECK-OLD: [[TS4]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 1, i64 1, 
[[TAG_CHAR]], i64 2, i64 1, [[TAG_CHAR]]}
 // CHECK-OLD: [[TS5]] = !{i64 0, i64 1, [[TAG_CHAR]], i64 4, i64 1, 
[[TAG_CHAR]], i64 5, i64 1, [[TAG_CHAR]]}
+// CHECK-OLD: [[TS6]] = !{i64 0, i64 4, [[TAG_INT]], i64 1, i64 4, 
[[TAG_INT]], i64 2, i64 1, [[TAG_CHAR]], i64 8, i64 8, [[TAG_DOUBLE:!.+]]}
+// CHECK-OLD: [[TAG_DOUBLE]] = !{[[DOUBLE:!.+]], [[DOUBLE]], i64 0}
+// CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
 
 // CHECK-NEW-DAG: [[TYPE_char:!.*]] = !{{{.*}}, i64 1, !"omnipotent char"}
 // CHECK-NEW-DAG: [[TAG_char]] = !{[[TYPE_char]], [[TYPE_char]], i64 0, i64 0}
@@ -127,3 +147,6 @@ void copy7(A *a1, AA *a2) {
 // CHECK-NEW-DAG: [[TAG_C]] = !{[[TYPE_C]], [[TYPE_C]], i64 0, i64 3}
 // CHECK-NEW-DAG: [[TYPE_D:!.*]] = !{[[TYPE_char]], i64 6, !"_ZTS1D", 
[[TYPE_char]], i64 0, i64 1, [[TYPE_char]], i64 4, i64 1, [[TYPE_char]], i64 5, 
i64 1}
 // CHECK-NEW-DAG: [[TAG_D]] = !{[[TYPE_D]], [[TYPE_D]], i64 0, i64 6}
+// CHECK-NEW-DAG: [[TAG_NamedBitfields]] = !{[[TYPE_NamedBitfields:!.+]], 
[[TYPE_NamedBitfields]], i64 0, i64 16}
+// CHECK-NEW-DAG: [[TYPE_NamedBitfields]] = !{[[TYPE_char]], i64 16, 
!"_ZTS14NamedBitfields", [[TYPE_int]], i64 0, i64 4, [[TYPE_int]], i64 1, i64 
4, [[TYPE_char]], i64 2, i64 1, [[TYPE_double:!.+]], i64 8, i64 8}
+// CHECK-NEW-DAG: [[TYPE_double]] = !{[[TYPE_char]], i64 8, !"double"}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SROA] Use !tbaa instead of !tbaa.struct if op matches field. (PR #81289)

2024-02-16 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/81289
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SROA] Use !tbaa instead of !tbaa.struct if op matches field. (PR #81289)

2024-02-16 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/81289

>From e6de9f21b162c57dd09cb4de3147b7ab09ef8681 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Fri, 9 Feb 2024 13:29:19 +
Subject: [PATCH 1/5] [TBAA] Extract logic to use TBAA tag for field of
 !tbaa.struct (NFC).

---
 llvm/include/llvm/IR/Metadata.h   |  5 +
 llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp  | 14 ++
 .../Transforms/InstCombine/InstCombineCalls.cpp   | 15 +--
 3 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index db1f44fea3b459..6f23ac44dee968 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -844,6 +844,11 @@ struct AAMDNodes {
   /// together. Different from `merge`, where different locations should
   /// overlap each other, `concat` puts non-overlapping locations together.
   AAMDNodes concat(const AAMDNodes ) const;
+
+  /// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode.
+  /// If his AAMDNode has !tbaa.struct and \p AccessSize matches the size of 
the
+  /// field at offset 0, get the TBAA tag describing the accessed field.
+  AAMDNodes adjustForAccess(unsigned AccessSize);
 };
 
 // Specialize DenseMapInfo for AAMDNodes.
diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp 
b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
index e4dc1a867f6f0c..edc08cde686f1f 100644
--- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
@@ -817,3 +817,17 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) {
   ConstantAsMetadata::get(ConstantInt::get(PreviousSize->getType(), Len));
   return MDNode::get(MD->getContext(), NextNodes);
 }
+
+AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) {
+  AAMDNodes New = *this;
+  MDNode *M = New.TBAAStruct;
+  New.TBAAStruct = nullptr;
+  if (M && M->getNumOperands() == 3 && M->getOperand(0) &&
+  mdconst::hasa(M->getOperand(0)) &&
+  mdconst::extract(M->getOperand(0))->isZero() &&
+  M->getOperand(1) && mdconst::hasa(M->getOperand(1)) &&
+  mdconst::extract(M->getOperand(1))->getValue() == 
AccessSize &&
+  M->getOperand(2) && isa(M->getOperand(2)))
+New.TBAA = cast(M->getOperand(2));
+  return New;
+}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index ed5d44757fbeb6..56d1259e955196 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -172,20 +172,7 @@ Instruction 
*InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
 
   // If the memcpy has metadata describing the members, see if we can get the
   // TBAA tag describing our copy.
-  AAMDNodes AACopyMD = MI->getAAMetadata();
-
-  if (MDNode *M = AACopyMD.TBAAStruct) {
-AACopyMD.TBAAStruct = nullptr;
-if (M->getNumOperands() == 3 && M->getOperand(0) &&
-mdconst::hasa(M->getOperand(0)) &&
-mdconst::extract(M->getOperand(0))->isZero() &&
-M->getOperand(1) &&
-mdconst::hasa(M->getOperand(1)) &&
-mdconst::extract(M->getOperand(1))->getValue() ==
-Size &&
-M->getOperand(2) && isa(M->getOperand(2)))
-  AACopyMD.TBAA = cast(M->getOperand(2));
-  }
+  AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
 
   Value *Src = MI->getArgOperand(1);
   Value *Dest = MI->getArgOperand(0);

>From 99cf032dfabb21b820559bae61d2354e56336fdd Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Fri, 9 Feb 2024 16:25:32 +
Subject: [PATCH 2/5] [TBAA] Only clear TBAAStruct if field can be extracted.

Retain TBAAStruct if we fail to match the access to a single field. All
users at the moment use this when using the full size of the original
access. SROA also retains the original TBAAStruct when accessing parts
at offset 0.

Motivation for this and follow-on patches is to improve codegen for
libc++, where using memcpy limits optimizations, like vectorization for
code iteration over std::vector>:
https://godbolt.org/z/f3vqYos3c

Depends on https://github.com/llvm/llvm-project/pull/81284
---
 llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp   | 8 +---
 llvm/test/Transforms/InstCombine/struct-assign-tbaa.ll | 5 +++--
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp 
b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
index edc08cde686f1f..bfd70414c0340c 100644
--- a/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
@@ -821,13 +821,15 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) {
 AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) {
   AAMDNodes New = *this;
   MDNode *M = New.TBAAStruct;
-  New.TBAAStruct = nullptr;
   if (M && M->getNumOperands() == 3 && M->getOperand(0) &&
   

[clang] [compiler-rt] [llvm] [clang-tools-extra] [libc] [flang] [VPlan] Consistently use (Part, 0) for first lane scalar values (PR #80271)

2024-02-06 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80271

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH 1/3] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:  

[llvm] [clang-tools-extra] [flang] [clang] [Matrix] Convert column-vector ops feeding dot product to row-vectors. (PR #72647)

2024-02-06 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/72647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [llvm] [clang] [flang] [Matrix] Convert column-vector ops feeding dot product to row-vectors. (PR #72647)

2024-02-06 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/72647

>From 3dfe86782806f048b130d46afa6293712919f672 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Fri, 14 Apr 2023 14:33:57 +0100
Subject: [PATCH 1/2] [Matrix] Convert column-vector ops feeding dot product to
 row-vectors.

Generalize the logic used to convert column-vector ops to row-vectors to
support converting chains of operations.

A potential next step is to further generalize this to convert
column-vector ops to row-vector ops in general, not just for operands of
dot products. Dot-product handling would then be driven by the general
conversion, rather than the other way around.
---
 .../Scalar/LowerMatrixIntrinsics.cpp  | 51 ++-
 .../LowerMatrixIntrinsics/dot-product-int.ll  | 47 -
 2 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp 
b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index 72b9db1e73d73..c6bb43d3a78cf 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -1332,8 +1332,8 @@ class LowerMatrixIntrinsics {
 if (!IsIntVec && !FMF.allowReassoc())
   return;
 
-auto CanBeFlattened = [this](Value *Op) {
-  if (match(Op, m_BinOp()) && ShapeMap.find(Op) != ShapeMap.end())
+auto CanBeFlattened = [](Value *Op) {
+  if (match(Op, m_BinOp()))
 return true;
   return match(
   Op, m_OneUse(m_CombineOr(
@@ -1346,6 +1346,9 @@ class LowerMatrixIntrinsics {
 // the returned cost is < 0, the argument is cheaper to use in the
 // dot-product lowering.
 auto GetCostForArg = [this, ](Value *Op, unsigned N) {
+  if (ShapeMap.find(Op) == ShapeMap.end())
+return InstructionCost::getInvalid();
+
   if (!isa(Op))
 return InstructionCost(0);
 
@@ -1356,7 +1359,7 @@ class LowerMatrixIntrinsics {
 InstructionCost EmbedCost(0);
 // Roughly estimate the cost for embedding the columns into a vector.
 for (unsigned I = 1; I < N; ++I)
-  EmbedCost -=
+  EmbedCost +=
   TTI.getShuffleCost(TTI::SK_Splice, FixedVectorType::get(EltTy, 
1),
  std::nullopt, TTI::TCK_RecipThroughput);
 return EmbedCost;
@@ -1378,7 +1381,7 @@ class LowerMatrixIntrinsics {
 // vector.
 InstructionCost EmbedCost(0);
 for (unsigned I = 1; I < N; ++I)
-  EmbedCost +=
+  EmbedCost -=
   TTI.getShuffleCost(TTI::SK_Splice, FixedVectorType::get(EltTy, 
1),
  std::nullopt, TTI::TCK_RecipThroughput);
 return EmbedCost;
@@ -1391,7 +1394,26 @@ class LowerMatrixIntrinsics {
   return TTI.getMemoryOpCost(Instruction::Load, VecTy, Align(1), 0) -
  N * TTI.getMemoryOpCost(Instruction::Load, EltTy, Align(1), 0);
 };
-auto LHSCost = GetCostForArg(LHS, LShape.NumColumns);
+
+SmallPtrSet Seen;
+SmallVector WorkList;
+SmallVector ToFlatten;
+WorkList.push_back(LHS);
+InstructionCost LHSCost(0);
+while (!WorkList.empty()) {
+  Value *Op = WorkList.pop_back_val();
+  if (!Seen.insert(Op).second)
+continue;
+
+  InstructionCost OpCost = GetCostForArg(Op, LShape.NumColumns);
+  if (OpCost + LHSCost >= LHSCost)
+continue;
+
+  LHSCost += OpCost;
+  ToFlatten.push_back(Op);
+  if (auto *I = dyn_cast(Op))
+WorkList.append(I->op_begin(), I->op_end());
+}
 
 // We compare the costs of a vector.reduce.add to sequential add.
 int AddOpCode = IsIntVec ? Instruction::Add : Instruction::FAdd;
@@ -1412,16 +1434,16 @@ class LowerMatrixIntrinsics {
 FusedInsts.insert(MatMul);
 IRBuilder<> Builder(MatMul);
 auto FlattenArg = [, , ,
-   this](Value *Op) -> Value * {
+   this](Value *Op) {
   // Matmul must be the only user of loads because we don't use LowerLoad
   // for row vectors (LowerLoad results in scalar loads and shufflevectors
   // instead of single vector load).
   if (!CanBeFlattened(Op))
-return Op;
+return;
 
   if (match(Op, m_BinOp()) && ShapeMap.find(Op) != ShapeMap.end()) {
 ShapeMap[Op] = ShapeMap[Op].t();
-return Op;
+return;
   }
 
   FusedInsts.insert(cast(Op));
@@ -1432,16 +1454,19 @@ class LowerMatrixIntrinsics {
 auto *NewLoad = Builder.CreateLoad(Op->getType(), Arg);
 Op->replaceAllUsesWith(NewLoad);
 cast(Op)->eraseFromParent();
-return NewLoad;
+return;
   } else if (match(Op, m_Intrinsic(
m_Value(Arg {
 ToRemove.push_back(cast(Op));
-return Arg;
+Op->replaceAllUsesWith(Arg);
+return;
   }
-
-  return Op;
 };
-LHS = FlattenArg(LHS);
+
+for (auto *V : ToFlatten)
+  

[llvm] [clang] [clang-tools-extra] [Matrix] Convert column-vector ops feeding dot product to row-vectors. (PR #72647)

2024-02-06 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/72647

>From 3dfe86782806f048b130d46afa6293712919f672 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Fri, 14 Apr 2023 14:33:57 +0100
Subject: [PATCH 1/2] [Matrix] Convert column-vector ops feeding dot product to
 row-vectors.

Generalize the logic used to convert column-vector ops to row-vectors to
support converting chains of operations.

A potential next step is to further generalize this to convert
column-vector ops to row-vector ops in general, not just for operands of
dot products. Dot-product handling would then be driven by the general
conversion, rather than the other way around.
---
 .../Scalar/LowerMatrixIntrinsics.cpp  | 51 ++-
 .../LowerMatrixIntrinsics/dot-product-int.ll  | 47 -
 2 files changed, 47 insertions(+), 51 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp 
b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index 72b9db1e73d73d..c6bb43d3a78cf3 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -1332,8 +1332,8 @@ class LowerMatrixIntrinsics {
 if (!IsIntVec && !FMF.allowReassoc())
   return;
 
-auto CanBeFlattened = [this](Value *Op) {
-  if (match(Op, m_BinOp()) && ShapeMap.find(Op) != ShapeMap.end())
+auto CanBeFlattened = [](Value *Op) {
+  if (match(Op, m_BinOp()))
 return true;
   return match(
   Op, m_OneUse(m_CombineOr(
@@ -1346,6 +1346,9 @@ class LowerMatrixIntrinsics {
 // the returned cost is < 0, the argument is cheaper to use in the
 // dot-product lowering.
 auto GetCostForArg = [this, ](Value *Op, unsigned N) {
+  if (ShapeMap.find(Op) == ShapeMap.end())
+return InstructionCost::getInvalid();
+
   if (!isa(Op))
 return InstructionCost(0);
 
@@ -1356,7 +1359,7 @@ class LowerMatrixIntrinsics {
 InstructionCost EmbedCost(0);
 // Roughly estimate the cost for embedding the columns into a vector.
 for (unsigned I = 1; I < N; ++I)
-  EmbedCost -=
+  EmbedCost +=
   TTI.getShuffleCost(TTI::SK_Splice, FixedVectorType::get(EltTy, 
1),
  std::nullopt, TTI::TCK_RecipThroughput);
 return EmbedCost;
@@ -1378,7 +1381,7 @@ class LowerMatrixIntrinsics {
 // vector.
 InstructionCost EmbedCost(0);
 for (unsigned I = 1; I < N; ++I)
-  EmbedCost +=
+  EmbedCost -=
   TTI.getShuffleCost(TTI::SK_Splice, FixedVectorType::get(EltTy, 
1),
  std::nullopt, TTI::TCK_RecipThroughput);
 return EmbedCost;
@@ -1391,7 +1394,26 @@ class LowerMatrixIntrinsics {
   return TTI.getMemoryOpCost(Instruction::Load, VecTy, Align(1), 0) -
  N * TTI.getMemoryOpCost(Instruction::Load, EltTy, Align(1), 0);
 };
-auto LHSCost = GetCostForArg(LHS, LShape.NumColumns);
+
+SmallPtrSet Seen;
+SmallVector WorkList;
+SmallVector ToFlatten;
+WorkList.push_back(LHS);
+InstructionCost LHSCost(0);
+while (!WorkList.empty()) {
+  Value *Op = WorkList.pop_back_val();
+  if (!Seen.insert(Op).second)
+continue;
+
+  InstructionCost OpCost = GetCostForArg(Op, LShape.NumColumns);
+  if (OpCost + LHSCost >= LHSCost)
+continue;
+
+  LHSCost += OpCost;
+  ToFlatten.push_back(Op);
+  if (auto *I = dyn_cast(Op))
+WorkList.append(I->op_begin(), I->op_end());
+}
 
 // We compare the costs of a vector.reduce.add to sequential add.
 int AddOpCode = IsIntVec ? Instruction::Add : Instruction::FAdd;
@@ -1412,16 +1434,16 @@ class LowerMatrixIntrinsics {
 FusedInsts.insert(MatMul);
 IRBuilder<> Builder(MatMul);
 auto FlattenArg = [, , ,
-   this](Value *Op) -> Value * {
+   this](Value *Op) {
   // Matmul must be the only user of loads because we don't use LowerLoad
   // for row vectors (LowerLoad results in scalar loads and shufflevectors
   // instead of single vector load).
   if (!CanBeFlattened(Op))
-return Op;
+return;
 
   if (match(Op, m_BinOp()) && ShapeMap.find(Op) != ShapeMap.end()) {
 ShapeMap[Op] = ShapeMap[Op].t();
-return Op;
+return;
   }
 
   FusedInsts.insert(cast(Op));
@@ -1432,16 +1454,19 @@ class LowerMatrixIntrinsics {
 auto *NewLoad = Builder.CreateLoad(Op->getType(), Arg);
 Op->replaceAllUsesWith(NewLoad);
 cast(Op)->eraseFromParent();
-return NewLoad;
+return;
   } else if (match(Op, m_Intrinsic(
m_Value(Arg {
 ToRemove.push_back(cast(Op));
-return Arg;
+Op->replaceAllUsesWith(Arg);
+return;
   }
-
-  return Op;
 };
-LHS = FlattenArg(LHS);
+
+for (auto *V : ToFlatten)
+  

[llvm] [mlir] [clang] [libcxx] [clang-tools-extra] [flang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -453,16 +453,17 @@ Value 
*VPInstruction::generateInstruction(VPTransformState ,
 else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
   TrackingVH ReductionStartValue =
   RdxDesc.getRecurrenceStartValue();
-  ReducedPartRdx = createAnyOfOp(Builder, ReductionStartValue, RK,
- ReducedPartRdx, RdxPart);
+  ReducedPartRdx = Builder.CreateOr(ReducedPartRdx, RdxPart);
 } else
   ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, 
RdxPart);
   }
 }
 
 // Create the reduction after the loop. Note that inloop reductions create
 // the target reduction in the loop using a Reduction recipe.
-if (State.VF.isVector() && !PhiR->isInLoop()) {
+if ((State.VF.isVector() ||
+ RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) &&
+!PhiR->isInLoop()) {
   ReducedPartRdx =
   createTargetReduction(Builder, RdxDesc, ReducedPartRdx, OrigPhi);

fhahn wrote:

Yes, but this will need a bit of additional refactoring, in particular how 
createAndCollectMergePhiForReduction looks up the reduction result value and 
when ComputeReductionResult VPInstructions are created.



https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [mlir] [clang-tools-extra] [clang] [libcxx] [flang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -9110,6 +9111,41 @@ void 
LoopVectorizationPlanner::adjustRecipesForReductions(
   continue;
 
 const RecurrenceDescriptor  = PhiR->getRecurrenceDescriptor();
+// Adjust AnyOf reductions; replace the reduction phi for the selected 
value

fhahn wrote:

Agreed, probably best as followup?


https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [libcxx] [flang] [mlir] [clang-tools-extra] [llvm] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -9142,7 +9178,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
 // then extend the loop exit value to enable InstCombine to evaluate the
 // entire expression in the smaller type.
 Type *PhiTy = PhiR->getStartValue()->getLiveInIRValue()->getType();
-if (MinVF.isVector() && PhiTy != RdxDesc.getRecurrenceType()) {
+if (MinVF.isVector() && PhiTy != RdxDesc.getRecurrenceType() &&
+!RecurrenceDescriptor::isAnyOfRecurrenceKind(
+RdxDesc.getRecurrenceKind())) {

fhahn wrote:

We only reach this path now because we adjust PhiR's start value to a bool. It 
requires more than plain truncates as below, so I think it's probably worth to 
keep it separate. I *think* it also needs handling before introducing selects 
for tail-folding; otherwise those selects would also need updating.

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [libcxx] [mlir] [llvm] [clang] [flang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -9110,6 +9111,41 @@ void 
LoopVectorizationPlanner::adjustRecipesForReductions(
   continue;
 
 const RecurrenceDescriptor  = PhiR->getRecurrenceDescriptor();
+// Adjust AnyOf reductions; replace the reduction phi for the selected 
value
+// with a boolean reduction phi node to check if the condition is true in
+// any iteration. The final value is selected by the final
+// ComputeReductionResult.
+if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
+RdxDesc.getRecurrenceKind())) {
+  auto *Select = cast(*find_if(PhiR->users(), [](VPUser *U) {
+return isa(U) ||
+   (isa(U) &&
+cast(U)->getUnderlyingInstr()->getOpcode() 
==
+Instruction::Select);
+  }));

fhahn wrote:

`cast` should already assert to check for non-null.

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [libcxx] [mlir] [clang] [llvm] [flang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -1079,16 +1070,13 @@ Value *llvm::createAnyOfTargetReduction(IRBuilderBase 
, Value *Src,
 NewVal = SI->getTrueValue();
   }
 
-  // Create a splat vector with the new value and compare this to the vector
-  // we want to reduce.
-  ElementCount EC = cast(Src->getType())->getElementCount();
-  Value *Right = Builder.CreateVectorSplat(EC, InitVal);
-  Value *Cmp =
-  Builder.CreateCmp(CmpInst::ICMP_NE, Src, Right, "rdx.select.cmp");
-
   // If any predicate is true it means that we want to select the new value.
-  Cmp = Builder.CreateOrReduce(Cmp);
-  return Builder.CreateSelect(Cmp, NewVal, InitVal, "rdx.select");
+  Value *AnyOf =
+  Src->getType()->isVectorTy() ? Builder.CreateOrReduce(Src) : Src;
+  // The compares in the loop may yield poison, which propagates through the
+  // bitwise ORs. Freeze it here before the condition is used.
+  AnyOf = Builder.CreateFreeze(AnyOf);

fhahn wrote:

> Similar to a plain "result |= value[i]" OR reduction,

Freeze won't be needed in that case; if there's already a binary OR in the 
input, poison from the compare gets already propagated. It is only needed when 
converting from the `select` form (which doesn't propagate poison from the 
condition to its result)

Yes, but this will need a bit of additional refactoring, in particular how 
`createAndCollectMergePhiForReduction` looks up the reduction result value and 
when `ComputeReductionResult` VPInstructions are created.

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [mlir] [libcxx] [flang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -9110,6 +9111,41 @@ void 
LoopVectorizationPlanner::adjustRecipesForReductions(
   continue;
 
 const RecurrenceDescriptor  = PhiR->getRecurrenceDescriptor();
+// Adjust AnyOf reductions; replace the reduction phi for the selected 
value
+// with a boolean reduction phi node to check if the condition is true in
+// any iteration. The final value is selected by the final
+// ComputeReductionResult.
+if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
+RdxDesc.getRecurrenceKind())) {
+  auto *Select = cast(*find_if(PhiR->users(), [](VPUser *U) {
+return isa(U) ||
+   (isa(U) &&
+cast(U)->getUnderlyingInstr()->getOpcode() 
==
+Instruction::Select);
+  }));
+  VPValue *Cmp = Select->getOperand(0);
+  // If the compare is checking the reduction PHI node, adjust it to check
+  // the start value.

fhahn wrote:

At the moment, AnyOf reduction are also formed for code like 


```
define i32 @select_i32_from_icmp_same_inputs(i32 %a, i32 %b, i64 %n) {  
 
entry:
  br label %for.body

for.body:  ; preds = %entry, %for.body
  %0 = phi i64 [ 0, %entry ], [ %4, %for.body ]
  %1 = phi i32 [ %a, %entry ], [ %3, %for.body ]
  %2 = icmp eq i32 %1, 3
  %3 = select i1 %2, i32 %1, i32 %b
  %4 = add nuw nsw i64 %0, 1
  %5 = icmp eq i64 %4, %n
  br i1 %5, label %exit, label %for.body

exit: ; preds = %for.body
  ret i32 %3
}
```

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [mlir] [flang] [libcxx] [llvm] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -452,16 +452,17 @@ Value 
*VPInstruction::generateInstruction(VPTransformState ,
 else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
   TrackingVH ReductionStartValue =
   RdxDesc.getRecurrenceStartValue();

fhahn wrote:

Folded into the CreateBinaryOp above, thanks!

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libcxx] [flang] [mlir] [clang] [llvm] [clang-tools-extra] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -453,16 +453,17 @@ Value 
*VPInstruction::generateInstruction(VPTransformState ,
 else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
   TrackingVH ReductionStartValue =
   RdxDesc.getRecurrenceStartValue();
-  ReducedPartRdx = createAnyOfOp(Builder, ReductionStartValue, RK,
- ReducedPartRdx, RdxPart);
+  ReducedPartRdx = Builder.CreateOr(ReducedPartRdx, RdxPart);

fhahn wrote:

Adjusted, thanks!

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libcxx] [llvm] [flang] [mlir] [clang-tools-extra] [clang] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -9110,6 +9111,41 @@ void 
LoopVectorizationPlanner::adjustRecipesForReductions(
   continue;
 
 const RecurrenceDescriptor  = PhiR->getRecurrenceDescriptor();
+// Adjust AnyOf reductions; replace the reduction phi for the selected 
value
+// with a boolean reduction phi node to check if the condition is true in
+// any iteration. The final value is selected by the final
+// ComputeReductionResult.
+if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
+RdxDesc.getRecurrenceKind())) {
+  auto *Select = cast(*find_if(PhiR->users(), [](VPUser *U) {
+return isa(U) ||
+   (isa(U) &&
+cast(U)->getUnderlyingInstr()->getOpcode() 
==
+Instruction::Select);
+  }));
+  VPValue *Cmp = Select->getOperand(0);
+  // If the compare is checking the reduction PHI node, adjust it to check
+  // the start value.
+  if (VPRecipeBase *CmpR = Cmp->getDefiningRecipe()) {
+for (unsigned I = 0; I != CmpR->getNumOperands(); ++I)
+  if (CmpR->getOperand(I) == PhiR)
+CmpR->setOperand(I, PhiR->getStartValue());
+  }
+  VPBuilder::InsertPointGuard Guard(Builder);
+  Builder.setInsertPoint(Select);
+
+  // If the true value of the select is the reduction phi, the new value is
+  // selected if the negated condition is true in any iteration.
+  if (Select->getOperand(1) == PhiR)
+Cmp = Builder.createNot(Cmp);

fhahn wrote:

Yes, this could be handled like that, but I think then we would need to support 
both patterns here and also in codegen, so negating seems simpler (and the 
negation should be removable by instcombine). Left as is for now, but happy to 
adjust if needed. But then it should probably be modeled as AllOf directly in 
the reduction descriptor.

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [flang] [clang] [libcxx] [llvm] [mlir] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-05 Thread Florian Hahn via cfe-commits


@@ -7486,7 +7486,8 @@ static void createAndCollectMergePhiForReduction(
   auto *PhiR = cast(RedResult->getOperand(0));
   const RecurrenceDescriptor  = PhiR->getRecurrenceDescriptor();
 
-  TrackingVH ReductionStartValue = RdxDesc.getRecurrenceStartValue();
+  TrackingVH ReductionStartValue =

fhahn wrote:

Not needed any longer, removed, thanks!

https://github.com/llvm/llvm-project/pull/78304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[mlir] [clang] [flang] [libcxx] [llvm] [clang-tools-extra] [LV] Improve AnyOf reduction codegen. (PR #78304)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/78304

>From 9846f970b6b394ccc3af25b92f238377a8ae7807 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Sun, 14 Jan 2024 18:06:36 +
Subject: [PATCH 1/2] [LV] Improve AnyOf reduction codegen.

Update AnyOf reduction code generation to only keep track of the AnyOf
property in a boolean vector in the loop, only selecting either the new
or start value in the middle block.

This fixes the #62565, as now there aren't multiple uses of the
start/new values.

Fixes https://github.com/llvm/llvm-project/issues/62565
---
 .../include/llvm/Transforms/Utils/LoopUtils.h |   9 --
 llvm/lib/Transforms/Utils/LoopUtils.cpp   |  24 +--
 .../Vectorize/LoopVectorizationPlanner.h  |   1 +
 .../Transforms/Vectorize/LoopVectorize.cpp|  44 +-
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |   7 +-
 .../LoopVectorize/AArch64/sve-select-cmp.ll   |  38 ++---
 .../RISCV/select-cmp-reduction.ll | 120 ++
 .../LoopVectorize/select-cmp-predicated.ll|  29 ++--
 .../Transforms/LoopVectorize/select-cmp.ll| 146 +-
 ...tion-start-value-may-be-undef-or-poison.ll |  43 +++---
 10 files changed, 218 insertions(+), 243 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/LoopUtils.h 
b/llvm/include/llvm/Transforms/Utils/LoopUtils.h
index 5a1385d01d8e4..3bad7b616d9d7 100644
--- a/llvm/include/llvm/Transforms/Utils/LoopUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/LoopUtils.h
@@ -363,15 +363,6 @@ Intrinsic::ID getMinMaxReductionIntrinsicOp(RecurKind RK);
 /// Returns the comparison predicate used when expanding a min/max reduction.
 CmpInst::Predicate getMinMaxReductionPredicate(RecurKind RK);
 
-/// See RecurrenceDescriptor::isAnyOfPattern for a description of the pattern 
we
-/// are trying to match. In this pattern, we are only ever selecting between 
two
-/// values: 1) an initial start value \p StartVal of the reduction PHI, and 2) 
a
-/// loop invariant value. If any of lane value in \p Left, \p Right is not 
equal
-/// to \p StartVal, select the loop invariant value. This is done by selecting
-/// \p Right iff \p Left is equal to \p StartVal.
-Value *createAnyOfOp(IRBuilderBase , Value *StartVal, RecurKind RK,
- Value *Left, Value *Right);
-
 /// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
 /// The Builder's fast-math-flags must be set to propagate the expected values.
 Value *createMinMaxOp(IRBuilderBase , RecurKind RK, Value *Left,
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp 
b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 59485126b280a..c0582fb7d7e15 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -962,15 +962,6 @@ CmpInst::Predicate 
llvm::getMinMaxReductionPredicate(RecurKind RK) {
   }
 }
 
-Value *llvm::createAnyOfOp(IRBuilderBase , Value *StartVal,
-   RecurKind RK, Value *Left, Value *Right) {
-  if (auto VTy = dyn_cast(Left->getType()))
-StartVal = Builder.CreateVectorSplat(VTy->getElementCount(), StartVal);
-  Value *Cmp =
-  Builder.CreateCmp(CmpInst::ICMP_NE, Left, StartVal, "rdx.select.cmp");
-  return Builder.CreateSelect(Cmp, Left, Right, "rdx.select");
-}
-
 Value *llvm::createMinMaxOp(IRBuilderBase , RecurKind RK, Value *Left,
 Value *Right) {
   Type *Ty = Left->getType();
@@ -1079,16 +1070,13 @@ Value *llvm::createAnyOfTargetReduction(IRBuilderBase 
, Value *Src,
 NewVal = SI->getTrueValue();
   }
 
-  // Create a splat vector with the new value and compare this to the vector
-  // we want to reduce.
-  ElementCount EC = cast(Src->getType())->getElementCount();
-  Value *Right = Builder.CreateVectorSplat(EC, InitVal);
-  Value *Cmp =
-  Builder.CreateCmp(CmpInst::ICMP_NE, Src, Right, "rdx.select.cmp");
-
   // If any predicate is true it means that we want to select the new value.
-  Cmp = Builder.CreateOrReduce(Cmp);
-  return Builder.CreateSelect(Cmp, NewVal, InitVal, "rdx.select");
+  Value *AnyOf =
+  Src->getType()->isVectorTy() ? Builder.CreateOrReduce(Src) : Src;
+  // The compares in the loop may yield poison, which propagates through the
+  // bitwise ORs. Freeze it here before the condition is used.
+  AnyOf = Builder.CreateFreeze(AnyOf);
+  return Builder.CreateSelect(AnyOf, NewVal, InitVal, "rdx.select");
 }
 
 Value *llvm::createSimpleTargetReduction(IRBuilderBase , Value *Src,
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h 
b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index a7ebf78e54ceb..9d3ef5b96c72f 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -68,6 +68,7 @@ class VPBuilder {
 public:
   VPBuilder() = default;
   VPBuilder(VPBasicBlock *InsertBB) { setInsertPoint(InsertBB); }
+  VPBuilder(VPRecipeBase *InsertPt) { setInsertPoint(InsertPt); }
 
   /// 

[llvm] [clang] [clang-tools-extra] [VPlan] Consistently use (Part, 0) for first lane scalar values (PR #80271)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80271

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH 1/3] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:  

[llvm] [clang] [clang-tools-extra] [VPlan] Consistently use (Part, 0) for first lane scalar values (PR #80271)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80271

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH 1/2] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:  

[clang] [llvm] [clang-tools-extra] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn closed https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang-tools-extra] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

fhahn wrote:

> The only observable changes to existing tests involve cse'ing IV steps from 
> replicate regions when unrolling w/o vectorizing, iinm. Is there some other 
> scenario worth testing?

I wasn't able to come up with a different test with vectorizing.

https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang-tools-extra] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn edited https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang-tools-extra] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits


@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {

fhahn wrote:

Done in 3444240540b0b36902dc8b9b11339b8969f7bca9, thanks!

https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang-tools-extra] [clang] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits


@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });

fhahn wrote:

Split off to 693647902076034e7c81f380d1b937a65aef8ae6, thanks!

https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits


@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 

fhahn wrote:

Added back, thanks!

https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang-tools-extra] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits


@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;

fhahn wrote:

Done, thanks!

https://github.com/llvm/llvm-project/pull/80269
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [llvm] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80269

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH 1/2] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:  

[clang] [clang-tools-extra] [llvm] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80269

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:   

[llvm] [clang-tools-extra] [clang] [VPlan] Update VPInst::onlyFirstLaneUsed to check users. (PR #80269)

2024-02-03 Thread Florian Hahn via cfe-commits

https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/80269

>From f4dabdfaa66744ecfca4c0a57472a357db9715d9 Mon Sep 17 00:00:00 2001
From: Florian Hahn 
Date: Wed, 31 Jan 2024 14:02:38 +
Subject: [PATCH] [VPlan] Update VPInst::onlyFirstLaneUsed to check users.

A VPInstruction only has its first lane used if all users use its first
lane only. Use vputils::onlyFirstLaneUsed to continue checking the
recipe's users to handle more cases.

Besides allowing additional introduction of scalar steps when
interleaving in some cases, this also enables using an Add VPInstruction
to model the increment.
---
 llvm/lib/Transforms/Vectorize/VPlan.cpp   |   4 +-
 llvm/lib/Transforms/Vectorize/VPlan.h |  20 +--
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  18 ++
 .../pr45679-fold-tail-by-masking.ll   | 160 +-
 .../tail-folding-vectorization-factor-1.ll|  66 
 5 files changed, 129 insertions(+), 139 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp 
b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index a1bd6aaf0e551..1ca2cfef447f6 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1397,9 +1397,9 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) 
{
   assignSlot(Def);
 }
 
-bool vputils::onlyFirstLaneUsed(VPValue *Def) {
+bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
-[Def](VPUser *U) { return U->onlyFirstLaneUsed(Def); });
+[Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
 }
 
 bool vputils::onlyFirstPartUsed(VPValue *Def) {
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h 
b/llvm/lib/Transforms/Vectorize/VPlan.h
index 20792cb9ac7c1..30dc521947b3b 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1256,23 +1256,7 @@ class VPInstruction : public VPRecipeWithIRFlags {
 }
   }
 
-  /// Returns true if the recipe only uses the first lane of operand \p Op.
-  bool onlyFirstLaneUsed(const VPValue *Op) const override {
-assert(is_contained(operands(), Op) &&
-   "Op must be an operand of the recipe");
-if (getOperand(0) != Op)
-  return false;
-switch (getOpcode()) {
-default:
-  return false;
-case VPInstruction::ActiveLaneMask:
-case VPInstruction::CalculateTripCountMinusVF:
-case VPInstruction::CanonicalIVIncrementForPart:
-case VPInstruction::BranchOnCount:
-  return true;
-};
-llvm_unreachable("switch should return");
-  }
+  bool onlyFirstLaneUsed(const VPValue *Op) const override;
 
   /// Returns true if the recipe only uses the first part of operand \p Op.
   bool onlyFirstPartUsed(const VPValue *Op) const override {
@@ -3385,7 +3369,7 @@ class VPlanSlp {
 namespace vputils {
 
 /// Returns true if only the first lane of \p Def is used.
-bool onlyFirstLaneUsed(VPValue *Def);
+bool onlyFirstLaneUsed(const VPValue *Def);
 
 /// Returns true if only the first part of \p Def is used.
 bool onlyFirstPartUsed(VPValue *Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp 
b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e51184b0dd1fe..21b8d1eb77bf9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -515,6 +515,24 @@ void VPInstruction::execute(VPTransformState ) {
 State.set(this, GeneratedValue, Part);
   }
 }
+bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
+  assert(is_contained(operands(), Op) && "Op must be an operand of the 
recipe");
+  if (Instruction::isBinaryOp(getOpcode()))
+return vputils::onlyFirstLaneUsed(this);
+
+  switch (getOpcode()) {
+  default:
+return false;
+  case Instruction::ICmp:
+return vputils::onlyFirstLaneUsed(this);
+  case VPInstruction::ActiveLaneMask:
+  case VPInstruction::CalculateTripCountMinusVF:
+  case VPInstruction::CanonicalIVIncrementForPart:
+  case VPInstruction::BranchOnCount:
+return getOperand(0) == Op;
+  };
+  llvm_unreachable("switch should return");
+}
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPInstruction::dump() const {
diff --git a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll 
b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
index e81fb66239bd4..f05ec30619c5d 100644
--- a/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr45679-fold-tail-by-masking.ll
@@ -67,7 +67,7 @@ define void @pr45679(ptr %A) optsize {
 ; CHECK-NEXT:store i32 13, ptr [[ARRAYIDX]], align 1
 ; CHECK-NEXT:[[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
 ; CHECK-NEXT:[[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], 14
-; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP2:![0-9]+]]
+; CHECK-NEXT:br i1 [[COND]], label [[EXIT]], label [[LOOP]], !llvm.loop 
[[LOOP3:![0-9]+]]
 ; CHECK:   

[clang] [libc] [llvm] [clang-tools-extra] [flang] [SLP] Initial vectorization of non-power-of-2 ops. (PR #77790)

2024-02-02 Thread Florian Hahn via cfe-commits


@@ -6987,6 +7024,17 @@ class BoUpSLP::ShuffleCostEstimator : public 
BaseShuffleAnalysis {
 auto *VecTy = FixedVectorType::get(VL.front()->getType(), VL.size());
 InstructionCost GatherCost = 0;
 SmallVector Gathers(VL.begin(), VL.end());
+auto ComputeGatherCost = [&]() {
+  return all_of(Gathers, UndefValue::classof)
+ ? TTI::TCC_Free
+ : R.getGatherCost(Gathers, !Root && VL.equals(Gathers));
+};

fhahn wrote:

Indeed not needed in the latest version, removed thanks!

https://github.com/llvm/llvm-project/pull/77790
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   4   5   >