[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-04-25 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff d26002ac38ee1dea32053e7d12f1bb5dc420ac2d 
2fa7299db558c2a953f641ffe760369f31a98b3d -- 
clang/test/CodeGen/RISCV/riscv-inline-asm.c 
clang/test/CodeGen/attr-counted-by.c clang/test/CodeGenCXX/atomicinit.cpp 
clang/test/CodeGenCXX/auto-var-init.cpp 
clang/test/Profile/c-unreachable-after-switch.c 
compiler-rt/test/profile/Linux/counter_promo_for.c 
compiler-rt/test/profile/Linux/counter_promo_while.c 
llvm/lib/Analysis/ConstantFolding.cpp
``





View the diff from clang-format here.


``diff
diff --git a/compiler-rt/test/profile/Linux/counter_promo_for.c 
b/compiler-rt/test/profile/Linux/counter_promo_for.c
index aa77e6084b..d843510697 100644
--- a/compiler-rt/test/profile/Linux/counter_promo_for.c
+++ b/compiler-rt/test/profile/Linux/counter_promo_for.c
@@ -18,30 +18,30 @@ int g;
 __attribute__((noinline)) void bar(int i) { g += i; }
 
 __attribute__((noinline)) void foo(int n, int N) {
-// PROMO-LABEL: @foo
-// PROMO: load{{.*}}@__profc_foo{{.*}} 24){{.*}}
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 24){{.*}}
-// PROMO: load{{.*}}@__profc_foo, align
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo, align
-// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// PROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
-//
-// NOPROMO-LABEL: @foo
-// NOPROMO: load{{.*}}@__profc_foo, align
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo, align
-// NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// NOPROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  // PROMO-LABEL: @foo
+  // PROMO: load{{.*}}@__profc_foo{{.*}} 24){{.*}}
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 24){{.*}}
+  // PROMO: load{{.*}}@__profc_foo, align
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo, align
+  // PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // PROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  //
+  // NOPROMO-LABEL: @foo
+  // NOPROMO: load{{.*}}@__profc_foo, align
+  // NOPROMO-NEXT: add
+  // NOPROMO-NEXT: store{{.*}}@__profc_foo, align
+  // NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // NOPROMO-NEXT: add
+  // NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // NOPROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  // NOPROMO-NEXT: add
+  // NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
   int i;
   for (i = 0; i < N; i++) {
 if (i < n + 1)
diff --git a/compiler-rt/test/profile/Linux/counter_promo_while.c 
b/compiler-rt/test/profile/Linux/counter_promo_while.c
index c6ea3a7282..e0a9ee3c24 100644
--- a/compiler-rt/test/profile/Linux/counter_promo_while.c
+++ b/compiler-rt/test/profile/Linux/counter_promo_while.c
@@ -16,27 +16,27 @@
 int g;
 __attribute__((noinline)) void bar(int i) { g += i; }
 __attribute__((noinline)) void foo(int n, int N) {
-// PROMO-LABEL: @foo
-// PROMO: load{{.*}}@__profc_foo, align
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo, align
-// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
-// PROMO-NEXT: add
-// PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
-//
-// NOPROMO-LABEL: @foo
-// NOPROMO: load{{.*}}@__profc_foo, align
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo, align
-// NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
-// NOPROMO: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
-// NOPROMO-NEXT: add
-// NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  // PROMO-LABEL: @foo
+  // PROMO: load{{.*}}@__profc_foo, align
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo, align
+  // PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // PROMO-NEXT: load{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  // PROMO-NEXT: add
+  // PROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 16){{.*}}
+  //
+  // NOPROMO-LABEL: @foo
+  // NOPROMO: load{{.*}}@__profc_foo, align
+  // NOPROMO-NEXT: add
+  // NOPROMO-NEXT: store{{.*}}@__profc_foo, align
+  // NOPROMO: load{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // NOPROMO-NEXT: add
+  // NOPROMO-NEXT: store{{.*}}@__profc_foo{{.*}} 8){{.*}}
+  // NOPROMO: load{{.*}}@__pr

[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-04-28 Thread Arthur Eubanks via cfe-commits


@@ -944,43 +943,18 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
 return ConstantExpr::getIntToPtr(C, ResTy);
   }
 
-  // Otherwise form a regular getelementptr. Recompute the indices so that
-  // we eliminate over-indexing of the notional static type array bounds.
-  // This makes it easy to determine if the getelementptr is "inbounds".
-
-  // For GEPs of GlobalValues, use the value type, otherwise use an i8 GEP.
-  if (auto *GV = dyn_cast(Ptr))
-SrcElemTy = GV->getValueType();
-  else
-SrcElemTy = Type::getInt8Ty(Ptr->getContext());
-
-  if (!SrcElemTy->isSized())
-return nullptr;
-
-  Type *ElemTy = SrcElemTy;
-  SmallVector Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
-  if (Offset != 0)
-return nullptr;
-
-  // Try to add additional zero indices to reach the desired result element
-  // type.
-  // TODO: Should we avoid extra zero indices if ResElemTy can't be reached and
-  // we'll have to insert a bitcast anyway?
-  while (ElemTy != ResElemTy) {
-Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0);
-if (!NextTy)
-  break;
-
-Indices.push_back(APInt::getZero(isa(ElemTy) ? 32 : BitWidth));
-ElemTy = NextTy;
+  // Try to infer inbounds for GEPs of globals.
+  if (!InBounds && Offset.isNonNegative()) {

aeubanks wrote:

is there a test for the case where `!Offset.isNonNegative()`?

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


[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-04-28 Thread Arthur Eubanks via cfe-commits

aeubanks wrote:

btw we're still looking into a performance regression caused by #68882 that 
still repros with LLVM head, even after the SROA enhancements. this patch looks 
good, but can we hold off a bit on submitting this?

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


[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-04-28 Thread Nikita Popov via cfe-commits


@@ -944,43 +943,18 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
 return ConstantExpr::getIntToPtr(C, ResTy);
   }
 
-  // Otherwise form a regular getelementptr. Recompute the indices so that
-  // we eliminate over-indexing of the notional static type array bounds.
-  // This makes it easy to determine if the getelementptr is "inbounds".
-
-  // For GEPs of GlobalValues, use the value type, otherwise use an i8 GEP.
-  if (auto *GV = dyn_cast(Ptr))
-SrcElemTy = GV->getValueType();
-  else
-SrcElemTy = Type::getInt8Ty(Ptr->getContext());
-
-  if (!SrcElemTy->isSized())
-return nullptr;
-
-  Type *ElemTy = SrcElemTy;
-  SmallVector Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
-  if (Offset != 0)
-return nullptr;
-
-  // Try to add additional zero indices to reach the desired result element
-  // type.
-  // TODO: Should we avoid extra zero indices if ResElemTy can't be reached and
-  // we'll have to insert a bitcast anyway?
-  while (ElemTy != ResElemTy) {
-Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0);
-if (!NextTy)
-  break;
-
-Indices.push_back(APInt::getZero(isa(ElemTy) ? 32 : BitWidth));
-ElemTy = NextTy;
+  // Try to infer inbounds for GEPs of globals.
+  if (!InBounds && Offset.isNonNegative()) {

nikic wrote:

https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/GlobalOpt/large-element-size.ll
 gets an incorrect inbounds without the check.

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


[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-04-28 Thread Nikita Popov via cfe-commits

nikic wrote:

> btw we're still looking into a performance regression caused by #68882 that 
> still repros with LLVM head, even after the SROA enhancements. this patch 
> looks good, but can we hold off a bit on submitting this?

Sure!

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


[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-05-16 Thread Arthur Eubanks via cfe-commits

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

we've resolved the performance regression from the previous patch internally, 
thanks for waiting!

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


[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)

2024-05-20 Thread Nikita Popov via cfe-commits

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