Repository navigation
Closed
Description
Consider the following code:
fn add(a: [f32, ..3], b: [f32, ..3]) -> [f32, ..3] {
let mut s: [f32, ..3] = [0.0, ..3];
for i in range(0, 3) {
s[i] = a[i] + b[i];
}
s
}
This function results in the following LLVM assembly when compiled with --opt-level 3:
define void @_ZN3bar16_d2fb2ba5dc6ea744v0.0E([3 x float]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone, [3 x float]* nocapture readonly, [3 x float]* nocapture readonly) #1 {
"function top level":
%s = alloca [3 x float], align 4
%4 = bitcast [3 x float]* %s to i8*
call void @llvm.memset.p0i8.i64(i8* %4, i8 0, i64 12, i32 4, i1 false)
br label %match_else
match_else: ; preds = %"function top level", %next6
%.sroa.013.0.load1922 = phi i64 [ 0, %"function top level" ], [ %8, %next6 ]
%5 = shl i64 %.sroa.013.0.load1922, 2
%6 = icmp ugt i64 %5, 11
br i1 %6, label %cond, label %next6
match_case: ; preds = %next6
%7 = bitcast [3 x float]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %7, i8* %4, i64 12, i32 4, i1 false)
ret void
cond: ; preds = %match_else
tail call void @"_ZN8unstable4lang17fail_bounds_check17_7112ff25e39642a211v0.8$x2dpreE"({ i64, %tydesc*, i8*, i8*, i8 }* undef, i8* getelementptr inbounds ([8 x i8]* @str998, i64 0, i64 0), i64 5, i64 %.sroa.013.0.load1922, i64 3)
unreachable
next6: ; preds = %match_else
%8 = add i64 %.sroa.013.0.load1922, 1
%9 = getelementptr inbounds [3 x float]* %3, i64 0, i64 %.sroa.013.0.load1922
%10 = getelementptr inbounds [3 x float]* %2, i64 0, i64 %.sroa.013.0.load1922
%11 = load float* %9, align 4
%12 = load float* %10, align 4
%13 = fadd float %11, %12
%14 = getelementptr inbounds [3 x float]* %s, i64 0, i64 %.sroa.013.0.load1922
store float %13, float* %14, align 4
%15 = icmp slt i64 %8, 3
br i1 %15, label %match_else, label %match_case
}
What's weird is that llvm opt (from LLVM 3.2) can optimize this just fine, maybe an optimization regression or we're lacking some optimization passes.
Also on an unrelated (?) note Mul can overflow:
let x = [0i32, ..16]; x[1u << 63]
The address computation overflows as well, yielding x[0] as a result.
Activity
alexcrichton commentedon Sep 7, 2013
Interesting. If you remove the
allocaof%sat the top, thememcpyat the end, and store directly into%0instead of%s, then the bounds check will be eliminated.For some reason, it appears that the last
memcpyin thematch_caseblock is preventing thecondblock from being eliminated...I was testing this by passing the following code to llvm's
optcommand(I added the
noaliasattributes myself, didn't help)thestinger commentedon Sep 18, 2013
This is still an issue despite the return value being tagged as
sretandnoaliasnow.dotdash commentedon Jan 12, 2014
This looks like a regression that happened somewhere between LLVM 3.2 and 3.3. I reduced the code to just a loop with the bounds check:
and
optfrom LLVM 3.2 optimizes that to:but
optfrom LLVM 3.3, 3.4, and a 3.5 snapshot as of svn197556 leave it unchanged.dotdash commentedon Jan 12, 2014
Filed a bug against LLVM at http://llvm.org/bugs/show_bug.cgi?id=18449
dotdash commentedon Mar 2, 2014
Looks like this is fixed. Using
rustc 0.10-pre (3d117cf 2014-03-01 03:06:31 -0800)I get:Auto merge of rust-lang#9025 - Alexendoo:unused-async-method, r=dswij
Merge rust-lang#9024