However, for test3, why LLVM doesn't use vec + 1 as the invariant?
- User
However, for test3, why LLVM doesn't use vec + 1 as the invariant?
Assistant
The difference in how LLVM represents the address arithmetic for
vec[vec2[j] + 1]intest3versustest4stems from how loop-invariant code motion (LICM) interacts with pointer analysis and bounds guarantees. Here's the breakdown:
Key Insight: Why LLVM Doesn't Hoist
vec + 1intest3-
Base Pointer Invariance:
- In
test4, the vector's data pointer (vec.data()) is explicitly loaded once outside the loop (%1 = load ptr, ptr %vec). This creates a known base address (%1) that LLVM can use to compute a loop-invariant offset (%invariant.gep = vec.data() + 4). - In
test3,vecis a raw pointer parameter. Whilevecitself is loop-invariant (it’s a function parameter), LLVM cannot directly hoistvec + 1as a precomputed base because:- The index into
vec(vec2[j]) is dynamic (changes per iteration). - LLVM must ensure all accesses are within bounds (via
inboundsGEP), which is harder to prove for raw pointers.
- The index into
- In
-
Bounds and
inboundsGuarantees:test4usesstd::vector, which has a known size and contiguous layout. LLVM can verify thatvec[vec2[j] + 1]stays within bounds, allowing it to hoistvec.data() + 4as an invariant base.test3uses a rawint* vecwith unknown size. LLVM cannot prove thatvec2[j] + 1stays within the array bounds for all iterations. To avoid undefined behavior, it avoids hoisting and instead uses a byte-wise offset (getelementptr i8, ptr %arrayidx2, i64 4), which implicitly assumes 4-byte elements without relying oninbounds.
-
Aliasing and Pointer Analysis:
- In
test3,vecandvec2are raw pointers. LLVM must assume they could overlap unless proven otherwise. Hoistingvec + 1might interact unpredictably withvec2[j]if they alias, so LLVM plays it safe. - In
test4,std::vector’s data pointer is encapsulated, and LLVM can better reason about non-overlapping (no aliasing betweenvecandvec2).
- In
Why the IR Looks Different
-
In
test3:
LLVM computesvec[vec2[j]]first, then adds 4 bytes to reachvec[vec2[j] + 1]. This avoids relying oninboundsfor the second access, which would require provingvec2[j] + 1is within bounds (hard for raw pointers). -
In
test4:
The vector’s data pointer is loaded once, and LLVM precomputesvec.data() + 4as an invariant. This leverages the vector’s known size and contiguous layout to hoist the offset safely.
Summary
The IR differences are not due to performance optimizations but rather how LLVM ensures memory safety:
- For
test3(raw pointers), LLVM avoids hoisting to prevent potential out-of-bounds accesses. - For
test4(vectors), LLVM knows the bounds and can safely hoist the invariant offset.
Both versions compile to equivalent assembly (e.g.,
vec2[j] * 4added to the base pointer). The IR divergence reflects LLVM’s conservative vs. aggressive optimization based on pointer attributes and bounds knowledge.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.