Skip to content

Commit 4c6a8ca

Browse files
authored
Use indexed sources for fallible constant rows (#9629)
## Summary - Tracking Issue: #9128 `RowFn` handles constant inputs so row kernels do not need separate execution paths for each input shape. #9628 adds an indexed source for infallible execution, but fallible `execute_owned` still uses a separate loop whenever any input is constant. That loop selects between row and constant values on every iteration, duplicating the traversal and hiding the fixed batch input shape from LLVM. Depends on #9628. ## Changes Extends the decoded indexed source to fallible `execute_owned` calls. Each source validates its decoded length and fixes its row-or-constant behavior before traversal, allowing every input shape to use `map_checked_into` without changing output initialization or deferred failure handling. This removes the separate `LaneZip` branch. Optimized x86 IR preserves the vector loop while reducing the focused deferred Boolean executor by 27% and the deferred `i64` executor by 19%, with the same bounds-panic sites. Tests cover a constant left-hand input, and the focused benchmark covers varying inputs and both partial-constant orientations. Signed-off-by: Connor Tsui <[email protected]>
1 parent 59a056d commit 4c6a8ca

5 files changed

Lines changed: 87 additions & 47 deletions

File tree

‎vortex-array/benches/row_fn_output.rs‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use vortex_array::scalar_fn::unstable::row::RowVisitor;
2828
use vortex_array::scalar_fn::unstable::row::execute_rows;
2929
use vortex_error::VortexExpect;
3030
use vortex_error::VortexResult;
31+
use vortex_error::vortex_ensure;
3132
use vortex_session::VortexSession;
3233
use vortex_session::registry::CachedId;
3334

@@ -87,6 +88,9 @@ impl BenchPrimitive for i64 {
8788
#[derive(Clone)]
8889
struct InfallibleBool<T>(PhantomData<T>);
8990

91+
#[derive(Clone)]
92+
struct DeferredI64;
93+
9094
impl<T: BenchPrimitive> RowFn for InfallibleBool<T> {
9195
type Options = EmptyOptions;
9296

@@ -108,22 +112,69 @@ impl<T: BenchPrimitive> RowFn for InfallibleBool<T> {
108112
}
109113
}
110114

115+
impl RowFn for DeferredI64 {
116+
type Options = EmptyOptions;
117+
118+
const ARG_NAMES: &'static [&'static str] = &["lhs", "rhs"];
119+
const INFALLIBLE: bool = false;
120+
121+
fn id(&self) -> ScalarFnId {
122+
static ID: CachedId = CachedId::new("bench.row_fn_output.deferred_i64");
123+
*ID
124+
}
125+
126+
fn dispatch<V: RowVisitor>(
127+
&self,
128+
_options: &Self::Options,
129+
_args: &[DType],
130+
visitor: V,
131+
) -> VortexResult<V::VisitResult> {
132+
visitor.visit_deferred::<(i64, i64), i64, bool>(
133+
|(lhs, rhs)| lhs.overflowing_add(rhs),
134+
|overflowed| {
135+
vortex_ensure!(
136+
!overflowed,
137+
"deferred-i64 benchmark inputs must not overflow"
138+
);
139+
Ok(())
140+
},
141+
)
142+
}
143+
}
144+
111145
#[vortex_bench_support::cpu_features]
112146
#[divan::bench(types = [i32, i64], args = INPUT_SHAPES)]
113147
fn infallible_bool<T: BenchPrimitive>(bencher: Bencher, &shape: &InputShape) {
114148
let function = InfallibleBool::<T>(PhantomData);
149+
bench_row_fn(bencher, &function, make_args::<T>(shape));
150+
}
151+
152+
#[vortex_bench_support::cpu_features]
153+
#[divan::bench(args = INPUT_SHAPES)]
154+
fn deferred_i64(bencher: Bencher, &shape: &InputShape) {
155+
bench_row_fn(bencher, &DeferredI64, make_args::<i64>(shape));
156+
}
157+
158+
fn make_args<T: BenchPrimitive>(shape: InputShape) -> VecExecutionArgs {
115159
let args = match shape {
116160
InputShape::PerRowPerRow => vec![T::per_row(0), T::per_row(1)],
117161
InputShape::PerRowConstant => vec![T::per_row(0), T::constant()],
118162
InputShape::ConstantPerRow => vec![T::constant(), T::per_row(1)],
119163
};
120-
let args = VecExecutionArgs::new(args, ROWS);
121164

165+
VecExecutionArgs::new(args, ROWS)
166+
}
167+
168+
fn bench_row_fn<F: RowFn<Options = EmptyOptions>>(
169+
bencher: Bencher,
170+
function: &F,
171+
args: VecExecutionArgs,
172+
) {
122173
bencher
123174
.counter(ItemsCount::new(ROWS))
124175
.with_inputs(|| (&args, SESSION.create_execution_ctx()))
125176
.bench_refs(|(args, ctx)| {
126-
execute_rows(&function, &EmptyOptions, *args, ctx)
177+
execute_rows(function, &EmptyOptions, *args, ctx)
127178
.vortex_expect("row execution should succeed in benchmark")
128179
});
129180
}

‎vortex-array/src/scalar_fn/unstable/row/batch/tests.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,20 @@ fn test_filter_and_scatter_preserves_runtime_sink_params(
760760
Ok(())
761761
}
762762

763+
#[test]
764+
fn test_deferred_owned_execution_handles_constant_lhs() -> VortexResult<()> {
765+
let lhs = ConstantArray::new(10_i64, 3).into_array();
766+
let rhs = PrimitiveArray::from_iter([1_i64, 2, 3]).into_array();
767+
let args = VecExecutionArgs::new(vec![lhs, rhs], 3);
768+
let mut ctx = array_session().create_execution_ctx();
769+
770+
let actual = execute_rows(&DeferredAdd::default(), &EmptyOptions, &args, &mut ctx)?;
771+
let expected = PrimitiveArray::from_iter([11_i64, 12, 13]).into_array();
772+
773+
assert_arrays_eq!(&actual, &expected, &mut ctx);
774+
Ok(())
775+
}
776+
763777
#[test]
764778
fn test_deferred_owned_execution_retries_null_row_failure() -> VortexResult<()> {
765779
let function = DeferredAdd::default();

‎vortex-array/src/scalar_fn/unstable/row/execute/owned.rs‎

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -177,44 +177,13 @@ where
177177
let mut values = Vec::<Out>::with_capacity(row_count);
178178
let output = &mut values.spare_capacity_mut()[..row_count];
179179

180-
let failure = if let Some(views) = Args::views_if_no_consts(&columns) {
181-
// Keep this validation beside the views so LLVM sees their common length here.
182-
vortex_ensure!(
183-
Args::view_lens_match(&views, row_count),
184-
"a decoded row input does not address exactly {row_count} rows",
185-
);
186-
187-
// SAFETY: `view_lens_match` checked that these exact retained views address `row_count`
188-
// rows.
189-
let source = unsafe { Args::indexed_source(views, row_count) };
190-
191-
source.map_checked_into(output, |elements| apply(&prepared, elements))
192-
} else {
193-
// Keep this proof branch-local. Shared validation prevents LLVM from specializing this
194-
// loop for each batch-constant arrangement, leaving it scalar under multiple CGUs without
195-
// LTO.
196-
vortex_ensure!(
197-
Args::decoded_lens_match(&columns, row_count),
198-
"a decoded row input does not address exactly {row_count} rows",
199-
);
200-
201-
let mut accumulated = Fail::default();
202-
203-
// Iterate over `output` directly. A `0..row_count` range reuses the address-taken value
204-
// from the validation error formatter and retains an output bounds check.
205-
for (index, slot) in output.iter_mut().enumerate() {
206-
// LLVM unswitches the batch-constant checks in `Args::get` before vectorizing the loop.
207-
let (value, row_failure) = apply(&prepared, Args::get(&columns, index));
208-
209-
slot.write(value);
210-
accumulated |= row_failure;
211-
}
212-
213-
accumulated
180+
let Some(source) = decoded_source::<Args>(&columns, row_count) else {
181+
vortex_bail!("a decoded row input does not address exactly {row_count} rows");
214182
};
183+
let failure = source.map_checked_into(output, |elements| apply(&prepared, elements));
215184

216-
// SAFETY: normal completion of either execution path initializes `0..row_count` exactly
217-
// once, and `values` was allocated with at least `row_count` capacity.
185+
// SAFETY: normal completion initializes `0..row_count` exactly once, and `values` was
186+
// allocated with at least `row_count` capacity.
218187
unsafe { values.set_len(row_count) };
219188

220189
// Defer rich error construction until after the row loop.

‎vortex-array/src/scalar_fn/unstable/row/types/element/input.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ pub unsafe trait InputElement: 'static {
9090
/// Read one row without repeating batch-constant work from [`decode`](Self::decode).
9191
fn get(column: &Self::Column, index: usize) -> Self::Elem<'_>;
9292

93-
/// Borrow the representation used when this argument varies within the batch.
93+
/// Borrow the representation used inside the row loop.
9494
///
95-
/// Called once before the hot loop. Constants do not use this view because the tuple adapter
96-
/// keeps their one-row decoded representation separate. For every index below the returned
97-
/// view's length, [`get_from_view`](Self::get_from_view) must produce the same element as
98-
/// [`get`](Self::get) on `column`.
95+
/// Executors call this before the hot loop. For every index below the returned view's length,
96+
/// [`get_from_view`](Self::get_from_view) must produce the same element as [`get`](Self::get) on
97+
/// `column`.
9998
fn view(column: &Self::Column) -> Self::View<'_>;
10099

101100
/// Read one row from a [`View`](Self::View).

‎vortex-array/src/scalar_fn/unstable/row/types/element/tuple/indexed.rs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ pub(in crate::scalar_fn::unstable::row) fn decoded_source<'a, Args: IndexedEleme
5151
/// the row loop.
5252
enum ArgColumnSource<'a, T: InputElement> {
5353
Rows(T::View<'a>),
54+
55+
/// A validated one-row view that logically addresses `row_count` rows.
5456
Constant {
55-
column: &'a T::Column,
57+
view: T::View<'a>,
5658
row_count: usize,
5759
},
5860
}
@@ -64,7 +66,10 @@ impl<'a, T: InputElement> ArgColumnSource<'a, T> {
6466
let view = T::view(column);
6567
(view.len() == row_count).then_some(Self::Rows(view))
6668
}
67-
ArgColumnKind::Const(column) => Some(Self::Constant { column, row_count }),
69+
ArgColumnKind::Const(column) => {
70+
let view = T::view(column);
71+
(view.len() == 1).then_some(Self::Constant { view, row_count })
72+
}
6873
}
6974
}
7075
}
@@ -86,8 +91,10 @@ impl<'a, T: InputElement> IndexedSource for ArgColumnSource<'a, T> {
8691
// caller guarantees that `index` is below the source length.
8792
unsafe { T::get_from_view_unchecked(view, index) }
8893
}
89-
// `ArgColumn::try_from_const` validated row zero when it constructed this column.
90-
Self::Constant { column, .. } => T::get(column, 0),
94+
Self::Constant { view, .. } => {
95+
// SAFETY: `try_new` checked that this exact retained view contains row zero.
96+
unsafe { T::get_from_view_unchecked(view, 0) }
97+
}
9198
}
9299
}
93100
}

0 commit comments

Comments
 (0)