Skip to content

Commit 26834a3

Browse files
authored
Fix uninitialized new operator (#298)
* Fix uninitialized new operator * Fix * test
1 parent 80c4577 commit 26834a3

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

tools/cgeist/Lib/clang-mlir.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,9 +1288,6 @@ ValueCategory MLIRScanner::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
12881288
auto shape = std::vector<int64_t>(mt.getShape());
12891289
mlir::Value args[1] = {count};
12901290
arrayCons = alloc = builder.create<mlir::memref::AllocOp>(loc, mt, args);
1291-
if (expr->hasInitializer() && isa<InitListExpr>(expr->getInitializer()))
1292-
(void)InitializeValueByInitListExpr(alloc, expr->getInitializer());
1293-
12941291
} else {
12951292
auto typeSize = getTypeSize(loc, expr->getAllocatedType());
12961293
mlir::Value arg = builder.create<arith::MulIOp>(loc, typeSize, count);
@@ -1306,11 +1303,20 @@ ValueCategory MLIRScanner::VisitCXXNewExpr(clang::CXXNewExpr *expr) {
13061303
alloc);
13071304
}
13081305
assert(alloc);
1309-
1310-
if (expr->getConstructExpr()) {
1311-
VisitConstructCommon(
1312-
const_cast<CXXConstructExpr *>(expr->getConstructExpr()),
1313-
/*name*/ nullptr, /*memtype*/ 0, arrayCons, count);
1306+
if (expr->hasInitializer()) {
1307+
auto init = expr->getInitializer();
1308+
if (isa<InitListExpr>(init)) {
1309+
(void)InitializeValueByInitListExpr(alloc, init);
1310+
} else if (isa<CXXConstructExpr>(init)) {
1311+
assert(arrayCons);
1312+
VisitConstructCommon(
1313+
const_cast<CXXConstructExpr *>(expr->getConstructExpr()),
1314+
/*name*/ nullptr, /*memtype*/ 0, arrayCons, count);
1315+
} else {
1316+
ValueCategory val = Visit(init);
1317+
ValueCategory(alloc, /* isReference */ true)
1318+
.store(loc, builder, val, /* isArray */ false);
1319+
}
13141320
}
13151321
return ValueCategory(alloc, /*isRefererence*/ false);
13161322
}

tools/cgeist/Test/Verification/new.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: cgeist %s %stdinclude --function=* -S | FileCheck %s
22

33
#include <stdio.h>
4+
#include <new>
45

56
struct A {
67
float x, y;
@@ -18,3 +19,72 @@ int main(int argc, char const *argv[]) {
1819
f(a);
1920
return 0;
2021
}
22+
23+
extern "C" char *malloc(int asd);
24+
25+
class SimStream {
26+
public:
27+
int n;
28+
SimStream(int _n) {
29+
n = _n;
30+
}
31+
};
32+
33+
int bar() {
34+
// CHECK: func.func @_Z3barv() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
35+
// CHECK-NEXT: %[[two:.*]] = arith.constant 2 : i32
36+
// CHECK-NEXT: return %[[two:.*]] : i32
37+
SimStream a(2);
38+
return a.n;
39+
}
40+
41+
SimStream *baz() {
42+
// CHECK: func.func @_Z3bazv()
43+
// CHECK-NEXT: %[[thirty:.*]] = arith.constant 30 : i32
44+
// CHECK-NEXT: %[[alloc:.*]] = memref.alloc() : memref<1x1xi32>
45+
// CHECK-NEXT: %[[cast:.*]] = memref.cast %[[alloc]] : memref<1x1xi32> to memref<?x1xi32>
46+
// CHECK-NEXT: affine.store %[[thirty]], %[[alloc]][0, 0] : memref<1x1xi32>
47+
// CHECK-NEXT: return %[[cast]] : memref<?x1xi32>
48+
SimStream *b = new SimStream(30);
49+
return b;
50+
}
51+
52+
int *bat() {
53+
// CHECK: func.func @_Z3batv()
54+
// CHECK-NEXT: %[[alloc:.*]] = memref.alloc() : memref<10xi32>
55+
// CHECK-NEXT: %[[cast:.*]] = memref.cast %[[alloc]] : memref<10xi32> to memref<?xi32>
56+
// CHECK-NEXT: return %[[cast]] : memref<?xi32>
57+
int *b = new int[10];
58+
return b;
59+
}
60+
61+
int *baf() {
62+
// CHECK: func.func @_Z3bafv()
63+
// CHECK-DAG: %[[three:.*]] = arith.constant 3 : i32
64+
// CHECK-DAG: %[[two:.*]] = arith.constant 2 : i32
65+
// CHECK-DAG: %[[one:.*]] = arith.constant 1 : i32
66+
// CHECK-NEXT: %[[alloc:.*]] = memref.alloc() : memref<3xi32>
67+
// CHECK-NEXT: %[[cast:.*]] = memref.cast %[[alloc]] : memref<3xi32> to memref<?xi32>
68+
// CHECK-DAG: affine.store %[[one]], %alloc[0] : memref<3xi32>
69+
// CHECK-DAG: affine.store %[[two]], %alloc[1] : memref<3xi32>
70+
// CHECK-DAG: affine.store %[[three]], %alloc[2] : memref<3xi32>
71+
// CHECK-NEXT: return %[[cast]] : memref<?xi32>
72+
int *b = new int[3] {1, 2, 3};
73+
return b;
74+
}
75+
76+
int foo() {
77+
// CHECK: func.func @_Z3foov()
78+
// CHECK-DAG: %[[one:.*]] = arith.constant 1 : i32
79+
// CHECK-DAG: %[[alloc:.*]] = memref.alloc() : memref<1xi32>
80+
// CHECK-NEXT: %[[v0:.*]] = "polygeist.memref2pointer"(%[[alloc]]) : (memref<1xi32>) -> !llvm.ptr<i8>
81+
// CHECK-NEXT: %[[v1:.*]] = llvm.bitcast %[[v0]] : !llvm.ptr<i8> to !llvm.ptr<i32>
82+
// CHECK-NEXT: llvm.store %[[one]], %[[v1]] : !llvm.ptr<i32>
83+
// CHECK-NEXT: %[[v2:.*]] = affine.load %[[alloc]][0] : memref<1xi32>
84+
// CHECK-NEXT: return %[[v2]] : i32
85+
int e = 1;
86+
int * __new_start((int *)malloc(sizeof(int)));
87+
::new((void*)__new_start) int(e);
88+
return (int) __new_start[0];
89+
}
90+

0 commit comments

Comments
 (0)