Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ClangIR][Lowering] Handle lowered array index #1008

Merged
merged 1 commit into from
Oct 26, 2024

Conversation

ChuanqiXu9
Copy link
Member

Previously we didn't generate the index for array correct.

The previous test is incorrect already: globals-neg-index-array.c

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s

struct __attribute__((packed)) PackedStruct {
    char a1;
    char a2;
    char a3;
};
struct PackedStruct packed[10];
char *packed_element = &(packed[-2].a3);
// CHECK: cir.global  external @packed = #cir.zero : !cir.array<!ty_PackedStruct x 10> {alignment = 16 : i64} loc(#loc5)
// CHECK: cir.global  external @packed_element = #cir.global_view<@packed, [-2 : i32, 2 : i32]>
// LLVM: @packed = global [10 x %struct.PackedStruct] zeroinitializer
+ // LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @packed, i32 0, i32 -2, i32 2)
- // LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @packed,  i32 -2, i32 2)

Compile it with -fclangir -S, we got:

packed:
        .zero   30

packed_element:
	.quad	packed-54

but the traditional pipeline shows (https://godbolt.org/z/eTj96EP1E):

packed:
        .zero   30

packed_element:
        .quad   packed-4

this may be a simple mismatch.

Copy link
Collaborator

@smeenai smeenai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great catch. I'm surprised we don't have more tests running into this?

@bcardosolopes bcardosolopes changed the title [clangir] [Lowering] Handle lowered array index [ClangIR][Lowering] Handle lowered array index Oct 26, 2024
@bcardosolopes bcardosolopes merged commit 74d8fe9 into llvm:main Oct 26, 2024
7 checks passed
lanza pushed a commit that referenced this pull request Nov 5, 2024
Previously we didn't generate the index for array correct.

The previous test is incorrect already:  globals-neg-index-array.c

```
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s

struct __attribute__((packed)) PackedStruct {
    char a1;
    char a2;
    char a3;
};
struct PackedStruct packed[10];
char *packed_element = &(packed[-2].a3);
// CHECK: cir.global  external @PACKED = #cir.zero : !cir.array<!ty_PackedStruct x 10> {alignment = 16 : i64} loc(#loc5)
// CHECK: cir.global  external @packed_element = #cir.global_view<@PACKED, [-2 : i32, 2 : i32]>
// LLVM: @PACKED = global [10 x %struct.PackedStruct] zeroinitializer
+ // LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @PACKED, i32 0, i32 -2, i32 2)
- // LLVM: @packed_element = global ptr getelementptr inbounds ([10 x %struct.PackedStruct], ptr @PACKED,  i32 -2, i32 2)
```

Compile it with `-fclangir -S`, we got:

```
packed:
        .zero   30

packed_element:
	.quad	packed-54
```

but the traditional pipeline shows (https://godbolt.org/z/eTj96EP1E):

```
packed:
        .zero   30

packed_element:
        .quad   packed-4
```

this may be a simple mismatch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants