Skip to content

Commit 503c573

Browse files
authored
Handle allocation when allocating struct with new (#227)
1 parent 38670b2 commit 503c573

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tools/cgeist/Lib/clang-mlir.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,9 @@ ValueCategory MLIRScanner::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
11991199
auto shape = std::vector<int64_t>(mt.getShape());
12001200
mlir::Value args[1] = {count};
12011201
arrayCons = alloc = builder.create<mlir::memref::AllocOp>(loc, mt, args);
1202+
if (expr->hasInitializer() && isa<InitListExpr>(expr->getInitializer()))
1203+
(void)InitializeValueByInitListExpr(alloc, expr->getInitializer());
1204+
12021205
} else {
12031206
auto i64 = mlir::IntegerType::get(count.getContext(), 64);
12041207
auto typeSize = getTypeSize(expr->getAllocatedType());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: cgeist %s %stdinclude --function=* -S | FileCheck %s
2+
3+
#include <stdio.h>
4+
5+
struct A {
6+
float x, y;
7+
};
8+
9+
void f(A *a) { printf("a.x = %f, a.y = %f\n", a->x, a->y); }
10+
11+
int main(int argc, char const *argv[]) {
12+
// CHECK-DAG: %[[two:.*]] = arith.constant 2.000000e+00 : f32
13+
// CHECK-DAG: %[[one:.*]] = arith.constant 1.000000e+00 : f32
14+
// CHECK: %[[alloc:.*]] = memref.alloc() : memref<1x2xf32>
15+
// CHECK: affine.store %[[one]], %[[alloc]][0, 0] : memref<1x2xf32>
16+
// CHECK: affine.store %[[two]], %[[alloc]][0, 1] : memref<1x2xf32>
17+
auto *a = new A{1.0f, 2.0f};
18+
f(a);
19+
return 0;
20+
}

0 commit comments

Comments
 (0)