Skip to content

Commit cbcaf00

Browse files
committed
streamline emission of arrays with broadcast initializer
1 parent 1546739 commit cbcaf00

33 files changed

Lines changed: 351 additions & 187 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,9 +3855,57 @@ void Converter::ConvertUnionInitList(clang::InitListExpr *expr,
38553855
ConvertFieldInit(field, expr->getInit(0));
38563856
}
38573857

3858+
namespace {
3859+
// Whether expr is a side-effect-free constant equal to the value-initialized
3860+
// (zero) value of its type, i.e., the value of an ImplicitValueInitExpr.
3861+
bool IsZeroValue(const clang::Expr *expr, const clang::ASTContext &ctx) {
3862+
clang::Expr::EvalResult result;
3863+
if (expr->isValueDependent() || !expr->EvaluateAsRValue(result, ctx) ||
3864+
result.HasSideEffects) {
3865+
return false;
3866+
}
3867+
const auto &val = result.Val;
3868+
if (val.isInt()) {
3869+
return val.getInt().isZero();
3870+
}
3871+
if (val.isFloat()) {
3872+
return val.getFloat().isPosZero();
3873+
}
3874+
return false;
3875+
}
3876+
} // namespace
3877+
3878+
bool Converter::IsBroadcastArrayInitList(clang::InitListExpr *expr) {
3879+
auto *arr_ty = ctx_.getAsConstantArrayType(expr->getType());
3880+
if (!arr_ty || arr_ty->getSize().ule(1) || !expr->hasArrayFiller() ||
3881+
!TypeIsCopyable(arr_ty->getElementType())) {
3882+
return false;
3883+
}
3884+
auto *filler = expr->getArrayFiller();
3885+
if (filler->HasSideEffects(ctx_)) {
3886+
return false;
3887+
}
3888+
// Explicit initializers must have the same value as the filler. This can
3889+
// only be checked for a value-initialized filler, e.g., int a[10] = {0};
3890+
if (expr->getNumInits() == 0) {
3891+
return true;
3892+
}
3893+
return clang::isa<clang::ImplicitValueInitExpr>(filler) &&
3894+
llvm::all_of(expr->inits(), [&](const clang::Expr *init) {
3895+
return IsZeroValue(init, ctx_);
3896+
});
3897+
}
3898+
38583899
void Converter::ConvertArrayInitList(clang::InitListExpr *expr) {
38593900
PushBracket bracket(*this);
38603901
auto element_type = ctx_.getAsArrayType(expr->getType())->getElementType();
3902+
if (IsBroadcastArrayInitList(expr)) {
3903+
// All elements have the same Copy value: emit [filler; N].
3904+
ConvertVarInit(element_type, expr->getArrayFiller());
3905+
auto size = ctx_.getAsConstantArrayType(expr->getType())->getSize();
3906+
StrCat(std::format("; {}", GetNumAsString(size).c_str()));
3907+
return;
3908+
}
38613909
for (auto *init : expr->inits()) {
38623910
ConvertVarInit(element_type, init);
38633911
StrCat(token::kComma);

‎cpp2rust/converter/converter.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
577577
virtual void ConvertUnionInitList(clang::InitListExpr *expr,
578578
const clang::FieldDecl *field);
579579
virtual void ConvertArrayInitList(clang::InitListExpr *expr);
580+
bool IsBroadcastArrayInitList(clang::InitListExpr *expr);
580581
bool VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr);
581582
bool VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr);
582583
virtual bool VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr);

