[flang] [clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2024-01-25 Thread Steven Johnson via cfe-commits

steven-johnson wrote:

FYI: this *appears* to have injected a failure into Halide builds (based on a 
bisect) -- there are some bitcode files that we build with LLVM's top-of-tree 
Clang then load later on to use for codegen, and these bitcode files are now 
missing certain type definitions that are required. (Does this change affect 
optimizer behavior at all?)

I'm investigating from the Halide side to see if I can confirm or deny that 
this is the injection point, but any suggestions as to how this could cause 
such an effect would be welcomed.


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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-12-20 Thread Nikita Popov via cfe-commits


@@ -560,14 +560,15 @@ define i32 @test28() nounwind  {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:[[ORIENTATIONS:%.*]] = alloca [1 x [1 x %struct.x]], align 8
 ; CHECK-NEXT:[[T3:%.*]] = call i32 @puts(ptr noundef nonnull 
dereferenceable(1) @.str) #[[ATTR0]]
+; CHECK-NEXT:[[T45:%.*]] = getelementptr inbounds i8, ptr 
[[ORIENTATIONS]], i64 1
 ; CHECK-NEXT:br label [[BB10:%.*]]
 ; CHECK:   bb10:
 ; CHECK-NEXT:[[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 
[[INDVAR_NEXT:%.*]], [[BB10]] ]
 ; CHECK-NEXT:[[T12_REC:%.*]] = xor i32 [[INDVAR]], -1
 ; CHECK-NEXT:[[TMP0:%.*]] = sext i32 [[T12_REC]] to i64
-; CHECK-NEXT:[[T12:%.*]] = getelementptr inbounds [1 x [1 x %struct.x]], 
ptr [[ORIENTATIONS]], i64 1, i64 0, i64 [[TMP0]]
+; CHECK-NEXT:[[T12:%.*]] = getelementptr inbounds [[STRUCT_X:%.*]], ptr 
[[T45]], i64 [[TMP0]]
 ; CHECK-NEXT:[[T16:%.*]] = call i32 (ptr, ...) @printf(ptr noundef nonnull 
dereferenceable(1) @.str1, ptr nonnull [[T12]]) #[[ATTR0]]
-; CHECK-NEXT:[[T84:%.*]] = icmp eq i32 [[INDVAR]], 0
+; CHECK-NEXT:[[T84:%.*]] = icmp eq ptr [[T12]], [[ORIENTATIONS]]

nikic wrote:

(I still plan to fix this remaining regression, but I don't think it needs to 
block this PR.)

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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-12-20 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-clang

Author: Nikita Popov (nikic)


Changes

This patch canonicalizes getelementptr instructions with constant indices to 
use the `i8` source element type. This makes it easier for optimizations to 
recognize that two GEPs are identical, because they don't need to see past many 
different ways to express the same offset.

This is a first step towards 
https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699. 
This is limited to constant GEPs only for now, as they have a clear canonical 
form, while we're not yet sure how exactly to deal with variable indices.

The test llvm/test/Transforms/PhaseOrdering/switch_with_geps.ll gives two 
representative examples of the kind of optimization improvement we expect from 
this change. In the first test SimplifyCFG can now realize that all switch 
branches are actually the same. In the second test it can convert it into 
simple arithmetic. These are representative of common enum optimization 
failures we see in Rust.

---

Patch is 775.99 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/68882.diff


174 Files Affected:

- (modified) clang/test/CodeGen/PowerPC/builtins-ppc-pair-mma.c (+5-5) 
- (modified) clang/test/CodeGen/aarch64-ls64-inline-asm.c (+9-9) 
- (modified) clang/test/CodeGen/attr-arm-sve-vector-bits-bitcast.c (+24-24) 
- (modified) clang/test/CodeGen/attr-riscv-rvv-vector-bits-bitcast.c (+12-12) 
- (modified) clang/test/CodeGen/cleanup-destslot-simple.c (+2-2) 
- (modified) clang/test/CodeGen/hexagon-brev-ld-ptr-incdec.c (+3-3) 
- (modified) clang/test/CodeGen/ms-intrinsics.c (+6-6) 
- (modified) clang/test/CodeGen/nofpclass.c (+4-4) 
- (modified) clang/test/CodeGen/union-tbaa1.c (+2-2) 
- (modified) clang/test/CodeGenCXX/RelativeVTablesABI/dynamic-cast.cpp (+1-1) 
- (modified) clang/test/CodeGenCXX/RelativeVTablesABI/type-info.cpp (+1-3) 
- (modified) clang/test/CodeGenCXX/microsoft-abi-dynamic-cast.cpp (+6-6) 
- (modified) clang/test/CodeGenCXX/microsoft-abi-typeid.cpp (+1-1) 
- (modified) clang/test/CodeGenObjC/arc-foreach.m (+2-2) 
- (modified) clang/test/CodeGenObjCXX/arc-cxx11-init-list.mm (+1-1) 
- (modified) clang/test/Headers/__clang_hip_math.hip (+12-12) 
- (modified) clang/test/OpenMP/bug57757.cpp (+6-6) 
- (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+9) 
- (modified) llvm/test/Analysis/BasicAA/featuretest.ll (+3-3) 
- (modified) llvm/test/CodeGen/AMDGPU/vector-alloca-bitcast.ll (+6-6) 
- (modified) llvm/test/CodeGen/BPF/preserve-static-offset/load-inline.ll (+2-2) 
- (modified) llvm/test/CodeGen/BPF/preserve-static-offset/load-unroll-inline.ll 
(+2-2) 
- (modified) llvm/test/CodeGen/BPF/preserve-static-offset/load-unroll.ll (+4-4) 
- (modified) 
llvm/test/CodeGen/BPF/preserve-static-offset/store-unroll-inline.ll (+2-2) 
- (modified) llvm/test/CodeGen/Hexagon/autohvx/vector-align-tbaa.ll (+27-27) 
- (modified) llvm/test/Transforms/Coroutines/coro-async.ll (+1-1) 
- (modified) llvm/test/Transforms/Coroutines/coro-retcon-alloca-opaque-ptr.ll 
(+1-1) 
- (modified) llvm/test/Transforms/Coroutines/coro-retcon-alloca.ll (+1-1) 
- (modified) llvm/test/Transforms/Coroutines/coro-retcon-once-value.ll (+3-3) 
- (modified) llvm/test/Transforms/Coroutines/coro-retcon-resume-values.ll 
(+4-4) 
- (modified) llvm/test/Transforms/Coroutines/coro-swifterror.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/2007-03-25-BadShiftMask.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/2009-01-08-AlignAlloca.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/2009-02-20-InstCombine-SROA.ll 
(+16-16) 
- (modified) llvm/test/Transforms/InstCombine/X86/x86-addsub-inseltpoison.ll 
(+1-1) 
- (modified) llvm/test/Transforms/InstCombine/X86/x86-addsub.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/add3.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/array.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/assume.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/cast_phi.ll (+2-2) 
- (modified) llvm/test/Transforms/InstCombine/catchswitch-phi.ll (+2-2) 
- (modified) llvm/test/Transforms/InstCombine/compare-alloca.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/extractvalue.ll (+2-2) 
- (modified) llvm/test/Transforms/InstCombine/gep-addrspace.ll (+1-1) 
- (modified) 
llvm/test/Transforms/InstCombine/gep-canonicalize-constant-indices.ll (+9-9) 
- (modified) llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll 
(+3-3) 
- (modified) llvm/test/Transforms/InstCombine/gep-custom-dl.ll (+2-2) 
- (modified) llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll 
(+7-7) 
- (modified) llvm/test/Transforms/InstCombine/gep-vector-indices.ll (+4-4) 
- (modified) llvm/test/Transforms/InstCombine/gep-vector.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/gepphigep.ll (+1-1) 
- (modified) 

[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-12-20 Thread Nikita Popov via cfe-commits

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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-12-20 Thread Nikita Popov via cfe-commits

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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-11-28 Thread Björn Pettersson via cfe-commits


@@ -2282,6 +2282,15 @@ Instruction 
*InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst ) {
   if (MadeChange)
 return 
 
+  // Canonicalize constant GEPs to i8 type.

bjope wrote:

Right. So things can be expected to just work (given getTypeStoreSize(i8)==1), 
even when the addressing unit isn't 8 bits.

Since
```
%gep = getelementptr i3, ptr %p, i16 1
%gep = getelementptr i8, ptr %p, i16 1
%gep = getelementptr i16, ptr %p, i16 1
```
all are equivalent (for my target), then this patch just makes that more 
obvious by canonicalizing them into a single form. 
So we just need to update some lit test checks to expect "getelementptr i8" 
instead of "getelementptr i16" downstream, and hopefully things will be fine.

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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-11-28 Thread Nikita Popov via cfe-commits


@@ -2282,6 +2282,15 @@ Instruction 
*InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst ) {
   if (MadeChange)
 return 
 
+  // Canonicalize constant GEPs to i8 type.

nikic wrote:

Note that i8 GEPs are already generated by some important components like 
SCEVExpander, so it's pretty likely that your target should handle them already 
(unless you are currently patching all the places generating them of course).

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


[clang] [llvm] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

2023-11-28 Thread Nikita Popov via cfe-commits


@@ -560,14 +560,15 @@ define i32 @test28() nounwind  {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:[[ORIENTATIONS:%.*]] = alloca [1 x [1 x %struct.x]], align 8
 ; CHECK-NEXT:[[T3:%.*]] = call i32 @puts(ptr noundef nonnull 
dereferenceable(1) @.str) #[[ATTR0]]
+; CHECK-NEXT:[[T45:%.*]] = getelementptr inbounds i8, ptr 
[[ORIENTATIONS]], i64 1
 ; CHECK-NEXT:br label [[BB10:%.*]]
 ; CHECK:   bb10:
 ; CHECK-NEXT:[[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 
[[INDVAR_NEXT:%.*]], [[BB10]] ]
 ; CHECK-NEXT:[[T12_REC:%.*]] = xor i32 [[INDVAR]], -1
 ; CHECK-NEXT:[[TMP0:%.*]] = sext i32 [[T12_REC]] to i64
-; CHECK-NEXT:[[T12:%.*]] = getelementptr inbounds [1 x [1 x %struct.x]], 
ptr [[ORIENTATIONS]], i64 1, i64 0, i64 [[TMP0]]
+; CHECK-NEXT:[[T12:%.*]] = getelementptr inbounds [[STRUCT_X:%.*]], ptr 
[[T45]], i64 [[TMP0]]
 ; CHECK-NEXT:[[T16:%.*]] = call i32 (ptr, ...) @printf(ptr noundef nonnull 
dereferenceable(1) @.str1, ptr nonnull [[T12]]) #[[ATTR0]]
-; CHECK-NEXT:[[T84:%.*]] = icmp eq i32 [[INDVAR]], 0
+; CHECK-NEXT:[[T84:%.*]] = icmp eq ptr [[T12]], [[ORIENTATIONS]]

nikic wrote:

Turns out this one is not the indexed compare fold, but the `icmp %p, gep(%p)` 
fold. Now we have two GEPs though and it no longer triggers.

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