I have two functions int test3(int vec, int vec2, int n, int m) { int sum = 0; for (int…
- User
I have two functions int test3(int *vec, int *vec2, int n, int m) { int sum = 0; for (int j = n; j < m; j++) sum += vec[vec2[j]] + vec[vec2[j] + 1]; return sum; }
int test4(std::vector vec, std::vector vec2, int n, int m) { int sum = 0; for (int j = n; j < m; j++) sum += vec[vec2[j]] + vec[vec2[j] + 1]; return sum; } Clang's result IR is following with -O1 define dso_local noundef i32 @test3(int*, int*, int, int)(ptr noundef readonly captures(none) %vec, ptr noundef readonly captures(none) %vec2, i32 noundef %n, i32 noundef %m) local_unnamed_addr { entry: %cmp15 = icmp slt i32 %n, %m br i1 %cmp15, label %for.body.preheader, label %for.cond.cleanup
for.body.preheader: %0 = sext i32 %n to i64 %wide.trip.count = sext i32 %m to i64 br label %for.body
for.cond.cleanup: %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add8, %for.body ] ret i32 %sum.0.lcssa
for.body: %indvars.iv = phi i64 [ %0, %for.body.preheader ], [ %indvars.iv.next, %for.body ] %sum.016 = phi i32 [ 0, %for.body.preheader ], [ %add8, %for.body ] %arrayidx = getelementptr inbounds i32, ptr %vec2, i64 %indvars.iv %1 = load i32, ptr %arrayidx, align 4 %idxprom1 = sext i32 %1 to i64 %arrayidx2 = getelementptr inbounds i32, ptr %vec, i64 %idxprom1 %2 = load i32, ptr %arrayidx2, align 4 %arrayidx6 = getelementptr i8, ptr %arrayidx2, i64 4 %3 = load i32, ptr %arrayidx6, align 4 %add7 = add i32 %2, %sum.016 %add8 = add i32 %add7, %3 %indvars.iv.next = add nsw i64 %indvars.iv, 1 %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count br i1 %exitcond.not, label %for.cond.cleanup, label %for.body }
define dso_local noundef i32 @test4(std::vector<int, std::allocator>, std::vector<int, std::allocator>, int, int)(ptr noundef readonly captures(none) %vec, ptr noundef readonly captures(none) %vec2, i32 noundef %n, i32 noundef %m) local_unnamed_addr { entry: %cmp16 = icmp slt i32 %n, %m br i1 %cmp16, label %for.body.lr.ph, label %for.cond.cleanup
for.body.lr.ph: %0 = load ptr, ptr %vec2, align 8 %1 = load ptr, ptr %vec, align 8 %invariant.gep = getelementptr i8, ptr %1, i64 4 %2 = sext i32 %n to i64 %wide.trip.count = sext i32 %m to i64 br label %for.body
for.cond.cleanup: %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add8, %for.body ] ret i32 %sum.0.lcssa
for.body: %indvars.iv = phi i64 [ %2, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ] %sum.017 = phi i32 [ 0, %for.body.lr.ph ], [ %add8, %for.body ] %add.ptr.i = getelementptr inbounds nuw i32, ptr %0, i64 %indvars.iv %3 = load i32, ptr %add.ptr.i, align 4 %conv1 = sext i32 %3 to i64 %add.ptr.i13 = getelementptr inbounds nuw i32, ptr %1, i64 %conv1 %4 = load i32, ptr %add.ptr.i13, align 4 %5 = sext i32 %3 to i64 %gep = getelementptr i32, ptr %invariant.gep, i64 %5 %6 = load i32, ptr %gep, align 4 %add7 = add i32 %4, %sum.017 %add8 = add i32 %add7, %6 %indvars.iv.next = add nsw i64 %indvars.iv, 1 %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count br i1 %exitcond.not, label %for.cond.cleanup, label %for.body }
Why for pointer case, the getelementptr for vec[vec2[j]+1] is %arrayidx20 = getelementptr i8, ptr %arrayidx15, i64 4, uses vec[vec2[j]]'s ptr offset 4, but for vector case, the getelementptr is %gep = getelementptr i32, ptr %invariant.gep, i64 %5, uses the invariant.gep that offset vec's ptr by 4
Assistant
This is an interesting observation about LLVM IR optimization. Let me break down the key differences:
In the pointer case (test3):
%arrayidx2 = getelementptr inbounds i32, ptr %vec, i64 %idxprom1 %2 = load i32, ptr %arrayidx2, align 4 ; vec[vec2[j]] %arrayidx6 = getelementptr i8, ptr %arrayidx2, i64 4 ; offset from previous position %3 = load i32, ptr %arrayidx6, align 4 ; vec[vec2[j] + 1]In the vector case (test4):
%invariant.gep = getelementptr i8, ptr %1, i64 4 ; pre-calculated offset ... %add.ptr.i13 = getelementptr inbounds nuw i32, ptr %1, i64 %conv1 %4 = load i32, ptr %add.ptr.i13, align 4 ; vec[vec2[j]] %gep = getelementptr i32, ptr %invariant.gep, i64 %5 ; using pre-calculated base %6 = load i32, ptr %gep, align 4 ; vec[vec2[j] + 1]The key difference is that in the vector case, the compiler has chosen to hoist the constant offset calculation (the +1 part) out of the loop. This is done by:
- Pre-calculating a base pointer (
%invariant.gep) that points to vec+4 bytes before the loop - Using this pre-calculated base for accessing vec[vec2[j] + 1]
This optimization is possible because:
- The offset of 4 bytes is constant throughout the loop
- The vector's base address is guaranteed not to change during loop execution
- std::vector provides stronger guarantees about memory layout and alignment
In the raw pointer case, the compiler is being more conservative since it has fewer guarantees about pointer validity and alignment. It calculates the offset relative to each vec[vec2[j]] position instead of pre-calculating a base offset.
This is generally a micro-optimization that might provide slightly better performance in the vector case by:
- Reducing the number of pointer calculations in the loop
- Potentially allowing better instruction scheduling
- Making the dependency chain slightly shorter
However, the actual performance impact would likely be minimal in most cases.
- Pre-calculating a base pointer (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.