‎tests/unit/array_const_init.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ struct S {
88

99
struct S s = {5};
1010

11+
struct P {
12+
int x;
13+
int y;
14+
};
15+
16+
int g[5] = {0};
17+
static const int k[8] = {3, 0, 4};
18+
19+
static int counter = 0;
20+
static int next_counter(void) { return ++counter; }
21+
1122
int main() {
1223
assert(s.head == 5);
1324
for (int i = 0; i < 3; i++) {
@@ -16,5 +27,41 @@ int main() {
1627
for (int i = 0; i < 4; i++) {
1728
assert(s.buf[i] == 0);
1829
}
30+
31+
int a[10] = {0};
32+
for (int i = 0; i < 10; i++) {
33+
assert(a[i] == 0);
34+
}
35+
36+
int seven[4] = {7};
37+
assert(seven[0] == 7);
38+
for (int i = 1; i < 4; i++) {
39+
assert(seven[i] == 0);
40+
}
41+
42+
int b[6] = {1, 0, 2};
43+
assert(b[0] == 1 && b[1] == 0 && b[2] == 2);
44+
for (int i = 3; i < 6; i++) {
45+
assert(b[i] == 0);
46+
}
47+
48+
double d[4] = {-0.0};
49+
assert(d[0] == 0.0 && d[3] == 0.0);
50+
51+
struct P p[4] = {{1, 2}};
52+
assert(p[0].x == 1 && p[0].y == 2);
53+
assert(p[3].x == 0 && p[3].y == 0);
54+
55+
int c[5] = {next_counter(), next_counter()};
56+
assert(c[0] + c[1] == 3 && c[4] == 0);
57+
58+
int t[2] = {0};
59+
assert(t[0] == 0 && t[1] == 0);
60+
61+
int u[3] = {5, 6};
62+
assert(u[0] == 5 && u[1] == 6 && u[2] == 0);
63+
64+
assert(g[4] == 0);
65+
assert(k[0] == 3 && k[1] == 0 && k[2] == 4 && k[7] == 0);
1966
return 0;
2067
}

‎tests/unit/init_list.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ struct Holder {
2020

2121
int sum(const Pair &p) { return p.a + p.b; }
2222

23+
struct Seven {
24+
int v = 7;
25+
};
26+
2327
int main() {
2428
int i1{3};
2529
int i2{};
@@ -35,5 +39,10 @@ int main() {
3539
assert(h.p.a == 1 && h.p.b == 2);
3640
assert(sum({p}) == 3);
3741

42+
Seven sevens[4] = {};
43+
for (int i = 0; i < 4; i++) {
44+
assert(sevens[i].v == 7);
45+
}
46+
3847
return 0;
3948
}

‎tests/unit/out/refcount/array_const_init.rs‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ thread_local!(
3636
buf: Box::new([0; 4]),
3737
}));
3838
);
39+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
40+
#[byte_size(8)]
41+
pub struct P {
42+
#[offset(0)]
43+
pub x: i32,
44+
#[offset(4)]
45+
pub y: i32,
46+
}
47+
thread_local!(
48+
pub static g_1: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([0_i32; 5])));
49+
);
50+
thread_local!(
51+
pub static k_2: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([
52+
3, 0, 4, 0_i32, 0_i32, 0_i32, 0_i32, 0_i32,
53+
])));
54+
);
55+
thread_local!(
56+
pub static counter_3: Value<i32> = Rc::new(RefCell::new(0));
57+
);
58+
pub fn next_counter_4() -> i32 {
59+
return (*(*counter_3.with(Value::clone).borrow_mut()).prefix_inc());
60+
}
3961
pub fn main() {
4062
__cpp2rust_init_globals();
4163
std::process::exit(main_0());
@@ -56,8 +78,83 @@ fn main_0() -> i32 {
5678
i.postfix_inc();
5779
}
5880
}
81+
let mut a: [i32; 10] = [0_i32; 10];
82+
{
83+
let mut i: i32 = 0;
84+
'loop_: while (i < 10) {
85+
assert!((a[(i) as usize] == 0));
86+
i.postfix_inc();
87+
}
88+
}
89+
let mut seven: [i32; 4] = [7, 0_i32, 0_i32, 0_i32];
90+
assert!((seven[(0) as usize] == 7));
91+
{
92+
let mut i: i32 = 1;
93+
'loop_: while (i < 4) {
94+
assert!((seven[(i) as usize] == 0));
95+
i.postfix_inc();
96+
}
97+
}
98+
let mut b: [i32; 6] = [1, 0, 2, 0_i32, 0_i32, 0_i32];
99+
assert!((((b[(0) as usize] == 1) && (b[(1) as usize] == 0)) && (b[(2) as usize] == 2)));
100+
{
101+
let mut i: i32 = 3;
102+
'loop_: while (i < 6) {
103+
assert!((b[(i) as usize] == 0));
104+
i.postfix_inc();
105+
}
106+
}
107+
let mut d: [f64; 4] = [-0_f64, 0_f64, 0_f64, 0_f64];
108+
assert!(((d[(0) as usize] == 0_f64) && (d[(3) as usize] == 0_f64)));
109+
let mut p: [P; 4] = [
110+
P { x: 1, y: 2 },
111+
<P>::default(),
112+
<P>::default(),
113+
<P>::default(),
114+
];
115+
assert!(((p[(0) as usize].x == 1) && (p[(0) as usize].y == 2)));
116+
assert!(((p[(3) as usize].x == 0) && (p[(3) as usize].y == 0)));
117+
let mut c: [i32; 5] = [
118+
({ next_counter_4() }),
119+
({ next_counter_4() }),
120+
0_i32,
121+
0_i32,
122+
0_i32,
123+
];
124+
assert!((((c[(0) as usize] + c[(1) as usize]) == 3) && (c[(4) as usize] == 0)));
125+
let mut t: [i32; 2] = [0_i32; 2];
126+
assert!(((t[(0) as usize] == 0) && (t[(1) as usize] == 0)));
127+
let mut u: [i32; 3] = [5, 6, 0_i32];
128+
assert!((((u[(0) as usize] == 5) && (u[(1) as usize] == 6)) && (u[(2) as usize] == 0)));
129+
assert!(
130+
(({
131+
let __idx0 = (4) as usize;
132+
g_1.with(|rc| rc.borrow()[__idx0])
133+
}) == 0)
134+
);
135+
assert!(
136+
((((({
137+
let __idx0 = (0) as usize;
138+
k_2.with(|rc| rc.borrow()[__idx0])
139+
}) == 3)
140+
&& (({
141+
let __idx0 = (1) as usize;
142+
k_2.with(|rc| rc.borrow()[__idx0])
143+
}) == 0))
144+
&& (({
145+
let __idx0 = (2) as usize;
146+
k_2.with(|rc| rc.borrow()[__idx0])
147+
}) == 4))
148+
&& (({
149+
let __idx0 = (7) as usize;
150+
k_2.with(|rc| rc.borrow()[__idx0])
151+
}) == 0))
152+
);
59153
return 0;
60154
}
61155
pub fn __cpp2rust_init_globals() {
62156
let _ = s_0.with(|_| ());
157+
let _ = g_1.with(|_| ());
158+
let _ = k_2.with(|_| ());
159+
let _ = counter_3.with(|_| ());
63160
}

