Skip to content

Commit 7051000

Browse files
[CIR][Lowering] Fix Global Attr Lowering (#906)
Consider the following code snippet `tmp.c`: ``` #define N 3200 struct S { double a[N]; double b[N]; } s; double *b = s.b; void foo() { double x = 0; for (int i = 0; i < N; i++) x += b[i]; } int main() { foo(); return 0; } ``` Running `bin/clang tmp.c -fclangir -o tmp && ./tmp` causes a segmentation fault. I compared the LLVM IR with and without CIR and noticed a difference which causes this: `@b = global ptr getelementptr inbounds (%struct.S, ptr @s, i32 0, i32 1)` // no CIR `@b = global ptr getelementptr inbounds (%struct.S, ptr @s, i32 1)` // with CIR It seems there is a missing index when creating global pointers from structs. I have updated `Lowering/DirectToLLVM/LowerToLLVM.cpp`, and added a few tests.
1 parent 1cd5944 commit 7051000

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp,
407407

408408
if (globalAttr.getIndices()) {
409409
llvm::SmallVector<mlir::LLVM::GEPArg> indices;
410+
411+
if (auto stTy = dyn_cast<mlir::LLVM::LLVMStructType>(sourceType))
412+
if (stTy.isIdentified())
413+
indices.push_back(0);
414+
410415
for (auto idx : globalAttr.getIndices()) {
411416
auto intAttr = dyn_cast<mlir::IntegerAttr>(idx);
412417
assert(intAttr && "index must be integers");

clang/test/CIR/Lowering/global-ptr.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
2+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
3+
4+
// LLVM: %struct.S1 = type { [3200 x double], [3200 x double] }
5+
// LLVM: %struct.S2 = type { [10 x ptr] }
6+
// LLVM: %struct.S3 = type { [2000 x i32], [2000 x i32], [2000 x i32] }
7+
// LLVM: %struct.S4 = type { i32, i32, i32 }
8+
// LLVM: %union.U1 = type { [2000 x i32] }
9+
10+
// LLVM: @s1 = global %struct.S1 zeroinitializer, align 8
11+
// LLVM: @b1 = global ptr getelementptr inbounds (%struct.S1, ptr @s1, i32 0, i32 1), align 8
12+
// LLVM: @s2 = global %struct.S2 zeroinitializer, align 8
13+
// LLVM: @b2 = global ptr @s2, align 8
14+
// LLVM: @s3 = global %struct.S3 zeroinitializer, align 4
15+
// LLVM: @b3 = global ptr getelementptr inbounds (%struct.S3, ptr @s3, i32 0, i32 2), align 8
16+
// LLVM: @s4 = global %struct.S4 zeroinitializer, align 4
17+
// LLVM: @b4 = global ptr getelementptr inbounds (%struct.S4, ptr @s4, i32 0, i32 2), align 8
18+
// LLVM: @u1 = global %union.U1 zeroinitializer, align 4
19+
// LLVM: @b5 = global ptr @u1, align 8
20+
21+
struct S1 {
22+
double a[3200];
23+
double b[3200];
24+
} s1;
25+
26+
double *b1 = s1.b;
27+
28+
struct S2 {
29+
double* a[10];
30+
} s2;
31+
32+
double **b2 = s2.a;
33+
34+
struct S3 {
35+
int a[2000];
36+
int b[2000];
37+
int c[2000];
38+
} s3;
39+
40+
double *b3 = s3.c;
41+
42+
struct S4 {
43+
int a, b, c;
44+
} s4;
45+
46+
int* b4 = &s4.c;
47+
48+
union U1 {
49+
int a[2000];
50+
int b[2000];
51+
int c[2000];
52+
} u1;
53+
54+
double *b5 = u1.a;

0 commit comments

Comments
 (0)