‎tests/unit/out/refcount/compound_assign_paren.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main_0() -> i32 {
2525
field!((ptr), flags)
2626
.write({ (((ptr).with(|__s| __s.flags) as i32) & ((!(1 << 0) as u8) as i32)) as u8 });
2727
assert!(((ptr.with(|__s| __s.flags) as i32) == 2));
28-
let mut bits: [u8; 4] = [0_u8, 0_u8, 0_u8, 0_u8];
28+
let mut bits: [u8; 4] = [0_u8; 4];
2929
(bits)[((5) / 8) as usize] =
3030
{ (((bits)[((5) / 8) as usize] as i32) | (((1 << ((5) & 7)) as u8) as i32)) as u8 };
3131
(bits)[((13) / 8) as usize] =

‎tests/unit/out/refcount/cstring.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn test_memcpy_0() {
1010
let src: Value<Box<[i8]>> = Rc::new(RefCell::new(i8::array_from_literal(b"hello\0")));
11-
let dst: Value<Box<[i8]>> =
12-
Rc::new(RefCell::new(Box::new([0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8])));
11+
let dst: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([0_i8; 6])));
1312
let mut r: AnyPtr = match ({ (dst.decay()).to_any() },) {
1413
(mut __tmp_0,) => {
1514
__tmp_0.memcpy(&(src.decay()).to_any(), 6_usize);

‎tests/unit/out/refcount/field_pointers.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ fn main_0() -> i32 {
205205
}
206206
};
207207
assert!(({ ((*y.borrow()) == 4) }));
208-
let bytes: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([
209-
0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8,
210-
])));
208+
let bytes: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([0_u8; 8])));
211209
let mut view: Ptr<Header> = bytes.decay().reinterpret_cast::<Header>();
212210
field!(view, tag).write(16909060);
213211
assert!(

‎tests/unit/out/refcount/goto_cleanup.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn via_arrays_4(mut fail: i32) -> i32 {
107107
goto_block!({
108108
'__entry: {
109109
ret = 0;
110-
remain = [0_u8, 0_u8, 0_u8, 0_u8];
110+
remain = [0_u8; 4];
111111
name = b"wxyz\0".map(i8::from_byte);
112112
if (fail != 0) {
113113
ret = -1_i32;

‎tests/unit/out/refcount/inet_pton_ntop.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ fn main_0() -> i32 {
317317
}
318318
} == 0)
319319
);
320-
let sixteen: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([
321-
0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8,
322-
0_u8,
323-
])));
320+
let sixteen: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([0_u8; 16])));
324321
(*sixteen.borrow_mut())[(15) as usize] = 1_u8;
325322
assert!(
326323
({

0 commit comments

Comments
 (0)